File: /home/cafsindia/cloud_cafsinfotech_in/application/controllers/Hr_overtime_entry.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Hr_overtime_entry extends Action_controller{
public function __construct(){
parent::__construct('hr_overtime_entry');
}
// 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;
$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;
/*APPROVAL STATUS 2 VIEW OPTION BY ANDREWS*/
$approval_type_qry = 'SELECT prime_time_entry_id,approval_type FROM cw_time_entry WHERE cw_time_entry.trans_status = 1';
$approval_type_info = $this->db->query("CALL sp_a_run ('SELECT','$approval_type_qry')");
$approval_type_rslt = $approval_type_info->result_array();
$approval_type_info->next_result();
$approval_type_arr = array_reduce($approval_type_rslt, function($result, $arr){
$result[$arr['prime_time_entry_id']] = $arr['approval_type'];
return $result;
}, array());
$data['approval_type_arr'] = $approval_type_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{
// add role based condition
$process_month = $this->input->post('process_month');
$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']);
//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;
//filter list
$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 */
$common_search = "";
if($search){
foreach($this->form_info 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));
if($label_id === 'approved_over_time'){
$label_id = 'approved_ot_mins';
}
$field_isdefault = (int)$setting->field_isdefault;
if($field_isdefault === 1){
if(($input_view_type === 1) || ($input_view_type === 2)){
$search_label = "cw_time_entry.$label_id";
if($search_label === "cw_time_entry.category"){
$search_label ="cw_employees.role";
}else if($search_label === "cw_time_entry.department"){
$search_label ="cw_employees.department";
}
$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);
}
}
if($process_month){
//get Process from date and to date.
$salary_date_arr = $this->tos_sal_strt_end_info("4",$process_month); // 4 => Posting Entry
$salary_start_date = date("Y-m-d",strtotime($salary_date_arr['salary_start_date']));
$salary_end_date = date("Y-m-d",strtotime($salary_date_arr['salary_end_date']));
}
$count_all_query = 'SELECT COUNT(*) as allcount from cw_time_entry inner join cw_approval_leave_status on cw_approval_leave_status.prime_approval_leave_status_id = cw_time_entry.approval_type inner join cw_shift_master on cw_shift_master.prime_shift_master_id = cw_time_entry.shift_id where cw_time_entry.trans_status = 1 and cw_time_entry.att_date >= "'.$salary_start_date.'" and cw_time_entry.att_date <= "'.$salary_end_date.'" and cw_time_entry.total_excess_hours > 0';
$search_total = $this->db->query($count_all_query);
$search_total_info = $search_total->result();
$total_count = $search_total_info[0]->allcount;
$search_query = 'SELECT cw_time_entry.employee_code,prime_time_entry_id,att_date,if(cw_time_entry.total_excess_hours > 0,time_format(concat(floor(cw_time_entry.total_excess_hours /60),":",lpad(mod(cw_time_entry.total_excess_hours,60),2,"0")),"%H:%i"),cw_time_entry.total_excess_hours) as total_excess_hours,if(cw_time_entry.total_work_hours > 0,time_format(concat(floor(cw_time_entry.total_work_hours /60),":",lpad(mod(cw_time_entry.total_work_hours,60),2,"0")),"%H:%i"),cw_time_entry.total_work_hours) as total_work_hours,cw_shift_master.shift_name as shift_id,cw_time_entry.punch_in,cw_time_entry.punch_out,approved_ot_mins as approved_over_time,cw_approval_leave_status.approval_leave_status as approval_type,cw_employees.role as category,cw_department.department,cw_day_status_legends.legends as whole_day_status from cw_time_entry inner join cw_employees on cw_employees.employee_code = cw_time_entry.employee_code inner join cw_department on cw_employees.department = cw_department.prime_department_id inner join cw_day_status_legends on cw_day_status_legends.legends = cw_time_entry.whole_day_status inner join cw_approval_leave_status on cw_approval_leave_status.prime_approval_leave_status_id = cw_time_entry.approval_type inner join cw_shift_master on cw_shift_master.prime_shift_master_id = cw_time_entry.shift_id where cw_time_entry.trans_status = 1 and cw_time_entry.att_date >= "'.$salary_start_date.'" and cw_time_entry.att_date <= "'.$salary_end_date.'" and cw_time_entry.total_excess_hours > 0 '.$common_search .$fliter_query .$role_condition;
$search_query .= "ORDER BY $order_col $order_sor ";
if((int)$per_page !== -1){
$search_query .= " LIMIT $start,$per_page";
}
$search_data = $this->db->query($search_query);
$search_result = $search_data->result();
$search_count = count($search_result);
$filtered_count = $total_count;
if($search_count < $per_page){
$filtered_count = $search_count;
}
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_query ='SELECT *,cw_employees.department,cw_employees.role as category,prime_time_entry_id,att_date,if(cw_time_entry.total_excess_hours > 0,time_format(concat(floor(cw_time_entry.total_excess_hours /60),":",lpad(mod(cw_time_entry.total_excess_hours,60),2,"0")),"%H:%i"),cw_time_entry.total_excess_hours) as total_excess_hours,if(cw_time_entry.total_work_hours > 0,time_format(concat(floor(cw_time_entry.total_work_hours /60),":",lpad(mod(cw_time_entry.total_work_hours,60),2,"0")),"%H:%i"),cw_time_entry.total_work_hours) as total_work_hours,if(cw_time_entry.approved_ot_mins > 0,time_format(concat(floor(cw_time_entry.approved_ot_mins /60),":",lpad(mod(cw_time_entry.approved_ot_mins,60),2,"0")),"%H:%i"),cw_time_entry.approved_ot_mins) as approved_over_time,cw_time_entry.approval_type from cw_time_entry inner join cw_employees on cw_employees.employee_code = cw_time_entry.employee_code inner join cw_department on cw_employees.department = cw_department.prime_department_id where cw_time_entry.prime_time_entry_id = "'.$form_view_id.'"and cw_time_entry.trans_status = 1';
$view_data = $this->db->query("CALL sp_a_run ('SELECT','$view_query')");
$view_result = $view_data->result();
$view_data->next_result();
$data['form_view'] = $view_result[0];
$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_data = $this->db->query("CALL sp_a_run ('SELECT','$view_qry')");
$view_result = $view_data->result();
$view_data->next_result();
$row_view_list = array();
foreach($view_result 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{
$financial_info = $this->get_leave_financial_details();
$prime_financial_id = $financial_info[0]->prime_leave_financial_year_id;
$att_date = $this->input->post('att_date');
$category = $this->input->post('category');
$prime_time_entry_id = $this->input->post("prime_time_entry_id");
$rejected_reason = $this->input->post("rejected_reason");
$approval_type = $this->input->post("approval_type");
$total_work_hours = $this->input->post("total_work_hours");
$approved_over_time = $this->input->post("approved_over_time");
$total_excess_hours = $this->input->post("total_excess_hours");
$employee_code = $this->input->post("employee_code");
$total_min = $this->hourstomins($approved_over_time);
$created_on = date("Y-m-d h:i:s");
if($total_excess_hours < $approved_over_time){
echo json_encode(array('success' => FALSE, 'message' => 'Approved Overtime Should Be Equal Or Less Than Overtime Worked!!'));
exit(0);
}
if($approved_over_time === "00:00"){
echo json_encode(array('success' => FALSE, 'message' => 'Approved Overtime Should not be Zero!!'));
exit(0);
}
//Get Month Days start and end date
$month_day_qry = 'select day_conditions,day_count,day_start,day_end from cw_tos_month_day where cw_tos_month_day.trans_status = 1 and entry_parameter = 4';
$month_day_data = $this->db->query("CALL sp_a_run ('SELECT','$month_day_qry')");
$month_day_result = $month_day_data->result();
$month_day_data->next_result();
if($month_day_result){
$day_conditions = $month_day_result[0]->day_conditions;
$day_count = $month_day_result[0]->day_count;
$day_start = $month_day_result[0]->day_start;
$day_end = $month_day_result[0]->day_end;
if((int)$day_conditions === 3){
if(date('d',strtotime($att_date)) > $day_end){
$end_month = date("Y-m-".$day_end,strtotime("+1 month", strtotime($att_date)));
}else{
$end_month = date("Y-m-".$day_end, strtotime($att_date));
}
}else{
$sal_start = '01';
if((int)$day_conditions === 2){
$day_end = date("t");
}
$end_month = date("Y-m-".$day_end,strtotime($att_date));
}
}
$process_month = date("m-Y",strtotime($end_month));
$exist_posting_lock_cateory_qry = 'SELECT count(*) as exist_count from cw_payroll_posting_lock where trans_status = 1 and category = "'.$category.'" and posting_month = "'.$process_month.'"';
$exist_posting_lock_cateory_info = $this->db->query("CALL sp_a_run ('SELECT','$exist_posting_lock_cateory_qry')");
$exist_posting_lock_cateory_rslt = $exist_posting_lock_cateory_info->result();
$exist_posting_lock_cateory_info->next_result();
$exist_count = (int)$exist_posting_lock_cateory_rslt[0]->exist_count;
if($exist_count > 0){
echo json_encode(array('success' => false, 'message' => "Payroll Lock for this Month!"));
exit(0);
}
$exist_emp_payroll_process_qry = 'SELECT count(*) as exist_payroll_count from cw_transactions where trans_status = 1 and employee_code = "'.$employee_code.'" and transactions_month = "'.$process_month.'" ';
$exist_emp_payroll_process_info = $this->db->query("CALL sp_a_run ('SELECT','$exist_emp_payroll_process_qry')");
$exist_emp_payroll_process_result = $exist_emp_payroll_process_info->result();
$exist_emp_payroll_process_info->next_result();
$exist_payroll_count = (int)$exist_emp_payroll_process_result[0]->exist_payroll_count;
if($exist_payroll_count > 0){
echo json_encode(array('success' => false, 'message' => "Payroll Already Processed for this Month!"));
exit(0);
}
/*this code add by andrews start*/
$form_id = (int)$this->input->post($this->prime_id);
$ot_type = 4;
$this->compoff_validation($employee_code,$category,$approved_over_time,$ot_type);
$this->coff_update($employee_code,$prime_financial_id,$created_on,$category,$approved_over_time,$approval_type,$form_id,$att_date);
/*this code add by andrews end*/
$prime_upd_query .= 'approval_type = "'.$approval_type.'",approved_ot_mins = "'.$total_min.'",rejected_reason = "'.$rejected_reason.'", trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
$prime_update_query = 'UPDATE cw_time_entry SET '. $prime_upd_query .' WHERE prime_time_entry_id = "'. $prime_time_entry_id .'"';
$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $prime_time_entry_id));
}
}
/*this function add by andrews start*/
public function coff_update($employee_code,$prime_financial_id,$created_on,$category,$approved_over_time,$approval_type,$form_id,$att_date){
$att_date = date('Y-m-d',strtotime($att_date));
$app_over_time_mins = $this->hourstomins($approved_over_time);
//Check Half day hours and Fullday Hours
$company_info = $this->company_info();
$coff_based_on = $company_info[0]->coff_based_on; //Get from company info module.
$coff_check_qry = "";
if($coff_based_on !== 'role' && $coff_based_on !== '0'){
$emp_pick_arr = $this->get_emp_data();
$coff_check_val = $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){
if((int)$app_over_time_mins === 0 || (int)$fullday_min === 0){
$full_day = 0;
}else{
$full_day = FLOOR($app_over_time_mins/$fullday_min);
}
if((int)$app_over_time_mins === 0 || (int)$fullday_min === 0){
$diff = 0;
}else{
$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((int)$approval_type === 2){
$update_qry = 'coff = coff+'.$coff_days.'';
}else{
$update_qry = 'coff = coff-'.$coff_days.'';
}
$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((int)$approval_type === 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,att_date,trans_created_by,trans_created_date) values ("'.$category.'","'.$employee_code.'","'.$credited_date.'","'.$expiry_date.'","'.$app_over_time_mins.'","'.$coff_days.'","'.$coff_days.'","'.$approved_over_time.'","'.$max_ot_hours.'","'.$approved_over_time.'","'.$halfday_min.'","'.$fullday_min.'","'.$prime_financial_id.'","'.$form_id.'","'.$att_date.'","'.$this->logged_id.'","'.$created_on.'")';
$upd_coff_entry_info = $this->db->query("CALL sp_a_run ('RUN','$coff_ins_qry')");
}else
if((int)$approval_type === 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;
}
}
}
/*this function add by andrews end*/
//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(){
$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"));
}
}
}
?>