@php $student = $fee->student; $academy = $student->academy ?? \App\Models\Academy::find($fee->academy_id); $parent = $student->primaryParent ?? null; $currency = $academy->currency ?? 'Rs.'; $rno = isset($receipt) ? $receipt->receipt_number : $fee->receipt_number ?? 'N/A'; $rDate = isset($receipt) ? $receipt->paid_date : $fee->paid_date; $rAmount = isset($receipt) ? (float) $receipt->amount : (float) $fee->paid_amount; $rMethod = isset($receipt) ? $receipt->payment_method : $fee->payment_method ?? null; $rNotes = isset($receipt) ? $receipt->notes : $fee->notes; $prevPaid = isset($receipt) ? max(0, (float) $fee->paid_amount - (float) $receipt->amount) : 0; $balance = max(0, (float) $fee->amount - (float) $fee->paid_amount); // Arrears breakdown $arrears = (float) ($fee->arrears ?? 0); $baseFee = (float) $fee->amount - $arrears; $prevPeriod = $arrears > 0 ? \Carbon\Carbon::createFromDate($fee->year, $fee->month, 1)->subMonth()->format('F Y') : null; // Allocate this receipt's payment: arrears first, then current month $arrearsPaidPrev = min($prevPaid, $arrears); $remainingArrears = max(0, $arrears - $arrearsPaidPrev); $thisArrearsPaid = min($rAmount, $remainingArrears); $thisCurrentPaid = max(0, $rAmount - $thisArrearsPaid); $brandColor = $academy->brand_color ?? '{{ $brandColor }}'; $academyLogoPath = $academy->logo ? storage_path('app/public/' . $academy->logo) : null; $academyLogo64 = ($academyLogoPath && file_exists($academyLogoPath)) ? 'data:image/' . pathinfo($academyLogoPath, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($academyLogoPath)) : null; $exePath = public_path('exesync-wordmark-black.png'); $exe64 = file_exists($exePath) ? 'data:image/png;base64,' . base64_encode(file_get_contents($exePath)) : null; $dateStr = $rDate ? \Carbon\Carbon::parse($rDate)->format('d M Y') : now()->format('d M Y'); $period = \Carbon\Carbon::createFromDate($fee->year, $fee->month, 1)->format('F Y'); // QR code for member portal $qrBase64 = null; if ($student->portal_token) { try { $portalUrl = rtrim(env('FRONTEND_URL', 'https://ironcore.com'), '/') . '/portal/' . $student->portal_token; $qrSvg = \SimpleSoftwareIO\QrCode\Facades\QrCode::format('svg')->size(90)->margin(1)->generate($portalUrl); $qrBase64 = 'data:image/svg+xml;base64,' . base64_encode($qrSvg); } catch (\Exception $e) { $qrBase64 = null; } } @endphp
{{-- Header --}}
@if ($academyLogo64)
{{ $academy->name }}
@endif
{{ $academy->name }}
@if ($academy->address) {{ $academy->address }}
@endif @if ($academy->phone) {{ $academy->phone }} @endif @if ($academy->phone && $academy->email)  •  @endif @if ($academy->email) {{ $academy->email }} @endif
Receipt
{{ $rno }}
{{ $dateStr }}

{{-- Status --}}
@if ($fee->status === 'paid') Paid in Full @elseif($fee->status === 'partial') Partial Payment @else Unpaid @endif

{{-- Info --}}
Billed To
{{ $student->name }}
@if ($student->class)
Class
{{ $student->class->name }}@if ($student->class->section) — {{ $student->class->section }} @endif
@endif @if ($student->roll_number)
Roll No.
{{ $student->roll_number }}
@endif @if ($parent)
Parent / Guardian
{{ $parent->name }}
@endif
Payment Details
Fee Period
{{ $period }}
Payment Date
{{ $dateStr }}
@if ($rMethod)
Method
{{ ucfirst($rMethod) }}
@endif @if ($fee->due_date)
Due Date
{{ $fee->due_date->format('d M Y') }}
@endif

{{-- Line Items --}}
{{-- Fee line items: breakdown if arrears exist --}} @if ($arrears > 0) @else @endif {{-- Previous receipts --}} @if ($prevPaid > 0) @endif {{-- This receipt: split into arrears + current if partial across both --}} @if ($rAmount > 0) @if ($arrears > 0 && $thisArrearsPaid > 0 && $thisCurrentPaid > 0) @else @endif @endif
Description Amount ({{ $currency }})
Arrears from {{ $prevPeriod }} {{ number_format($arrears, 0) }}
Monthly Tuition Fee — {{ $period }} {{ number_format($baseFee, 0) }}
Total Due {{ number_format($fee->amount, 0) }}
Monthly Tuition Fee — {{ $period }} {{ number_format($fee->amount, 0) }}
Previously Paid - {{ number_format($prevPaid, 0) }}
Applied to Arrears @if ($rMethod)  ({{ ucfirst($rMethod) }}) @endif - {{ number_format($thisArrearsPaid, 0) }}
Applied to {{ $period }} - {{ number_format($thisCurrentPaid, 0) }}
This Payment @if ($rMethod)  ({{ ucfirst($rMethod) }}) @endif - {{ number_format($rAmount, 0) }}

{{ $balance <= 0 ? 'Paid in Full' : 'Balance Due' }}
{{ $currency }} {{ number_format($balance <= 0 ? $fee->amount : $balance, 0) }}
{{-- Notes --}} @if ($rNotes)
Note
{{ $rNotes }}
@endif {{-- Footer --}}