toXml method
Generates the XML representation of the debit note in a format required for reporting.
This method creates a fully structured XML document with all the necessary elements such as the debit note ID, customer details, items, tax, and payment methods. It also includes additional references like the ICV and PIH.
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.simplifiedDebitNote().toXml(builder);
builder.element('cbc:DocumentCurrencyCode', nest: currency);
builder.element('cbc:TaxCurrencyCode', nest: currency);
// Build the BillingReference element.
billingReference.toXml(builder);
builder.element('cac:AdditionalDocumentReference', nest: () {
builder.element('cbc:ID', nest: 'ICV');
builder.element('cbc:UUID', nest: icv);
});
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);
});
});
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);
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));
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);
}