-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): Make tax zone/price calculations configurable
Relates to #31
- Loading branch information
1 parent
fe05f44
commit 52ecc37
Showing
19 changed files
with
270 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { RequestContext } from '../../api/common/request-context'; | ||
import { idsAreEqual } from '../../common/utils'; | ||
import { TaxCategory } from '../../entity'; | ||
import { TaxCalculationResult } from '../../service/helpers/tax-calculator/tax-calculator'; | ||
import { TaxRateService } from '../../service/services/tax-rate.service'; | ||
|
||
import { TaxCalculationArgs, TaxCalculationStrategy } from './tax-calculation-strategy'; | ||
|
||
export class DefaultTaxCalculationStrategy implements TaxCalculationStrategy { | ||
calculate(args: TaxCalculationArgs): TaxCalculationResult { | ||
const { inputPrice, activeTaxZone, ctx, taxCategory, taxRateService } = args; | ||
let price = 0; | ||
let priceWithTax = 0; | ||
let priceWithoutTax = 0; | ||
let priceIncludesTax = false; | ||
const taxRate = taxRateService.getApplicableTaxRate(activeTaxZone, taxCategory); | ||
|
||
if (ctx.channel.pricesIncludeTax) { | ||
const isDefaultZone = idsAreEqual(activeTaxZone.id, ctx.channel.defaultTaxZone.id); | ||
const taxRateForDefaultZone = taxRateService.getApplicableTaxRate( | ||
ctx.channel.defaultTaxZone, | ||
taxCategory, | ||
); | ||
priceWithoutTax = taxRateForDefaultZone.netPriceOf(inputPrice); | ||
|
||
if (isDefaultZone) { | ||
priceIncludesTax = true; | ||
price = inputPrice; | ||
priceWithTax = inputPrice; | ||
} else { | ||
price = priceWithoutTax; | ||
priceWithTax = taxRate.grossPriceOf(priceWithoutTax); | ||
} | ||
} else { | ||
const netPrice = inputPrice; | ||
price = netPrice; | ||
priceWithTax = netPrice + taxRate.taxPayableOn(netPrice); | ||
priceWithoutTax = netPrice; | ||
} | ||
|
||
return { | ||
price, | ||
priceIncludesTax, | ||
priceWithTax, | ||
priceWithoutTax, | ||
}; | ||
} | ||
} |
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,9 @@ | ||
import { Channel, Order, Zone } from '../../entity'; | ||
|
||
import { TaxZoneStrategy } from './tax-zone-strategy'; | ||
|
||
export class DefaultTaxZoneStrategy implements TaxZoneStrategy { | ||
determineTaxZone(zones: Zone[], channel: Channel, order?: Order): Zone { | ||
return channel.defaultTaxZone; | ||
} | ||
} |
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,19 @@ | ||
import { RequestContext } from '../../api/common/request-context'; | ||
import { TaxCategory, Zone } from '../../entity'; | ||
import { TaxCalculationResult } from '../../service/helpers/tax-calculator/tax-calculator'; | ||
import { TaxRateService } from '../../service/services/tax-rate.service'; | ||
|
||
export interface TaxCalculationArgs { | ||
inputPrice: number; | ||
taxCategory: TaxCategory; | ||
activeTaxZone: Zone; | ||
ctx: RequestContext; | ||
taxRateService: TaxRateService; | ||
} | ||
|
||
/** | ||
* Defines how taxes are calculated based on the input price, tax zone and current request context. | ||
*/ | ||
export interface TaxCalculationStrategy { | ||
calculate(args: TaxCalculationArgs): TaxCalculationResult; | ||
} |
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,8 @@ | ||
import { Channel, Order, Zone } from '../../entity'; | ||
|
||
/** | ||
* Defines how the active Zone is determined for the purposes of calculating taxes. | ||
*/ | ||
export interface TaxZoneStrategy { | ||
determineTaxZone(zones: Zone[], channel: Channel, order?: Order): Zone; | ||
} |
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
Oops, something went wrong.