diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index 8a6ef50401..c3f40bf8cc 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -301,6 +301,7 @@ medusaIntegrationTestRunner({ // updated the requested quantitty const updateReturnItemActionId = result.data.order_preview.items[0].actions[0].id + result = await api.post( `/admin/returns/${returnId}/request-items/${updateReturnItemActionId}`, { diff --git a/integration-tests/modules/__tests__/cart/store/carts.spec.ts b/integration-tests/modules/__tests__/cart/store/carts.spec.ts index b35b9c0407..219bb27741 100644 --- a/integration-tests/modules/__tests__/cart/store/carts.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/carts.spec.ts @@ -2030,7 +2030,7 @@ medusaIntegrationTestRunner({ shipping_subtotal: 0, shipping_tax_total: 0, original_shipping_tax_total: 0, - original_shipping_tax_subtotal: 0, + original_shipping_subtotal: 0, original_shipping_total: 0, items: [ expect.objectContaining({ diff --git a/integration-tests/modules/__tests__/order/order.spec.ts b/integration-tests/modules/__tests__/order/order.spec.ts index a1ac1fb28b..bd4f4a90c5 100644 --- a/integration-tests/modules/__tests__/order/order.spec.ts +++ b/integration-tests/modules/__tests__/order/order.spec.ts @@ -143,12 +143,12 @@ medusaIntegrationTestRunner({ summary: expect.objectContaining({ // TODO: add all summary fields }), - total: 59.8, - subtotal: 50, - tax_total: 0.9, - discount_total: 1, - discount_tax_total: 0.1, - original_total: 61, + total: 59.79, + subtotal: 60, + tax_total: 0.89, + discount_total: 1.1, + discount_tax_total: 0.11, + original_total: 70.9, original_tax_total: 1, item_total: 50, item_subtotal: 50, @@ -156,24 +156,24 @@ medusaIntegrationTestRunner({ original_item_total: 50, original_item_subtotal: 50, original_item_tax_total: 0, - shipping_total: 9.9, + shipping_total: 9.79, shipping_subtotal: 10, - shipping_tax_total: 0.9, + shipping_tax_total: 0.89, original_shipping_tax_total: 1, - original_shipping_tax_subtotal: 10, + original_shipping_subtotal: 10, original_shipping_total: 11, created_at: expect.any(String), updated_at: expect.any(String), raw_total: { - value: "59.799999999999999995", + value: "59.789999999999999995", precision: 20, }, raw_subtotal: { - value: "50", + value: "60", precision: 20, }, raw_discount_total: { - value: "1.000000000000000005", + value: "1.100000000000000005", precision: 20, }, items: [ @@ -430,10 +430,10 @@ medusaIntegrationTestRunner({ deleted_at: null, shipping_method_id: expect.any(String), rate: 10, - total: 0.9, + total: 0.89, subtotal: 1, raw_total: { - value: "0.9", + value: "0.89", precision: 20, }, raw_subtotal: { @@ -472,18 +472,18 @@ medusaIntegrationTestRunner({ ]), amount: 10, subtotal: 10, - total: 9.9, + total: 9.79, original_total: 11, - discount_total: 1, - discount_tax_total: 0.1, - tax_total: 0.9, + discount_total: 1.1, + discount_tax_total: 0.11, + tax_total: 0.89, original_tax_total: 1, raw_subtotal: { value: "10", precision: 20, }, raw_total: { - value: "9.9", + value: "9.79", precision: 20, }, raw_original_total: { @@ -491,15 +491,15 @@ medusaIntegrationTestRunner({ precision: 20, }, raw_discount_total: { - value: "1", + value: "1.1", precision: 20, }, raw_discount_tax_total: { - value: "0.1", + value: "0.11", precision: 20, }, raw_tax_total: { - value: "0.9", + value: "0.89", precision: 20, }, raw_original_tax_total: { diff --git a/packages/core/core-flows/src/definition/cart/utils/fields.ts b/packages/core/core-flows/src/definition/cart/utils/fields.ts index a57c0dee90..811264641e 100644 --- a/packages/core/core-flows/src/definition/cart/utils/fields.ts +++ b/packages/core/core-flows/src/definition/cart/utils/fields.ts @@ -44,7 +44,7 @@ export const completeCartFields = [ "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", "raw_total", "raw_subtotal", @@ -64,7 +64,7 @@ export const completeCartFields = [ "raw_shipping_subtotal", "raw_shipping_tax_total", "raw_original_shipping_tax_total", - "raw_original_shipping_tax_subtotal", + "raw_original_shipping_subtotal", "raw_original_shipping_total", "items.*", "items.tax_lines.*", diff --git a/packages/core/utils/src/order/order-change-action.ts b/packages/core/utils/src/order/order-change-action.ts index 47fe9dbcfb..dec489e7e2 100644 --- a/packages/core/utils/src/order/order-change-action.ts +++ b/packages/core/utils/src/order/order-change-action.ts @@ -1,5 +1,4 @@ export enum ChangeActionType { - CANCEL = "CANCEL", FULFILL_ITEM = "FULFILL_ITEM", CANCEL_ITEM_FULFILLMENT = "CANCEL_ITEM_FULFILLMENT", ITEM_ADD = "ITEM_ADD", diff --git a/packages/core/utils/src/totals/__tests__/totals.ts b/packages/core/utils/src/totals/__tests__/totals.ts index 19295cdad6..dae434d368 100644 --- a/packages/core/utils/src/totals/__tests__/totals.ts +++ b/packages/core/utils/src/totals/__tests__/totals.ts @@ -111,7 +111,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 9, + total: 8.9, subtotal: 10, }, ], @@ -123,24 +123,24 @@ describe("Total calculation", function () { }, ], subtotal: 100, - total: 99, + total: 97.9, original_total: 110, - discount_total: 10, - discount_tax_total: 1, - tax_total: 9, + discount_total: 11, + discount_tax_total: 1.1, + tax_total: 8.9, original_tax_total: 10, }, ], - total: 99, + total: 97.9, subtotal: 100, - tax_total: 9, - discount_total: 10, - discount_tax_total: 1, - original_total: 100, + tax_total: 8.9, + discount_total: 11, + discount_tax_total: 1.1, + original_total: 99, original_tax_total: 10, - item_total: 99, + item_total: 97.9, item_subtotal: 100, - item_tax_total: 9, + item_tax_total: 8.9, original_item_total: 110, original_item_subtotal: 100, original_item_tax_total: 10, @@ -252,7 +252,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 0.7, + total: 0.67, subtotal: 1, }, ], @@ -264,11 +264,11 @@ describe("Total calculation", function () { }, ], subtotal: 10, - total: 7.7, + total: 7.37, original_total: 11, - discount_total: 3, - discount_tax_total: 0.3, - tax_total: 0.7, + discount_total: 3.3, + discount_tax_total: 0.33, + tax_total: 0.67, original_tax_total: 1, }, ], @@ -290,7 +290,7 @@ describe("Total calculation", function () { total: 2, }, ], - subtotal: 10.380952380952381, + subtotal: 9.523809523809524, total: 8, original_total: 10, discount_total: 2, @@ -304,7 +304,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 0.3, + total: 0.28, subtotal: 0.5, }, ], @@ -316,32 +316,32 @@ describe("Total calculation", function () { }, ], subtotal: 5, - total: 3.3, + total: 3.08, original_total: 5.5, - discount_total: 2, - discount_tax_total: 0.2, - tax_total: 0.3, + discount_total: 2.2, + discount_tax_total: 0.22, + tax_total: 0.28, original_tax_total: 0.5, }, ], - total: 104.77186147186147, - subtotal: 100.9090909090909, - tax_total: 9.562770562770563, - discount_total: 17, - discount_tax_total: 1.6, - original_total: 110.47619047619048, + total: 107.445670995671, + subtotal: 115.43290043290044, + tax_total: 9.512770562770562, + discount_total: 17.5, + discount_tax_total: 1.65, + original_total: 124.5, original_tax_total: 11.067099567099566, - item_total: 97.7, + item_total: 97.37, item_subtotal: 100.9090909090909, - item_tax_total: 8.881818181818181, + item_tax_total: 8.851818181818182, original_item_total: 111, original_item_subtotal: 100.9090909090909, original_item_tax_total: 10.090909090909092, - shipping_total: 11.3, - shipping_subtotal: 15.380952380952381, - shipping_tax_total: 0.680952380952381, + shipping_total: 11.08, + shipping_subtotal: 14.523809523809524, + shipping_tax_total: 0.660952380952381, original_shipping_tax_total: 0.9761904761904762, - original_shipping_tax_subtotal: 15.380952380952381, + original_shipping_subtotal: 14.523809523809524, original_shipping_total: 15.5, }) }) @@ -567,7 +567,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 8, + total: 7.8, subtotal: 10, }, ], @@ -579,11 +579,11 @@ describe("Total calculation", function () { }, ], subtotal: 100, - total: 88, + total: 85.8, original_total: 110, - discount_total: 20, - discount_tax_total: 2, - tax_total: 8, + discount_total: 22, + discount_tax_total: 2.2, + tax_total: 7.8, original_tax_total: 10, }, ], @@ -593,7 +593,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 2.3, + total: 2.28, subtotal: 2.5, }, ], @@ -605,32 +605,32 @@ describe("Total calculation", function () { }, ], subtotal: 25, - total: 25.3, + total: 25.08, original_total: 27.5, - discount_total: 2, - discount_tax_total: 0.2, - tax_total: 2.3, + discount_total: 2.2, + discount_tax_total: 0.22, + tax_total: 2.28, original_tax_total: 2.5, }, ], - total: 113.6, - subtotal: 100, - tax_total: 10.3, - discount_total: 22, - discount_tax_total: 2.2, - original_total: 118, + total: 110.88, + subtotal: 125, + tax_total: 10.08, + discount_total: 24.2, + discount_tax_total: 2.42, + original_total: 140.8, original_tax_total: 12.5, - item_total: 88, + item_total: 85.8, item_subtotal: 100, - item_tax_total: 8, + item_tax_total: 7.8, original_item_total: 110, original_item_subtotal: 100, original_item_tax_total: 10, - shipping_total: 25.3, + shipping_total: 25.08, shipping_subtotal: 25, - shipping_tax_total: 2.3, + shipping_tax_total: 2.28, original_shipping_tax_total: 2.5, - original_shipping_tax_subtotal: 25, + original_shipping_subtotal: 25, original_shipping_total: 27.5, }) }) @@ -673,7 +673,7 @@ describe("Total calculation", function () { tax_lines: [ { rate: 10, - total: 8, + total: 7.8, subtotal: 10, }, ], @@ -693,41 +693,41 @@ describe("Total calculation", function () { written_off_quantity: 1, }, subtotal: 100, - total: 88, + total: 85.8, original_total: 110, - discount_total: 20, - discount_tax_total: 2, - tax_total: 8, + discount_total: 22, + discount_tax_total: 2.2, + tax_total: 7.8, original_tax_total: 10, - fulfilled_total: 88, - shipped_total: 88, + fulfilled_total: 85.8, + shipped_total: 85.8, return_requested_total: 0, - return_received_total: 44, - return_dismissed_total: 44, - write_off_total: 44, + return_received_total: 42.9, + return_dismissed_total: 42.9, + write_off_total: 42.9, refundable_total: 0, refundable_total_per_unit: 0, }, ], - total: 88, + total: 85.8, subtotal: 100, - tax_total: 8, - discount_total: 20, - discount_tax_total: 2, - original_total: 90, + tax_total: 7.8, + discount_total: 22, + discount_tax_total: 2.2, + original_total: 88, original_tax_total: 10, - item_total: 88, + item_total: 85.8, item_subtotal: 100, - item_tax_total: 8, + item_tax_total: 7.8, original_item_total: 110, original_item_subtotal: 100, original_item_tax_total: 10, - fulfilled_total: 88, - shipped_total: 88, + fulfilled_total: 85.8, + shipped_total: 85.8, return_requested_total: 0, - return_received_total: 44, - return_dismissed_total: 44, - write_off_total: 44, + return_received_total: 42.9, + return_dismissed_total: 42.9, + write_off_total: 42.9, }) }) }) diff --git a/packages/core/utils/src/totals/adjustment/index.ts b/packages/core/utils/src/totals/adjustment/index.ts index 940b3314ea..70feb29090 100644 --- a/packages/core/utils/src/totals/adjustment/index.ts +++ b/packages/core/utils/src/totals/adjustment/index.ts @@ -30,8 +30,10 @@ export function calculateAdjustmentTotal({ adj["subtotal"] = new BigNumber(MathBN.sub(adj.amount, taxAmount)) adj["total"] = new BigNumber(adj.amount) } else { + total = MathBN.add(adj.amount, taxAmount) + adj["subtotal"] = new BigNumber(adj.amount) - adj["total"] = new BigNumber(MathBN.add(adj.amount, taxAmount)) + adj["total"] = new BigNumber(total) } } } diff --git a/packages/core/utils/src/totals/cart/index.ts b/packages/core/utils/src/totals/cart/index.ts index b26c73c63b..e54d455d27 100644 --- a/packages/core/utils/src/totals/cart/index.ts +++ b/packages/core/utils/src/totals/cart/index.ts @@ -91,10 +91,8 @@ export function decorateCartTotals( let shippingOriginalSubtotal = MathBN.convert(0) let shippingTaxTotal = MathBN.convert(0) - let shippingTaxSubTotal = MathBN.convert(0) let shippingOriginalTaxTotal = MathBN.convert(0) - let shippingOriginalTaxSubtotal = MathBN.convert(0) const cartItems = items.map((item, index) => { const itemTotals = Object.assign(item, itemsTotals[item.id ?? index] ?? {}) @@ -147,6 +145,8 @@ export function decorateCartTotals( const methodSubtotal = MathBN.convert(methodTotals.subtotal) + subtotal = MathBN.add(subtotal, methodSubtotal) + const methodTotal = MathBN.convert(methodTotals.total) const methodOriginalTotal = MathBN.convert(methodTotals.original_total) const methodTaxTotal = MathBN.convert(methodTotals.tax_total) @@ -175,11 +175,6 @@ export function decorateCartTotals( shippingOriginalTaxTotal, methodOriginalTaxTotal ) - shippingOriginalTaxSubtotal = MathBN.add( - shippingOriginalTaxSubtotal, - methodSubtotal - ) - discountTotal = MathBN.add(discountTotal, methodDiscountTotal) discountTaxTotal = MathBN.add(discountTaxTotal, methodDiscountTaxTotal) @@ -202,7 +197,7 @@ export function decorateCartTotals( ) const originalTotal = MathBN.sub(originalTempTotal, discountTotal) // TODO: subtract (cart.gift_card_total + cart.gift_card_tax_total) - const tempTotal = MathBN.add(subtotal, shippingTotal, taxTotal) + const tempTotal = MathBN.add(subtotal, taxTotal) const total = MathBN.sub(tempTotal, discountTotal) const cart = cartLike as any @@ -245,9 +240,7 @@ export function decorateCartTotals( cart.shipping_tax_total = new BigNumber(shippingTaxTotal) cart.original_shipping_tax_total = new BigNumber(shippingOriginalTaxTotal) - cart.original_shipping_tax_subtotal = new BigNumber( - shippingOriginalTaxSubtotal - ) + cart.original_shipping_subtotal = new BigNumber(shippingOriginalSubtotal) cart.original_shipping_total = new BigNumber(shippingOriginalTotal) } diff --git a/packages/core/utils/src/totals/line-item/index.ts b/packages/core/utils/src/totals/line-item/index.ts index b533de98c2..1523508b97 100644 --- a/packages/core/utils/src/totals/line-item/index.ts +++ b/packages/core/utils/src/totals/line-item/index.ts @@ -132,8 +132,8 @@ function getLineItemTotals( unit_price: item.unit_price, subtotal: new BigNumber(subtotal), - total: new BigNumber(total), + original_total: new BigNumber(subtotal), discount_total: new BigNumber(discountTotal), diff --git a/packages/core/utils/src/totals/shipping-method/index.ts b/packages/core/utils/src/totals/shipping-method/index.ts index ca485111cf..a91a587301 100644 --- a/packages/core/utils/src/totals/shipping-method/index.ts +++ b/packages/core/utils/src/totals/shipping-method/index.ts @@ -115,7 +115,7 @@ export function getShippingMethodTotals( const isTaxInclusive = context.includeTax ?? shippingMethod.is_tax_inclusive if (isTaxInclusive) { - const subtotal = MathBN.add(shippingMethod.amount, taxTotal) + const subtotal = MathBN.sub(shippingMethod.amount, originalTaxTotal) totals.subtotal = new BigNumber(subtotal) } else { const originalTotal = MathBN.add( diff --git a/packages/medusa/src/api/admin/orders/query-config.ts b/packages/medusa/src/api/admin/orders/query-config.ts index df08f01b4f..72c5414ce5 100644 --- a/packages/medusa/src/api/admin/orders/query-config.ts +++ b/packages/medusa/src/api/admin/orders/query-config.ts @@ -32,7 +32,7 @@ export const defaultAdminRetrieveOrderFields = [ "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", "created_at", "updated_at", diff --git a/packages/medusa/src/api/store/carts/query-config.ts b/packages/medusa/src/api/store/carts/query-config.ts index 6b02bc8b97..7ffafb9dfa 100644 --- a/packages/medusa/src/api/store/carts/query-config.ts +++ b/packages/medusa/src/api/store/carts/query-config.ts @@ -21,7 +21,7 @@ export const defaultStoreCartFields = [ "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", "promotions.id", "promotions.code", diff --git a/packages/medusa/src/api/store/orders/query-config.ts b/packages/medusa/src/api/store/orders/query-config.ts index 25998b9b75..108a6bc7e2 100644 --- a/packages/medusa/src/api/store/orders/query-config.ts +++ b/packages/medusa/src/api/store/orders/query-config.ts @@ -39,7 +39,7 @@ export const defaultStoreRetrieveOrderFields = [ "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", "created_at", "updated_at", diff --git a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index 5e9b7a9d6d..f7168129dc 100644 --- a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -1,8 +1,8 @@ import { ICartModuleService } from "@medusajs/types" import { BigNumber, Module, Modules } from "@medusajs/utils" import { CheckConstraintViolationException } from "@mikro-orm/core" -import { moduleIntegrationTestRunner } from "medusa-test-utils" import { CartModuleService } from "@services" +import { moduleIntegrationTestRunner } from "medusa-test-utils" jest.setTimeout(50000) @@ -2756,11 +2756,11 @@ moduleIntegrationTestRunner({ }, ], total: 210, - subtotal: 500, + subtotal: 510, tax_total: 0, discount_total: 300, discount_tax_total: 0, - original_total: 210, + original_total: 220, original_tax_total: 0, item_total: 200, item_subtotal: 500, @@ -2772,14 +2772,14 @@ moduleIntegrationTestRunner({ shipping_subtotal: 10, shipping_tax_total: 0, original_shipping_tax_total: 0, - original_shipping_tax_subtotal: 10, + original_shipping_subtotal: 10, original_shipping_total: 10, raw_total: { value: "210", precision: 20, }, raw_subtotal: { - value: "500", + value: "510", precision: 20, }, raw_tax_total: { @@ -2795,7 +2795,7 @@ moduleIntegrationTestRunner({ precision: 20, }, raw_original_total: { - value: "210", + value: "220", precision: 20, }, raw_original_tax_total: { @@ -2842,7 +2842,7 @@ moduleIntegrationTestRunner({ value: "0", precision: 20, }, - raw_original_shipping_tax_subtotal: { + raw_original_shipping_subtotal: { value: "10", precision: 20, }, diff --git a/packages/modules/cart/src/services/cart-module.ts b/packages/modules/cart/src/services/cart-module.ts index 867157424a..d6fb071665 100644 --- a/packages/modules/cart/src/services/cart-module.ts +++ b/packages/modules/cart/src/services/cart-module.ts @@ -9,16 +9,16 @@ import { ModulesSdkTypes, } from "@medusajs/types" import { - createRawPropertiesFromBigNumber, - decorateCartTotals, - deduplicate, InjectManager, InjectTransactionManager, - isObject, - isString, MedusaContext, MedusaError, ModulesSdkUtils, + createRawPropertiesFromBigNumber, + decorateCartTotals, + deduplicate, + isObject, + isString, } from "@medusajs/utils" import { Address, @@ -132,7 +132,7 @@ export default class CartModuleService "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", ] diff --git a/packages/modules/order/src/services/order-module-service.ts b/packages/modules/order/src/services/order-module-service.ts index 51592b3df3..2b1c0d74f3 100644 --- a/packages/modules/order/src/services/order-module-service.ts +++ b/packages/modules/order/src/services/order-module-service.ts @@ -256,7 +256,7 @@ export default class OrderModuleService< "shipping_subtotal", "shipping_tax_total", "original_shipping_tax_total", - "original_shipping_tax_subtotal", + "original_shipping_subtotal", "original_shipping_total", ] diff --git a/packages/modules/order/src/utils/calculate-order-change.ts b/packages/modules/order/src/utils/calculate-order-change.ts index 529461063f..4552c3579e 100644 --- a/packages/modules/order/src/utils/calculate-order-change.ts +++ b/packages/modules/order/src/utils/calculate-order-change.ts @@ -2,7 +2,6 @@ import { BigNumberInput, OrderSummaryDTO } from "@medusajs/types" import { BigNumber, MathBN, - isDefined, isPresent, transformPropertiesToBigNumber, } from "@medusajs/utils" @@ -195,7 +194,7 @@ export class OrderChangeProcessing { calculatedAmount = type.operation(params) as BigNumberInput // the action.amount has priority over the calculated amount - if (!isDefined(action.amount)) { + if (action.amount == undefined) { action.amount = calculatedAmount ?? 0 } }