Commit adb79de3 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(waiver): stop creating subscriptions for waiver targets, set join_date on dependents

Waiver-acquired members should not have subscription rows created — the waiver
fee covers the annual subscription implicitly. Also sets join_date on all
dependents (spouses, children, temps) when they are activated during waiver
completion, using COALESCE to preserve any pre-existing value.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent b732d877
......@@ -118,7 +118,16 @@ $_memberInitials = mb_substr($member->full_name_ar, 0, 2);
<?php endif; ?>
<?php endif; ?>
</div>
<div style="font-size:12px;color:rgba(255,255,255,0.7);">تاريخ: <?= e(substr($member->membership_origin_date ?? $member->activated_at ?? $member->created_at, 0, 10)) ?></div>
<div style="font-size:12px;color:rgba(255,255,255,0.7);"><?php
$isWaiverAcquired = !empty($member->transferred_from_waiver_id);
$isDeathAcquired = !empty($member->transferred_from_death_id);
$isTransferAcquired = !empty($member->transferred_from_transfer_id);
if ($isWaiverAcquired || $isDeathAcquired || $isTransferAcquired):
echo 'تاريخ الانتقال: ' . e(substr($member->ownership_transfer_date ?? $member->activated_at ?? $member->created_at, 0, 10));
else:
echo 'تاريخ: ' . e(substr($member->membership_origin_date ?? $member->activated_at ?? $member->created_at, 0, 10));
endif;
?></div>
</div>
</div>
<div style="padding:0 25px 25px;margin-top:-45px;position:relative;">
......@@ -225,6 +234,9 @@ $_memberInitials = mb_substr($member->full_name_ar, 0, 2);
<?php elseif (!empty($waiverTransfer)): ?>
<tr><td style="padding:6px 0;color:#6B7280;width:160px;">انتقلت من</td><td style="padding:6px 0;font-weight:600;"><a href="/members/archive/<?= (int) ($waiverTransfer['source_member_id_ref'] ?? 0) ?>" style="color:#7C3AED;"><?= e($waiverTransfer['source_member_name'] ?? $member->waived_from_member_name ?? '—') ?></a> (تنازل)</td></tr>
<tr><td style="padding:6px 0;color:#6B7280;">تاريخ الانتقال</td><td style="padding:6px 0;font-weight:600;"><?= e($member->ownership_transfer_date ?? substr($waiverTransfer['waiver_date'] ?? '', 0, 10)) ?></td></tr>
<?php if (!empty($member->membership_origin_date)): ?>
<tr><td style="padding:6px 0;color:#6B7280;">تاريخ العضوية الأصلية</td><td style="padding:6px 0;font-weight:600;"><?= e($member->membership_origin_date) ?></td></tr>
<?php endif; ?>
<?php if (!empty($transferFeePayment)): ?>
<tr><td style="padding:6px 0;color:#6B7280;">المبلغ المدفوع فعلياً</td><td style="padding:6px 0;font-weight:700;color:#059669;font-size:15px;"><?= number_format((float) $transferFeePayment['amount'], 2) ?> ج.م</td></tr>
<tr><td style="padding:6px 0;color:#6B7280;">تاريخ الدفع</td><td style="padding:6px 0;"><?= e($transferFeePayment['payment_date'] ?? '—') ?></td></tr>
......
......@@ -7,7 +7,6 @@ use App\Core\App;
use App\Core\EventBus;
use App\Core\Logger;
use App\Modules\Archive\Services\ArchiveService;
use App\Modules\Subscriptions\Services\SubscriptionSyncService;
final class WaiverProcessor
{
......@@ -96,49 +95,23 @@ final class WaiverProcessor
$db->commit();
// Generate subscription rows for target member and mark ALL family as paid
// Activate all dependents and set join_date — their fees are included in the waiver fee
$targetMemberId = (int) $waiver['target_member_id'];
try {
SubscriptionSyncService::syncForMember($targetMemberId);
// Mark current year subscriptions as paid for ALL family members
// (member + spouses + children + temporary — the waiver fee includes annual subscription)
$now = new \DateTime();
$month = (int) $now->format('n');
$year = (int) $now->format('Y');
$fyStart = $month >= 7 ? $year : $year - 1;
$currentFy = $fyStart . '/' . ($fyStart + 1);
$pendingSubs = $db->select(
"SELECT id, total_amount, fine_amount, development_fee FROM subscriptions WHERE member_id = ? AND financial_year = ? AND status IN ('pending','overdue')",
[$targetMemberId, $currentFy]
);
$ts = date('Y-m-d H:i:s');
$paidByEmp = $employee ? (int) $employee->id : null;
foreach ($pendingSubs as $sub) {
$paidAmount = bcadd(bcadd($sub['total_amount'], $sub['fine_amount'] ?? '0', 2), $sub['development_fee'] ?? '0', 2);
$db->update('subscriptions', [
'paid_amount' => $paidAmount,
'payment_id' => $waiverPaymentId,
'status' => 'paid',
'paid_at' => $ts,
'paid_by' => $paidByEmp,
'updated_at' => $ts,
], '`id` = ?', [(int) $sub['id']]);
}
// Activate all dependents — their fees are included in the waiver fee
$today = date('Y-m-d');
$db->query(
"UPDATE spouses SET status = 'active', activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$waiverPaymentId, $ts, $targetMemberId]
"UPDATE spouses SET status = 'active', join_date = COALESCE(join_date, ?), activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$today, $waiverPaymentId, $ts, $targetMemberId]
);
$db->query(
"UPDATE children SET status = 'active', activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$waiverPaymentId, $ts, $targetMemberId]
"UPDATE children SET status = 'active', join_date = COALESCE(join_date, ?), activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$today, $waiverPaymentId, $ts, $targetMemberId]
);
$db->query(
"UPDATE temporary_members SET status = 'active', activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$waiverPaymentId, $ts, $targetMemberId]
"UPDATE temporary_members SET status = 'active', join_date = COALESCE(join_date, ?), activated_by_payment_id = ?, updated_at = ? WHERE member_id = ? AND is_archived = 0 AND status IN ('pending_payment','pending')",
[$today, $waiverPaymentId, $ts, $targetMemberId]
);
} catch (\Throwable $e) {
Logger::warning("Waiver subscription sync failed: " . $e->getMessage());
......
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