- added flow for company creation without vat number;
This commit is contained in:
@@ -14,6 +14,7 @@ import FormField from '../../components/FormField';
|
|||||||
import { Button } from 'primereact/button';
|
import { Button } from 'primereact/button';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import BlockingOverlay from '../../components/BlockingOverlay';
|
import BlockingOverlay from '../../components/BlockingOverlay';
|
||||||
|
import { ConfirmDialog } from 'primereact/confirmdialog';
|
||||||
|
|
||||||
// api
|
// api
|
||||||
import CompanyService from '../../service/company-service';
|
import CompanyService from '../../service/company-service';
|
||||||
@@ -30,6 +31,8 @@ const AddCompany = () => {
|
|||||||
const infoMsgs = useRef(null);
|
const infoMsgs = useRef(null);
|
||||||
const [, debouncedPivaValue, setInputPiva] = useDebounce('', 1000);
|
const [, debouncedPivaValue, setInputPiva] = useDebounce('', 1000);
|
||||||
const [vatCheckResponse, setVatCheckResponse] = useState({});
|
const [vatCheckResponse, setVatCheckResponse] = useState({});
|
||||||
|
const [visibleConfirmation, setVisibleConfirmation] = useState(false);
|
||||||
|
const [pendingFormData, setPendingFormData] = useState(null);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
@@ -60,14 +63,25 @@ const AddCompany = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const onSubmit = (formData) => {
|
const onSubmit = (formData) => {
|
||||||
|
if (!formData.vatNumber) {
|
||||||
|
setPendingFormData({ ...formData, vatCheckResponse });
|
||||||
|
setVisibleConfirmation(true);
|
||||||
|
} else {
|
||||||
infoMsgs.current.clear();
|
infoMsgs.current.clear();
|
||||||
storeSet('setAsyncRequest');
|
storeSet('setAsyncRequest');
|
||||||
const submitData = {
|
CompanyService.createCompany({ ...formData, vatCheckResponse }, updateCallback, updateError);
|
||||||
...formData,
|
|
||||||
vatCheckResponse
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
CompanyService.createCompany(submitData, updateCallback, updateError);
|
const acceptConstituting = () => {
|
||||||
|
infoMsgs.current.clear();
|
||||||
|
storeSet('setAsyncRequest');
|
||||||
|
CompanyService.createCompany(pendingFormData, updateCallback, updateError);
|
||||||
|
setPendingFormData(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const rejectConstituting = () => {
|
||||||
|
setPendingFormData(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateCallback = (data) => {
|
const updateCallback = (data) => {
|
||||||
@@ -192,6 +206,19 @@ const AddCompany = () => {
|
|||||||
<div className="appPage__spacer"></div>
|
<div className="appPage__spacer"></div>
|
||||||
<Messages ref={infoMsgs}/>
|
<Messages ref={infoMsgs}/>
|
||||||
|
|
||||||
|
<ConfirmDialog
|
||||||
|
group="declarative"
|
||||||
|
visible={visibleConfirmation}
|
||||||
|
onHide={() => setVisibleConfirmation(false)}
|
||||||
|
message={__('Confermi che l\'azienda è una costituenda?', 'gepafin')}
|
||||||
|
header={__('Conferma', 'gepafin')}
|
||||||
|
icon="pi pi-exclamation-triangle"
|
||||||
|
accept={acceptConstituting}
|
||||||
|
acceptLabel={__('Si', 'gepafin')}
|
||||||
|
reject={rejectConstituting}
|
||||||
|
style={{ maxWidth: '500px' }}
|
||||||
|
/>
|
||||||
|
|
||||||
<form id="companyForm" className="appForm" onSubmit={handleSubmit(onSubmit)}>
|
<form id="companyForm" className="appForm" onSubmit={handleSubmit(onSubmit)}>
|
||||||
<BlockingOverlay shouldDisplay={isAsyncRequest}/>
|
<BlockingOverlay shouldDisplay={isAsyncRequest}/>
|
||||||
<div className="appPageSection">
|
<div className="appPageSection">
|
||||||
@@ -238,9 +265,8 @@ const AddCompany = () => {
|
|||||||
control={control}
|
control={control}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
config={{
|
config={{
|
||||||
required: __('È obbligatorio', 'gepafin'),
|
|
||||||
validate: {
|
validate: {
|
||||||
isPIVA
|
isPIVA: (value) => !value || isPIVA(value)
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ const ProfileCompany = () => {
|
|||||||
const companies = useStoreValue('companies');
|
const companies = useStoreValue('companies');
|
||||||
const infoMsgs = useRef(null);
|
const infoMsgs = useRef(null);
|
||||||
const [formInitialData, setFormInitialData] = useState({});
|
const [formInitialData, setFormInitialData] = useState({});
|
||||||
|
const [validVat, setValidVat] = useState(true);
|
||||||
|
const [vatCheckResponse, setVatCheckResponse] = useState({});
|
||||||
|
const originalVatNumber = useRef('');
|
||||||
const [delegaData, setDelegaData] = useState({});
|
const [delegaData, setDelegaData] = useState({});
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [delega, setDelega] = useState([]);
|
const [delega, setDelega] = useState([]);
|
||||||
@@ -53,7 +56,8 @@ const ProfileCompany = () => {
|
|||||||
formState: { errors },
|
formState: { errors },
|
||||||
setValue,
|
setValue,
|
||||||
watch,
|
watch,
|
||||||
reset
|
reset,
|
||||||
|
trigger
|
||||||
} = useForm({
|
} = useForm({
|
||||||
defaultValues: useMemo(() => {
|
defaultValues: useMemo(() => {
|
||||||
return formInitialData;
|
return formInitialData;
|
||||||
@@ -79,7 +83,7 @@ const ProfileCompany = () => {
|
|||||||
infoMsgs.current.clear();
|
infoMsgs.current.clear();
|
||||||
storeSet('setAsyncRequest');
|
storeSet('setAsyncRequest');
|
||||||
|
|
||||||
CompanyService.updateCompany(formData.id, formData, updateCallback, updateError);
|
CompanyService.updateCompany(formData.id, { ...formData, vatCheckResponse }, updateCallback, updateError);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateCallback = (data) => {
|
const updateCallback = (data) => {
|
||||||
@@ -115,46 +119,78 @@ const ProfileCompany = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const checkVatNumber = (e) => {
|
const checkVatNumber = (e) => {
|
||||||
|
const value = e.target.value;
|
||||||
infoMsgs.current.clear();
|
infoMsgs.current.clear();
|
||||||
const isValid = isPIVA(e.target.value);
|
|
||||||
|
|
||||||
if (isValid) {
|
if (isPIVA(value)) {
|
||||||
storeSet('setAsyncRequest');
|
storeSet('setAsyncRequest');
|
||||||
CompanyService.checkVat(checkVatCallback, errCheckVatCallback, [['vatNumber', e.target.value]])
|
CompanyService.checkVat(checkVatCallback, errCheckVatCallback, [['vatNumber', value]]);
|
||||||
} else {
|
} else if (value !== originalVatNumber.current) {
|
||||||
setEmptyValues();
|
setValue('vatNumber', originalVatNumber.current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkVatCallback = (data) => {
|
const checkVatCallback = (data) => {
|
||||||
if (data.status === 'SUCCESS') {
|
if (data.status === 'SUCCESS') {
|
||||||
const resp = data.data.data;
|
if (data.data.valid === false) {
|
||||||
|
setValue('vatNumber', originalVatNumber.current);
|
||||||
|
if (data.data.message && infoMsgs.current) {
|
||||||
|
infoMsgs.current.show({ severity: 'error', summary: '', detail: data.data.message, sticky: true });
|
||||||
|
}
|
||||||
|
trigger();
|
||||||
|
storeSet('unsetAsyncRequest');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const version = data.data.version;
|
||||||
|
const resp = data.data.vatCheckResponse.data;
|
||||||
if (!isEmpty(resp)) {
|
if (!isEmpty(resp)) {
|
||||||
|
let formData = {};
|
||||||
|
|
||||||
|
if (version === 'V2') {
|
||||||
|
const firstItem = resp[0];
|
||||||
|
const { taxCode, vatCode, address, companyName, pec } = firstItem;
|
||||||
|
const { streetName, zipCode, town } = address?.registeredOffice;
|
||||||
|
|
||||||
|
formData = {
|
||||||
|
cap: zipCode,
|
||||||
|
pec,
|
||||||
|
email: pec,
|
||||||
|
city: town,
|
||||||
|
codiceFiscale: taxCode ? taxCode : vatCode,
|
||||||
|
address: streetName,
|
||||||
|
vatNumber: vatCode,
|
||||||
|
companyName
|
||||||
|
}
|
||||||
|
} else {
|
||||||
const {
|
const {
|
||||||
cap, cf, denominazione, piva, indirizzo, comune, dettaglio: { pec }
|
cap, cf, denominazione, piva, indirizzo, comune, dettaglio: { pec }
|
||||||
} = resp;
|
} = resp;
|
||||||
|
|
||||||
const formData = {
|
formData = {
|
||||||
cap,
|
cap,
|
||||||
pec,
|
pec,
|
||||||
email: pec,
|
email: pec,
|
||||||
city: comune,
|
city: comune,
|
||||||
codiceFiscale: cf,
|
codiceFiscale: cf ? cf : piva,
|
||||||
address: indirizzo,
|
address: indirizzo,
|
||||||
vatNumber: piva,
|
vatNumber: piva,
|
||||||
companyName: denominazione
|
companyName: denominazione
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Object.keys(formData).map(k => setValue(k, formData[k]));
|
Object.keys(formData).map(k => setValue(k, formData[k]));
|
||||||
}
|
setVatCheckResponse(data.data.vatCheckResponse);
|
||||||
//setData(getFormattedBandiData(data.data));
|
|
||||||
} else {
|
} else {
|
||||||
setEmptyValues();
|
setValue('vatNumber', originalVatNumber.current);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setValue('vatNumber', originalVatNumber.current);
|
||||||
|
}
|
||||||
|
trigger();
|
||||||
storeSet('unsetAsyncRequest');
|
storeSet('unsetAsyncRequest');
|
||||||
}
|
}
|
||||||
|
|
||||||
const errCheckVatCallback = (data) => {
|
const errCheckVatCallback = (data) => {
|
||||||
setEmptyValues();
|
setValue('vatNumber', originalVatNumber.current);
|
||||||
set404FromErrorResponse(data);
|
set404FromErrorResponse(data);
|
||||||
storeSet('unsetAsyncRequest');
|
storeSet('unsetAsyncRequest');
|
||||||
}
|
}
|
||||||
@@ -323,7 +359,7 @@ const ProfileCompany = () => {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
cap, codiceFiscale, companyName, vatNumber, address, city, pec, email,
|
cap, codiceFiscale, companyName, vatNumber, address, city, pec, email,
|
||||||
contactName, contactEmail, isLegalRepresentant, id
|
contactName, contactEmail, isLegalRepresentant, id, validVat: companyValidVat
|
||||||
} = chosenCompany;
|
} = chosenCompany;
|
||||||
const companyData = {
|
const companyData = {
|
||||||
id,
|
id,
|
||||||
@@ -341,6 +377,8 @@ const ProfileCompany = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setFormInitialData(companyData);
|
setFormInitialData(companyData);
|
||||||
|
setValidVat(!!companyValidVat);
|
||||||
|
originalVatNumber.current = vatNumber || '';
|
||||||
}, [chosenCompanyId, companies]);
|
}, [chosenCompanyId, companies]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -382,12 +420,17 @@ const ProfileCompany = () => {
|
|||||||
<FormField
|
<FormField
|
||||||
type="textinput"
|
type="textinput"
|
||||||
fieldName="vatNumber"
|
fieldName="vatNumber"
|
||||||
disabled={true}
|
disabled={validVat}
|
||||||
label={__('P.IVA', 'gepafin')}
|
label={__('P.IVA', 'gepafin')}
|
||||||
onBlurFn={checkVatNumber}
|
onBlurFn={checkVatNumber}
|
||||||
control={control}
|
control={control}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
config={{ required: __('È obbligatorio', 'gepafin') }}
|
config={{
|
||||||
|
required: __('È obbligatorio', 'gepafin'),
|
||||||
|
validate: {
|
||||||
|
isPIVA: (value) => value === originalVatNumber.current || isPIVA(value)
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
@@ -597,6 +640,8 @@ const ProfileCompany = () => {
|
|||||||
<span>{__('Azioni', 'gepafin')}</span>
|
<span>{__('Azioni', 'gepafin')}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="appPage__spacer"></div>
|
||||||
|
|
||||||
<div className="appPageSection">
|
<div className="appPageSection">
|
||||||
<div className="appPageSection__actions">
|
<div className="appPageSection__actions">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Reference in New Issue
Block a user