chnage count cycle function as per account type

This commit is contained in:
ns77@siliconpin.com
2025-08-28 13:21:25 +00:00
parent 527b982e84
commit ce54b72e0e

View File

@@ -69,95 +69,63 @@
// // Ensure count doesn't exceed total installments // // Ensure count doesn't exceed total installments
// return min($count, $totalInstallment); // return min($count, $totalInstallment);
// } // }
function countCycles($cycle, $createDate, $totalInstallment, $accountType) { function countCycles($cycle, $createDate, $totalInstallment) {
$today = new DateTime(); $today = new DateTime();
$startDate = new DateTime($createDate); $startDate = new DateTime($createDate);
$cycle = strtoupper(trim($cycle)); $cycle = strtoupper(trim($cycle));
$accountType = strtoupper(trim($accountType));
if ($accountType === "Recurring") { if ($cycle === "D") {
// Recurring account: creation date ই first EMI date // দৈনিক চক্র: ক্রিয়েশন ডেট থেকেই শুরু
$firstPaymentDate = clone $startDate; $firstPaymentDate = clone $startDate; // পরের দিন নয়, সেই দিনই
if ($firstPaymentDate > $today) { if ($firstPaymentDate > $today) {
$count = 0; $count = 0;
} else { } else {
if ($cycle === "D") { $diff = $firstPaymentDate->diff($today);
$diff = $firstPaymentDate->diff($today); $count = $diff->days + 1; // +1 because diff->days doesn't include start day
$count = $diff->days + 1;
} elseif ($cycle === "W") {
$diff = $firstPaymentDate->diff($today);
$count = floor($diff->days / 7) + 1;
} elseif ($cycle === "M") {
$count = 1; // প্রথম EMI already due
$nextPaymentDate = clone $firstPaymentDate;
while ($nextPaymentDate <= $today) {
$nextPaymentDate->modify('+1 month');
if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) {
$nextPaymentDate->modify('last day of this month');
}
if ($nextPaymentDate <= $today) {
$count++;
}
}
} else {
$count = 0;
}
} }
} elseif ($accountType === "Loan") { } elseif ($cycle === "W") {
// Loan account: cycle অনুযায়ী first EMI later // সাপ্তাহিক চক্র: দিন পরে প্রথম EMI
if ($cycle === "D") { $firstPaymentDate = (clone $startDate)->modify('+7 days');
$firstPaymentDate = (clone $startDate)->modify('+1 day');
if ($firstPaymentDate > $today) { if ($firstPaymentDate > $today) {
$count = 0; $count = 0;
} else { } else {
$diff = $firstPaymentDate->diff($today); $diff = $firstPaymentDate->diff($today);
$count = $diff->days + 1; $count = floor($diff->days / 7) + 1;
} }
} elseif ($cycle === "W") { } elseif ($cycle === "M") {
$firstPaymentDate = (clone $startDate)->modify('+7 days'); // মাসিক চক্র: পরের মাসের同一天 প্রথম EMI
$firstPaymentDate = (clone $startDate)->modify('+1 month');
if ($firstPaymentDate > $today) { // যদি পরের মাসে同一天 না থাকে (যেমন 31st Jan থেকে 28/29th Feb)
$count = 0; if ($firstPaymentDate->format('d') != $startDate->format('d')) {
} else { $firstPaymentDate->modify('last day of this month');
$diff = $firstPaymentDate->diff($today); }
$count = floor($diff->days / 7) + 1;
}
} elseif ($cycle === "M") {
$firstPaymentDate = (clone $startDate)->modify('+1 month');
if ($firstPaymentDate->format('d') != $startDate->format('d')) { if ($firstPaymentDate > $today) {
$firstPaymentDate->modify('last day of this month'); $count = 0;
} } else {
$count = 1; // প্রথম EMI already due
$nextPaymentDate = clone $firstPaymentDate;
if ($firstPaymentDate > $today) { // পরের মাসগুলির জন্য গণনা
$count = 0; while ($nextPaymentDate <= $today) {
} else { $nextPaymentDate->modify('+1 month');
$count = 1;
$nextPaymentDate = clone $firstPaymentDate;
while ($nextPaymentDate <= $today) { // তারিখ adjust করাが必要 হলে
$nextPaymentDate->modify('+1 month'); if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) {
$nextPaymentDate->modify('last day of this month');
}
if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) { if ($nextPaymentDate <= $today) {
$nextPaymentDate->modify('last day of this month'); $count++;
}
if ($nextPaymentDate <= $today) {
$count++;
}
} }
} }
} else {
$count = 0;
} }
} else { } else {
$count = 0; // Unknown account type $count = 0;
} }
return min($count, $totalInstallment); return min($count, $totalInstallment);
@@ -215,7 +183,7 @@ foreach($types as $type){
$paidInstallment = ($row['AA_MATURE_VALUE'] + $row['AA_BAL']) / $row['AA_INSTALLMENT']; $paidInstallment = ($row['AA_MATURE_VALUE'] + $row['AA_BAL']) / $row['AA_INSTALLMENT'];
$paidInstallment = number_format($paidInstallment, 2, '.', ''); $paidInstallment = number_format($paidInstallment, 2, '.', '');
$deuEMITillDate = countCycles($row['AA_ACTYPE'], $row['AA_DATE'], $row['AA_NO_OF_PAYMENT'], $row['AA_TYPE']) - $paidInstallment; $deuEMITillDate = countCycles($row['AA_ACTYPE'], $row['AA_DATE'], $row['AA_NO_OF_PAYMENT']) - $paidInstallment;
// $deuEMITillDate = abs($deuEMITillDate); // $deuEMITillDate = abs($deuEMITillDate);
$deuEMITillDate = number_format($deuEMITillDate, 2, '.', ''); $deuEMITillDate = number_format($deuEMITillDate, 2, '.', '');
@@ -223,7 +191,7 @@ foreach($types as $type){
$paidInstallment = $row['AA_BAL'] / $row['AA_INSTALLMENT']; $paidInstallment = $row['AA_BAL'] / $row['AA_INSTALLMENT'];
$paidInstallment = number_format($paidInstallment, 2, '.', ''); $paidInstallment = number_format($paidInstallment, 2, '.', '');
$deuEMITillDate = countCycles($row['AA_ACTYPE'], $row['AA_DATE'], $row['AA_NO_OF_PAYMENT'], $row['AA_TYPE']) - $paidInstallment; $deuEMITillDate = countCycles($row['AA_ACTYPE'], $row['AA_DATE'], $row['AA_NO_OF_PAYMENT']) - $paidInstallment;
// $deuEMITillDate = abs($deuEMITillDate); // $deuEMITillDate = abs($deuEMITillDate);
$deuEMITillDate = number_format($deuEMITillDate, 2, '.', ''); $deuEMITillDate = number_format($deuEMITillDate, 2, '.', '');