import {
Invoice,
InvoiceLine,
Item,
Price,
TaxTotal,
TaxSubtotal,
TaxCategory,
TaxScheme,
ClassifiedTaxCategory,
LegalMonetaryTotal,
SellersItemIdentification,
} from 'ubl-builder';
const invoiceOptions = {
enviroment: '2',
issuer: {
prefix: 'INV',
resolutionNumber: '321654987',
startDate: '2024-01-01',
endDate: '2025-12-31',
startRange: '1000',
endRange: '5000',
technicalKey: 'tech-key-123',
},
software: {
id: 'soft-123',
pin: '123456789',
providerNit: '900123456-1',
},
};
const invoice = new Invoice('INV-002', invoiceOptions);
// Set basic fields
invoice.setDefaultProperties();
invoice.setID('INV-002');
invoice.setUBLVersionID('UBL 2.1');
invoice.setIssueDate('2026-03-06');
invoice.setIssueTime('10:30:00-05:00');
invoice.setInvoiceTypeCode('01');
invoice.setDocumentCurrencyCode('USD');
// Create tax category and scheme for line items
const taxScheme = new TaxScheme({
id: '01',
name: 'VAT',
});
const classifiedTaxCategory = new ClassifiedTaxCategory({
id: 'S',
percent: '19.00',
taxScheme: taxScheme,
});
// Create invoice line 1 - Product with tax
const item1 = new Item({
name: 'Laptop Computer',
sellersItemIdentification: new SellersItemIdentification({
id: 'PROD-001',
}),
classifiedTaxCategory: classifiedTaxCategory,
});
const price1 = new Price({
priceAmount: '1000.00',
});
const invoiceLine1 = new InvoiceLine({
id: '1',
invoicedQuantity: '2',
lineExtensionAmount: '2000.00',
item: item1,
price: price1,
});
invoice.addInvoiceLine(invoiceLine1);
// Create invoice line 2 - Service with tax
const item2 = new Item({
name: 'Technical Support',
sellersItemIdentification: new SellersItemIdentification({
id: 'SERV-001',
}),
classifiedTaxCategory: classifiedTaxCategory,
});
const price2 = new Price({
priceAmount: '500.00',
});
const invoiceLine2 = new InvoiceLine({
id: '2',
invoicedQuantity: '1',
lineExtensionAmount: '500.00',
item: item2,
price: price2,
});
invoice.addInvoiceLine(invoiceLine2);
// Calculate tax amounts
const subtotalAmount = 2500.00; // 2000 + 500
const taxRate = 0.19; // 19%
const taxAmount = (subtotalAmount * taxRate).toFixed(2); // 475.00
const totalAmount = (subtotalAmount + parseFloat(taxAmount)).toFixed(2); // 2975.00
// Create tax category for tax total
const taxCategory = new TaxCategory({
id: 'S',
percent: '19.00',
taxScheme: taxScheme,
});
// Create tax subtotal
const taxSubtotal = new TaxSubtotal({
taxableAmount: subtotalAmount.toFixed(2),
taxAmount: taxAmount,
taxCategory: taxCategory,
});
// Create tax total
const taxTotal = new TaxTotal({
taxAmount: taxAmount,
taxSubtotals: [taxSubtotal],
});
invoice.addTaxTotal(taxTotal);
// Set monetary totals
const legalMonetaryTotal = new LegalMonetaryTotal({
lineExtensionAmount: subtotalAmount.toFixed(2),
taxExclusiveAmount: subtotalAmount.toFixed(2),
taxInclusiveAmount: totalAmount,
payableAmount: totalAmount,
});
invoice.setLegalMonetaryTotal(legalMonetaryTotal);
// Set line count
invoice.setLineCountNumeric('2');
// Generate XML
const xml = invoice.getXml(true);
console.log(xml);