-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Implement TaxLineCalculationStrategy
Relates to #307
- Loading branch information
1 parent
d2a971c
commit 95663b4
Showing
22 changed files
with
698 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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
17 changes: 17 additions & 0 deletions
17
packages/core/src/config/tax/default-tax-line-calculation-strategy.ts
This file contains 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,17 @@ | ||
import { TaxLine } from '@vendure/common/lib/generated-types'; | ||
|
||
import { CalculateTaxLinesArgs, TaxLineCalculationStrategy } from './tax-line-calculation-strategy'; | ||
|
||
/** | ||
* @description | ||
* The default {@link TaxLineCalculationStrategy} which applies a single TaxLine to the OrderItem | ||
* based on the applicable {@link TaxRate}. | ||
* | ||
* @docsCategory tax | ||
*/ | ||
export class DefaultTaxLineCalculationStrategy implements TaxLineCalculationStrategy { | ||
calculate(args: CalculateTaxLinesArgs): TaxLine[] { | ||
const { orderItem, applicableTaxRate } = args; | ||
return [applicableTaxRate.apply(orderItem.proratedUnitPrice)]; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/core/src/config/tax/tax-line-calculation-strategy.ts
This file contains 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,47 @@ | ||
import { TaxLine } from '@vendure/common/lib/generated-types'; | ||
|
||
import { RequestContext } from '../../api/common/request-context'; | ||
import { InjectableStrategy } from '../../common/types/injectable-strategy'; | ||
import { OrderItem } from '../../entity/order-item/order-item.entity'; | ||
import { OrderLine } from '../../entity/order-line/order-line.entity'; | ||
import { Order } from '../../entity/order/order.entity'; | ||
import { TaxRate } from '../../entity/tax-rate/tax-rate.entity'; | ||
|
||
/** | ||
* @description | ||
* This strategy defines how the TaxLines on OrderItems are calculated. By default, | ||
* the {@link DefaultTaxLineCalculationStrategy} is used, which directly applies | ||
* a single TaxLine based on the applicable {@link TaxRate}. | ||
* | ||
* However, custom strategies may use any suitable method for calculating TaxLines. | ||
* For example, a third-party tax API or a lookup of a custom tax table may be used. | ||
* | ||
* @docsCategory tax | ||
* @docsPage TaxLineCalculationStrategy | ||
* @docsWeight 0 | ||
*/ | ||
export interface TaxLineCalculationStrategy extends InjectableStrategy { | ||
/** | ||
* @description | ||
* This method is called when calculating the Order prices. Since it will be called | ||
* whenever an Order is modified in some way (adding/removing items, applying promotions, | ||
* setting ShippingMethod etc), care should be taken so that calling the function does | ||
* not adversely impact overall performance. For example, by using caching and only | ||
* calling external APIs when absolutely necessary. | ||
*/ | ||
calculate(args: CalculateTaxLinesArgs): TaxLine[] | Promise<TaxLine[]>; | ||
} | ||
|
||
/** | ||
* @description | ||
* | ||
* @docsCategory tax | ||
* @docsPage TaxLineCalculationStrategy | ||
*/ | ||
export interface CalculateTaxLinesArgs { | ||
ctx: RequestContext; | ||
order: Order; | ||
orderLine: OrderLine; | ||
orderItem: OrderItem; | ||
applicableTaxRate: TaxRate; | ||
} |
This file contains 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
Oops, something went wrong.