UNPKG

@fin.cx/einvoice

Version:

A TypeScript module for creating, manipulating, and embedding XML data within PDF files specifically tailored for electronic invoice (einvoice) packages.

65 lines (57 loc) 1.98 kB
import { BaseEncoder } from '../base/base.encoder.js'; import type { TInvoice, TCreditNote, TDebitNote } from '../../interfaces/common.js'; import { CII_NAMESPACES, CIIProfile } from './cii.types.js'; /** * Base encoder for CII-based invoice formats */ export abstract class CIIBaseEncoder extends BaseEncoder { protected profile: CIIProfile = CIIProfile.EN16931; /** * Sets the CII profile to use for encoding * @param profile CII profile */ public setProfile(profile: CIIProfile): void { this.profile = profile; } /** * Encodes a TInvoice object into CII XML * @param invoice TInvoice object to encode * @returns CII XML string */ public async encode(invoice: TInvoice): Promise<string> { // TInvoice is always an invoice, treat it as debit note for encoding return this.encodeDebitNote(invoice as unknown as TDebitNote); } /** * Encodes a TCreditNote object into CII XML * @param creditNote TCreditNote object to encode * @returns CII XML string */ protected abstract encodeCreditNote(creditNote: TCreditNote): Promise<string>; /** * Encodes a TDebitNote object into CII XML * @param debitNote TDebitNote object to encode * @returns CII XML string */ protected abstract encodeDebitNote(debitNote: TDebitNote): Promise<string>; /** * Creates the XML declaration and root element * @returns XML string with declaration and root element */ protected createXmlRoot(): string { return `<?xml version="1.0" encoding="UTF-8"?> <rsm:CrossIndustryInvoice xmlns:rsm="${CII_NAMESPACES.RSM}" xmlns:ram="${CII_NAMESPACES.RAM}" xmlns:udt="${CII_NAMESPACES.UDT}"> </rsm:CrossIndustryInvoice>`; } /** * Formats a date as an ISO string (YYYY-MM-DD) * @param timestamp Timestamp to format * @returns Formatted date string */ protected formatDate(timestamp: number): string { const date = new Date(timestamp); return date.toISOString().split('T')[0]; } }