@extends('layout.app') @section('content')

Monthly Installment

@can('monthly-installment-history') Edit & Delete Log @endcan @can('monthly-installment-create') @endcan
@if (request('manager_id') || request('pop_id')) Clear @endif
@php // "Billed" grows on its own: amount × months elapsed since the start month. $grandPerMonth = (float) $installments->sum('amount'); $grandAmount = (float) $installments->sum('total_billable'); $grandPaid = (float) $installments->sum('payments_sum_amount'); // Sum each installment's own clamped remaining so an overpaid one can't cancel real dues on others. $grandRemaining = (float) $installments->sum('remaining_due'); @endphp {{-- The Total row lives in , above the rows: DataTables never sorts or paginates a header row, and it pins under the sticky header. --}} @foreach ($installments as $item) @php $totalPaid = (float) ($item->payments_sum_amount ?? 0); $billable = $item->total_billable; $remaining = $item->remaining_due; @endphp @endforeach
# Manager POP Start Month Status Per Month (৳) Billed Till Now (৳) Paid (৳) Due (৳) Action
Total {{ number_format($grandPerMonth, 2) }} {{ number_format($grandAmount, 2) }} {{ number_format($grandPaid, 2) }} {{ number_format($grandRemaining, 2) }}
{{ $item->id }} {{ $item->manager->name_with_type ?? '—' }} {{ $item->pop->popname ?? '—' }} {{ $item->installment_month ? $item->installment_month->format('M Y') : '—' }} @if ($item->end_month)
{{ $item->is_stopped ? 'Stopped after' : 'Ends' }} {{ $item->end_month->format('M Y') }} @else
Recurring @endif @if ($item->description)
{{ $item->description }} @endif
Added by: {{ $item->createdBy->name ?? '—' }}
@include('partials.approval-status', ['item' => $item]) @if ($item->status === 'approved') {{-- Never "Fully Paid": next month brings another instalment. --}} @if ($billable > 0 && $remaining <= 0) Up to date @endif @endif @include('partials.approval-comment', ['item' => $item]) {{ number_format($item->amount, 2) }} @if ($billable > 0) {{ number_format($billable, 2) }}
{{ $item->elapsed_periods }} × (till {{ $item->current_period_label }}) @else — @endif
@if ($totalPaid > 0) {{ number_format($totalPaid, 2) }} @else — @endif @if ($remaining > 0) {{ number_format($remaining, 2) }} @elseif ($billable > 0) Paid @else — @endif {{-- Edit stays available after approval: it is how an ongoing installment gets its End Month set to stop the monthly billing. Touching an approved record needs the extra *-approved permission. --}} @php $isApproved = $item->status === 'approved'; $canEditRow = auth()->user()->can('monthly-installment-edit') && (!$isApproved || auth()->user()->can('monthly-installment-edit-approved')); $canDeleteRow = auth()->user()->can('monthly-installment-delete') && (!$isApproved || auth()->user()->can('monthly-installment-delete-approved')); @endphp @if ($canEditRow) @endif @can('monthly-installment-approve') @include('partials.approval-decision-buttons', [ 'item' => $item, 'url' => route('monthly-installment.approve', $item->id), 'label' => 'installment', ]) @endcan @if ($canDeleteRow)
@csrf @method('DELETE')
@endif @if ($item->status === 'approved') @can('monthly-installment-payment') @if ($remaining > 0) @endif @if ($totalPaid > 0) @endif @endcan @endif @can('monthly-installment-history') History @endcan
{{-- Add Installment Modal --}} @can('monthly-installment-create') @endcan {{-- Edit Installment Modal --}} @can('monthly-installment-edit') @endcan @can('monthly-installment-payment') {{-- Pay Modal --}} {{-- Payment History Modal --}} @endcan
@endsection @section('ajax') @endsection @include('partials.finance-sticky-table') @include('partials.approve-comment-modal')