Commit ccd548f7 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fee on gross income only: all confirmed inbound payments, no refund deduction

Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent c90ed983
...@@ -37,28 +37,23 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee ...@@ -37,28 +37,23 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee
$result = $conn->table('payments') $result = $conn->table('payments')
->whereNull('deleted_at') ->whereNull('deleted_at')
->where('direction', 'inbound') ->where('direction', 'inbound')
->where('status', 'confirmed')
->whereDate('payment_date', $date) ->whereDate('payment_date', $date)
->selectRaw(" ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total')
COUNT(*) FILTER (WHERE status = 'confirmed') as inbound_count,
COALESCE(SUM(amount) FILTER (WHERE status = 'confirmed'), 0) as inbound_total,
COALESCE(SUM(amount) FILTER (WHERE status = 'refunded'), 0) as refunded_total
")
->first(); ->first();
DB::disconnect("instance_{$instance->id}"); DB::disconnect("instance_{$instance->id}");
$grossIncome = (int) $result->inbound_total; $grossIncome = (int) $result->total;
$refunds = (int) $result->refunded_total;
$feeableAmount = max(0, $grossIncome - $refunds);
$feePercent = $instance->plan->platform_fee_percent ?? 0; $feePercent = $instance->plan->platform_fee_percent ?? 0;
$feeAmount = (int) round($feeableAmount * $feePercent / 100); $feeAmount = (int) round($grossIncome * $feePercent / 100);
return PlatformFee::updateOrCreate( return PlatformFee::updateOrCreate(
['instance_id' => $instance->id, 'date' => $date, 'source' => 'pull'], ['instance_id' => $instance->id, 'date' => $date, 'source' => 'pull'],
[ [
'total_transactions_amount' => $feeableAmount, 'total_transactions_amount' => $grossIncome,
'fee_amount' => $feeAmount, 'fee_amount' => $feeAmount,
'transaction_count' => (int) $result->inbound_count, 'transaction_count' => (int) $result->count,
'verified' => true, 'verified' => true,
] ]
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment