toXml method

String toXml()

Generates the XML representation of the debit note.

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

Returns the XML string representing the debit note.

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.standardDebitNote().toXml(builder);

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

    // Build the BillingReference element.
    billingReference.toXml(builder);

    // 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);

    // PaymentMeans builder
    paymentMeans.toXml(builder);

    // AllowanceCharge builder
    allowanceCharge?.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));

    // Debit Note Lines (Invoice lines)
    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);
}