Skip to content

Commit

Permalink
feat(medusa): cleanup calculate tax amount utils and its usage (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p authored and carlos-r-l-rodrigues committed Sep 4, 2022
1 parent 3f7dcb2 commit 33024d1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
14 changes: 12 additions & 2 deletions packages/medusa/src/services/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ class PricingService extends TransactionBaseService {
}

if (variantPricing.calculated_price !== null) {
const includesTax = !!(
this.featureFlagRouter.isFeatureEnabled(
TaxInclusivePricingFeatureFlag.key
) && variantPricing.calculated_price_includes_tax
)
taxedPricing.calculated_tax = Math.round(
calculatePriceTaxAmount({
price: variantPricing.calculated_price,
taxRate: rate,
includesTax: variantPricing.calculated_price_includes_tax ?? false,
includesTax,
})
)

Expand All @@ -136,11 +141,16 @@ class PricingService extends TransactionBaseService {
}

if (variantPricing.original_price !== null) {
const includesTax = !!(
this.featureFlagRouter.isFeatureEnabled(
TaxInclusivePricingFeatureFlag.key
) && variantPricing.original_price_includes_tax
)
taxedPricing.original_tax = Math.round(
calculatePriceTaxAmount({
price: variantPricing.original_price,
taxRate: rate,
includesTax: variantPricing.original_price_includes_tax ?? false,
includesTax,
})
)

Expand Down
6 changes: 5 additions & 1 deletion packages/medusa/src/services/totals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,17 @@ class TotalsService extends TransactionBaseService {
if (isOrder(cartOrOrder) && cartOrOrder.tax_rate !== null) {
const taxRate = cartOrOrder.tax_rate / 100

const includesTax =
this.featureFlagRouter_.isFeatureEnabled(
TaxInclusivePricingFeatureFlag.key
) && lineItem.includes_tax
const taxIncludedInPrice = !lineItem.includes_tax
? 0
: Math.round(
calculatePriceTaxAmount({
price: lineItem.unit_price,
taxRate: taxRate,
includesTax: lineItem.includes_tax,
includesTax,
})
)
lineItemTotals.subtotal =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ describe("TaxCalculationStrategy", () => {
const calcStrat = new TaxCalculationStrategy({
featureFlagRouter,
})

const val = await calcStrat.calculate(items, taxLines, context)
expect(val).toEqual(expected)

Expand Down
13 changes: 6 additions & 7 deletions packages/medusa/src/strategies/tax-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ class TaxCalculationStrategy implements ITaxCalculationStrategy {
const allocations = context.allocation_map[item.id] || {}

const filteredTaxLines = taxLines.filter((tl) => tl.item_id === item.id)

let taxableAmount
if (
const includesTax =
this.featureFlagRouter_.isFeatureEnabled(
TaxInclusivePricingFeatureFlag.key
) &&
item.includes_tax
) {
) && item.includes_tax

let taxableAmount
if (includesTax) {
const taxRate = filteredTaxLines.reduce(
(accRate: number, nextLineItemTaxLine: LineItemTaxLine) => {
return accRate + (nextLineItemTaxLine.rate || 0) / 100
Expand All @@ -66,7 +65,7 @@ class TaxCalculationStrategy implements ITaxCalculationStrategy {
calculatePriceTaxAmount({
price: item.unit_price,
taxRate,
includesTax: item.includes_tax,
includesTax,
})
)
taxableAmount = (item.unit_price - taxIncludedInPrice) * item.quantity
Expand Down
8 changes: 1 addition & 7 deletions packages/medusa/src/utils/calculate-price-tax-amount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { featureFlagRouter } from "../loaders/feature-flags"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"

/**
* Return the tax amount that
* - is includes in the price if it is tax inclusive
Expand All @@ -18,10 +15,7 @@ export function calculatePriceTaxAmount({
includesTax?: boolean
taxRate: number
}): number {
if (
featureFlagRouter.isFeatureEnabled(TaxInclusivePricingFeatureFlag.key) &&
includesTax
) {
if (includesTax) {
return (taxRate * price) / (1 + taxRate)
}

Expand Down

0 comments on commit 33024d1

Please sign in to comment.