Files
SupplierSystem/server/src/services/supplierPrepBoardService.ts
T
雨天 eea2aa839b
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
采购与备货表: 新增「规格」列(展示录单规格)
两表原「商品描述」列取的是商品库 skus.spec/product_catalog.spec,没取录单写的 order_items.spec(ff15808)⇒ 商品库无规格时该列为空。新增独立只读「规格」列(商品名后,三语),取值录单规格优先 → 商品库兜底(采购再兜底 remark 里的 规格:)。

- 三处查询带出 order_spec / order_item_spec(备货内部表、供应商看板、采购列表)
- 不改「商品描述」列,也不动分组折叠/复制/导出文本与 purchase_status
- 新增契约测试 purchasePrepSpecColumn.contract.test.ts

导出/打印同步(原设计 §4 ⑥)本轮不做。
2026-10-16 16:37:33 +08:00

185 lines
7.7 KiB
TypeScript

import { pool } from '../db';
import { orderCodeActiveExistsSql } from './orderTrashScope';
import { resolveCategoryQualityRequirement } from './qualityReportService';
export type SupplierPrepBoardRow = {
id: string;
order_code: string | null;
sku_code: string | null;
sku_name?: string | null;
item_name?: string | null;
category_code?: string | null;
category_name?: string | null;
supplier_code: string;
supplier_name?: string | null;
supplier_contact?: string | null;
supplier_phone?: string | null;
prepare_qty: number;
purchase_price: number;
prepare_status: number;
prepare_time?: string | null;
created_at?: string | null;
is_stockout: boolean;
stockout_reason?: string | null;
transferred_to_purchase: boolean;
purchase_code?: string | null;
specification?: string | null;
/** 录单写的规格 order_items.spec(优先展示) */
order_spec?: string | null;
product_brand?: string | null;
unit?: string | null;
delivery_date?: string | null;
company_name?: string | null;
delivery_address?: string | null;
processing_method?: string | null;
order_item_remark?: string | null;
requires_quality_report?: boolean;
};
/**
* 供应商 Web 备货看板:仅本供应商行 + 展示所需联表字段。
* 见 docs/design/20260804-supplier-web-prep-board.md
*/
export async function listSupplierPrepBoard(input: {
supplierCode: string;
branchCode?: string | null;
}): Promise<SupplierPrepBoardRow[]> {
const supplierCode = String(input.supplierCode || '').trim();
if (!supplierCode) {
throw Object.assign(new Error('账号未绑定供应商,请联系管理员'), {
status: 403,
code: 'supplier_unbound',
});
}
const params: unknown[] = [supplierCode];
const filters: string[] = [
'sp.supplier_code = $1',
'COALESCE(sp.is_history_repair, false) = false',
`(
sp.order_code IS NULL
OR TRIM(sp.order_code) = ''
OR ${orderCodeActiveExistsSql('sp.order_code')}
)`,
];
const branchCode = String(input.branchCode || '').trim();
if (branchCode) {
params.push(branchCode);
filters.push(`(
sp.branch_code = $${params.length}
OR (
(sp.branch_code IS NULL OR TRIM(sp.branch_code) = '')
AND EXISTS (
SELECT 1 FROM orders ox
WHERE ox.order_code = sp.order_code AND ox.branch_code = $${params.length}
)
)
)`);
}
const result = await pool.query(
`SELECT sp.id, sp.order_code, sp.sku_code, sp.supplier_code, sp.prepare_qty, sp.purchase_price,
sp.prepare_status, sp.prepare_time, sp.created_at, sp.branch_code,
sp.is_stockout, sp.stockout_reason, sp.transferred_to_purchase, sp.purchase_code,
sp.category_code AS prep_category_code,
COALESCE(NULLIF(TRIM(s.sku_name), ''), sp.item_name, oi.item_name, sp.sku_code) AS item_name,
s.sku_name,
s.spec AS specification,
oi.spec AS order_spec,
s.brand AS product_brand,
s.category_code AS sku_category_code,
cat.category_name,
COALESCE(NULLIF(TRIM(sp.unit), ''), NULLIF(TRIM(oi.ordered_unit), ''), oi.unit, s.unit, '个') AS unit,
COALESCE(
sp.processing_method,
CASE
WHEN LOWER(TRIM(COALESCE(oi.processing_owner, ''))) = 'supplier'
THEN NULLIF(TRIM(oi.processing_method), '')
ELSE NULL
END
) AS processing_method,
NULLIF(TRIM(oi.remark), '') AS order_item_remark,
to_char(o.delivery_date::date, 'YYYY-MM-DD') AS delivery_date,
COALESCE(NULLIF(TRIM(sup.company_name), ''), sp.supplier_code) AS supplier_name,
NULLIF(TRIM(sup.contact_name), '') AS supplier_contact,
NULLIF(TRIM(sup.contact_phone), '') AS supplier_phone,
COALESCE(c.company_name, '') AS company_name,
NULLIF(TRIM(o.delivery_address), '') AS delivery_address
FROM supplier_preparations sp
LEFT JOIN skus s ON s.sku_code = sp.sku_code
LEFT JOIN LATERAL (
SELECT oi0.item_name, oi0.unit, oi0.ordered_unit, oi0.processing_method, oi0.processing_owner,
NULLIF(TRIM(oi0.remark), '') AS remark, oi0.spec
FROM order_items oi0
WHERE oi0.order_code = sp.order_code
AND oi0.sku_code IS NOT DISTINCT FROM sp.sku_code
ORDER BY oi0.sort_order ASC NULLS LAST, oi0.id ASC
LIMIT 1
) oi ON TRUE
LEFT JOIN orders o ON o.order_code = sp.order_code
LEFT JOIN suppliers sup ON COALESCE(sup.supplier_code, '') = COALESCE(sp.supplier_code, '')
LEFT JOIN customers c ON c.customer_code = o.customer_code
LEFT JOIN categories cat ON cat.category_code = COALESCE(NULLIF(TRIM(sp.category_code), ''), s.category_code)
WHERE ${filters.join(' AND ')}
ORDER BY delivery_date ASC NULLS LAST, sp.created_at DESC
LIMIT 800`,
params
);
const rows: SupplierPrepBoardRow[] = [];
const reqCache = new Map<string, Awaited<ReturnType<typeof resolveCategoryQualityRequirement>>>();
for (const row of result.rows) {
const categoryCode =
String(row.prep_category_code || row.sku_category_code || '').trim() || null;
const branch = String(row.branch_code || branchCode || '').trim();
const sku = String(row.sku_code || '').trim();
const cacheKey = `${branch}|${sku}|${categoryCode || ''}`;
let req = reqCache.get(cacheKey);
if (!req) {
req = await resolveCategoryQualityRequirement({
branchCode: branch || null,
skuCode: sku || null,
categoryCode,
});
reqCache.set(cacheKey, req);
}
rows.push({
id: String(row.id),
order_code: row.order_code != null ? String(row.order_code) : null,
sku_code: row.sku_code != null ? String(row.sku_code) : null,
sku_name: row.sku_name != null ? String(row.sku_name) : null,
item_name: row.item_name != null ? String(row.item_name) : null,
category_code: categoryCode || undefined,
category_name: row.category_name != null ? String(row.category_name) : undefined,
supplier_code: String(row.supplier_code),
supplier_name: row.supplier_name != null ? String(row.supplier_name) : null,
supplier_contact: row.supplier_contact != null ? String(row.supplier_contact) : null,
supplier_phone: row.supplier_phone != null ? String(row.supplier_phone) : null,
prepare_qty: Number(row.prepare_qty) || 0,
purchase_price: Number(row.purchase_price) || 0,
prepare_status: Number(row.prepare_status) || 0,
prepare_time: row.prepare_time != null ? String(row.prepare_time) : null,
created_at: row.created_at != null ? String(row.created_at) : null,
is_stockout: Boolean(row.is_stockout),
stockout_reason: row.stockout_reason != null ? String(row.stockout_reason) : null,
transferred_to_purchase: Boolean(row.transferred_to_purchase),
purchase_code: row.purchase_code != null ? String(row.purchase_code) : null,
specification: row.specification != null ? String(row.specification) : null,
order_spec: row.order_spec != null ? String(row.order_spec).trim() || null : null,
product_brand: row.product_brand != null ? String(row.product_brand) : null,
unit: row.unit != null ? String(row.unit) : null,
delivery_date: row.delivery_date != null ? String(row.delivery_date) : null,
company_name: row.company_name != null ? String(row.company_name) : null,
delivery_address: row.delivery_address != null ? String(row.delivery_address) : null,
processing_method: row.processing_method != null ? String(row.processing_method) : null,
order_item_remark: row.order_item_remark != null ? String(row.order_item_remark).trim() || null : null,
requires_quality_report: req.requires === true,
});
}
return rows;
}