Commit 705bbcbe authored by Fares's avatar Fares

fix(transfers): fix annual subscription calculation and marriage_date column error

1. isCurrentYearSubscriptionCovered() was comparing activation date against
   its own fiscal year (always true) — now correctly compares against the
   current fiscal year's July 1st.

2. Annual subscription in calculate() now includes all family members
   (spouses, children, temps) not just the member alone.

3. marriage_date and dep count columns are only included in INSERT when
   non-null, preventing column-not-found error if migration hasn't run.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 2e4f8575
...@@ -253,7 +253,7 @@ class TransferController extends Controller ...@@ -253,7 +253,7 @@ class TransferController extends Controller
$notesJson = $notes ?: null; $notesJson = $notes ?: null;
} }
$transferReq = TransferRequest::create([ $createData = [
'source_member_id' => (int) $memberId, 'source_member_id' => (int) $memberId,
'transfer_type' => $transferType, 'transfer_type' => $transferType,
'child_id' => $childId, 'child_id' => $childId,
...@@ -276,13 +276,24 @@ class TransferController extends Controller ...@@ -276,13 +276,24 @@ class TransferController extends Controller
'is_employed' => $isEmployed !== null ? ($isEmployed ? 1 : 0) : null, 'is_employed' => $isEmployed !== null ? ($isEmployed ? 1 : 0) : null,
'graduation_date' => $graduationDate, 'graduation_date' => $graduationDate,
'work_date' => $workDate, 'work_date' => $workDate,
'marriage_date' => $marriageDate,
'date_turned_25' => $dateTurned25, 'date_turned_25' => $dateTurned25,
'effective_transfer_date' => $effectiveTransferDate, 'effective_transfer_date' => $effectiveTransferDate,
'target_spouses_count' => $depSpouses > 0 ? $depSpouses : null, ];
'target_children_count' => $depChildren > 0 ? $depChildren : null,
'target_temps_count' => $depTemps > 0 ? $depTemps : null, if ($marriageDate !== null) {
]); $createData['marriage_date'] = $marriageDate;
}
if ($depSpouses > 0) {
$createData['target_spouses_count'] = $depSpouses;
}
if ($depChildren > 0) {
$createData['target_children_count'] = $depChildren;
}
if ($depTemps > 0) {
$createData['target_temps_count'] = $depTemps;
}
$transferReq = TransferRequest::create($createData);
if (FormBridge::exists('TRANSFER_SEPARATION')) { if (FormBridge::exists('TRANSFER_SEPARATION')) {
FormBridge::submit('TRANSFER_SEPARATION', $request->all(), (int) $memberId, 'طلب فصل/تحويل'); FormBridge::submit('TRANSFER_SEPARATION', $request->all(), (int) $memberId, 'طلب فصل/تحويل');
......
...@@ -73,10 +73,32 @@ final class SeparationFeeCalculator ...@@ -73,10 +73,32 @@ final class SeparationFeeCalculator
// Annual subscription: skip if member was activated after July 1 of current FY // Annual subscription: skip if member was activated after July 1 of current FY
$activatedAt = $member['activated_at'] ?? $member['created_at'] ?? date('Y-m-d'); $activatedAt = $member['activated_at'] ?? $member['created_at'] ?? date('Y-m-d');
$annualSubCovered = self::isCurrentYearSubscriptionCovered($activatedAt); $annualSubCovered = self::isCurrentYearSubscriptionCovered($activatedAt);
$annualSub = ServicePrice::getPrice('SVC_ANNUAL_MEMBER', '492.00');
$memberSub = ServicePrice::getPrice('SVC_ANNUAL_MEMBER', '492.00');
$spouseSub = ServicePrice::getPrice('SVC_ANNUAL_SPOUSE', '492.00');
$childSub = ServicePrice::getPrice('SVC_ANNUAL_CHILD', '222.00');
$tempSub = ServicePrice::getPrice('SVC_ANNUAL_TEMP', '222.00');
$devFeeData = RuleEngine::get('DEVELOPMENT_FEE'); $devFeeData = RuleEngine::get('DEVELOPMENT_FEE');
$devFee = $devFeeData['amount'] ?? '35.00'; $devFee = $devFeeData['amount'] ?? '35.00';
$annualSubscriptionFee = $annualSubCovered ? '0.00' : bcadd($annualSub, $devFee, 2);
$spouseCount = (int) ($db->selectOne(
"SELECT COUNT(*) as cnt FROM spouses WHERE member_id = ? AND is_archived = 0 AND status = 'active'",
[$sourceMemberId]
)['cnt'] ?? 0);
$childCount = (int) ($db->selectOne(
"SELECT COUNT(*) as cnt FROM children WHERE member_id = ? AND is_archived = 0 AND status = 'active'",
[$sourceMemberId]
)['cnt'] ?? 0);
$tempCount = (int) ($db->selectOne(
"SELECT COUNT(*) as cnt FROM temporary_members WHERE member_id = ? AND is_archived = 0 AND status = 'active'",
[$sourceMemberId]
)['cnt'] ?? 0);
$familyTotal = $memberSub;
$familyTotal = bcadd($familyTotal, bcmul($spouseSub, (string) $spouseCount, 2), 2);
$familyTotal = bcadd($familyTotal, bcmul($childSub, (string) $childCount, 2), 2);
$familyTotal = bcadd($familyTotal, bcmul($tempSub, (string) $tempCount, 2), 2);
$annualSubscriptionFee = $annualSubCovered ? '0.00' : bcadd($familyTotal, $devFee, 2);
$totalFee = bcadd(bcadd($separationFee, $formFee, 2), $annualSubscriptionFee, 2); $totalFee = bcadd(bcadd($separationFee, $formFee, 2), $annualSubscriptionFee, 2);
...@@ -107,14 +129,13 @@ final class SeparationFeeCalculator ...@@ -107,14 +129,13 @@ final class SeparationFeeCalculator
public static function isCurrentYearSubscriptionCovered(string $activatedAt): bool public static function isCurrentYearSubscriptionCovered(string $activatedAt): bool
{ {
$activated = new \DateTime(substr($activatedAt, 0, 10)); $activated = new \DateTime(substr($activatedAt, 0, 10));
$year = (int) $activated->format('Y'); $now = new \DateTime();
$month = (int) $activated->format('n'); $currentYear = (int) $now->format('Y');
$currentMonth = (int) $now->format('n');
// Start of current fiscal year = July 1 of the year the member joined
// (if activated Jan-Jun, fiscal year started previous July) $fyStart = $currentMonth >= 7
$fyStart = $month >= 7 ? new \DateTime("{$currentYear}-07-01")
? new \DateTime("{$year}-07-01") : new \DateTime(($currentYear - 1) . '-07-01');
: new \DateTime(($year - 1) . '-07-01');
return $activated >= $fyStart; return $activated >= $fyStart;
} }
......
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