Commit 5c0fe31e authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix 3 broken reports: daily revenue group-by, cash sessions columns, revenue by activity

1. Daily revenue: group by date only (removed method split per manager request)
2. Cash sessions: fix column names (cash_in→total_cash_in, cash_out→total_cash_out) and null-safe arithmetic to prevent string/int type error
3. Revenue by activity: fix column name (line_total→total_amount) and replace broken whereHasMorph with proper join for branch filter
4. Money formatting: cast to int before division to prevent type error on empty values
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 7fd089ad
...@@ -33,10 +33,9 @@ public function dailyRevenue(string $from, string $to, ?int $branchId = null): C ...@@ -33,10 +33,9 @@ public function dailyRevenue(string $from, string $to, ?int $branchId = null): C
->select( ->select(
DB::raw("DATE(payment_date) as date"), DB::raw("DATE(payment_date) as date"),
DB::raw("SUM(amount) as total"), DB::raw("SUM(amount) as total"),
DB::raw("COUNT(*) as count"), DB::raw("COUNT(*) as count")
'method'
) )
->groupBy('date', 'method') ->groupBy('date')
->orderBy('date') ->orderBy('date')
->get(); ->get();
} }
...@@ -125,12 +124,12 @@ public function cashSessionSummary(string $from, string $to, ?int $branchId = nu ...@@ -125,12 +124,12 @@ public function cashSessionSummary(string $from, string $to, ?int $branchId = nu
'branch' => $cs->branch?->name_ar ?? '', 'branch' => $cs->branch?->name_ar ?? '',
'opened_at' => $cs->opened_at?->format('Y-m-d H:i'), 'opened_at' => $cs->opened_at?->format('Y-m-d H:i'),
'closed_at' => $cs->closed_at?->format('Y-m-d H:i') ?? '—', 'closed_at' => $cs->closed_at?->format('Y-m-d H:i') ?? '—',
'opening_balance' => $cs->opening_balance, 'opening_balance' => $cs->opening_balance ?? 0,
'cash_in' => $cs->cash_in, 'cash_in' => $cs->total_cash_in ?? 0,
'cash_out' => $cs->cash_out, 'cash_out' => $cs->total_cash_out ?? 0,
'expected_balance' => $cs->opening_balance + $cs->cash_in - $cs->cash_out, 'expected_balance' => ($cs->opening_balance ?? 0) + ($cs->total_cash_in ?? 0) - ($cs->total_cash_out ?? 0),
'actual_balance' => $cs->closing_balance, 'actual_balance' => $cs->closing_balance ?? 0,
'variance' => ($cs->closing_balance ?? 0) - ($cs->opening_balance + $cs->cash_in - $cs->cash_out), 'variance' => $cs->variance ?? (($cs->closing_balance ?? 0) - (($cs->opening_balance ?? 0) + ($cs->total_cash_in ?? 0) - ($cs->total_cash_out ?? 0))),
'status' => $cs->status ?? ($cs->closed_at ? 'closed' : 'open'), 'status' => $cs->status ?? ($cs->closed_at ? 'closed' : 'open'),
]); ]);
} }
...@@ -620,10 +619,13 @@ public function revenueByActivity(string $from, string $to, ?int $branchId = nul ...@@ -620,10 +619,13 @@ public function revenueByActivity(string $from, string $to, ?int $branchId = nul
->where('invoice_items.itemable_type', 'App\\Domain\\Training\\Models\\TrainingProgram') ->where('invoice_items.itemable_type', 'App\\Domain\\Training\\Models\\TrainingProgram')
->whereIn('invoices.status', ['paid', 'partially_paid']) ->whereIn('invoices.status', ['paid', 'partially_paid'])
->whereBetween('invoices.created_at', [$from, $to . ' 23:59:59']) ->whereBetween('invoices.created_at', [$from, $to . ' 23:59:59'])
->when($branchId, fn ($q) => $q->whereHasMorph('invoices.billable', [Participant::class], fn ($p) => $p->where('branch_id', $branchId))) ->when($branchId, fn ($q) => $q->join('participants', function ($j) {
$j->on('invoices.billable_id', '=', 'participants.id')
->where('invoices.billable_type', 'App\\Domain\\Participant\\Models\\Participant');
})->where('participants.branch_id', $branchId))
->select( ->select(
'activities.name_ar as activity', 'activities.name_ar as activity',
DB::raw('SUM(invoice_items.line_total) as revenue'), DB::raw('SUM(invoice_items.total_amount) as revenue'),
DB::raw('COUNT(DISTINCT invoices.id) as invoices_count') DB::raw('COUNT(DISTINCT invoices.id) as invoices_count')
) )
->groupBy('activities.id', 'activities.name_ar') ->groupBy('activities.id', 'activities.name_ar')
......
...@@ -42,8 +42,8 @@ public function getReportConfig(): array ...@@ -42,8 +42,8 @@ public function getReportConfig(): array
'daily_revenue' => [ 'daily_revenue' => [
'name' => 'الإيرادات اليومية', 'name' => 'الإيرادات اليومية',
'method' => 'dailyRevenue', 'method' => 'dailyRevenue',
'headers' => ['التاريخ', 'طريقة الدفع', 'المبلغ', 'عدد العمليات'], 'headers' => ['التاريخ', 'المبلغ', 'عدد العمليات'],
'columns' => ['date', 'method', 'total', 'count'], 'columns' => ['date', 'total', 'count'],
'money_cols' => ['total'], 'money_cols' => ['total'],
'uses_dates' => true, 'uses_dates' => true,
], ],
......
...@@ -105,7 +105,7 @@ class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:r ...@@ -105,7 +105,7 @@ class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:r
]; ];
@endphp @endphp
@if($isMoney) @if($isMoney)
<span dir="ltr">{{ number_format($value / 100, 2) }}</span> <span dir="ltr">{{ number_format((int) $value / 100, 2) }}</span>
@elseif(in_array($col, ['status', 'gender', 'method', 'payment_method']) && is_string($value) && isset($statusLabels[$value])) @elseif(in_array($col, ['status', 'gender', 'method', 'payment_method']) && is_string($value) && isset($statusLabels[$value]))
{{ $statusLabels[$value] }} {{ $statusLabels[$value] }}
@else @else
......
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