|
|
@@ -4,6 +4,7 @@ namespace App\Services;
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Schema;
|
|
|
|
|
|
class FundsSummaryService
|
|
|
{
|
|
|
@@ -16,6 +17,7 @@ class FundsSummaryService
|
|
|
$withdraw = self::withdrawAggregates($start, $end);
|
|
|
$paymentOrder = self::paymentOrderAggregates($start, $end);
|
|
|
$combinedPeople = self::combinedPeople($start, $end);
|
|
|
+ $agentFees = self::agentFeeAggregates($start, $end);
|
|
|
$commission = app(ThirdGameCommissionService::class)->summary($start?->toDateTimeString(), $end?->toDateTimeString());
|
|
|
|
|
|
$income = [
|
|
|
@@ -30,8 +32,8 @@ class FundsSummaryService
|
|
|
self::item(
|
|
|
'member_withdraw_deduction',
|
|
|
'会员出款扣除',
|
|
|
- (float)$withdraw->member_withdraw_deduction_amount
|
|
|
- + (float)$paymentOrder->manual_withdrawal_fee_amount,
|
|
|
+ // 只有 USDT 提现向会员收手续费;三方/人工提现的 fee 是平台成本,不扣会员。
|
|
|
+ $withdraw->member_withdraw_deduction_amount,
|
|
|
$combinedPeople['member_withdraw_deduction']
|
|
|
),
|
|
|
];
|
|
|
@@ -52,9 +54,10 @@ class FundsSummaryService
|
|
|
self::item('vip_discount', '给予优惠(VIP 折扣)', 0, 0),
|
|
|
self::item('rebate', '给予返点', $balance->rebate_amount, $balance->rebate_people),
|
|
|
self::item('bonus', '优惠金', $balance->bonus_amount, $balance->bonus_people),
|
|
|
+ // 下面几项手续费都是三方按通道费率向平台收的,平台承担,不扣会员/代理。
|
|
|
self::item(
|
|
|
'third_party_fee',
|
|
|
- '第三方手续费',
|
|
|
+ '第三方支付(入款)手续费',
|
|
|
$paymentOrder->third_party_fee_amount,
|
|
|
$paymentOrder->third_party_fee_people
|
|
|
),
|
|
|
@@ -70,6 +73,8 @@ class FundsSummaryService
|
|
|
$paymentOrder->auto_withdrawal_fee_amount,
|
|
|
$paymentOrder->auto_withdrawal_fee_people
|
|
|
),
|
|
|
+ self::item('agent_deposit_channel_fee', '代理三方充值手续费', $agentFees->deposit_fee, $agentFees->deposit_fee_people),
|
|
|
+ self::item('agent_payout_channel_fee', '代理三方提现手续费', $agentFees->channel_fee, $agentFees->channel_fee_people),
|
|
|
['key' => 'third_game_commission', 'label' => '第三方游戏平台抽成',
|
|
|
'amount' => $commission['amount'] === null ? null : self::commissionMoney($commission['amount']),
|
|
|
'people' => $commission['people']],
|
|
|
@@ -86,9 +91,73 @@ class FundsSummaryService
|
|
|
$commission['complete'] ? '0.00' : self::commissionMoney($commission['known_amount']), 2),
|
|
|
'third_game_net_profit' => $commission['net_profit'] === null ? null : self::commissionMoney($commission['net_profit']),
|
|
|
'third_game_commission_meta' => $commission,
|
|
|
+ 'fee_summary' => self::feeSummary($withdraw, $paymentOrder, $agentFees, $start, $end),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 手续费盈亏:收入是向客户收的手续费(会员 USDT 提现、代理提现),成本是三方按通道费率向平台收的手续费。
|
|
|
+ * 代理提现的历史订单没有记录三方成本(channel_fee 为 NULL),计入 unrecorded_count。
|
|
|
+ */
|
|
|
+ private static function feeSummary(object $withdraw, object $paymentOrder, object $agentFees, ?Carbon $start, ?Carbon $end): array
|
|
|
+ {
|
|
|
+ $income = [
|
|
|
+ self::item('member_usdt_withdraw_fee', '会员USDT提现手续费', $withdraw->member_withdraw_deduction_amount,
|
|
|
+ self::successfulWithdrawLogs($start, $end)->where('w.service_charge', '>', 0)->distinct()->count('bl.member_id')),
|
|
|
+ self::item('agent_withdraw_fee', '代理提现手续费', $agentFees->withdraw_fee, $agentFees->withdraw_fee_people),
|
|
|
+ ];
|
|
|
+ $cost = [
|
|
|
+ self::item('member_deposit_channel_fee', '会员三方充值手续费', $paymentOrder->third_party_fee_amount, $paymentOrder->third_party_fee_people),
|
|
|
+ self::item('member_payout_channel_fee', '会员三方提现手续费', $paymentOrder->auto_withdrawal_fee_amount, $paymentOrder->auto_withdrawal_fee_people),
|
|
|
+ self::item('agent_deposit_channel_fee', '代理三方充值手续费', $agentFees->deposit_fee, $agentFees->deposit_fee_people),
|
|
|
+ self::item('agent_payout_channel_fee', '代理三方提现手续费', $agentFees->channel_fee, $agentFees->channel_fee_people),
|
|
|
+ ];
|
|
|
+ $incomeTotal = self::sumItems($income);
|
|
|
+ $costTotal = self::sumItems($cost);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'income' => $income,
|
|
|
+ 'cost' => $cost,
|
|
|
+ 'income_total' => $incomeTotal,
|
|
|
+ 'cost_total' => $costTotal,
|
|
|
+ 'net' => bcsub($incomeTotal, $costTotal, 2),
|
|
|
+ 'unrecorded_agent_payout_count' => (int)$agentFees->unrecorded,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 代理侧手续费:代理在线充值的三方手续费(平台成本)、代理提现向代理收的手续费(收入)和三方代付成本(平台成本)。
|
|
|
+ * 代理提现的历史订单没有记录三方成本(channel_fee 为 NULL),计入 unrecorded。
|
|
|
+ */
|
|
|
+ private static function agentFeeAggregates(?Carbon $start, ?Carbon $end): object
|
|
|
+ {
|
|
|
+ $result = ['deposit_fee' => 0, 'deposit_fee_people' => 0, 'withdraw_fee' => 0, 'withdraw_fee_people' => 0,
|
|
|
+ 'channel_fee' => 0, 'channel_fee_people' => 0, 'unrecorded' => 0];
|
|
|
+ if (Schema::hasTable('agent_payment_orders')) {
|
|
|
+ $deposit = self::applyDateRange(DB::table('agent_payment_orders')
|
|
|
+ ->where('type', PaymentOrderService::TYPE_PAY)
|
|
|
+ ->where('status', PaymentOrderService::STATUS_SUCCESS)
|
|
|
+ ->where('fee', '>', 0), 'created_at', $start, $end)
|
|
|
+ ->selectRaw('COALESCE(SUM(fee), 0) AS deposit_fee, COUNT(DISTINCT agent_id) AS deposit_fee_people')
|
|
|
+ ->first();
|
|
|
+ $result = array_merge($result, (array)$deposit);
|
|
|
+ }
|
|
|
+ if (Schema::hasTable('agent_withdrawals') && Schema::hasColumn('agent_withdrawals', 'channel_fee')) {
|
|
|
+ $withdraw = self::applyDateRange(DB::table('agent_withdrawals')->where('status', 'paid'), 'paid_at', $start, $end)
|
|
|
+ ->selectRaw(<<<'SQL'
|
|
|
+ COALESCE(SUM(fee), 0) AS withdraw_fee,
|
|
|
+ COUNT(DISTINCT CASE WHEN fee > 0 THEN agent_id END) AS withdraw_fee_people,
|
|
|
+ COALESCE(SUM(channel_fee), 0) AS channel_fee,
|
|
|
+ COUNT(DISTINCT CASE WHEN channel_fee > 0 THEN agent_id END) AS channel_fee_people,
|
|
|
+ COUNT(CASE WHEN channel_fee IS NULL THEN 1 END) AS unrecorded
|
|
|
+ SQL)
|
|
|
+ ->first();
|
|
|
+ $result = array_merge($result, (array)$withdraw);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (object)$result;
|
|
|
+ }
|
|
|
+
|
|
|
private static function balanceAggregates(?Carbon $start, ?Carbon $end): object
|
|
|
{
|
|
|
$query = DB::table('balance_logs')
|
|
|
@@ -187,9 +256,7 @@ class FundsSummaryService
|
|
|
COUNT(DISTINCT CASE WHEN type = {$payout} AND fee > 0 THEN member_id END)
|
|
|
AS auto_withdrawal_fee_people,
|
|
|
COALESCE(SUM(CASE WHEN type = {$manualPayout} THEN amount ELSE 0 END), 0)
|
|
|
- AS manual_withdrawal_amount,
|
|
|
- COALESCE(SUM(CASE WHEN type = {$manualPayout} AND fee > 0 THEN fee ELSE 0 END), 0)
|
|
|
- AS manual_withdrawal_fee_amount
|
|
|
+ AS manual_withdrawal_amount
|
|
|
SQL)
|
|
|
->first();
|
|
|
}
|
|
|
@@ -205,12 +272,6 @@ class FundsSummaryService
|
|
|
->where('w.service_charge', '>', 0)
|
|
|
->selectRaw("'member_withdraw_deduction' AS category")
|
|
|
->addSelect('bl.member_id as member_id');
|
|
|
- $manualPayoutFees = self::applyDateRange(DB::table('payment_orders as po')
|
|
|
- ->where('po.type', PaymentOrderService::TYPE_SELF_PAYOUT)
|
|
|
- ->where('po.status', PaymentOrderService::STATUS_SUCCESS)
|
|
|
- ->where('po.fee', '>', 0), 'po.created_at', $start, $end)
|
|
|
- ->selectRaw("'member_withdraw_deduction' AS category")
|
|
|
- ->addSelect('po.member_id as member_id');
|
|
|
$manualDebits = self::applyDateRange(DB::table('balance_logs as bl')
|
|
|
->where('bl.change_type', '人工扣款')
|
|
|
->where('bl.amount', '<', 0), 'bl.created_at', $start, $end)
|
|
|
@@ -223,7 +284,6 @@ class FundsSummaryService
|
|
|
->addSelect('po.member_id as member_id');
|
|
|
|
|
|
$union = $withdrawFees
|
|
|
- ->unionAll($manualPayoutFees)
|
|
|
->unionAll($manualDebits)
|
|
|
->unionAll($manualPayouts);
|
|
|
$rows = DB::query()
|