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:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Reconcile;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Models\ReconcileJob;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -9,12 +10,24 @@ use App\Models\ReconcileItem;
|
||||
use App\Support\AdminApiList;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\AdminTransferOrderCapabilities;
|
||||
use App\Services\Wallet\LotteryTransferService;
|
||||
use App\Services\Wallet\HttpMainSiteWalletIdempotentProbeClient;
|
||||
use App\Services\Wallet\MainSiteWalletIdempotentProbeResult;
|
||||
|
||||
/** GET /api/v1/admin/reconcile-jobs/{reconcile_job}/items */
|
||||
final class ReconcileItemIndexController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LotteryTransferService $transferService,
|
||||
private readonly HttpMainSiteWalletIdempotentProbeClient $mainSiteProbeClient,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, ReconcileJob $reconcile_job): JsonResponse
|
||||
{
|
||||
$admin = $request->lotteryAdmin();
|
||||
abort_if($admin === null, 401);
|
||||
|
||||
$p = AdminApiList::readPaging($request, 50, 200);
|
||||
|
||||
$paginator = $reconcile_job->items()
|
||||
@@ -27,26 +40,104 @@ final class ReconcileItemIndexController extends Controller
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$transferStatuses = $transferNos === []
|
||||
? []
|
||||
$transferOrders = $transferNos === []
|
||||
? collect()
|
||||
: TransferOrder::query()
|
||||
->with(['player:id,site_code,site_player_id'])
|
||||
->whereIn('transfer_no', $transferNos)
|
||||
->pluck('status', 'transfer_no')
|
||||
->all();
|
||||
->get()
|
||||
->keyBy('transfer_no');
|
||||
|
||||
return AdminApiList::jsonWith($paginator, fn (ReconcileItem $r) => [
|
||||
'id' => (int) $r->id,
|
||||
'side_a_ref' => $r->side_a_ref,
|
||||
'side_b_ref' => $r->side_b_ref,
|
||||
'difference_amount' => (int) $r->difference_amount,
|
||||
'status' => $r->status,
|
||||
'resolved_at' => $r->resolved_at?->toIso8601String(),
|
||||
'is_resolved' => $r->resolved_at !== null || in_array($transferStatuses[$r->side_a_ref ?? ''] ?? null, ['success', 'reversed', 'manually_processed'], true),
|
||||
'current_transfer_status' => $transferStatuses[$r->side_a_ref ?? ''] ?? null,
|
||||
'created_at' => $r->created_at?->toIso8601String(),
|
||||
], [
|
||||
return AdminApiList::jsonWith($paginator, function (ReconcileItem $r) use ($transferOrders, $admin): array {
|
||||
$transferNo = (string) ($r->side_a_ref ?? '');
|
||||
/** @var TransferOrder|null $order */
|
||||
$order = $transferNo !== '' ? $transferOrders->get($transferNo) : null;
|
||||
$resolvedStatuses = ['success', 'reversed', 'manually_processed'];
|
||||
$currentStatus = $order?->status;
|
||||
$isResolved = $r->resolved_at !== null
|
||||
|| in_array($currentStatus, $resolvedStatuses, true);
|
||||
|
||||
$capabilities = $order instanceof TransferOrder
|
||||
? AdminTransferOrderCapabilities::forOrder(
|
||||
$order,
|
||||
$admin instanceof AdminUser ? $admin : null,
|
||||
$this->transferService,
|
||||
)
|
||||
: [
|
||||
'can_reverse' => false,
|
||||
'can_complete_credit' => false,
|
||||
'can_manually_process' => false,
|
||||
];
|
||||
|
||||
$mainSiteCheck = $this->resolveMainSiteCheck($order);
|
||||
|
||||
return [
|
||||
'id' => (int) $r->id,
|
||||
'side_a_ref' => $r->side_a_ref,
|
||||
'side_b_ref' => $r->side_b_ref,
|
||||
'external_ref_no' => $order?->external_ref_no,
|
||||
'difference_amount' => (int) $r->difference_amount,
|
||||
'status' => $r->status,
|
||||
'resolved_at' => $r->resolved_at?->toIso8601String(),
|
||||
'is_resolved' => $isResolved,
|
||||
'current_transfer_status' => $currentStatus,
|
||||
'transfer_direction' => $order?->direction,
|
||||
'transfer_fail_reason' => $order?->fail_reason,
|
||||
'main_site_check' => $mainSiteCheck['status'],
|
||||
'main_site_check_message' => $mainSiteCheck['message'],
|
||||
'main_site_external_ref_no' => $mainSiteCheck['external_ref_no'],
|
||||
...$capabilities,
|
||||
'created_at' => $r->created_at?->toIso8601String(),
|
||||
];
|
||||
}, [
|
||||
'job_id' => (int) $reconcile_job->id,
|
||||
'job_no' => $reconcile_job->job_no,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{status: string, message: ?string, external_ref_no: ?string}
|
||||
*/
|
||||
private function resolveMainSiteCheck(?TransferOrder $order): array
|
||||
{
|
||||
if ($order === null || $order->player === null) {
|
||||
return [
|
||||
'status' => MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED,
|
||||
'message' => null,
|
||||
'external_ref_no' => null,
|
||||
];
|
||||
}
|
||||
|
||||
if (trim((string) $order->idempotent_key) === '') {
|
||||
return [
|
||||
'status' => MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED,
|
||||
'message' => null,
|
||||
'external_ref_no' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$shouldProbe = trim((string) $order->external_ref_no) !== ''
|
||||
|| $order->status === 'success'
|
||||
|| (
|
||||
$order->direction === 'in'
|
||||
&& $order->status === 'pending_reconcile'
|
||||
&& $order->fail_reason === 'lottery_credit_failed'
|
||||
);
|
||||
|
||||
if (! $shouldProbe) {
|
||||
return [
|
||||
'status' => MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED,
|
||||
'message' => null,
|
||||
'external_ref_no' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$probe = $this->mainSiteProbeClient->probe($order->player, (string) $order->idempotent_key);
|
||||
|
||||
return [
|
||||
'status' => $probe->status,
|
||||
'message' => $probe->message,
|
||||
'external_ref_no' => $probe->externalRefNo ?? $order->external_ref_no,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,12 @@ final class ReconcileJobStoreController extends Controller
|
||||
(string) ($data['reconcile_type'] ?? 'wallet_transfer'),
|
||||
isset($data['period_start'])
|
||||
? Carbon::parse((string) $data['period_start'])
|
||||
: (isset($data['date_from']) ? Carbon::parse((string) $data['date_from']) : null),
|
||||
: (isset($data['date_from']) ? Carbon::parse((string) $data['date_from'])->startOfDay() : null),
|
||||
isset($data['period_end'])
|
||||
? Carbon::parse((string) $data['period_end'])
|
||||
: (isset($data['date_to']) ? Carbon::parse((string) $data['date_to']) : null),
|
||||
: (isset($data['date_to']) ? Carbon::parse((string) $data['date_to'])->endOfDay() : null),
|
||||
$items,
|
||||
isset($data['player_id']) ? (int) $data['player_id'] : null,
|
||||
);
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -30,6 +30,8 @@ final class AdminReportDailyProfitController extends Controller
|
||||
$scope,
|
||||
);
|
||||
|
||||
return AdminApiList::json($paginator, static fn (array $row): array => $row);
|
||||
return AdminApiList::jsonWith($paginator, static fn (array $row): array => $row, [
|
||||
'currency_code' => $service->resolvePeriodCurrencyCode($range['date_from'], $range['date_to'], $scope),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ final class AdminReportPlayDimensionController extends Controller
|
||||
$scope,
|
||||
);
|
||||
|
||||
return AdminApiList::json($paginator, static function (object $row): array {
|
||||
return AdminApiList::jsonWith($paginator, static function (object $row): array {
|
||||
return [
|
||||
'play_code' => (string) $row->play_code,
|
||||
'dimension' => (int) $row->dimension,
|
||||
@@ -40,6 +40,8 @@ final class AdminReportPlayDimensionController extends Controller
|
||||
'total_payout_minor' => (int) $row->total_payout_minor,
|
||||
'approx_house_gross_minor' => (int) $row->approx_house_gross_minor,
|
||||
];
|
||||
});
|
||||
}, [
|
||||
'currency_code' => $service->resolvePeriodCurrencyCode($range['date_from'], $range['date_to'], $scope),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ final class AdminReportPlayerWinLossController extends Controller
|
||||
$scope,
|
||||
);
|
||||
|
||||
return AdminApiList::json($paginator, static function (object $row): array {
|
||||
return AdminApiList::jsonWith($paginator, static function (object $row): array {
|
||||
return [
|
||||
'player_id' => (int) $row->player_id,
|
||||
'agent_node_id' => isset($row->agent_node_id) ? (int) $row->agent_node_id : null,
|
||||
@@ -43,6 +43,8 @@ final class AdminReportPlayerWinLossController extends Controller
|
||||
'total_payout_minor' => (int) $row->total_payout_minor,
|
||||
'net_win_loss_minor' => (int) $row->net_win_loss_minor,
|
||||
];
|
||||
});
|
||||
}, [
|
||||
'currency_code' => $service->resolvePeriodCurrencyCode($range['date_from'], $range['date_to'], $scope),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,21 +32,16 @@ final class AdminReportRebateCommissionController extends Controller
|
||||
$scope,
|
||||
);
|
||||
|
||||
$response = AdminApiList::json($paginator, static function (object $row): array {
|
||||
return AdminApiList::jsonWith($paginator, static function (object $row): array {
|
||||
return [
|
||||
'play_code' => (string) $row->play_code,
|
||||
'total_rebate_minor' => (int) $row->total_rebate_minor,
|
||||
'order_count' => (int) $row->order_count,
|
||||
'ticket_item_count' => (int) $row->ticket_item_count,
|
||||
];
|
||||
});
|
||||
|
||||
$payload = $response->getData(true);
|
||||
if (is_array($payload)) {
|
||||
$payload['disclaimer'] = 'wallet_instant_rebate_not_agent_period_settlement';
|
||||
$response->setData($payload);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}, [
|
||||
'currency_code' => $service->resolvePeriodCurrencyCode($range['date_from'], $range['date_to'], $scope),
|
||||
'disclaimer' => 'wallet_instant_rebate_not_agent_period_settlement',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Support\PaginationTrait;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Support\AdminScopePolicy;
|
||||
use App\Support\AgentNodeApiPresenter;
|
||||
use App\Support\AdminTransferOrderCapabilities;
|
||||
use App\Support\CurrencyFormatter;
|
||||
use App\Services\Wallet\LotteryTransferService;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -117,11 +118,6 @@ final class TransferOrderListController extends Controller
|
||||
{
|
||||
$p = $o->player;
|
||||
$amount = (int) $o->amount;
|
||||
$canWriteWallet = $admin !== null && (
|
||||
$admin->hasPermissionCode('service.wallet.adjust')
|
||||
|| $admin->hasPermissionCode('service.reconcile.manage')
|
||||
|| $admin->hasPermissionCode('service.wallet.manage')
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => $o->id,
|
||||
@@ -138,16 +134,7 @@ final class TransferOrderListController extends Controller
|
||||
'amount_formatted' => CurrencyFormatter::fromMinor($amount),
|
||||
'idempotent_key' => $o->idempotent_key,
|
||||
'status' => $o->status,
|
||||
'can_reverse' => $canWriteWallet
|
||||
&& $o->status === 'pending_reconcile'
|
||||
&& ($o->direction === 'out' || $this->transferService->isEligibleForTransferInReverse($o)),
|
||||
'can_complete_credit' => $canWriteWallet
|
||||
&& $o->direction === 'in'
|
||||
&& $o->status === 'pending_reconcile'
|
||||
&& $o->fail_reason === 'lottery_credit_failed'
|
||||
&& trim((string) $o->external_ref_no) !== '',
|
||||
'can_manually_process' => $canWriteWallet
|
||||
&& $this->transferService->isEligibleForManualProcess($o),
|
||||
...AdminTransferOrderCapabilities::forOrder($o, $admin, $this->transferService),
|
||||
'external_ref_no' => $o->external_ref_no,
|
||||
'external_request_payload' => $o->external_request_payload,
|
||||
'external_response_payload' => $o->external_response_payload,
|
||||
|
||||
Reference in New Issue
Block a user