- 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 { useForm } from 'react-hook-form';
|
||||
import BlockingOverlay from '../../components/BlockingOverlay';
|
||||
import { ConfirmDialog } from 'primereact/confirmdialog';
|
||||
|
||||
// api
|
||||
import CompanyService from '../../service/company-service';
|
||||
@@ -30,6 +31,8 @@ const AddCompany = () => {
|
||||
const infoMsgs = useRef(null);
|
||||
const [, debouncedPivaValue, setInputPiva] = useDebounce('', 1000);
|
||||
const [vatCheckResponse, setVatCheckResponse] = useState({});
|
||||
const [visibleConfirmation, setVisibleConfirmation] = useState(false);
|
||||
const [pendingFormData, setPendingFormData] = useState(null);
|
||||
|
||||
const {
|
||||
control,
|
||||
@@ -60,14 +63,25 @@ const AddCompany = () => {
|
||||
}
|
||||
|
||||
const onSubmit = (formData) => {
|
||||
if (!formData.vatNumber) {
|
||||
setPendingFormData({ ...formData, vatCheckResponse });
|
||||
setVisibleConfirmation(true);
|
||||
} else {
|
||||
infoMsgs.current.clear();
|
||||
storeSet('setAsyncRequest');
|
||||
CompanyService.createCompany({ ...formData, vatCheckResponse }, updateCallback, updateError);
|
||||
}
|
||||
};
|
||||
|
||||
const acceptConstituting = () => {
|
||||
infoMsgs.current.clear();
|
||||
storeSet('setAsyncRequest');
|
||||
const submitData = {
|
||||
...formData,
|
||||
vatCheckResponse
|
||||
}
|
||||
CompanyService.createCompany(pendingFormData, updateCallback, updateError);
|
||||
setPendingFormData(null);
|
||||
};
|
||||
|
||||
CompanyService.createCompany(submitData, updateCallback, updateError);
|
||||
const rejectConstituting = () => {
|
||||
setPendingFormData(null);
|
||||
};
|
||||
|
||||
const updateCallback = (data) => {
|
||||
@@ -192,6 +206,19 @@ const AddCompany = () => {
|
||||
<div className="appPage__spacer"></div>
|
||||
<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)}>
|
||||
<BlockingOverlay shouldDisplay={isAsyncRequest}/>
|
||||
<div className="appPageSection">
|
||||
@@ -238,9 +265,8 @@ const AddCompany = () => {
|
||||
control={control}
|
||||
errors={errors}
|
||||
config={{
|
||||
required: __('È obbligatorio', 'gepafin'),
|
||||
validate: {
|
||||
isPIVA
|
||||
isPIVA: (value) => !value || isPIVA(value)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -39,6 +39,9 @@ const ProfileCompany = () => {
|
||||
const companies = useStoreValue('companies');
|
||||
const infoMsgs = useRef(null);
|
||||
const [formInitialData, setFormInitialData] = useState({});
|
||||
const [validVat, setValidVat] = useState(true);
|
||||
const [vatCheckResponse, setVatCheckResponse] = useState({});
|
||||
const originalVatNumber = useRef('');
|
||||
const [delegaData, setDelegaData] = useState({});
|
||||
const navigate = useNavigate();
|
||||
const [delega, setDelega] = useState([]);
|
||||
@@ -53,7 +56,8 @@ const ProfileCompany = () => {
|
||||
formState: { errors },
|
||||
setValue,
|
||||
watch,
|
||||
reset
|
||||
reset,
|
||||
trigger
|
||||
} = useForm({
|
||||
defaultValues: useMemo(() => {
|
||||
return formInitialData;
|
||||
@@ -79,7 +83,7 @@ const ProfileCompany = () => {
|
||||
infoMsgs.current.clear();
|
||||
storeSet('setAsyncRequest');
|
||||
|
||||
CompanyService.updateCompany(formData.id, formData, updateCallback, updateError);
|
||||
CompanyService.updateCompany(formData.id, { ...formData, vatCheckResponse }, updateCallback, updateError);
|
||||
};
|
||||
|
||||
const updateCallback = (data) => {
|
||||
@@ -115,46 +119,78 @@ const ProfileCompany = () => {
|
||||
}
|
||||
|
||||
const checkVatNumber = (e) => {
|
||||
const value = e.target.value;
|
||||
infoMsgs.current.clear();
|
||||
const isValid = isPIVA(e.target.value);
|
||||
|
||||
if (isValid) {
|
||||
if (isPIVA(value)) {
|
||||
storeSet('setAsyncRequest');
|
||||
CompanyService.checkVat(checkVatCallback, errCheckVatCallback, [['vatNumber', e.target.value]])
|
||||
} else {
|
||||
setEmptyValues();
|
||||
CompanyService.checkVat(checkVatCallback, errCheckVatCallback, [['vatNumber', value]]);
|
||||
} else if (value !== originalVatNumber.current) {
|
||||
setValue('vatNumber', originalVatNumber.current);
|
||||
}
|
||||
}
|
||||
|
||||
const checkVatCallback = (data) => {
|
||||
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)) {
|
||||
const {
|
||||
cap, cf, denominazione, piva, indirizzo, comune, dettaglio: { pec }
|
||||
} = resp;
|
||||
let formData = {};
|
||||
|
||||
const formData = {
|
||||
cap,
|
||||
pec,
|
||||
email: pec,
|
||||
city: comune,
|
||||
codiceFiscale: cf,
|
||||
address: indirizzo,
|
||||
vatNumber: piva,
|
||||
companyName: denominazione
|
||||
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 {
|
||||
cap, cf, denominazione, piva, indirizzo, comune, dettaglio: { pec }
|
||||
} = resp;
|
||||
|
||||
formData = {
|
||||
cap,
|
||||
pec,
|
||||
email: pec,
|
||||
city: comune,
|
||||
codiceFiscale: cf ? cf : piva,
|
||||
address: indirizzo,
|
||||
vatNumber: piva,
|
||||
companyName: denominazione
|
||||
}
|
||||
}
|
||||
Object.keys(formData).map(k => setValue(k, formData[k]));
|
||||
setVatCheckResponse(data.data.vatCheckResponse);
|
||||
} else {
|
||||
setValue('vatNumber', originalVatNumber.current);
|
||||
}
|
||||
//setData(getFormattedBandiData(data.data));
|
||||
} else {
|
||||
setEmptyValues();
|
||||
setValue('vatNumber', originalVatNumber.current);
|
||||
}
|
||||
trigger();
|
||||
storeSet('unsetAsyncRequest');
|
||||
}
|
||||
|
||||
const errCheckVatCallback = (data) => {
|
||||
setEmptyValues();
|
||||
setValue('vatNumber', originalVatNumber.current);
|
||||
set404FromErrorResponse(data);
|
||||
storeSet('unsetAsyncRequest');
|
||||
}
|
||||
@@ -323,7 +359,7 @@ const ProfileCompany = () => {
|
||||
|
||||
const {
|
||||
cap, codiceFiscale, companyName, vatNumber, address, city, pec, email,
|
||||
contactName, contactEmail, isLegalRepresentant, id
|
||||
contactName, contactEmail, isLegalRepresentant, id, validVat: companyValidVat
|
||||
} = chosenCompany;
|
||||
const companyData = {
|
||||
id,
|
||||
@@ -341,6 +377,8 @@ const ProfileCompany = () => {
|
||||
}
|
||||
|
||||
setFormInitialData(companyData);
|
||||
setValidVat(!!companyValidVat);
|
||||
originalVatNumber.current = vatNumber || '';
|
||||
}, [chosenCompanyId, companies]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -382,12 +420,17 @@ const ProfileCompany = () => {
|
||||
<FormField
|
||||
type="textinput"
|
||||
fieldName="vatNumber"
|
||||
disabled={true}
|
||||
disabled={validVat}
|
||||
label={__('P.IVA', 'gepafin')}
|
||||
onBlurFn={checkVatNumber}
|
||||
control={control}
|
||||
errors={errors}
|
||||
config={{ required: __('È obbligatorio', 'gepafin') }}
|
||||
config={{
|
||||
required: __('È obbligatorio', 'gepafin'),
|
||||
validate: {
|
||||
isPIVA: (value) => value === originalVatNumber.current || isPIVA(value)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
@@ -597,6 +640,8 @@ const ProfileCompany = () => {
|
||||
<span>{__('Azioni', 'gepafin')}</span>
|
||||
</div>
|
||||
|
||||
<div className="appPage__spacer"></div>
|
||||
|
||||
<div className="appPageSection">
|
||||
<div className="appPageSection__actions">
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user