File: //home/cafsindia/cloud_cafsinfotech_in/application/controllers/Overtime_approval.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Overtime_approval extends Action_controller{
public function __construct(){
parent::__construct('overtime_approval');
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
$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;
//LEAVE FINANCIAL YEAR GET FUNCTION
$leave_financial_info = $this->get_leave_financial_details();
$prime_financial_id = $leave_financial_info[0]->prime_leave_financial_year_id;
$approval_status_qry = 'SELECT prime_overtime_approval_id,approval_status FROM cw_overtime_approval WHERE cw_overtime_approval.trans_status = 1 and financial_setting_id = "'.$prime_financial_id.'"';
$approval_status_info = $this->db->query("CALL sp_a_run ('SELECT','$approval_status_qry')");
$approval_status_rslt = $approval_status_info->result_array();
$approval_status_info->next_result();
$approval_status_arr = array();
foreach($approval_status_rslt as $app_key => $app_val){
$prime_id = $can_val['prime_overtime_approval_id'];
$approval_status_arr[$prime_id] = $can_val['approval_status'];
}
$data['approval_status_arr'] = $approval_status_arr;
$this->load->view("$this->control_name/manage",$data);
}
//LOAD TABEL WITH FILTERS
public function search(){
$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_all_query = $count_all_query.' where '.$this->prime_table.'.trans_status = 1 AND financial_setting_id = '.$prime_financial_id;
$count_query = $count_all_query.' '.$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){
//VIEW INFO FUNCTION
$this->view_info($form_view_id);
$data['module_sts'] = (int)$this->module_sts;
//VIEW, FORM INPUT
$data['view_info'] = $this->view_info;
$data['form_info'] = $this->form_info;
$data['formula_result'] = $this->formula_result;//for label color change
$data['role_based_condition'] = $this->role_based_condition;//for label name readonly options
$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();
$this->load->view("$this->control_name/form",$data);
}
//SAVE MODEL DATA TO DATA BASE
public function save(){
//SAVE INFO FUNCTION
$this->save_info();
$module_sts = (int)$this->module_sts;
if(!$module_sts){
echo json_encode(array('success' => FALSE, 'message' => "Save Info Query Process Error...!"));
exit(0);
}else{
$unq_chk = array();
$prime_qry_key = "";
$prime_qry_value = "";
$prime_upd_query = "";
$cf_qry_key = "";
$cf_qry_value = "";
$cf_upd_query = "";
$cf_has = false;
$form_id = (int)$this->input->post($this->prime_id);
$ot_type = (int)$this->input->post('ot_type');
$category = (int)$this->input->post('category');
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$fin_start_date = date("Y-m-d",strtotime($financial_info[0]->starting_date));
$fin_end_date = date("Y-m-d",strtotime($financial_info[0]->ending_date));
$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 === 8){//textbox only
$value = str_replace('"',"~",$value);
$value = str_replace("'","`",$value);
$value = str_replace("&","^",$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);
}
}
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.'",';
$post_data[$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;
}
if($label_id === "prime_overtime_request_id"){
$overtime_request_id = (int)$value;
}
if($label_id === "entry_date"){
$entry_date = date("Y-m-d",strtotime($value));
}
if($label_id === "first_approval_status"){
$first_approval_sts = (int)$value;
}
if($label_id === "second_approval_status"){
$second_approval_sts = (int)$value;
}
if($label_id === "cancellation_request"){
$cancel_request = (int)$value;
}
if($label_id === "cancellation_reason"){
$cancellation_reason = $value;
}
if($label_id === "first_approval_cancel_status"){
$first_approval_can_sts = (int)$value;
}
if($label_id === "second_approval_cancel_status"){
$second_approval_can_sts = (int)$value;
}
if($label_id === "approval_status"){
$approval_status = (int)$value;
}
if($label_id === "excess_work"){
$excess_work = $value;
}
if($label_id === "over_time"){
$app_over_time = date("H:i", 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{
$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 $send_mail_status", 'insert_id' => $insert_id));
}else{
//APPROVAL PROCESS
if($excess_work < $app_over_time){
echo json_encode(array('success' => FALSE, 'message' => "Excess Work Time Should be Greater than Approved Overtime..!"));
exit(0);
}
if($first_approval_sts === 2 || $second_approval_sts === 2){
if($app_over_time === "" || $app_over_time === "0" || $app_over_time === "00:00"){
echo json_encode(array('success' => FALSE, 'message' => "Approved Overtime Should not Empty or Zero..!"));
exit(0);
}
}
if($approval_status === 2 || $approval_status === 4){
if($app_over_time === "" || $app_over_time === "0" || $app_over_time === "00:00"){
echo json_encode(array('success' => FALSE, 'message' => "Approved Overtime Should not Empty or Zero..!"));
exit(0);
}else
if($excess_work < $app_over_time){
echo json_encode(array('success' => FALSE, 'message' => " Approved Overtime Should not be Greater than Total OT Hours? Please Check it..!"));
exit(0);
}else{
if($approval_status === 2){
$app_over_time_arr = explode(":",$app_over_time);
$app_over_time_min = $app_over_time_arr[0] * 60 + $app_over_time_arr[1];
}else
if($approval_status === 4){
$app_over_time_min = "0";
}
// $time_entry_rslt = $this->time_entry_upd($post_data,$form_id,$prime_financial_id,$approval_status);
// $success = $time_entry_rslt['success'];
if($ot_type === 4){
$approved_qry = 'approved_ot_mins = "0"';
}else
if($ot_type === 3){
$approved_qry = 'approved_special_ot = "'.$app_over_time_min.'"';
}else{
$approved_qry = 'approved_ot_mins = "'.$app_over_time_min.'"';
}
//TIME ENTRY UPDATE FUNCTION
$upd_time_entry_qry = 'UPDATE cw_time_entry SET '.$approved_qry.',trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE employee_code = "'.$employee_code.'" and att_date = "'.$entry_date.'"';
$upd_time_entry_info = $this->db->query("CALL sp_a_run ('RUN','$upd_time_entry_qry')");
if($upd_time_entry_info){
if($ot_type === 4){
$this->coff_update($employee_code,$prime_financial_id,$created_on,$category,$app_over_time,$approval_status,$entry_date,$form_id);
}
}else{
echo json_encode(array('success' => FALSE, 'message' => 'Data not Updated..!'));
exit(0);
}
}
}
//function for update a overtime entry and overtime
$upd_sts = $this->ot_entry_update($overtime_request_id,$employee_code,$entry_date,$app_over_time,$first_approval_sts,$second_approval_sts,$cancel_request,$cancellation_reason,$first_approval_can_sts,$second_approval_can_sts,$approval_status,$prime_financial_id,$created_on,$ot_type,$category);
if((int)$upd_sts){
$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 $send_mail_status",'insert_id' => $form_id));
}
}
}
}
}
public function ot_entry_update($overtime_request_id,$employee_code,$entry_date,$app_over_time,$first_approval_sts,$second_approval_sts,$cancel_request,$cancellation_reason,$first_approval_can_sts,$second_approval_can_sts,$approval_status,$prime_financial_id,$created_on,$ot_type,$category){
$approved_date = "";
if($approval_status === 2 || $approval_status === 4){
$approved_date = date("Y-m-d");
}
$upd_ot_request_qry = 'UPDATE cw_overtime_request SET first_approval_status = "'.$first_approval_sts.'",second_approval_status = "'.$second_approval_sts.'",cancellation_request = "'.$cancel_request.'",cancellation_reason = "'.$cancellation_reason.'",first_approval_cancel_status = "'.$first_approval_can_sts.'",second_approval_cancel_status = "'.$second_approval_can_sts.'",approval_status = "'.$approval_status.'",over_time = "'.$app_over_time.'",approved_date = "'.$approved_date.'",trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'" WHERE cw_overtime_request.employee_code = "'.$employee_code.'" and prime_overtime_request_id = "'.$overtime_request_id.'" and cw_overtime_request.trans_status = 1';
$upd_ot_request_info = $this->db->query("CALL sp_a_run ('UPDATE','$upd_ot_request_qry')");
if($upd_ot_request_info){
$upd_ot_entry_qry = 'UPDATE cw_overtime_entry SET approval_status = "'.$approval_status.'",over_time = "'.$app_over_time.'",trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'" WHERE cw_overtime_entry.employee_code = "'.$employee_code.'" and prime_overtime_request_id = "'.$overtime_request_id.'" and trans_status = 1';
$upd_ot_entry_info = $this->db->query("CALL sp_a_run ('RUN','$upd_ot_entry_qry')");
if($upd_ot_entry_info){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function coff_update($employee_code,$prime_financial_id,$created_on,$category,$app_over_time,$approval_status,$entry_date,$form_id){
$app_over_time_mins = $this->hourstomins($app_over_time);
//Check Half day hours and Fullday Hours
$coff_based_on = $this->company_info[0]->coff_based_on; //Get from company info module.
$coff_check_qry = "";
if($coff_based_on !== 'role' && $coff_based_on !== '0'){
$coff_check_val = $this->emp_pick_arr[$employee_code][$coff_based_on];
$coff_check_qry = ' and FIND_IN_SET('.$coff_check_val.', cw_coff_settings.check_input)';
}
$check_inp_qry = 'SELECT halfday_min,fullday_min,expiry_days,max_ot_hours from cw_coff_settings where FIND_IN_SET('.$category.', cw_coff_settings.category) '.$coff_check_qry;
$check_inp_info = $this->db->query("CALL sp_a_run ('SELECT','$check_inp_qry')");
$check_inp_result = $check_inp_info->result();
$check_inp_info->next_result();
$halfday_min = $check_inp_result[0]->halfday_min;
$fullday_min = $check_inp_result[0]->fullday_min;
$expiry_day = $check_inp_result[0]->expiry_days;
$max_ot_hours = $check_inp_result[0]->max_ot_hours;
$credited_date = date("Y-m-d");
$expiry_date = date("Y-m-d",strtotime($credited_date. " + $expiry_day days"));
if($app_over_time_mins >= $halfday_min && $app_over_time_mins < $fullday_min){ //Half day cond
$coff_days = 0.5;
}else
if($app_over_time_mins >= $fullday_min){
$full_day = FLOOR($app_over_time_mins/$fullday_min);
$diff = $app_over_time_mins % $fullday_min;
$half_day = FLOOR($diff/$halfday_min);
if($half_day == '1'){
$half_day = 0.5;
}else{
$half_day = 0;
}
$coff_days = $full_day + $half_day;
}
if($approval_status === 2){
$update_qry = 'coff = coff+'.$coff_days.'';
}else{
$update_qry = 'coff = coff-'.$coff_days.'';
}
//coff_mins = coff_mins+'.$app_over_time_mins.',coff = IF(FLOOR((coff_mins+'.$app_over_time_mins.')/'.$fullday_min.') >= 1,FLOOR((coff_mins+'.$app_over_time_mins.')/'.$fullday_min.'),0) + IF(FLOOR(((coff_mins+'.$app_over_time_mins.') MOD '.$fullday_min.')/'.$halfday_min.') >= 1,0.5,0)
$upd_coff_entry_qry = 'UPDATE cw_leave_opening SET '.$update_qry.',trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'" WHERE cw_leave_opening.employee_code = "'.$employee_code.'" and cw_leave_opening.financial_setting_id = "'.$prime_financial_id.'" and trans_status = 1';
$upd_coff_entry_info = $this->db->query("CALL sp_a_run ('RUN','$upd_coff_entry_qry')");
if($upd_coff_entry_info){
if($approval_status === 2){
$coff_ins_qry = 'INSERT into cw_coff_entry(category,employee_code,credited_date,end_date,coff_mins,coff_count,balance_count,ot_hours,max_ot_hours,coff_hours,halfday_min,fullday_min,financial_setting_id,approved_id,trans_created_by,trans_created_date) values ("'.$category.'","'.$employee_code.'","'.$credited_date.'","'.$expiry_date.'","'.$app_over_time_mins.'","'.$coff_days.'","'.$coff_days.'","'.$app_over_time.'","'.$max_ot_hours.'","'.$app_over_time.'","'.$halfday_min.'","'.$fullday_min.'","'.$prime_financial_id.'","'.$form_id.'","'.$this->logged_id.'","'.$created_on.'")';
$upd_coff_entry_info = $this->db->query("CALL sp_a_run ('RUN','$coff_ins_qry')");
}else
if($approval_status === 4){
$upd_coff_entry_qry = 'UPDATE cw_coff_entry SET coff_status = "4",trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'" WHERE cw_coff_entry.employee_code = "'.$employee_code.'" and cw_coff_entry.financial_setting_id = "'.$prime_financial_id.'" and trans_status = 1';
$upd_coff_entry_info = $this->db->query("CALL sp_a_run ('RUN','$upd_coff_entry_qry')");
}
if($upd_coff_entry_info){
return true;
}
}
}
//UPDATE Bulk Approve
public function bulk_approve(){
$approve_ids = implode(",",$this->input->post('approve_ids'));
$can_process = true;
//Get Data from Overtime Approval Table
$check_table_query = 'SELECT cw_overtime_entry.prime_overtime_entry_id,cw_overtime_approval.prime_overtime_request_id,prime_time_entry_id,prime_overtime_approval_id,'.$this->prime_table.'.approval_type,cw_overtime_approval.first_level_approval,cw_overtime_approval.second_level_approval,cw_overtime_approval.first_approval_status,cw_overtime_approval.second_approval_status,cw_overtime_approval.approval_status,total_excess_hours,cw_overtime_approval.over_time,TIME_TO_SEC(cw_overtime_approval.over_time) as over_time_mins,'.$this->prime_table.'.ot_type,'.$this->prime_table.'.employee_code,'.$this->prime_table.'.category,'.$this->prime_table.'.cancellation_request,'.$this->prime_table.'.entry_date from '.$this->prime_table.' inner join cw_time_entry on cw_time_entry.employee_code = '.$this->prime_table.'.employee_code and cw_time_entry.att_date = '.$this->prime_table.'.entry_date inner join cw_overtime_request on cw_overtime_request.prime_overtime_request_id = '.$this->prime_table.'.prime_overtime_request_id inner join cw_overtime_entry on cw_overtime_entry.prime_overtime_request_id = cw_overtime_request.prime_overtime_request_id WHERE '.$this->prime_table.'.trans_status = 1 and cw_overtime_approval.approval_status = 1 and '. $this->prime_id .' in ('. $approve_ids .')';
$check_table_info = $this->db->query("CALL sp_a_run ('SELECT','$check_table_query')");
$check_table_rslt = $check_table_info->result();
$check_table_info->next_result();
//if(count($this->input->post('approve_ids')) === count($check_table_rslt)){
$logged_emp_code = $this->logged_emp_code;
$logged_id = $this->logged_id;
$ot_approve_upd = "";
$ot_entry_upd = "";
$ot_request_upd = "";
$time_entry_upd = "";
$can_process = false;
foreach ($check_table_rslt as $key => $value){
$prime_approval_id = $value->prime_overtime_approval_id;
$overtime_request_id = $value->prime_overtime_request_id;
$overtime_entry_id = $value->prime_overtime_entry_id;
$prime_time_entry_id = $value->prime_time_entry_id;
$approve_type = (int)$value->approval_type;
$first_level_approval = $value->first_level_approval;
$second_level_approval = $value->second_level_approval;
$first_approval_status = (int)$value->first_approval_status;
$second_approval_status = (int)$value->second_approval_status;
$approve_status = (int)$value->approval_status;
$total_excess_hours = (int)$value->total_excess_hours;
$employee_code = $value->employee_code;
$category = $value->category;
$approved_over_time = $value->over_time;
$ot_type = (int)$value->ot_type;
$entry_date = $value->entry_date;
$cancellation_request = $value->cancellation_request;
$approved_ot_mins = (int)$value->over_time_mins/60; //Hours converted to mins
//OT status update based on first and second level
if($logged_emp_code === $first_level_approval){
$first_approval_status = 2;
}
if($logged_emp_code === $second_level_approval){
$second_approval_status = 2;
}
if($approve_type === 1){
if($first_approval_status === 2 || $second_approval_status === 2){
$approve_status = 2;
$approve_date = date("Y-m-d");
}
}else
if($approve_type === 2){
if($first_approval_status === 2 && $second_approval_status === 2){
$approve_status = 2;
$approve_date = date("Y-m-d");
}
}else
if($approve_type === 3){
if($first_approval_status === 2){
$approve_status = 2;
$approve_date = date("Y-m-d");
}
}else
if($approve_type === 4){
if($first_approval_status === 2){
$approve_status = 2;
$approve_date = date("Y-m-d");
}
}
if((int)$cancellation_request === 1 && $approve_status === 2){
$approve_status = 4;
$approved_ot_mins = 0;
}
if($ot_type === 4){
$approved_qry_key = 'approved_ot_mins';
$approved_qry_val = 'approved_ot_mins = VALUES(approved_ot_mins)';
}else
if($ot_type === 3){
$approved_qry_key = 'approved_special_ot';
$approved_qry_val = 'approved_special_ot = VALUES(approved_special_ot)';
}else{
$approved_qry_key = 'approved_ot_mins';
$approved_qry_val = 'approved_ot_mins = VALUES(approved_ot_mins)';
}
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$created_on = date("Y-m-d H:i:s");
//if(($approved_ot_mins <= $total_excess_hours) && ($approved_ot_mins !== 0)){
if($approve_status === 2){
$ot_request_upd .= '("'.$overtime_request_id.'","'.$first_approval_status.'","'.$second_approval_status.'","'.$approve_status.'","'.$approve_date.'","'.$approved_over_time.'","'.$logged_id.'","'.$created_on.'"),';
$ot_entry_upd .= '("'.$overtime_entry_id.'","'.$approve_status.'","'.$approved_over_time.'","'.$logged_id.'","'.$created_on.'"),';
$ot_approve_upd .= '("'.$prime_approval_id.'","'.$first_approval_status.'","'.$second_approval_status.'","'.$approve_status.'","'.$approve_date.'","'.$logged_id.'","'.$created_on.'"),';
$time_entry_upd .= '("'.$prime_time_entry_id.'","'.$approved_ot_mins.'","'.$logged_id.'","'.$created_on.'"),';
}else{
$ot_request_upd .= '("'.$overtime_request_id.'","'.$first_approval_status.'","'.$second_approval_status.'","'.$approve_status.'","'.$approve_date.'","'.$approved_over_time.'","'.$logged_id.'","'.$created_on.'"),';
$ot_entry_upd .= '("'.$overtime_entry_id.'","'.$approve_status.'","'.$approved_over_time.'","'.$logged_id.'","'.$created_on.'"),';
$ot_approve_upd .= '("'.$prime_approval_id.'","'.$first_approval_status.'","'.$second_approval_status.'","'.$approve_status.'","'.$approve_date.'","'.$logged_id.'","'.$created_on.'"),';
}
//}
}
if($ot_approve_upd){
$ot_request_upd = rtrim($ot_request_upd,",");
$ot_entry_upd = rtrim($ot_entry_upd,",");
$ot_approve_upd = rtrim($ot_approve_upd,",");
$time_entry_upd = rtrim($time_entry_upd,",");
//OT APPROVAL UPD QUERY
$approval_upd_qry = 'INSERT INTO '.$this->prime_table.' (prime_overtime_approval_id,first_approval_status,second_approval_status,approval_status,approved_date,trans_updated_by,trans_updated_date) VALUES '.$ot_approve_upd.' ON DUPLICATE KEY UPDATE first_approval_status = VALUES(first_approval_status),second_approval_status = VALUES(second_approval_status),approval_status = VALUES(approval_status),approved_date = VALUES(approved_date),trans_updated_by = VALUES(trans_updated_by),trans_updated_date = VALUES(trans_updated_date)';
$approval_upd_info = $this->db->query("CALL sp_a_run ('RUN','$approval_upd_qry')");
if($approval_upd_info){
//OT ENTRY UPD QUERY
$ot_entry_upd_qry = 'INSERT INTO cw_overtime_entry (prime_overtime_entry_id,approval_status,over_time,trans_updated_by,trans_updated_date) VALUES '.$ot_entry_upd.' ON DUPLICATE KEY UPDATE approval_status = VALUES(approval_status),over_time = VALUES(over_time),trans_updated_by = VALUES(trans_updated_by),trans_updated_date = VALUES(trans_updated_date)';
$ot_entry_upd_info = $this->db->query("CALL sp_a_run ('RUN','$ot_entry_upd_qry')");
//OT REQUEST UPD QUERY
$request_upd_qry = 'INSERT INTO cw_overtime_request (prime_overtime_request_id,first_approval_status,second_approval_status,approval_status,approved_date,over_time,trans_updated_by,trans_updated_date) VALUES '.$ot_request_upd.' ON DUPLICATE KEY UPDATE first_approval_status = VALUES(first_approval_status),second_approval_status = VALUES(second_approval_status),approval_status = VALUES(approval_status),approved_date = VALUES(approved_date),over_time = VALUES(over_time),trans_updated_by = VALUES(trans_updated_by),trans_updated_date = VALUES(trans_updated_date)';
$request_upd_info = $this->db->query("CALL sp_a_run ('RUN','$request_upd_qry')");
if($request_upd_info){
if($time_entry_upd){
//Time Entry UPD QUERY to update Approved Overtime
$time_entry_upd_qry = 'INSERT INTO cw_time_entry (prime_time_entry_id,'.$approved_qry_key.',trans_updated_by,trans_updated_date) VALUES '.$time_entry_upd.' ON DUPLICATE KEY UPDATE '.$approved_qry_val.',trans_updated_by = VALUES(trans_updated_by),trans_updated_date = VALUES(trans_updated_date)';
$time_entry_upd_info = $this->db->query("CALL sp_a_run ('RUN','$time_entry_upd_qry')");
if($time_entry_upd_info){
if((int)$ot_type === 4){
$rslt = $this->coff_update($employee_code,$prime_financial_id,$created_on,$category,$approved_over_time,$approve_status,$entry_date,$prime_approval_id);
if($rslt){
echo json_encode(array('success' => TRUE, 'message' => "Approved Successfully.."));
}
}else{
echo json_encode(array('success' => TRUE, 'message' => "Approved Successfully.."));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve.. Please Try After Sometime..."));
}
}else{
echo json_encode(array('success' => TRUE, 'message' => "Approved Successfully.."));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve.. Please Try After sometime.."));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve.. Please Try After sometime..."));
}
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve.. Please check your Approved Overtime.."));
}
/*}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve.. Some Data Already Approved or Not Available in Time Entry..."));
}*/
}
//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
public function remove_file(){
$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['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);
}
}
?>