Files
SupplierSystem/server/src/services/orderOutboundBatchService.ts
T
雨天 072d02b687
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(delivery-note): 送货单图片增加生产时间列
生产时间取本单实际出库批次的 inventory.production_date,跨批逐行
显示;未出库不猜批次、该格留空。订单详情只新增
batch_production_dates 字段,取数下沉到 service。
2026-10-20 10:05:12 +08:00

43 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { pool } from '../db';
/**
* 送货单「生产时间」取数:本单**实际用掉的批次**的生产日期。
*
* 唯一事实源 = 出库流水 `inventory_logs`(`related_order_code` + `sku_code` + `batch_no`)
* ⋈ 批次表 `inventory.production_date`。出库走 FIFO 可能跨多批 ⇒ 同一 SKU 多个日期(去重升序);
* **未出库就没有流水 ⇒ 返回空数组,不猜批次**(不拿该 SKU 的在库批次顶替)。
*
* 只读:不改出库/FIFO/成本口径。设计:docs/design/20261020-送货单图片增加生产时间列.md
* 放在 services 层(路由层不允许新增裸 SQL,见 server/tests/routeLayerBoundary.test.ts)。
*/
export async function listOrderBatchProductionDates(
orderCode: string
): Promise<Map<string, string[]>> {
const bySku = new Map<string, string[]>();
const code = String(orderCode || '').trim();
if (!code) return bySku;
const result = await pool.query(
`SELECT il.sku_code, inv.production_date::text AS production_date
FROM inventory_logs il
JOIN inventory inv ON inv.batch_no = il.batch_no AND inv.sku_code = il.sku_code
WHERE il.related_order_code = $1
AND il.operation_type IN ('picking_out', 'delivery_loading_out', 'retail_out')
AND COALESCE(il.quantity, 0) > 0
AND inv.production_date IS NOT NULL
GROUP BY il.sku_code, inv.production_date
ORDER BY il.sku_code, inv.production_date ASC`,
[code]
);
for (const row of result.rows as Array<{ sku_code: string | null; production_date: string | null }>) {
const skuCode = String(row.sku_code || '');
const productionDate = String(row.production_date || '');
if (!skuCode || !productionDate) continue;
const dates = bySku.get(skuCode);
if (dates) dates.push(productionDate);
else bySku.set(skuCode, [productionDate]);
}
return bySku;
}