@openade/fe
Version:
Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)
179 lines • 6.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReceiptHandler = void 0;
exports.parseReceiptType = parseReceiptType;
exports.parseReceiptFilename = parseReceiptFilename;
const fast_xml_parser_1 = require("fast-xml-parser");
function parseReceiptType(filename) {
const match = filename.match(/_([A-Z]{2})_/);
if (!match)
return null;
const type = match[1];
if (['RC', 'NS', 'MC', 'NE', 'MT', 'DT'].includes(type)) {
return type;
}
return null;
}
function parseReceiptFilename(filename) {
const pattern = /^([A-Z]{2})([^_]+)_([^_]+)_([A-Z]{2})_(\d+)\.xml$/;
const match = filename.match(pattern);
if (!match) {
return {};
}
return {
idPaese: match[1],
idCodice: match[2],
progressivoInvio: match[3],
tipoFile: match[4],
identificativoSdI: match[5],
};
}
class ReceiptHandler {
constructor() {
this.parser = new fast_xml_parser_1.XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@_',
});
}
parseReceipt(xml, type) {
try {
const parsed = this.parser.parse(xml);
if (!type) {
if (parsed.RicevutaConsegna)
type = 'RC';
else if (parsed.NotificaScarto)
type = 'NS';
else if (parsed.NotificaMancataConsegna)
type = 'MC';
else if (parsed.NotificaEsito)
type = 'NE';
else
return null;
}
switch (type) {
case 'RC':
return this.parseDeliveryReceipt(parsed);
case 'NS':
return this.parseRejectionNotice(parsed);
case 'MC':
return this.parseUndeliveredNotice(parsed);
case 'NE':
return this.parseOutcomeNotice(parsed);
default:
return null;
}
}
catch (error) {
console.error('Failed to parse receipt:', error);
return null;
}
}
parseDeliveryReceipt(parsed) {
const data = parsed.RicevutaConsegna || parsed['ns2:RicevutaConsegna'];
if (!data)
return null;
return {
identifcativoSdI: data.IdentificativoSdI,
nomeFile: data.NomeFile,
hash: data.Hash,
dataOraRicezione: data.DataOraRicezione,
dataOraConsegna: data.DataOraConsegna,
destinatario: {
codice: data.Destinatario?.Codice,
descrizione: data.Destinatario?.Descrizione,
},
...(data.MessageIdCommittente && {
messaggioPEC: { identificativo: data.MessageIdCommittente },
}),
};
}
parseRejectionNotice(parsed) {
const data = parsed.NotificaScarto || parsed['ns2:NotificaScarto'];
if (!data)
return null;
const listaErrori = Array.isArray(data.ListaErrori?.Errore)
? data.ListaErrori.Errore
: data.ListaErrori?.Errore
? [data.ListaErrori.Errore]
: [];
return {
identifcativoSdI: data.IdentificativoSdI,
nomeFile: data.NomeFile,
hash: data.Hash,
dataOraRicezione: data.DataOraRicezione,
...(data.RiferimentoFattura && {
riferimentoFattura: {
numeroFattura: data.RiferimentoFattura.NumeroFattura,
annoFattura: data.RiferimentoFattura.AnnoFattura,
posizione: data.RiferimentoFattura.PosizioneFattura,
},
}),
listaErrori: listaErrori.map((err) => ({
errore: {
codice: err.Codice,
descrizione: err.Descrizione,
suggerimento: err.Suggerimento,
},
})),
...(data.MessageIdCommittente && {
messaggioPEC: { identificativo: data.MessageIdCommittente },
}),
};
}
parseUndeliveredNotice(parsed) {
const data = parsed.NotificaMancataConsegna || parsed['ns2:NotificaMancataConsegna'];
if (!data)
return null;
return {
identifcativoSdI: data.IdentificativoSdI,
nomeFile: data.NomeFile,
hash: data.Hash,
dataOraRicezione: data.DataOraRicezione,
descrizione: data.Descrizione,
...(data.MessageIdCommittente && {
messaggioPEC: { identificativo: data.MessageIdCommittente },
}),
};
}
parseOutcomeNotice(parsed) {
const data = parsed.NotificaEsito || parsed['ns2:NotificaEsito'];
if (!data)
return null;
return {
identifcativoSdI: data.IdentificativoSdI,
riferimentoFattura: {
numeroFattura: data.RiferimentoFattura.NumeroFattura,
annoFattura: data.RiferimentoFattura.AnnoFattura,
posizione: data.RiferimentoFattura.PosizioneFattura,
},
esito: data.Esito,
descrizione: data.Descrizione,
messageIdCommittente: data.MessageIdCommittente,
pecCommittente: data.PecCommittente,
dataOraRicezione: data.DataOraRicezione,
};
}
isSuccessReceipt(receipt) {
if ('dataOraConsegna' in receipt) {
return true;
}
if ('esito' in receipt) {
return receipt.esito === 'EC01';
}
return false;
}
getErrors(receipt) {
if ('listaErrori' in receipt) {
return receipt.listaErrori.map((e) => `${e.errore.codice}: ${e.errore.descrizione}`);
}
if ('descrizione' in receipt && typeof receipt.descrizione === 'string') {
return [receipt.descrizione];
}
if ('esito' in receipt && receipt.esito === 'EC02') {
return [receipt.descrizione || 'Invoice rejected by customer'];
}
return [];
}
}
exports.ReceiptHandler = ReceiptHandler;
//# sourceMappingURL=receipt.handler.js.map