import type { PoolClient } from 'pg'; import { pool } from '../db'; type Db = Pick; 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), }); }