Commit 96a03740 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix: Collection rate respects branch filter

Without branch filtering, the collection rate showed global stats even
when viewing a specific branch. Now filters invoices by branch via
their associated payments.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent bf710121
...@@ -278,11 +278,19 @@ private function getRevenueBySource($from, $to): array ...@@ -278,11 +278,19 @@ private function getRevenueBySource($from, $to): array
private function getCollectionRate($from, $to): array private function getCollectionRate($from, $to): array
{ {
$invoiced = Invoice::whereBetween('issue_date', [$from, $to])->sum('total_amount'); $invoicedQuery = Invoice::whereBetween('issue_date', [$from, $to]);
$collected = Invoice::whereBetween('issue_date', [$from, $to])->sum('paid_amount'); $overdueQuery = Invoice::where('status', 'overdue');
$overdue = Invoice::where('status', 'overdue')->sum('due_amount'); if ($this->branch_id) {
$overdueCount = Invoice::where('status', 'overdue')->count(); $invoicedQuery->whereHas('payments', fn ($q) => $q->where('branch_id', $this->branch_id));
$overdueQuery->whereHas('payments', fn ($q) => $q->where('branch_id', $this->branch_id));
}
$invoiced = (clone $invoicedQuery)->sum('total_amount');
$collected = (clone $invoicedQuery)->sum('paid_amount');
$overdue = (clone $overdueQuery)->sum('due_amount');
$overdueCount = (clone $overdueQuery)->count();
return [ return [
'invoiced' => $invoiced, 'invoiced' => $invoiced,
......
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