modify('+1 day'); // if ($startDate > $today) { // $count = 0; // } else { // $diff = $startDate->diff($today); // $count = $diff->days; // } // } elseif ($cycle === "W") { // // For weekly cycles, first payment is next week // $startDate->modify('next monday'); // or use '+1 week' if you want exactly 7 days later // if ($startDate > $today) { // $count = 0; // } else { // $diff = $startDate->diff($today); // $count = floor($diff->days / 7); // } // } elseif ($cycle === "M") { // // For monthly cycles, first payment is next month // $startDate->modify('first day of next month'); // if ($startDate > $today) { // $count = 0; // } else { // $diff = $startDate->diff($today); // $count = ($diff->y * 12) + $diff->m; // // If we're in the same month but after the start date, add one // if ($diff->y == 0 && $diff->m == 0 && $diff->d > 0) { // $count++; // } // } // } else { // $count = 0; // unknown cycle // } // // Ensure count doesn't exceed total installments // return min($count, $totalInstallment); // } function countCycles($cycle, $createDate, $totalInstallment, $accountType) { $today = new DateTime(); $startDate = new DateTime($createDate); $cycle = strtoupper(trim($cycle)); if ($cycle === "D") { // দৈনিক চক্র: ক্রিয়েশন ডেট থেকেই শুরু if($accountType === 'Recurring'){ $firstPaymentDate = clone $startDate; // সেই দিনই শুরু } elseif($accountType === 'Loan') { $firstPaymentDate = (clone $startDate)->modify('+1 day'); // পরের দিন থেকে } if ($firstPaymentDate > $today) { $count = 0; } else { $diff = $firstPaymentDate->diff($today); $count = $diff->days + 1; // +1 because diff->days doesn't include start day } } elseif ($cycle === "W") { if($accountType === 'Recurring'){ $firstPaymentDate = clone $startDate; }elseif($accountType === 'Loan'){ // সাপ্তাহিক চক্র: ৭ দিন পরে প্রথম 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") { if($accountType === 'Recurring'){ $firstPaymentDate = clone $startDate; }elseif($accountType === 'Loan'){ // মাসিক চক্র: পরের মাসের同一天 প্রথম 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; // পরের মাসগুলির জন্য গণনা while ($nextPaymentDate <= $today) { $nextPaymentDate->modify('+1 month'); // তারিখ adjust করাが必要 হলে if ($nextPaymentDate->format('d') != $firstPaymentDate->format('d')) { $nextPaymentDate->modify('last day of this month'); } if ($nextPaymentDate <= $today) { $count++; } } } } else { $count = 0; } return min($count, $totalInstallment); } $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $totalLoanEMIAmount = 0; $totalDeuEMITillDate = 0; $totalRecurringEMIAmount = 0; $totalDeuInstallTillDate = 0; $agent_id = $_SESSION['user_id']; $types = ['Loan', 'Recurring']; $pdfData = []; $sl = 1; // Grand total for PDF $grandTotal = [ 'accounts' => 0, 'paidInstallments' => 0, 'dueInstallments' => 0, 'dueAmount' => 0 ]; foreach($types as $type){ $typeLike = "%$type%"; if($_SESSION['type']==='admin'){ $sql = "SELECT * FROM `" . $GLOBALS['arif_ac'] . "` WHERE `AA_TYPE` LIKE ? AND (`STATUS` IS NULL OR (`STATUS`!='closed' AND `STATUS`!='matured')) ORDER BY `AA_ID` DESC"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $typeLike); } elseif($_SESSION['type']==='agent'){ $sql = "SELECT * FROM `" . $GLOBALS['arif_ac'] . "` WHERE `AA_TYPE` LIKE ? AND (`STATUS` IS NULL OR (`STATUS`!='closed' AND `STATUS`!='matured')) AND `AA_AGENT`=? ORDER BY `AA_ID` DESC"; $stmt = $conn->prepare($sql); $stmt->bind_param("ss", $typeLike, $agent_id); } $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows>0){ echo ""; while($row = $result->fetch_assoc()){ $totalDueAmt = $row['AA_BAL']; if ($row['AA_INSTALLMENT'] > 0) { $paidInst = ($row['AA_MATURE_VALUE'] + $row['AA_BAL']) / $row['AA_INSTALLMENT']; $paidInst = number_format($paidInst, 2); $remainInst = abs($row['AA_BAL']) / $row['AA_INSTALLMENT']; $remainInst = number_format($remainInst, 2); } else { $paidInst = 0; // বা অন্য লজিক $remainInst = 0; } if($row['AA_TYPE'] === 'Loan'){ $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 = number_format($deuEMITillDate, 2, '.', ''); if($deuEMITillDate > 0){ // echo $deuEMITillDate; $totalDeuEMITillDate += $deuEMITillDate; // এখানে যোগ হচ্ছে $totalLoanEMIAmount += $row['AA_INSTALLMENT']; } }elseif($row['AA_TYPE'] === 'Recurring'){ $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 = abs($deuEMITillDate); $deuEMITillDate = number_format($deuEMITillDate, 2, '.', ''); if($deuEMITillDate > 0){ // echo $deuEMITillDate; $totalDeuInstallTillDate += $deuEMITillDate; // এখানে যোগ হচ্ছে $totalRecurringEMIAmount += $row['AA_INSTALLMENT']; } } // HTML table row if($deuEMITillDate > 0){ echo ""; } // // PDF simplified row // "SL","NAME ","MOBILE NO", "CYCLE", "ACCOUNT NO","EMI PAID","EMI DUE", "EMI AMOUNT", "CUSTOMER. SIGN" if($deuEMITillDate > 0){ $pdfData[] = [ $type, $sl, $row['AA_NAME'], // NAME OF ACCOUNT HOLDER $row['AA_PHONE'], // MOBILE NO (blank) $row['AA_ACTYPE'], // EMI Cycle $row['AA_ACNO'], // ACCOUNT NO $paidInstallment, // NO OF INSTALLMENT PAID $deuEMITillDate, // NO OF INSTALLMENT DUE $row['AA_INSTALLMENT'], // EMI AMOUNT // $totalDueAmt, // TOTAL DUE AMOUNT "" // CUSTOMER SIGNATURE ]; } // Update grand total $grandTotal['accounts']++; $grandTotal['paidInstallments'] += $paidInstallment; $grandTotal['dueInstallments'] += $deuEMITillDate; $grandTotal['dueAmount'] += $totalDueAmt; $sl++; // echo $dueInst . '
'; } } } $conn->close(); $grandTotal['totalNumberOfLoanEMI'] = $totalDeuEMITillDate; $grandTotal['totalNumberOfRecurringEMI'] = $totalDeuInstallTillDate; $grandTotal['totalLoanEMIAmount'] = $totalLoanEMIAmount; $grandTotal['totalRecurringEMIAmount'] = $totalRecurringEMIAmount; // echo "
Total Due EMI Till Date (Loan): ".$totalDeuEMITillDate; // echo "
Total Due Installment Till Date (Recurring): ".$totalDeuInstallTillDate; ?>
SL Type AC No Name Mobile Account Create Date Maturity Value Balance Total EMI Total Paid EMI Deu EMI Till Date EMI Amount Total Due Amount
$type Demand
".$sl." ".$row['AA_ACTYPE'].", ".$row['AA_TYPE']." ".$row['AA_ACNO']." ".$row['AA_NAME']." ".$row['AA_PHONE']." ".$row['AA_DATE']." ".$row['AA_MATURE_VALUE']." ".$row['AA_BAL']." ".$row['AA_NO_OF_PAYMENT']." ".$paidInstallment." ".$deuEMITillDate." ".$row['AA_INSTALLMENT']." ".$totalDueAmt."
".$remainInst." ".$row['AA_NO_OF_PAYMENT']."