Sales Tax Presets and Line Calculation

Preset behavior

SalesTaxPreset stores jurisdiction defaults by country and region.

Seeded defaults include all Canadian provinces and territories:

  • AB, BC, MB, NB, NL, NS, NT, NU, ON, PE, QC, SK, YT

Preset resolution order

  1. If Tax Mode = Manual, manual rates are used.

  2. If Tax Mode = Auto and preset is selected on the invoice, preset rates are used.

  3. If Tax Mode = Auto and no preset is selected, the system searches active presets by ship_to_country_code + ship_to_region_code.

  4. If no preset matches, fallback defaults are used (for Canada/US only).

Line-level (Xero-style) calculation

Tax is computed per line and rounded per line item to two decimals, then summed. This matches common accounting behavior where line-level rounding may differ from single-document rounding.

Algorithm summary:

  1. Build invoice lines from ItemTransactionModel plus optional Shipping line.

  2. Allocate invoice-level discount proportionally across taxable lines.

  3. For each line: * taxable_line_amount = line_amount - allocated_discount * line_tax = round(taxable_line_amount * tax_rate, 2)

  4. Sum all line tax components for totals.

Tax-exempt items

Any item with item_model.additional_info["sales_tax_exempt"] = true is treated as non-taxable in this flow.