Files
lotteryLaravel/app/Services/AgentSettlement/AgentSettlementBillAdjustmentService.php
kang 3c57941895
Some checks failed
lotterLaravel CI / test (push) Has been cancelled
lotterLaravel E2E / e2e-api (push) Has been cancelled
feat: add idempotency support for settlement adjustments and payments, fix rebate handling
- Added idempotency_key parameter to settlement bill adjustment and payment endpoints to prevent duplicate operations
- Fixed player rebate profile updates to preserve existing values when only one field is modified
- Moved wallet player validation earlier in AdminPlayerStoreController to prevent unnecessary processing
- Enhanced extra rebate calculation to include 'in_bill' and 'settled' statuses in AgentP
2026-06-23 16:49:57 +08:00

122 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services\AgentSettlement;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
/**
* 已锁定账单的补差/冲正单§21.1、§21.2)。
*/
final class AgentSettlementBillAdjustmentService
{
public function __construct(
private readonly AgentSettlementBillGuard $billGuard,
private readonly AgentSettlementPeriodCompletionService $periodCompletion,
) {}
public function createAdjustment(
int $originalBillId,
int $amount,
string $adjustmentType,
?string $reason,
int $adminUserId,
?string $idempotencyKey = null,
): int {
$original = DB::table('settlement_bills')->where('id', $originalBillId)->first();
if ($original === null) {
throw new \InvalidArgumentException('bill_not_found');
}
if ($this->periodCompletion->isPeriodReadOnly((int) $original->settlement_period_id)) {
throw ValidationException::withMessages([
'period' => ['completed'],
]);
}
if (! in_array((string) $original->status, ['confirmed', 'partial_paid', 'settled', 'overdue'], true)) {
throw ValidationException::withMessages([
'bill' => ['not_locked'],
]);
}
if ($amount === 0) {
throw ValidationException::withMessages([
'amount' => ['zero'],
]);
}
if ($idempotencyKey !== null && $idempotencyKey !== '') {
$existing = DB::table('settlement_adjustments')
->where('original_bill_id', $originalBillId)
->where('idempotency_key', $idempotencyKey)
->first();
if ($existing !== null) {
return (int) DB::table('settlement_bills')
->where('reversed_bill_id', $originalBillId)
->where('bill_type', 'adjustment')
->value('id');
}
}
$type = in_array($adjustmentType, ['adjustment', 'reversal'], true)
? $adjustmentType
: 'adjustment';
return (int) DB::transaction(function () use ($original, $amount, $type, $reason, $adminUserId, $idempotencyKey): int {
if ($idempotencyKey !== null && $idempotencyKey !== '') {
$existing = DB::table('settlement_adjustments')
->where('original_bill_id', (int) $original->id)
->where('idempotency_key', $idempotencyKey)
->lockForUpdate()
->first();
if ($existing !== null) {
return (int) DB::table('settlement_bills')
->where('reversed_bill_id', (int) $original->id)
->where('bill_type', 'adjustment')
->value('id');
}
}
$now = now();
$newBillId = (int) DB::table('settlement_bills')->insertGetId([
'settlement_period_id' => (int) $original->settlement_period_id,
'bill_type' => $type,
'owner_type' => (string) $original->owner_type,
'owner_id' => (int) $original->owner_id,
'counterparty_type' => (string) $original->counterparty_type,
'counterparty_id' => (int) $original->counterparty_id,
'gross_win_loss' => 0,
'rebate_amount' => 0,
'adjustment_amount' => $amount,
'platform_rounding_adjustment' => 0,
'net_amount' => $amount,
'paid_amount' => 0,
'unpaid_amount' => abs($amount),
'status' => 'pending_confirm',
'reversed_bill_id' => (int) $original->id,
'meta_json' => json_encode([
'original_bill_id' => (int) $original->id,
'original_net_amount' => (int) $original->net_amount,
]),
'created_at' => $now,
'updated_at' => $now,
]);
DB::table('settlement_adjustments')->insert([
'settlement_period_id' => (int) $original->settlement_period_id,
'original_bill_id' => (int) $original->id,
'adjustment_type' => $type,
'amount' => $amount,
'reason' => $reason,
'idempotency_key' => $idempotencyKey,
'created_by' => $adminUserId > 0 ? $adminUserId : null,
'created_at' => $now,
'updated_at' => $now,
]);
return $newBillId;
});
}
}