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.
This commit is contained in:
Sebastian Rindom authored and GitHub committed 2022-07-11 12:18:43 +00:00
1 parent 3e197e3adf
commit 39f2c0c15e
19 files changed
+413 -83

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
Calculates correct taxes and totals on line items when carts and orders have gift cards
@@ -0,0 +1,170 @@
const path = require("path")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const {
simpleRegionFactory,
simpleCartFactory,
simpleGiftCardFactory,
simpleProductFactory,
} = require("../../factories")
jest.setTimeout(30000)
describe("Order Totals", () => {
let medusaProcess
let dbConnection
const doAfterEach = async () => {
const db = useDb()
return await db.teardown()
}
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
try {
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
} catch (error) {
console.log(error)
}
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
medusaProcess.kill()
})
afterEach(async () => {
return await doAfterEach()
})
test("calculates totals correctly for order with non-taxable gift card", async () => {
await adminSeeder(dbConnection)
await simpleProductFactory(dbConnection, {
variants: [
{ id: "variant_1", prices: [{ currency: "usd", amount: 95600 }] },
{ id: "variant_2", prices: [{ currency: "usd", amount: 79600 }] },
],
})
const region = await simpleRegionFactory(dbConnection, {
gift_cards_taxable: false,
tax_rate: 25,
})
const cart = await simpleCartFactory(dbConnection, {
id: "test-cart",
email: "testnation@medusajs.com",
region: region.id,
line_items: [],
})
const giftCard = await simpleGiftCardFactory(dbConnection, {
region_id: region.id,
value: 160000,
balance: 160000,
})
const api = useApi()
await api.post("/store/carts/test-cart/line-items", {
quantity: 1,
variant_id: "variant_1",
})
await api.post("/store/carts/test-cart/line-items", {
quantity: 1,
variant_id: "variant_2",
})
await api.post("/store/carts/test-cart", {
gift_cards: [{ code: giftCard.code }],
})
await api.post(`/store/carts/${cart.id}/payment-sessions`)
const response = await api.post(`/store/carts/test-cart/complete`)
expect(response.status).toEqual(200)
expect(response.data.type).toEqual("order")
const orderId = response.data.data.id
const { data } = await api.get(`/admin/orders/${orderId}`, {
headers: { Authorization: `Bearer test_token` },
})
expect(data.order.gift_card_transactions).toEqual([
expect.objectContaining({
amount: 160000,
is_taxable: false,
tax_rate: null,
}),
])
expect(data.order.gift_card_total).toEqual(160000)
expect(data.order.gift_card_tax_total).toEqual(0)
expect(data.order.total).toEqual(59000)
})
test("calculates totals correctly for order with taxable gift card", async () => {
await adminSeeder(dbConnection)
await simpleProductFactory(dbConnection, {
variants: [
{ id: "variant_1", prices: [{ currency: "usd", amount: 95600 }] },
{ id: "variant_2", prices: [{ currency: "usd", amount: 79600 }] },
],
})
const region = await simpleRegionFactory(dbConnection, {
gift_cards_taxable: true,
tax_rate: 25,
})
const cart = await simpleCartFactory(dbConnection, {
id: "test-cart",
email: "testnation@medusajs.com",
region: region.id,
line_items: [],
})
const giftCard = await simpleGiftCardFactory(dbConnection, {
region_id: region.id,
value: 160000,
balance: 160000,
})
const api = useApi()
await api.post("/store/carts/test-cart/line-items", {
quantity: 1,
variant_id: "variant_1",
})
await api.post("/store/carts/test-cart/line-items", {
quantity: 1,
variant_id: "variant_2",
})
await api.post("/store/carts/test-cart", {
gift_cards: [{ code: giftCard.code }],
})
await api.post(`/store/carts/${cart.id}/payment-sessions`)
const response = await api.post(`/store/carts/test-cart/complete`)
expect(response.status).toEqual(200)
expect(response.data.type).toEqual("order")
const orderId = response.data.data.id
const { data } = await api.get(`/admin/orders/${orderId}`, {
headers: { Authorization: `Bearer test_token` },
})
expect(data.order.gift_card_transactions).toEqual([
expect.objectContaining({
amount: 160000,
is_taxable: true,
tax_rate: 25,
}),
])
expect(data.order.gift_card_total).toEqual(160000)
expect(data.order.gift_card_tax_total).toEqual(40000)
expect(data.order.tax_total).toEqual(3800)
expect(data.order.total).toEqual(19000)
})
})
+1
View File
@@ -1,3 +1,4 @@
export * from "./simple-gift-card-factory"
export * from "./simple-payment-factory" export * from "./simple-payment-factory"
export * from "./simple-batch-job-factory" export * from "./simple-batch-job-factory"
export * from "./simple-discount-factory" export * from "./simple-discount-factory"
@@ -0,0 +1,33 @@
import { GiftCard } from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
export type GiftCardFactoryData = {
id?: string
code?: string
region_id: string
value: number
balance: number
}
export const simpleGiftCardFactory = async (
connection: Connection,
data: GiftCardFactoryData,
seed?: number
): Promise<GiftCard> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const toSave = manager.create(GiftCard, {
id: data.id,
code: data.code ?? "TESTGCCODE",
region_id: data.region_id,
value: data.value,
balance: data.balance,
})
return await manager.save(toSave)
}
@@ -112,5 +112,9 @@ export const simpleProductFactory = async (
await simpleProductVariantFactory(connection, factoryData) await simpleProductVariantFactory(connection, factoryData)
} }
return manager.findOne(Product, { id: prodId }, { relations: ["tags", "variants", "variants.prices"] }) return manager.findOne(
Product,
{ id: prodId },
{ relations: ["tags", "variants", "variants.prices"] }
)
} }
@@ -9,6 +9,7 @@ export type RegionFactoryData = {
tax_rate?: number tax_rate?: number
countries?: string[] countries?: string[]
automatic_taxes?: boolean automatic_taxes?: boolean
gift_cards_taxable?: boolean
} }
export const simpleRegionFactory = async ( export const simpleRegionFactory = async (
@@ -29,6 +30,7 @@ export const simpleRegionFactory = async (
currency_code: data.currency_code || "usd", currency_code: data.currency_code || "usd",
tax_rate: data.tax_rate || 0, tax_rate: data.tax_rate || 0,
payment_providers: [{ id: "test-pay" }], payment_providers: [{ id: "test-pay" }],
gift_cards_taxable: data.gift_cards_taxable ?? true,
automatic_taxes: automatic_taxes:
typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true, typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true,
}) })
@@ -753,6 +753,7 @@ Object {
"external_id": null, "external_id": null,
"fulfillment_status": "canceled", "fulfillment_status": "canceled",
"fulfillments": Array [], "fulfillments": Array [],
"gift_card_tax_total": 0,
"gift_card_total": "0.00 USD", "gift_card_total": "0.00 USD",
"gift_card_transactions": Array [], "gift_card_transactions": Array [],
"gift_cards": Array [], "gift_cards": Array [],
@@ -975,6 +976,7 @@ Object {
"external_id": null, "external_id": null,
"fulfillment_status": "fulfilled", "fulfillment_status": "fulfilled",
"fulfillments": Array [], "fulfillments": Array [],
"gift_card_tax_total": 0,
"gift_card_total": "0.00 USD", "gift_card_total": "0.00 USD",
"gift_card_transactions": Array [], "gift_card_transactions": Array [],
"gift_cards": Array [], "gift_cards": Array [],
@@ -1244,6 +1246,7 @@ Object {
"updated_at": Any<Date>, "updated_at": Any<Date>,
}, },
], ],
"gift_card_tax_total": 0,
"gift_card_total": 0, "gift_card_total": 0,
"gift_card_transactions": Array [], "gift_card_transactions": Array [],
"gift_cards": Array [], "gift_cards": Array [],
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from "typeorm"
export class taxedGiftCardTransactions1657098186554
implements MigrationInterface
{
name = "taxedGiftCardTransactions1657098186554"
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "gift_card_transaction" ADD "is_taxable" boolean`
)
await queryRunner.query(
`ALTER TABLE "gift_card_transaction" ADD "tax_rate" real`
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "gift_card_transaction" DROP COLUMN "is_taxable"`
)
await queryRunner.query(
`ALTER TABLE "gift_card_transaction" DROP COLUMN "tax_rate"`
)
}
}
+1
View File
@@ -252,6 +252,7 @@ export class Cart extends SoftDeletableEntity {
subtotal?: number subtotal?: number
refundable_amount?: number refundable_amount?: number
gift_card_total?: number gift_card_total?: number
gift_card_tax_total?: number
@AfterLoad() @AfterLoad()
private afterLoad(): void { private afterLoad(): void {
@@ -42,6 +42,12 @@ export class GiftCardTransaction {
@CreateDateColumn({ type: resolveDbType("timestamptz") }) @CreateDateColumn({ type: resolveDbType("timestamptz") })
created_at: Date created_at: Date
@Column({ nullable: true })
is_taxable: boolean
@Column({ type: "real", nullable: true })
tax_rate: number | null
@BeforeInsert() @BeforeInsert()
private beforeInsert(): void { private beforeInsert(): void {
this.id = generateEntityId(this.id, "gct") this.id = generateEntityId(this.id, "gct")
+1
View File
@@ -249,6 +249,7 @@ export class Order extends BaseEntity {
paid_total: number paid_total: number
refundable_amount: number refundable_amount: number
gift_card_total: number gift_card_total: number
gift_card_tax_total: number
@BeforeInsert() @BeforeInsert()
private async beforeInsert(): Promise<void> { private async beforeInsert(): Promise<void> {
@@ -1,13 +1,19 @@
import { IdMap } from "medusa-test-utils" import { IdMap } from "medusa-test-utils"
export const TotalsServiceMock = { export const TotalsServiceMock = {
getTotal: jest.fn().mockImplementation(cart => { getTotal: jest.fn().mockImplementation((cart) => {
if (cart.total) { if (cart.total) {
return cart.total return cart.total
} }
return 0 return 0
}), }),
getSubtotal: jest.fn().mockImplementation(cart => { getGiftCardableAmount: jest.fn().mockImplementation((cart) => {
if (cart.subtotal) {
return cart.subtotal
}
return 0
}),
getSubtotal: jest.fn().mockImplementation((cart) => {
if (cart.subtotal) { if (cart.subtotal) {
return cart.subtotal return cart.subtotal
} }
+24 -20
View File
@@ -8,6 +8,9 @@ describe("OrderService", () => {
getTotal: (o) => { getTotal: (o) => {
return o.total || 0 return o.total || 0
}, },
getGiftCardableAmount: (o) => {
return o.subtotal || 0
},
getRefundedTotal: (o) => { getRefundedTotal: (o) => {
return o.refunded_total || 0 return o.refunded_total || 0
}, },
@@ -33,7 +36,7 @@ describe("OrderService", () => {
const eventBusService = { const eventBusService = {
emit: jest.fn(), emit: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -78,20 +81,20 @@ describe("OrderService", () => {
}) })
const lineItemService = { const lineItemService = {
update: jest.fn(), update: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
const shippingOptionService = { const shippingOptionService = {
updateShippingMethod: jest.fn(), updateShippingMethod: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
const giftCardService = { const giftCardService = {
update: jest.fn(), update: jest.fn(),
createTransaction: jest.fn(), createTransaction: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -103,7 +106,7 @@ describe("OrderService", () => {
cancelPayment: jest.fn().mockImplementation((payment) => { cancelPayment: jest.fn().mockImplementation((payment) => {
return Promise.resolve({ ...payment, status: "cancelled" }) return Promise.resolve({ ...payment, status: "cancelled" })
}), }),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -142,7 +145,7 @@ describe("OrderService", () => {
total: 100, total: 100,
}) })
}), }),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -281,6 +284,7 @@ describe("OrderService", () => {
id: "test", id: "test",
currency_code: "eur", currency_code: "eur",
name: "test", name: "test",
gift_cards_taxable: true,
tax_rate: 25, tax_rate: 25,
}, },
shipping_address_id: "1234", shipping_address_id: "1234",
@@ -339,6 +343,8 @@ describe("OrderService", () => {
expect(giftCardService.createTransaction).toHaveBeenCalledWith({ expect(giftCardService.createTransaction).toHaveBeenCalledWith({
gift_card_id: "gid", gift_card_id: "gid",
order_id: "id", order_id: "id",
is_taxable: true,
tax_rate: 25,
amount: 80, amount: 80,
}) })
@@ -633,14 +639,14 @@ describe("OrderService", () => {
const fulfillmentService = { const fulfillmentService = {
cancelFulfillment: jest.fn(), cancelFulfillment: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
const paymentProviderService = { const paymentProviderService = {
cancelPayment: jest.fn(), cancelPayment: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -737,7 +743,7 @@ describe("OrderService", () => {
? Promise.reject() ? Promise.reject()
: Promise.resolve({ ...p, captured_at: "notnull" }) : Promise.resolve({ ...p, captured_at: "notnull" })
), ),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -842,7 +848,7 @@ describe("OrderService", () => {
const lineItemService = { const lineItemService = {
update: jest.fn(), update: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -855,7 +861,7 @@ describe("OrderService", () => {
}, },
]) ])
}), }),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1022,7 +1028,7 @@ describe("OrderService", () => {
}) })
} }
}), }),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1091,7 +1097,7 @@ describe("OrderService", () => {
.mockImplementation((p) => .mockImplementation((p) =>
p.id === "payment_fail" ? Promise.reject() : Promise.resolve() p.id === "payment_fail" ? Promise.reject() : Promise.resolve()
), ),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1232,7 +1238,7 @@ describe("OrderService", () => {
.fn() .fn()
.mockImplementation(() => Promise.resolve({})), .mockImplementation(() => Promise.resolve({})),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1366,7 +1372,7 @@ describe("OrderService", () => {
const lineItemService = { const lineItemService = {
update: jest.fn(), update: jest.fn(),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1389,7 +1395,7 @@ describe("OrderService", () => {
], ],
}) })
}), }),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
@@ -1416,9 +1422,7 @@ describe("OrderService", () => {
) )
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1) expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
expect( expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
fulfillmentService.createShipment
).toHaveBeenCalledWith(
IdMap.getId("fulfillment"), IdMap.getId("fulfillment"),
[{ tracking_number: "1234" }, { tracking_number: "2345" }], [{ tracking_number: "1234" }, { tracking_number: "2345" }],
{ metadata: undefined, no_notification: true } { metadata: undefined, no_notification: true }
@@ -1510,7 +1514,7 @@ describe("OrderService", () => {
refundPayment: jest refundPayment: jest
.fn() .fn()
.mockImplementation((p) => Promise.resolve({ id: "ref" })), .mockImplementation((p) => Promise.resolve({ id: "ref" })),
withTransaction: function() { withTransaction: function () {
return this return this
}, },
} }
+5 -2
View File
@@ -234,9 +234,12 @@ class CartService extends TransactionBaseService<CartService> {
options.force_taxes options.force_taxes
) )
break break
case "gift_card_total": case "gift_card_total": {
totals.gift_card_total = this.totalsService_.getGiftCardTotal(cart) const giftCardBreakdown = this.totalsService_.getGiftCardTotal(cart)
totals.gift_card_total = giftCardBreakdown.total
totals.gift_card_tax_total = giftCardBreakdown.tax_total
break break
}
case "subtotal": case "subtotal":
totals.subtotal = this.totalsService_.getSubtotal(cart) totals.subtotal = this.totalsService_.getSubtotal(cart)
break break
+37 -35
View File
@@ -464,20 +464,21 @@ class OrderService extends BaseService {
*/ */
async createFromCart(cartId) { async createFromCart(cartId) {
return this.atomicPhase_(async (manager) => { return this.atomicPhase_(async (manager) => {
const cart = await this.cartService_ const cartService = this.cartService_.withTransaction(manager)
.withTransaction(manager) const inventoryService = this.inventoryService_.withTransaction(manager)
.retrieve(cartId, {
select: ["subtotal", "total"], const cart = await cartService.retrieve(cartId, {
relations: [ select: ["subtotal", "total"],
"region", relations: [
"payment", "region",
"items", "payment",
"discounts", "items",
"discounts.rule", "discounts",
"gift_cards", "discounts.rule",
"shipping_methods", "gift_cards",
], "shipping_methods",
}) ],
})
if (cart.items.length === 0) { if (cart.items.length === 0) {
throw new MedusaError( throw new MedusaError(
@@ -490,18 +491,17 @@ class OrderService extends BaseService {
for (const item of cart.items) { for (const item of cart.items) {
try { try {
await this.inventoryService_ await inventoryService.confirmInventory(
.withTransaction(manager) item.variant_id,
.confirmInventory(item.variant_id, item.quantity) item.quantity
)
} catch (err) { } catch (err) {
if (payment) { if (payment) {
await this.paymentProviderService_ await this.paymentProviderService_
.withTransaction(manager) .withTransaction(manager)
.cancelPayment(payment) .cancelPayment(payment)
} }
await this.cartService_ await cartService.update(cart.id, { payment_authorized_at: null })
.withTransaction(manager)
.update(cart.id, { payment_authorized_at: null })
throw err throw err
} }
} }
@@ -564,8 +564,7 @@ class OrderService extends BaseService {
toCreate.no_notification = draft.no_notification_order toCreate.no_notification = draft.no_notification_order
} }
const o = await orderRepo.create(toCreate) const o = orderRepo.create(toCreate)
const result = await orderRepo.save(o) const result = await orderRepo.save(o)
if (total !== 0) { if (total !== 0) {
@@ -576,19 +575,25 @@ class OrderService extends BaseService {
}) })
} }
let gcBalance = cart.subtotal let gcBalance = await this.totalsService_.getGiftCardableAmount(cart)
const gcService = this.giftCardService_.withTransaction(manager)
for (const g of cart.gift_cards) { for (const g of cart.gift_cards) {
const newBalance = Math.max(0, g.balance - gcBalance) const newBalance = Math.max(0, g.balance - gcBalance)
const usage = g.balance - newBalance const usage = g.balance - newBalance
await this.giftCardService_.withTransaction(manager).update(g.id, { await gcService.update(g.id, {
balance: newBalance, balance: newBalance,
disabled: newBalance === 0, disabled: newBalance === 0,
}) })
await this.giftCardService_.withTransaction(manager).createTransaction({ await gcService.createTransaction({
gift_card_id: g.id, gift_card_id: g.id,
order_id: result.id, order_id: result.id,
amount: usage, amount: usage,
is_taxable: cart.region.gift_cards_taxable,
tax_rate: cart.region.gift_cards_taxable
? cart.region.tax_rate
: null,
}) })
gcBalance = gcBalance - usage gcBalance = gcBalance - usage
@@ -600,16 +605,13 @@ class OrderService extends BaseService {
.updateShippingMethod(method.id, { order_id: result.id }) .updateShippingMethod(method.id, { order_id: result.id })
} }
const lineItemService = this.lineItemService_.withTransaction(manager)
for (const item of cart.items) { for (const item of cart.items) {
await this.lineItemService_ await lineItemService.update(item.id, { order_id: result.id })
.withTransaction(manager)
.update(item.id, { order_id: result.id })
} }
for (const item of cart.items) { for (const item of cart.items) {
await this.inventoryService_ await inventoryService.adjustInventory(item.variant_id, -item.quantity)
.withTransaction(manager)
.adjustInventory(item.variant_id, -item.quantity)
} }
await this.eventBus_ await this.eventBus_
@@ -619,9 +621,7 @@ class OrderService extends BaseService {
no_notification: result.no_notification, no_notification: result.no_notification,
}) })
await this.cartService_ await cartService.update(cart.id, { completed_at: new Date() })
.withTransaction(manager)
.update(cart.id, { completed_at: new Date() })
return result return result
}) })
@@ -1383,7 +1383,9 @@ class OrderService extends BaseService {
break break
} }
case "gift_card_total": { case "gift_card_total": {
order.gift_card_total = this.totalsService_.getGiftCardTotal(order) const giftCardBreakdown = this.totalsService_.getGiftCardTotal(order)
order.gift_card_total = giftCardBreakdown.total
order.gift_card_tax_total = giftCardBreakdown.tax_total
break break
} }
case "discount_total": { case "discount_total": {
+85 -17
View File
@@ -1,4 +1,3 @@
import _ from "lodash"
import { MedusaError } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
import { ITaxCalculationStrategy, TaxCalculationContext } from "../interfaces" import { ITaxCalculationStrategy, TaxCalculationContext } from "../interfaces"
@@ -64,6 +63,7 @@ type TotalsServiceProps = {
} }
type GetTotalsOptions = { type GetTotalsOptions = {
exclude_gift_cards?: boolean
force_taxes?: boolean force_taxes?: boolean
} }
@@ -111,10 +111,14 @@ class TotalsService extends BaseService {
const taxTotal = const taxTotal =
(await this.getTaxTotal(cartOrOrder, options.force_taxes)) || 0 (await this.getTaxTotal(cartOrOrder, options.force_taxes)) || 0
const discountTotal = this.getDiscountTotal(cartOrOrder) const discountTotal = this.getDiscountTotal(cartOrOrder)
const giftCardTotal = this.getGiftCardTotal(cartOrOrder) const giftCardTotal = options.exclude_gift_cards
? { total: 0 }
: this.getGiftCardTotal(cartOrOrder)
const shippingTotal = this.getShippingTotal(cartOrOrder) const shippingTotal = this.getShippingTotal(cartOrOrder)
return subtotal + taxTotal + shippingTotal - discountTotal - giftCardTotal return (
subtotal + taxTotal + shippingTotal - discountTotal - giftCardTotal.total
)
} }
/** /**
@@ -292,6 +296,7 @@ class TotalsService extends BaseService {
} }
const calculationContext = this.getCalculationContext(cartOrOrder) const calculationContext = this.getCalculationContext(cartOrOrder)
const giftCardTotal = this.getGiftCardTotal(cartOrOrder)
let taxLines: (ShippingMethodTaxLine | LineItemTaxLine)[] let taxLines: (ShippingMethodTaxLine | LineItemTaxLine)[]
if (isOrder(cartOrOrder)) { if (isOrder(cartOrOrder)) {
@@ -317,9 +322,8 @@ class TotalsService extends BaseService {
const subtotal = this.getSubtotal(cartOrOrder) const subtotal = this.getSubtotal(cartOrOrder)
const shippingTotal = this.getShippingTotal(cartOrOrder) const shippingTotal = this.getShippingTotal(cartOrOrder)
const discountTotal = this.getDiscountTotal(cartOrOrder) const discountTotal = this.getDiscountTotal(cartOrOrder)
const giftCardTotal = this.getGiftCardTotal(cartOrOrder)
return this.rounded( return this.rounded(
(subtotal - discountTotal - giftCardTotal + shippingTotal) * (subtotal - discountTotal - giftCardTotal.total + shippingTotal) *
(cartOrOrder.tax_rate / 100) (cartOrOrder.tax_rate / 100)
) )
} }
@@ -354,6 +358,10 @@ class TotalsService extends BaseService {
calculationContext calculationContext
) )
if (cartOrOrder.region.gift_cards_taxable) {
return this.rounded(toReturn - giftCardTotal.tax_total)
}
return this.rounded(toReturn) return this.rounded(toReturn)
} }
@@ -385,13 +393,13 @@ class TotalsService extends BaseService {
if (allocationMap[ld.item.id]) { if (allocationMap[ld.item.id]) {
allocationMap[ld.item.id].discount = { allocationMap[ld.item.id].discount = {
amount: ld.amount, amount: ld.amount,
unit_amount: ld.amount / ld.item.quantity, unit_amount: Math.round(ld.amount / ld.item.quantity),
} }
} else { } else {
allocationMap[ld.item.id] = { allocationMap[ld.item.id] = {
discount: { discount: {
amount: ld.amount, amount: ld.amount,
unit_amount: ld.amount / ld.item.quantity, unit_amount: Math.round(ld.amount / ld.item.quantity),
}, },
} }
} }
@@ -406,13 +414,13 @@ class TotalsService extends BaseService {
// If the fixed discount exceeds the subtotal we should // If the fixed discount exceeds the subtotal we should
// calculate a 100% discount // calculate a 100% discount
const nominator = Math.min(giftCardTotal, subtotal) const nominator = Math.min(giftCardTotal.total, subtotal)
const percentage = nominator / subtotal const percentage = nominator / subtotal
lineGiftCards = orderOrCart.items.map((l) => { lineGiftCards = orderOrCart.items.map((l) => {
return { return {
item: l, item: l,
amount: l.unit_price * l.quantity * percentage, amount: Math.round(l.unit_price * l.quantity * percentage),
} }
}) })
} }
@@ -421,13 +429,13 @@ class TotalsService extends BaseService {
if (allocationMap[lgc.item.id]) { if (allocationMap[lgc.item.id]) {
allocationMap[lgc.item.id].gift_card = { allocationMap[lgc.item.id].gift_card = {
amount: lgc.amount, amount: lgc.amount,
unit_amount: lgc.amount / lgc.item.quantity, unit_amount: Math.round(lgc.amount / lgc.item.quantity),
} }
} else { } else {
allocationMap[lgc.item.id] = { allocationMap[lgc.item.id] = {
discount: { gift_card: {
amount: lgc.amount, amount: lgc.amount,
unit_amount: lgc.amount / lgc.item.quantity, unit_amount: Math.round(lgc.amount / lgc.item.quantity),
}, },
} }
} }
@@ -821,31 +829,91 @@ class TotalsService extends BaseService {
return toReturn return toReturn
} }
/**
* Gets the amount that can be gift carded on a cart. In regions where gift
* cards are taxable this amount should exclude taxes.
* @param cartOrOrder - the cart or order to get gift card amount for
* @return the gift card amount applied to the cart or order
*/
async getGiftCardableAmount(cartOrOrder: Cart | Order): Promise<number> {
if (cartOrOrder.region?.gift_cards_taxable) {
return this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder)
}
return await this.getTotal(cartOrOrder, {
exclude_gift_cards: true,
})
}
/** /**
* Gets the gift card amount on a cart or order. * Gets the gift card amount on a cart or order.
* @param cartOrOrder - the cart or order to get gift card amount for * @param cartOrOrder - the cart or order to get gift card amount for
* @return the gift card amount applied to the cart or order * @return the gift card amount applied to the cart or order
*/ */
getGiftCardTotal(cartOrOrder: Cart | Order): number { getGiftCardTotal(cartOrOrder: Cart | Order): {
total: number
tax_total: number
} {
const giftCardable = const giftCardable =
this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder) this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder)
if ("gift_card_transactions" in cartOrOrder) { if ("gift_card_transactions" in cartOrOrder) {
// gift_card_transactions only exist on orders so we can
// safely calculate the total based on the gift card transactions
return cartOrOrder.gift_card_transactions.reduce( return cartOrOrder.gift_card_transactions.reduce(
(acc, next) => acc + next.amount, (acc, next) => {
0 let taxMultiplier = (next.tax_rate || 0) / 100
// Previously we did not record whether a gift card was taxable or not.
// All gift cards where is_taxable === null are from the old system,
// where we defaulted to taxable gift cards.
//
// This is a backwards compatability fix for orders that were created
// before we added the gift card tax rate.
if (
next.is_taxable === null &&
cartOrOrder.region?.gift_cards_taxable
) {
taxMultiplier = cartOrOrder.region.tax_rate / 100
}
return {
total: acc.total + next.amount,
tax_total: acc.tax_total + next.amount * taxMultiplier,
}
},
{
total: 0,
tax_total: 0,
}
) )
} }
if (!cartOrOrder.gift_cards || !cartOrOrder.gift_cards.length) { if (!cartOrOrder.gift_cards || !cartOrOrder.gift_cards.length) {
return 0 return {
total: 0,
tax_total: 0,
}
} }
const toReturn = cartOrOrder.gift_cards.reduce( const toReturn = cartOrOrder.gift_cards.reduce(
(acc, next) => acc + next.balance, (acc, next) => acc + next.balance,
0 0
) )
return Math.min(giftCardable, toReturn) const orderGiftCardAmount = Math.min(giftCardable, toReturn)
if (cartOrOrder.region?.gift_cards_taxable) {
return {
total: orderGiftCardAmount,
tax_total: (orderGiftCardAmount * cartOrOrder.region.tax_rate) / 100,
}
}
return {
total: orderGiftCardAmount,
tax_total: 0,
}
} }
/** /**
@@ -64,7 +64,7 @@ const toTest = [
* Taxline 2 = 180 * 0.125 = 23 * Taxline 2 = 180 * 0.125 = 23
* Total tax = 38 * Total tax = 38
*/ */
expected: 38, expected: 40,
items: [ items: [
{ {
id: "item_1", id: "item_1",
@@ -38,11 +38,6 @@ class TaxCalculationStrategy implements ITaxCalculationStrategy {
let taxableAmount = i.quantity * i.unit_price let taxableAmount = i.quantity * i.unit_price
if (context.region.gift_cards_taxable) {
taxableAmount -=
(allocations.gift_card && allocations.gift_card.amount) || 0
}
taxableAmount -= taxableAmount -=
((allocations.discount && allocations.discount.unit_amount) || 0) * ((allocations.discount && allocations.discount.unit_amount) || 0) *
i.quantity i.quantity
+1
View File
@@ -67,6 +67,7 @@ export type TotalField =
| "subtotal" | "subtotal"
| "refundable_amount" | "refundable_amount"
| "gift_card_total" | "gift_card_total"
| "gift_card_tax_total"
export interface FindConfig<Entity> { export interface FindConfig<Entity> {
select?: (keyof Entity)[] select?: (keyof Entity)[]