feat: 增强代理结算和账单管理功能

- 在多个控制器中引入 SettlementPartyEnrichment 服务,以优化代理结算和账单的处理逻辑。
- 更新 AgentSettlementBillIndexController 和 AgentSettlementBillShowController,支持根据账单 ID 和关键字进行查询。
- 在 AgentSettlementPeriodCloseController 中添加对站点管理权限的验证,确保只有具备相应权限的管理员能够关闭账期。
- 在 AgentSettlementPeriodIndexController 中更新账期数据的返回格式,提升数据的完整性和可用性。
- 引入对相对占成比例的支持,增强代理资料的管理能力,确保数据一致性。
This commit is contained in:
2026-06-05 18:00:56 +08:00
parent a44679665d
commit 2d32f006c5
63 changed files with 4893 additions and 288 deletions

View File

@@ -2,19 +2,32 @@
namespace App\Services\AgentSettlement;
use App\Models\AdminUser;
use App\Support\AdminDataScope;
use App\Support\AgentSettlementPeriodWindow;
use App\Support\PlayerFundingMode;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/** 账期窗口内信用流水与占成流水笔数(关账前诊断)。 */
final class AgentSettlementPeriodPipelineService
{
public function __construct(
private readonly UnsettledTicketPeriodWarning $unsettledWarning,
private readonly ShareLedgerScopedProfitAggregator $scopedProfitAggregator,
) {}
/**
* @param Collection<int, object> $periods settlement_periods 行,须含 id、period_start、period_end、admin_site_id
* @return array<int, array{credit_ledger_count: int, share_ledger_count: int}>
* @return array<int, array{
* credit_ledger_count: int,
* share_ledger_count: int,
* game_win_loss_total: int,
* win_loss_scope: 'platform'|'agent',
* basic_rebate_total: int,
* unsettled_ticket_count: int,
* }>
*/
public function countsForPeriods(Collection $periods): array
public function countsForPeriods(Collection $periods, ?AdminUser $admin = null): array
{
if ($periods->isEmpty()) {
return [];
@@ -26,37 +39,73 @@ final class AgentSettlementPeriodPipelineService
->pluck('code', 'id');
$out = [];
$viewer = $this->scopedProfitAggregator->resolveViewer($admin);
foreach ($periods as $period) {
$periodId = (int) $period->id;
$siteCode = (string) ($siteCodes[(int) $period->admin_site_id] ?? '');
if ($siteCode === '') {
$out[$periodId] = ['credit_ledger_count' => 0, 'share_ledger_count' => 0];
$out[$periodId] = [
'credit_ledger_count' => 0,
'share_ledger_count' => 0,
'game_win_loss_total' => 0,
'win_loss_scope' => $viewer['scope'],
'basic_rebate_total' => 0,
'unsettled_ticket_count' => 0,
];
continue;
}
$start = Carbon::parse($period->period_start)->startOfDay();
$end = Carbon::parse($period->period_end)->endOfDay();
[$start, $end] = AgentSettlementPeriodWindow::bounds(
(string) $period->period_start,
(string) $period->period_end,
);
$creditCount = (int) DB::table('credit_ledger as cl')
$creditQuery = DB::table('credit_ledger as cl')
->join('players as p', function ($join): void {
$join->on('p.id', '=', 'cl.owner_id')
->where('cl.owner_type', '=', 'player');
})
->where('p.site_code', $siteCode)
->where('p.funding_mode', PlayerFundingMode::CREDIT)
->whereBetween('cl.created_at', [$start, $end])
->count();
->whereBetween('cl.created_at', [$start, $end]);
$shareCount = (int) DB::table('share_ledger as sl')
if ($admin !== null) {
AdminDataScope::applyToPlayersAlias($creditQuery, $admin, 'p');
}
$shareQuery = DB::table('share_ledger as sl')
->join('players as p', 'p.id', '=', 'sl.player_id')
->where('p.site_code', $siteCode)
->whereBetween('sl.settled_at', [$start, $end])
->count();
->whereNull('sl.reversal_of_id');
if ($admin !== null) {
AdminDataScope::applyToPlayersAlias($shareQuery, $admin, 'p');
}
$shareAgg = (clone $shareQuery)
->selectRaw('COUNT(*) as share_ledger_count')
->selectRaw('COALESCE(SUM(sl.basic_rebate), 0) as basic_rebate_total')
->first();
$scopedWinLoss = $viewer['scope'] === 'platform'
? $this->scopedProfitAggregator->sumRawGameWinLoss($shareQuery)
: $this->scopedProfitAggregator->sumForShareQuery($shareQuery, $viewer['key']);
$unsettled = $this->unsettledWarning->countForSite(
(int) $period->admin_site_id,
(string) $period->period_start,
(string) $period->period_end,
);
$out[$periodId] = [
'credit_ledger_count' => $creditCount,
'share_ledger_count' => $shareCount,
'credit_ledger_count' => (int) $creditQuery->count(),
'share_ledger_count' => (int) ($shareAgg->share_ledger_count ?? 0),
'game_win_loss_total' => $scopedWinLoss,
'win_loss_scope' => $viewer['scope'],
'basic_rebate_total' => (int) ($shareAgg->basic_rebate_total ?? 0),
'unsettled_ticket_count' => $unsettled['count'],
];
}