fix(types,medusa): calculate taxes for original price (#11692)

This commit is contained in:
Riqwan Thamir
2025-03-03 14:20:48 +01:00
committed by GitHub
parent 954136f13a
commit 51b0af193c
4 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/types": patch
"@medusajs/medusa": patch
---
fix(types,medusa): calculate taxes for original price

View File

@@ -2369,6 +2369,12 @@ medusaIntegrationTestRunner({
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"calculated_amount_without_tax"
)
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"original_amount_with_tax"
)
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"original_amount_without_tax"
)
})
it("should not return tax pricing if automatic taxes are off when listing products", async () => {
@@ -2386,6 +2392,12 @@ medusaIntegrationTestRunner({
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"calculated_amount_without_tax"
)
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"original_amount_with_tax"
)
expect(products[0].variants[0].calculated_price).not.toHaveProperty(
"original_amount_without_tax"
)
})
it("should return prices with and without tax for a tax inclusive region when listing products", async () => {
@@ -2430,6 +2442,66 @@ medusaIntegrationTestRunner({
)
})
it("should return prices with and without tax for a tax inclusive region when listing products with a price list sale", async () => {
const customerGroup = (
await api.post(
"/admin/customer-groups",
{ name: "VIP" },
adminHeaders
)
).data.customer_group
await api.post(
`/admin/customer-groups/${customerGroup.id}/customers`,
{ add: [customer.id] },
adminHeaders
)
await api.post(
`/admin/price-lists`,
{
title: "test price list",
description: "test",
status: PriceListStatus.ACTIVE,
type: PriceListType.SALE,
prices: [
{
amount: 35,
currency_code: euRegion.currency_code,
variant_id: product1.variants[0].id,
},
],
rules: { "customer.groups.id": [customerGroup.id] },
},
adminHeaders
)
const products = (
await api.get(
`/store/products?fields=id,*variants.calculated_price&region_id=${euRegion.id}&country_code=it`,
storeHeadersWithCustomer
)
).data.products
expect(products.length).toBe(2)
expect(products[0].variants).toEqual(
expect.arrayContaining([
expect.objectContaining({
calculated_price: expect.objectContaining({
currency_code: "eur",
calculated_amount: 35,
original_amount: 45,
is_calculated_price_price_list: true,
calculated_amount_with_tax: 38.5,
calculated_amount_without_tax: 35,
original_amount_with_tax: 45,
original_amount_without_tax: 40.90909090909091,
}),
}),
])
)
})
it("should return prices with and without tax for a tax exclusive region when listing products", async () => {
const products = (
await api.get(
@@ -2547,6 +2619,13 @@ medusaIntegrationTestRunner({
expect(product.variants[0].calculated_price).not.toHaveProperty(
"calculated_amount_without_tax"
)
expect(product.variants[0].calculated_price).not.toHaveProperty(
"original_amount_with_tax"
)
expect(product.variants[0].calculated_price).not.toHaveProperty(
"original_amount_without_tax"
)
})
it("should return prices with and without tax for a tax inclusive region when fetching a single product", async () => {

View File

@@ -46,6 +46,16 @@ export interface BaseCalculatedPriceSet {
*/
original_amount: number | null
/**
* The amount of the original price with taxes included. If the original price is tax inclusive, this field will be the same as `original_amount`.
*/
original_amount_with_tax: number | null
/**
* The amount of the original price without taxes included. If the original price is tax exclusive, this field will be the same as `original_amount`.
*/
original_amount_without_tax: number | null
/**
* The currency code of the calculated price, or null if there isn't a calculated price.
*/

View File

@@ -77,6 +77,19 @@ export const wrapProductsWithTaxPrices = async <T>(
variant.calculated_price.calculated_amount_with_tax = priceWithTax
variant.calculated_price.calculated_amount_without_tax = priceWithoutTax
const {
priceWithTax: originalPriceWithTax,
priceWithoutTax: originalPriceWithoutTax,
} = calculateAmountsWithTax({
taxLines: taxRatesForVariant,
amount: variant.calculated_price!.original_amount!,
includesTax: variant.calculated_price!.is_original_price_tax_inclusive!,
})
variant.calculated_price.original_amount_with_tax = originalPriceWithTax
variant.calculated_price.original_amount_without_tax =
originalPriceWithoutTax
})
})
}