File: //home/cafsindia/hrms_allyindian_com/application_bk/controllers/Shift_muster_report.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Shift_muster_report extends Action_controller{
public function __construct(){
parent::__construct('shift_muster_report');
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
//* EMPLOYEE COMPONENT PICKLIST
$get_match_columns = 'SELECT prime_form_id,prime_module_id,label_name,view_name FROM `cw_form_setting` WHERE prime_module_id = "employees" and label_name not in ("emp_name","employee_code","role") and transaction_type in ("1","4") order by view_name asc';
$match_column_info = $this->db->query("CALL sp_a_run ('SELECT','$get_match_columns')");
$match_column_result = $match_column_info->result();
$match_column_info->next_result();
$match_columns[""] = "---- Select Column ----";
foreach($match_column_result as $match_column){
$prime_form_id = $match_column->prime_form_id;
$column_value = $match_column->label_name;
$view_name = $match_column->view_name;
$match_columns[$this->xss_clean($column_value)] = $this->xss_clean($view_name);
}
$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;
$data['emp_component_list'] = $match_columns;
$data['key'] = $this->generateKey();
$this->load->view("$this->control_name/manage",$data);
}
//SAVE MODEL DATA TO DATA BASE
public function generate_muster_report(){
$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);
}
//SEARCH INFO FUNCTION
$this->search_info();
$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;
$fliter_query = "";
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_component = ltrim(implode('","',$this->input->post('emp_component[]') ?? []),',');
$report_period = (int)$this->input->post('report_period');
$month_and_year = $this->input->post('month_and_year');
$not_allotted = $this->input->post('not_allotted');
$process_date = date("Y-m-d",strtotime("01-".$month_and_year));
$start_date = date("Y-m-d",strtotime("01-".$month_and_year));
if($report_period === 1){
//report type 1
$start_date = $process_date;
$end_date = date('Y-m-t',strtotime($process_date));
}else{
//report type 2
$start_date = date("Y-m-d",strtotime($this->input->post('from_date')));
$end_date = date("Y-m-d",strtotime($this->input->post('to_date')));
}
if($start_date > $end_date){
echo json_encode(array('success' => false,'message' => "From Date Should Be Less Than To Date"));
exit(0);
}
if($emp_component){
//FOR DATE FORMAT AND PICKLIST VALUE SHOW IN TABLE ROW OR EXCEL ROW
$find_label_qry = 'select * from cw_form_setting where trans_status = 1 and prime_module_id = "employees" and label_name in ("'.$emp_component.'")';
$find_label_info = $this->db->query("CALL sp_a_run ('SELECT','$find_label_qry')");
$label_result = $find_label_info->result();
$find_label_info->next_result();
$select_query = "";
foreach($label_result as $rslt){
$prime_form_id = (int)$rslt->prime_form_id;
$prime_module_id = $rslt->prime_module_id;
$input_view_type = (int)$rslt->input_view_type;
$input_for = (int)$rslt->input_for;
$field_type = (int)$rslt->field_type;
$label_id = $rslt->label_name;
$label_name = ucwords($rslt->view_name);
$field_length = $rslt->field_length;
$field_decimals = $rslt->field_decimals;
$pick_list_type = (int)$rslt->pick_list_type;
$pick_list = $rslt->pick_list;
$pick_table = $rslt->pick_table;
$auto_prime_id = $rslt->auto_prime_id;
$auto_dispaly_value = $rslt->auto_dispaly_value;
$field_isdefault = (int)$rslt->field_isdefault;
$pick_display = $rslt->pick_display_value;
$table_name = "cw_employees";
if($field_isdefault === 1){
$pick_sel_table = "$table_name";
}
if((int)$prime_form_id === 0){
$label_name = ucwords(str_replace("_"," ",$label_list));
$label_id = $label_list;
}
//TABLE HEADER
$table_array[] = array('field_type'=>$field_type,'label_name'=>$label_id,'view_name'=>$label_name);
$array_list = array();
//SEARCH FILTERS
if($field_type === 4){
$select_query .= ',CASE WHEN '.$pick_sel_table.'.'.$label_id.' IS NULL or '.$pick_sel_table.'.'.$label_id.' = "1970-01-01" or '.$pick_sel_table.'.'.$label_id.' = "0000-00-00" then "" else DATE_FORMAT('.$pick_sel_table.'.'.$label_id.', "%d-%m-%Y") end as '.$label_id;
}else
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];
//pick query for get a picklist data
$pick_query = "select $pick_list from $pick_table where trans_status = 1 $where_condition";
$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;
}
// $this->all_pick[$prime_form_id] = $array_list;
$pick_query_as = $pick_table."_".$prime_form_id;
if(($input_view_type === 1) || ($input_view_type === 2)){
if($pick_table === "cw_employees"){
$select_query .= ',CONCAT_WS(" - ", '.$pick_query_as.'.'.$pick_list_val_1.','.$pick_query_as.'.'.$pick_list_val_2.') as '.$label_id;
}else{
$select_query .= ",$pick_query_as.$pick_list_val_2 as $label_id";
}
$pick_query_join .= " left join $pick_table as $pick_query_as on $pick_query_as.$pick_list_val_1 = $pick_sel_table.$label_id ";
}
}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;
}
// $this->all_pick[$prime_form_id] = $array_list;
$pick_query_as = $pick_table."_".$prime_form_id;
if(($input_view_type === 1) || ($input_view_type === 2)){
$select_query .= ",$pick_query_as.$pick_list_val_2 as $label_id";
$pick_query_join .= " left join $pick_table as $pick_query_as on $pick_query_as.$pick_list_val_1 = $pick_sel_table.$label_id ";
}
}
}else
if($field_type === 9){
$pick_query_as = $pick_table."_".$prime_form_id;
if(($input_view_type === 1) || ($input_view_type === 2)){
$select_query .= ",$pick_query_as.$auto_dispaly_value as $label_id";
$pick_query_join .= " left join $pick_table as $pick_query_as on $pick_query_as.$auto_prime_id = $pick_sel_table.$label_id ";
}
}else
if($field_type === 13){
$select_query .= ',DATE_FORMAT('.$pick_sel_table.'.'.$label_id.', "%d-%m-%Y %H:%i:%s") as '.$label_id;
}else{
if(($input_view_type === 1) || ($input_view_type === 2)){
if($field_isdefault === 1){
$select_query .= ",$pick_sel_table.$label_id";
}else
if($field_isdefault === 2){
$select_query .= ",$pick_sel_table.$label_id";
}
}
}
}
}
$role_condition = "";
if($this->role_condition){
$role_condition = $this->role_condition; //$start_date
}
// $select_query = rtrim($select_query,",");
$emp_qry = 'SELECT cw_employees.employee_code,cw_employees.emp_name,cw_category.category_name as role,date_of_joining as doj,termination_status as term_sts,resignation_date as resign_date'.$select_query.' FROM `cw_employees` inner join cw_category on cw_category.prime_category_id = cw_employees.role '.$pick_query_join.' where cw_employees.trans_status = 1 and cw_employees.user_right !=1 and cw_employees.role !=1 and (cw_employees.termination_status = 0 or cw_employees.resignation_date >= "'.$start_date.'") '.$fliter_query.' '.$role_condition;
$emp_info = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
$emp_rslt = $emp_info->result_array();
$emp_info->next_result();
$filter_list = $this->get_filter_data($emp_rslt);
$shift_name_qry = 'SELECT cw_shift_master.short_name,cw_shift_import.shift_date,employee_code FROM cw_shift_import inner join cw_shift_master on cw_shift_master.prime_shift_master_id = cw_shift_import.shift_name 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_name_qry')");
$shift_rslt = $shift_info->result_array();
$shift_info->next_result();
$shift_name_arr = array();
foreach ($shift_rslt as $key => $arr) {
$shift_name_arr[$arr['employee_code']][$arr['shift_date']] = $arr['short_name'];
}
/*$shift_name_arr = array_reduce($shift_rslt, function ($result, $arr) {
$result[$arr['employee_code']][$arr['shift_date']] = $arr['short_name'];
return $result;
}, array());*/
$emp_comp_arr = explode('","', $emp_component);
$date_period = new DatePeriod(
new DateTime($start_date),
new DateInterval('P1D'),
new DateTime(date("Y-m-d",strtotime("+1 days",strtotime($end_date))))
);
/*echo "<pre>";
print_r($date_period); die;*/
$weekoff_import_qry = 'SELECT employee_code,employee_name,weekoff_date,weekoff_type FROM `cw_weekoff_import` WHERE trans_status = 1 and weekoff_date >="'.$start_date.'" and weekoff_date <="'.$end_date.'"';
$weekoff_info = $this->db->query("CALL sp_a_run ('SELECT','$weekoff_import_qry')");
$weekoff_rslt = $weekoff_info->result_array();
$weekoff_info->next_result();
$weekoff_import_arr = array();
foreach ($weekoff_rslt as $key => $arr) {
$weekoff_import_arr[$arr['employee_code']][$arr['weekoff_date']] = $arr['weekoff_type'];
}
$tr_line = "";
$not_exist_arr = array();
$emp_arr = array();
foreach($emp_rslt as $key => $emp_data){
$employee_code = $emp_data['employee_code'];
$emp_name = $emp_data['emp_name'];
$category = $emp_data['role'];
$emp_act_sts = (int)$emp_data['term_sts'];
$emp_doj = date("Y-m-d",strtotime($emp_data['doj']));
$emp_resign_date = $emp_data['resign_date'];
$tr_head = "<tr><th>Category</th><th>Employee Code</th><th>Employee Name</th>";
$comp_line = "";
foreach ($emp_comp_arr as $key => $value){
if($value){
$comp_val = $emp_data[$value];
if($value === "role"){
$value = 'Category';
}
$value = str_replace("_"," ",$value);
$tr_head .= "<th>$value</th>";
$comp_line .= "<td>$comp_val</td>";
}
}
$date_line = "";
foreach ($date_period as $key => $value){
$day = $value->format('d-m-Y');
$shift_date = $value->format('Y-m-d');
$weekoff_date = $value->format('Y-m-d');
$shift_details = $shift_name_arr[$employee_code][$shift_date];
$weekoff_details = $weekoff_import_arr[$employee_code][$weekoff_date];
$tr_head .= "<th>$day</th>";
if($weekoff_details && $shift_details){
$date_line .= "<td style='white-space: nowrap;'> W | $shift_details </td>";
}else
if($weekoff_details){
if(($emp_act_sts === 1 && date("Y-m-d",strtotime($emp_resign_date)) > $shift_date) || ($emp_doj <= $shift_date)){
$not_exist_arr[$shift_date] = "-";
$emp_arr[$employee_code] = $emp_data;
}
$date_line .= "<td style='white-space: nowrap;'>W | - </td>";
}else
if($shift_details){
$date_line .= "<td style='white-space: nowrap;'>$shift_details</td>";
}else{
if(($emp_act_sts === 1 && date("Y-m-d",strtotime($emp_resign_date)) > $shift_date) || ($emp_doj <= $shift_date)){
$not_exist_arr[$shift_date] = "-";
$emp_arr[$employee_code] = $emp_data;
}
$date_line .= "<td>-</td>";
}
}
$tr_line .= "<tr><td>$category</td><td>$employee_code</td><td>$emp_name</td>$comp_line $date_line</tr>";
$tr_head = "<tr>$tr_head</tr>";
}
ksort($not_exist_arr); // order by date asc
if((int)$not_allotted === 1){ //Only List Shift not allocated employees
$tr_line = "";
foreach ($emp_arr as $emp_code => $emp_det) {
$category = $emp_det['role'];
$emp_name = $emp_det['emp_name'];
$comp_line = "";
$tr_head = "<tr><th>Category</th><th>Employee Code</th><th>Employee Name</th>";
foreach ($emp_comp_arr as $key => $value){
if($value){
$comp_val = $emp_det[$value];
if($value === "role"){
$value = 'Category';
}
$value = str_replace("_"," ",$value);
$tr_head .= "<th>$value</th>";
$comp_line .= "<td>$comp_val</td>";
}
}
$date_line = "";
foreach ($not_exist_arr as $date => $val){
$shift_details = $shift_name_arr[$emp_code][$date];
$weekoff_details = $weekoff_import_arr[$emp_code][$date];
$day = date("d-m-Y",strtotime($date));
$tr_head .= "<th>$day</th>";
if($weekoff_details && $shift_details){
$date_line .= "<td style='white-space: nowrap;'> W | $shift_details </td>";
}else
if($weekoff_details){
$date_line .= "<td style='white-space: nowrap;'>W | - </td>";
}else
if($shift_details){
$date_line .= "<td style='white-space: nowrap;'>$shift_details</td>";
}else{
$date_line .= "<td style='white-space: nowrap;'>$val</td>";
}
}
$tr_line .= "<tr><td>$category</td><td>$emp_code</td><td>$emp_name</td>$comp_line $date_line</tr>";
$tr_head = "<tr>$tr_head</tr>";
}
}
$table_content = "<div style='margin:20px;'>
<table class='table table-striped table-bordered' id='shift_muster_report'>
<thead>
$tr_head
</thead>
<tbody>
$tr_line
</tbody>
</table>
</div>";
echo json_encode(array('success'=>true,'table_content'=>$table_content));
}
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;
}
//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);
}
}
?>