How Invoice Discount Calculations Work
Source: invoice.rb:1113-1165, invoice_item.rb:284-306
- Two invoice-level paths: discount_includes_tax vs discount_excludes_tax
- When discount includes tax, the discount amount is split proportionally between subtotal and GST components
- When discount excludes tax, discount_on_tax is always 0 (discount only reduces the subtotal)
- Line-item discounts have two types: currency (flat amount per unit quantity) and percentage (based on adjusted_retail_price discount% * quantity)
- Adjusted_retail_price is the higher of the product's retail_price and the item's unit_price -- used as the base for percentage discount calculations
- If discount_includes_tax is true at line-item level, the discount is split between tax and subtotal proportionally
- BUG: adjusted_retail_price is only recalculated if blank or less than current unit_price, meaning it can become stale if the unit price is lowered after being raised
Support scenarios
- "My discount amount doesn't match what I expected" -> check whether discount_includes_tax is on; when on, the visible discount includes the GST component
- "The percentage discount is wrong" -> percentage is calculated from adjusted_retail_price (higher of retail and unit price), not the current unit price
- "Discount works on one invoice but errors on another" -> the second invoice likely has mixed tax rates with discount_includes_tax enabled, which is not supported