Commit b732d877 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(installments): zero remaining_balance when plan completes

Both completion paths (last installment paid + early cash settlement) now
set remaining_balance = 0.00. Fixed 14 existing completed plans in live DB.
Archive view colors remaining green when 0, red when outstanding.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 826684db
......@@ -382,6 +382,7 @@ class InstallmentController extends Controller
$db->update('installment_plans', [
'status' => 'completed',
'is_cash_settled' => 1,
'remaining_balance' => '0.00',
'updated_at' => $ts,
], '`id` = ?', [(int) $id]);
......@@ -504,7 +505,7 @@ class InstallmentController extends Controller
// Check if all paid → complete plan
$remaining = $db->selectOne("SELECT COUNT(*) as cnt FROM installment_schedule WHERE installment_plan_id = ? AND status = 'pending'", [(int) $planId]);
if ((int) ($remaining['cnt'] ?? 0) === 0) {
$db->update('installment_plans', ['status' => 'completed', 'updated_at' => date('Y-m-d H:i:s')], '`id` = ?', [(int) $planId]);
$db->update('installment_plans', ['status' => 'completed', 'remaining_balance' => '0.00', 'updated_at' => date('Y-m-d H:i:s')], '`id` = ?', [(int) $planId]);
}
EventBus::dispatch('installment.paid', ['plan_id' => (int) $planId, 'schedule_id' => (int) $scheduleId, 'member_id' => (int) $plan['member_id']]);
......
......@@ -243,7 +243,7 @@ krsort($groupedSubs);
<table style="width:100%;font-size:13px;">
<tr><td style="color:#6B7280;width:35%;">إجمالي المبلغ</td><td style="font-weight:600;"><?= money($plan['total_amount'] ?? '0') ?></td></tr>
<tr><td style="color:#6B7280;">المقدم</td><td><?= money($plan['down_payment'] ?? '0') ?></td></tr>
<tr><td style="color:#6B7280;">المتبقي</td><td style="color:<?= ($plan['status'] ?? '') === 'completed' ? '#059669' : '#DC2626' ?>;font-weight:600;"><?= ($plan['status'] ?? '') === 'completed' ? '0.00 ج.م (مكتمل)' : money($plan['remaining_balance'] ?? '0') ?></td></tr>
<tr><td style="color:#6B7280;">المتبقي</td><td style="color:<?= bccomp($plan['remaining_balance'] ?? '0', '0', 2) <= 0 ? '#059669' : '#DC2626' ?>;font-weight:600;"><?= money($plan['remaining_balance'] ?? '0') ?></td></tr>
<tr><td style="color:#6B7280;">عدد الأشهر</td><td><?= (int) ($plan['number_of_months'] ?? 0) ?></td></tr>
<tr><td style="color:#6B7280;">القسط الشهري</td><td><?= money($plan['monthly_payment'] ?? '0') ?></td></tr>
<tr><td style="color:#6B7280;">تاريخ البدء</td><td><?= e($plan['start_date'] ?? '—') ?></td></tr>
......
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