toXml method

String toXml()

Generates the XML representation of the invoice.

This method creates an XML document structured with all the required elements for the invoice, including information such as the invoice ID, customer details, items, tax information, supplier details, and other related references like delivery and PIH.

Returns the XML string representing the invoice.

Implementation

String toXml() {
  final builder = XmlBuilder();
  builder.processing('xml', 'version="1.0" encoding="UTF-8"');
  builder.element('Invoice', namespaces: {
    "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2": "",
    "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2":
        "cac",
    "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2":
        "cbc",
    "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2":
        "ext"
  }, nest: () {
    builder.element('cbc:ProfileID', nest: 'reporting:1.0');
    builder.element('cbc:ID', nest: id);
    builder.element('cbc:UUID', nest: uuid);
    builder.element('cbc:IssueDate',
        nest: issueDate.toIso8601String().split('T')[0]);
    builder.element('cbc:IssueTime',
        nest: issueTime.toIso8601String().split('T')[1].split('.')[0]);

    // Invoice Type Code
    invoiceTypeCode != null
        ? invoiceTypeCode?.toXml(builder)
        : InvoiceTypeCode.standardTaxInvoice().toXml(builder);

    builder.element('cbc:DocumentCurrencyCode', nest: currency);
    builder.element('cbc:TaxCurrencyCode', nest: currency);

    // Additional Document Reference for ICV
    builder.element('cac:AdditionalDocumentReference', nest: () {
      builder.element('cbc:ID', nest: 'ICV');
      builder.element('cbc:UUID', nest: icv);
    });

    // Additional Document Reference for PIH
    builder.element('cac:AdditionalDocumentReference', nest: () {
      builder.element('cbc:ID', nest: 'PIH');
      builder.element('cac:Attachment', nest: () {
        builder.element('cbc:EmbeddedDocumentBinaryObject',
            attributes: {'mimeCode': 'text/plain'},
            nest: pih.isEmpty ? getPIHForFirstInvoice() : pih);
      });
    });

    // Supplier and Customer parties
    builder.element('cac:AccountingSupplierParty',
        nest: () => supplier?.toXml(builder));
    builder.element('cac:AccountingCustomerParty',
        nest: () => customer.toXml(builder));

    // Delivery builder
    delivery.toXml(builder);

    // Tax totals and Legal Monetary totals
    builder.element('cac:TaxTotal', nest: () {
      builder.element('cbc:TaxAmount',
          attributes: {'currencyID': tax.currency}, nest: tax.amount);
    });

    builder.element('cac:TaxTotal', nest: () => tax.toXml(builder));
    builder.element('cac:LegalMonetaryTotal',
        nest: () => monetaryTotal.toXml(builder));

    // Invoice Lines (Items)
    for (int id = 0; id < lines.length; id++) {
      InvoiceLine line = lines[id];
      builder.element('cac:InvoiceLine', nest: () => line.toXml(builder, id));
    }
  });

  return builder.buildDocument().toXmlString(pretty: true);
}