feat: enhance reconciliation job processing and reporting features

- Updated ReconcileItemIndexController to include admin user validation and improved transfer order handling.
- Refactored ReconcileJobStoreController to ensure date range handling is more precise with start and end of day adjustments.
- Enhanced various report controllers to include currency code in responses for better financial clarity.
- Introduced new methods in AdminReconcileJobService for wallet transfer job creation and improved item scanning logic.
- Added wallet lookup idempotent path configuration for integration services to enhance API interactions.
This commit is contained in:
2026-06-16 13:50:55 +08:00
parent 606ed6817e
commit 1e7b2b31ee
21 changed files with 886 additions and 71 deletions

View File

@@ -0,0 +1,88 @@
<?php
use App\Models\AdminUser;
use App\Models\Player;
use App\Models\ReconcileJob;
use App\Models\TransferOrder;
use Illuminate\Support\Facades\Hash;
use Database\Seeders\AdminRbacAndUserSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->seed(AdminRbacAndUserSeeder::class);
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
function reconcileScanToken(): string
{
$admin = AdminUser::query()->create([
'username' => 'reconcile_scan_admin',
'name' => 'Reconcile Scan',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
}
test('manual reconcile job create runs wallet transfer detector scan', function (): void {
$token = reconcileScanToken();
$admin = AdminUser::query()->where('username', 'reconcile_scan_admin')->firstOrFail();
$player = Player::query()->create([
'site_code' => 'main',
'site_player_id' => 'manual-reconcile-1',
'username' => null,
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
TransferOrder::query()->create([
'transfer_no' => 'TO_manual_scan',
'player_id' => $player->id,
'direction' => 'out',
'currency_code' => 'NPR',
'amount' => 500,
'idempotent_key' => 'manual-scan-key',
'status' => 'pending_reconcile',
'external_request_payload' => null,
'external_response_payload' => null,
'external_ref_no' => null,
'fail_reason' => 'main_site_timeout',
'finished_at' => null,
'created_at' => now()->subHours(2),
]);
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/reconcile-jobs', [
'reconcile_type' => 'wallet_transfer',
'date_from' => now()->subDay()->toDateString(),
'date_to' => now()->toDateString(),
'player_id' => $player->id,
])
->assertOk()
->assertJsonPath('data.item_count', 1);
$job = ReconcileJob::query()->latest('id')->firstOrFail();
expect($job->admin_user_id)->toBe($admin->id)
->and($job->items()->count())->toBe(1)
->and($job->items()->value('side_a_ref'))->toBe('TO_manual_scan');
});
test('manual reconcile job create returns zero items when scan finds nothing', function (): void {
$token = reconcileScanToken();
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/reconcile-jobs', [
'reconcile_type' => 'wallet_transfer',
'date_from' => now()->subDay()->toDateString(),
'date_to' => now()->toDateString(),
])
->assertOk()
->assertJsonPath('data.item_count', 0);
});

View File

@@ -0,0 +1,171 @@
<?php
use App\Models\Draw;
use App\Models\Player;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use App\Services\Admin\AdminReportQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('resolve date range defaults to last 30 days when filters empty', function (): void {
$range = app(AdminReportQueryService::class)->resolveDateRange([]);
expect($range['date_to'])->toBe(now()->toDateString())
->and($range['date_from'])->toBe(now()->subDays(29)->toDateString());
});
test('daily profit rows omit business days with no betting activity', function (): void {
$player = Player::query()->create([
'site_code' => 'main',
'site_player_id' => 'rpt-daily-1',
'username' => 'daily_rpt',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
Draw::query()->create([
'draw_no' => '20260502-001',
'business_date' => '2026-05-02',
'sequence_no' => 2,
'status' => 'settled',
'start_time' => now()->subDay(),
'close_time' => now()->subDay()->addHour(),
'draw_time' => now()->subDay()->addHours(2),
'cooling_end_time' => null,
'result_source' => null,
'current_result_version' => 1,
'settle_version' => 1,
'is_reopened' => false,
]);
$activeDraw = Draw::query()->create([
'draw_no' => '20260501-001',
'business_date' => '2026-05-01',
'sequence_no' => 1,
'status' => 'settled',
'start_time' => now()->subDays(2),
'close_time' => now()->subDays(2)->addHour(),
'draw_time' => now()->subDays(2)->addHours(2),
'cooling_end_time' => null,
'result_source' => null,
'current_result_version' => 1,
'settle_version' => 1,
'is_reopened' => false,
]);
$order = TicketOrder::query()->create([
'order_no' => 'RPT-DAILY-ORD',
'player_id' => $player->id,
'draw_id' => $activeDraw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 1000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 1000,
'total_estimated_payout' => 0,
'status' => 'settled',
'submit_source' => 'h5',
'client_trace_id' => 'trace-daily',
]);
TicketItem::query()->create([
'ticket_no' => 'RPT-DAILY-TK',
'order_id' => $order->id,
'player_id' => $player->id,
'draw_id' => $activeDraw->id,
'original_number' => '1234',
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 4,
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => 1000,
'total_bet_amount' => 1000,
'rebate_rate_snapshot' => '0.0000',
'commission_rate_snapshot' => '0.0000',
'actual_deduct_amount' => 1000,
'win_amount' => 0,
'jackpot_win_amount' => 0,
'status' => 'settled',
]);
$rows = app(AdminReportQueryService::class)->dailyProfitRows('2026-05-01', '2026-05-02');
expect($rows)->toHaveCount(1)
->and($rows[0]['business_date'])->toBe('2026-05-01')
->and($rows[0]['total_bet_minor'])->toBe(1000);
});
test('player win loss uses draw business date not order created date', function (): void {
$player = Player::query()->create([
'site_code' => 'main',
'site_player_id' => 'rpt-axis-1',
'username' => 'axis_user',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$businessDate = now()->toDateString();
$orderCreatedAt = now()->addDay()->startOfDay();
$draw = Draw::query()->create([
'draw_no' => 'RPT-AXIS-'.now()->format('YmdHis'),
'business_date' => $businessDate,
'sequence_no' => 1,
'status' => 'settled',
'start_time' => now()->subHour(),
'close_time' => now()->addHour(),
'draw_time' => now()->addHours(2),
'cooling_end_time' => null,
'result_source' => null,
'current_result_version' => 1,
'settle_version' => 1,
'is_reopened' => false,
]);
$order = TicketOrder::query()->create([
'order_no' => 'RPT-AXIS-ORD',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 2000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 2000,
'total_estimated_payout' => 0,
'status' => 'settled',
'submit_source' => 'h5',
'client_trace_id' => 'trace-axis',
'created_at' => $orderCreatedAt,
'updated_at' => $orderCreatedAt,
]);
TicketItem::query()->create([
'ticket_no' => 'RPT-AXIS-TK',
'order_id' => $order->id,
'player_id' => $player->id,
'draw_id' => $draw->id,
'original_number' => '5678',
'normalized_number' => '5678',
'play_code' => 'big',
'dimension' => 4,
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => 2000,
'total_bet_amount' => 2000,
'rebate_rate_snapshot' => '0.0000',
'commission_rate_snapshot' => '0.0000',
'actual_deduct_amount' => 2000,
'win_amount' => 0,
'jackpot_win_amount' => 0,
'status' => 'settled',
]);
$service = app(AdminReportQueryService::class);
$orderDate = $orderCreatedAt->toDateString();
expect($service->playerWinLossPaginated(null, $businessDate, $businessDate, 1, 20)->total())->toBe(1)
->and($service->playerWinLossPaginated(null, $orderDate, $orderDate, 1, 20)->total())->toBe(0);
});