From 35051489943e8f7add57075f3f3450bdf9598b44 Mon Sep 17 00:00:00 2001 From: chpark Date: Thu, 14 May 2026 01:33:53 +0900 Subject: [PATCH] =?UTF-8?q?feat(orders):=20=EC=B6=9C=EA=B3=A0=EC=99=84?= =?UTF-8?q?=EB=A3=8C=20=EC=83=81=ED=83=9C=20=EC=88=98=EB=9F=89=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EC=8B=9C=20=EC=9E=AC=EA=B3=A0=20=C2=B1=20=EB=8F=99?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20+=20=EC=9D=B4=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 직전엔 items/update 가 momo_order_items.qty 만 UPDATE → 출고완료 발주의 수량을 줄여도 재고는 그대로 (출고 시 차감된 양 그대로 묶임). - 수정: status='APPROVED' 인 경우 newQty - oldQty 차이만큼 재고 보정 · diff > 0 (추가 출고) → stock 차감 + stock_moves OUT · diff < 0 (수량 줄임) → stock 복원 + stock_moves IN - 사용된 창고는 기존 stock_moves(ref_type='ORDER', ref_objid=order, 동일 item) 의 wh_objid 로 lookup (approve 시 사용했던 창고와 동일 유지). - 이력 memo: "수량 수정: oldQty → newQty" --- src/app/api/m/orders/items/update/route.ts | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/app/api/m/orders/items/update/route.ts b/src/app/api/m/orders/items/update/route.ts index 5ec64df..9a8983c 100644 --- a/src/app/api/m/orders/items/update/route.ts +++ b/src/app/api/m/orders/items/update/route.ts @@ -113,6 +113,43 @@ export async function POST(req: NextRequest) { const isFree = cur.is_tax_free === "Y"; const calc = calcLine({ unitPrice: Number(cur.unit_price), qty: newQty, isTaxFree: isFree }); + + // 출고완료(APPROVED) 상태에서 수량 변경 시 재고 ± 동기화 + stock_moves 이력 + // ITEM 종류만 (택배/용차는 재고 미차감이라 skip — 위 cur.kind 검사로 ITEM 확정됨) + if (order.status === "APPROVED") { + const oldQty = Number(cur.qty); + const diff = newQty - oldQty; // +면 추가 차감, -면 복원 + if (diff !== 0) { + // 출고 시 사용된 창고 — momo_stock_moves 의 OUT ref_objid 매칭으로 lookup + const whRes = await client.query( + `SELECT wh_objid FROM momo_stock_moves + WHERE ref_type = 'ORDER' AND ref_objid = $1 AND item_objid = $2 + ORDER BY regdate ASC LIMIT 1`, + [orderObjid, cur.item_objid] + ); + if (whRes.rowCount && whRes.rows[0].wh_objid) { + const whObjid = whRes.rows[0].wh_objid; + await client.query( + `UPDATE momo_stocks SET qty = qty - $1, update_date = NOW() + WHERE wh_objid = $2 AND item_objid = $3`, + [diff, whObjid, cur.item_objid] + ); + // 이력 — qty 부호: +이면 추가 출고(-diff), -이면 복원(+|diff|). + // move_type 은 OUT(-) / IN(+) 으로 표기, qty 컬럼은 부호 그대로. + const moveType = diff > 0 ? "OUT" : "IN"; + const moveQty = -diff; // OUT 이면 -, IN 이면 + + const moveObjid = String(Date.now()) + String(Math.floor(Math.random() * 10000)); + await client.query( + `INSERT INTO momo_stock_moves + (objid, wh_objid, item_objid, move_type, qty, ref_type, ref_objid, memo, regdate, regid) + VALUES ($1, $2, $3, $4, $5, 'ORDER', $6, $7, NOW(), $8)`, + [moveObjid, whObjid, cur.item_objid, moveType, moveQty, orderObjid, + `수량 수정: ${oldQty} → ${newQty}`, r.user.objid ?? r.user.userId] + ); + } + } + } + await client.query( `UPDATE momo_order_items SET qty = $2,