{% comment %}
  XRechnung 3.0, syntax CII (UN/CEFACT). Download the XML from the Preview page and send it to the buyer.
  Put the Leitweg-ID (or the buyer's reference) into the invoice Order number field. Copy into an Invoice template (Administration > Invoices > Invoice templates > New invoice template).
  The printed part is ordinary HTML; the xml_invoice block below is not printed.
  Seller data is read from the "Company info" and "Bill info" settings, one "Key: value" per line:
    Street: Musterstraße 1 / Postcode: 10115 / City: Berlin / Country: DE / VAT ID: DE123456789 / IBAN: DE89370400440532013000
    Phone: +49 30 1234567 / Email: billing@example.com (the contact person is the Representative setting)
  A key that is missing there falls back to the value in the assign lines below; edit those defaults.
  Buyer data comes from the invoice contact (company name, address, primary email); every text value goes through | xml_escape.
{% endcomment %}
<h1>{{ invoice.number }}</h1>
<p>{{ account.company }}<br>{{ account.info | newline_to_br }}</p>
<p>{{ invoice.contact.company }} {{ invoice.contact.name }}<br>{{ invoice.contact.address.post_address | newline_to_br }}</p>
<table>
  {% for line in invoice.lines %}
  <tr><td>{{ line.description }}</td><td>{{ line.quantity }} {{ line.units }}</td><td>{{ line.price_to_s }}</td><td>{{ line.total_to_s }}</td></tr>
  {% endfor %}
</table>
<p>{{ invoice.amount }} {{ invoice.currency }}</p>
{% xml_invoice %}
{% assign seller_name = account.company | default: 'Your Company GmbH' %}
{% assign seller_street = 'Musterstraße 1' %}
{% assign seller_postcode = '10115' %}
{% assign seller_city = 'Berlin' %}
{% assign seller_country = 'DE' %}
{% assign seller_vat_id = 'DE123456789' %}
{% assign seller_iban = 'DE89370400440532013000' %}
{% assign seller_contact = account.representative | default: 'Max Mustermann' %}
{% assign seller_phone = '+49 30 1234567' %}
{% assign seller_email = 'billing@example.com' %}
{% capture seller_text %}{{ account.info }}
{{ account.bill_info }}{% endcapture %}
{% assign seller_lines = seller_text | newline_to_br | split: '<br />' %}
{% for seller_line in seller_lines %}
  {% assign seller_key = seller_line | split: ':' | first | strip %}
  {% assign seller_value = seller_line | remove_first: seller_key | remove_first: ':' | strip %}
  {% case seller_key %}
    {% when 'Street' %}{% assign seller_street = seller_value %}
    {% when 'Postcode' %}{% assign seller_postcode = seller_value %}
    {% when 'City' %}{% assign seller_city = seller_value %}
    {% when 'Country' %}{% assign seller_country = seller_value %}
    {% when 'VAT ID' %}{% assign seller_vat_id = seller_value %}
    {% when 'Phone' %}{% assign seller_phone = seller_value %}
    {% when 'Email' %}{% assign seller_email = seller_value %}
    {% when 'IBAN' %}{% assign seller_iban = seller_value | remove: ' ' %}
  {% endcase %}
{% endfor %}
{% assign buyer = invoice.contact %}
{% assign buyer_name = buyer.company %}{% if buyer_name == blank %}{% assign buyer_name = buyer.name %}{% endif %}
{% assign buyer_country = buyer.address.country_code | default: 'DE' %}
<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
  <rsm:ExchangedDocumentContext>
    <ram:BusinessProcessSpecifiedDocumentContextParameter><ram:ID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</ram:ID></ram:BusinessProcessSpecifiedDocumentContextParameter>
    <ram:GuidelineSpecifiedDocumentContextParameter><ram:ID>urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0</ram:ID></ram:GuidelineSpecifiedDocumentContextParameter>
  </rsm:ExchangedDocumentContext>
  <rsm:ExchangedDocument>
    <ram:ID>{{ invoice.number | xml_escape }}</ram:ID>
    <ram:TypeCode>380</ram:TypeCode>
    <ram:IssueDateTime><udt:DateTimeString format="102">{{ invoice.invoice_date | date: '%Y%m%d' }}</udt:DateTimeString></ram:IssueDateTime>
  </rsm:ExchangedDocument>
  <rsm:SupplyChainTradeTransaction>
    {% for line in invoice.lines %}
    <ram:IncludedSupplyChainTradeLineItem>
      <ram:AssociatedDocumentLineDocument><ram:LineID>{{ forloop.index }}</ram:LineID></ram:AssociatedDocumentLineDocument>
      <ram:SpecifiedTradeProduct><ram:Name>{{ line.description | xml_escape }}</ram:Name></ram:SpecifiedTradeProduct>
      <ram:SpecifiedLineTradeAgreement><ram:NetPriceProductTradePrice><ram:ChargeAmount>{{ line.price | round: 2 }}</ram:ChargeAmount></ram:NetPriceProductTradePrice></ram:SpecifiedLineTradeAgreement>
      <ram:SpecifiedLineTradeDelivery><ram:BilledQuantity unitCode="C62">{{ line.quantity }}</ram:BilledQuantity></ram:SpecifiedLineTradeDelivery>
      <ram:SpecifiedLineTradeSettlement>
        <ram:ApplicableTradeTax><ram:TypeCode>VAT</ram:TypeCode><ram:CategoryCode>{% if line.tax > 0 %}S{% else %}Z{% endif %}</ram:CategoryCode><ram:RateApplicablePercent>{{ line.tax | round: 2 }}</ram:RateApplicablePercent></ram:ApplicableTradeTax>
        <ram:SpecifiedTradeSettlementLineMonetarySummation><ram:LineTotalAmount>{{ line.total | round: 2 }}</ram:LineTotalAmount></ram:SpecifiedTradeSettlementLineMonetarySummation>
      </ram:SpecifiedLineTradeSettlement>
    </ram:IncludedSupplyChainTradeLineItem>
    {% endfor %}
    <ram:ApplicableHeaderTradeAgreement>
      <ram:BuyerReference>{{ invoice.order_number | default: invoice.number | xml_escape }}</ram:BuyerReference>
      <ram:SellerTradeParty>
        <ram:Name>{{ seller_name | xml_escape }}</ram:Name>
        <ram:DefinedTradeContact>
          <ram:PersonName>{{ seller_contact | xml_escape }}</ram:PersonName>
          <ram:TelephoneUniversalCommunication><ram:CompleteNumber>{{ seller_phone | xml_escape }}</ram:CompleteNumber></ram:TelephoneUniversalCommunication>
          <ram:EmailURIUniversalCommunication><ram:URIID>{{ seller_email | xml_escape }}</ram:URIID></ram:EmailURIUniversalCommunication>
        </ram:DefinedTradeContact>
        <ram:PostalTradeAddress>
          <ram:PostcodeCode>{{ seller_postcode | xml_escape }}</ram:PostcodeCode>
          <ram:LineOne>{{ seller_street | xml_escape }}</ram:LineOne>
          <ram:CityName>{{ seller_city | xml_escape }}</ram:CityName>
          <ram:CountryID>{{ seller_country | xml_escape }}</ram:CountryID>
        </ram:PostalTradeAddress>
        <ram:URIUniversalCommunication><ram:URIID schemeID="EM">{{ seller_email | xml_escape }}</ram:URIID></ram:URIUniversalCommunication>
        <ram:SpecifiedTaxRegistration><ram:ID schemeID="VA">{{ seller_vat_id | xml_escape }}</ram:ID></ram:SpecifiedTaxRegistration>
      </ram:SellerTradeParty>
      <ram:BuyerTradeParty>
        <ram:Name>{{ buyer_name | xml_escape }}</ram:Name>
        <ram:PostalTradeAddress>
          <ram:PostcodeCode>{{ buyer.address.postcode | xml_escape }}</ram:PostcodeCode>
          {% if buyer.address.street1 != blank %}<ram:LineOne>{{ buyer.address.street1 | xml_escape }}</ram:LineOne>{% endif %}
          <ram:CityName>{{ buyer.address.city | xml_escape }}</ram:CityName>
          <ram:CountryID>{{ buyer_country | xml_escape }}</ram:CountryID>
        </ram:PostalTradeAddress>
        <ram:URIUniversalCommunication><ram:URIID schemeID="EM">{{ buyer.primary_email | xml_escape }}</ram:URIID></ram:URIUniversalCommunication>
      </ram:BuyerTradeParty>
    </ram:ApplicableHeaderTradeAgreement>
    <ram:ApplicableHeaderTradeDelivery>
      <ram:ActualDeliverySupplyChainEvent><ram:OccurrenceDateTime><udt:DateTimeString format="102">{{ invoice.invoice_date | date: '%Y%m%d' }}</udt:DateTimeString></ram:OccurrenceDateTime></ram:ActualDeliverySupplyChainEvent>
    </ram:ApplicableHeaderTradeDelivery>
    <ram:ApplicableHeaderTradeSettlement>
      <ram:InvoiceCurrencyCode>{{ invoice.currency | xml_escape }}</ram:InvoiceCurrencyCode>
      <ram:SpecifiedTradeSettlementPaymentMeans>
        <ram:TypeCode>58</ram:TypeCode>
        <ram:PayeePartyCreditorFinancialAccount><ram:IBANID>{{ seller_iban | xml_escape }}</ram:IBANID></ram:PayeePartyCreditorFinancialAccount>
      </ram:SpecifiedTradeSettlementPaymentMeans>
      {% for group in invoice.tax_groups %}
      <ram:ApplicableTradeTax>
        <ram:CalculatedAmount>{{ group[1] | round: 2 }}</ram:CalculatedAmount>
        <ram:TypeCode>VAT</ram:TypeCode>
        <ram:BasisAmount>{{ group[2] | round: 2 }}</ram:BasisAmount>
        <ram:CategoryCode>{% if group[0] > 0 %}S{% else %}Z{% endif %}</ram:CategoryCode>
        <ram:RateApplicablePercent>{{ group[0] | round: 2 }}</ram:RateApplicablePercent>
      </ram:ApplicableTradeTax>
      {% endfor %}
      {% if invoice.due_date %}
      <ram:SpecifiedTradePaymentTerms><ram:DueDateDateTime><udt:DateTimeString format="102">{{ invoice.due_date | date: '%Y%m%d' }}</udt:DateTimeString></ram:DueDateDateTime></ram:SpecifiedTradePaymentTerms>
      {% endif %}
      <ram:SpecifiedTradeSettlementHeaderMonetarySummation>
        <ram:LineTotalAmount>{{ invoice.subtotal | round: 2 }}</ram:LineTotalAmount>
        <ram:TaxBasisTotalAmount>{{ invoice.subtotal | round: 2 }}</ram:TaxBasisTotalAmount>
        <ram:TaxTotalAmount currencyID="{{ invoice.currency | xml_escape }}">{{ invoice.tax_amount | round: 2 }}</ram:TaxTotalAmount>
        <ram:GrandTotalAmount>{{ invoice.amount | round: 2 }}</ram:GrandTotalAmount>
        <ram:DuePayableAmount>{{ invoice.remaining_balance | round: 2 }}</ram:DuePayableAmount>
      </ram:SpecifiedTradeSettlementHeaderMonetarySummation>
    </ram:ApplicableHeaderTradeSettlement>
  </rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>
{% endxml_invoice %}
