File: /home/cafsindia/hrms_allyindian_com/application_bk/controllers/Loan_eligibility.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Loan_eligibility extends Action_controller{
public function __construct(){
parent::__construct('loan_eligibility');
if(!$this->Appconfig->isAppvalid()){
redirect('config');
}
}
// LOAD PAGE WITH TABLE DATA
public function index(){
$data['encKey'] = $this->generateKey();
$loan_type_query = 'select label_name,view_name from cw_form_setting where prime_module_id = "employees" and loan_check = 1 ORDER BY input_for,field_sort asc';
$loan_type_data = $this->db->query("CALL sp_a_run ('SELECT','$loan_type_query')");
$loan_type_result = $loan_type_data->result();
$loan_type_data->next_result();
if($loan_type_result){
$pick_key = array_column($loan_type_result, "label_name");
$pick_val = array_column($loan_type_result, "view_name");
$final_pick = array_combine( $pick_key, $pick_val);
$final_pick = array("" => "---- Loan Type ----") + $final_pick;
}
$data['loan_type'] = $final_pick;
$this->load->view("$this->control_name/manage",$data);
}
public function save_data(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
$loan_code = $this->input->post('loan_code');
$created_on = date("Y-m-d H:i:s");
$prime_qry_key = '';
$prime_qry_value = '';
$based_qry = 'select prime_loan_eligibility_settings_id,eligibility_based_on from cw_loan_eligibility_settings where cw_loan_eligibility_settings.trans_status = 1';
$based_qry_info = $this->db->query("CALL sp_a_run ('SELECT','$based_qry')");
$based_qry_result = $based_qry_info->result();
$based_qry_info->next_result();
if(count($based_qry_result) === 0){
echo json_encode(array('success' => false,'message' => 'Loan Eligibility Settings not done..','table_data' => ""));
exit(0);
}
$based_type = $based_qry_result[0]->eligibility_based_on;
$based_col = str_replace("cw_","",$based_qry_result[0]->eligibility_based_on);
$based_col_qry = 'select label_name from cw_form_setting where prime_module_id = "'.$based_col.'" and trans_status = 1';
$based_col_info = $this->db->query("CALL sp_a_run ('SELECT','$based_col_qry')");
$based_col_result = $based_col_info->result();
$based_col_info->next_result();
$column_name = $based_col_result[0]->label_name;
if($column_name === 'category_name'){
$column_name = 'category';
}else{
$column_name = $based_col_result[0]->label_name;
}
$empty_qry = 'select prime_'.$column_name.'_id as eligibility_name from '.$based_type.' where trans_status = 1';
$empty_qry_info = $this->db->query("CALL sp_a_run ('SELECT','$empty_qry')");
$empty_qry_result = $empty_qry_info->result();
$empty_qry_info->next_result();
foreach($empty_qry_result as $empty_rslt){
$loan_qry = 'select prime_loan_eligibilty_id as rslt_count from cw_loan_eligibility where based_type = "'.$column_name.'" and loan_code="'.$loan_code.'" and eligibility_name="'.$empty_rslt->eligibility_name.'" and trans_status = 1';
$loan_info = $this->db->query("CALL sp_a_run ('SELECT','$loan_qry')");
$loan_result = $loan_info->result();
$loan_info->next_result();
$rslt_count = $loan_result[0]->rslt_count;
if((int)$rslt_count === 0){
$prime_qry_key .= "trans_created_by,trans_created_date";
$prime_qry_value .= '"'.$this->logged_id.'",'.'"'.$created_on.'"';
$prime_qry_key = "based_type,loan_code,eligibility_name,gross_field,less_than_six,one_to_two,two_to_three,greater_than_three,number_of_loan";
$prime_qry_value = '"'.$column_name.'","'.$loan_code.'","'.$empty_rslt->eligibility_name.'","0","0","0","0","0","0"';
$entry_insert_qry = "insert into cw_loan_eligibility ($prime_qry_key) values ($prime_qry_value)";
$entry_insert_info = $this->db->query("CALL sp_a_run ('INSERT','$entry_insert_qry')");
$entry_insert_result = $entry_insert_info->result();
$entry_insert_info->next_result();
$message = "Successfully added your loan eligibility entry";
}else{
$message = "Data Already Exist";
}
}
$data = $this->get_list($based_col,$column_name,$based_type,$loan_code);
echo json_encode(array('success' => true, 'message' => $message,'data' => $data));
}
public function get_list($based_col,$column_name,$based_type,$loan_code){
if($column_name === 'category'){
$join_qry = ',prime_category_id,'.$based_type.'.category_name from cw_loan_eligibility inner join '.$based_type.' on '.$based_type.'.prime_'.$column_name.'_id = cw_loan_eligibility.eligibility_name';
}else{
$join_qry = ',prime_'.$based_col.'_id,'.$based_type.'.'.$column_name.' from cw_loan_eligibility inner join '.$based_type.' on '.$based_type.'.prime_'.$column_name.'_id = cw_loan_eligibility.eligibility_name';
}
$qry = 'select prime_loan_eligibilty_id, gross_field,less_than_six,one_to_two,two_to_three,greater_than_three,number_of_loan'.$join_qry.' where based_type = "'.$column_name.'" and loan_code="'.$loan_code.'" and cw_loan_eligibility.trans_status = 1';
$qry_info = $this->db->query("CALL sp_a_run ('SELECT','$qry')");
$qry_result = $qry_info->result();
$qry_info->next_result();
$tr_line = "";
$prime_based_col_id = "prime_".$based_col."_id";
foreach($qry_result as $rslt){
if($column_name === 'category'){
$based_column_name = $rslt->category_name;
}else{
$based_column_name = $rslt->$column_name;
}
$prime_based_col_id = $rslt->prime_."$based_col"."_id";
$eligibility_id = $rslt->prime_loan_eligibilty_id;
$eligibility_name = $rslt->eligibility_name;
$gross_field = $rslt->gross_field;
$less_than_six = $rslt->less_than_six;
$one_to_two = $rslt->one_to_two;
$two_to_three = $rslt->two_to_three;
$greater_than_three = $rslt->greater_than_three;
$number_of_loan = $rslt->number_of_loan;
$prime_id = $prime_."$column_name"."_id";
$td = "<td><input type='hidden' id='$based_col_$prime_id' name='".$column_name."[]' value='$prime_id'/>$based_column_name</td>";
$tr_line .= "<tr>$td<td><input type='text' id='gross_field' name='gross_field[]' value='$gross_field' class='number' onchange=update_data('$eligibility_id',this.value,'gross_field'); /></td>
<td><input type='text' id='less_than_six' name='less_than_six[]' value='$less_than_six' class='number' onchange=update_data('$eligibility_id',this.value,'less_than_six'); /></td>
<td><input type='text' id='one_to_two' name='one_to_two[]' value='$one_to_two' class='number' onchange=update_data('$eligibility_id',this.value,'one_to_two'); /></td>
<td><input type='text' id='two_to_three' name='two_to_three[]' value='$two_to_three' class='number' onchange=update_data('$eligibility_id',this.value,'two_to_three'); /></td>
<td><input type='text' id='greater_than_three' name='greater_than_three[]' value='$greater_than_three' class='number' onchange=update_data('$eligibility_id',this.value,'greater_than_three'); /></td>
<td><input type='text' id='number_of_loan' name='number_of_loan[]' value='$number_of_loan' class='number' onchange=update_data('$eligibility_id',this.value,'number_of_loan'); /></td></tr>";
}
$table_info = "<table class='table table-bordered'>
<thead>
<tr>
<th>Eligibility based on</th>
<th>Gross Field(CTC)</th>
<th> Below 6 Months</th>
<th>1 to 2 Years</th>
<th>2 to 3 Years</th>
<th> Above 3 Years</th>
<th>Number Of Loan</th>
</tr>
</thead>
<tbody>
$tr_line
</tbody>
</table>";
return $table_info;
}
public function get_table_data(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
$based_qry = 'select prime_loan_eligibility_settings_id,eligibility_based_on from cw_loan_eligibility_settings where cw_loan_eligibility_settings.trans_status = 1';
$based_qry_info = $this->db->query("CALL sp_a_run ('SELECT','$based_qry')");
$based_qry_result = $based_qry_info->result();
$based_qry_info->next_result();
if(count($based_qry_result) === 0){
echo json_encode(array('success' => false,'message' => 'Loan Eligibility Settings not done..','table_data' => ""));
exit(0);
}
$based_type = $based_qry_result[0]->eligibility_based_on;
$based_col = str_replace("cw_","",$based_qry_result[0]->eligibility_based_on);
$based_col_qry = 'select label_name from cw_form_setting where prime_module_id = "'.$based_col.'" and trans_status = 1';
$based_col_info = $this->db->query("CALL sp_a_run ('SELECT','$based_col_qry')");
$based_col_result = $based_col_info->result();
$based_col_info->next_result();
$column_name = $based_col_result[0]->label_name;
$loan_code = $this->input->post('loan_code');
if($column_name === 'category_name'){
$join_qry = ',prime_category_id,'.$based_type.'.category_name from cw_loan_eligibility inner join '.$based_type.' on '.$based_type.'.prime_category_id = cw_loan_eligibility.eligibility_name';
}else{
$join_qry = ',prime_'.$based_col.'_id,'.$based_type.'.'.$column_name.' from cw_loan_eligibility inner join '.$based_type.' on '.$based_type.'.prime_'.$column_name.'_id = cw_loan_eligibility.eligibility_name';
}
$qry = 'select prime_loan_eligibilty_id, gross_field,less_than_six,one_to_two,two_to_three,greater_than_three,number_of_loan'.$join_qry.' where based_type = "'.$column_name.'" and loan_code="'.$loan_code.'" and cw_loan_eligibility.trans_status = 1';
$qry_info = $this->db->query("CALL sp_a_run ('SELECT','$qry')");
$qry_result = $qry_info->result();
$qry_info->next_result();
$tr_line = "";
$prime_based_col_id = "prime_".$based_col."_id";
foreach($qry_result as $rslt){
if($column_name === 'category'){
$based_column_name = $rslt->category_name;
}else{
$based_column_name = $rslt->$column_name;
}
$prime_based_col_id = $rslt->prime_."$based_col"._id;
$eligibility_id = $rslt->prime_loan_eligibilty_id;
$eligibility_name = $rslt->eligibility_name;
$gross_field = $rslt->gross_field;
$less_than_six = $rslt->less_than_six;
$one_to_two = $rslt->one_to_two;
$two_to_three = $rslt->two_to_three;
$greater_than_three = $rslt->greater_than_three;
$number_of_loan = $rslt->number_of_loan;
$prime_id = $prime_."$column_name"._id;
$td = "<td><input type='hidden' id='$based_col_$prime_id' name='".$column_name."[]' value='$prime_id'/>$based_column_name</td>";
$tr_line .= "<tr>$td<td><input type='text' id='gross_field' name='gross_field[]' value='0.00' class='number' /></td>
<td><input type='text' id='less_than_six' name='less_than_six[]' value='0' class='number'/></td>
<td><input type='text' id='one_to_two' name='one_to_two[]' value='0' class='number'/></td>
<td><input type='text' id='two_to_three' name='two_to_three[]' value='0' class='number' /></td>
<td><input type='text' id='greater_than_three' name='greater_than_three[]' value='0' class='number' /></td>
<td><input type='text' id='number_of_loan' name='number_of_loan[]' value='0' class='number' /></td></tr>";
}
$empty_table_info = "<table class='table table-bordered'>
<thead>
<tr>
<th>Eligibility based on</th>
<th>Gross Field(CTC)</th>
<th> Below 6 Months</th>
<th>1 to 2 Years</th>
<th>2 to 3 Years</th>
<th> Above 3 Years</th>
<th>Number Of Loan</th>
</tr>
</thead>
<tbody>
$tr_line
</tbody>
</table>";
echo json_encode(array('success' => true, 'data' => $empty_table_info));
}
public function update_data(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
$eligibility_id = (int)$this->input->post('eligibility_id');
$value = $this->input->post('value');
$field = $this->input->post('field');
$update_query = 'UPDATE cw_loan_eligibility SET '. $field .'="'.$value.'" WHERE prime_loan_eligibilty_id ="'.$eligibility_id.'" and trans_status = 1';
$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
echo json_encode(array('success' => true, 'message' => 'Successfully updated!!!'));
}
}
?>