拆分接单平台规则
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export type AfterSalesRecoveryAllocation = {
|
||||
pendingDepositDeductionAmount: number
|
||||
availableDeductionAmount: number
|
||||
debtAmount: number
|
||||
}
|
||||
|
||||
export function resolveAfterSalesRecoveryAllocation(input: {
|
||||
recoveryAmount: number
|
||||
pendingDepositAmount: number
|
||||
availableAmount: number
|
||||
}): AfterSalesRecoveryAllocation {
|
||||
let remaining = Math.max(0, Number(input.recoveryAmount) || 0)
|
||||
const pendingDepositDeductionAmount = Math.min(
|
||||
remaining,
|
||||
Math.max(0, Number(input.pendingDepositAmount) || 0),
|
||||
)
|
||||
remaining -= pendingDepositDeductionAmount
|
||||
const availableDeductionAmount = Math.min(
|
||||
remaining,
|
||||
Math.max(0, Number(input.availableAmount) || 0),
|
||||
)
|
||||
remaining -= availableDeductionAmount
|
||||
return {
|
||||
pendingDepositDeductionAmount,
|
||||
availableDeductionAmount,
|
||||
debtAmount: remaining,
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { PoolClient } from 'pg'
|
||||
|
||||
import { query, withTransaction } from '../../db/client.js'
|
||||
import { resolveAfterSalesRecoveryAllocation } from './after-sales-recovery.js'
|
||||
import { ensureWorkerWalletWithClient, getWorkerWalletWithClient } from './shared.js'
|
||||
import type {
|
||||
WorkerAfterSalesCaseEventRow,
|
||||
@@ -426,8 +427,6 @@ async function recoverAfterSalesAmountWithClient(
|
||||
availableDeductionAmount: number
|
||||
debtAmount: number
|
||||
}> {
|
||||
let remaining = Math.max(0, input.amount)
|
||||
let pendingDepositDeductionAmount = 0
|
||||
const pendingResult = await client.query<WorkerDepositUnfreezeRow>(
|
||||
`
|
||||
SELECT * FROM worker_deposit_unfreezes
|
||||
@@ -439,10 +438,19 @@ async function recoverAfterSalesAmountWithClient(
|
||||
)
|
||||
await ensureWorkerWalletWithClient(client, input.workerId, input.now)
|
||||
let wallet: WorkerWalletRow | null = await getWorkerWalletWithClient(client, input.workerId)
|
||||
const allocation = resolveAfterSalesRecoveryAllocation({
|
||||
recoveryAmount: input.amount,
|
||||
pendingDepositAmount: pendingResult.rows.reduce(
|
||||
(total, row) => total + Math.max(0, Number(row.amount) || 0),
|
||||
0,
|
||||
),
|
||||
availableAmount: Number(wallet?.available_amount || 0),
|
||||
})
|
||||
let pendingRemaining = allocation.pendingDepositDeductionAmount
|
||||
for (const row of pendingResult.rows) {
|
||||
if (remaining <= 0) break
|
||||
const rowAmount = Number(row.amount || 0)
|
||||
const deduction = Math.min(rowAmount, remaining)
|
||||
if (pendingRemaining <= 0) break
|
||||
const rowAmount = Math.max(0, Number(row.amount) || 0)
|
||||
const deduction = Math.min(rowAmount, pendingRemaining)
|
||||
if (deduction <= 0) continue
|
||||
if (deduction >= rowAmount) {
|
||||
await client.query(
|
||||
@@ -478,12 +486,11 @@ async function recoverAfterSalesAmountWithClient(
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
pendingDepositDeductionAmount += deduction
|
||||
remaining -= deduction
|
||||
pendingRemaining -= deduction
|
||||
}
|
||||
|
||||
wallet = await getWorkerWalletWithClient(client, input.workerId)
|
||||
const availableDeductionAmount = Math.min(remaining, Number(wallet?.available_amount || 0))
|
||||
const availableDeductionAmount = allocation.availableDeductionAmount
|
||||
if (availableDeductionAmount > 0) {
|
||||
const nextAvailable = Number(wallet?.available_amount || 0) - availableDeductionAmount
|
||||
await client.query(
|
||||
@@ -507,19 +514,18 @@ async function recoverAfterSalesAmountWithClient(
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
remaining -= availableDeductionAmount
|
||||
}
|
||||
if (remaining > 0) {
|
||||
if (allocation.debtAmount > 0) {
|
||||
await client.query(
|
||||
`
|
||||
INSERT INTO worker_after_sales_debts (
|
||||
case_id, worker_id, original_amount, outstanding_amount, status, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $3, 'open', $4, $4)
|
||||
`,
|
||||
[input.caseId, input.workerId, remaining, input.now],
|
||||
[input.caseId, input.workerId, allocation.debtAmount, input.now],
|
||||
)
|
||||
}
|
||||
return { pendingDepositDeductionAmount, availableDeductionAmount, debtAmount: remaining }
|
||||
return allocation
|
||||
}
|
||||
|
||||
async function createAfterSalesCaseEventWithClient(
|
||||
|
||||
Reference in New Issue
Block a user