File: /home/cafsindia/cpaqua.cafsinfotech.in/dump/application/controllers/Error_log.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Error_log extends Action_controller{
public function __construct(){
parent::__construct('error_log');
$this->collect_base_info();
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
$data['encKey'] = $this->generateKey();
//TEAM
$team_qry = 'SELECT prime_team_list_id,team FROM cw_team_list WHERE trans_status = 1 ';
$team_info = $this->db->query("CALL sp_a_run ('SELECT','$team_qry')");
$team_rslt = $team_info->result();
$team_info->next_result();
$team_list[""] = "---- TEAM ----";
foreach($team_rslt as $for){
$prime_id = $for->prime_team_list_id;
$team = $for->team;
$team_list[$prime_id] = $team;
}
$data['team'] = $team_list;
$this->load->view("$this->control_name/manage",$data);
}
//AUTOCOMPLETE
public function rea_suggest_name(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
$search_term = $this->input->post_get('term');
$final_qry = 'SELECT rea FROM cw_project_list WHERE trans_status = 1 AND rea like "%'.$search_term.'%"';
$final_data = $this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
$final_result = $final_data->result();
$final_data->next_result();
foreach($final_result as $rslt){
$rea = $rslt->rea;
$suggestions[] = array('value' => "$rea", 'label' =>"$rea");
}
if(empty($suggestions)){
$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
}
echo json_encode($suggestions);
}
//FETCH PROJECT DATA BASED ON REA
public function fetch_rea_data(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
$rea = $this->input->post('rea');
$pro_qry = 'SELECT project_name,client_name,prime_project_list_id FROM cw_project_list WHERE trans_status = 1 AND rea = "'.$rea.'"';
$pro_data = $this->db->query("CALL sp_a_run ('SELECT','$pro_qry')");
$pro_rslt = $pro_data->result();
$pro_data->next_result();
$project = $pro_rslt[0]->project_name;
$client = $pro_rslt[0]->client_name;
$pro_id = $pro_rslt[0]->prime_project_list_id;
echo json_encode(array('success' => TRUE, 'project' => $project, 'client' => $client, 'pro_id' => $pro_id));
}
//FETCH DATA TO DISPLAY FOR ESTIMATOR,CHECKER AND RECHECKER
public function fetch_estimator(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
$pro_id = $this->input->post('pro_id');
$team = $this->input->post('team');
//ESTIMATOR
$ass_qry = 'SELECT prime_assignment_id,estimator,emp_name FROM cw_assignment INNER JOIN cw_employees ON cw_assignment.estimator = cw_employees.employee_code WHERE cw_assignment.trans_status = 1 AND cw_employees.trans_status = 1 AND pro_id = "'.$pro_id.'" AND estimation_team = "'.$team.'" GROUP BY cw_assignment.estimator';
$ass_data = $this->db->query("CALL sp_a_run ('SELECT','$ass_qry')");
$ass_rslt = $ass_data->result();
$ass_data->next_result();
$est_list = "<option value = ''>---- Estimator ----</option>";
foreach($ass_rslt as $for){
$prime_id = $for->prime_assignment_id;
$estimator = $for->estimator;
$emp_name = $for->emp_name;
$est_list .= "<option value='$estimator'> $estimator - $emp_name </option>";
}
//CHECKER
$chk_qry = 'SELECT employee_code,emp_name FROM cw_employees WHERE trans_status = 1 AND estimation_team = "'.$team.'" AND role in (13,15) ';
$chk_data = $this->db->query("CALL sp_a_run ('SELECT','$chk_qry')");
$chk_rslt = $chk_data->result();
$chk_data->next_result();
$chklist = "<option value = ''>---- Checker ----</option>";
foreach($chk_rslt as $for){
$emp_code = $for->employee_code;
$emp_name = $for->emp_name;
$chklist .= "<option value='$emp_code'> $emp_code - $emp_name </option>";
}
//RECHECKER
$rechk_qry = 'SELECT employee_code,emp_name FROM cw_employees WHERE trans_status = 1 AND estimation_team = "'.$team.'" AND role in (12,15) ';
$rechk_data = $this->db->query("CALL sp_a_run ('SELECT','$rechk_qry')");
$rechk_rslt = $rechk_data->result();
$rechk_data->next_result();
$rechklist = "<option value = ''>---- Re-checker ----</option>";
foreach($rechk_rslt as $for){
$emp_code = $for->employee_code;
$emp_name = $for->emp_name;
$rechklist.= "<option value='$emp_code'> $emp_code - $emp_name </option>";
}
echo json_encode(array('success' => True,'est_rslt' => $est_list,'chk_rslt' => $chklist,'rechk_rslt' => $rechklist));
}
//INSERT AND UPDATE ERROR DATA
public function error_data_save(){
//Encryption
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
$jsonData_arr = $this->input->post('jsonData');
$err_id = $jsonData_arr['err_id'];
$rea = $jsonData_arr['rea'];
$project = $jsonData_arr['project'];
$project_name = $jsonData_arr['project_name'];
$client_name = $jsonData_arr['client_name'];
$job_weight = $jsonData_arr['job_weight'];
$err_ton = $jsonData_arr['err_ton'];
$weight_diff = $jsonData_arr['weight_diff'];
$err_impact = $jsonData_arr['err_impact'];
$err_type = $jsonData_arr['err_type[]'];
$team = $jsonData_arr['team'];
$estimator = $jsonData_arr['estimator'];
$checker = $jsonData_arr['checker'];
$rechecker = $jsonData_arr['rechecker'];
$err_report = $jsonData_arr['err_report'];
$err_log = $jsonData_arr['err_log'];
$sub_date = date("Y-m-d",strtotime($jsonData_arr['sub_date']));
$err_receive_date = date("Y-m-d",strtotime($jsonData_arr['err_receive_date']));
$prop_date = date("Y-m-d",strtotime($jsonData_arr['prop_date']));
$err_dec_date = date("Y-m-d",strtotime($jsonData_arr['err_dec_date']));
# ATTACHMENT
$filename = $_FILES['pro_docs']['name'];
$tmp_name = $_FILES['pro_docs']['tmp_name'];
$filetype = $_FILES['pro_docs']['type'];
$filesize = $_FILES['pro_docs']['size'];
$err_remarks = $jsonData_arr['err_remarks'];
$reason_behind = $jsonData_arr['reason_behind'];
$soln = $jsonData_arr['soln'];
$action_taken = $jsonData_arr['action_taken'];
$created_on = date("Y-m-d h:i:s");
$pro_docs = [];
$doc_files = '';
$file_path = "./error_declaration/docs/";
if(!file_exists($file_path)){
mkdir($file_path, 0777, true);
}
$format = ['text/xml','image/png','image/jpeg','text/plain','application/pdf','application/zip','application/vnd.ms-excel','application/vnd.oasis.opendocument.spreadsheet','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.ms-outlook','message/rfc822','application/x-zip-compressed','application/octet-stream'];
# FILE TYPE VALIDATION
foreach($filetype as $type){
if(!in_array($type, $format)){
echo json_encode(array('success' => False,'message' => "File type $type is not allowed."));
exit(0);
}
}
# FILE SIZE VALIDATION
foreach($filesize as $size){
if($size > 5242880){ # MB IN BYTES (5 * 1024) * 5024
echo json_encode(array('success' => False,'message' => "File size above 5MB not allowed."));
exit(0);
}
}
if($filename){
for($i = 0; $i < count($filename);$i++){
$pro_docs[] = ['name' => $filename[$i],'tmp_name' => $tmp_name[$i]];
}
}
$doc_files = array();
foreach($pro_docs as $val){
$name = str_replace(' ', '_', $val['name']);
if(file_exists($file_path.$name)){
$doc_files[] = $file_path.$name;
}else{
$tmp_file = $val['tmp_name'];
$random = rand(1000000000, 9999999999);
$filename = $random.'_'.$name;
$doc_files[] = $file_path .$filename;
$upload_file = $file_path .$filename;
move_uploaded_file($tmp_file, $upload_file);
}
}
$doc_files = implode(',',$doc_files);
$err_qry = 'SELECT * from cw_error_declaration WHERE trans_status = 1 AND prime_error_declaration_id = "'.$err_id.'"';
$err_info = $this->db->query("CALL sp_a_run ('SELECT','$err_qry')");
$err_rslt = $err_info->result();
$err_info->next_result();
$err_count = count($err_rslt);
# DATA TO INSERT/UPDATE
$data = [
'rea' => $rea,
'project' => $project,
'project_name' => $project_name,
'client_name' => $client_name,
'job_weight' => $job_weight,
'err_ton' => $err_ton,
'weight_diff' => $weight_diff,
'err_impact' => $err_impact,
'err_type' => $err_type,
'team' => $team,
'estimator' => $estimator,
'checker' => $checker,
'rechecker' => $rechecker,
'err_report' => $err_report,
'err_log' => $err_log,
'sub_date' => $sub_date,
'prop_date' => $prop_date,
'err_dec_date' => $err_dec_date,
'err_remarks' => $err_remarks,
'reason_behind' => $reason_behind,
'soln' => $soln,
'action_taken' => $action_taken,
'err_documents' => $doc_files,
'err_receive_date'=> $err_receive_date,
];
if((int)$err_count === 0){ # INSERT
$data['trans_created_date'] = $created_on;
$data['trans_created_by'] = $this->logged_id;
$this->db->insert('cw_error_declaration', $data);
echo json_encode(array('success' => true,'message' => 'Error log Inserted Successfully!'));
exit(0);
}else{ # UPDATE
$data['trans_updated_date'] = $created_on;
$data['trans_updated_by'] = $this->logged_id;
$this->db->where('prime_error_declaration_id', $err_id);
$this->db->update('cw_error_declaration', $data);
echo json_encode(array('success' => true,'message' => 'Error log Updated Successfully!'));
exit(0);
}
}
// PROJECT DATA ONLOAD
public function search(){
$dec_data = $this->cryptoDecrypt($_POST['Payload']);
$_POST = $dec_data['d'];
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..'));
exit(0);
}
$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']);
$logged_est_team = $this->session->userdata('logged_est_team');
$error_type = $this->input->post('fil_err_type');
$error_impact = $this->input->post('fil_err_impact');
$fil_team = $this->input->post('fil_team');
$dec_date = $this->input->post('fil_dec_date');
//FILTER
$filter_query = "";
if(!empty($fil_team) && array_filter($fil_team)){
$team_list = implode(',', $fil_team);
$filter_query .= ' AND team IN (' . $team_list . ')';
}
if($error_type !== ''){
$filter_query .= ' AND err_type = "'.$error_type.'"';
}
if($error_impact !== ''){
$filter_query .= ' AND err_impact = "'.$error_impact.'"';
}
if($dec_date !== ''){
$dec_date = date('Y-m-d',strtotime($dec_date));
$filter_query .= ' AND err_dec_date = "'.$dec_date.'"';
}
$common_search = "";
if($search){
$common_search .= ' or client_name like "'.$search.'%" or rea like "'.$search.'%" or project_name like "'.$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);
}
$search_query = 'SELECT * FROM cw_error_declaration WHERE trans_status = 1 '.$filter_query.' '.$common_search.' ';
$search_query .= " ORDER BY $order_col $order_sor";
if((int)$per_page !== -1){
$search_query .= " LIMIT $start,$per_page";
}
$count_all_query = 'SELECT count(*) as allcount FROM cw_error_declaration where trans_status = 1 ';
$count_query = 'SELECT count(*) as allcount FROM cw_error_declaration where trans_status = 1 '.$filter_query.' '.$common_search.' ' ;
$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));
}
//ERROR DATA EDIT
public function error_edit(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
exit(0);
}
$prime_id = $_POST['Payload'];
$err_qry = 'SELECT * from cw_error_declaration WHERE trans_status = 1 AND prime_error_declaration_id = "'.$prime_id.'" ';
$err_info = $this->db->query("CALL sp_a_run ('SELECT','$err_qry')");
$err_rslt = $err_info->result();
$err_info->next_result();
echo json_encode(array('success' => True,'err_rslt' => $err_rslt));
}
}
?>