File: /home/cafsindia/hrms_cafsinfotech_in/OLD/application_bk/controllers/Manual_punch_entry.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Manual_punch_entry extends Action_controller{
public function __construct(){
parent::__construct('manual_punch_entry');
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
$data['encKey'] = $this->generateKey();
$this->page_info();
$data['module_sts'] = (int)$this->module_sts;
$data['quick_link'] = $this->quick_link;
$data['pick_list'] = $this->pick_list;
$data['form_info'] = $this->form_info;
$data['table_head'] = $this->table_head;
$data['fliter_list'] = $this->fliter_list;
$data['freeze_list'] = $this->freeze_list;
$data['components']['weekoff_component'] = $this->time_setting_components();
/* Custom Code for Component value and Weekoff Component */
$component_query = 'SELECT pick_table,pick_list,components 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_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);
}
$this->pick_list['component_value']['array_list'] = array("" => "---- Component Value----") + $final_pick;
$data['master_pick'] = $this->pick_list;
$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' => 'Invalid Request..'));
exit(0);
}
$this->search_info();
$module_sts = (int)$this->module_sts;
if(!$module_sts){
echo json_encode(array('success' => FALSE, 'message' => "Search Info Query Process Error..!"));
exit(0);
}else{
$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']);
$search_query = str_replace("@SELECT@",$this->select_query,$this->base_query);
$search_result = array();
//ADDED BASIC,FILTER,COMMON QUERY HERE
$role_condition = "";
if($this->role_condition){
$role_condition = $this->role_condition;
}
//CURRENT FINANCIAL YEAR ID
$leave_financial_info = $this->get_leave_financial_details();
$prime_financial_id = $leave_financial_info[0]->prime_leave_financial_year_id;
$fliter_query = "";
foreach($this->fliter_list as $fliter){
$label_id = $fliter->label_name;
$field_isdefault = (int)$fliter->field_isdefault;
$field_type = (int)$fliter->field_type;
$prime_form_id = $fliter->prime_form_id;
if($field_isdefault === 1){
$column_name = $this->prime_table .".$label_id";
$search_val = $this->input->post("$label_id");
if($search_val){
if($field_type === 4){
$search_val = date('Y-m-d',strtotime($search_val));
$fliter_query .= ' and '.$column_name.' = "'.$search_val.'"';
}else
if(($field_type === 5) || ($field_type === 7)){
$search_val = trim(implode('","',$search_val));
$fliter_query .= ' and '.$column_name.' in ("'.$search_val.'")';
}else
if($field_type === 9){
$search_id = 'filter_'.$label_id.'_hidden_'.$prime_form_id;
$search_val = $this->input->post("$search_id");
$fliter_query .= ' and '.$column_name.' in ("'.$search_val.'")';
}else
if($field_type === 13){
$search_val = date('Y-m-d H:i:s',strtotime($search_val));
$fliter_query .= ' and '.$column_name.' = "'.$search_val.'"';
}else{
$fliter_query .= ' and '.$column_name.' LIKE "'.$search_val.'%"';
}
}
}
}
$common_search = "";
if($search){
foreach($this->table_head as $setting){
$prime_form_id = $setting->prime_form_id;
$field_type = (int)$setting->field_type;
$pick_list = $setting->pick_list;
$pick_table = $setting->pick_table;
$pick_list_type = $setting->pick_list_type;
$input_view_type = (int)$setting->input_view_type;
$auto_prime_id = $setting->auto_prime_id;
$auto_dispaly_value = $setting->auto_dispaly_value;
$label_id = strtolower(str_replace(" ","_",$setting->label_name));
$field_isdefault = (int)$setting->field_isdefault;
if($field_isdefault === 1){
if(($input_view_type === 1) || ($input_view_type === 2)){
$search_label = "$this->prime_table.$label_id";
$search_val = "";
if($field_type === 4){ // having issues in date search
if(strtotime($search)){
$search_val = date('Y-m-d',strtotime($search));
$common_search .= ' or '. $search_label .' like "%'.$search_val.'%"';
}
}else
if(($field_type === 5) || ($field_type === 7) || ($field_type === 9)){
$result = array_filter($this->pick_list[$label_id]['array_list'] ?? [], function ($item) use ($search) {
if (stripos($item, $search) !== false) {
return true;
}
return false;
});
if($result){
$pick_key = implode('", "', array_keys($result ?? []) ?? []);
$common_search .= ' or '. $search_label .' in("'.$pick_key.'")';
}
}else{
$common_search .= ' or '. $search_label .' like "%'.$search.'%"';
}
}
}
}
if($common_search){
$common_search = ltrim($common_search,' or ');
$common_search = " and ($common_search)";
$common_search = str_replace("(,","(",$common_search);
$common_search = str_replace("()","(0)",$common_search);
}
}
$count_all_query = str_replace("@SELECT@","count(*) as allcount",$this->base_query);
$count_query = $count_all_query.' where '.$this->prime_table.'.trans_status = 1 AND financial_setting_id = '.$prime_financial_id.' '.$role_condition.$fliter_query.$common_search;
$search_query .= " where $this->prime_table.trans_status = 1 AND financial_setting_id = $prime_financial_id $role_condition $fliter_query $common_search";
$search_query .= " ORDER BY $order_col $order_sor";
if((int)$per_page !== -1){
$search_query .= " LIMIT $start,$per_page";
}
$search_pro_qry = [];
$search_pro_qry[] = array("return"=>"total_count","qry"=>$count_all_query);
$search_pro_qry[] = array("return"=>"filtered_count","qry"=>$count_query);
$search_pro_qry[] = array("return"=>"search_result","qry"=>$search_query);
$search_info_rslt = $this->run_multi_qry($search_pro_qry);
$total_count = $search_info_rslt->rslt->total_count[0]->allcount;
$filtered_count = $search_info_rslt->rslt->filtered_count[0]->allcount;
$search_result = json_decode(json_encode($search_info_rslt->rslt->search_result),true);
if($search_result === null || $search_result === ''){
$search_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){
$data['primeId'] = $form_view_id;
if($form_view_id !== -1){
//Decrypt prime id from URL
$decRslt = $this->cryptoDecrypt(base64_decode(urldecode($form_view_id)));
$form_view_id = $decRslt['prime_id'];
if(!$form_view_id){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
}
//VIEW INFO FUNCTION
$this->view_info($form_view_id);
$data['encKey'] = $this->generateKey();
$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;
/* Custom Code for Component value and Weekoff Component */
$component_query = 'SELECT pick_table,pick_list,components 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_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);
}
$this->pick_list[$components]['array_list'] = array("" => "---- $components ----") + $final_pick;
$data['weekoff_comp'] = $this->time_setting_components();
$data['all_pick'] = $this->pick_list;
$data['components'] = $components;
//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(){
//Encryption
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
//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 = "";
//Decrypt Form id
$form_id = $this->input->post($this->prime_id);
if($form_id !== '-1'){ //Decryption
$decRslt = $this->cryptoDecrypt(base64_decode(urldecode($form_id)));
$form_id = (int)$decRslt['prime_id'];
if(!$form_id){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
}else{
$form_id = 0;
}
$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 === 5){
$value = trim($this->input->post($label_id));
$pick_array[$label_id] = $value;
}else
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);
}
}
$value = str_replace('"',"xdbquot",$value);
$value = str_replace("'","xquot",$value);
$value = str_replace("&","xxamp",$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 .= "trans_created_by,trans_created_date";
$prime_qry_value .= '"'.$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 .= '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' => 'Invalid Request..','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;
}
}
}
$delete_insert_query = 'INSERT INTO cw_time_entry_dlt SELECT cw_time_entry.* FROM cw_time_entry inner join cw_manual_punch_entry on cw_manual_punch_entry.shift_date = cw_time_entry.att_date and cw_manual_punch_entry.employee_code = cw_time_entry.employee_code WHERE cw_time_entry.trans_status = 1 and prime_manual_punch_entry_id in ('.$delete_ids.') and prime_request_id = 0 and prime_direct_entry_id = 0';
$delete_insert_info = $this->db->query("CALL sp_a_run ('INSERT','$delete_insert_query')");
$delete_insert_rlst = $delete_insert_info->result();
$delete_insert_info->next_result();
if($delete_insert_rlst){
$delete_query = 'DELETE cw_time_entry FROM cw_time_entry inner join cw_manual_punch_entry on cw_manual_punch_entry.leave_date = cw_time_entry.att_date and cw_manual_punch_entry.employee_code = cw_time_entry.employee_code WHERE cw_time_entry.trans_status = 1 and prime_manual_punch_entry_id in ('.$delete_ids.')';
$delete_info = $this->db->query("CALL sp_a_run ('RUN','$delete_query')");
}else{
$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(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','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');
$input_val = $this->input->post('input_val');
if($input_val){
chmod($input_val, 0777);
unlink($input_val);
}
$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 FILE VIEW INFORMATION
public function import(){
$data['encKey'] = $this->generateKey();
$data['module_id'] = $this->control_name;
$excel_format_qry = 'select prime_excel_format_id,excel_name from cw_util_excel_format where excel_module_id = "'.$this->control_name.'" 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();
$excel_format_drop[""] = "---- Excel Format ----";
foreach($excel_result as $excel){
$prime_excel_format_id = $excel->prime_excel_format_id;
$excel_name = $excel->excel_name;
$excel_format_drop[$prime_excel_format_id] = $excel_name;
}
$data['excel_format_drop'] = $excel_format_drop;
$category_qry = 'SELECT prime_category_id,category_name FROM cw_category WHERE trans_status = 1 AND prime_category_id != 1 ';
$category_info = $this->db->query("CALL sp_a_run ('RUN','$category_qry')");
$category_result = $category_info->result();
$category_info->next_result();
$category_list[""] = "---- Category List----";
foreach($category_result as $excel){
$prime_id = $excel->prime_category_id;
$category_name = $excel->category_name;
$category_list[$prime_id] = $category_name;
}
$data['category_list'] = $category_list;
$this->load->view("$this->control_name/import",$data);
}
//process month set for import process
public function process_month_set(){
$category = $this->input->post('category');
//GET DEFAULT FINANCIAL YEAR
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$starting_date = date('Y-m-d',strtotime($financial_info[0]->starting_date));
$ending_date = date('Y-m-d',strtotime($financial_info[0]->ending_date));
//GET A EMPLOYEE CODE AND PROCESS MONTH FORM LEAVE EXPORT MODULE
$payroll_posting_query = 'SELECT role as category,date_format(MAX(str_to_date(CONCAT("01-", transactions_month), "%d-%m-%Y")) , "%m-%Y") AS process_month from cw_transactions where cw_transactions.role ="'.$category.'" and cw_transactions.trans_status = 1';
$payroll_posting_info = $this->db->query("CALL sp_a_run ('SELECT','$payroll_posting_query')");
$payroll_posting_result = $payroll_posting_info->result();
$payroll_posting_info->next_result();
$process_month = $payroll_posting_result[0]->process_month;
if(!$process_month){
$process_month = date("m-Y",strtotime($starting_date));
}else{
$process_month = date("m-Y",strtotime("+1 month",strtotime("01-".$process_month)));
}
echo json_encode(array('process_month' =>$process_month));
}
//IMPORT FUNCTION STARTS
public function save_import(){
$module_id = $this->input->post('module_id');
$import_type = $this->input->post('import_type');
$excel_format = $this->input->post('excel_format');
$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_import (import_type,module_id,excel_format,excel_file_path,excel_sheet_name,excel_start_row,excel_end_row,trans_created_by,trans_created_date) VALUES ("'.$import_type.'","'.$module_id.'","'.$excel_format.'","'.$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;
$category = $this->input->post('category');
$process_month = $this->input->post('process_month');
echo $this->do_excel_manual_punch_import($import_id,$category,$process_month);
}
//FOR MANUAL PUNCH IMPORT
public function do_excel_manual_punch_import($import_id,$category,$process_month){
$filename = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
include($filename);
if($import_id < 0){
return json_encode(array('success' => false, 'message' => "Invalid file upload"));
}
$excel_path_qry = 'SELECT * FROM cw_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){
return json_encode(array('success' => false, 'message' => "Invalid file upload"));
}else{
$excel_file_path = $excel_path_result[0]->excel_file_path;
$module_id = $excel_path_result[0]->module_id;
$excel_format = $excel_path_result[0]->excel_format;
$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;
$format_qry = 'SELECT * FROM cw_util_excel_format WHERE prime_excel_format_id = "'.$excel_format.'" AND cw_util_excel_format.trans_status = 1';
$format_info = $this->db->query("CALL sp_a_run ('SELECT','$format_qry')");
$format_rslt = $format_info->result();
$format_info->next_result();
if(!$format_rslt){
return json_encode(array('success' => false, 'message' => "Please add excel format before import"));
}else{
$exist_column_name = explode(",",$format_rslt[0]->exist_column_name);
$excel_format_qry = 'SELECT view_name,field_type,pick_table,pick_list_type,pick_list_import,pick_list,mandatory_field,field_isdefault,excel_line_column_name,excel_line_value FROM cw_util_excel_format_line INNER JOIN cw_form_setting ON label_name = excel_line_column_name WHERE excel_line_module_id = "'.$module_id.'" AND prime_excel_format_id = "'.$excel_format.'" AND cw_form_setting.prime_module_id = "'.$module_id.'" AND cw_util_excel_format_line.trans_status = 1 ORDER BY cw_util_excel_format_line.excel_line_value ASC';
$excel_format = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
$excel_format_result = $excel_format->result();
$excel_format->next_result();
if(!$excel_format_result){
return json_encode(array('success' => false, 'message' => "Please map excel cell column 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());
return 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();
}
//EMPLOYEES DATA
$emp_code_qry = 'SELECT employee_code,termination_status FROM cw_employees WHERE trans_status = 1';
$emp_code_data = $this->db->query("CALL sp_a_run ('SELECT','$emp_code_qry')");
$emp_code_data_result = $emp_code_data->result_array();
$emp_code_data->next_result();
$emp_code_data_result = array_map(function($v){
$return_array = array();
$return_array['termination_status'] = $v['termination_status'];
$return_array['employee_code'] = $v['employee_code'];
return $return_array;
}, $emp_code_data_result);
$emp_code_data_result = array_column($emp_code_data_result,'termination_status','employee_code');
//COMPONENT VALUE
$sel_comp_qry = 'SELECT cw_form_setting.label_name FROM cw_general_setting INNER JOIN cw_form_setting ON cw_general_setting.components = cw_form_setting.prime_form_id WHERE cw_general_setting.trans_status = 1 and cw_form_setting.trans_status = 1 and cw_general_setting.entry_parameter_type = 1';
$sel_comp_info = $this->db->query("CALL sp_a_run ('SELECT','$sel_comp_qry')");
$sel_comp_rslt = $sel_comp_info->result_array();
$sel_comp_info->next_result();
$component_col = $sel_comp_rslt[0]['label_name'];
if($component_col){
$sel_comp_col= ','.$component_col.' as component_value';
}
$emp_qry = 'SELECT employee_code'.$sel_comp_col.' FROM cw_employees where trans_status = 1';
$emp_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
$emp_result = $emp_info->result_array();
$emp_info->next_result();
$emp_result = array_map(function($v){
$return_array = array();
$return_array['component_value'] = $v['component_value'];
$return_array['employee_code'] = $v['employee_code'];
return $return_array;
}, $emp_result);
$emp_result = array_column($emp_result,'component_value','employee_code');
//VALIDATION FUNCTION STARTS -> NB[05-04-24]
foreach($excel_format_result as $excel_info){
$field_isdefault = (int)$excel_info->field_isdefault;
$mandatory_field = (int)$excel_info->mandatory_field;
$field_type = (int)$excel_info->field_type;
$excel_line_column_name = $excel_info->excel_line_column_name;
$excel_line_value = $excel_info->excel_line_value;
$view_name = $excel_info->view_name;
$pick_table = $excel_info->pick_table;
$pick_list_type = (int)$excel_info->pick_list_type;
$pick_list = $excel_info->pick_list;
$picklist_data = (int)$excel_info->picklist_data;
$pick_list_import = (int)$excel_info->pick_list_import;
$text_type = (int)$excel_info->text_type;
$date_type = (int)$excel_info->date_type;
$duplicate_data = (int)$excel_info->duplicate_data;
$field_length = (int)$excel_info->field_length;
$input_view_type = (int)$excel_info->input_view_type;
$common_multi_cell_value = $sheet->rangeToArray("$excel_line_value$excel_row_start:$excel_line_value$total_rows", NULL, TRUE, TRUE, TRUE);
$i = $excel_row_start;
foreach($common_multi_cell_value as $common_value){
foreach($common_value as $col_key =>$cell_value){
//REQUIRED VALIDATION
if(empty($cell_value) && $mandatory_field === 1){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = " - Columns are required and they are empty. Please check.!";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}elseif($excel_line_column_name === "employee_code"){
$employee_code = $cell_value; //FOR RESIGNED AND INACTIVE
if(!isset($emp_code_data_result[$cell_value])){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "[$cell_value] Not found in employee master. Please check.";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}elseif($emp_code_data_result[$cell_value] === "1"){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "[$cell_value] Already resigned. Please check.!";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}elseif(!$emp_result[$cell_value]){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "[$cell_value] component value is not mapped. Please check.!";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}
}elseif($field_type === 4){
if($date_type === 1){ //FOR DATE-MONTH-YEAR
$format = $sheet->getCell("$col_key$i")->getStyle()->getNumberFormat()->getFormatCode();
//Based on excel cell format converting to dd-mm-yyyy
if($format === "General" || $format === "@"){
$cell = $sheet->getCell("$col_key$i");
$excelDate = $cell->getFormattedValue();
}else
if($format === "m/d/yyyy" || $format === "[$-14009]dd/mm/yyyy" || $format === "[$-F800]dddd\,\ mmmm\ dd\,\ yyyy" || $format === "[$-14009]dd/mm/yyyy;@"){
$excelDate = trim(date('d-m-Y',PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell("$col_key$i")->getValue())));
}else{
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = " Invalid... Please check the format and try again.";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name . " " . $msg_line;
}
if($excelDate){
if($excelDate === "31-01-1900"){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "- Invalid Date. Please change it!";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}elseif((int)$this->validateDATE($excelDate) === 0){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "[$excelDate] - Invalid ... Please map the correct DATE format (DD-MM-YYYY)";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}
}
}
}elseif($field_type === 5 || $field_type === 7){ //FOR PICKLIST
if($pick_list_type === 1){ //from table
$pick_list_val = explode(",",$pick_list);
$pick_list_val_1 = $pick_list_val[0];
$pick_list_val_2 = $pick_list_val[1];
$pick_list_where = $pick_list_val_2.' = "'.$cell_value.'"';
if($pick_list_import === 1){
$pick_list_where = $pick_list_val_1.' = "'.$cell_value.'"';
}
//Checking if the value is present in the select table.
$pick_query = 'select '.$pick_list.' from '.$pick_table.' where '.$pick_list_where;
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_count = $pick_data->num_rows();
$pick_data->next_result();
if((int)$pick_count === 0){
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "- Invalid data[$cell_value] is present... Please check and try again.!";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}
}
}elseif($field_type === 15){ // FOR TIME
$cell = $sheet->getCell("$col_key$i");
$excelDateValue = $cell->getFormattedValue();
$excelDateValue = preg_replace('/(\b\d{1}\b)/', '0$1', $excelDateValue);
if(!empty($excelDateValue)){
if ((int)$this->validatetime($excelDateValue) === 0) {
$err_column_array['error']["$excel_line_value$i"] = $view_name;
$msg_line = "[$cell_value] - Invalid ... Please map the correct TIME format (HH:MM:SS)";
$err_column_tabview['error']["$excel_line_value$i"] = $view_name." ".$msg_line;
}
}
}
$i++;
}
}
}
$err_column_count = count($err_column_array['error']);
if((int)$err_column_count > 0){
$table_info = $this->get_excel_error_ui($err_column_tabview);
echo json_encode(array('success'=>false,'message'=>"Data Not Imported",'table_info'=>$table_info));
}else{
// IMPORT TO DB
$final_result = $this->final_excel_import($module_id,$sheet,$excel_format_result,$excel_row_start,$total_rows,$exist_column_name,$process_month);
}
}
}
}
}
//FINAL IMPORT
public function final_excel_import($module_id,$sheet,$excel_format_result,$excel_row_start,$total_rows,$exist_column_name,$process_month){
$insert_data = array();
$update_data = array();
$created_on = date("Y-m-d H:i:s");
//USER ID
$emp_qry = 'SELECT employee_code,device_code FROM cw_employees WHERE trans_status = 1';
$emp_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
$emp_rslt = $emp_info->result_array();
$emp_info->next_result();
$emp_rslt = array_map(function($v){
$return_array = array();
$return_array['device_code'] = $v['device_code'];
$return_array['employee_code'] = $v['employee_code'];
return $return_array;
}, $emp_rslt);
$emp_rslt = array_column($emp_rslt,'device_code','employee_code');
//SHIFT DATA
$shift_qry = 'SELECT employee_code,shift_date,shift_name FROM cw_shift_import WHERE trans_status = 1 ';
$shift_info = $this->db->query("CALL sp_a_run ('SELECT','$shift_qry')");
$shift_result = $shift_info->result();
$shift_info->next_result();
$shift_data = array();
foreach($shift_result as $shift){
$shift_data[$shift->employee_code][$shift->shift_date] = $shift->shift_name;
}
//MANUAL PUNCH ENTRY
$manual_qry = 'SELECT employee_code,in_date,out_date,in_time,out_time FROM cw_manual_punch_entry WHERE trans_status = 1 ';
$manual_info = $this->db->query("CALL sp_a_run ('SELECT','$shift_qry')");
$manual_result = $shift_info->result();
$manual_info->next_result();
$manual_in_date = array();
$manual_out_date = array();
foreach($manual_result as $manual){
$log_date_in = date('Y-m-d H:i:s', strtotime("$manual->in_date $manual->in_time"));
$log_date_out = date('Y-m-d H:i:s', strtotime("$manual->out_date $manual->out_time"));
$manual_in_date[$manual->employee_code] = $log_date_in;
$manual_out_date[$manual->employee_code] = $log_date_out;
}
//TIME ENTRY
$time_qry = 'SELECT employee_code,punch_in,punch_out,att_date FROM cw_time_entry WHERE trans_status = 1';
$time_info = $this->db->query("CALL sp_a_run ('SELECT','$time_qry')");
$time_rslt = $time_info->result();
$time_info->next_result();
$time_data = array();
foreach($time_rslt as $time){
$time_data[$time->employee_code][$time->att_date] = $time;
}
for($row = $excel_row_start; $row <= $total_rows; $row++){
$prime_column_val = "";
$prime_cell_val = "";
foreach($excel_format_result as $excel_info){
$label_id = $excel_info->label_name;
$module_id = $excel_info->prime_module_id;
$field_isdefault = (int)$excel_info->field_isdefault;
$mandatory_field = (int)$excel_info->mandatory_field;
$field_type = (int)$excel_info->field_type;
$pick_table = $excel_info->pick_table;
$pick_list_type = (int)$excel_info->pick_list_type;
$pick_list = $excel_info->pick_list;
$excel_line_column_name = $excel_info->excel_line_column_name;
$excel_line_value = $excel_info->excel_line_value;
$pick_list_import = (int)$excel_info->pick_list_import;
$get_cell_value = trim($sheet->getCell("$excel_line_value$row")->getCalculatedValue());
// FOR DATE
if($field_type === 4){
$get_cell_value = trim(date('Y-m-d',PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell("$excel_line_value$row")->getCalculatedValue())));
}
// FOR TIME
if($field_type === 15){
$cell = $sheet->getCell("$excel_line_value$row");
$get_cell_value = $cell->getFormattedValue();
}
if($excel_line_column_name === "mp_reason"){
$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 '.$pick_list_val_2.' = "'.$get_cell_value.'"';
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_count = $pick_data->num_rows();
$pick_data->next_result();
$mp_reason = $pick_result[0]->$pick_list_val_1;
}
if($field_isdefault === 1){
if($excel_line_column_name === "employee_code"){
$employee_code = $get_cell_value;
$user_id = $emp_rslt[$employee_code];
}
if($excel_line_column_name === "shift_date"){
$shift_date = $get_cell_value;
}
if($excel_line_column_name === "in_time"){
$in_time = $get_cell_value;
}
if($excel_line_column_name === "out_time"){
$out_time = $get_cell_value;
}
if($excel_line_column_name === "in_date"){
$in_date = $get_cell_value;
}
if($excel_line_column_name === "out_date"){
$out_date = $get_cell_value;
}
$prime_column_val .= $excel_line_column_name.",";
$prime_cell_val .= '"'.$get_cell_value.'",';
}
}
if($employee_code && $shift_date){
if(!isset($shift_data[$employee_code][$shift_date])){
$err_column_array['error']["$excel_line_value$row"] = $view_name;
$msg_line = "- shift not mapped.kindly verify!";
$err_column_tabview['error']["$excel_line_value$row"] = $view_name." ".$msg_line;
}
}
if($employee_code && $shift_date && $in_time && $out_time){
$log_date_in = date('Y-m-d H:i:s', strtotime("$shift_date $in_time"));
$log_date_out = date('Y-m-d H:i:s', strtotime("$shift_date $out_time"));
if($manual_in_date[$employee_code] === $log_date_in){
$err_column_array['error']["$excel_line_value$row"] = $view_name;
$msg_line = "Invalid - Same date time already present($log_date_in).!";
$err_column_tabview['error']["$excel_line_value$row"] = $view_name." ".$msg_line;
}elseif($manual_out_date[$employee_code] === $log_date_out){
$err_column_array['error']["$excel_line_value$row"] = $view_name;
$msg_line = "Invalid - Same date time already present($log_date_out).!";
$err_column_tabview['error']["$excel_line_value$row"] = $view_name." ".$msg_line;
}elseif($log_date_in >= $time_data[$employee_code][$shift_date]->punch_in){
$err_column_array['error']["$excel_line_value$row"] = $view_name;
$msg_line = "In Date and Time should Lesser than or equal to Punch in Time ($log_date_in).!";
$err_column_tabview['error']["$excel_line_value$row"] = $view_name." ".$msg_line;
}elseif($log_date_out <= $time_data[$employee_code][$shift_date]->punch_out){
$err_column_array['error']["$excel_line_value$row"] = $view_name;
$msg_line = "Out Date and Time should Lesser than or equal to Punch in Time ($log_date_out).!";
$err_column_tabview['error']["$excel_line_value$row"] = $view_name." ".$msg_line;
}
//FOR INSERT
$key = $employee_code . '-' . $shift_date . '-' . $user_id;
$insert_data[$key] = array('user_id' => $user_id,'log_date_in' => $log_date_in,'log_date_out' => $log_date_out,'in_time'=>$in_time,'out_time'=>$out_time,'shift_date'=>$shift_date,'mp_reason'=>$mp_reason,'employee_code'=>$employee_code,'in_date' => $in_date,'out_date' => $out_date);
}
}
$err_column_count = count($err_column_array['error']);
if((int)$err_column_count > 0){
$table_info = $this->get_excel_error_ui($err_column_tabview);
echo json_encode(array('success' => false,'message' => "Data Not Imported",'table_info' => $table_info));
exit(0);
}else{
//BULK INSERT
if($insert_data){
//MANUAL PUNCH ENTRY
$manual_values = [];
$manual_query = 'INSERT INTO cw_manual_punch_entry (employee_code,in_date,out_date,in_time,out_time,total_time,mp_reason,shift_date,trans_created_by,trans_created_date) VALUES ';
//TIME LOG
$insert_values = [];
$insert_query = 'INSERT INTO cw_time_log (device_id,user_id,log_date,record_type,trans_created_by,trans_created_date) VALUES ';
foreach($insert_data as $data){
$user_id = $data['user_id'];
$in_date = $data['in_date'];
$out_date = $data['out_date'];
$in_time = $data['in_time'];
$out_time = $data['out_time'];
$mp_reason = $data['mp_reason'];
$total_seconds = $this->timeToSeconds($in_time) + $this->timeToSeconds($out_time);
$total_time = $this->secondsToTime($total_seconds);
$log_date_in = $data['log_date_in'];
$log_date_out = $data['log_date_out'];
$insert_values[] = '("MAN01","'.$user_id.'", "'.$log_date_in.'","in","'.$this->logged_id.'", "'.$created_on.'"),("MAN02","'.$user_id.'", "'.$log_date_out.'","out","'.$this->logged_id.'", "'.$created_on.'")';
$manual_values[] = '("'.$data['employee_code'].'", "'.$in_time.'", "'.$out_time.'", "'.$total_time.'" , "'.$mp_reason.'" , "'.$data['shift_date'].'" ,"'.$this->logged_id.'", "'.$created_on.'")';
}
$insert_query .= implode(',', $insert_values);
$manual_query .= implode(',', $manual_values);
$insert_info = $this->db->query("CALL sp_a_run ('INSERT','$insert_query')");
if($insert_info){
$insert_result = $insert_info->result();
$insert_info->next_result();
$manual_info = $this->db->query("CALL sp_a_run ('INSERT','$manual_query')");
$manual_result = $manual_info->result();
$manual_info->next_result();
echo json_encode(array('success' => true,'message' => 'Successfully date imported','table_info'=>''));
}else{
echo json_encode(array('success' => false,'message' => "Data Not Imported",'table_info' => $table_info));
}
}
}
}
// Function to convert time string to seconds
public function timeToSeconds($time) {
list($hours, $minutes, $seconds) = explode(':', $time);
return $hours * 3600 + $minutes * 60 + $seconds;
}
// Function to convert seconds to time string
public function secondsToTime($seconds) {
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);
$seconds = $seconds % 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
}
?>