-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(core): Add configurable OrderTaxSummaryCalculationStrategy #4376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelbromley
merged 14 commits into
vendurehq:minor
from
colinpieper:feat/order-tax-summary-calculation-strategy
Mar 11, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cb9871c
feat(core): Add configurable OrderTaxSummaryCalculationStrategy
colinpieper 5e8403b
chore(core): Revert accidentally changed @since version
colinpieper dd6ab41
fix(core): Fix tax summary rounding inconsistency in OrderLevelTaxSum…
colinpieper 15a3573
fix(core): Add consistent null-safety guards for order relations in t…
colinpieper 41564a4
test(core): Add test for same tax rate on items and shipping
colinpieper 7db57d2
test(core): Simplify tax consistency assertion in order-calculator test
colinpieper 3134d49
test(core): Strengthen tax strategy test assertions
colinpieper 686c5c1
Merge branch 'minor' into feat/order-tax-summary-calculation-strategy
michaelbromley 4141899
Merge branch 'minor' into feat/order-tax-summary-calculation-strategy
michaelbromley 246548f
chore(core): Rename strategies
colinpieper b19dd53
Merge branch 'minor' into feat/order-tax-summary-calculation-strategy
michaelbromley 6452494
Merge branch 'minor' into feat/order-tax-summary-calculation-strategy
michaelbromley fc073e2
Merge branch 'minor' into feat/order-tax-summary-calculation-strategy
michaelbromley f60efe7
fix(core): Add missing orderTaxCalculationStrategy to shipping test m…
michaelbromley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/core/src/config/tax/default-order-tax-calculation-strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { OrderTaxSummary, TaxLine } from '@vendure/common/lib/generated-types'; | ||
| import { summate } from '@vendure/common/lib/shared-utils'; | ||
|
|
||
| import { OrderLine } from '../../entity/order-line/order-line.entity'; | ||
| import { Order } from '../../entity/order/order.entity'; | ||
| import { Surcharge } from '../../entity/surcharge/surcharge.entity'; | ||
|
|
||
| import { OrderTaxCalculationStrategy, OrderTotalsResult } from './order-tax-calculation-strategy'; | ||
|
|
||
| /** | ||
| * @description | ||
| * The default {@link OrderTaxCalculationStrategy}. Tax is rounded at the | ||
| * individual line level and then summed. This matches the standard Vendure behaviour | ||
| * prior to the introduction of this strategy. | ||
| * | ||
| * @docsCategory tax | ||
| * @docsPage OrderTaxCalculationStrategy | ||
| * @since 3.6.0 | ||
| */ | ||
| export class DefaultOrderTaxCalculationStrategy implements OrderTaxCalculationStrategy { | ||
| calculateOrderTotals(order: Order): OrderTotalsResult { | ||
| let subTotal = 0; | ||
| let subTotalWithTax = 0; | ||
| for (const line of order.lines ?? []) { | ||
| subTotal += line.proratedLinePrice; | ||
| subTotalWithTax += line.proratedLinePriceWithTax; | ||
| } | ||
| for (const surcharge of order.surcharges ?? []) { | ||
| subTotal += surcharge.price; | ||
| subTotalWithTax += surcharge.priceWithTax; | ||
| } | ||
|
|
||
| let shipping = 0; | ||
| let shippingWithTax = 0; | ||
| for (const shippingLine of order.shippingLines ?? []) { | ||
| shipping += shippingLine.discountedPrice; | ||
| shippingWithTax += shippingLine.discountedPriceWithTax; | ||
| } | ||
|
|
||
| return { subTotal, subTotalWithTax, shipping, shippingWithTax }; | ||
| } | ||
|
|
||
| calculateTaxSummary(order: Order): OrderTaxSummary[] { | ||
| const taxRateMap = new Map< | ||
| string, | ||
| { rate: number; base: number; tax: number; description: string } | ||
| >(); | ||
| const taxId = (taxLine: TaxLine): string => `${taxLine.description}:${taxLine.taxRate}`; | ||
| const taxableLines = [ | ||
| ...(order.lines ?? []), | ||
| ...(order.shippingLines ?? []), | ||
| ...(order.surcharges ?? []), | ||
| ]; | ||
| for (const line of taxableLines) { | ||
| if (!line.taxLines?.length) { | ||
| continue; | ||
| } | ||
| const taxRateTotal = summate(line.taxLines, 'taxRate'); | ||
| for (const taxLine of line.taxLines) { | ||
| const id = taxId(taxLine); | ||
| const row = taxRateMap.get(id); | ||
| const proportionOfTotalRate = 0 < taxLine.taxRate ? taxLine.taxRate / taxRateTotal : 0; | ||
|
|
||
| const lineBase = | ||
| line instanceof OrderLine | ||
| ? line.proratedLinePrice | ||
| : line instanceof Surcharge | ||
| ? line.price | ||
| : line.discountedPrice; | ||
| const lineWithTax = | ||
| line instanceof OrderLine | ||
| ? line.proratedLinePriceWithTax | ||
| : line instanceof Surcharge | ||
| ? line.priceWithTax | ||
| : line.discountedPriceWithTax; | ||
| const amount = Math.round((lineWithTax - lineBase) * proportionOfTotalRate); | ||
| if (row) { | ||
| row.tax += amount; | ||
| row.base += lineBase; | ||
| } else { | ||
| taxRateMap.set(id, { | ||
| tax: amount, | ||
| base: lineBase, | ||
| description: taxLine.description, | ||
| rate: taxLine.taxRate, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return Array.from(taxRateMap.entries()).map(([, row]) => ({ | ||
| taxRate: row.rate, | ||
| description: row.description, | ||
| taxBase: row.base, | ||
| taxTotal: row.tax, | ||
| })); | ||
| } | ||
| } | ||
136 changes: 136 additions & 0 deletions
136
packages/core/src/config/tax/order-level-tax-calculation-strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { OrderTaxSummary, TaxLine } from '@vendure/common/lib/generated-types'; | ||
|
|
||
| import { taxPayableOn } from '../../common/tax-utils'; | ||
| import { Order } from '../../entity/order/order.entity'; | ||
|
|
||
| import { OrderTaxCalculationStrategy, OrderTotalsResult } from './order-tax-calculation-strategy'; | ||
|
|
||
| interface TaxGroup { | ||
| rate: number; | ||
| description: string; | ||
| netBase: number; | ||
| } | ||
|
|
||
| /** | ||
| * @description | ||
| * An {@link OrderTaxCalculationStrategy} that groups net subtotals by tax rate | ||
| * and rounds once per group. This eliminates per-line rounding accumulation errors | ||
| * present in the {@link DefaultOrderTaxCalculationStrategy}. | ||
| * | ||
| * This approach is required by certain jurisdictions and ERP systems that expect | ||
| * tax to be calculated on the subtotal per tax rate rather than per line. | ||
| * | ||
| * Note that when using this strategy, the `taxTotal` in the tax summary may differ | ||
| * by ±1 minor unit from the sum of individual line-level `proratedLineTax` values. | ||
| * This is expected and is the intended behaviour. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { OrderLevelTaxCalculationStrategy, VendureConfig } from '\@vendure/core'; | ||
| * | ||
| * export const config: VendureConfig = { | ||
| * taxOptions: { | ||
| * orderTaxCalculationStrategy: new OrderLevelTaxCalculationStrategy(), | ||
| * }, | ||
| * }; | ||
| * ``` | ||
| * | ||
| * @docsCategory tax | ||
| * @docsPage OrderTaxCalculationStrategy | ||
| * @since 3.6.0 | ||
| */ | ||
| export class OrderLevelTaxCalculationStrategy implements OrderTaxCalculationStrategy { | ||
| calculateOrderTotals(order: Order): OrderTotalsResult { | ||
| const { subTotal, subTotalGroups, shipping, shippingGroups } = this.groupOrder(order); | ||
|
|
||
| let subTotalTax = 0; | ||
| for (const [, group] of subTotalGroups) { | ||
| subTotalTax += Math.round(taxPayableOn(group.netBase, group.rate)); | ||
| } | ||
|
|
||
| let shippingTax = 0; | ||
| for (const [, group] of shippingGroups) { | ||
| shippingTax += Math.round(taxPayableOn(group.netBase, group.rate)); | ||
| } | ||
|
|
||
| return { | ||
| subTotal, | ||
| subTotalWithTax: subTotal + subTotalTax, | ||
| shipping, | ||
| shippingWithTax: shipping + shippingTax, | ||
| }; | ||
| } | ||
|
|
||
| calculateTaxSummary(order: Order): OrderTaxSummary[] { | ||
| const { subTotalGroups, shippingGroups } = this.groupOrder(order); | ||
|
|
||
| const merged = new Map<string, TaxGroup & { tax: number }>(); | ||
| for (const groups of [subTotalGroups, shippingGroups]) { | ||
| for (const [key, group] of groups) { | ||
| const roundedTax = Math.round(taxPayableOn(group.netBase, group.rate)); | ||
| const existing = merged.get(key); | ||
| if (existing) { | ||
| existing.netBase += group.netBase; | ||
| existing.tax += roundedTax; | ||
| } else { | ||
| merged.set(key, { ...group, tax: roundedTax }); | ||
| } | ||
| } | ||
| } | ||
| const taxSummary: OrderTaxSummary[] = []; | ||
| for (const [, group] of merged) { | ||
| taxSummary.push({ | ||
| taxRate: group.rate, | ||
| description: group.description, | ||
| taxBase: group.netBase, | ||
| taxTotal: group.tax, | ||
| }); | ||
| } | ||
| return taxSummary; | ||
| } | ||
|
|
||
| private groupOrder(order: Order) { | ||
| let subTotal = 0; | ||
| let shipping = 0; | ||
| const subTotalGroups = new Map<string, TaxGroup>(); | ||
| const shippingGroups = new Map<string, TaxGroup>(); | ||
|
|
||
| for (const line of order.lines ?? []) { | ||
| subTotal += line.proratedLinePrice; | ||
| this.accumulateIntoGroups(subTotalGroups, line.taxLines, line.proratedLinePrice); | ||
| } | ||
| for (const surcharge of order.surcharges ?? []) { | ||
| subTotal += surcharge.price; | ||
| this.accumulateIntoGroups(subTotalGroups, surcharge.taxLines, surcharge.price); | ||
| } | ||
| for (const shippingLine of order.shippingLines ?? []) { | ||
| shipping += shippingLine.discountedPrice; | ||
| this.accumulateIntoGroups(shippingGroups, shippingLine.taxLines, shippingLine.discountedPrice); | ||
| } | ||
|
|
||
| return { subTotal, subTotalGroups, shipping, shippingGroups }; | ||
| } | ||
|
|
||
| private accumulateIntoGroups( | ||
| groups: Map<string, TaxGroup>, | ||
| taxLines: TaxLine[] | undefined, | ||
| lineNetBase: number, | ||
| ) { | ||
| if (!taxLines?.length) { | ||
| return; | ||
| } | ||
| for (const taxLine of taxLines) { | ||
| const key = `${taxLine.description}:${taxLine.taxRate}`; | ||
| const existing = groups.get(key); | ||
| if (existing) { | ||
| existing.netBase += lineNetBase; | ||
| } else { | ||
| groups.set(key, { | ||
| rate: taxLine.taxRate, | ||
| description: taxLine.description, | ||
| netBase: lineNetBase, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.