Commit 6bb5fe27 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix remaining column mismatches in listeners, commands, services, and controllers

Fixes ->invoice_number to ->number across all financial listeners, console
commands, and POS services. Fixes ->full_name_ar to ->full_name (participant
accessor) and ->name_ar (person column). Fixes POSService passing wrong keys
('invoice_number' → 'number', 'subtotal' → 'subtotal_amount') to InvoiceService.
Fixes ReceiptService using non-existent participant relationship on Invoice.
Fixes SendOverdueReminders using ->participant instead of ->billable.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 43259f2c
...@@ -28,7 +28,7 @@ public function handle(): int ...@@ -28,7 +28,7 @@ public function handle(): int
if ((int) $invoice->paid_amount !== (int) $actualPaid) { if ((int) $invoice->paid_amount !== (int) $actualPaid) {
$discrepancies++; $discrepancies++;
$this->warn("Invoice {$invoice->invoice_number}: stored={$invoice->paid_amount}, actual={$actualPaid}"); $this->warn("Invoice {$invoice->number}: stored={$invoice->paid_amount}, actual={$actualPaid}");
if ($fix) { if ($fix) {
$invoice->update([ $invoice->update([
......
...@@ -16,12 +16,12 @@ public function handle(NotificationService $notificationService): int ...@@ -16,12 +16,12 @@ public function handle(NotificationService $notificationService): int
{ {
$invoices = Invoice::whereIn('status', ['overdue', 'partially_paid']) $invoices = Invoice::whereIn('status', ['overdue', 'partially_paid'])
->where('due_date', '<', now()->toDateString()) ->where('due_date', '<', now()->toDateString())
->with(['participant.person']) ->with(['billable.person'])
->get(); ->get();
$sent = 0; $sent = 0;
foreach ($invoices as $invoice) { foreach ($invoices as $invoice) {
$participant = $invoice->participant; $participant = $invoice->billable;
$person = $participant?->person; $person = $participant?->person;
if (!$person) { if (!$person) {
...@@ -39,7 +39,7 @@ public function handle(NotificationService $notificationService): int ...@@ -39,7 +39,7 @@ public function handle(NotificationService $notificationService): int
academyId: $invoice->academy_id, academyId: $invoice->academy_id,
variables: [ variables: [
'participant_name' => $person->name_ar ?? $person->name ?? '', 'participant_name' => $person->name_ar ?? $person->name ?? '',
'invoice_number' => $invoice->invoice_number ?? '', 'invoice_number' => $invoice->number ?? '',
'balance_due' => number_format($balanceDue / 100, 2), 'balance_due' => number_format($balanceDue / 100, 2),
'due_date' => $invoice->due_date?->format('Y-m-d') ?? '', 'due_date' => $invoice->due_date?->format('Y-m-d') ?? '',
'days_overdue' => (string) $daysOverdue, 'days_overdue' => (string) $daysOverdue,
......
...@@ -46,7 +46,7 @@ public function handle(NotificationService $notificationService): int ...@@ -46,7 +46,7 @@ public function handle(NotificationService $notificationService): int
$phone = $guardian->phone ?? $guardian->person?->phone ?? null; $phone = $guardian->phone ?? $guardian->person?->phone ?? null;
if (!$phone) continue; if (!$phone) continue;
$message = "تقرير أسبوعي - {$participant->full_name_ar}: " $message = "تقرير أسبوعي - {$participant->full_name}: "
. "حضر {$attendance->attended}/{$attendance->total} حصة " . "حضر {$attendance->attended}/{$attendance->total} حصة "
. "({$rate}%) - غياب: {$attendance->absent}"; . "({$rate}%) - غياب: {$attendance->absent}";
...@@ -60,7 +60,7 @@ public function handle(NotificationService $notificationService): int ...@@ -60,7 +60,7 @@ public function handle(NotificationService $notificationService): int
); );
$sent++; $sent++;
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->warn("Failed for {$participant->full_name_ar}: {$e->getMessage()}"); $this->warn("Failed for {$participant->full_name}: {$e->getMessage()}");
} }
} }
} }
......
...@@ -33,7 +33,7 @@ public function handle(ParticipantAbsent $event): void ...@@ -33,7 +33,7 @@ public function handle(ParticipantAbsent $event): void
recipientId: $primaryGuardian->id, recipientId: $primaryGuardian->id,
recipientType: 'guardian', recipientType: 'guardian',
data: [ data: [
'participant_name' => $participant->person->full_name_ar ?? '', 'participant_name' => $participant->person->name_ar ?? '',
'session_date' => $event->record->session?->session_date?->format('Y-m-d'), 'session_date' => $event->record->session?->session_date?->format('Y-m-d'),
'group_name' => $event->record->session?->group?->name_ar ?? '', 'group_name' => $event->record->session?->group?->name_ar ?? '',
], ],
......
...@@ -21,7 +21,7 @@ public function handle(AttendanceThresholdBreached $event): void ...@@ -21,7 +21,7 @@ public function handle(AttendanceThresholdBreached $event): void
recipientId: $event->participant->id, recipientId: $event->participant->id,
recipientType: 'participant', recipientType: 'participant',
data: [ data: [
'participant_name' => $event->participant->person->full_name_ar ?? '', 'participant_name' => $event->participant->person->name_ar ?? '',
'rate' => round($event->rate, 1), 'rate' => round($event->rate, 1),
'threshold' => $event->threshold, 'threshold' => $event->threshold,
], ],
......
...@@ -21,7 +21,7 @@ public function handle(InvoiceCreated $event): void ...@@ -21,7 +21,7 @@ public function handle(InvoiceCreated $event): void
recipientId: $event->invoice->billable_id, recipientId: $event->invoice->billable_id,
recipientType: $event->invoice->billable_type, recipientType: $event->invoice->billable_type,
data: [ data: [
'invoice_number' => $event->invoice->invoice_number, 'invoice_number' => $event->invoice->number,
'total_amount' => $event->invoice->total_amount, 'total_amount' => $event->invoice->total_amount,
'due_date' => $event->invoice->due_date?->format('Y-m-d'), 'due_date' => $event->invoice->due_date?->format('Y-m-d'),
], ],
......
...@@ -21,7 +21,7 @@ public function handle(InvoiceOverdue $event): void ...@@ -21,7 +21,7 @@ public function handle(InvoiceOverdue $event): void
recipientId: $event->invoice->billable_id, recipientId: $event->invoice->billable_id,
recipientType: $event->invoice->billable_type, recipientType: $event->invoice->billable_type,
data: [ data: [
'invoice_number' => $event->invoice->invoice_number, 'invoice_number' => $event->invoice->number,
'total_amount' => $event->invoice->total_amount, 'total_amount' => $event->invoice->total_amount,
'balance_due' => $event->invoice->due_amount, 'balance_due' => $event->invoice->due_amount,
'due_date' => $event->invoice->due_date?->format('Y-m-d'), 'due_date' => $event->invoice->due_date?->format('Y-m-d'),
......
...@@ -24,7 +24,7 @@ public function handle(PaymentReceived $event): void ...@@ -24,7 +24,7 @@ public function handle(PaymentReceived $event): void
'payment_reference' => $event->payment->reference, 'payment_reference' => $event->payment->reference,
'amount' => $event->payment->amount, 'amount' => $event->payment->amount,
'method' => $event->payment->method, 'method' => $event->payment->method,
'invoice_number' => $event->payment->invoice?->invoice_number, 'invoice_number' => $event->payment->invoice?->number,
], ],
); );
} catch (\Throwable $e) { } catch (\Throwable $e) {
......
...@@ -51,7 +51,7 @@ public function handle(PaymentReceived $event): void ...@@ -51,7 +51,7 @@ public function handle(PaymentReceived $event): void
'method' => $methodLabel, 'method' => $methodLabel,
'date' => $paymentDate, 'date' => $paymentDate,
'receipt_number' => $payment->reference ?? '', 'receipt_number' => $payment->reference ?? '',
'invoice_number' => $payment->invoice?->invoice_number ?? '', 'invoice_number' => $payment->invoice?->number ?? '',
]; ];
$this->notificationService->send( $this->notificationService->send(
......
...@@ -149,9 +149,9 @@ public function processTransaction( ...@@ -149,9 +149,9 @@ public function processTransaction(
'branch_id' => $branchId, 'branch_id' => $branchId,
'billable_type' => $participant ? $participant->getMorphClass() : null, 'billable_type' => $participant ? $participant->getMorphClass() : null,
'billable_id' => $participant?->id, 'billable_id' => $participant?->id,
'invoice_number' => $this->invoiceService->generateNumber($cashSession->academy_id), 'number' => $this->invoiceService->generateNumber($cashSession->academy_id),
'total_amount' => $totalAmount, 'total_amount' => $totalAmount,
'subtotal' => $subtotal, 'subtotal_amount' => $subtotal,
'discount_amount' => $totalDiscount, 'discount_amount' => $totalDiscount,
'tax_amount' => $taxAmount, 'tax_amount' => $taxAmount,
'service_fee_amount' => $serviceFeeAmount, 'service_fee_amount' => $serviceFeeAmount,
...@@ -239,7 +239,7 @@ private function recordSinglePayment($invoice, string $method, int $amount, User ...@@ -239,7 +239,7 @@ private function recordSinglePayment($invoice, string $method, int $amount, User
// Wallet deduction // Wallet deduction
if ($method === 'wallet' && $participant) { if ($method === 'wallet' && $participant) {
$wallet = $this->walletService->getOrCreateWallet($participant, $participant->academy_id); $wallet = $this->walletService->getOrCreateWallet($participant, $participant->academy_id);
$this->walletService->withdraw($wallet, $amount, 'دفع فاتورة: ' . $invoice->invoice_number, $invoice, $cashier); $this->walletService->withdraw($wallet, $amount, 'دفع فاتورة: ' . $invoice->number, $invoice, $cashier);
} }
$this->paymentService->recordPayment([ $this->paymentService->recordPayment([
......
...@@ -90,7 +90,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat ...@@ -90,7 +90,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat
} }
if ($fields['invoice_number'] ?? false) { if ($fields['invoice_number'] ?? false) {
$data['invoice_number'] = $transaction->invoice?->invoice_number ?? ''; $data['invoice_number'] = $transaction->invoice?->number ?? '';
} }
// Footer // Footer
...@@ -106,7 +106,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat ...@@ -106,7 +106,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat
*/ */
public function buildPaymentReceiptData(Payment $payment, ?ReceiptTemplate $template = null): array public function buildPaymentReceiptData(Payment $payment, ?ReceiptTemplate $template = null): array
{ {
$payment->load(['invoice.participant.person', 'processedBy']); $payment->load(['invoice', 'processedBy']);
$branchId = $payment->branch_id ?? $payment->invoice?->branch_id; $branchId = $payment->branch_id ?? $payment->invoice?->branch_id;
$template = $template ?? ReceiptTemplate::resolveFor($branchId ?? 0, 'payment'); $template = $template ?? ReceiptTemplate::resolveFor($branchId ?? 0, 'payment');
...@@ -118,8 +118,8 @@ public function buildPaymentReceiptData(Payment $payment, ?ReceiptTemplate $temp ...@@ -118,8 +118,8 @@ public function buildPaymentReceiptData(Payment $payment, ?ReceiptTemplate $temp
'receipt_number' => $payment->reference ?? 'PAY-' . str_pad($payment->id, 6, '0', STR_PAD_LEFT), 'receipt_number' => $payment->reference ?? 'PAY-' . str_pad($payment->id, 6, '0', STR_PAD_LEFT),
'date' => $payment->paid_at?->format($settings['date_format'] ?? 'Y/m/d') ?? '', 'date' => $payment->paid_at?->format($settings['date_format'] ?? 'Y/m/d') ?? '',
'time' => $payment->paid_at?->format($settings['time_format'] ?? 'H:i') ?? '', 'time' => $payment->paid_at?->format($settings['time_format'] ?? 'H:i') ?? '',
'participant_name' => $payment->invoice?->participant?->person?->name_ar ?? '', 'participant_name' => $payment->invoice?->contact_name ?? '',
'invoice_number' => $payment->invoice?->invoice_number ?? '', 'invoice_number' => $payment->invoice?->number ?? '',
'amount' => $payment->amount, 'amount' => $payment->amount,
'method' => $this->paymentMethodLabel($payment->method?->value ?? $payment->method ?? ''), 'method' => $this->paymentMethodLabel($payment->method?->value ?? $payment->method ?? ''),
'cashier_name' => $payment->processedBy?->name_ar ?? '', 'cashier_name' => $payment->processedBy?->name_ar ?? '',
......
...@@ -30,7 +30,7 @@ public function __invoke(Request $request): JsonResponse ...@@ -30,7 +30,7 @@ public function __invoke(Request $request): JsonResponse
'financial' => [ 'financial' => [
'revenue_today' => Payment::where('status', 'confirmed')->whereDate('created_at', $today)->sum('amount'), 'revenue_today' => Payment::where('status', 'confirmed')->whereDate('created_at', $today)->sum('amount'),
'revenue_month' => Payment::where('status', 'confirmed')->where('created_at', '>=', now()->startOfMonth())->sum('amount'), 'revenue_month' => Payment::where('status', 'confirmed')->where('created_at', '>=', now()->startOfMonth())->sum('amount'),
'outstanding' => Invoice::whereIn('status', ['sent', 'partially_paid', 'overdue'])->sum('balance_due'), 'outstanding' => Invoice::whereIn('status', ['sent', 'partially_paid', 'overdue'])->sum('due_amount'),
'overdue_count' => Invoice::where('status', 'overdue')->count(), 'overdue_count' => Invoice::where('status', 'overdue')->count(),
], ],
]); ]);
......
...@@ -49,7 +49,7 @@ public function payments(Request $request): StreamedResponse ...@@ -49,7 +49,7 @@ public function payments(Request $request): StreamedResponse
$from = $request->get('from', now()->startOfMonth()->toDateString()); $from = $request->get('from', now()->startOfMonth()->toDateString());
$to = $request->get('to', now()->toDateString()); $to = $request->get('to', now()->toDateString());
$query = Payment::with(['invoice', 'createdBy']) $query = Payment::with(['invoice', 'creator'])
->where('status', 'confirmed') ->where('status', 'confirmed')
->whereBetween('payment_date', [$from, $to]) ->whereBetween('payment_date', [$from, $to])
->when($branchId, fn ($q) => $q->where('branch_id', $branchId)) ->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
...@@ -63,8 +63,8 @@ public function payments(Request $request): StreamedResponse ...@@ -63,8 +63,8 @@ public function payments(Request $request): StreamedResponse
$p->reference ?? '', $p->reference ?? '',
number_format($p->amount / 100, 2), number_format($p->amount / 100, 2),
$p->method?->value ?? '', $p->method?->value ?? '',
$p->invoice?->invoice_number ?? '', $p->invoice?->number ?? '',
$p->createdBy?->name ?? '', $p->creator?->name ?? '',
]; ];
}); });
} }
...@@ -77,9 +77,8 @@ public function invoices(Request $request): StreamedResponse ...@@ -77,9 +77,8 @@ public function invoices(Request $request): StreamedResponse
$from = $request->get('from', now()->startOfMonth()->toDateString()); $from = $request->get('from', now()->startOfMonth()->toDateString());
$to = $request->get('to', now()->toDateString()); $to = $request->get('to', now()->toDateString());
$query = Invoice::with(['participant.person']) $query = Invoice::query()
->whereBetween('created_at', [$from, $to]) ->whereBetween('created_at', [$from, $to])
->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
->when($request->status, fn ($q, $s) => $q->where('status', $s)) ->when($request->status, fn ($q, $s) => $q->where('status', $s))
->orderByDesc('created_at'); ->orderByDesc('created_at');
...@@ -87,8 +86,8 @@ public function invoices(Request $request): StreamedResponse ...@@ -87,8 +86,8 @@ public function invoices(Request $request): StreamedResponse
'رقم الفاتورة', 'المشترك', 'الإجمالي', 'المدفوع', 'المستحق', 'الحالة', 'التاريخ', 'تاريخ الاستحقاق', 'رقم الفاتورة', 'المشترك', 'الإجمالي', 'المدفوع', 'المستحق', 'الحالة', 'التاريخ', 'تاريخ الاستحقاق',
], $query, function ($inv) { ], $query, function ($inv) {
return [ return [
$inv->invoice_number ?? '', $inv->number ?? '',
$inv->participant?->person?->name_ar ?? '', $inv->contact_name ?? '',
number_format($inv->total_amount / 100, 2), number_format($inv->total_amount / 100, 2),
number_format($inv->paid_amount / 100, 2), number_format($inv->paid_amount / 100, 2),
number_format(($inv->total_amount - $inv->paid_amount) / 100, 2), number_format(($inv->total_amount - $inv->paid_amount) / 100, 2),
......
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