feat(finance): 支持代录指定客服账户,并上线影子总账/对账/关账/快手平台退款
- 人工确认履约:管理员/财务/运营可传 fundAccountId / creditOwnerAdminUserId 指定客服备用金/积分账户;客服仍强制本人账户 - 新增迁移 112:finance_accounts / journal_entries / journal_lines / periods / reconciliations / reconciliation_items,种子 11 个科目 - 影子总账:销售事实按日生成收入/退款/成本凭证,支持手工凭证、试算平衡; 不接管钱包余额;总账同步接入定时任务与退款重算 - 期间关账/反关账:关账先补凭证并校验试算平衡,closed 期间禁止新增凭证 - 平台对账:JSON 账单导入,按 provider + platform_order_id 匹配订单/退款, 标记 matched/amount_mismatch/missing_order/missing_refund/duplicate - 平台退款:快手行业(91)同意退款 API 接入,成功后落 source=platform; 抖音/拼多多返回 501,保留手工退款 - 新增后台「财务 → 总账与对账」页面(总账/期间/对账三个 Tab) - 集成测试扩展到代录、总账平衡、关账阻断、手工凭证、对账匹配 验证:backend check 844(840 pass / 4 skip);frontend check 172 + build; 迁移 112 在 scratch 库全量执行并重跑幂等;本地生产副本已升级到 112, 销售事实同步 56 条凭证,借贷合计 1,885,351,864 = 1,885,351,864。
This commit is contained in:
@@ -322,7 +322,7 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
|
||||
|
||||
if (platformKey === 'fund_cash') {
|
||||
// 备用金现金:复用唯一写入口,但把 client 传进去,和快照共用同一个事务。
|
||||
const fundAccountId = await resolveFundAccountId(session, client)
|
||||
const fundAccountId = await resolveFundAccountId(payload, session, client)
|
||||
const { writeFundExpense } = await import('../admin/admin-fund-service.js')
|
||||
const written = await writeFundExpense(client, {
|
||||
agentAccountId: fundAccountId,
|
||||
@@ -349,8 +349,10 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
|
||||
balanceAfter = written.balanceAfterFen
|
||||
subjectSource = 'manual_expense'
|
||||
} else {
|
||||
// 管理员/财务可以代录到指定客服的积分账户;客服只能用自己的。
|
||||
const creditOwnerAdminUserId = await resolveCreditOwnerAdminUserId(payload, session, client)
|
||||
const consumed = await consumePlatformCreditWithClient(client, {
|
||||
ownerAdminUserId: session.userId,
|
||||
ownerAdminUserId: creditOwnerAdminUserId,
|
||||
platformKey,
|
||||
quantity: creditQuantity,
|
||||
relatedTaskId: null,
|
||||
@@ -400,6 +402,10 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
|
||||
configId: Number(row.config_id),
|
||||
platformKey,
|
||||
platformLabel: PLATFORM_LABELS[platformKey] || platformKey,
|
||||
creditOwnerAdminUserId:
|
||||
platformKey === 'fund_cash'
|
||||
? null
|
||||
: Number(payload.creditOwnerAdminUserId || 0) || null,
|
||||
creditQuantity,
|
||||
creditUnitPriceFen: unitPriceFen,
|
||||
costMode: costPlan.costMode,
|
||||
@@ -454,8 +460,43 @@ export async function confirmManualFulfillment(payload: JsonObject, session: Adm
|
||||
}
|
||||
|
||||
/** 客服自己的备用金账户(管理员代录时用指定账户)。 */
|
||||
async function resolveFundAccountId(session: AdminSession, client?: Pick<PoolClient, 'query'>) {
|
||||
const PRIVILEGED_ROLES = new Set(['admin', 'finance', 'operator'])
|
||||
|
||||
function canActOnBehalf(session: AdminSession) {
|
||||
return PRIVILEGED_ROLES.has(String(session.role || ''))
|
||||
}
|
||||
|
||||
/**
|
||||
* 备用金账户:
|
||||
* - 客服只能用本人账户;
|
||||
* - 管理员/财务/运营可显式指定客服账户(fundAccountId),未指定时回退本人账户。
|
||||
*/
|
||||
async function resolveFundAccountId(
|
||||
payload: JsonObject,
|
||||
session: AdminSession,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
) {
|
||||
const { getOwnAgentAccountId } = await import('../admin/admin-fund-service.js')
|
||||
const selectedId = Number(payload.fundAccountId ?? payload.fund_account_id ?? 0)
|
||||
if (canActOnBehalf(session) && Number.isInteger(selectedId) && selectedId > 0) {
|
||||
const sql = `SELECT fa.id
|
||||
FROM fund_accounts fa
|
||||
JOIN admin_users au ON au.id = fa.owner_admin_user_id
|
||||
WHERE fa.id = $1
|
||||
AND fa.account_type = 'agent'
|
||||
AND fa.status = 'active'
|
||||
AND au.status = 'active'
|
||||
LIMIT 1`
|
||||
const selected = client
|
||||
? await client.query<{ id: number }>(sql, [selectedId])
|
||||
: await query<{ id: number }>(sql, [selectedId])
|
||||
const found = selected.rows[0]?.id
|
||||
if (found) return Number(found)
|
||||
throw createHttpError('指定的客服备用金账户不存在或已停用', {
|
||||
statusCode: 404,
|
||||
errorCode: 'fund_agent_account_not_found',
|
||||
})
|
||||
}
|
||||
const own = await getOwnAgentAccountId(session.userId, client)
|
||||
if (own) return own
|
||||
throw createHttpError('当前账号还没有备用金账户', {
|
||||
@@ -464,6 +505,33 @@ async function resolveFundAccountId(session: AdminSession, client?: Pick<PoolCli
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分账户归属人:
|
||||
* - 客服只能扣本人;
|
||||
* - 管理员/财务/运营可指定客服(creditOwnerAdminUserId),未指定时回退本人。
|
||||
*/
|
||||
async function resolveCreditOwnerAdminUserId(
|
||||
payload: JsonObject,
|
||||
session: AdminSession,
|
||||
client?: Pick<PoolClient, 'query'>,
|
||||
) {
|
||||
const selectedId = Number(
|
||||
payload.creditOwnerAdminUserId ?? payload.credit_owner_admin_user_id ?? 0,
|
||||
)
|
||||
if (canActOnBehalf(session) && Number.isInteger(selectedId) && selectedId > 0) {
|
||||
const sql = `SELECT id FROM admin_users WHERE id = $1 AND status = 'active' AND role = 'support' LIMIT 1`
|
||||
const selected = client
|
||||
? await client.query<{ id: number }>(sql, [selectedId])
|
||||
: await query<{ id: number }>(sql, [selectedId])
|
||||
if (selected.rows[0]?.id) return Number(selected.rows[0].id)
|
||||
throw createHttpError('指定的客服账号不存在或已停用', {
|
||||
statusCode: 404,
|
||||
errorCode: 'platform_credit_owner_not_found',
|
||||
})
|
||||
}
|
||||
return Number(session.userId)
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认前按 SKU 匹配人工履约配置。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user