'use strict'; const assert = require('assert'); const OrderB2bService = require('../../../../app/service/systems/order_b2b'); describe('OrderB2bService packing quantity validation', () => { function createService({ orderItems, packedItems }) { const service = Object.create(OrderB2bService.prototype); service.ctx = { model: { OrderItemsFactory: { getCurrentModel: async () => ({ findAll: async () => orderItems, }), }, OrderB2bItem: { findAll: async () => packedItems, }, }, }; return service; } it('accepts exact per-line quantities and matching carton count', async () => { const service = createService({ orderItems: [ { id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 }, { id: 12, item_code: 'SKU-B', item_name: '商品B', plan_qty: 1 }, ], packedItems: [ { order_item_id: 11, box_no: 'A01', quantity: 2 }, { order_item_id: 11, box_no: 'A02', quantity: 1 }, { order_item_id: 12, box_no: 'A02', quantity: 1 }, ], }); const result = await service.assertB2bPackingQuantityComplete({ warehouseId: 7, orderId: 99, sourceType: 'current', totalBoxCount: 2, transaction: { LOCK: { UPDATE: 'UPDATE' } }, }); assert.deepStrictEqual(result, { boxCount: 2 }); }); it('rejects underpacked product lines with per-item mismatch details', async () => { const service = createService({ orderItems: [ { id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 }, ], packedItems: [ { order_item_id: 11, box_no: 'A01', quantity: 2 }, ], }); await assert.rejects( service.assertB2bPackingQuantityComplete({ warehouseId: 7, orderId: 99, sourceType: 'current', totalBoxCount: 1, transaction: { LOCK: { UPDATE: 'UPDATE' } }, }), error => { assert.strictEqual(error.code, 'B2B_PREP_PACKING_INCOMPLETE'); assert.deepStrictEqual(error.data.item_mismatches, [ { order_item_id: 11, item_code: 'SKU-A', item_name: '商品A', expected_quantity: 3, packed_quantity: 2, }, ]); return true; } ); }); it('replaces all packing rows only with item lines from the target order', async () => { const transaction = { LOCK: { UPDATE: 'UPDATE' }, commit: async () => {}, rollback: async () => {}, }; const deleted = []; const created = []; const service = Object.create(OrderB2bService.prototype); service.ctx = { model: { transaction: async () => transaction, OrderItemsFactory: { getCurrentModel: async () => ({ findAll: async () => [{ id: 11 }], }), }, OrderB2bItem: { destroy: async options => deleted.push(options), bulkCreate: async (rows, options) => created.push({ rows, options }), }, OrderB2b: { update: async () => {}, }, }, }; service.loadPrepTargets = async () => ({ rowMap: new Map([[ 99, { is_b2b: 'Y', status: 'pending' }]]), b2bMap: new Map([[ 99, { prep_status: 'padding' }]]), sourceTypeMap: new Map([[ 99, 'current' ]]), }); service.assertPrepOrders = () => {}; service.writePrepOperationLogs = async () => {}; const result = await service.updateB2bPackingItems({ warehouseId: 7, orderIds: [ 99 ], items: [ { order_item_id: 11, box_no: 'A01', quantity: 2 }, ], }); assert.deepStrictEqual(result, { order_id: 99, item_count: 1, box_count: 1 }); assert.deepStrictEqual(deleted[0].where, { warehouse_id: 7, order_id: 99 }); assert.strictEqual(created[0].rows[0].order_item_id, 11); assert.strictEqual(created[0].rows[0].quantity, 2); assert.strictEqual(created[0].options.transaction, transaction); }); it('returns per-item pick locations from wms_picking merged by space', async () => { const transaction = { commit: async () => {}, rollback: async () => {} }; const pickQueries = []; const service = Object.create(OrderB2bService.prototype); service.app = { Sequelize: { Op: { ne: Symbol('ne') } } }; service.ctx = { model: { transaction: async () => transaction, OrderDeliveryFactory: { getCurrentModel: async () => ({ findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }), }), }, OrderItemsFactory: { getCurrentModel: async () => ({ findAll: async () => [ { id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 3 }, { id: 12, item_code: 'SKU-B', item_name: '商品B', plan_qty: 1 }, ], }), }, OrderB2bItem: { findAll: async () => [{ order_item_id: 11, box_no: 'A01', quantity: 3 }] }, OrderB2b: { findOne: async () => ({ prep_status: 'padding', total_box_count: 1 }) }, Picking: { findAll: async options => { pickQueries.push(options); return [ { order_item_id: 11, space_id: 31, space_name: 'B-02-01', quantity: 2 }, { order_item_id: 11, space_id: 30, space_name: 'A-01-01', quantity: 1 }, { order_item_id: 11, space_id: 31, space_name: 'B-02-01', quantity: 1 }, { order_item_id: 12, space_id: 30, space_name: 'A-01-01', quantity: 1 }, { order_item_id: 12, space_id: null, space_name: '', quantity: 1 }, ]; }, }, }, }; service.isB2bPackingQuantityCheckEnabled = async () => false; const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] }); assert.strictEqual(pickQueries.length, 1); assert.deepStrictEqual(pickQueries[0].where, { warehouse_id: 7, order_id: 99, status: { [service.app.Sequelize.Op.ne]: 'cancel' }, }); assert.strictEqual(result.pick_location_count, 3); assert.deepStrictEqual(result.items[0].pick_locations, [ { space_id: 30, space_name: 'A-01-01', quantity: 1 }, { space_id: 31, space_name: 'B-02-01', quantity: 3 }, ]); assert.deepStrictEqual(result.items[1].pick_locations, [ { space_id: null, space_name: null, quantity: 1 }, { space_id: 30, space_name: 'A-01-01', quantity: 1 }, ]); }); it('outputs empty pick locations when the order has no allocation rows', async () => { const transaction = { commit: async () => {}, rollback: async () => {} }; const service = Object.create(OrderB2bService.prototype); service.app = { Sequelize: { Op: { ne: Symbol('ne') } } }; service.ctx = { model: { transaction: async () => transaction, OrderDeliveryFactory: { getCurrentModel: async () => ({ findOne: async () => ({ id: 99, is_b2b: 'Y', status: 'pending', delivery_order_code: 'DO-99' }), }), }, OrderItemsFactory: { getCurrentModel: async () => ({ findAll: async () => [{ id: 11, item_code: 'SKU-A', item_name: '商品A', plan_qty: 1 }], }), }, OrderB2bItem: { findAll: async () => [] }, OrderB2b: { findOne: async () => null }, Picking: { findAll: async () => [] }, }, }; service.isB2bPackingQuantityCheckEnabled = async () => false; const result = await service.getB2bPackingSnapshot({ warehouseId: 7, orderIds: [ 99 ] }); assert.strictEqual(result.pick_location_count, 0); assert.deepStrictEqual(result.items[0].pick_locations, []); assert.strictEqual(result.items[0].packed_quantity, 0); }); });