feat(finance): 完成成本快照运行层、销售事实与人工支出登记

迁移 108-111:快照业务时间与预估转实际、销售事实表、系统成本项、公司人工支出登记。接单平台按工单实际 reward 入账;人工支出支持登记/审批/驳回/报销并写 actual 快照。新增销售毛利页、人工支出页与订单入口。
This commit is contained in:
yml2213
2026-10-22 16:41:03 +08:00
parent 15989af87c
commit 0889235870
39 changed files with 4824 additions and 143 deletions
@@ -47,6 +47,39 @@ export async function replaceOrderItems(
)
}
export async function applyOrderItemRevenueAllocationWithExecutor(
executor: QueryExecutor,
orderId: number | string,
totalAmountFen: number,
): Promise<number> {
const revenue = Math.max(0, Math.trunc(Number(totalAmountFen) || 0))
const result = await executor(
`
WITH ranked AS (
SELECT
id,
ROW_NUMBER() OVER (ORDER BY id ASC) AS item_rank,
(SELECT COUNT(*) FROM order_items WHERE order_id = $1) AS item_count
FROM order_items
WHERE order_id = $1
)
UPDATE order_items oi
SET
revenue_amount_fen = CASE
WHEN r.item_count = 1 THEN $2::bigint
ELSE ($2::bigint / r.item_count)
+ CASE WHEN r.item_rank = r.item_count THEN ($2::bigint % r.item_count) ELSE 0 END
END,
revenue_basis = CASE WHEN r.item_count = 1 THEN 'order_total' ELSE 'equal_split' END,
updated_at = $3
FROM ranked r
WHERE oi.id = r.id
`,
[orderId, revenue, new Date().toISOString()],
)
return result.rowCount || 0
}
export async function replaceOrderItemsWithExecutor(
executor: QueryExecutor,
orderId: number | string,