<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
         xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
         xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">{o.lang=en-us}{o.useHighPrecisionArithmetic=true}{#amount($value) = $value:formatN(2):replace(',', '')}{d.lines[].quantity:mul(.price):round(2):set(.net)}{d.allowanceCharges[].baseAmount:mul(.percent):div(100):round(2):set(.amount)}
  <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID>
  <cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
  <cbc:ID>{d.invoice.number}</cbc:ID>
  <cbc:IssueDate>{d.invoice.issueDate:formatD('YYYY-MM-DD')}</cbc:IssueDate>
  <cbc:DueDate>{d.invoice.dueDate:formatD('YYYY-MM-DD')}</cbc:DueDate>
  <cbc:InvoiceTypeCode>{d.invoice.typeCode}</cbc:InvoiceTypeCode>{d.invoice.note:ifNEM:showBegin}
  <cbc:Note>{d.invoice.note}</cbc:Note>{d.invoice.note:showEnd}
  <cbc:DocumentCurrencyCode>{d.invoice.currency}</cbc:DocumentCurrencyCode>
  <cbc:BuyerReference>{d.invoice.buyerReference}</cbc:BuyerReference>
  <cac:InvoicePeriod>
    <cbc:StartDate>{d.invoice.periodStart:formatD('YYYY-MM-DD')}</cbc:StartDate>
    <cbc:EndDate>{d.invoice.periodEnd:formatD('YYYY-MM-DD')}</cbc:EndDate>
  </cac:InvoicePeriod>
  <cac:OrderReference>
    <cbc:ID>{d.invoice.orderReference}</cbc:ID>
  </cac:OrderReference>
  <cac:AccountingSupplierParty>
    <cac:Party>
      <cbc:EndpointID schemeID="{d.seller.endpointScheme}">{d.seller.endpointId}</cbc:EndpointID>
      <cac:PartyName>
        <cbc:Name>{d.seller.tradingName}</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:StreetName>{d.seller.street}</cbc:StreetName>
        <cbc:CityName>{d.seller.city}</cbc:CityName>
        <cbc:PostalZone>{d.seller.postalCode}</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>{d.seller.country}</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID>{d.seller.vatId}</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>{d.seller.legalName}</cbc:RegistrationName>
        <cbc:CompanyID schemeID="{d.seller.companyIdScheme}">{d.seller.companyId}</cbc:CompanyID>
        <cbc:CompanyLegalForm>{d.seller.legalForm}</cbc:CompanyLegalForm>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:Name>{d.seller.contact.name}</cbc:Name>
        <cbc:Telephone>{d.seller.contact.phone}</cbc:Telephone>
        <cbc:ElectronicMail>{d.seller.contact.email}</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:AccountingSupplierParty>
  <cac:AccountingCustomerParty>
    <cac:Party>
      <cbc:EndpointID schemeID="{d.buyer.endpointScheme}">{d.buyer.endpointId}</cbc:EndpointID>
      <cac:PartyName>
        <cbc:Name>{d.buyer.tradingName}</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:StreetName>{d.buyer.street}</cbc:StreetName>
        <cbc:CityName>{d.buyer.city}</cbc:CityName>
        <cbc:PostalZone>{d.buyer.postalCode}</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>{d.buyer.country}</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID>{d.buyer.vatId}</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>{d.buyer.legalName}</cbc:RegistrationName>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:Name>{d.buyer.contact.name}</cbc:Name>
        <cbc:ElectronicMail>{d.buyer.contact.email}</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:AccountingCustomerParty>
  <cac:Delivery>
    <cac:DeliveryLocation>
      <cac:Address>
        <cbc:StreetName>{d.delivery.street}</cbc:StreetName>
        <cbc:CityName>{d.delivery.city}</cbc:CityName>
        <cbc:PostalZone>{d.delivery.postalCode}</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>{d.delivery.country}</cbc:IdentificationCode>
        </cac:Country>
      </cac:Address>
    </cac:DeliveryLocation>
    <cac:DeliveryParty>
      <cac:PartyName>
        <cbc:Name>{d.delivery.partyName}</cbc:Name>
      </cac:PartyName>
    </cac:DeliveryParty>
  </cac:Delivery>
  <cac:PaymentMeans>
    <cbc:PaymentMeansCode name="{d.payment.meansName}">{d.payment.meansCode}</cbc:PaymentMeansCode>
    <cbc:PaymentID>{d.payment.reference}</cbc:PaymentID>
    <cac:PayeeFinancialAccount>
      <cbc:ID>{d.payment.iban}</cbc:ID>
      <cbc:Name>{d.payment.accountName}</cbc:Name>
    </cac:PayeeFinancialAccount>
  </cac:PaymentMeans>
  <cac:PaymentTerms>
    <cbc:Note>{d.payment.terms}</cbc:Note>
  </cac:PaymentTerms>
{d.allowanceCharges[i]}  <cac:AllowanceCharge>
    <cbc:ChargeIndicator>{d.allowanceCharges[i].chargeIndicator}</cbc:ChargeIndicator>
    <cbc:AllowanceChargeReasonCode>{d.allowanceCharges[i].reasonCode}</cbc:AllowanceChargeReasonCode>
    <cbc:AllowanceChargeReason>{d.allowanceCharges[i].reason}</cbc:AllowanceChargeReason>
    <cbc:MultiplierFactorNumeric>{d.allowanceCharges[i].percent}</cbc:MultiplierFactorNumeric>
    <cbc:Amount currencyID="{d.invoice.currency}">{$amount(d.allowanceCharges[i].amount)}</cbc:Amount>
    <cbc:BaseAmount currencyID="{d.invoice.currency}">{$amount(d.allowanceCharges[i].baseAmount)}</cbc:BaseAmount>
    <cac:TaxCategory>
      <cbc:ID>{d.allowanceCharges[i].vatCategory}</cbc:ID>
      <cbc:Percent>{d.allowanceCharges[i].vatRate}</cbc:Percent>
      <cac:TaxScheme>
        <cbc:ID>VAT</cbc:ID>
      </cac:TaxScheme>
    </cac:TaxCategory>
  </cac:AllowanceCharge>
{d.allowanceCharges[i+1]}  <cac:TaxTotal>{d.lines[].vatCategory:append(.vatRate):set(.taxKey)}{d.allowanceCharges[].vatCategory:append(.vatRate):set(.taxKey)}{d.allowanceCharges[].chargeIndicator:ifEQ(true):show(1):elseShow(-1):mul(.amount):set(.net)}{d.lines[]:set(c.base[])}{d.allowanceCharges[]:set(c.base[])}{c.base[].net:aggSum(.taxKey):set(.taxable)}{c.base[]:set(c.vat[key=.taxKey])}{c.vat[].taxable:mul(.vatRate):div(100):round(2):set(.tax)}{c.vat[].tax:aggSum:round(2):set(c.vatTotal)}
    <cbc:TaxAmount currencyID="{d.invoice.currency}">{$amount(c.vatTotal)}</cbc:TaxAmount>
{c.vat[i]}    <cac:TaxSubtotal>
      <cbc:TaxableAmount currencyID="{d.invoice.currency}">{$amount(c.vat[i].taxable)}</cbc:TaxableAmount>
      <cbc:TaxAmount currencyID="{d.invoice.currency}">{$amount(c.vat[i].tax)}</cbc:TaxAmount>
      <cac:TaxCategory>
        <cbc:ID>{c.vat[i].vatCategory}</cbc:ID>
        <cbc:Percent>{c.vat[i].vatRate}</cbc:Percent>{c.vat[i].exemptionCode:ifNEM:showBegin}
        <cbc:TaxExemptionReasonCode>{c.vat[i].exemptionCode}</cbc:TaxExemptionReasonCode>{c.vat[i].exemptionCode:showEnd}
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:TaxCategory>
    </cac:TaxSubtotal>
{c.vat[i+1]}  </cac:TaxTotal>
  <cac:LegalMonetaryTotal>{d.lines[].net:aggSum:round(2):set(c.lineTotal)}{d.allowanceCharges[chargeIndicator=false].amount:aggSum:round(2):set(c.allowanceTotal)}{d.allowanceCharges[chargeIndicator=true].amount:aggSum:round(2):set(c.chargeTotal)}{c.lineTotal:sub(c.allowanceTotal):add(c.chargeTotal):set(c.taxExclusive)}{c.taxExclusive:add(c.vatTotal):set(c.taxInclusive)}{c.taxInclusive:sub(d.invoice.prepaidAmount):set(c.payable)}
    <cbc:LineExtensionAmount currencyID="{d.invoice.currency}">{$amount(c.lineTotal)}</cbc:LineExtensionAmount>
    <cbc:TaxExclusiveAmount currencyID="{d.invoice.currency}">{$amount(c.taxExclusive)}</cbc:TaxExclusiveAmount>
    <cbc:TaxInclusiveAmount currencyID="{d.invoice.currency}">{$amount(c.taxInclusive)}</cbc:TaxInclusiveAmount>{c.allowanceTotal:ifGT(0):showBegin}
    <cbc:AllowanceTotalAmount currencyID="{d.invoice.currency}">{$amount(c.allowanceTotal)}</cbc:AllowanceTotalAmount>{c.allowanceTotal:showEnd}{c.chargeTotal:ifGT(0):showBegin}
    <cbc:ChargeTotalAmount currencyID="{d.invoice.currency}">{$amount(c.chargeTotal)}</cbc:ChargeTotalAmount>{c.chargeTotal:showEnd}{d.invoice.prepaidAmount:ifGT(0):showBegin}
    <cbc:PrepaidAmount currencyID="{d.invoice.currency}">{$amount(d.invoice.prepaidAmount)}</cbc:PrepaidAmount>{d.invoice.prepaidAmount:showEnd}
    <cbc:PayableAmount currencyID="{d.invoice.currency}">{$amount(c.payable)}</cbc:PayableAmount>
  </cac:LegalMonetaryTotal>
{d.lines[i]}  <cac:InvoiceLine>
    <cbc:ID>{d.lines[i].id}</cbc:ID>
    <cbc:InvoicedQuantity unitCode="{d.lines[i].unitCode}">{d.lines[i].quantity}</cbc:InvoicedQuantity>
    <cbc:LineExtensionAmount currencyID="{d.invoice.currency}">{$amount(d.lines[i].net)}</cbc:LineExtensionAmount>{d.lines[i].orderLineId:ifNEM:showBegin}
    <cac:OrderLineReference>
      <cbc:LineID>{d.lines[i].orderLineId}</cbc:LineID>
    </cac:OrderLineReference>{d.lines[i].orderLineId:showEnd}
    <cac:Item>
      <cbc:Name>{d.lines[i].name}</cbc:Name>
      <cac:SellersItemIdentification>
        <cbc:ID>{d.lines[i].sellerItemId}</cbc:ID>
      </cac:SellersItemIdentification>
      <cac:ClassifiedTaxCategory>
        <cbc:ID>{d.lines[i].vatCategory}</cbc:ID>
        <cbc:Percent>{d.lines[i].vatRate}</cbc:Percent>
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:ClassifiedTaxCategory>
    </cac:Item>
    <cac:Price>
      <cbc:PriceAmount currencyID="{d.invoice.currency}">{d.lines[i].price}</cbc:PriceAmount>
    </cac:Price>
  </cac:InvoiceLine>
{d.lines[i+1]}</Invoice>
