@extends('dashboard.layouts.app') @section('content') @include('components.alert') @php $student = $studentInvoice->student; $isSalesOrderInvoice = str_starts_with((string) $studentInvoice->id, 'sales-'); $studentName = $student ? trim(($student->full_name ?? '') ?: (($student->first_name ?? '') . ' ' . ($student->last_name ?? ''))) : ($studentInvoice->party_name ?? 'Sales Customer'); $courseItems = $studentInvoice->courseStudentInvoices ?? collect(); $transactions = $studentInvoice->transactions ?? collect(); $latestTransactionId = $transactions->max('id'); $paymentAllocations = ($studentInvoice->paymentAllocations ?? collect()) ->filter(fn ($allocation) => $allocation->payment) ->sortByDesc(fn ($allocation) => optional($allocation->payment->payment_date)->timestamp ?? $allocation->payment->id); $paymentStatusClass = match ($studentInvoice->payment_status) { 'paid', 'paid_purchased' => 'status-pill--success', 'paid_deposite', 'deposite', 'partial' => 'status-pill--warning', 'rejected', 'refunded', 'unpaid', 'not_paid' => 'status-pill--danger', default => 'status-pill--muted', }; $completionStatusClass = !$studentInvoice->display_completion_status ? 'status-pill--muted' : ($studentInvoice->display_completion_status === 'complete' ? 'status-pill--success' : 'status-pill--warning'); $displayLastSessionAt = $studentInvoice->display_last_session_at; $totalPrice = (float) ($studentInvoice->total_price ?? 0); $netPrice = (float) ($studentInvoice->display_total_amount ?? ($studentInvoice->net_price ?? $totalPrice)); $paidAmount = (float) ($studentInvoice->display_paid_amount ?? 0); $receivedAmounts = app(\App\Services\InvoiceReceiptPresentationService::class)->groups($studentInvoice); $depositAmount = (float) ($studentInvoice->display_deposit_amount ?? 0); $remainingAmount = (float) ($studentInvoice->display_remaining_amount ?? max($netPrice - $paidAmount, 0)); $discountAmount = max($totalPrice - $netPrice, 0); $vatAmount = (float) ($studentInvoice->vat_amount ?? 0); if ($vatAmount <= 0) { $vatAmount = max($netPrice - $totalPrice + $discountAmount, 0); } $vatRate = (float) ($studentInvoice->vat_rate ?? 0); $paidPercent = $netPrice > 0 ? min(100, ($paidAmount / $netPrice) * 100) : 0; $receiptUrl = function (?string $path): ?string { if (blank($path)) { return null; } if (str_starts_with($path, 'http')) { return $path; } $path = ltrim($path, '/'); return str_starts_with($path, 'storage/') || str_starts_with($path, 'Uploads/') || str_starts_with($path, 'uploads/') ? asset($path) : asset('storage/' . $path); }; @endphp
@unless($isSalesOrderInvoice) @endunless
Invoice #{{ $studentInvoice->invoice_number }}

Created {{ optional($studentInvoice->created_at)->format('M d, Y') ?: '--' }}

{{ $studentInvoice->display_payment_status_label ?? ucfirst(str_replace('_', ' ', $studentInvoice->payment_status ?? 'not_paid')) }} {{ $studentInvoice->display_completion_status_label }}
Student {{ $studentName ?: '--' }}
Student ID {{ $studentInvoice->student_id ?? '--' }}
Invoice Type {{ ucfirst($studentInvoice->type ?? '--') }}
Remaining {{ number_format($remainingAmount, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
Deposit {{ $depositAmount > 0 ? number_format($depositAmount, 2) : '--' }}
Last Session {{ $displayLastSessionAt ? $displayLastSessionAt->format('M d, Y h:i A') : '--' }}
Payment

Create a full or partial payment from this invoice.

{{ $remainingAmount > 0 ? 'Open' : 'Fully Paid' }}
Paid: @include('dashboard.student_invoices._received_amounts', ['receiptGroups' => $receivedAmounts, 'receiptFallbackAmount' => $paidAmount, 'receiptFallbackCurrency' => $studentInvoice->currency ?: 'EGP']) Remaining: {{ number_format($remainingAmount, 2) }} {{ $studentInvoice->currency ?: 'EGP' }} Net: {{ number_format($netPrice, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
@if($remainingAmount > 0 && !$isSalesOrderInvoice)
@elseif($remainingAmount > 0 && $canPayViaPurchaseFlow && $purchaseInvoiceId) {{-- Staff / company sales-order invoice — settle via purchase invoice payment flow --}}
@csrf
@error('payment_date')
{{ $message }}
@enderror
@error('treasury_id')
{{ $message }}
@enderror
@error('amount')
{{ $message }}
@enderror
@error('notes')
{{ $message }}
@enderror
@elseif($remainingAmount <= 0)
This invoice is fully paid.
@else
Use the sales payment flow to collect this invoice.
@endif
Summary
Original total {{ number_format($totalPrice, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
Discount {{ number_format($discountAmount, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
@if($vatAmount > 0)
VAT{{ $vatRate > 0 ? ' (' . number_format($vatRate, 2) . '%)' : '' }} {{ number_format($vatAmount, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
@endif
Paid @include('dashboard.student_invoices._received_amounts', ['receiptGroups' => $receivedAmounts, 'receiptFallbackAmount' => $paidAmount, 'receiptFallbackCurrency' => $studentInvoice->currency ?: 'EGP'])
@if($depositAmount > 0)
Deposit @include('dashboard.student_invoices._received_amounts', ['receiptGroups' => $receivedAmounts, 'receiptFallbackAmount' => $depositAmount, 'receiptFallbackCurrency' => $studentInvoice->currency ?: 'EGP'])
@endif
{{ $remainingAmount <= 0 && $receivedAmounts ? 'Paid total' : 'Invoice total' }} @if($remainingAmount <= 0 && $receivedAmounts) @include('dashboard.student_invoices._received_amounts', ['receiptGroups' => $receivedAmounts]) @else {{ number_format($netPrice, 2) }} {{ $studentInvoice->currency ?: 'EGP' }} @endif
Remaining {{ number_format($remainingAmount, 2) }} {{ $studentInvoice->currency ?: 'EGP' }}
Payments

{{ $paymentAllocations->count() }} payment(s) allocated to this invoice.

@forelse ($paymentAllocations as $allocation) @php $payment = $allocation->payment; $paymentStatusClass = match ($payment->status ?? null) { 'posted' => 'status-pill--success', 'reversed', 'voided' => 'status-pill--danger', 'pending' => 'status-pill--warning', default => 'status-pill--muted', }; [$allocatedAmount, $allocatedCurrency] = app(\App\Services\InvoiceReceiptPresentationService::class)->allocationAmount($allocation, $studentInvoice->currency ?: 'EGP'); @endphp @empty @endforelse
# Payment No Date Status Method Allocated Receipt From To Action
{{ $loop->iteration }} {{ $payment->payment_no ?: ('#' . $payment->id) }} {{ optional($payment->payment_date)->format('M d, Y') ?: '--' }} {{ ucfirst($payment->status ?? 'draft') }} {{ ucwords(str_replace('_', ' ', $payment->payment_method ?? '--')) }} {{ number_format($allocatedAmount, 2) }} {{ $allocatedCurrency }} @if (!empty($payment->receipt_image)) View @else -- @endif {{ $payment->fromAccount ? ($payment->fromAccount->code . ' - ' . $payment->fromAccount->title) : '--' }} {{ $payment->toAccount ? ($payment->toAccount->code . ' - ' . $payment->toAccount->title) : '--' }} View
No payments allocated to this invoice yet.
Courses

{{ $courseItems->count() }} course item(s) linked to this invoice.

@forelse ($courseItems as $item) @php $course = $item->course; $courseTitle = $course?->course_name_en ?: ($course?->track?->name_en ?? null) ?: ($course?->academyProduct?->name_en ?? 'N/A'); $teacherName = $course?->staff ? trim(($course->staff->first_name ?? '') . ' ' . ($course->staff->last_name ?? '')) : '--'; @endphp @empty @endforelse
# Course Booking ID Course ID Start End Teacher
{{ $loop->iteration }} {{ $courseTitle }} {{ $item->id }} {{ $item->course_id }} {{ $course?->start_date ?? '--' }} {{ $course?->end_date ?? '--' }} {{ $teacherName ?: '--' }}
No courses linked to this invoice.
Transactions

{{ $transactions->count() }} transaction(s).

@forelse ($transactions as $transaction) @php $transactionReceiptPath = $transaction->transaction_img ?? $transaction->receipt_image ?? $transaction->payment_receipt ?? $transaction->receipt ?? $transaction->image ?? $transaction->invoice_img ?? null; $transactionReceiptUrl = $receiptUrl($transactionReceiptPath); @endphp @empty @endforelse
# Amount Payment Type Status Date Receipt Actions
{{ $loop->iteration }} {{ number_format($transaction->treasury_amount ?? $transaction->amount ?? 0, 2) }} {{ $transaction->treasury_currency ?: $transaction->currency }} {{ ucfirst(str_replace('_', ' ', $transaction->payment_type ?? '--')) }} {{ ucfirst($transaction->status ?? 'pending') }} {{ optional($transaction->created_at)->format('M d, Y') ?: '--' }} @if ($transactionReceiptUrl) @else -- @endif @if ($transaction->id === $latestTransactionId && $transaction->status === 'pending' && $transaction->payment_type === 'instapay')
@csrf @method('PATCH')
@else -- @endif
No transactions found.
@foreach ($transactions as $transaction) @php $transactionReceiptPath = $transaction->transaction_img ?? $transaction->receipt_image ?? $transaction->payment_receipt ?? $transaction->receipt ?? $transaction->image ?? $transaction->invoice_img ?? null; $transactionReceiptUrl = $receiptUrl($transactionReceiptPath); @endphp @if ($transactionReceiptUrl) @endif @if ($transaction->id === $latestTransactionId && $transaction->status === 'pending' && $transaction->payment_type === 'instapay') @endif @endforeach
@endsection