@@ -238,9 +265,8 @@ const AddCompany = () => {
control={control}
errors={errors}
config={{
- required: __('È obbligatorio', 'gepafin'),
validate: {
- isPIVA
+ isPIVA: (value) => !value || isPIVA(value)
}
}}
/>
diff --git a/src/pages/ProfileCompany/index.js b/src/pages/ProfileCompany/index.js
index 18a7a3a..eecd0c1 100644
--- a/src/pages/ProfileCompany/index.js
+++ b/src/pages/ProfileCompany/index.js
@@ -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 = () => {
value === originalVatNumber.current || isPIVA(value)
+ }
+ }}
/>
{
{__('Azioni', 'gepafin')}
+
+