Overview
TheInvoice class is the primary entry point for building UBL 2.1 compliant invoice XML documents. It provides a fluent API for setting invoice properties, adding line items, managing parties, taxes, and generating the final XML output with Colombian DIAN validation support.
Constructor
The unique identifier for the invoice (e.g., “SETP990000001”)
Configuration object containing issuer, software, and environment settings
Issue time in milliseconds for date/time fields. Defaults to
Date.now()DIAN environment:
"1" for production, "2" for testing. Defaults to "2"Configuration of the invoice issuer
Resolution number from DIAN authorization
Issuer technical ID provided by DIAN
Assigned prefix for invoice numbering (e.g., “SETP”)
Authorized start number (e.g., “990000000”)
Authorized end number (e.g., “995000000”)
Authorization start date in
yyyy-mm-dd format (e.g., “2019-01-19”)Authorization end date in
yyyy-mm-dd format (e.g., “2030-01-19”)Example
Core Methods
Document Properties
addProperty()
Attribute name
Attribute value
setDefaultProperties()
- UBL Invoice schema
- Common Aggregate Components (cac)
- Common Basic Components (cbc)
- XML Digital Signature (ds)
- Extension Components (ext)
- DIAN Structures (sts)
- XAdES signatures
Basic Invoice Information
setUBLVersionID()
setID()
getID()
If
true, returns the string value; if false, returns the UdtIdentifier objectsetProfileID()
setProfileExecutionID()
setCustomizationID()
setUUID()
setInvoiceTypeCode()
"01": Standard invoice"02": Export invoice"03": Contingency invoice
Dates and Times
setIssueDate()
yyyy-mm-dd format.
setIssueTime()
setDueDate()
setTaxPointDate()
Currency Settings
setDocumentCurrencyCode()
setTaxCurrencyCode()
setPricingCurrencyCode()
setPaymentCurrencyCode()
setPaymentAlternativeCurrencyCode()
Notes and References
addNote()
setCopyIndicator()
setBuyerReference()
setAccountingCost()
setAccountingCostCode()
setLineCountNumeric()
finalizeDocument().
Periods and References
addInvoicePeriod()
clearInvoicePeriods()
setOrderReference()
addBillingReference()
addDespatchDocumentReference()
addReceiptDocumentReference()
addStatementDocumentReference()
addOriginatorDocumentReference()
addContractDocumentReference()
addAdditionalDocumentReference()
addProjectReference()
Parties
setAccountingSupplierParty()
setAccountingCustomerParty()
setTaxRepresentativeParty()
addSignature()
Delivery and Payment
addDelivery()
setDeliveryTerms()
addPaymentMeans()
addPrepaidPayment()
setPaymentExchangeRate()
Financial Totals
addAllowanceCharge()
addTaxTotal()
setLegalMonetaryTotal()
Invoice Lines
addInvoiceLine()
Extensions
setUBLExtensions()
Colombian DIAN-Specific Methods
calculateDianExtension()
- Invoice control information (authorization, ranges)
- Software provider details
- Software security code (hashed)
- Authorization provider (DIAN)
issuer and software options provided in the constructor.
applyCufeCode()
- Invoice number
- Issue date and time
- Gross amount
- Tax codes and amounts (IVA, INC, ICA)
- Total payable
- Supplier and customer NITs
- Technical key
- Environment
This method is automatically called by
finalizeDocument().applyQRCode()
This method is automatically called by
finalizeDocument() and requires the CUFE to be set first.getQRCode()
findTaxTotalById()
Tax scheme ID (e.g., “01” for IVA, “04” for INC, “03” for ICA)
If
true, returns formatted string; if false, returns numberfinalizeDocument()
- Setting the line count based on number of invoice lines
- Assigning sequential IDs to each invoice line
- Calculating and setting the CUFE code
- Generating and setting the QR code
Always call
finalizeDocument() before calling getXml() to ensure all required Colombian DIAN validations are applied.XML Generation
getXml()
If
true, formats the XML with indentation and line breaksIf
true, omits the XML declaration headerComplete Example
Type Definitions
InvoiceOptions
Method Chaining
Most methods return the Invoice instance, enabling fluent method chaining:Best Practices
Order of Operations: Always follow this sequence when building invoices:
- Create Invoice instance with options
- Set default properties and basic information
- Configure parties (supplier, customer)
- Add invoice lines with tax information
- Add document-level allowances/charges if needed
- Add tax totals
- Set legal monetary total
- Call
calculateDianExtension()for Colombian invoices - Call
finalizeDocument()to calculate CUFE and QR code - Generate XML with
getXml()
CUFE Calculation: The CUFE must be calculated after all monetary amounts and tax information have been set, as it depends on these values. Always call
finalizeDocument() before generating XML for Colombian DIAN invoices.Tax Totals: Ensure that tax totals at the document level match the sum of tax amounts from all invoice lines. The
findTaxTotalById() method helps verify tax amounts by tax type.Related Classes
- Party - Supplier and customer party information
- InvoiceLine - Individual line items
- TaxTotal - Tax calculations
- AllowanceCharge - Discounts and charges