File: /home/cafsindia/uds.cafsinfotech.in/application/views/loan/installment.php
<style>
table th{
position: sticky;
top: 0;
z-index: 2;
background-color: #001630;
}
</style>
<?php
//Monthly Installment Details
$tr_line = "";
$i = 1;
$check_sts_arr = array(1 => "1",3 => "3");
$result_count = count($install_result);
foreach($install_result as $loan){
$installment_id = $loan->prime_loan_installment_id;
$loan_id = $loan->loan_id;
$category = $loan->category;
$emp_code = $loan->employee_code;
$loan_type = $loan->loan_type;
$loan_date = $loan->loan_date;
$apply_year = $loan->apply_year;
$loan_amount = $loan->loan_amount;
$interest_rate = $loan->interest_rate;
$number_of_installment = $loan->number_of_installment;
$per_month = $loan->per_month;
$total_amount = $loan->total_amount;
$installment_count = $loan->installment_count;
$install_year = $loan->install_year;
$install_month = $loan->install_month;
$install_amount = $loan->install_amount;
$recovery_amount = $loan->recovery_amount;
$balance_amount = $loan->balance_amount;
$prev_pending = $loan->prev_pending;
$paid_status = (int)$loan->paid_status;
$foreclose_remark = $loan->loan_foreclose_remark;
$read = '';
$delete_btn = "";
$tot_installment += $install_amount;
$format_loan_date = date("d-m-Y", strtotime($loan_date));
$view_name_query = 'SELECT view_name FROM cw_form_setting WHERE label_name="'.$loan_type.'" AND prime_module_id="employees" AND trans_status="1"';
// echo $view_name_query; die;
$view_name_info = $this->db->query("CALL sp_a_run ('SELECT','$view_name_query')");
$view_name_result = $view_name_info->result();
$view_name_info->next_result();
$loan_type_view_name =$view_name_result[0]->view_name;
//GET PAID STATUS
$delete_btn = "<td style = 'text-align:center;' class='delete_btn_td'></td>";
if($max_install_year){ //for max install year(all status Readonly except not paid)
$installment_year = date('Y-m',strtotime("01-$install_year"));
$max_install_year = date('Y-m',strtotime($max_install_year));
if($installment_year <= $max_install_year){
$read = 'readonly';
}
// else
if($result_count === $i){
$delete_btn = "<td style='text-align:center;' class='delete_btn_td'><button Onclick=delete_install('$installment_id','$install_amount','$install_year','$loan_id','$emp_code','$prev_pending') class='btn btn-xs btn-danger' id='delete_btn'><i class='fa fa-trash' aria-hidden='true'></i>Delete</button></td>";
}
// else{
// $delete_btn ="<td style = 'text-align:center;'></td>";
// }
if($installment_year !== $max_install_year && $result_count === $i && $paid_status === 0){
$read = "";
}
}else{
if($result_count === $i){
$read = "";
$delete_btn = "<td style='text-align:center;' class='delete_btn_td'><button Onclick=delete_install('$installment_id','$install_amount','$install_year','$loan_id','$emp_code','$prev_pending') class='btn btn-xs btn-danger' id='delete_btn'><i class='fa fa-trash' aria-hidden='true'></i>Delete</button></td>";
}
}
if($paid_status === 0){
$paid_status = 'Not Paid';
}else
if($paid_status === 1){
$paid_status = 'Paid';
$read = 'readonly';
}else
if($paid_status === 2){
$read = 'readonly';
$paid_status = 'Partially Paid';
}else
if($paid_status === 3){
$paid_status = 'Foreclosed';
}else
if($paid_status === 4){
$paid_status = "Carry Forward";
$read = 'readonly';
}else
if($paid_status === 5){
$paid_status = "Partially paid & Carry Forward";
$read = 'readonly';
}
$value_id = 'install_amount_'.$installment_id;
$tr_line .= "<tr id='install_row_$installment_id'><input type='hidden' name='hid_install_id[]' value='$installment_id'>
$delete_btn
<td style = 'text-align:center;'>$installment_count<input type='hidden' name='hid_installment[]' value='$installment_count'></td>
<td style = 'text-align:center;'>$install_year <input type='hidden' name='hid_install_year[]' value='$install_year'></td>
<td><input type='text' name='install_amount[]' class='numbersOnly form-control' id='$value_id' value='$install_amount' $read '/></td>
<td style = 'text-align:center;'>$recovery_amount</td>
<td style = 'text-align:center;'>$balance_amount</td>
<!--<td style = 'text-align:center;'>$prev_pending</td> -->
<td style = 'text-align:center;'>$paid_status</td>
<td style = 'text-align:center;'>$foreclose_remark</td>";
if($result_count === $i){
// echo ("$result_count:::: $i"); die;
$last_install_year = date('Y-m-d',strtotime('01-'.$install_year));
$next_installment = date('m-Y',strtotime('+1 month',strtotime($last_install_year)));
$tr_line .= "<td style='text-align:center;'><button Onclick=add_new_row($installment_count,$installment_id,'$next_installment','$total_amount','$install_amount','$install_year','$emp_code','$loan_id') class='btn btn-xs btn-info'><i class='fa fa-plus' aria-hidden='true'></i>Add New</button></td></tr>";
}
$i++;
}
//TABLE FOR INSTALLMENT DETAILS
echo "<div style='padding:10px;' id='loan_modal'>
<div style='background-color: #FFFFFF !important; box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2); border-radius: 3px; margin: 8px; padding: 8px;overflow: auto;'>
<table style='width:100%;'>
<tr>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Employee Code</span><br/>
$emp_code
</td>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Loan Type</span><br/>
$loan_type_view_name
</td>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Loan Date</span><br/>
$format_loan_date
</td>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Apply Month Year</span><br/>
$apply_year
</td>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Loan Amount</span><br/>
$loan_amount
</td>
<td style='white-space:nowrap;padding-right:10px;font-weight:bold;text-align: center;'>
<span style='color:#00b0eb !important;font-size:12px;'>Total Amount</span><br/>
$total_amount
</td>
</tr>
</table>
</div>
<div id='installment' style='max-height:450px !important;overflow:auto !important;'>
<table id='installment_data' class='table table-striped table-hover' style='width:100% !important;'>
<thead>
<tr>
<th style = 'text-align:center;'></th>
<th style = 'text-align:center;'>Installment</th>
<th style = 'text-align:center;'>Installment Month and Year</th>
<th style = 'text-align:center;'>Monthly Amount Deduct</th>
<th style = 'text-align:center;'>Recovery Amount</th>
<th style = 'text-align:center;'>Balance Amount</th>
<!--<th style = 'text-align:center;'>Prevoius Pending</th> -->
<th style = 'text-align:center;'>Paid Status</th>
<th style = 'text-align:center;'>Foreclose Remark</th>
<th style = 'text-align:center;'>Options</th>
</tr>
</thead>
<tbody>
$tr_line
</tbody >
<tfoot><tr><td></td><td style='font-weight:bold; font-size:15px;'></td><td><h5 style='font-weight:bold; text-align:center;'>Total</h5></td><td><h5 id='footer' style='font-weight:bold;'>$tot_installment</h5></td><td></td><td></td><td></td><td></td><td><button id='installment_submit' OnClick =save_data('$loan_id','$category','$emp_code','$loan_type','$loan_date','$apply_year','$loan_amount','$total_amount','$tot_installment','$interest_rate') class='btn btn-sm btn-primary'>Submit</button><button type='button' class='btn btn-sm btn-primary' style='margin-left:10px;' data-dismiss='modal' aria-label='Close' onclick='close_formfun()'><span aria-hidden='true'>Close</span></button></td>
</tr></tfoot>
</table>
<div></div>
</div>
</div>";
?>
<script type="text/javascript">
$(document).ready(function(){
//FUNCTION FOR ONLY ALLOW NUMBER
jQuery('.numbersOnly').keyup(function () {
this.value = this.value.replace(/[^0-9\.]/g,'');
});
//ONCHANGE FOOTER SUM
$(document).on("input",'input[name="install_amount[]"]',function() {
this.value = this.value.replace(/[^0-9\.]/g,'');
var install_amt = $('input[name="install_amount[]"]').map(function(){return $(this).val();}).get();
var total = 0;
for (var i = 0; i < install_amt.length; i++) {
total += install_amt[i] << 0;
}
$('#installment_data #footer').html(total);
});
$('input[type=search]').addClass('form-control input-sm');
$("#close_btn").on("click",function(){
$('#loan_modal').modal('hide');
})
});
//FUNCTION FOR ADD NEW ROW
function add_new_row(row_count,installment_id,next_installment,total_amount,install_amount,install_year,emp_code,loan_id){
var row_count = row_count+1;
var install_amt = $('input[name="install_amount[]"]').map(function(){return $(this).val();}).get();
var lastvalue = install_amt[install_amt.length - 1];
if(lastvalue === ""){
toastr.error("Input is empty please enter value");
return false;
}
var last_tr = $("#installment_data").find("tr").last();
count = $('#installment_data tr').length - 1;
var installment_id = "0";
var today = "01-"+next_installment;
var future_mnth = moment(today, 'DD-MM-YYYY').format('YYYY-MM');
var future = moment(future_mnth).add(1, 'M').format('MM-YYYY');
var install_amt = $('input[name="install_amount[]"]').map(function(){return $(this).val();}).get();
var total = 0;
for (var i = 0; i < install_amt.length; i++) {
total += install_amt[i] << 0;
}
total = parseInt(total);
if(total_amount <= total ){
toastr.error("Loan amount matched with install Amount. Could not able to create a new installment..");
return false;
}
$('tbody tr button').remove();
$('tbody tr a').remove();
var data;
data+="<tr><td><td style='text-align:center;'>" + (row_count) + "<input type='hidden' name='hid_install_id[]' value='0'><input type='hidden' name='hid_installment[]' value='"+ (row_count) +"'></td><td style='text-align:center'>"+next_installment+"<input type='hidden' name='hid_install_year[]' value='"+next_installment+"'></td><td><input type='text' name='install_amount[]' class='numbersOnly form-control'><input type='hidden' name='hid_install_amt' class='numbersOnly form-control'></td><td style='text-align:center;'>0.00</td><td style='text-align:center;'>0.00</td><td style='text-align:center;'>Not Paid</td><td style='text-align:center;'></td><td style='text-align:center;'><button Onclick=add_new_row("+row_count+","+installment_id+",'"+future+"','"+total_amount+"','"+install_amount+"','"+install_year+"','"+emp_code+"','"+loan_id+"') class='btn btn-xs btn-info new_btn'><i class='fa fa-plus' aria-hidden='true'></i>Add New</button><a style='color:red; font-weight:bold; font-size:14px; font-weight:strong; cursor: pointer; border:1px solid black; padding:1px; margin-left:3px;' Onclick=remove_row("+row_count+","+installment_id+",'"+future+"','"+total_amount+"','"+install_amount+"','"+install_year+"','"+emp_code+"','"+loan_id+"')>❌</a></td>";
$('#installment_data').append(data);
row_count +=1;
var today = "01-"+next_installment;
var future_mnth = moment(today, 'DD-MM-YYYY').format('YYYY-MM');
var future = moment(future_mnth).add(1, 'M').format('DD-MM-YYYY');
}
function remove_row(row_count, installment_id, next_installment, total_amount, install_amount, install_year, emp_code, loan_id) {
var install_amt = $('input[name="install_amount[]"]').map(function () { return $(this).val(); }).get();
var lastvalue = install_amt[install_amt.length - 1];
if (lastvalue && parseInt(lastvalue) !== 0) {
toastr.error("Please remove the input value first.");
return false;
}
// Find the current row
var currentRow = $("#installment_data tbody tr:last");
// Find the previous row
var previousRow = currentRow.prev();
var today = "01-" + next_installment; // Combine with day to make it "DD-MM-YYYY"
var next_installment = moment(today, 'DD-MM-YYYY').subtract(1, 'months').format('MM-YYYY');
// Find the hidden input and get its value
var hid_install_id = previousRow.find('input[name="hid_install_id[]"]').val();
if(hid_install_id === "0" || hid_install_id === ""){
var remove_btn = "<a style='color:red; font-weight:bold; font-size:14px; font-weight:strong; cursor: pointer; border:1px solid black; padding:1px; margin-left:3px;' Onclick=remove_row("+row_count+","+installment_id+",'"+next_installment+"','"+total_amount+"','"+install_amount+"','"+install_year+"','"+emp_code+"','"+loan_id+"')>❌</a>"
}else{
var remove_btn = "";
}
// Create the button
var add_btn = "<button Onclick=add_new_row("+row_count+","+installment_id+",'"+next_installment+"','"+total_amount+"','"+install_amount+"','"+install_year+"','"+emp_code+"','"+loan_id+"') class='btn btn-xs btn-info new_btn'><i class='fa fa-plus' aria-hidden='true'></i>Add New</button>"+remove_btn;
// Remove the current row
currentRow.remove();
// Find the last <td> within the previous row and append the button
previousRow.find("td:last").append(add_btn);
}
function delete_install(installment_id,install_amount,install_month,loan_id,emp_code,prev_pending){
$.confirm({
title: 'Confirm!',
content: 'This month installment will be added to the previous month Installment. Are you sure you want to continue..?',
type: 'red',
typeAnimated: true,
buttons: {
tryAgain: {
text: 'Yes',
btnClass: 'btn-red',
action: function(){
// Encryption
var encKey = '<?php echo $encKey; ?>';
var data = {installment_id:installment_id,install_amount:install_amount,install_month:install_month,loan_id:loan_id,emp_code:emp_code,prev_pending:prev_pending};
var encData = encrypt(encKey,data);
$.ajax({
type: 'POST',
url: '<?php echo site_url("$this->control_name/delete_installment");?>',
data:encData,
contentType: 'text/plain',
success: function(data){
var rslt = JSON.parse(data);
if(rslt.success){
toastr.success(rslt.message);
$('#loan_modal').html('');
$("#loan_modal").html(rslt.all_data);
}else{
toastr.error(rslt.message);
if(rslt.all_data){
$('#loan_modal').html('');
$("#loan_modal").html(rslt.all_data);
}
}
}
});
}
},
close: function () {
}
}
});
}
//SAVE INSTALLMENTS FUNCTION
function save_data(loan_id,category,emp_code,loan_type,loan_date,apply_year,loan_amount,total_amount,tot_installment,interest_rate){
$('#installment_submit').html("<i class='fa fa-spinner fa-spin'></i> Processing...");
var installment_count = $('input[name="hid_installment[]"]').map(function(){return $(this).val();}).get();
var install_year = $('input[name="hid_install_year[]"]').map(function(){return $(this).val();}).get();
var install_amt = $('input[name="install_amount[]"]').map(function(){return $(this).val();}).get();
var install_id = $('input[name="hid_install_id[]"]').map(function(){return $(this).val();}).get();
// Encryption
var encKey = '<?php echo $encKey; ?>';
var data = {installment_count:installment_count,install_year:install_year,install_amt:install_amt,install_id:install_id,loan_id:loan_id,category:category,emp_code:emp_code,loan_type:loan_type,loan_date:loan_date,apply_year:apply_year,loan_amount:loan_amount,total_amount:total_amount,tot_installment:tot_installment,interest_rate:interest_rate};
var encData = encrypt(encKey,data);
$.ajax({
type: 'POST',
url: '<?php echo site_url("$this->control_name/save_installment");?>',
data:encData,
contentType: 'text/plain',
success: function(data){
var rslt = JSON.parse(data);
$('#installment_submit').html("Submit");
if(rslt.success){
toastr.success(rslt.message);
$('.modal').modal('toggle');
$('#notify_list_model').modal('hide');
}else{
toastr.error(rslt.message);
// $('.modal').modal('toggle');
// $('#notify_list_model').modal('hide');
}
}
});
}
// function update_data(installment_id,loan_id,loan_amount,per_month,installment_count,installments,install_year,loan_type){
// var install_amount = $('#install_amount_'+installment_id).val();
// if(installment_id){
// $.ajax({
// type: 'POST',
// url: '<?php echo site_url("$this->control_name/update_data");?>',
// data:{loan_id:loan_id,installment_id:installment_id,install_amount:install_amount,loan_amount:loan_amount,per_month:per_month,installment_count:installment_count,installments:installments,install_year:install_year,loan_type:loan_type},
// success: function(data){
// var rslt = JSON.parse(data);
// if(rslt.success){
// toastr.success(rslt.message);
// $('.modal').modal('toggle');
// $('#notify_list_model').modal('hide');
// }else{
// toastr.error(rslt.message);
// }
// }
// });
// }
// }
</script>