feat(finance): 完成财务控制、L1 钱包硬化与迁移收敛

- P0/P1:影子总账追加调整、关账/退款/资金/积分幂等、成本版本追溯、对账订单项维度
- 迁移收敛:原 103–114 合并为 103_finance_foundation、104_finance_controls、105_work_order_consignment
- 数据:L1 钱包 BIGINT/CHECK/奖励幂等;重复奖励审计口径修正与人工核对清单
- 报表:销售毛利缺失成本收入提示,旧收支页明确现金流口径
- 文档:同步迁移基线、合并评估、代码复核与上线说明
This commit is contained in:
yml2213
2026-10-27 16:08:16 +08:00
parent a53321ef30
commit 9d5af359fa
47 changed files with 4527 additions and 1410 deletions
@@ -342,18 +342,49 @@ export async function setAdminAffiliateProductPrice(
}
}
// 2. 追溯改价或相同生效时间改价:直接就地更新当前 active 版本,避免破坏 effective_to > effective_from 约束或触发唯一索引冲突
if (existing && existingTime !== null && targetTime <= existingTime) {
// 2. 追溯改价:退休旧版本并新增一个从 targetTime 起生效的版本,旧行保留可追溯。
if (existing && existingTime !== null && targetTime < existingTime) {
await query("UPDATE cost_versions SET status = 'retired', updated_at = $1 WHERE id = $2", [
now,
existing.id,
])
const inserted = await query<{ id: number }>(
`
INSERT INTO cost_versions (
cost_item_id, scope_type, provider, shop_id, canonical_product_id,
work_product_rule_id, amount_fen, rate_bps, effective_from, effective_to,
source, status, change_reason, created_by_admin_user_id,
approved_by_admin_user_id, created_at, updated_at
) VALUES (
$1, 'product', '', '', $2,
NULL, $3, 0, $4, NULL,
'manual', 'active', $5, $6, $6, $7, $7
)
RETURNING id
`,
[costItem.id, product.id, amountFen, effectiveFrom, changeReason, session.userId, now],
)
return {
success: true,
product,
costVersionId: Number(inserted.rows[0]?.id || 0),
amountFen,
effectiveFrom,
message: '采购成本已更新(新增追溯版本)',
}
}
// 2.1 相同生效时间的更正:没有可插入的时间区间,只能就地更正并保留变更原因。
if (existing && existingTime !== null && targetTime === existingTime) {
await query(
`UPDATE cost_versions
SET amount_fen = $1,
effective_from = $2,
change_reason = $3,
created_by_admin_user_id = $4,
approved_by_admin_user_id = $4,
updated_at = $5
WHERE id = $6`,
[amountFen, effectiveFrom, changeReason, session.userId, now, existing.id],
change_reason = $2,
created_by_admin_user_id = $3,
approved_by_admin_user_id = $3,
updated_at = $4
WHERE id = $5`,
[amountFen, changeReason, session.userId, now, existing.id],
)
return {
success: true,
@@ -178,6 +178,7 @@ export async function grantPlatformCredit(payload: JsonObject, session: AdminSes
const quantity = normalizePositiveQuantity(payload.quantity)
const platformRef = optionalText(payload.platformRef ?? payload.platform_ref, 100)
const note = optionalText(payload.note, 300)
const requestId = optionalText(payload.requestId ?? payload.request_id, 80)
const unitPrices = await loadUnitPrices()
const unitPriceFen = unitPrices[platformKey]
@@ -194,16 +195,36 @@ export async function grantPlatformCredit(payload: JsonObject, session: AdminSes
WHERE id = $1`,
[account.id, nextBalance, now],
)
const entryResult = await client.query<CreditEntryRow>(
`
INSERT INTO platform_credit_entries (
account_id, entry_type, quantity_delta, balance_after, amount_fen,
platform_ref, note, created_at
) VALUES ($1, 'grant', $2, $3, $4, $5, $6, $7)
RETURNING *
`,
[account.id, quantity, nextBalance, quantity * unitPriceFen, platformRef, note, now],
)
let entryResult
try {
entryResult = await client.query<CreditEntryRow>(
`
INSERT INTO platform_credit_entries (
account_id, entry_type, quantity_delta, balance_after, amount_fen,
platform_ref, note, request_id, created_at
) VALUES ($1, 'grant', $2, $3, $4, $5, $6, $7, $8)
RETURNING *
`,
[
account.id,
quantity,
nextBalance,
quantity * unitPriceFen,
platformRef,
note,
requestId,
now,
],
)
} catch (error) {
if ((error as { code?: string }).code === '23505') {
throw createHttpError('该请求已处理,请勿重复提交', {
statusCode: 409,
errorCode: 'platform_credit_request_duplicate',
})
}
throw error
}
return {
accountId: Number(account.id),
platformKey,
@@ -236,6 +257,7 @@ export async function adjustPlatformCredit(payload: JsonObject, session: AdminSe
errorCode: 'platform_credit_adjust_note_required',
})
}
const requestId = optionalText(payload.requestId ?? payload.request_id, 80)
const unitPrices = await loadUnitPrices()
const unitPriceFen = unitPrices[platformKey]
const now = nowIso()
@@ -256,16 +278,27 @@ export async function adjustPlatformCredit(payload: JsonObject, session: AdminSe
WHERE id = $1`,
[account.id, nextBalance, now],
)
const entryResult = await client.query<CreditEntryRow>(
`
INSERT INTO platform_credit_entries (
account_id, entry_type, quantity_delta, balance_after, amount_fen,
note, created_at
) VALUES ($1, 'adjust', $2, $3, $4, $5, $6)
RETURNING *
`,
[account.id, quantity, nextBalance, quantity * unitPriceFen, note, now],
)
let entryResult
try {
entryResult = await client.query<CreditEntryRow>(
`
INSERT INTO platform_credit_entries (
account_id, entry_type, quantity_delta, balance_after, amount_fen,
note, request_id, created_at
) VALUES ($1, 'adjust', $2, $3, $4, $5, $6, $7)
RETURNING *
`,
[account.id, quantity, nextBalance, quantity * unitPriceFen, note, requestId, now],
)
} catch (error) {
if ((error as { code?: string }).code === '23505') {
throw createHttpError('该请求已处理,请勿重复提交', {
statusCode: 409,
errorCode: 'platform_credit_request_duplicate',
})
}
throw error
}
return {
accountId: Number(account.id),
platformKey,
@@ -1391,30 +1391,49 @@ export async function setAdminProductPrice(
let costVersionId = existing?.id || 0
if (existing && existingTime !== null && targetTime <= existingTime) {
// 追溯改价或相同生效时间改价:就地更新 active 版本
await query(
`UPDATE cost_versions
SET amount_fen = $1,
effective_from = $2,
canonical_product_id = $3,
work_product_rule_id = $4,
change_reason = $5,
created_by_admin_user_id = $6,
approved_by_admin_user_id = $6,
updated_at = $7
WHERE id = $8`,
if (existing && existingTime !== null && targetTime < existingTime) {
// 追溯改价:退休旧版本并新增一个从 targetTime 起生效的版本,旧行保留可追溯。
// 不能就地改金额,否则 cost_snapshots.cost_version_id 指向的版本内容会被篡改。
await query("UPDATE cost_versions SET status = 'retired', updated_at = $1 WHERE id = $2", [
now,
existing.id,
])
const inserted = await query<{ id: number }>(
`INSERT INTO cost_versions (
cost_item_id, scope_type, provider, shop_id, canonical_product_id,
work_product_rule_id, amount_fen, rate_bps, effective_from, effective_to,
source, status, change_reason, created_by_admin_user_id,
approved_by_admin_user_id, created_at, updated_at
) VALUES (
$1, 'rule', '', '', $2,
$3, $4, 0, $5, NULL,
'manual', 'active', $6, $7,
$7, $8, $8
) RETURNING id`,
[
amountFen,
effectiveFrom,
costItemId,
product.id,
rule?.id ?? null,
amountFen,
effectiveFrom,
changeReason,
session.userId,
now,
existing.id,
],
)
costVersionId = inserted.rows[0]?.id || 0
} else if (existing && existingTime !== null && targetTime === existingTime) {
// 相同生效时间的更正:没有可插入的时间区间,只能就地更正并保留变更原因。
await query(
`UPDATE cost_versions
SET amount_fen = $1,
change_reason = $2,
created_by_admin_user_id = $3,
approved_by_admin_user_id = $3,
updated_at = $4
WHERE id = $5`,
[amountFen, changeReason, session.userId, now, existing.id],
)
} else {
if (existing) {
await query(
@@ -1510,17 +1529,36 @@ export async function setAdminProductPrice(
const targetTime = Date.parse(effectiveFrom)
let costVersionId = existing?.id || 0
if (existing && existingTime !== null && targetTime <= existingTime) {
if (existing && existingTime !== null && targetTime < existingTime) {
await query("UPDATE cost_versions SET status = 'retired', updated_at = $1 WHERE id = $2", [
now,
existing.id,
])
const inserted = await query<{ id: number }>(
`INSERT INTO cost_versions (
cost_item_id, scope_type, provider, shop_id, canonical_product_id,
work_product_rule_id, amount_fen, rate_bps, effective_from, effective_to,
source, status, change_reason, created_by_admin_user_id,
approved_by_admin_user_id, created_at, updated_at
) VALUES (
$1, 'product', '', '', $2,
NULL, $3, 0, $4, NULL,
'manual', 'active', $5, $6,
$6, $7, $7
) RETURNING id`,
[costItemId, product.id, amountFen, effectiveFrom, changeReason, session.userId, now],
)
costVersionId = inserted.rows[0]?.id || 0
} else if (existing && existingTime !== null && targetTime === existingTime) {
await query(
`UPDATE cost_versions
SET amount_fen = $1,
effective_from = $2,
change_reason = $3,
created_by_admin_user_id = $4,
approved_by_admin_user_id = $4,
updated_at = $5
WHERE id = $6`,
[amountFen, effectiveFrom, changeReason, session.userId, now, existing.id],
change_reason = $2,
created_by_admin_user_id = $3,
approved_by_admin_user_id = $3,
updated_at = $4
WHERE id = $5`,
[amountFen, changeReason, session.userId, now, existing.id],
)
} else {
if (existing) {