UNPKG

@openade/common

Version:

Common types, validators, and XML builders for Italian fiscal receipts

107 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatISODate = formatISODate; exports.formatISODateTime = formatISODateTime; exports.calculateVAT = calculateVAT; exports.calculateTotal = calculateTotal; exports.roundAmount = roundAmount; exports.normalizePartitaIVA = normalizePartitaIVA; exports.generatePEMId = generatePEMId; exports.generateDocumentNumber = generateDocumentNumber; exports.parseBirthDateFromCodiceFiscale = parseBirthDateFromCodiceFiscale; exports.formatAmount = formatAmount; exports.isPastDate = isPastDate; exports.isToday = isToday; function formatISODate(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } function formatISODateTime(date) { const dateStr = formatISODate(date); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); return `${dateStr}T${hours}:${minutes}:${seconds}`; } function calculateVAT(imponibile, aliquotaIVA) { const vat = (imponibile * aliquotaIVA) / 100; return Math.round(vat * 100) / 100; } function calculateTotal(imponibile, imposta) { return Math.round((imponibile + imposta) * 100) / 100; } function roundAmount(amount) { return Math.round(amount * 100) / 100; } function normalizePartitaIVA(partitaIVA) { const cleaned = partitaIVA.replace(/\s/g, '').toUpperCase(); if (cleaned.startsWith('IT')) { return cleaned; } if (/^\d{11}$/.test(cleaned)) { return `IT${cleaned}`; } return cleaned; } function generatePEMId() { const timestamp = Date.now(); const random = Math.floor(Math.random() * 10000) .toString() .padStart(4, '0'); return `PEM-${timestamp}-${random}`; } function generateDocumentNumber(sequential, year) { const y = year || new Date().getFullYear(); const seq = String(sequential).padStart(6, '0'); return `${y}-${seq}`; } function parseBirthDateFromCodiceFiscale(codiceFiscale) { if (codiceFiscale.length !== 16) { return null; } const yearChars = codiceFiscale.substring(6, 8); const monthChar = codiceFiscale.charAt(8); const dayChars = codiceFiscale.substring(9, 11); const monthMap = { A: 1, B: 2, C: 3, D: 4, E: 5, H: 6, L: 7, M: 8, P: 9, R: 10, S: 11, T: 12, }; const month = monthMap[monthChar]; if (!month) { return null; } let year = parseInt(yearChars, 10); const currentYear = new Date().getFullYear() % 100; year += year > currentYear ? 1900 : 2000; let day = parseInt(dayChars, 10); if (day > 40) { day -= 40; } return new Date(year, month - 1, day); } function formatAmount(amount, currency = '€') { const formatted = amount.toFixed(2).replace('.', ','); return `${currency} ${formatted}`; } function isPastDate(date) { return date.getTime() < Date.now(); } function isToday(date) { const today = new Date(); return (date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth() && date.getDate() === today.getDate()); } //# sourceMappingURL=helpers.js.map