Commit 944c5001 authored by Claude's avatar Claude

Scope financial overview expenses and P&L to the active branch

The financial overview filtered revenue by branch but not expenses, so
every branch showed the same expense figure. The "مدفوعات أخرى" line was
academy-wide outbound payments — 32,510 EGP of customer refunds issued
this month, all belonging to Zayed — displayed identically under all 7
branches. Purchase orders had the same problem, and in the 6-month P&L
chart both income and outbound were unfiltered.

Also: PaymentService::refund() created the outbound payment without
copying branch_id from the payment being refunded, so refunds taken
through that path landed in no branch at all and were invisible in every
branch view. RefundService already did this correctly.
Co-Authored-By: 's avatarClaude Opus 5 <noreply@anthropic.com>
parent aeab7b7d
......@@ -61,6 +61,9 @@ public function refund(Payment $payment, int $amount, User $creator): Payment
return DB::transaction(function () use ($payment, $amount, $creator) {
$refund = Payment::create([
'academy_id' => $payment->academy_id,
// Inherit the branch of the payment being refunded, otherwise the
// refund lands in no branch and disappears from every branch view.
'branch_id' => $payment->branch_id,
'invoice_id' => $payment->invoice_id,
'reference' => 'REF-' . $payment->reference,
'direction' => 'outbound',
......
......@@ -170,10 +170,12 @@ private function getExpenses($from, $to): array
$outboundPayments = Payment::where('direction', 'outbound')
->where('status', 'confirmed')
->whereBetween('payment_date', [$from, $to])
->when($this->branch_id, fn ($q) => $q->where('branch_id', $this->branch_id))
->sum('amount');
$purchaseOrders = PurchaseOrder::whereIn('status', ['confirmed', 'received', 'partially_received'])
->whereBetween('created_at', [$from, $to])
->when($this->branch_id, fn ($q) => $q->where('branch_id', $this->branch_id))
->sum('total_amount');
$facilityCostsThisPeriod = $this->calculateProRatedFacilityCost($from, $to);
......@@ -242,15 +244,18 @@ private function getMonthlyPL(): array
$income = Payment::where('direction', 'inbound')
->where('status', 'confirmed')
->whereBetween('payment_date', [$start, $end])
->when($this->branch_id, fn ($q) => $q->where('branch_id', $this->branch_id))
->sum('amount');
$outbound = Payment::where('direction', 'outbound')
->where('status', 'confirmed')
->whereBetween('payment_date', [$start, $end])
->when($this->branch_id, fn ($q) => $q->where('branch_id', $this->branch_id))
->sum('amount');
$purchases = PurchaseOrder::whereIn('status', ['confirmed', 'received', 'partially_received'])
->whereBetween('created_at', [$start, $end])
->when($this->branch_id, fn ($q) => $q->where('branch_id', $this->branch_id))
->sum('total_amount');
$facilityQuery = Facility::whereNotNull('monthly_rental_cost')->where('monthly_rental_cost', '>', 0);
......
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