UblEx.Generator.Helpers (UblEx v0.8.1)

Copy Markdown View Source

Helper functions for UBL generation.

Contains utility functions for calculating totals, formatting values, and generating XML fragments.

Summary

Functions

Generate AllowanceCharge XML for a detail with discount.

Generate BillingReference XML elements.

Generate optional CityName XML element.

Generate Delivery terms XML for invoices.

Escape XML special characters.

Format a Decimal amount as a string with 2 decimal places.

Infer Peppol scheme ID from country code.

Infer tax category from detail, defaulting based on VAT percentage.

Generate PartyLegalEntity XML with optional CompanyID.

Get scheme for a party, using explicit scheme if provided, otherwise inferring from country.

Generate optional PartyTaxScheme XML element for supplier.

Convert a Peppol tax category code to a descriptive atom.

Generate PostalAddress XML with optional elements. Only Country > IdentificationCode is required.

Generate optional PostalZone XML element.

Format street address with optional house number.

Generate optional StreetName XML element.

Generate tax category XML (ID and Percent).

Generate tax category XML with optional exemption fields.

Generate a TaxSubtotal XML element.

Generate tax totals XML for all tax categories.

Calculate UBL-compliant line total for a detail item.

Calculate UBL-compliant totals with proper rounding for Peppol validation.

Extract numeric VAT number (remove country code and non-digits).

Functions

allowance_charge_xml(detail)

Generate AllowanceCharge XML for a detail with discount.

billing_reference(invoice_numbers)

Generate BillingReference XML elements.

Accepts a list of invoice numbers and generates BillingReference elements.

city_name_xml(party)

Generate optional CityName XML element.

delivery_terms(customer, bool)

Generate Delivery terms XML for invoices.

escape(str)

Escape XML special characters.

format(amt)

Format a Decimal amount as a string with 2 decimal places.

infer_scheme(country)

Infer Peppol scheme ID from country code.

Returns the country-specific scheme if known, otherwise returns "0088" (EAN/GLN).

Examples

iex> UblEx.Generator.Helpers.infer_scheme("BE")
"0208"

iex> UblEx.Generator.Helpers.infer_scheme("US")
"0088"

infer_tax_category(detail)

Infer tax category from detail, defaulting based on VAT percentage.

If tax_category is explicitly set, use it. Otherwise:

  • VAT 6%, 12%, 21% → :standard
  • VAT 0% → :zero_rated

party_scheme(party)

Get scheme for a party, using explicit scheme if provided, otherwise inferring from country.

party_tax_scheme_xml(party)

Generate optional PartyTaxScheme XML element for supplier.

peppol_code_to_category(code)

Convert a Peppol tax category code to a descriptive atom.

Returns :standard for unknown codes.

postal_address_xml(party)

Generate PostalAddress XML with optional elements. Only Country > IdentificationCode is required.

postal_zone_xml(party)

Generate optional PostalZone XML element.

street_address(party)

Format street address with optional house number.

street_name_xml(party)

Generate optional StreetName XML element.

tax(perc, tax_category)

Generate tax category XML (ID and Percent).

Accepts VAT percentage (integer or Decimal) and tax category atom.

tax(perc, tax_category, reason_code, reason)

Generate tax category XML with optional exemption fields.

Includes TaxExemptionReasonCode and TaxExemptionReason when provided.

tax_sub_total(arg)

Generate a TaxSubtotal XML element.

tax_totals(details)

Generate tax totals XML for all tax categories.

Groups details by VAT percentage and tax category, then generates TaxSubtotal elements. VAT is calculated on the grouped subtotal to avoid rounding errors.

ubl_line_total(detail)

Calculate UBL-compliant line total for a detail item.

Ensures consistency with AllowanceCharge calculations.

ubl_totals(details)

Calculate UBL-compliant totals with proper rounding for Peppol validation.

Ensures:

  • BR-CO-15: TaxInclusiveAmount = TaxExclusiveAmount + TaxAmount
  • BR-CO-10: LineExtensionAmount = Σ InvoiceLine/LineExtensionAmount
  • PEPPOL-EN16931-R120: Line net amounts are correctly calculated

VAT is calculated by grouping lines with the same VAT rate, summing their totals, then applying VAT to the sum. This prevents rounding errors from accumulating when multiple lines have the same VAT rate.

Returns a map with:

  • :subtotal - Sum of all line totals
  • :vat - Sum of all VAT amounts
  • :grand_total - Subtotal + VAT

vat_number(str)

Extract numeric VAT number (remove country code and non-digits).