Files
medusa-store/integration-tests/api/factories/simple-region-factory.ts
T
Sebastian Rindom 39f2c0c15e fix(medusa): calculates correct taxes and totals on order with gift cards (#1807)
**What**
Since the release of the Tax API the line item totals calculations on orders with gift cards have been wrong. To understand the bug consider the below order:

Region:
- tax_rate: 25%
- gift_cards_taxable: true

Order:
- applied gift card: 1000
- items: 
  - A: unit_price: 1000
  - B: unit_price: 500
- Subtotal: 1500

**Previous calculation method**
1. Determine how much of the gift card is used for each item using `item_total / subtotal * gift_card_amount`:
  - Item A: 1000/1500 * 1000 = 666.67
  - Item B: 500/1500 * 1000 = 333.33
2. Calculate line item totals including taxes using `(unit_price - gift_card) * (1 + tax_rate)`
  - Item A: 1000 - 666.67 = 333.33; vat amount -> 83.33
  - Item B: 500 - 333.33 = 166.67; vat amount -> 41.67
3. Add up the line item totals: order subtotal = 500; vat amount = 125; total = 625

This is all correct at the totals level; but at the line item level we should still use the "original prices" i.e. the line item total for item a should be (1000 * 1.25) = 1250 with a tax amount of 250. 

**New calculation method**
1. Use default totals calculations
  - Item A: subtotal: 1000, tax_total: 250, total: 1250
  - Item B: subtotal: 500, tax_total: 125, total: 625
2. Add up the line item totals: subtotal: 1500, tax_total: 375, total: 1875
3. Reduce total with gift card: subtotal: 1500 - 1000 = 500, tax_total: 375 - 250 = 125, total = 625

Totals can now be forwarded correctly to accounting plugins.

Fixes CORE-310.
2022-07-11 12:18:43 +00:00

50 lines
1.2 KiB
TypeScript

import { Connection } from "typeorm"
import faker from "faker"
import { Region } from "@medusajs/medusa"
export type RegionFactoryData = {
id?: string
name?: string
currency_code?: string
tax_rate?: number
countries?: string[]
automatic_taxes?: boolean
gift_cards_taxable?: boolean
}
export const simpleRegionFactory = async (
connection: Connection,
data: RegionFactoryData = {},
seed?: number
): Promise<Region> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const regionId = data.id || `simple-region-${Math.random() * 1000}`
const r = manager.create(Region, {
id: regionId,
name: data.name || "Test Region",
currency_code: data.currency_code || "usd",
tax_rate: data.tax_rate || 0,
payment_providers: [{ id: "test-pay" }],
gift_cards_taxable: data.gift_cards_taxable ?? true,
automatic_taxes:
typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true,
})
const region = await manager.save(r)
const countries = data.countries || ["us"]
for (const cc of countries) {
await manager.query(
`UPDATE "country" SET region_id='${regionId}' WHERE iso_2 = '${cc}'`
)
}
return region
}