Skip to content

Commit

Permalink
feat(medusa): cleanup calculate tax amount utils and its usage
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Sep 1, 2022
1 parent 2be6626 commit ee5690a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
15 changes: 13 additions & 2 deletions packages/medusa/src/services/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "../interfaces/price-selection-strategy"
import { FlagRouter } from "../utils/flag-router"
import { calculatePriceTaxAmount } from "../utils"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"

type InjectedDependencies = {
manager: EntityManager
Expand Down Expand Up @@ -121,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 +142,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 @@ -776,13 +776,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
11 changes: 3 additions & 8 deletions packages/medusa/src/strategies/__tests__/tax-calculation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import TaxCalculationStrategy from "../tax-calculation"
import TaxInclusivePricingFeatureFlag from "../../loaders/feature-flags/tax-inclusive-pricing";
import { featureFlagRouter } from "../../loaders/feature-flags";
import { FlagRouter } from "../../utils/flag-router";

const toTest = [
[
Expand Down Expand Up @@ -169,19 +170,13 @@ describe("TaxCalculationStrategy", () => {
test.each(toTest)(
"%s",
async (title, { items, taxLines, context, expected, flags }) => {
if (flags) {
Object.entries(flags).forEach(([key, value]) => featureFlagRouter.setFlag(key, value))
}

const featureFlagRouter = new FlagRouter(flags ?? {})
const calcStrat = new TaxCalculationStrategy({
featureFlagRouter
})

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

if (flags) {
Object.entries(flags).forEach(([key]) => featureFlagRouter.setFlag(key, false))
}
}
)
})
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
5 changes: 1 addition & 4 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,7 +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 ee5690a

Please sign in to comment.