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
if ((int) $invoice->paid_amount !== (int) $actualPaid) {
$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) {
$invoice->update([
......
......@@ -16,12 +16,12 @@ public function handle(NotificationService $notificationService): int
{
$invoices = Invoice::whereIn('status', ['overdue', 'partially_paid'])
->where('due_date', '<', now()->toDateString())
->with(['participant.person'])
->with(['billable.person'])
->get();
$sent = 0;
foreach ($invoices as $invoice) {
$participant = $invoice->participant;
$participant = $invoice->billable;
$person = $participant?->person;
if (!$person) {
......@@ -39,7 +39,7 @@ public function handle(NotificationService $notificationService): int
academyId: $invoice->academy_id,
variables: [
'participant_name' => $person->name_ar ?? $person->name ?? '',
'invoice_number' => $invoice->invoice_number ?? '',
'invoice_number' => $invoice->number ?? '',
'balance_due' => number_format($balanceDue / 100, 2),
'due_date' => $invoice->due_date?->format('Y-m-d') ?? '',
'days_overdue' => (string) $daysOverdue,
......
......@@ -46,7 +46,7 @@ public function handle(NotificationService $notificationService): int
$phone = $guardian->phone ?? $guardian->person?->phone ?? null;
if (!$phone) continue;
$message = "تقرير أسبوعي - {$participant->full_name_ar}: "
$message = "تقرير أسبوعي - {$participant->full_name}: "
. "حضر {$attendance->attended}/{$attendance->total} حصة "
. "({$rate}%) - غياب: {$attendance->absent}";
......@@ -60,7 +60,7 @@ public function handle(NotificationService $notificationService): int
);
$sent++;
} 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
recipientId: $primaryGuardian->id,
recipientType: 'guardian',
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'),
'group_name' => $event->record->session?->group?->name_ar ?? '',
],
......
......@@ -21,7 +21,7 @@ public function handle(AttendanceThresholdBreached $event): void
recipientId: $event->participant->id,
recipientType: 'participant',
data: [
'participant_name' => $event->participant->person->full_name_ar ?? '',
'participant_name' => $event->participant->person->name_ar ?? '',
'rate' => round($event->rate, 1),
'threshold' => $event->threshold,
],
......
......@@ -21,7 +21,7 @@ public function handle(InvoiceCreated $event): void
recipientId: $event->invoice->billable_id,
recipientType: $event->invoice->billable_type,
data: [
'invoice_number' => $event->invoice->invoice_number,
'invoice_number' => $event->invoice->number,
'total_amount' => $event->invoice->total_amount,
'due_date' => $event->invoice->due_date?->format('Y-m-d'),
],
......
......@@ -21,7 +21,7 @@ public function handle(InvoiceOverdue $event): void
recipientId: $event->invoice->billable_id,
recipientType: $event->invoice->billable_type,
data: [
'invoice_number' => $event->invoice->invoice_number,
'invoice_number' => $event->invoice->number,
'total_amount' => $event->invoice->total_amount,
'balance_due' => $event->invoice->due_amount,
'due_date' => $event->invoice->due_date?->format('Y-m-d'),
......
......@@ -24,7 +24,7 @@ public function handle(PaymentReceived $event): void
'payment_reference' => $event->payment->reference,
'amount' => $event->payment->amount,
'method' => $event->payment->method,
'invoice_number' => $event->payment->invoice?->invoice_number,
'invoice_number' => $event->payment->invoice?->number,
],
);
} catch (\Throwable $e) {
......
......@@ -51,7 +51,7 @@ public function handle(PaymentReceived $event): void
'method' => $methodLabel,
'date' => $paymentDate,
'receipt_number' => $payment->reference ?? '',
'invoice_number' => $payment->invoice?->invoice_number ?? '',
'invoice_number' => $payment->invoice?->number ?? '',
];
$this->notificationService->send(
......
......@@ -149,9 +149,9 @@ public function processTransaction(
'branch_id' => $branchId,
'billable_type' => $participant ? $participant->getMorphClass() : null,
'billable_id' => $participant?->id,
'invoice_number' => $this->invoiceService->generateNumber($cashSession->academy_id),
'number' => $this->invoiceService->generateNumber($cashSession->academy_id),
'total_amount' => $totalAmount,
'subtotal' => $subtotal,
'subtotal_amount' => $subtotal,
'discount_amount' => $totalDiscount,
'tax_amount' => $taxAmount,
'service_fee_amount' => $serviceFeeAmount,
......@@ -239,7 +239,7 @@ private function recordSinglePayment($invoice, string $method, int $amount, User
// Wallet deduction
if ($method === 'wallet' && $participant) {
$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([
......
......@@ -90,7 +90,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat
}
if ($fields['invoice_number'] ?? false) {
$data['invoice_number'] = $transaction->invoice?->invoice_number ?? '';
$data['invoice_number'] = $transaction->invoice?->number ?? '';
}
// Footer
......@@ -106,7 +106,7 @@ public function buildPosReceiptData(POSTransaction $transaction, ?ReceiptTemplat
*/
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;
$template = $template ?? ReceiptTemplate::resolveFor($branchId ?? 0, 'payment');
......@@ -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),
'date' => $payment->paid_at?->format($settings['date_format'] ?? 'Y/m/d') ?? '',
'time' => $payment->paid_at?->format($settings['time_format'] ?? 'H:i') ?? '',
'participant_name' => $payment->invoice?->participant?->person?->name_ar ?? '',
'invoice_number' => $payment->invoice?->invoice_number ?? '',
'participant_name' => $payment->invoice?->contact_name ?? '',
'invoice_number' => $payment->invoice?->number ?? '',
'amount' => $payment->amount,
'method' => $this->paymentMethodLabel($payment->method?->value ?? $payment->method ?? ''),
'cashier_name' => $payment->processedBy?->name_ar ?? '',
......
......@@ -30,7 +30,7 @@ public function __invoke(Request $request): JsonResponse
'financial' => [
'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'),
'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(),
],
]);
......
......@@ -49,7 +49,7 @@ public function payments(Request $request): StreamedResponse
$from = $request->get('from', now()->startOfMonth()->toDateString());
$to = $request->get('to', now()->toDateString());
$query = Payment::with(['invoice', 'createdBy'])
$query = Payment::with(['invoice', 'creator'])
->where('status', 'confirmed')
->whereBetween('payment_date', [$from, $to])
->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
......@@ -63,8 +63,8 @@ public function payments(Request $request): StreamedResponse
$p->reference ?? '',
number_format($p->amount / 100, 2),
$p->method?->value ?? '',
$p->invoice?->invoice_number ?? '',
$p->createdBy?->name ?? '',
$p->invoice?->number ?? '',
$p->creator?->name ?? '',
];
});
}
......@@ -77,9 +77,8 @@ public function invoices(Request $request): StreamedResponse
$from = $request->get('from', now()->startOfMonth()->toDateString());
$to = $request->get('to', now()->toDateString());
$query = Invoice::with(['participant.person'])
$query = Invoice::query()
->whereBetween('created_at', [$from, $to])
->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
->when($request->status, fn ($q, $s) => $q->where('status', $s))
->orderByDesc('created_at');
......@@ -87,8 +86,8 @@ public function invoices(Request $request): StreamedResponse
'رقم الفاتورة', 'المشترك', 'الإجمالي', 'المدفوع', 'المستحق', 'الحالة', 'التاريخ', 'تاريخ الاستحقاق',
], $query, function ($inv) {
return [
$inv->invoice_number ?? '',
$inv->participant?->person?->name_ar ?? '',
$inv->number ?? '',
$inv->contact_name ?? '',
number_format($inv->total_amount / 100, 2),
number_format($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