@if($sale->type == 'installment') @php $installmentNo = (int) $sale->installment_no; $totalAmount = (float) $sale->total - $sale->received; $installmentAmount = $installmentNo > 0 ? ($totalAmount / $installmentNo) : 0; $startDate = \Carbon\Carbon::parse($sale->installment_date)->startOfDay(); // total paid from ledgers $totalPaid = $saleLedger->sum('amount'); $remainingAmount = $totalAmount - $totalPaid; // We'll consume ledger payments month by month $ledgerIndex = 0; $ledgerList = $saleLedger->values(); @endphp

Monthly Installment Plan

Total Amount: {{ number_format($totalAmount, 2) }}
@php $runningRemaining = $totalAmount; @endphp @for($i = 0; $i < $installmentNo; $i++) @php $dueDate = $startDate->copy()->addMonths($i); $rowDue = $installmentAmount; $paidThisMonth = 0; $paidDate = null; // ✅ Apply ledger payments into this installment row while ($ledgerIndex < $ledgerList->count() && $paidThisMonth < $rowDue) { $ledger = $ledgerList[$ledgerIndex]; $availableAmount = (float) $ledger->amount; $need = $rowDue - $paidThisMonth; $apply = min($need, $availableAmount); if ($apply > 0) { $paidThisMonth += $apply; if (!$paidDate) { $paidDate = $ledger->date; } $ledger->amount = $availableAmount - $apply; if ($ledger->amount == 0) { $ledgerIndex++; } } else { $ledgerIndex++; } } $pending = max(0, $rowDue - $paidThisMonth); $runningRemaining -= $paidThisMonth; if ($runningRemaining < 0) { $runningRemaining = 0; } @endphp @endfor
Sr Month Due Date Installment Amount Paid Date Paid Pending Remaining
{{ $i+1 }} {{ $dueDate->format('F Y') }} {{ $dueDate->format('d M Y') }} {{ number_format($rowDue, 2) }} {{ $paidDate ? \Carbon\Carbon::parse($paidDate)->format('d M Y') : '' }} {{ number_format($paidThisMonth, 2) }} {{ number_format($pending, 2) }} {{ number_format($runningRemaining, 2) }}
Total {{ number_format($totalAmount, 2) }} {{ number_format($totalPaid, 2) }} {{ number_format($totalAmount - $totalPaid, 2) }} {{ number_format($totalAmount - $totalPaid, 2) }}
@endif