388 lines
16 KiB
PHP
388 lines
16 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service;
|
|
|
|
use support\think\Db;
|
|
use Throwable;
|
|
|
|
class ChannelSettlementService
|
|
{
|
|
/**
|
|
* @param array<int, string>|null $handlingFeeByAdmin admin_id => handling fee
|
|
*/
|
|
public static function settleBySuperAdmin(
|
|
int $channelId,
|
|
int $operatorAdminId,
|
|
string $remark = '',
|
|
bool $auto = false,
|
|
?array $handlingFeeByAdmin = null
|
|
): array {
|
|
$channel = Db::name('channel')->where('id', $channelId)->find();
|
|
if (!is_array($channel)) {
|
|
return ['ok' => false, 'msg' => __('Channel not found')];
|
|
}
|
|
$payload = self::buildSettlePayload($channel);
|
|
if (is_string($payload)) {
|
|
return ['ok' => false, 'msg' => $payload];
|
|
}
|
|
$defaultFee = bcadd(strval($channel['settlement_handling_fee'] ?? '0'), '0', 2);
|
|
$settlementNo = self::generateAgentSettlementNo($auto ? 'A' : 'M', $channelId, intval($payload['period_end_ts']));
|
|
if (Db::name('agent_settlement_period')->where('settlement_no', $settlementNo)->value('id')) {
|
|
return ['ok' => false, 'msg' => __('Settlement number conflict, please retry')];
|
|
}
|
|
$distributions = AdminCommissionDistributionService::distributeChannelCommission(
|
|
$channelId,
|
|
strval($payload['commission_amount']),
|
|
strval($payload['calc_base_amount']),
|
|
$defaultFee,
|
|
$handlingFeeByAdmin
|
|
);
|
|
if ($distributions === []) {
|
|
return ['ok' => false, 'msg' => __('No channel root agent configured for commission distribution')];
|
|
}
|
|
$now = time();
|
|
Db::startTrans();
|
|
try {
|
|
$periodId = intval(Db::name('agent_settlement_period')->insertGetId([
|
|
'settlement_no' => $settlementNo,
|
|
'period_start_at' => $payload['period_start_ts'],
|
|
'period_end_at' => $payload['period_end_ts'],
|
|
'total_bet_amount' => $payload['total_bet_amount'],
|
|
'total_payout_amount' => $payload['total_payout_amount'],
|
|
'platform_profit_amount' => $payload['platform_profit_amount'],
|
|
'status' => 2,
|
|
'remark' => $remark !== '' ? $remark : (($auto ? '自动' : '手动') . '渠道结算-CH' . $channelId),
|
|
'create_time' => $now,
|
|
'update_time' => $now,
|
|
]));
|
|
$rows = self::buildCommissionRowsFromDistribution(
|
|
$distributions,
|
|
$channelId,
|
|
$periodId,
|
|
$remark !== '' ? $remark : '渠道待分红记录',
|
|
$now
|
|
);
|
|
if ($rows === []) {
|
|
throw new \RuntimeException('Failed to generate commission rows');
|
|
}
|
|
foreach ($rows as $row) {
|
|
$adminId = intval($row['admin_id'] ?? 0);
|
|
$netAmount = strval($row['net_commission_amount'] ?? '0.00');
|
|
if ($adminId <= 0) {
|
|
continue;
|
|
}
|
|
unset($row['net_commission_amount']);
|
|
$row['status'] = 1;
|
|
$row['settled_at'] = $now;
|
|
$row['remark'] = strval($row['remark'] ?? '') . ' | 超管结算直接发放';
|
|
$row['update_time'] = $now;
|
|
$commissionRecordId = intval(Db::name('agent_commission_record')->insertGetId($row));
|
|
if (bccomp($netAmount, '0.00', 2) > 0) {
|
|
AdminWalletService::creditCommission(
|
|
$adminId,
|
|
$channelId,
|
|
$netAmount,
|
|
'agent_commission_record',
|
|
$commissionRecordId,
|
|
$remark !== '' ? $remark : '超管结算自动发放分红',
|
|
$operatorAdminId
|
|
);
|
|
}
|
|
}
|
|
// 已改为超管结算即发放,结算后不再保留渠道待分红余额。
|
|
Db::name('channel')->where('id', $channelId)->update([
|
|
'carryover_balance' => '0.00',
|
|
'update_time' => $now,
|
|
]);
|
|
Db::commit();
|
|
} catch (Throwable $e) {
|
|
Db::rollback();
|
|
return ['ok' => false, 'msg' => $e->getMessage()];
|
|
}
|
|
return ['ok' => true, 'payload' => $payload];
|
|
}
|
|
|
|
public static function settleDividendByChannelAdmin(int $channelId, int $operatorAdminId, string $remark = ''): array
|
|
{
|
|
return ['ok' => false, 'msg' => __('This flow pays commissions automatically after super admin settlement; channel admin does not need to settle again')];
|
|
}
|
|
|
|
public static function settleAllDueChannels(int $operatorAdminId, bool $respectCycle = true, ?array $channelIds = null): array
|
|
{
|
|
$query = Db::name('channel')->where('status', 1);
|
|
if ($channelIds !== null) {
|
|
if ($channelIds === []) {
|
|
return ['ok_count' => 0, 'failed' => []];
|
|
}
|
|
$query->whereIn('id', $channelIds);
|
|
}
|
|
$channels = $query->select()->toArray();
|
|
$ok = 0;
|
|
$failed = [];
|
|
$now = time();
|
|
foreach ($channels as $channel) {
|
|
$channelId = intval($channel['id'] ?? 0);
|
|
if ($channelId <= 0) {
|
|
continue;
|
|
}
|
|
if ($respectCycle && !self::isChannelDueForAutoSettle($channel, $now)) {
|
|
continue;
|
|
}
|
|
$res = self::settleBySuperAdmin($channelId, $operatorAdminId, '周期自动结算', true);
|
|
if (($res['ok'] ?? false) === true) {
|
|
$ok++;
|
|
continue;
|
|
}
|
|
$failed[] = [
|
|
'channel_id' => $channelId,
|
|
'msg' => strval($res['msg'] ?? '结算失败'),
|
|
];
|
|
}
|
|
return ['ok_count' => $ok, 'failed' => $failed];
|
|
}
|
|
|
|
private static function isChannelDueForAutoSettle(array $channel, int $now): bool
|
|
{
|
|
$channelId = intval($channel['id'] ?? 0);
|
|
if ($channelId <= 0) {
|
|
return false;
|
|
}
|
|
$lastEnd = self::getLastSettlementEndForChannel($channelId);
|
|
$cycle = strval($channel['settle_cycle'] ?? 'weekly');
|
|
$settleTime = strval($channel['settle_time'] ?? '02:00:00');
|
|
$today = date('Y-m-d', $now);
|
|
$targetTs = strtotime($today . ' ' . $settleTime);
|
|
if ($targetTs === false || $now < $targetTs) {
|
|
return false;
|
|
}
|
|
if ($lastEnd !== null && $lastEnd >= $targetTs) {
|
|
return false;
|
|
}
|
|
if ($cycle === 'daily') {
|
|
return true;
|
|
}
|
|
if ($cycle === 'weekly') {
|
|
$weekday = intval($channel['settle_weekday'] ?? 1);
|
|
$w = intval(date('N', $now));
|
|
return $weekday === $w;
|
|
}
|
|
if ($cycle === 'monthly') {
|
|
$monthday = intval($channel['settle_monthday'] ?? 1);
|
|
$d = intval(date('j', $now));
|
|
return $monthday === $d;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function buildSettlePayload(array $row): array|string
|
|
{
|
|
$channelId = intval($row['id'] ?? 0);
|
|
if ($channelId <= 0) {
|
|
return (string) __('Invalid channel data');
|
|
}
|
|
$endTs = time();
|
|
$lastEnd = self::getLastSettlementEndForChannel($channelId);
|
|
$channelCreateTs = intval($row['create_time'] ?? 0);
|
|
$periodStartTs = $lastEnd === null ? ($channelCreateTs > 0 ? $channelCreateTs : 0) : $lastEnd;
|
|
if ($periodStartTs >= $endTs) {
|
|
return (string) __('Invalid settlement period (start time is not earlier than now)');
|
|
}
|
|
$stats = self::aggregateBetOrderForChannel($channelId, $periodStartTs, $lastEnd !== null, $endTs);
|
|
$totalBet = $stats['total_bet'];
|
|
$totalPayout = $stats['total_payout'];
|
|
$profit = bcsub($totalBet, $totalPayout, 2);
|
|
$mode = strval($row['agent_mode'] ?? 'turnover');
|
|
$commission = self::computeCommissionAmounts($row, $totalBet, $profit, $mode);
|
|
if (is_string($commission)) {
|
|
return $commission;
|
|
}
|
|
return [
|
|
'period_start_ts' => $periodStartTs,
|
|
'period_end_ts' => $endTs,
|
|
'period_start_at' => date('Y-m-d H:i:s', $periodStartTs),
|
|
'period_end_at' => date('Y-m-d H:i:s', $endTs),
|
|
'total_bet_amount' => $totalBet,
|
|
'total_payout_amount' => $totalPayout,
|
|
'platform_profit_amount' => $profit,
|
|
'commission_rate' => $commission['commission_rate'],
|
|
'calc_base_amount' => $commission['calc_base_amount'],
|
|
'commission_amount' => $commission['commission_amount'],
|
|
'agent_mode' => $mode,
|
|
'settlement_handling_fee' => bcadd(strval($row['settlement_handling_fee'] ?? '0'), '0', 2),
|
|
'commission_split' => AdminCommissionDistributionService::buildSplitPreview(
|
|
$channelId,
|
|
$commission['commission_amount'],
|
|
$commission['calc_base_amount'],
|
|
bcadd(strval($row['settlement_handling_fee'] ?? '0'), '0', 2)
|
|
),
|
|
];
|
|
}
|
|
|
|
private static function getLastSettlementEndForChannel(int $channelId): ?int
|
|
{
|
|
$row = Db::name('agent_commission_record')->alias('acr')
|
|
->join('agent_settlement_period asp', 'acr.settlement_period_id = asp.id')
|
|
->where('acr.channel_id', $channelId)
|
|
->field('MAX(asp.period_end_at) AS m')
|
|
->find();
|
|
if (!is_array($row)) {
|
|
return null;
|
|
}
|
|
$m = $row['m'] ?? null;
|
|
if ($m === null || $m === '') {
|
|
return null;
|
|
}
|
|
return intval($m);
|
|
}
|
|
|
|
private static function aggregateBetOrderForChannel(int $channelId, int $periodStartTs, bool $hasPriorSettlement, int $endTs): array
|
|
{
|
|
$query = Db::name('bet_order')
|
|
->where('channel_id', $channelId)
|
|
->where('status', 2)
|
|
->where('create_time', '<=', $endTs);
|
|
if ($hasPriorSettlement) {
|
|
$query->where('create_time', '>', $periodStartTs);
|
|
} else {
|
|
$query->where('create_time', '>=', $periodStartTs);
|
|
}
|
|
$row = $query->field('SUM(total_amount) AS tb, SUM(win_amount) AS tw, SUM(jackpot_extra_amount) AS tj')->find();
|
|
$tb = is_array($row) && $row['tb'] !== null && $row['tb'] !== '' ? strval($row['tb']) : '0.00';
|
|
$tw = is_array($row) && $row['tw'] !== null && $row['tw'] !== '' ? strval($row['tw']) : '0.00';
|
|
$tj = is_array($row) && $row['tj'] !== null && $row['tj'] !== '' ? strval($row['tj']) : '0.00';
|
|
$totalPayout = bcadd($tw, $tj, 2);
|
|
return ['total_bet' => bcadd($tb, '0', 2), 'total_payout' => bcadd($totalPayout, '0', 2)];
|
|
}
|
|
|
|
private static function computeCommissionAmounts(array $row, string $totalBet, string $platformProfit, string $mode): array|string
|
|
{
|
|
if ($mode === 'turnover') {
|
|
$ratePercent = $row['turnover_share_rate'] ?? null;
|
|
if ($ratePercent === null || $ratePercent === '') {
|
|
return (string) __('Turnover agent commission rate is not configured');
|
|
}
|
|
$rateDec = bcdiv(strval($ratePercent), '100', 4);
|
|
return [
|
|
'commission_rate' => $rateDec,
|
|
'calc_base_amount' => $totalBet,
|
|
'commission_amount' => bcmul($totalBet, $rateDec, 2),
|
|
];
|
|
}
|
|
if ($mode === 'affiliate') {
|
|
$fee = $row['affiliate_fee_rate'] ?? null;
|
|
$rulesRaw = $row['affiliate_ladder_rules'] ?? null;
|
|
if ($fee === null || $fee === '') {
|
|
return (string) __('Affiliate agent fee rate is not configured');
|
|
}
|
|
$rules = self::normalizeLadderRulesForSettlement($rulesRaw);
|
|
if ($rules === []) {
|
|
return (string) __('Affiliate ladder rules are empty or invalid');
|
|
}
|
|
if (bccomp($platformProfit, '0', 2) <= 0) {
|
|
return ['commission_rate' => '0.0000', 'calc_base_amount' => '0.00', 'commission_amount' => '0.00'];
|
|
}
|
|
$afterFee = bcmul($platformProfit, bcsub('1', strval($fee), 4), 2);
|
|
if (bccomp($afterFee, '0', 2) <= 0) {
|
|
return ['commission_rate' => '0.0000', 'calc_base_amount' => '0.00', 'commission_amount' => '0.00'];
|
|
}
|
|
$shareRate = self::pickAffiliateShareRateFromLadder($rules, $platformProfit);
|
|
$rateDec = number_format($shareRate, 6, '.', '');
|
|
return [
|
|
'commission_rate' => $rateDec,
|
|
'calc_base_amount' => $afterFee,
|
|
'commission_amount' => bcmul($afterFee, $rateDec, 2),
|
|
];
|
|
}
|
|
return (string) __('Unknown agent mode');
|
|
}
|
|
|
|
private static function normalizeLadderRulesForSettlement(mixed $rulesRaw): array
|
|
{
|
|
if ($rulesRaw === null || $rulesRaw === '') {
|
|
return [];
|
|
}
|
|
if (is_string($rulesRaw)) {
|
|
$decoded = json_decode($rulesRaw, true);
|
|
$rulesRaw = is_array($decoded) ? $decoded : [];
|
|
}
|
|
if (!is_array($rulesRaw)) {
|
|
return [];
|
|
}
|
|
$out = [];
|
|
foreach ($rulesRaw as $rule) {
|
|
if (!is_array($rule)) {
|
|
continue;
|
|
}
|
|
$minLoss = $rule['minLoss'] ?? ($rule['min_loss'] ?? null);
|
|
$shareRate = $rule['shareRate'] ?? ($rule['share_rate'] ?? null);
|
|
if ($minLoss === null || $shareRate === null || !is_numeric(strval($minLoss)) || !is_numeric(strval($shareRate))) {
|
|
continue;
|
|
}
|
|
$out[] = [
|
|
'minLoss' => number_format(floatval($minLoss), 4, '.', ''),
|
|
'shareRate' => number_format(floatval($shareRate), 6, '.', ''),
|
|
];
|
|
}
|
|
usort($out, static function (array $a, array $b): int {
|
|
return bccomp($a['minLoss'], $b['minLoss'], 4);
|
|
});
|
|
return $out;
|
|
}
|
|
|
|
private static function pickAffiliateShareRateFromLadder(array $rules, string $playerLoss): float
|
|
{
|
|
$chosen = floatval($rules[0]['shareRate']);
|
|
foreach ($rules as $rule) {
|
|
if (bccomp($playerLoss, strval($rule['minLoss']), 2) >= 0) {
|
|
$chosen = floatval($rule['shareRate']);
|
|
}
|
|
}
|
|
return $chosen;
|
|
}
|
|
|
|
private static function generateAgentSettlementNo(string $sourceFlag, int $channelId, int $endTs): string
|
|
{
|
|
$flag = strtoupper(trim($sourceFlag));
|
|
if ($flag !== 'M' && $flag !== 'A') {
|
|
$flag = 'M';
|
|
}
|
|
$base = $flag . str_pad(strval(max(0, $channelId)), 6, '0', STR_PAD_LEFT) . str_pad(strval(max(0, $endTs)), 10, '0', STR_PAD_LEFT);
|
|
return $base . strtoupper(substr(bin2hex(random_bytes(4)), 0, 2));
|
|
}
|
|
|
|
/**
|
|
* @param array<int, array{admin_id:int,commission_amount:string,commission_rate:string,calc_base_amount:string,handling_fee:string,net_commission_amount:string}> $distributions
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private static function buildCommissionRowsFromDistribution(array $distributions, int $channelId, int $periodId, string $remark, int $now): array
|
|
{
|
|
$rows = [];
|
|
foreach ($distributions as $dist) {
|
|
$adminId = intval($dist['admin_id'] ?? 0);
|
|
if ($adminId <= 0) {
|
|
continue;
|
|
}
|
|
$amount = strval($dist['commission_amount'] ?? '0.00');
|
|
$rows[] = [
|
|
'settlement_period_id' => $periodId,
|
|
'channel_id' => $channelId,
|
|
'admin_id' => $adminId,
|
|
'commission_rate' => strval($dist['commission_rate'] ?? '0.0000'),
|
|
'calc_base_amount' => strval($dist['calc_base_amount'] ?? '0.00'),
|
|
'commission_amount' => $amount,
|
|
'handling_fee' => strval($dist['handling_fee'] ?? '0.00'),
|
|
'net_commission_amount' => strval($dist['net_commission_amount'] ?? '0.00'),
|
|
'status' => 0,
|
|
'settled_at' => null,
|
|
'remark' => $remark . ' | 树形分红实发',
|
|
'create_time' => $now,
|
|
'update_time' => $now,
|
|
];
|
|
}
|
|
return $rows;
|
|
}
|
|
}
|
|
|