Commit da344cda authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix: renewal invoices now attributed to enrollment creator, not random User::first()

The old code tried to find 'system@oc-sport.com' then fell back to
User::first() which silently attributed 222 invoices to a random
Head Trainer. Now uses enrollment.created_by as the actor — if that
user no longer exists, the invoice is skipped with an error log.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 5fff9330
...@@ -73,8 +73,15 @@ public function handle(InvoiceService $invoiceService, PricingService $pricingSe ...@@ -73,8 +73,15 @@ public function handle(InvoiceService $invoiceService, PricingService $pricingSe
continue; continue;
} }
$systemUser = User::where('email', 'system@oc-sport.com')->first() $actor = User::find($enrollment->created_by);
?? User::first(); if (!$actor) {
Log::error('Renewal invoice skipped — enrollment has no valid created_by user', [
'enrollment_id' => $enrollment->id,
'created_by' => $enrollment->created_by,
]);
$failed++;
continue;
}
$invoice = $invoiceService->create([ $invoice = $invoiceService->create([
'academy_id' => $enrollment->academy_id ?? $program->academy_id, 'academy_id' => $enrollment->academy_id ?? $program->academy_id,
...@@ -97,7 +104,7 @@ public function handle(InvoiceService $invoiceService, PricingService $pricingSe ...@@ -97,7 +104,7 @@ public function handle(InvoiceService $invoiceService, PricingService $pricingSe
'discount_amount' => $priceResult->totalDiscount, 'discount_amount' => $priceResult->totalDiscount,
'tax_amount' => 0, 'tax_amount' => 0,
], ],
], $systemUser); ], $actor);
// Mark as sent immediately so it appears in payment wizard // Mark as sent immediately so it appears in payment wizard
$invoice->update(['status' => \App\Domain\Financial\Enums\InvoiceStatus::Sent]); $invoice->update(['status' => \App\Domain\Financial\Enums\InvoiceStatus::Sent]);
......
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