来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import type { PoolClient } from 'pg';
|
|
import { pool } from '../db';
|
|
import { upsertOrderSkuPurchaseCostFromUnitCost } from './orderCostPostingService';
|
|
|
|
type Db = Pick<PoolClient, 'query'>;
|
|
|
|
function normalizeUnitCost(value: unknown): number {
|
|
const n = Number(value);
|
|
if (!Number.isFinite(n) || n < 0) return 0;
|
|
return Math.round(n * 10000) / 10000;
|
|
}
|
|
|
|
/** 按明细 id 覆盖当前单位成本预估,并同步采购账本(一套账 1B) */
|
|
export async function setOrderItemUnitCostById(
|
|
db: Db,
|
|
itemId: string,
|
|
unitCost: unknown
|
|
): Promise<void> {
|
|
const id = String(itemId || '').trim();
|
|
if (!id) return;
|
|
const cost = normalizeUnitCost(unitCost);
|
|
await db.query(`UPDATE order_items SET unit_cost = $1::numeric WHERE id = $2::uuid`, [
|
|
cost,
|
|
id,
|
|
]);
|
|
const row = await db.query(
|
|
`SELECT order_code, sku_code, COALESCE(is_extra_dish, false) AS is_extra_dish
|
|
FROM order_items WHERE id = $1::uuid LIMIT 1`,
|
|
[id]
|
|
);
|
|
if (!row.rowCount) return;
|
|
const orderCode = String(row.rows[0].order_code || '').trim();
|
|
const skuCode = String(row.rows[0].sku_code || '').trim();
|
|
if (!orderCode || !skuCode) return;
|
|
// 加菜成本由 reconcileExtraDishPurchaseCosts 入自采侧,勿记供应
|
|
if (row.rows[0].is_extra_dish === true || row.rows[0].is_extra_dish === 't') return;
|
|
await upsertOrderSkuPurchaseCostFromUnitCost(db, orderCode, skuCode, cost, {
|
|
orderItemId: id,
|
|
});
|
|
}
|
|
|
|
/** 按订单+SKU 覆盖同 SKU 全部明细行的单位成本预估,并同步采购账本(一套账 1B) */
|
|
export async function setOrderItemUnitCostByOrderSku(
|
|
db: Db,
|
|
orderCode: string,
|
|
skuCode: string,
|
|
unitCost: unknown
|
|
): Promise<void> {
|
|
const order = String(orderCode || '').trim();
|
|
const sku = String(skuCode || '').trim();
|
|
if (!order || !sku) return;
|
|
const cost = normalizeUnitCost(unitCost);
|
|
const updated = await db.query(
|
|
`UPDATE order_items SET unit_cost = $1::numeric
|
|
WHERE order_code = $2 AND sku_code = $3
|
|
RETURNING id::text AS id, COALESCE(is_extra_dish, false) AS is_extra_dish`,
|
|
[cost, order, sku]
|
|
);
|
|
if (!updated.rowCount) {
|
|
// 静默漏账高危点:没有匹配明细就没有后续账本写入,必须留痕。
|
|
// 见 docs/design/20260911-backfill-missing-prep-purchase-cost.md
|
|
console.warn(
|
|
`[costPosting] setOrderItemUnitCostByOrderSku 未匹配到订单明细,跳过账本写入: order=${order}, sku=${sku}, cost=${cost}`
|
|
);
|
|
return;
|
|
}
|
|
for (const row of updated.rows) {
|
|
if (row.is_extra_dish === true || row.is_extra_dish === 't') continue;
|
|
await upsertOrderSkuPurchaseCostFromUnitCost(db, order, sku, cost, {
|
|
orderItemId: String(row.id),
|
|
});
|
|
}
|
|
}
|
|
|
|
/** 客户生效报价单上的 SKU 成本价(录单种子) */
|
|
export async function resolveActiveQuoteSheetCostPrices(
|
|
customerCode: string,
|
|
skuCodes: string[],
|
|
branchCode?: string | null,
|
|
db: Db = pool
|
|
): Promise<Map<string, number>> {
|
|
const codes = [...new Set(skuCodes.map((c) => String(c || '').trim()).filter(Boolean))];
|
|
const map = new Map<string, number>();
|
|
if (!customerCode.trim() || !codes.length) return map;
|
|
|
|
const params: unknown[] = [customerCode.trim(), codes];
|
|
let branchFilter = '';
|
|
const branch = String(branchCode || '').trim();
|
|
if (branch) {
|
|
params.push(branch);
|
|
branchFilter = `AND (qs.branch_code IS NULL OR qs.branch_code = $${params.length})`;
|
|
}
|
|
|
|
const result = await db.query(
|
|
`SELECT DISTINCT ON (i.sku_code)
|
|
i.sku_code,
|
|
COALESCE(i.cost_price, 0)::numeric AS cost_price
|
|
FROM customer_quote_sheet_items i
|
|
INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code
|
|
INNER JOIN customer_quote_sheet_customers qsc
|
|
ON qsc.quote_sheet_code = qs.quote_sheet_code
|
|
WHERE qsc.customer_code = $1
|
|
AND i.sku_code = ANY($2::text[])
|
|
AND qs.approval_status = 30
|
|
AND qs.status = 1
|
|
AND (qs.expires_at IS NULL OR qs.expires_at > NOW())
|
|
${branchFilter}
|
|
ORDER BY i.sku_code,
|
|
CASE WHEN COALESCE(i.cost_price, 0) > 0 THEN 0 ELSE 1 END,
|
|
qs.updated_at DESC NULLS LAST,
|
|
qs.created_at DESC`,
|
|
params
|
|
);
|
|
|
|
for (const row of result.rows) {
|
|
map.set(String(row.sku_code), normalizeUnitCost(row.cost_price));
|
|
}
|
|
return map;
|
|
}
|