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,57 +69,24 @@
// // 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") {
// Recurring account: creation date ই first EMI date
$firstPaymentDate = clone $startDate;
if ($firstPaymentDate > $today) {
$count = 0;
} else {
if ($cycle === "D") { if ($cycle === "D") {
$diff = $firstPaymentDate->diff($today); // দৈনিক চক্র: ক্রিয়েশন ডেট থেকেই শুরু
$count = $diff->days + 1; $firstPaymentDate = clone $startDate; // পরের দিন নয়, সেই দিনই
} 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") {
// Loan account: cycle অনুযায়ী first EMI later
if ($cycle === "D") {
$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 = $diff->days + 1; // +1 because diff->days doesn't include start day
} }
} elseif ($cycle === "W") { } elseif ($cycle === "W") {
// সাপ্তাহিক চক্র: দিন পরে প্রথম EMI
$firstPaymentDate = (clone $startDate)->modify('+7 days'); $firstPaymentDate = (clone $startDate)->modify('+7 days');
if ($firstPaymentDate > $today) { if ($firstPaymentDate > $today) {
@@ -129,8 +96,10 @@
$count = floor($diff->days / 7) + 1; $count = floor($diff->days / 7) + 1;
} }
} elseif ($cycle === "M") { } elseif ($cycle === "M") {
// মাসিক চক্র: পরের মাসের同一天 প্রথম EMI
$firstPaymentDate = (clone $startDate)->modify('+1 month'); $firstPaymentDate = (clone $startDate)->modify('+1 month');
// যদি পরের মাসে同一天 না থাকে (যেমন 31st Jan থেকে 28/29th Feb)
if ($firstPaymentDate->format('d') != $startDate->format('d')) { if ($firstPaymentDate->format('d') != $startDate->format('d')) {
$firstPaymentDate->modify('last day of this month'); $firstPaymentDate->modify('last day of this month');
} }
@@ -138,12 +107,14 @@
if ($firstPaymentDate > $today) { if ($firstPaymentDate > $today) {
$count = 0; $count = 0;
} else { } else {
$count = 1; $count = 1; // প্রথম EMI already due
$nextPaymentDate = clone $firstPaymentDate; $nextPaymentDate = clone $firstPaymentDate;
// পরের মাসগুলির জন্য গণনা
while ($nextPaymentDate <= $today) { while ($nextPaymentDate <= $today) {
$nextPaymentDate->modify('+1 month'); $nextPaymentDate->modify('+1 month');
// তারিখ adjust করাが必要 হলে
if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) { if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) {
$nextPaymentDate->modify('last day of this month'); $nextPaymentDate->modify('last day of this month');
} }
@@ -156,9 +127,6 @@
} else { } else {
$count = 0; $count = 0;
} }
} else {
$count = 0; // Unknown account type
}
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, '.', '');