File: /home/cafsindia/hrms_allyindian_com/application_bk/controllers/Shift_import.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Shift_import extends Action_controller{
public function __construct(){
parent::__construct('shift_import');
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
//PAGE INFO FUNCTION
$this->page_info();
$data['encKey'] = $this->generateKey();
$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;
// SHIFT ENTRY BASED TOS MONTH DAY SETTINGS FOR GET A START AND END DATE
$month_day_info = $this->tos_day_info('',"1");
$start_date = date('Y-m-d',strtotime($month_day_info['salary_start_date']));
$end_date = date('Y-m-d',strtotime($month_day_info['salary_end_date']));
$data['start_date'] = $start_date;
$data['end_date'] = $end_date;
//FUNCTION FOR GET IDS FOR SET SHIFT EDIT AND VIEW ROWS
$disable_arr = $this->get_disable_ids($start_date);
$data['disable_arr'] = $disable_arr;
$this->load->view("$this->control_name/manage",$data);
}
//FUNCTION FOR GET IDS FOR SET SHIFT EDIT AND VIEW ROWS
public function get_disable_ids($start_date){
$disable_ids = "";
$shift_qry = 'SELECT GROUP_CONCAT(prime_shift_import_id) as prime_shift_import_id from cw_shift_import where cw_shift_import.trans_status = 1 and shift_date >= "'.$start_date.'"';
// and shift_date <= "'.$end_date.'"
$shift_info = $this->db->query("CALL sp_a_run ('SELECT','$shift_qry')");
$shift_rslt = $shift_info->result_array();
$shift_info->next_result();
$disable_ids = $shift_rslt[0]['prime_shift_import_id'];
return $disable_ids;
}
//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);
}
$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' => 'Url Expired.. Please refresh the page and try again....','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
$data['all_pick'] = $this->pick_list;//all pick result
//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();
// SHIFT ENTRY BASED TOS MONTH DAY SETTINGS FOR GET A START AND END DATE
$month_day_info = $this->tos_day_info('',"1");
$start_date = date('Y-m-d',strtotime($month_day_info['salary_start_date']));
$end_date = date('Y-m-d',strtotime($month_day_info['salary_end_date']));
$data['start_date'] = $start_date;
$data['end_date'] = $end_date;
$this->load->view("$this->control_name/form",$data);
}
//SAVE MODEL DATA TO DATA BASE
public function save(){//SAVE INFO FUNCTION
//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);
}
$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 = "";
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_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' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
}else{
$form_id = 0;
}
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);
}
}
//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,);
}
}
if($label_id === "employee_code"){
$employee_code = $value;
}else
if($label_id === "shift_date"){
$shift_date = date("Y-m-d",strtotime($value));
}
}
}
$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{
//DR ALL SHIFT MASTER SHIFT DETAILS SHOULD MAP IN PERMISSION SETTING MODULE CHECK 3NOV22 START
$error_shift = $this->shift_permission_set_check();
if($error_shift){
echo json_encode(array('success'=>false,'message'=>"Please Add Permission Details for shift ($error_shift)"));
exit(0);
}
//DR ALL SHIFT MASTER SHIFT DETAILS SHOULD MAP IN PERMISSION SETTING MODULE CHECK 3NOV22 END
$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 .= '"'.$prime_financial_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{
//SELECT QUERY FOR CHECK ALREADY SAME SHIFT EXIST OR NOT
$check_shift_exist_qry = 'select count(cw_shift_import.prime_shift_import_id) as count from cw_shift_import where employee_code = "'.$employee_code.'" and shift_date = "'.$shift_date.'" and trans_status = 1 and financial_setting_id = "'. $prime_financial_id.'" and prime_shift_import_id != "'.$form_id.'"';
$check_shift_exist_info = $this->db->query("CALL sp_a_run ('SELECT','$check_shift_exist_qry')");
$check_shift_exist_rslt = $check_shift_exist_info->result();
$check_shift_exist_info->next_result();
$check_shift_exist = (int)$check_shift_exist_rslt[0]->count;
if($check_shift_exist){
echo json_encode(array('success' => FALSE, 'message' => "Shift Already Exist.!",'insert_id' => $form_id));
}else{
//SHIFT DATE VALIDATE FOR SHIFT UPDATION PROCESS
$retun_rslt = $this->shift_update_validate($employee_code,$shift_date,$prime_financial_id);
if($retun_rslt){
$prime_upd_query .= 'financial_setting_id = "'. $prime_financial_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));
}
}
}
}
}
}
//SHIFT DATE VALIDATE FOR SHIFT UPDATION PROCESS
public function shift_update_validate($employee_code,$shift_date,$prime_financial_id){
//-------------------- for shift request start ---------------------------
$shift_request_qry = 'SELECT count(prime_request_id) pending_request FROM cw_request WHERE cw_request.trans_status = 1 and cw_request.request_type = "6" and cw_request.leave_status in (1) and cw_request.financial_setting_id = "'.$prime_financial_id.'" and cw_request.shift_date = "'.$shift_date.'" and cw_request.employee_code = "'.$employee_code.'"';
// cw_request.leave_status in (1,2)
$shift_request_info = $this->db->query("CALL sp_a_run ('SELECT','$shift_request_qry')");
$shift_request_rslt = $shift_request_info->result();
$shift_request_info->next_result();
$pending_request = (int)$shift_request_rslt[0]->pending_request;
if($pending_request){
echo json_encode(array('success' => FALSE, 'message' => "Shift Change Request Already Pending.!"));
exit(0);
}else{
//------------------- for on duty start -----------------------------
/*$on_duty_entry_qry = 'select count(prime_on_duty_entry_id) on_duty_count from cw_on_duty_entry where trans_status = 1 and on_duty_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_on_duty_entry.on_duty_date = "'.$shift_date.'" and cw_on_duty_entry.employee_code = "'.$employee_code.'"';
$on_duty_entry_info = $this->db->query("CALL sp_a_run ('RUN','$on_duty_entry_qry')");
$on_duty_entry_rslt = $on_duty_entry_info->result();
$on_duty_entry_info->next_result();
$on_duty_count = (int)$on_duty_entry_rslt[0]->on_duty_count;
if($on_duty_count){
echo json_encode(array('success' => FALSE, 'message' => "Already ON Duty Exist.!"));
exit(0);
}else{*/
//-------------------- for permission start ---------------------------
//QUERY FOR CHECK EXIST DATA FROM PERMISSION ENTRY TABLE
$permission_entry_qry = 'select count(prime_permission_entry_id) permission_count from cw_permission_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_permission_entry.permission_date = "'.$shift_date.'" and cw_permission_entry.employee_code = "'.$employee_code.'"';
$permission_entry_info = $this->db->query("CALL sp_a_run ('RUN','$permission_entry_qry')");
$permission_entry_rslt = $permission_entry_info->result();
$permission_entry_info->next_result();
$permission_count = (int)$permission_entry_rslt[0]->permission_count;
if($permission_count){
echo json_encode(array('success' => FALSE, 'message' => "Already Permission Exist.!"));
exit(0);
}else{
//-------------------- for manual punch start ---------------------------
$mp_entry_qry = 'select count(prime_manual_punch_entry_id) mp_count from cw_manual_punch_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_manual_punch_entry.shift_date = "'.$shift_date.'" and cw_manual_punch_entry.employee_code = "'.$employee_code.'"';
$mp_entry_info = $this->db->query("CALL sp_a_run ('RUN','$mp_entry_qry')");
$mp_entry_rslt = $mp_entry_info->result();
$mp_entry_info->next_result();
$mp_count = (int)$mp_entry_rslt[0]->mp_count;
if($mp_count){
echo json_encode(array('success' => FALSE, 'message' => "Already Manual Punch Exist.!"));
exit(0);
}else{
//-------------------- for ot start ---------------------------
$ot_entry_qry = 'select count(prime_overtime_entry_id) ot_count from cw_overtime_entry where trans_status = 1 and approval_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_overtime_entry.entry_date = "'.$shift_date.'" and cw_overtime_entry.employee_code = "'.$employee_code.'"';
$ot_entry_info = $this->db->query("CALL sp_a_run ('RUN','$ot_entry_qry')");
$ot_entry_rslt = $ot_entry_info->result();
$ot_entry_info->next_result();
$ot_count = (int)$ot_entry_rslt[0]->ot_count;
if($ot_count){
echo json_encode(array('success' => FALSE, 'message' => "Already Overtime Exist.!"));
exit(0);
}else{
return true;
}
}
}
/*}*/
}
}
//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') ?? []);
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$time_entry_arr = "";
$leave_entry_arr = "";
$permission_entry_arr = "";
$on_duty_entry_arr = "";
$check_cond = '"O","M","L","FL","SL","LP","PL","LM","ML","OL","LO","LL","WL","LW","UL","LU","MP","PM","LI","IL","MI","IM","OI","IO","OP","PO"';
//QUERY FOR CHECK EXIST DATA FROM TIME ENTRY TABLE
$time_entry_qry = 'SELECT employee_code,att_date from cw_time_entry WHERE whole_day_status not in ('.$check_cond.') and trans_status = 1';
$time_entry_info = $this->db->query("CALL sp_a_run ('RUN','$time_entry_qry')");
$time_entry_result = $time_entry_info->result_array();
$time_entry_info->next_result();
$time_entry_arr = array_reduce($time_entry_result, function ($result, $arr) {
$result[$arr['employee_code']][$arr['att_date']] = $arr['att_date'];
return $result;
}, array());
//QUERY FOR CHECK EXIST DATA FROM LEAVE ENTRY TABLE
$leave_entry_qry = 'select employee_code,leave_date from cw_leave_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'"';
$leave_entry_info = $this->db->query("CALL sp_a_run ('RUN','$leave_entry_qry')");
$leave_entry_result = $leave_entry_info->result_array();
$leave_entry_info->next_result();
$leave_entry_arr = array_reduce($leave_entry_result, function ($result, $arr) {
$result[$arr['employee_code']][$arr['leave_date']] = $arr['leave_date'];
return $result;
}, array());
//QUERY FOR CHECK EXIST DATA FROM PERMISSION ENTRY TABLE
$permission_entry_qry = 'select employee_code,permission_date from cw_permission_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'"';
$permission_entry_info = $this->db->query("CALL sp_a_run ('RUN','$permission_entry_qry')");
$permission_entry_result = $permission_entry_info->result_array();
$permission_entry_info->next_result();
$permission_entry_arr = array_reduce($permission_entry_result, function ($result, $arr) {
$result[$arr['employee_code']][$arr['permission_date']] = $arr['permission_date'];
return $result;
}, array());
//QUERY FOR CHECK EXIST DATA FROM ON DUTY ENTRY TABLE
$on_duty_entry_qry = 'select employee_code,on_duty_date from cw_on_duty_entry where trans_status = 1 and on_duty_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'"';
$on_duty_entry_info = $this->db->query("CALL sp_a_run ('RUN','$on_duty_entry_qry')");
$on_duty_entry_result = $on_duty_entry_info->result_array();
$on_duty_entry_info->next_result();
$on_duty_entry_arr = array_reduce($on_duty_entry_result, function ($result, $arr) {
$result[$arr['employee_code']][$arr['on_duty_date']] = $arr['on_duty_date'];
return $result;
}, array());
//QUERY FOR CHECK EXIST DATA FROM SHIFT IMPORT TABLE
$shift_import_exist_qry = 'select prime_shift_import_id,employee_code,shift_date from cw_shift_import where trans_status = 1 and prime_shift_import_id in ("'.$delete_ids.'") and financial_setting_id ="'.$prime_financial_id.'"';
$shift_import_exist_info = $this->db->query("CALL sp_a_run ('RUN','$shift_import_exist_qry')");
$shift_import_exist_rslt = $shift_import_exist_info->result();
$shift_import_exist_info->next_result();
$exit_time_entry = "";
$delete_data = "";
foreach($shift_import_exist_rslt as $value){
$shift_import_id = $value->prime_shift_import_id;
$employee_code = $value->employee_code;
$shift_date = $value->shift_date;
// $weekoff_type = $value->weekoff_type;
$exit_time_entry = $time_entry_arr[$employee_code][$shift_date];
$exit_leave_entry = $leave_entry_arr[$employee_code][$shift_date];
$exit_permission_entry = $permission_entry_arr[$employee_code][$shift_date];
$exit_on_duty_entry = $on_duty_entry_arr[$employee_code][$shift_date];
if($exit_leave_entry){
echo json_encode(array('success' => FALSE, 'message' => "Unable to Delete, This Date is Already Exist in Leave Entry Table..!!"));
exit(0);
}else
if($exit_permission_entry){
echo json_encode(array('success' => FALSE, 'message' => "Unable to Delete, This Date is Already Exist in Permission Entry Table..!!"));
exit(0);
}else
if($exit_on_duty_entry){
echo json_encode(array('success' => FALSE, 'message' => "Unable to Delete, This Date is Already Exist in On Duty Entry Table..!!"));
exit(0);
}else{
$delete_data .= "$shift_import_id,";
}
}
if($delete_data){
$delete_data = rtrim($delete_data,',');
$delete_query = 'DELETE FROM '. $this->prime_table .' WHERE '. $this->prime_id .' in ('. $delete_data .')';
$this->db->query("CALL sp_a_run ('RUN','$delete_query')");
echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
}
}
//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' => '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');
$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;
$this->load->view("$this->control_name/import",$data);
}
// public function sheet_name(){
// $file_path = $this->input->post('file_path');
// $filename = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
// include($filename);
// $excel_obj = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_path);
// $sheet_count = $excel_obj->getSheetCount();
// $sheet_name = array();
// for($i= 0; $i< $sheet_count; $i++){
// $sheet = $excel_obj->getSheet($i);
// $sheet_name[] = $sheet->getTitle();
// }
// echo json_encode(array('sheet_name' =>$sheet_name));
// }
//SHIFT min date and max date validation by DR 5JUL22
public function tos_shift_mon_day(){
$entry_parameter = (int)$this->input->post('entry_parameter');
$employee_code = $this->input->post('employee_code');
$month_day_info = $this->tos_day_info($employee_code,$entry_parameter);
if(count($month_day_info ?? [])){
$salary_start_date = $month_day_info['salary_start_date'];
$salary_end_date = $month_day_info['salary_end_date'];
$year_end_date = $month_day_info['year_end_date'];
$starting_date = $month_day_info['starting_date'];
$ending_date = $month_day_info['ending_date'];
echo json_encode(array('success' => TRUE, 'start_date' => $salary_start_date, 'end_date' => $year_end_date, 'fin_start_date' => date("d-m-Y",strtotime($starting_date)), 'fin_end_date' => date("d-m-Y",strtotime($ending_date))));
}else{
echo json_encode(array('success' => FALSE, 'message' => "Please set Month Day for this Request Module.! Please Contact Admin.!"));
exit(0);
}
}
//LOAD MODEL PAGE ADD A BULK SHIFT DATA
public function add($form_view_id=-1){
$data['encKey'] = $this->generateKey();
$role_info = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_category` where trans_status = 1 and prime_category_id !=1')");
$role_result = $role_info->result();
$role_info->next_result();
$category_list[""] = "---- Select Category ----";
foreach($role_result as $for){
$role_id = $for->prime_category_id;
$category_name = $for->category_name;
$category_list[$role_id] = $category_name;
}
$data['category_list'] = $category_list;
$day_type_info = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_day_type` where trans_status = 1')");
$day_type_result = $day_type_info->result();
$day_type_info->next_result();
$day_type_list[""] = "---- Select ----";
foreach($day_type_result as $for){
$prime_id = $for->prime_day_type_id;
$day_type = $for->day_type;
$day_type_list[$prime_id] = $day_type;
}
$data['day_type_list'] = $day_type_list;
$from_query = 'select * from cw_form_setting where prime_module_id = "employees" and field_show = "1" and label_name != "role" ORDER BY input_for,field_sort asc';
$form_data = $this->db->query("CALL sp_a_run ('SELECT','$from_query')");
$form_result = $form_data->result();
$form_data->next_result();
$this->fliter_list = $this->get_filter_data($form_result);
$data['fliter_list'] = $this->fliter_list;
//SHIFT ID AND NAME GET FROM SHIFT MASTER
$shift_master_qry = 'select prime_shift_master_id,shift_name from cw_shift_master where trans_status = "1"';
$shift_master_data = $this->db->query("CALL sp_a_run ('SELECT','$shift_master_qry')");
$shift_master_rslt = $shift_master_data->result();
$shift_master_data->next_result();
$shift_name_list[""] = "---- Select ----";
foreach($shift_master_rslt as $shift_data){
$prime_id = $shift_data->prime_shift_master_id;
$shift_name = $shift_data->shift_name;
$shift_name_list[$prime_id] = $shift_name;
}
$data['shift_name_list'] = $shift_name_list;
$this->load->view("$this->control_name/add",$data);
}
//PROVIDE QUERY AND DROPDOWN VALUES
public function get_filter_data($form_result){
$filter = array();
foreach($form_result as $setting){
$prime_form_id = (int)$setting->prime_form_id;
$prime_module_id = $setting->prime_module_id;
$input_view_type = (int)$setting->input_view_type;
$input_for = (int)$setting->input_for;
$field_type = (int)$setting->field_type;
$label_id = $setting->label_name;
$label_name = ucwords($setting->view_name);
$field_length = $setting->field_length;
$field_decimals = $setting->field_decimals;
$pick_list_type = (int)$setting->pick_list_type;
$pick_list = $setting->pick_list;
$pick_table = $setting->pick_table;
$auto_prime_id = $setting->auto_prime_id;
$auto_dispaly_value = $setting->auto_dispaly_value;
$field_isdefault = (int)$setting->field_isdefault;
$file_type = (int)$setting->file_type;
$mandatory_field = (int)$setting->mandatory_field;
$unique_field = (int)$setting->unique_field;
$search_show = (int)$setting->search_show;
$array_list = array();
if( $label_id != 'employee_code' && $label_id != 'emp_name'){
if(($field_type === 5) || ($field_type === 7)){
if($pick_list_type === 1){
$pick_list_val = explode(",",$pick_list);
$pick_list_val_1 = $pick_list_val[0];
$pick_list_val_2 = $pick_list_val[1];
if($pick_table == "cw_category"){
$qry = " and prime_category_id != 1";
}else{
$qry = "";
}
if($pick_table == "cw_payroll_formula"){
$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();
$array_list[""] = "---- $label_name ----";
foreach($pick_result as $pick){
$pick_key = $pick->$pick_list_val_1;
$pick_val = ucwords(str_replace("_"," ",$pick->$pick_list_val_2));
$array_list[$pick_key] = $pick_val;
}
}else{
if($label_id === "excemption_component"){
$pick_query = "select $pick_list from $pick_table where trans_status = 1 and tax_section = 1 $qry";
}else{
$pick_query = "select $pick_list from $pick_table where trans_status = 1 $qry";
}
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_data->next_result();
$array_list[""] = "---- $label_name ----";
foreach($pick_result as $pick){
$pick_key = $pick->$pick_list_val_1;
$pick_val = $pick->$pick_list_val_2;
$array_list[$pick_key] = $pick_val;
}
}
}else
if($pick_list_type === 2){
$pick_list_val_1 = $pick_table."_id";
$pick_list_val_2 = $pick_table."_value";
$pick_list_val_3 = $pick_table."_status";
$pick_query = "select $pick_list_val_1,$pick_list_val_2 from $pick_table where $pick_list_val_3 = 1";
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_data->next_result();
$array_list[""] = "---- $label_name ----";
foreach($pick_result as $pick){
$pick_key = $pick->$pick_list_val_1;
$pick_val = $pick->$pick_list_val_2;
$array_list[$pick_key] = $pick_val;
}
}
}
if(($input_view_type === 1) || ($input_view_type === 2)){
$filter[] = array('label_id'=> $label_id, 'field_isdefault'=> $field_isdefault, 'array_list'=> $array_list, 'field_type'=> $field_type);
}
}
}
return $filter;
}
//Employee code and Selective code dropdown list.
public function emp_code_list(){
//GET FINANCIAL DETAILS
$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));
$category = (int)$this->input->post('category');
$cat_qry= "";
if($category !== 0){ //if !==0 then selective employee result will get else single employee result will get.
$cat_qry= ' cw_category.prime_category_id = "'.$category.'" AND ';
}
$emp_qry = 'SELECT cw_employees.employee_code,cw_employees.emp_name,cw_category.category_name FROM cw_category JOIN cw_employees
ON cw_employees.role=cw_category.prime_category_id WHERE '.$cat_qry.' cw_employees.trans_status = 1 ';
$emp_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
$emp_rslt = $emp_info->result();
$emp_info->next_result();
$emp_list = "<option value= ''>---- Select Employees----</option>";
foreach($emp_rslt as $for){
$employee_code = $for->employee_code;
$emp_name = $for->emp_name;
$emp_list .= "<option value= '".$employee_code."'>".$employee_code." - ".$emp_name."</option>";
}
echo json_encode(array('success' => TRUE, 'emp_list' => $emp_list));
}
public function emp_category_sal_date(){
//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);
}
$employee_code = $this->input->post('employee_code');
$month_day_info = $this->tos_day_info($employee_code,"1"); // 1 => SHIFT ENTRY
if(count($month_day_info ?? [])){
$salary_start_date = $month_day_info['salary_start_date'];
$salary_end_date = $month_day_info['salary_end_date'];
$year_end_date = $month_day_info['year_end_date'];
$starting_date = $month_day_info['starting_date'];
$ending_date = $month_day_info['ending_date'];
$emp_cat_qry = 'SELECT cw_employees.role FROM `cw_employees` where cw_employees.trans_status = 1 and cw_employees.employee_code = "'.$employee_code.'"';
$emp_cat_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_cat_qry')");
$emp_cat_rslt = $emp_cat_info->result();
$emp_cat_info->next_result();
$category = (int)$emp_cat_rslt[0]->role;
if(!$category){
echo json_encode(array('success' => FALSE, 'message' => "Category should not Empty.! Please check Employee Master.?"));
exit(0);
}else{
echo json_encode(array('success' => TRUE, 'category' => $category, 'salary_start_date' => $salary_start_date, 'salary_end_date' => $year_end_date, 'fin_start_date' => date("d-m-Y",strtotime($starting_date)), 'fin_end_date' => date("d-m-Y",strtotime($ending_date))));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => "Please add Month Day Settings for Shift Entry.!"));
exit(0);
}
}
//FUNCTION FOR VALIDATE TO SHIFT START AND END DATE
public function check_transaction_status(){
//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);
}
$category = (int)$this->input->post('category');
//GET FINANCIAL DETAILS
$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));
//TO CALL A TIME OFFICE SHIFT SETTING BASED MONTH DAY QRY FUNCTION
$tos_mon_day_rslt = $this->tos_day_qry_fun("1");
if($tos_mon_day_rslt[0]){
$day_conditions = (int)$tos_mon_day_rslt[0]->day_conditions;
$day_count = $tos_mon_day_rslt[0]->day_count;
$day_start = $tos_mon_day_rslt[0]->day_start;
$day_end = $tos_mon_day_rslt[0]->day_end;
$pay_posting_result = $this->cat_last_pay_process_info($starting_date,$ending_date,$category);
$process_month = $pay_posting_result[0]->process_month;
$month_day_arr = $this->min_max_date_fun($prime_financial_id,$starting_date,$ending_date,$day_conditions,$day_count,$tos_mon_day_rslt,$process_month);
$sal_start_date = date("d-m-Y",strtotime($month_day_arr['salary_start_date']));
$sal_end_date = date("d-m-Y",strtotime($month_day_arr['salary_end_date']));
$year_end_date = date("d-m-Y",strtotime($month_day_arr['year_end_date']));
echo json_encode(array('success' => TRUE, 'salary_start_date' => $sal_start_date, 'salary_end_date' => $year_end_date));
}else{
echo json_encode(array('success' => false, 'message' => 'Please add Time Office Month Day Settings for Shift Entry.!'));
}
}
//BULK SHIFT ADD FUNCTION
public function shift_bulk_add(){
$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);
}
$category = (int)$this->input->post('category');
$employee_type = (int)$this->input->post('employee_type');
$employee_code = $this->input->post('employee_code');
$selective_employees = implode('","',$this->input->post('selective_employee') ?? []);
$start_date = date("Y-m-d",strtotime($this->input->post('start_date')));
$end_date = date("Y-m-d",strtotime($this->input->post('end_date')));
$shift_name = (int)$this->input->post('shift_name');
$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));
$created_on = date("Y-m-d H:i:s");
$fliter_query = "";
if($employee_type === 1){
$fliter_query = ' and employee_code = "'.$employee_code.'"';
}else
if($employee_type === 3){
$fliter_query = ' and employee_code in ("'.$selective_employees.'")';
}else
if($employee_type === 2){
$fliter_label = $this->input->post('fliter_label');
$fliter_type = $this->input->post('fliter_type');
$filter_cond = $this->input->post('filter_cond');
$fliter_val = $this->input->post('fliter_val');
$filter_count = count($fliter_label ?? []);
$search_count = 0;
for($i=0;$i<=(int)$filter_count;$i++){
$db_name = $fliter_label[$i];
$table_name = $fliter_type[$i];
$db_cond = $filter_cond[$i];
$db_value = $fliter_val[$i];
$field_type = $field_type[$i];
if(($db_cond) && ($db_value)){
$search_count++;
if((int)$field_type === 4){
$search_val = date("Y-m-d",strtotime($db_value));
}else{
$search_val = $db_value;
}
if($db_cond === "LIKE"){ $search_val = "$db_value%"; }
if((int)$table_name === 1){
$this->prime_table = "cw_employees";
$fliter_query .= ' and '. $this->prime_table .".". $db_name ." ". $db_cond .' "'.$search_val.'"';
}
}
}
}
$emp_details_arr = array();
$on_duty_entry_arr = array();
$permission_entry_arr = array();
$mp_entry_arr = array();
$ot_entry_arr = array();
$shift_request_arr = array();
$shift_import_arr = array();
$table_info = "";
//Select employee codes
$emp_qry = 'SELECT GROUP_CONCAT(cw_employees.employee_code) as emp_codes FROM `cw_employees` where cw_employees.trans_status = 1 and cw_employees.date_of_joining <= "'.$start_date.'" and (cw_employees.termination_status = 0 or cw_employees.resignation_date >= "'.$end_date.'") and cw_employees.role in ('.$category.')'.$fliter_query;
// cw_employees.resignation_date between "'.$start_date.'" and "'.$end_date.'"
$emp_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
$emp_rslt = $emp_info->result();
$emp_info->next_result();
$emp_codes = $emp_rslt[0]->emp_codes;
if(!$emp_codes){
//ERROR
echo json_encode(array('success'=>FALSE, 'message'=>'No Records Found.!', 'table_info'=>$table_info,));
exit(0);
}else{
$emp_codes = str_replace(',', '","', $emp_codes);
if(count($emp_rslt ?? [])){
//SELECT EMPLOYEE DETAILS
$emp_detail_qry = 'SELECT cw_employees.employee_code,cw_employees.emp_name,date_of_joining FROM `cw_employees` where cw_employees.trans_status = 1 and cw_employees.date_of_joining <= "'.$start_date.'" and (cw_employees.termination_status = 0 or cw_employees.resignation_date >= "'.$end_date.'") and cw_employees.role in ('.$category.')'.$fliter_query;
// cw_employees.resignation_date between "'.$start_date.'" and "'.$end_date.'"
$emp_detail_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_detail_qry')");
$emp_detail_rslt = $emp_detail_info->result_array();
$emp_detail_info->next_result();
foreach($emp_detail_rslt as $emp_rslt){
$emp_code_val = $emp_rslt['employee_code'];
$emp_details_arr[$emp_code_val] = $emp_rslt;
}
}
//------------------- for on duty start -----------------------------
//QUERY FOR CHECK EXIST DATA FROM ON DUTY ENTRY TABLE
$on_duty_entry_qry = 'select employee_code,on_duty_date from cw_on_duty_entry where trans_status = 1 and on_duty_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_on_duty_entry.on_duty_date >= "'.$start_date.'" and cw_on_duty_entry.on_duty_date <= "'.$end_date.'" and cw_on_duty_entry.employee_code in ("'.$emp_codes.'")';
$on_duty_entry_info = $this->db->query("CALL sp_a_run ('RUN','$on_duty_entry_qry')");
$on_duty_entry_result = $on_duty_entry_info->result_array();
$on_duty_entry_info->next_result();
foreach($on_duty_entry_result as $on_duty_key => $on_duty_val){
$on_duty_emp_code = $on_duty_val['employee_code'];
$on_duty_date = $on_duty_val['on_duty_date'];
$on_duty_entry_arr[$on_duty_emp_code][$on_duty_date] = $on_duty_date;
}
//-------------------- for on duty end -------------------------------
//-------------------- for permission start ---------------------------
//QUERY FOR CHECK EXIST DATA FROM PERMISSION ENTRY TABLE
$permission_entry_qry = 'select employee_code,permission_date from cw_permission_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_permission_entry.permission_date >= "'.$start_date.'" and cw_permission_entry.permission_date <= "'.$end_date.'" and cw_permission_entry.employee_code in ("'.$emp_codes.'")';
$permission_entry_info = $this->db->query("CALL sp_a_run ('RUN','$permission_entry_qry')");
$permission_entry_result = $permission_entry_info->result_array();
$permission_entry_info->next_result();
foreach($permission_entry_result as $per_key => $per_val){
$per_emp_code = $per_val['employee_code'];
$permission_date = $per_val['permission_date'];
$permission_entry_arr[$per_emp_code][$permission_date] = $permission_date;
}
//-------------------- for permission end ---------------------------
//-------------------- for manual punch start ---------------------------
$mp_entry_qry = 'select employee_code,shift_date from cw_manual_punch_entry where trans_status = 1 and leave_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_manual_punch_entry.shift_date >= "'.$start_date.'" and cw_manual_punch_entry.shift_date <= "'.$end_date.'" and cw_manual_punch_entry.employee_code in ("'.$emp_codes.'")';
$mp_entry_info = $this->db->query("CALL sp_a_run ('RUN','$mp_entry_qry')");
$mp_entry_result = $mp_entry_info->result_array();
$mp_entry_info->next_result();
foreach($mp_entry_result as $mp_entry_key => $mp_entry_val){
$mp_entry_emp_code = $mp_entry_val['employee_code'];
$mp_entry_date = $mp_entry_val['shift_date'];
$mp_entry_arr[$mp_entry_emp_code][$mp_entry_date] = $mp_entry_date;
}
//-------------------- for manual punch end ------------------------------
//-------------------- for ot start ---------------------------
$ot_entry_qry = 'select employee_code,entry_date from cw_overtime_entry where trans_status = 1 and approval_status NOT IN (3,4) and financial_setting_id = "'.$prime_financial_id.'" and cw_overtime_entry.entry_date >= "'.$start_date.'" and cw_overtime_entry.entry_date <= "'.$end_date.'" and cw_overtime_entry.employee_code in ("'.$emp_codes.'")';
$ot_entry_info = $this->db->query("CALL sp_a_run ('RUN','$ot_entry_qry')");
$ot_entry_result = $ot_entry_info->result_array();
$ot_entry_info->next_result();
$ot_entry_arr = array();
foreach($ot_entry_result as $ot_entry_key => $ot_entry_val){
$ot_entry_emp_code = $ot_entry_val['employee_code'];
$ot_entry_date = $ot_entry_val['entry_date'];
$ot_entry_arr[$ot_entry_emp_code][$ot_entry_date] = $ot_entry_date;
}
//-------------------- for ot end ------------------------------
//-------------------- for shift request start ---------------------------
$shift_request_qry = 'SELECT employee_code,shift_date FROM cw_request WHERE cw_request.trans_status = 1 and cw_request.request_type = "6" and cw_request.leave_status in (1) and cw_request.financial_setting_id = "'.$prime_financial_id.'" and cw_request.shift_date >= "'.$start_date.'" and cw_request.shift_date <= "'.$end_date.'" and cw_request.employee_code in ("'.$emp_codes.'")';
// cw_request.leave_status in (1,2)
$shift_request_info = $this->db->query("CALL sp_a_run ('SELECT','$shift_request_qry')");
$shift_request_rslt = $shift_request_info->result_array();
$shift_request_info->next_result();
foreach($shift_request_rslt as $shift_req_key => $shift_req_val){
$shift_req_emp_code = $shift_req_val['employee_code'];
$shift_req_date = $shift_req_val['shift_date'];
$shift_request_arr[$shift_req_emp_code][$shift_req_date] = $shift_req_date;
}
//-------------------- for shift request end ------------------------------
//date interval
$date_period = new DatePeriod(
new DateTime($start_date),
new DateInterval('P1D'),
new DateTime(date("Y-m-d",strtotime("+1 days",strtotime($end_date))))
);
$err_table_body = "";
$ins_value = "";
$error_count = 0;
$emp_code_arr = explode('","', $emp_codes);
$success_count = 0;
foreach($emp_code_arr as $emp_code){
$emp_name = $emp_details_arr[$emp_code]['emp_name'];
//GET EMPLOYEE DATE OF JOINING
$emp_doj = $emp_details_arr[$emp_code]['date_of_joining'];
//-------------------- for shift import start ---------------------------
//QUERY FOR CHECK EXIST DATA FROM SHIFT IMPORT TABLE
$shift_import_exist_qry = 'select employee_code,shift_date,prime_shift_import_id as prime_id from cw_shift_import where trans_status = 1 and cw_shift_import.financial_setting_id = "'.$prime_financial_id.'" and cw_shift_import.shift_date >= "'.$start_date.'" and cw_shift_import.shift_date <= "'.$end_date.'" and cw_shift_import.employee_code in ("'.$emp_code.'")';
$shift_import_exist_info = $this->db->query("CALL sp_a_run ('RUN','$shift_import_exist_qry')");
$shift_import_exist_rslt = $shift_import_exist_info->result_array();
$shift_import_exist_info->next_result();
foreach($shift_import_exist_rslt as $shift_key => $shift_val){
$shift_emp_code = $shift_val['employee_code'];
$shift_date = $shift_val['shift_date'];
$shift_import_arr[$shift_emp_code][$shift_date] = $shift_val;
}
//-------------------- for shift import end ---------------------------
//LOOP FOR GET DAY BY DAY
foreach ($date_period as $key => $value){
$day_name = $value->format('l');
$shift_date = $value->format('Y-m-d');
$display_date = $value->format('d-m-Y');
$exist_count = 0;
// --------------------- VALIDATION CHECKING START ----------------------
//check DOJ exist
if($emp_doj > $shift_date){
(int)$exist_count += 1;
if($exist_count === 1){
$err_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:red; font-weight: bold;'>Shift Date Should not lesser than date of Joining</td></tr>";
$error_count++;
}
}
//pending shift change request
$shift_request_date = $shift_request_arr[$emp_code][$shift_date];
if($shift_request_date){
(int)$exist_count += 1;
if($exist_count === 1){
$err_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:red;'>Already add pending Shift Change Request</td></tr>";
$error_count++;
}
}
// check permission exist
$check_permission = $permission_entry_arr[$emp_code][$shift_date];
if($check_permission){
(int)$exist_count += 1;
if($exist_count === 1){
$err_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:red;'>Already Permission Exist</td></tr>";
$error_count++;
}
}
// check manual punch exist
$check_man_punch = $mp_entry_arr[$emp_code][$shift_date];
if($check_man_punch){
(int)$exist_count += 1;
if($exist_count === 1){
$err_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:red;'>Already Manual Punch Exist</td></tr>";
$error_count++;
}
}
// check ot exist
$check_ot = $ot_entry_arr[$emp_code][$shift_date];
if($check_ot){
(int)$exist_count += 1;
if($exist_count === 1){
$err_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:red;'>Already Overtime Exist</td></tr>";
$error_count++;
}
}
//check shift date exist
$exist_shift_date = $shift_import_arr[$emp_code][$shift_date]['shift_date'];
$prime_shift_id = $shift_import_arr[$emp_code][$shift_date]['prime_id'];
//NO ERROR OCCURS THEN CREATE INSERT & UPDATE QUERY VALUES
if($exist_count === 0 ){
if($exist_shift_date && $prime_shift_id){
//UPDATE QUERY VALUES
$upd_value .= '("'.$prime_shift_id.'","'.$emp_code.'","'.$emp_name.'","'.$start_date.'","'.$end_date.'","'.$shift_name.'","'.$shift_date.'","'.$prime_financial_id.'","'.$this->session->userdata('logged_id').'","'.$created_on.'"),';
$success_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:green;'>Successfully updated!</td></tr>";
$success_count++;
}else{
//INSERT QUERY VALUES
$ins_value .= '("'.$emp_code.'","'.$emp_name.'","'.$start_date.'","'.$end_date.'","'.$shift_name.'","'.$shift_date.'","'.$prime_financial_id.'","'.$this->session->userdata('logged_id').'","'.$created_on.'"),';
$success_table_body .= "<tr><td style = 'font-weight: bold;'>$emp_code - $emp_name</td><td style = 'font-weight: bold;'>$display_date</td><td style = 'color:green;'>Successfully Inserted!</td></tr>";
$success_count++;
}
}
}
}
//INSERT AND UPDATE PROCESS
if(!$ins_value && !$upd_value){
//ERROR
echo json_encode(array('success'=>FALSE, 'message'=>'Invalid Details Added.!'));
exit(0);
}else{
//FOR SHIFT UPDATE
$insert_info = '';
if($upd_value){
$upd_value = rtrim($upd_value,",");
//TIME ENTRY UPD QUERY
$shift_import_upd_qry = 'INSERT INTO cw_shift_import (prime_shift_import_id,employee_code,employee_name,from_date,to_date,shift_name,shift_date,financial_setting_id,trans_updated_by,trans_updated_date) VALUES '.$upd_value.' ON DUPLICATE KEY UPDATE employee_code = VALUES(employee_code),employee_name = VALUES(employee_name),from_date = VALUES(from_date),to_date = VALUES(to_date),shift_name = VALUES(shift_name),shift_date = VALUES(shift_date),financial_setting_id = VALUES(financial_setting_id),trans_updated_by = VALUES(trans_updated_by),trans_updated_date = VALUES(trans_updated_date)';
$insert_info = $this->db->query($shift_import_upd_qry);
}
//FOR SHIFT INSERT
if($ins_value){
$ins_value = rtrim($ins_value,',');
$insert_query = 'insert into cw_shift_import (employee_code,employee_name,from_date,to_date,shift_name,shift_date,financial_setting_id,trans_created_by,trans_created_date) values '.$ins_value;
$insert_info = $this->db->query($insert_query);
}
if($insert_info){
//INSERT LOG QUERY PROCESS
$ins_log_query = 'insert into cw_shift_process_log (category,from_date,to_date,employee_type,shift_name,financial_setting_id,trans_created_by,trans_created_date) values ("'.$category.'","'.$start_date.'","'.$end_date.'","'.$employee_type.'","'.$shift_name.'","'.$prime_financial_id.'","'. $this->session->userdata('logged_id').'","'.$created_on.'")';
$ins_log_info = $this->db->query("CALL sp_a_run ('INSERT','$ins_log_query')");
$ins_log_result = $ins_log_info->result();
$ins_log_info->next_result();
// SHIFT ENTRY BASED TOS MONTH DAY SETTINGS FOR GET A START AND END DATE(EMPLOYEE BASED)
if($employee_type === 1){
$month_day_info = $this->tos_day_info($employee_code,"1");
}else{
//TO CALL A TIME OFFICE SHIFT SETTING BASED MONTH DAY QRY FUNCTION (CATEGORY BASED)
$tos_mon_day_rslt = $this->tos_day_qry_fun("1");
if($tos_mon_day_rslt[0]){
$day_conditions = (int)$tos_mon_day_rslt[0]->day_conditions;
$day_count = $tos_mon_day_rslt[0]->day_count;
$day_start = $tos_mon_day_rslt[0]->day_start;
$day_end = $tos_mon_day_rslt[0]->day_end;
$pay_posting_result = $this->cat_last_pay_process_info($starting_date,$ending_date,$category);
$process_month = $pay_posting_result[0]->process_month;
$month_day_info = $this->min_max_date_fun($prime_financial_id,$starting_date,$ending_date,$day_conditions,$day_count,$tos_mon_day_rslt,$process_month);
}
}
$start_date = date('Y-m-d',strtotime($month_day_info['salary_start_date']));
$end_date = date('Y-m-d',strtotime($month_day_info['salary_end_date']));
//FUNCTION FOR GET IDS FOR SET SHIFT EDIT AND VIEW ROWS
$disable_arr = $this->get_disable_ids($start_date);
//SUCCESS
echo json_encode(array('success'=>TRUE, 'message'=>'Successfully Shift Uploaded.!', 'error' => $error_count,'success' => $success_count, 'success_info'=>$success_table_body, 'error_info' =>$err_table_body ,'disable_arr' => $disable_arr));
}else{
//ERROR
echo json_encode(array('success'=>FALSE, 'message'=>'Insert Query Error.!'));
}
}
}
// }
}
}
?>