@@ -141,52 +141,45 @@ class MemberController extends Controller
$qualification=$member->qualification_id?$db->selectOne("SELECT name_ar FROM qualifications WHERE id = ?",[(int)$member->qualification_id]):null;
$spouses=[];$children=[];$temporaries=[];
try{$spouses=$db->select("SELECT * FROM spouses WHERE member_id = ? AND is_archived = 0 ORDER BY spouse_order",[(int)$id]);}catch(\Throwable$e){}
try{$children=$db->select("SELECT * FROM children WHERE member_id = ? AND is_archived = 0 ORDER BY child_order",[(int)$id]);}catch(\Throwable$e){}
try{$temporaries=$db->select("SELECT * FROM temporary_members WHERE member_id = ? AND is_archived = 0 ORDER BY id",[(int)$id]);}catch(\Throwable$e){}
// Self-healing: fix dependents stuck at pending_payment after membership was paid
if($member->status==='active'){
$membershipPaid=$db->selectOne(
"SELECT id FROM payments WHERE member_id = ? AND payment_type IN ('membership_fee','down_payment') AND is_voided = 0 LIMIT 1",
[(int)$id]
try{
$spouses=$db->select(
"SELECT s.*, pr.created_at AS due_date, p.payment_date
FROM spouses s
LEFT JOIN payment_requests pr ON pr.related_entity_type = 'spouses' AND pr.related_entity_id = s.id AND pr.payment_type = 'addition_fee' AND pr.is_voided = 0
LEFT JOIN payments p ON p.id = s.activated_by_payment_id AND p.is_voided = 0
"SELECT id FROM `{$tbl}` WHERE member_id = ? AND status = 'pending_payment' AND is_archived = 0",
[(int)$id]
);
foreach($stuckas$dep){
$hasPending=$db->selectOne(
"SELECT id FROM payment_requests WHERE member_id = ? AND payment_type = 'addition_fee' AND related_entity_type = ? AND related_entity_id = ? AND status IN ('pending','processing') AND is_voided = 0 LIMIT 1",
[(int)$id,$tbl,(int)$dep['id']]
);
$hasCompletedPayment=$db->selectOne(
"SELECT id FROM payment_requests WHERE member_id = ? AND payment_type = 'addition_fee' AND related_entity_type = ? AND related_entity_id = ? AND status = 'completed' AND is_voided = 0 LIMIT 1",
"SELECT id FROM payment_requests WHERE member_id = ? AND payment_type = 'addition_fee' AND related_entity_type = ? AND related_entity_id = ? AND status = 'cancelled' ORDER BY id DESC LIMIT 1",
try{$spouses=$db->select("SELECT * FROM spouses WHERE member_id = ? AND is_archived = 0 ORDER BY spouse_order",[(int)$id]);}catch(\Throwable$e2){}
}
try{
$children=$db->select(
"SELECT c.*, pr.created_at AS due_date, p.payment_date
FROM children c
LEFT JOIN payment_requests pr ON pr.related_entity_type = 'children' AND pr.related_entity_id = c.id AND pr.payment_type = 'addition_fee' AND pr.is_voided = 0
LEFT JOIN payments p ON p.id = c.activated_by_payment_id AND p.is_voided = 0
WHERE c.member_id = ? AND c.is_archived = 0
ORDER BY c.child_order",[(int)$id]
);
}catch(\Throwable$e){
try{$children=$db->select("SELECT * FROM children WHERE member_id = ? AND is_archived = 0 ORDER BY child_order",[(int)$id]);}catch(\Throwable$e2){}
}
try{
$temporaries=$db->select(
"SELECT t.*, pr.created_at AS due_date, p.payment_date
FROM temporary_members t
LEFT JOIN payment_requests pr ON pr.related_entity_type = 'temporary_members' AND pr.related_entity_id = t.id AND pr.payment_type = 'addition_fee' AND pr.is_voided = 0
LEFT JOIN payments p ON p.id = t.activated_by_payment_id AND p.is_voided = 0
WHERE t.member_id = ? AND t.is_archived = 0
ORDER BY t.id",[(int)$id]
);
}catch(\Throwable$e){
try{$temporaries=$db->select("SELECT * FROM temporary_members WHERE member_id = ? AND is_archived = 0 ORDER BY id",[(int)$id]);}catch(\Throwable$e2){}
}
// Reconcile membership status against payment source-of-truth
<divstyle="font-size:12px;color:#0D7377;font-weight:700;margin-bottom:10px;text-transform:uppercase;">👤 الأعضاء المؤقتون (<?=count($temporaries)?>)</div>
// Find the separation_fee payment that was made for this transfer
$transferPayment=$db->selectOne(
"SELECT id FROM payments WHERE member_id = ? AND payment_type = 'separation_fee' AND related_entity_type = 'transfer_requests' AND related_entity_id = ? AND is_voided = 0 ORDER BY id DESC LIMIT 1",