来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
185 lines
5.2 KiB
TypeScript
185 lines
5.2 KiB
TypeScript
import type { PoolClient } from 'pg';
|
|
import { pool } from '../db';
|
|
|
|
type Db = Pick<PoolClient, 'query'>;
|
|
|
|
function dbClient(client?: PoolClient): Db {
|
|
return client || pool;
|
|
}
|
|
|
|
function currentCycleCode() {
|
|
const d = new Date();
|
|
return `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, '0')}`;
|
|
}
|
|
|
|
function roundMoney(value: number) {
|
|
return Math.round(Math.max(0, value) * 100) / 100;
|
|
}
|
|
|
|
async function upsertOperationCost(
|
|
client: PoolClient | undefined,
|
|
input: {
|
|
cycleCode: string;
|
|
orderCode: string;
|
|
costType: string;
|
|
costSubtype: string;
|
|
amount: number;
|
|
branchCode?: string | null;
|
|
skuCode?: string | null;
|
|
referenceType: string;
|
|
referenceId: string;
|
|
}
|
|
) {
|
|
const amount = roundMoney(input.amount);
|
|
if (amount <= 0) return { posted: false as const };
|
|
|
|
const db = dbClient(client);
|
|
const orderCode = String(input.orderCode || '').trim();
|
|
const referenceId = String(input.referenceId || '').trim();
|
|
if (!orderCode || !referenceId) return { posted: false as const };
|
|
|
|
const skuCode = input.skuCode ? String(input.skuCode).trim() : null;
|
|
|
|
const byRef = await db.query(
|
|
`SELECT id FROM cost_details
|
|
WHERE reference_type = $1 AND reference_id = $2
|
|
LIMIT 1`,
|
|
[input.referenceType, referenceId]
|
|
);
|
|
if (byRef.rowCount) {
|
|
await db.query(
|
|
`UPDATE cost_details
|
|
SET amount = $2,
|
|
allocated_amount = $2,
|
|
cycle_code = $3,
|
|
branch_code = COALESCE($4, branch_code),
|
|
sku_code = COALESCE($5, sku_code),
|
|
cost_type = $6,
|
|
cost_subtype = $7,
|
|
order_code = $8,
|
|
status = 20
|
|
WHERE id = $1`,
|
|
[
|
|
byRef.rows[0].id,
|
|
amount,
|
|
input.cycleCode,
|
|
input.branchCode || null,
|
|
skuCode,
|
|
input.costType,
|
|
input.costSubtype,
|
|
orderCode,
|
|
]
|
|
);
|
|
return { posted: true as const, updated: true as const, id: String(byRef.rows[0].id) };
|
|
}
|
|
|
|
// 同订单同类型(加工还按 SKU)再查一次,避免缺 reference 时重复
|
|
let byBiz;
|
|
if (skuCode) {
|
|
byBiz = await db.query(
|
|
`SELECT id FROM cost_details
|
|
WHERE order_code = $1 AND cost_type = $2 AND sku_code = $3
|
|
LIMIT 1`,
|
|
[orderCode, input.costType, skuCode]
|
|
);
|
|
} else {
|
|
byBiz = await db.query(
|
|
`SELECT id FROM cost_details
|
|
WHERE order_code = $1 AND cost_type = $2
|
|
AND (sku_code IS NULL OR TRIM(sku_code) = '')
|
|
LIMIT 1`,
|
|
[orderCode, input.costType]
|
|
);
|
|
}
|
|
if (byBiz.rowCount) {
|
|
await db.query(
|
|
`UPDATE cost_details
|
|
SET amount = $2,
|
|
allocated_amount = $2,
|
|
cycle_code = $3,
|
|
branch_code = COALESCE($4, branch_code),
|
|
reference_type = $5,
|
|
reference_id = $6,
|
|
cost_subtype = $7,
|
|
status = 20
|
|
WHERE id = $1`,
|
|
[
|
|
byBiz.rows[0].id,
|
|
amount,
|
|
input.cycleCode,
|
|
input.branchCode || null,
|
|
input.referenceType,
|
|
referenceId,
|
|
input.costSubtype,
|
|
]
|
|
);
|
|
return { posted: true as const, updated: true as const, id: String(byBiz.rows[0].id) };
|
|
}
|
|
|
|
const inserted = await db.query(
|
|
`INSERT INTO cost_details (
|
|
cycle_code, order_code, cost_type, cost_subtype, amount, allocated_amount,
|
|
allocation_method, status, branch_code, sku_code, reference_type, reference_id
|
|
) VALUES ($1,$2,$3,$4,$5,$5,'manual',20,$6,$7,$8,$9)
|
|
RETURNING id`,
|
|
[
|
|
input.cycleCode,
|
|
orderCode,
|
|
input.costType,
|
|
input.costSubtype,
|
|
amount,
|
|
input.branchCode || null,
|
|
skuCode,
|
|
input.referenceType,
|
|
referenceId,
|
|
]
|
|
);
|
|
return { posted: true as const, updated: false as const, id: String(inserted.rows[0].id) };
|
|
}
|
|
|
|
/** 加工验收通过:写入 labor_processing(金额来自 process_fee 或 unit×净重) */
|
|
export async function postProcessingLaborCost(
|
|
client: PoolClient | undefined,
|
|
input: {
|
|
taskId: string;
|
|
orderCode: string;
|
|
skuCode?: string | null;
|
|
processFee?: number | null;
|
|
processUnitPrice?: number | null;
|
|
netWeight?: number | null;
|
|
cycleCode?: string | null;
|
|
branchCode?: string | null;
|
|
}
|
|
) {
|
|
const fee = Number(input.processFee);
|
|
const unit = Number(input.processUnitPrice);
|
|
const net = Number(input.netWeight);
|
|
let amount = Number.isFinite(fee) && fee > 0 ? fee : 0;
|
|
if (amount <= 0 && Number.isFinite(unit) && unit > 0 && Number.isFinite(net) && net > 0) {
|
|
amount = unit * net;
|
|
}
|
|
amount = roundMoney(amount);
|
|
if (amount <= 0) return { posted: false as const };
|
|
|
|
const db = dbClient(client);
|
|
const orderMeta = await db.query(
|
|
`SELECT cycle_code, branch_code FROM orders WHERE order_code = $1 LIMIT 1`,
|
|
[input.orderCode]
|
|
);
|
|
const cycleCode =
|
|
String(input.cycleCode || orderMeta.rows[0]?.cycle_code || '').trim() || currentCycleCode();
|
|
const branchCode = input.branchCode || orderMeta.rows[0]?.branch_code || null;
|
|
|
|
return upsertOperationCost(client, {
|
|
cycleCode,
|
|
orderCode: input.orderCode,
|
|
costType: 'labor_processing',
|
|
costSubtype: 'processing_inspection',
|
|
amount,
|
|
branchCode,
|
|
skuCode: input.skuCode,
|
|
referenceType: 'processing_task',
|
|
referenceId: String(input.taskId),
|
|
});
|
|
}
|