Skip to content

Commit 9563ee4

Browse files
fix(utils,core-flows): subtotal calculation and returns location (#13497)
* fix(utils,core-flows): subtotal calculation and returns location * changeset * fix test * var * rm extra field from test * fix original total * fix partial refunds and pending difference * fix test * fix test * test * extract to util * original total and update payment when receive return * original_subtotal * default fields * test * calculate pending difference * revert claims test * pending difference * creadit line fix * if
1 parent 4736c58 commit 9563ee4

File tree

37 files changed

+745
-203
lines changed

37 files changed

+745
-203
lines changed

.changeset/rare-colts-warn.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@medusajs/utils": patch
3+
"@medusajs/types": patch
4+
"@medusajs/core-flows": patch
5+
"@medusajs/order": patch
6+
"@medusajs/medusa": patch
7+
---
8+
9+
fix(utils): subtotal calculation discounting returned items

integration-tests/http/__tests__/exchanges/exchanges.spec.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ medusaIntegrationTestRunner({
2222
let returnReason
2323
let inventoryItem
2424
let inventoryItemExtra
25+
let inventoryItemExtra2
2526
let location
2627
let productExtra
28+
let productExtra2
2729
const shippingProviderId = "manual_test-provider"
2830

2931
beforeEach(async () => {
@@ -123,6 +125,31 @@ medusaIntegrationTestRunner({
123125
)
124126
).data.product
125127

128+
productExtra2 = (
129+
await api.post(
130+
"/admin/products",
131+
{
132+
title: "Extra product 2, same price",
133+
shipping_profile_id: shippingProfile.id,
134+
options: [{ title: "size", values: ["large", "small"] }],
135+
variants: [
136+
{
137+
title: "my variant 2",
138+
sku: "variant-sku-2",
139+
options: { size: "large" },
140+
prices: [
141+
{
142+
currency_code: "usd",
143+
amount: 25,
144+
},
145+
],
146+
},
147+
],
148+
},
149+
adminHeaders
150+
)
151+
).data.product
152+
126153
returnReason = (
127154
await api.post(
128155
"/admin/return-reasons",
@@ -269,6 +296,10 @@ medusaIntegrationTestRunner({
269296
await api.get(`/admin/inventory-items?sku=variant-sku`, adminHeaders)
270297
).data.inventory_items[0]
271298

299+
inventoryItemExtra2 = (
300+
await api.get(`/admin/inventory-items?sku=variant-sku-2`, adminHeaders)
301+
).data.inventory_items[0]
302+
272303
await api.post(
273304
`/admin/inventory-items/${inventoryItemExtra.id}/location-levels`,
274305
{
@@ -278,6 +309,15 @@ medusaIntegrationTestRunner({
278309
adminHeaders
279310
)
280311

312+
await api.post(
313+
`/admin/inventory-items/${inventoryItemExtra2.id}/location-levels`,
314+
{
315+
location_id: location.id,
316+
stocked_quantity: 2,
317+
},
318+
adminHeaders
319+
)
320+
281321
const remoteLink = container.resolve(
282322
ContainerRegistrationKeys.REMOTE_LINK
283323
)
@@ -323,6 +363,14 @@ medusaIntegrationTestRunner({
323363
inventory_item_id: inventoryItemExtra.id,
324364
},
325365
},
366+
{
367+
[Modules.PRODUCT]: {
368+
variant_id: productExtra2.variants[0].id,
369+
},
370+
[Modules.INVENTORY]: {
371+
inventory_item_id: inventoryItemExtra2.id,
372+
},
373+
},
326374
])
327375

328376
// create reservation for inventory item that is initially on the order
@@ -440,6 +488,95 @@ medusaIntegrationTestRunner({
440488
})
441489

442490
describe("Exchanges lifecycle", () => {
491+
it("test full exchange flow", async () => {
492+
const orderBefore = (
493+
await api.get(`/admin/orders/${order.id}`, adminHeaders)
494+
).data.order
495+
496+
let result = await api.post(
497+
"/admin/exchanges",
498+
{
499+
order_id: order.id,
500+
description: "Test",
501+
},
502+
adminHeaders
503+
)
504+
505+
expect(result.data.exchange.created_by).toEqual(expect.any(String))
506+
507+
const exchangeId = result.data.exchange.id
508+
509+
const item = order.items[0]
510+
511+
result = await api.post(
512+
`/admin/exchanges/${exchangeId}/inbound/items`,
513+
{
514+
items: [
515+
{
516+
id: item.id,
517+
reason_id: returnReason.id,
518+
quantity: 2,
519+
},
520+
],
521+
},
522+
adminHeaders
523+
)
524+
525+
// New Item
526+
result = await api.post(
527+
`/admin/exchanges/${exchangeId}/outbound/items`,
528+
{
529+
items: [
530+
{
531+
variant_id: productExtra2.variants[0].id,
532+
quantity: 2,
533+
},
534+
],
535+
},
536+
adminHeaders
537+
)
538+
539+
result = await api.post(
540+
`/admin/exchanges/${exchangeId}/request`,
541+
{},
542+
adminHeaders
543+
)
544+
const returnId = result.data.exchange.return_id
545+
546+
result = (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data
547+
.order
548+
549+
expect(orderBefore.total).toBe(61)
550+
expect(result.total).toBe(112)
551+
552+
// receive return
553+
await api.post(`/admin/returns/${returnId}/receive`, {}, adminHeaders)
554+
await api.post(
555+
`/admin/returns/${returnId}/receive-items`,
556+
{
557+
items: [
558+
{
559+
id: item.id,
560+
quantity: 2,
561+
},
562+
],
563+
},
564+
adminHeaders
565+
)
566+
567+
await api.post(
568+
`/admin/returns/${returnId}/receive/confirm`,
569+
{},
570+
adminHeaders
571+
)
572+
573+
result = (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data
574+
.order
575+
576+
expect(orderBefore.total).toBe(61)
577+
expect(result.total).toBe(62) // +1 is from taxes of the new item
578+
})
579+
443580
it("Full flow with 2 orders", async () => {
444581
let result = await api.post(
445582
"/admin/exchanges",

integration-tests/http/__tests__/fixtures/order.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AdminStockLocation,
77
MedusaContainer,
88
} from "@medusajs/types"
9+
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
910
import {
1011
adminHeaders,
1112
generatePublishableKey,
@@ -175,6 +176,26 @@ export async function createOrderSeeder({
175176
adminHeaders
176177
)
177178

179+
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK)
180+
await remoteLink.create([
181+
{
182+
[Modules.SALES_CHANNEL]: {
183+
sales_channel_id: salesChannel.id,
184+
},
185+
[Modules.STOCK_LOCATION]: {
186+
stock_location_id: stockLocation.id,
187+
},
188+
},
189+
{
190+
[Modules.PRODUCT]: {
191+
variant_id: product.variants[0].id,
192+
},
193+
[Modules.INVENTORY]: {
194+
inventory_item_id: inventoryItem.id,
195+
},
196+
},
197+
])
198+
178199
/**
179200
* Create shipping options for each shipping profile provided
180201
*/

integration-tests/http/__tests__/payment/admin/payment.spec.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,35 @@ medusaIntegrationTestRunner({
3232
adminHeaders
3333
)
3434

35-
await api.post(`/admin/claims/${claim.id}/request`, {}, adminHeaders)
35+
const createdClaim = await api.post(
36+
`/admin/claims/${claim.id}/request`,
37+
{},
38+
adminHeaders
39+
)
40+
41+
const returnOrder = createdClaim.data.return
42+
const returnId = returnOrder.id
43+
await api.post(`/admin/returns/${returnId}/receive`, {}, adminHeaders)
44+
45+
let lineItem = returnOrder.items[0].item
46+
await api.post(
47+
`/admin/returns/${returnId}/receive-items`,
48+
{
49+
items: [
50+
{
51+
id: lineItem.id,
52+
quantity: returnOrder.items[0].quantity,
53+
},
54+
],
55+
},
56+
adminHeaders
57+
)
58+
59+
await api.post(
60+
`/admin/returns/${returnId}/receive/confirm`,
61+
{},
62+
adminHeaders
63+
)
3664
}
3765

3866
beforeEach(async () => {
@@ -64,10 +92,6 @@ medusaIntegrationTestRunner({
6492
})
6593

6694
describe("with outstanding amount due to claim", () => {
67-
beforeEach(async () => {
68-
await createClaim({ order })
69-
})
70-
7195
it("should capture an authorized payment", async () => {
7296
const payment = order.payment_collections[0].payments[0]
7397

@@ -189,6 +213,8 @@ medusaIntegrationTestRunner({
189213
adminHeaders
190214
)
191215

216+
await createClaim({ order })
217+
192218
const refundReason = (
193219
await api.post(
194220
`/admin/refund-reasons`,
@@ -253,6 +279,8 @@ medusaIntegrationTestRunner({
253279
)
254280
).data.refund_reason
255281

282+
await createClaim({ order })
283+
256284
await api.post(
257285
`/admin/payments/${payment.id}/refund`,
258286
{
@@ -311,6 +339,8 @@ medusaIntegrationTestRunner({
311339
adminHeaders
312340
)
313341

342+
await createClaim({ order })
343+
314344
await api.post(
315345
`/admin/payments/${payment.id}/refund`,
316346
{ amount: 25 },

integration-tests/http/__tests__/returns/returns.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
12
import {
23
ContainerRegistrationKeys,
34
Modules,
45
RuleOperator,
56
} from "@medusajs/utils"
6-
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
77
import {
88
adminHeaders,
99
createAdminUser,
@@ -478,6 +478,16 @@ medusaIntegrationTestRunner({
478478
adminHeaders
479479
)
480480

481+
expect(result.data.order_preview.summary).toEqual(
482+
expect.objectContaining({
483+
transaction_total: 0,
484+
current_order_total: 61,
485+
pending_difference: 11,
486+
paid_total: 0,
487+
refunded_total: 0,
488+
})
489+
)
490+
481491
expect(result.data.order_preview).toEqual(
482492
expect.objectContaining({
483493
id: order.id,

integration-tests/modules/__tests__/order/order.spec.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { createOrderChangeWorkflow, createOrderWorkflow, } from "@medusajs/core-flows"
1+
import {
2+
createOrderChangeWorkflow,
3+
createOrderWorkflow,
4+
} from "@medusajs/core-flows"
25
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
3-
import { CreateOrderLineItemDTO, IOrderModuleService, OrderDTO, } from "@medusajs/types"
6+
import {
7+
CreateOrderLineItemDTO,
8+
IOrderModuleService,
9+
OrderDTO,
10+
} from "@medusajs/types"
411
import { Modules, ProductStatus } from "@medusajs/utils"
5-
import { adminHeaders, createAdminUser, } from "../../../helpers/create-admin-user"
12+
import {
13+
adminHeaders,
14+
createAdminUser,
15+
} from "../../../helpers/create-admin-user"
616

717
jest.setTimeout(50000)
818

@@ -288,6 +298,7 @@ medusaIntegrationTestRunner({
288298
discount_total: 1.1,
289299
discount_tax_total: 0.1,
290300
original_total: 61,
301+
original_subtotal: 60,
291302
original_tax_total: 1,
292303
item_total: 50,
293304
item_subtotal: 50,
@@ -433,6 +444,7 @@ medusaIntegrationTestRunner({
433444
subtotal: 50,
434445
total: 50,
435446
original_total: 50,
447+
original_subtotal: 50,
436448
discount_total: 0,
437449
discount_tax_total: 0,
438450
discount_subtotal: 0,
@@ -490,6 +502,10 @@ medusaIntegrationTestRunner({
490502
precision: 20,
491503
value: "0",
492504
},
505+
raw_original_subtotal: {
506+
precision: 20,
507+
value: "50",
508+
},
493509
raw_return_dismissed_total: {
494510
precision: 20,
495511
value: "0",

0 commit comments

Comments
 (0)