36 lines
1.4 KiB
PHP
36 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\SettlementBatch;
|
|
|
|
final class SettlementBatchFinancialSummary
|
|
{
|
|
/**
|
|
* @return array{total_bet_amount: int, total_actual_deduct: int, platform_profit: int, currency_code: ?string}
|
|
*/
|
|
public static function forBatch(SettlementBatch $batch): array
|
|
{
|
|
$totals = $batch->details()
|
|
->join('ticket_items', 'ticket_items.id', '=', 'ticket_settlement_details.ticket_item_id')
|
|
->join('ticket_orders', 'ticket_orders.id', '=', 'ticket_items.order_id')
|
|
->selectRaw('COALESCE(SUM(ticket_items.total_bet_amount), 0) as total_bet_amount')
|
|
->selectRaw('COALESCE(SUM(ticket_items.actual_deduct_amount), 0) as total_actual_deduct')
|
|
->selectRaw('MIN(ticket_orders.currency_code) as currency_code')
|
|
->first();
|
|
|
|
$totalBet = (int) ($totals?->total_bet_amount ?? 0);
|
|
$totalActualDeduct = (int) ($totals?->total_actual_deduct ?? 0);
|
|
$totalPayout = (int) $batch->total_payout_amount;
|
|
|
|
return [
|
|
'total_bet_amount' => $totalBet,
|
|
'total_actual_deduct' => $totalActualDeduct,
|
|
'platform_profit' => $totalActualDeduct - $totalPayout,
|
|
'currency_code' => is_string($totals?->currency_code) && $totals->currency_code !== ''
|
|
? $totals->currency_code
|
|
: null,
|
|
];
|
|
}
|
|
}
|