feat: Enhance settlement and draw management functionality

- Implement error handling for skipped settlement runs in DrawSettlementRunController, returning appropriate error messages based on draw status.
- Add validation in DrawPublishService to ensure draws are ready for publication, rejecting outdated result batches.
- Update SettlementBatchWorkflowService to revert ticket statuses upon settlement rejection and restore jackpot pool amounts.
- Refactor LotteryTransferService to improve transaction handling for transfer order reconciliation, ensuring idempotency during reversals.
- Add multi-language support for new error messages related to settlement processes.
This commit is contained in:
2026-05-26 14:10:16 +08:00
parent e4118d7b1d
commit 48349e3302
10 changed files with 354 additions and 43 deletions

View File

@@ -7,6 +7,7 @@ use App\Models\Player;
use App\Models\AdminUser;
use App\Models\TicketItem;
use App\Lottery\DrawStatus;
use App\Models\JackpotPool;
use App\Models\TicketOrder;
use App\Models\SettlementBatch;
use Illuminate\Support\Facades\DB;
@@ -59,10 +60,19 @@ final class SettlementBatchWorkflowService
throw new \RuntimeException('settlement_not_pending_review');
}
TicketItem::query()
->whereIn('id', $locked->details()->pluck('ticket_item_id'))
->where('status', 'pending_payout')
->update(['status' => 'success', 'win_amount' => 0, 'jackpot_win_amount' => 0]);
$itemIds = $locked->details()->pluck('ticket_item_id')->map(fn ($id) => (int) $id)->all();
if ($itemIds !== []) {
TicketItem::query()
->whereIn('id', $itemIds)
->update([
'status' => 'pending_draw',
'win_amount' => 0,
'jackpot_win_amount' => 0,
'settled_at' => null,
]);
}
$this->restoreJackpotPoolAfterReject($locked);
$locked->forceFill([
'status' => SettlementBatchStatus::Rejected->value,
@@ -85,6 +95,27 @@ final class SettlementBatchWorkflowService
throw new \RuntimeException('settlement_not_approved');
}
$batchItemIds = $locked->details()->pluck('ticket_item_id')->map(fn ($id) => (int) $id)->all();
$hasPendingDraw = TicketItem::query()
->where('draw_id', $locked->draw_id)
->where('status', 'pending_draw')
->exists();
if ($hasPendingDraw) {
throw new \RuntimeException('draw_has_unsettled_tickets');
}
if ($batchItemIds !== []) {
$orphanPendingPayout = TicketItem::query()
->where('draw_id', $locked->draw_id)
->where('status', 'pending_payout')
->whereNotIn('id', $batchItemIds)
->exists();
if ($orphanPendingPayout) {
throw new \RuntimeException('draw_has_unsettled_tickets');
}
}
$details = $locked->details()->with(['ticketItem.order'])->get();
$playerTotals = [];
$currencyByPlayer = [];
@@ -141,4 +172,31 @@ final class SettlementBatchWorkflowService
return $locked->refresh();
});
}
private function restoreJackpotPoolAfterReject(SettlementBatch $batch): void
{
$restoreAmount = (int) $batch->total_jackpot_payout_amount;
if ($restoreAmount <= 0) {
return;
}
$orderId = TicketItem::query()->where('draw_id', $batch->draw_id)->value('order_id');
$currencyCode = strtoupper((string) (TicketOrder::query()
->whereKey($orderId)
->value('currency_code') ?? 'NPR'));
$pool = JackpotPool::query()
->where('currency_code', $currencyCode)
->where('status', 1)
->lockForUpdate()
->first();
if ($pool === null) {
return;
}
$pool->forceFill([
'current_amount' => (int) $pool->current_amount + $restoreAmount,
])->save();
}
}