Commit 4200c04a authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix platform fee calculation: use plan percentage instead of instance column

The fee collector was reading service_fee_amount from the instance's invoices
table (always 0). Now calculates fee as total_amount * plan.platform_fee_percent.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 78af7d20
...@@ -36,16 +36,20 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee ...@@ -36,16 +36,20 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee
->table('invoices') ->table('invoices')
->whereDate('created_at', $date) ->whereDate('created_at', $date)
->where('status', '!=', 'cancelled') ->where('status', '!=', 'cancelled')
->selectRaw('COUNT(*) as count, COALESCE(SUM(total_amount), 0) as total, COALESCE(SUM(service_fee_amount), 0) as fees') ->selectRaw('COUNT(*) as count, COALESCE(SUM(total_amount), 0) as total')
->first(); ->first();
DB::disconnect("instance_{$instance->id}"); DB::disconnect("instance_{$instance->id}");
$totalAmount = (int) $result->total;
$feePercent = $instance->plan->platform_fee_percent ?? 0;
$feeAmount = (int) round($totalAmount * $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' => (int) $result->total, 'total_transactions_amount' => $totalAmount,
'fee_amount' => (int) $result->fees, 'fee_amount' => $feeAmount,
'transaction_count' => (int) $result->count, 'transaction_count' => (int) $result->count,
'verified' => true, 'verified' => true,
] ]
...@@ -59,7 +63,7 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee ...@@ -59,7 +63,7 @@ public function pullFeesForDate(Instance $instance, string $date): ?PlatformFee
public function pullFeesForAllInstances(string $date): array public function pullFeesForAllInstances(string $date): array
{ {
$results = []; $results = [];
$instances = Instance::whereIn('status', ['active', 'trial'])->get(); $instances = Instance::whereIn('status', ['active', 'trial'])->with('plan')->get();
foreach ($instances as $instance) { foreach ($instances as $instance) {
$fee = $this->pullFeesForDate($instance, $date); $fee = $this->pullFeesForDate($instance, $date);
......
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