diff --git a/.changeset/chilled-kings-flow.md b/.changeset/chilled-kings-flow.md new file mode 100644 index 0000000000..3b033d0d3e --- /dev/null +++ b/.changeset/chilled-kings-flow.md @@ -0,0 +1,6 @@ +--- +"@medusajs/types": patch +"@medusajs/medusa": patch +--- + +fix(types,medusa): calculate taxes for original price diff --git a/integration-tests/http/__tests__/product/store/product.spec.ts b/integration-tests/http/__tests__/product/store/product.spec.ts index 8283af92e0..36bea60e32 100644 --- a/integration-tests/http/__tests__/product/store/product.spec.ts +++ b/integration-tests/http/__tests__/product/store/product.spec.ts @@ -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®ion_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 () => { diff --git a/packages/core/types/src/http/pricing/common.ts b/packages/core/types/src/http/pricing/common.ts index a5aaf771b5..b9ce6a91ef 100644 --- a/packages/core/types/src/http/pricing/common.ts +++ b/packages/core/types/src/http/pricing/common.ts @@ -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. */ diff --git a/packages/medusa/src/api/store/products/helpers.ts b/packages/medusa/src/api/store/products/helpers.ts index bc169c897b..8119991d5a 100644 --- a/packages/medusa/src/api/store/products/helpers.ts +++ b/packages/medusa/src/api/store/products/helpers.ts @@ -77,6 +77,19 @@ export const wrapProductsWithTaxPrices = async ( 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 }) }) }