@openade/fe
Version:
Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)
96 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EUVATService = void 0;
const http_service_1 = require("./http.service");
class EUVATService {
constructor() {
this.viesEndpoint = 'https://ec.europa.eu/taxation_customs/vies/rest-api/ms/';
this.httpService = new http_service_1.HttpService();
}
async validateVATNumber(vatNumber, countryCode) {
try {
const cleanVatNumber = vatNumber.replace(/\s/g, '').toUpperCase();
if (!this.isValidVATFormat(cleanVatNumber, countryCode)) {
return {
valid: false,
validationDate: new Date().toISOString(),
error: 'Invalid VAT number format',
};
}
const response = await this.httpService.get(`${this.viesEndpoint}${countryCode}/vat/${cleanVatNumber}`);
if (response.valid) {
return {
valid: true,
companyName: response.name,
companyAddress: response.address,
validationDate: new Date().toISOString(),
};
}
else {
return {
valid: false,
validationDate: new Date().toISOString(),
error: 'VAT number not found in VIES database',
};
}
}
catch (error) {
return {
valid: false,
validationDate: new Date().toISOString(),
error: error instanceof Error ? error.message : 'Validation service error',
};
}
}
isValidVATFormat(vatNumber, countryCode) {
const patterns = {
AT: /^ATU\d{8}$/,
BE: /^BE[0-1]\d{9}$/,
BG: /^BG\d{9,10}$/,
CY: /^CY\d{8}[A-Z]$/,
CZ: /^CZ\d{8,10}$/,
DE: /^DE\d{9}$/,
DK: /^DK\d{8}$/,
EE: /^EE\d{9}$/,
EL: /^EL\d{9}$/,
ES: /^ES[A-Z0-9]\d{7}[A-Z0-9]$/,
FI: /^FI\d{8}$/,
FR: /^FR[A-HJ-NP-Z0-9]{2}\d{9}$/,
HR: /^HR\d{11}$/,
HU: /^HU\d{8}$/,
IE: /^IE\d[A-Z0-9+*]\d{5}[A-Z]$/,
IT: /^IT\d{11}$/,
LT: /^LT(\d{9}|\d{12})$/,
LU: /^LU\d{8}$/,
LV: /^LV\d{11}$/,
MT: /^MT\d{8}$/,
NL: /^NL\d{9}B\d{2}$/,
PL: /^PL\d{10}$/,
PT: /^PT\d{9}$/,
RO: /^RO\d{2,10}$/,
SE: /^SE\d{12}$/,
SI: /^SI\d{8}$/,
SK: /^SK\d{10}$/,
};
const pattern = patterns[countryCode.toUpperCase()];
return pattern ? pattern.test(vatNumber) : false;
}
extractCountryCode(vatNumber) {
const match = vatNumber.match(/^([A-Z]{2})/);
return match ? match[1] : null;
}
formatVATNumber(vatNumber) {
const cleanVatNumber = vatNumber.replace(/\s/g, '').toUpperCase();
const countryCode = this.extractCountryCode(cleanVatNumber);
if (!countryCode) {
return vatNumber;
}
const numberPart = cleanVatNumber.substring(2);
if (numberPart.length > 4) {
return `${countryCode} ${numberPart.substring(0, 4)} ${numberPart.substring(4)}`;
}
return `${countryCode} ${numberPart}`;
}
}
exports.EUVATService = EUVATService;
//# sourceMappingURL=euvat.service.js.map