toXml method

void toXml(
  1. XmlBuilder builder
)

Converts the AllowanceCharge instance to an XML representation using the XmlBuilder.

This method builds the XML structure for an allowance or charge, following the specified XML schema for the transaction. The XML elements are structured according to the standards required for tax reporting or invoicing.

Example:

XmlBuilder builder = XmlBuilder();
allowanceCharge.toXml(builder);

Implementation

void toXml(XmlBuilder builder) {
  builder.element('cac:AllowanceCharge', nest: () {
    // Charge indicator element (indicating whether this is a charge or allowance)
    builder.element('cbc:ChargeIndicator', nest: false);

    // Allowance charge reason element (e.g., 'discount')
    builder.element('cbc:AllowanceChargeReason', nest: reason);

    // Amount element, with the currency set to SAR (Saudi Riyal)
    builder.element('cbc:Amount',
        attributes: {'currencyID': 'SAR'}, nest: amount);

    // Tax category element containing tax-related information
    builder.element('cac:TaxCategory', nest: () {
      // Tax category ID element with scheme details
      builder.element('cbc:ID',
          attributes: {'schemeID': 'UN/ECE 5305', 'schemeAgencyID': '6'},
          nest: taxCategoryCodeValues[tax.code ?? TaxCategoryCode.standard]);

      // Tax percentage element
      builder.element('cbc:Percent', nest: tax.percent);

      // Tax scheme element with scheme ID and code
      builder.element('cac:TaxScheme', nest: () {
        builder.element('cbc:ID',
            attributes: {'schemeID': 'UN/ECE 5153', 'schemeAgencyID': '6'},
            nest: tax.taxSchemeCode?.name.toUpperCase() ??
                TaxSchemeCode.vat.name.toUpperCase());
      });
    });
  });
}