@openade/pem
Version:
Punto di Emissione (Emission Point) - Device library for fiscal receipts
89 lines • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateDataMatrixContent = generateDataMatrixContent;
exports.generateCommercialDocumentPDF = generateCommercialDocumentPDF;
function generateDataMatrixContent(document, config) {
const content = {
Cau: config.cau,
Hash: config.documentHash,
Numero: document.datiGenerali.numero,
Data: document.datiGenerali.dataOra,
Importo: document.importoTotale.toFixed(2),
};
return JSON.stringify(content);
}
async function generateCommercialDocumentPDF(document, config) {
const dataMatrixJSON = generateDataMatrixContent(document, config);
const pdfContent = createTextReceipt(document, config, dataMatrixJSON);
return new TextEncoder().encode(pdfContent);
}
function createTextReceipt(document, config, dataMatrixJSON) {
const lines = [];
const width = 50;
lines.push('═'.repeat(width));
lines.push(centerText(document.contribuente.denominazione.toUpperCase(), width));
lines.push(centerText(`P.IVA ${document.contribuente.partitaIVA}`, width));
lines.push('');
lines.push(centerText('DOCUMENTO COMMERCIALE', width));
lines.push(centerText('di vendita o prestazione', width));
lines.push('═'.repeat(width));
lines.push('');
const dateTime = new Date(document.datiGenerali.dataOra).toLocaleString('it-IT');
lines.push(`Data: ${dateTime}`);
lines.push(`Doc. N.: ${document.datiGenerali.numero}`);
lines.push(`PEM: ${document.identificativoPEM}`);
lines.push('');
lines.push('DESCRIZIONE'.padEnd(30) + 'IVA'.padEnd(8) + 'PREZZO');
lines.push('─'.repeat(width));
for (const line of document.dettaglioLinee) {
const desc = truncate(line.descrizione, 30);
const vat = line.aliquotaIVA ? `${line.aliquotaIVA}%`.padEnd(8) : (line.natura || '').padEnd(8);
const price = `€${line.prezzoTotale.toFixed(2)}`.padStart(10);
lines.push(desc.padEnd(30) + vat + price);
if (line.quantita && line.quantita > 1) {
lines.push(` ${line.quantita} x €${line.prezzoUnitario.toFixed(2)}`);
}
}
lines.push('');
lines.push('─'.repeat(width));
for (const riepilogo of document.datiRiepilogo) {
const label = riepilogo.aliquotaIVA
? `IVA ${riepilogo.aliquotaIVA}%`
: `Esente (${riepilogo.natura})`;
lines.push(label.padEnd(28) +
`€${riepilogo.imponibile.toFixed(2)}`.padStart(10) +
`€${riepilogo.imposta.toFixed(2)}`.padStart(10));
}
lines.push('');
lines.push('═'.repeat(width));
lines.push('TOTALE'.padEnd(38) + `€${document.importoTotale.toFixed(2)}`.padStart(12));
lines.push('═'.repeat(width));
lines.push('');
if (document.datiPagamento && document.datiPagamento.length > 0) {
for (const payment of document.datiPagamento) {
lines.push(`Pagamento: ${payment.modalitaPagamento}`);
if (payment.importo) {
lines.push(`Importo: €${payment.importo.toFixed(2)}`);
}
}
lines.push('');
}
lines.push(`Matricola: ${config.deviceSerial}`);
lines.push(`Hash: ${config.documentHash.substring(0, 16)}...`);
lines.push('');
lines.push('Codice Bidimensionale (Data Matrix):');
lines.push(dataMatrixJSON);
lines.push('');
lines.push('═'.repeat(width));
lines.push(centerText('Arrivederci e Grazie!', width));
lines.push('═'.repeat(width));
return lines.join('\n');
}
function centerText(text, width) {
const padding = Math.max(0, Math.floor((width - text.length) / 2));
return ' '.repeat(padding) + text;
}
function truncate(text, maxLength) {
return text.length > maxLength ? text.substring(0, maxLength - 3) + '...' : text;
}
//# sourceMappingURL=document.pdf.js.map