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