File: /home/cafsindia/hrms_cafsinfotech_in/application/controllers/Leave_encash_days.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Leave_encash_days extends Action_controller{
public function __construct(){
parent::__construct('leave_encash_days');
$this->control_name = 'leave_encash_days';
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
$db_name = $this->config->item("db_name");
$data['encKey'] = $this->generateKey();
//LEAVE OPENING YES COLUMNS ONLY GET(TABLE HEAD COLUMNS)
$prime_table_query = $this->db->query("SELECT leave_name FROM cw_leave_creation WHERE trans_status = 1 and leave_opening = 1 ");
$columns = $prime_table_query->result();
$select_columns = "";
foreach ($columns as $key => $value) {
$leave_name = rtrim(ltrim($value->leave_name," ")," ");
$leave_name = str_replace(" ","_",strtolower($leave_name));
if($leave_name){
$select_columns .= ",'encash_".$leave_name."'";
}
}
// $prime_table_query = $this->db->query("SELECT COLUMN_NAME AS label_name, COLUMN_KEY as key_exist FROM information_schema.COLUMNS WHERE TABLE_SCHEMA ='$db_name' and TABLE_NAME = 'cw_leave_encash_days' and COLUMN_NAME NOT IN ('trans_created_by','trans_created_date','trans_updated_by','trans_updated_date','trans_deleted_by','trans_deleted_date','trans_status','prime_leave_encash_days_id') ORDER BY ORDINAL_POSITION");
$prime_table_query = $this->db->query("SELECT COLUMN_NAME AS label_name, COLUMN_KEY as key_exist FROM information_schema.COLUMNS WHERE TABLE_SCHEMA ='$db_name' and TABLE_NAME = 'cw_leave_encash_days' and COLUMN_NAME IN ('financial_setting_id','component_value','employee_code'$select_columns) ORDER BY ORDINAL_POSITION");
$prime_table_col = $prime_table_query->result();
$data['table_head'] = $prime_table_col;
$data['module_id'] = $this->control_name;
/* Custom Code for Weekoff Component */
$component_query = 'SELECT pick_table,pick_list,components,label_name FROM cw_general_setting inner join cw_form_setting on cw_form_setting.prime_form_id = cw_general_setting.components WHERE entry_parameter_type = 3 and cw_general_setting.trans_status = 1';
$component_info = $this->db->query("CALL sp_a_run ('SELECT','$component_query')");
$component_result = $component_info->result();
$component_info->next_result();
$pick_label_name = ucwords($component_result[0]->label_name);
if($pick_label_name === "Role"){
$pick_label_name = "Category";
}
$pick_list = $component_result[0]->pick_list;
$pick_table = $component_result[0]->pick_table;
$components = $component_result[0]->components;
$pick_list_val = explode(",",$pick_list);
$pick_list_val_1 = $pick_list_val[0];
$pick_list_val_2 = $pick_list_val[1];
$pick_query = "select $pick_list from $pick_table where trans_status = 1";
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_data->next_result();
if($pick_result){
$pick_key = array_column($pick_result, $pick_list_val_1);
$pick_val = array_column($pick_result, $pick_list_val_2);
$final_pick = array_combine( $pick_key, $pick_val);
}
$data["component_value"]["component_value"] = array("" => "---- Select ----") + $final_pick;
$data['components'] = $components;
$data['components_arr'] = $this->time_setting_components();
$pick_label_name = lcfirst($pick_label_name);
$data['pick_label_name'] = $pick_label_name;
//GET A TEMPLATE NAME FOR IMPORT A LEAVE OPENING DETAILS
$temp_format_qry = 'select prime_leave_temp_setting_id,template_name from cw_leave_temp_setting where trans_status = 1 and module_id="'.$this->control_name.'"';
$temp_format = $this->db->query("CALL sp_a_run ('SELECT','$temp_format_qry')");
$temp_result = $temp_format->result();
$temp_format->next_result();
$temp_format_drop[""] = "---- Template Format ----";
foreach($temp_result as $template){
$temp_format_id = $template->prime_leave_temp_setting_id;
$template_name = $template->template_name;
$temp_format_drop[$temp_format_id] = $template_name;
}
$data['temp_format_drop'] = $temp_format_drop;
$data['financial_info'] = $this->get_leave_financial_details();
$data['key'] = $this->generateKey();
$this->load->view("$this->control_name/manage",$data);
}
//LOAD TABEL WITH FILTERS
public function search(){
$dec_data = $this->cryptoDecrypt($_POST['Payload']);
$_POST = $dec_data['data'];
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....'));
exit(0);
}
$draw = $this->input->post('draw');
$start = $this->input->post('start');
$per_page = $this->input->post('length');
$order = $this->input->post('order');
$order_col = $this->input->post('columns');
$search = $this->input->post('search');
$column = $order[0]['column'];
$order_sor = $order[0]['dir'];
$order_col = $order_col[$column]['data'];
$search = trim($search['value']);
$common_search = "";
if($search){
$common_search .= " and cw_leave_encash_days.employee_code like '%".$search."%'";
}
$search_total = $this->db->query("SELECT count(*) as allcount from cw_leave_encash_days ");
$search_total_info = $search_total->result();
$total_count = $search_total_info[0]->allcount;
$search_count = $this->db->query("SELECT count(*) as allcount from cw_leave_encash_days where trans_status = 1 $common_search");
$search_info = $search_count->result();
$filtered_count = $search_info[0]->allcount;
$prime_table_query = $this->db->query("SELECT leave_name FROM cw_leave_creation WHERE trans_status = 1 and leave_opening = 1 ");
$columns = $prime_table_query->result();
$select_columns = "";
foreach ($columns as $key => $value) {
$leave_name = rtrim(ltrim($value->leave_name," ")," ");
$leave_name = str_replace(" ","_",strtolower($leave_name));
if($leave_name){
$select_columns .= ",encash_".$leave_name;
}
}
$select_columns = rtrim($select_columns,",");
/* Custom Code for Weekoff Component */
$component_query = 'SELECT pick_table,pick_list,components,label_name FROM cw_general_setting inner join cw_form_setting on cw_form_setting.prime_form_id = cw_general_setting.components WHERE entry_parameter_type = 3 and cw_general_setting.trans_status = 1 ';
$component_info = $this->db->query("CALL sp_a_run ('SELECT','$component_query')");
$component_result = $component_info->result();
$component_info->next_result();
$pick_list = $component_result[0]->pick_list;
$pick_table = $component_result[0]->pick_table;
$components = $component_result[0]->components;
$pick_label_name = $component_result[0]->label_name;
if($pick_label_name === "role"){
$pick_label_name = "category_name";
}
$pick_list_val = explode(",",$pick_list);
$pick_list_val_1 = $pick_list_val[0];
$pick_list_val_2 = $pick_list_val[1];
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$financial_setting_date = "cw_leave_financial_year.starting_date"."-"." cw_leave_financial_year.ending_date";
$search_query .= "SELECT cw_leave_encash_days.prime_leave_encash_days_id,employee_code,concat(DATE_FORMAT(cw_leave_financial_year.starting_date, '%d-%m-%Y'),' - ',DATE_FORMAT(cw_leave_financial_year.ending_date, '%d-%m-%Y')) as financial_setting_id,component_value ".$select_columns." from cw_leave_encash_days inner join $pick_table on $pick_table.$pick_list_val_1 = cw_leave_encash_days.component_value inner join cw_leave_financial_year on cw_leave_financial_year.prime_leave_financial_year_id = cw_leave_encash_days.financial_setting_id where cw_leave_encash_days.trans_status = 1";
$search_query .= "$common_search ORDER BY $order_col $order_sor";
if((int)$per_page !== -1){
$search_query .= " LIMIT $start,$per_page";
}
$search_data = $this->db->query($search_query);
$search_result = $search_data->result();
echo json_encode(array("draw" => intval($draw),"recordsTotal" => $total_count,"recordsFiltered" => $filtered_count,"data" => $search_result));
}
//LOAD MODEL PAGE VIEW WITH DATA
public function view($form_view_id=-1){
//VIEW INFO FUNCTION
$this->view_info($form_view_id);
$data['module_sts'] = (int)$this->module_sts;
//VIEW, FORM INPUT
$data['view_info'] = $this->view_info;
$data['form_info'] = $this->form_info;
$data['formula_result'] = $this->formula_result;//for label color change
$data['role_based_condition'] = $this->role_based_condition;//for label name readonly options
//DEPENDENT AUTO PICKLIST
$data['auto_pick'] = $this->depen_auto_list;//dependent auto result
$data['condition_list'] = $this->form_condition_list;
//VIEW DATA
$base_query = str_replace("@SELECT@",$this->view_select,$this->base_query);
$form_view_qry = $base_query .' where '.$this->prime_table.'.'.$this->prime_id.' = '.$form_view_id.' and '.$this->prime_table.'.trans_status = 1';
$row_view_qry = 'select * from cw_form_view_setting where prime_view_module_id = "'.$this->control_name.'" and form_view_type = "3" and trans_status = 1';
$view_pro_qry = [];
$view_pro_qry[] = array("return"=>"form_view","qry"=>$form_view_qry);
$view_pro_qry[] = array("return"=>"row_view_list","qry"=>$row_view_qry);
$view_info_rslt = $this->run_multi_qry($view_pro_qry);
$form_view_rslt = $view_info_rslt->rslt->form_view[0];
$row_view_rslt = $view_info_rslt->rslt->row_view_list;
$data['form_view'] = $form_view_rslt;
$row_view_list = array();
foreach($row_view_rslt as $view){
$prime_form_view_id = $view->prime_form_view_id;
$row_set_data = $this->get_row_set_data($prime_form_view_id,$form_view_id);
$row_view_list[$prime_form_view_id] = $row_set_data;
}
$data['row_view_list'] = $row_view_list;
//FOR DEPENDENT
$data['get_depend_prime_id'] = $this->get_depend_fun();
$data['depend_label_id'] = $this->depend_label_fun();
$this->load->view("$this->control_name/form",$data);
}
//SAVE MODEL DATA TO DATA BASE
public function save(){
//SAVE INFO FUNCTION
$this->save_info();
$module_sts = (int)$this->module_sts;
if(!$module_sts){
echo json_encode(array('success' => FALSE, 'message' => "Save Info Query Process Error...!"));
exit(0);
}else{
$unq_chk = array();
$prime_qry_key = "";
$prime_qry_value = "";
$prime_upd_query = "";
$cf_qry_key = "";
$cf_qry_value = "";
$cf_upd_query = "";
$cf_has = false;
$form_id = (int)$this->input->post($this->prime_id);
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$form_post_data = array();
foreach($this->form_info as $setting){
$field_type = $setting->field_type;
$input_view_type = (int)$setting->input_view_type;
$label_id = strtolower(str_replace(" ","_",$setting->label_name));
$field_isdefault = $setting->field_isdefault;
$unique_field = (int)$setting->unique_field;
$view_name = $setting->view_name;
$date_type = $setting->date_type;
if((int)$field_type === 7){
$multi_name = $label_id."[]";
$value = trim(implode(",",$this->input->post($multi_name) ?? []));
}else{
$value = trim($this->input->post($label_id));
}
if((int)$field_type === 4){
if((int)$date_type === 1){
$value = date('Y-m-d',strtotime($value));
}else{
$value = $value;
}
}else
if((int)$field_type === 13){
$value = date('Y-m-d H:i:s',strtotime($value));
}else
if((int)$field_type === 10){//File Upload
$value = $value;
$remove_upload_fname = "old_".$label_id;
$remove_upload_file = $this->input->post($remove_upload_fname);
if($value !== $remove_upload_file){
unlink($remove_upload_file);
}
}
//For replace special character
$value = str_replace('"',"~",$value);
$value = str_replace("'","`",$value);
//Sanitize SQL InJection chars
$value = $this->sanitize_input($value, $field_type);
if(($input_view_type === 1) || ($input_view_type === 2)){
if((int)$field_isdefault === 1){
$prime_qry_key .= $label_id.",";
$prime_qry_value .= '"'.$value.'",';
$prime_upd_query .= $label_id.' = "'.$value.'",';
if($unique_field === 1){
$prime_unq_chk = $label_id.'= "'.$value.'"';
$query = "select count(*) as rslt_count from $this->prime_table where $this->prime_id != $form_id and $prime_unq_chk";
$unq_chk[] = array('label_id'=>$label_id,'view_name'=>$view_name,'query'=>$query,);
}
}
}
}
$rslt_count = 0;
$can_process = array();
foreach($unq_chk as $unq_rslt){
$query = $unq_rslt['query'];
$label_id = $unq_rslt['label_id'];
$view_name = $unq_rslt['view_name'];
$unq_info = $this->db->query("CALL sp_a_run ('RUN','$query')");
$unq_result = $unq_info->result();
$unq_info->next_result();
if($unq_result){
$rslt_count = (int)$unq_result[0]->rslt_count;
if($rslt_count !== 0){
$can_process[] = $view_name." already exist";
}
}
}
if(count($can_process) > 0){
$can_process = array_values($can_process);
$can_process = implode(",<br/>", $can_process);
echo json_encode(array('success' => false, 'message' => $can_process,));
}else{
$created_on = date("Y-m-d h:i:s");
if((int)$form_id === 0){
$prime_qry_key .= "financial_setting_id,trans_created_by,trans_created_date";
$prime_qry_value .= '"'.$this->logged_id.'",'.'"'.$this->logged_id.'",'.'"'.$created_on.'"';
$prime_insert_query = "insert into $this->prime_table ($prime_qry_key) values ($prime_qry_value)";
$insert_info = $this->db->query("CALL sp_a_run ('INSERT','$prime_insert_query')");
$insert_result = $insert_info->result();
$insert_info->next_result();
$insert_id = $insert_result[0]->ins_id;
echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id));
}else{
$prime_upd_query .= 'financial_setting_id = "'. $this->logged_id .'",trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
$prime_update_query = 'UPDATE '. $this->prime_table .' SET '. $prime_upd_query .' WHERE '. $this->prime_id .' = "'. $form_id .'"';
$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $form_id));
}
}
}
}
//UPDATE STATUS TO DELETE IN MODULE PRIMARY TABLE
public function delete(){
//Encryption
$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);
}
$delete_ids = implode(",",$this->input->post('delete_ids') ?? []);
$can_process = TRUE;
$delete_status = FALSE;
if($this->check_delete_status()){
$delete_status = TRUE;
$check_table_query = 'SELECT GROUP_CONCAT(prime_module_id) as prime_module_id,GROUP_CONCAT(label_name) as label_name from cw_form_setting WHERE pick_table = "'. $this->prime_table .'" and trans_status = 1 ';
$check_table_info = $this->db->query("CALL sp_a_run ('SELECT','$check_table_query')");
$check_table_rlst = $check_table_info->row();
$check_table_info->next_result();
if($check_table_rlst->prime_module_id){
$prime_module_id = explode(",",$check_table_rlst->prime_module_id);
$label_name = explode(",",$check_table_rlst->label_name);
$i = 0;
foreach($prime_module_id as $check_modules){
$table_name = "cw_".$check_modules;
$select_table = "$table_name.$label_name[$i]";
$select_where = " and $table_name.$label_name[$i] in ($delete_ids)";
$check_module_query = 'SELECT '.$select_table.' from '.$table_name.' WHERE '.$table_name.'.trans_status = 1 '.$select_where.' LIMIT 0,1';
$check_module_info = $this->db->query("CALL sp_a_run ('SELECT','$check_module_query')");
$values_count = $check_module_info->num_rows();
$check_module_info->next_result();
if((int)$values_count > 0){
$table_names .= "$check_modules,";
$can_process = False;
$delete_status = False;
}
$i++;
}
}
$table_names = rtrim($table_names,",");
if($delete_status){
$delete_query = 'DELETE FROM '. $this->prime_table .' WHERE '. $this->prime_id .' in ('. $delete_ids .')';
if($this->db->query("CALL sp_a_run ('RUN','$delete_query')")){
$row_set_query = 'SELECT form_view_label_name from cw_form_view_setting where form_view_type = "3" and prime_view_module_id = "'. $this->control_name .'" and trans_status = 1';
$row_set_info = $this->db->query("CALL sp_a_run ('SELECT','$row_set_query')");
$row_count = (int)$row_set_info->num_rows();
$row_set_info->next_result();
if($row_count !== 0){
$row_set_result = $row_set_info->result();
$delete_table_name = '';
$delete_table_condition = '';
foreach($row_set_result as $row_set){
$row_set_table_name = "cw_".$this->control_name."_".$row_set->form_view_label_name;
$delete_table_name .= "$row_set_table_name,";
$delete_table_condition .= " $row_set_table_name.$this->prime_id in ('$delete_ids') and";
}
$delete_table_name = rtrim($delete_table_name,',');
$delete_table_condition = rtrim($delete_table_condition,'and');
$delete_row_set_query = 'DELETE FROM '. $delete_table_name .' WHERE '. $delete_table_condition.'';
$this->db->query("CALL sp_a_run ('RUN','$delete_row_set_query')");
}
$can_process = False;
}
}
}
if($can_process){
$created_on = date("Y-m-d h:i:s");
$prime_upd_query .= 'trans_deleted_by = "'. $this->logged_id .'",trans_deleted_date = "'.$created_on.'"';
$prime_update_query = 'UPDATE '. $this->prime_table .' SET trans_status = 0,'. $prime_upd_query .' WHERE '. $this->prime_id .' in ('. $delete_ids .')';
if($this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')")){
echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to delete"));
}
}else
if($delete_status){
echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to delete, This value is already used in $table_names modules"));
}
}
//CHECK UNIQUE FIELD STATUS
public function check_delete_status(){
$check_delete_query = 'SELECT GROUP_CONCAT(unique_field) as unique_field from cw_form_setting WHERE prime_module_id = "'. $this->control_name .'" and trans_status = 1 ';
$check_delete_info = $this->db->query("CALL sp_a_run ('SELECT','$check_delete_query')");
$check_delete_rlst = $check_delete_info->row();
$check_delete_info->next_result();
$unique_info = explode(",",$check_delete_rlst->unique_field);
if(in_array('1', $unique_info ?? [])){
return TRUE;
}else{
return FALSE;
}
}
//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
public function remove_file(){
//Encryption
$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);
}
$prime_id_val = $this->input->post('prime_id_val');
$is_defult = (int)$this->input->post('is_defult');
$input_name = $this->input->post('input_name');
$table_name = '';
if($is_defult === 1){
$table_name = $this->prime_table;
}else
if($is_defult === 2){
$table_name = $this->cf_table;
}
if($table_name){
$created_on = date("Y-m-d h:i:s");
$set_query = $input_name .' = "" ,trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
$update_query = 'UPDATE '.$table_name .' SET '. $set_query .' WHERE '. $this->prime_id .' = "'. $prime_id_val .'"';
$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
echo json_encode(array('success' => TRUE, 'message' => "Successfully updated"));
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
}
}
//Import function start
public function save_leave_import(){
$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);
}
$components = $this->input->post('components');
$component_value = $this->input->post('component_value');
$template_name = $this->input->post('template_name');
$excel_file_path = $this->input->post('excel_file_path');
$excel_sheet_name = $this->input->post('excel_sheet_name');
$excel_start_row = $this->input->post('excel_start_row');
$excel_end_row = $this->input->post('excel_end_row');
$logged_id = $this->session->userdata('logged_id');
$today_date = date("Y-m-d H:i:s");
$import_query = 'insert into cw_leave_import (components,component_value,template_name,excel_file_path,excel_sheet_name,excel_start_row,excel_end_row,trans_created_by,trans_created_date) value ("'.$components.'","'.$component_value.'","'.$template_name.'","'.$excel_file_path.'","'.$excel_sheet_name.'","'.$excel_start_row.'","'.$excel_end_row.'","'.$logged_id.'","'.$today_date.'")';
$import_info = $this->db->query("CALL sp_a_run ('INSERT','$import_query')");
$import_result = $import_info->result();
$import_info->next_result();
$import_id = $import_result[0]->ins_id;
echo $this->do_excel_leave_import($import_id);
}
//import validation and checking updated
public function do_excel_leave_import($import_id){
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$logged_id = $this->session->userdata('logged_id');
$filename = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
include($filename);
if($import_id < 0){
echo json_encode(array('success' => false, 'message' => "Invalid file upload"));
}
$excel_path_qry = 'select * from cw_leave_import where import_id = "'.$import_id.'"';
$excel_path_info = $this->db->query("CALL sp_a_run ('SELECT','$excel_path_qry')");
$excel_path_result = $excel_path_info->result();
$excel_path_info->next_result();
if(!$excel_path_result){
echo json_encode(array('success' => false, 'message' => "Invalid file upload"));
}else{
$components = $excel_path_result[0]->components;
$component_value = $excel_path_result[0]->component_value;
$excel_format = $excel_path_result[0]->template_name;
$excel_file_path = $excel_path_result[0]->excel_file_path;
$excel_sheet_name = (int)$excel_path_result[0]->excel_sheet_name;
$excel_row_start = (int)$excel_path_result[0]->excel_start_row;
$excel_row_end = (int)$excel_path_result[0]->excel_end_row;
if($components === "category"){
$field_name = "role";
}else{
$field_name = $components;
}
$format_qry = 'select employee_code,GROUP_CONCAT(column_name) as column_name,
GROUP_CONCAT(column_map) as column_map from cw_leave_opening_template where temp_name = "'.$excel_format.'" and column_map != "" and trans_status = 1';
$format_info = $this->db->query("CALL sp_a_run ('SELECT','$format_qry')");
$format_rslt = $format_info->result();
$format_info->next_result();
$employee_code = $format_rslt[0]->employee_code;
$column_name = explode(",",$format_rslt[0]->column_name);
$column_map = explode(",",$format_rslt[0]->column_map);
$column_value = array_change_key_case(array_combine($column_name,$column_map),CASE_LOWER);
/* Array Build START */
$emp_exist_query = 'select employee_code,termination_status from cw_employees where trans_status = 1 and '.$field_name.'="'.$component_value.'"';
$emp_exist_info = $this->db->query("CALL sp_a_run ('RUN','$emp_exist_query')");
// echo $emp_exist_query;
$emp_exist_result = $emp_exist_info->result_array();
$emp_exist_info->next_result();
$emp_exist_result = array_reduce($emp_exist_result, function ($result, $arr) {
$result[$arr['employee_code']] = $arr;
return $result;
}, array());
//Check Data Exist in Leave Entry
$leave_exist_query = 'select count(*) as leave_count from cw_leave_entry where trans_status = 1 and financial_setting_id = "'.$prime_financial_id.'"';
$leave_exist_info = $this->db->query("CALL sp_a_run ('RUN','$leave_exist_query')");
$leave_exist_result = $leave_exist_info->result();
$leave_exist_info->next_result();
$leave_entry_count = (int)$leave_exist_result[0]->leave_count;
$prime_table_query = $this->db->query("SELECT leave_name FROM cw_leave_creation WHERE trans_status = 1 and leave_opening = 1 ");
$columns = $prime_table_query->result();
$select_columns = "";
foreach ($columns as $key => $value) {
$leave_name = strtolower($value->leave_name);
if($leave_name){
$select_columns .= ",((".$leave_name."+".$leave_name."_credit) - (".$leave_name."_debit + used_".$leave_name." + pending_".$leave_name.")) as encash_".$leave_name;
}
}
$balance_leave_count_qry = 'select employee_code'.$select_columns.' from cw_leave_opening where trans_status = 1 and financial_setting_id = "'.$prime_financial_id.'"';
$balance_leave_count_info = $this->db->query("CALL sp_a_run ('RUN','$balance_leave_count_qry')");
$balance_leave_count_rslt = $balance_leave_count_info->result_array();
$balance_leave_count_info->next_result();
$balance_leave_count_rslt = array_reduce($balance_leave_count_rslt, function ($result, $arr) {
$result[$arr['employee_code']] = $arr;
return $result;
}, array());
/* Array Build END */
if(!$format_rslt){
echo json_encode(array('success' => false, 'message' => "Please add excel format before import"));
}else{
try{
$excel_obj = \PhpOffice\PhpSpreadsheet\IOFactory::load($excel_file_path);
}catch(Exception $e){
die('Error loading file "' . pathinfo($excel_file_path, PATHINFO_BASENAME). '": ' . $e->getMessage());
echo json_encode(array('success' => false, 'message' => "Invalid file or path"));
}
$sheet = $excel_obj->getSheet($excel_sheet_name);
if($excel_row_end){
$total_rows = $excel_row_end;
}else{
$total_rows = $sheet->getHighestRow();
}
$created_on = date("Y-m-d h:i:s");
$primary_column_key = "";
$primary_column_val = "";
$status_array = array();
$error_array = array();
//DELETE QUERY FOR CURRENT FINANCIAL YEAR LEAVE
$delete_query = 'DELETE from cw_leave_encash_days where trans_status = 1 and financial_setting_id = "'.$prime_financial_id.'"';
$delete_info = $this->db->query("CALL sp_a_run ('RUN','$delete_query')");
$select_query = 'select count(*) as count from cw_leave_encash_days';
$select_info = $this->db->query("CALL sp_a_run ('RUN','$select_query')");
$select_result = $select_info->result();
$select_info->next_result();
$total_table_count = $select_result[0]->count + 1;
$truncate_table = 'ALTER TABLE cw_leave_encash_days AUTO_INCREMENT = '.$total_table_count.'';
$truncate_info = $this->db->query("CALL sp_a_run ('RUN','$truncate_table')");
for($row = $excel_row_start; $row <= $total_rows; $row++){ //validation purpose
$employee_code_val = trim($sheet->getCell("$employee_code$row")->getCalculatedValue());
if(!$emp_exist_result[$employee_code_val]['employee_code']){
$error_array["$employee_code$row"] = "Employee Code Not Present in this $components";
}else
if((int)$emp_exist_result[$employee_code_val]['termination_status'] === 1){
$error_array["$employee_code$row"] = "Already Resigned Employee";
}
foreach($column_value as $column_name_key => $column_name_val){
$column_name_value = trim($sheet->getCell("$column_name_val$row")->getCalculatedValue());
if($balance_leave_count_rslt[$employee_code_val][$column_name_key] < $column_name_value){
$column_name_key = strtolower(str_replace("_"," ",$column_name_key));
$error_array["$employee_code$row$column_name_val"] = "Employee $column_name_key Leave Count was not Correct!!...";
}
}
}
$status_count_info = count($error_array);
if((int)$status_count_info > 0){
$table_info = $this->get_excel_import_error_sts($error_array);
echo json_encode(array('success'=>False,'message'=>"Please check your excel file",'table_info'=>$table_info));
exit(0);
}else{
for($row = $excel_row_start; $row <= $total_rows; $row++){
$employee_code_val = trim($sheet->getCell("$employee_code$row")->getCalculatedValue());
$primary_column_key = "component_value,employee_code, financial_setting_id,trans_created_by,trans_created_date,";
$primary_column_val = '"'.$component_value.'","'.$employee_code_val.'","'.$prime_financial_id.'","'.$this->logged_id.'","'.$created_on.'",';
$update_qry = "";
foreach($column_value as $column_name_key => $column_name_val){
$column_name_value = trim($sheet->getCell("$column_name_val$row")->getCalculatedValue());
$primary_column_key .= $column_name_key.",";
$primary_column_val .= '"'.$column_name_value.'",';
$update_qry .= $column_name_key.' = "'.$column_name_value.'",';
}
$primary_column_key = rtrim($primary_column_key,",");
$primary_column_val = rtrim($primary_column_val,",");
$update_qry .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.date("Y-m-d H:i:s").'"';
$prime_dec_ent_qry = "insert into cw_leave_encash_days ($primary_column_key) VALUES ($primary_column_val)";
$insert_info = $this->db->query("CALL sp_a_run ('RUN','$prime_dec_ent_qry')");
//UPDATE A ENCASH DAYS COUNT TO LEAVE OPENING TABLE
$prime_opening_upd_query = 'UPDATE cw_leave_opening SET '.$update_qry.' WHERE financial_setting_id = "'. $prime_financial_id .'" and employee_code = "'. $employee_code_val .'"';
$this->db->query("CALL sp_a_run ('UPDATE','$prime_opening_upd_query')");
$sts = True;
$sts_message = "Successfully file imported";
$status_info['Status'] = "Inserted to DB";
$status_array[] = $status_info;
}
}
}
}
$table_info = $this->get_excel_import_sts($status_array);
echo json_encode(array('success'=>$sts,'message'=>$sts_message,'table_info'=>$table_info));
}
public function excel($template_name){
$excel_format_qry = 'select DISTINCT employee_code,column_name,column_map from cw_leave_opening_template where temp_name ="'.$template_name.'" and column_map != "" and trans_status = 1';
$excel_format = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
$excel_result = $excel_format->result();
$excel_format->next_result();
if(empty($excel_result)){
echo json_encode(array('success' => FALSE, 'output' => "Template settings not Mapped"));
exit(0);
}
// require_once APPPATH."/controllers/php_excel/PHPExcel.php";;
$obj = new Spreadsheet();
//Set the first row as the header row
foreach($excel_result as $excel){
$excel_employee_code = $excel->employee_code;
$excel_line_column_name = strtolower(str_replace("_"," ",$excel->column_name));
$excel_line_value = $excel->column_map;
//echo "BSK "
$obj->getActiveSheet()->setCellValue($excel_line_value."1", $excel_line_column_name);
}
$obj->getActiveSheet()->setCellValue($excel_employee_code."1", "Employee Code");
ob_end_clean();
// Rename worksheet name
$filename= $this->control_name."_".$template_name.".xls"; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
ob_end_clean();
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($obj, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
echo json_encode(array('success' => TRUE, 'output' => $excelOutput));
}
//import upload success table view
public function get_excel_import_sts($status_array){
$table_info = "";
$th_line = "";
$tr_line = "";
$count = 0;
foreach($status_array as $status){
$count++;
$status_array_count = count($status);
$status_count = 0;
foreach($status as $key => $value){
$status_count++;
if((int)$count === 1){
$th_line .= "<th style='text-align:center !important;'>Si.No</th><th style='text-align:center !important;'>Column Status</th>";
}
$td_line .= "<td>$count</td><td>$value</td>";
if((int)$status_count === (int)$status_array_count){
$color = "style='color:#15da15 !important;'";
if($value === "Already Exist in DB"){
$color = "style='color:#ff0303 !important;'";
}
$tr_line .= "<tr $color>$td_line</tr>";
$td_line = "";
}
}
}
if($th_line !== ""){
$table_info = "<table class='table table-bordered' style='text-align:center;'>
<thead>
<tr>
$th_line
</tr>
</thead>
<tbody>
$tr_line
</tbody>
</table>";
}
return $table_info;
}
//import error table view
public function get_excel_import_error_sts($error_array){
$table_info = "";
$th_line = "";
$tr_line = "";
$count = 0;
foreach($error_array as $key => $value){
$th_line = "<th style='text-align:center !important;'>Column Name</th><th style='text-align:center !important;'>Column Status</th>";
$tr_line .= "<tr style='color:#ff0303 !important;'><td>$key</td><td>$value</td></tr>";
}
if($th_line !== ""){
$table_info = "<table class='table table-bordered' style='text-align:center;'>
<thead>
<tr>
$th_line
</tr>
</thead>
<tbody>
$tr_line
</tbody>
</table>";
}
return $table_info;
}
}
?>