nhance/app/Controllers/EmployeeServiceController.php
2024-02-26 13:32:08 +05:30

189 lines
10 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\EmployeeModel;
use App\Models\EmployeePolicyModel;
use App\Models\ClientModel;
use App\Models\FileModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class EmployeeServiceController extends AdminController
{
protected $myLogger;
protected $employeeModel;
protected $employeePolicyModel;
protected $clientModel;
protected $fileModel;
protected $general_relationships = ['self' => ['name' => 'Self', 'sex' => 'M','age_min' => 18,'age_max' => null], 'spouse' => ['name' => 'Spouse', 'sex' => 'F','age_min' => 18,'age_max' => null], 'son' => ['name' => 'Son', 'sex' => 'M','age_min' => null,'age_max' => 25], 'daughter' => ['name' => 'Daughter', 'sex' => 'F','age_min' => null,'age_max' => 25], 'father' => ['name' => 'Father', 'sex' => 'M','age_min' => 18,'age_max' => null], 'mother' => ['name' => 'Mother', 'sex' => 'F','age_min' => 18,'age_max' => null], 'father-in-law' => ['name' => 'Father in Law', 'sex' => 'M','age_min' => 18,'age_max' => null], 'mother-in-law' => ['name' => 'Mother in Law', 'sex' => 'F','age_min' => 18,'age_max' => null]];
protected $inception_excel_columns = ['sno' => ['col_idx' => 0,'col_cell_name' => 'A','col_name' => 'S.No','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => null],'emp_id' => ['col_idx' => 1,'col_cell_name' => 'B','col_name' => 'EMP ID','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => null],'name_of_emp_dep' => ['col_idx' => 2,'col_cell_name' => 'C','col_name' => 'NAME OF EMP/DEP','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => null],'dob' => ['col_idx' => 3,'col_cell_name' => 'D','col_name' => 'DOB','is_mandatory' => true,'data_type' => 'str','format' => 'd-M-Y','allowed_values' => null,'custom' => 'check_dob_diff','params' => ['row','relationship']],'sex' => ['col_idx' => 4,'col_cell_name' => 'E','col_name' => 'SEX','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => ['M','F']],'relationship' => ['col_idx' => 5,'col_cell_name' => 'F','col_name' => 'RELATIONSHIP','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => null,'custom' => 'check_relationship','params' => ['row','relationship']],'basic_cover_si' => ['col_idx' => 6,'col_cell_name' => 'G','col_name' => 'BASIC COVER SI','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => null],'doj' => ['col_idx' => 7,'col_cell_name' => 'H','col_name' => 'DOJ','is_mandatory' => 'custom','data_type' => 'str','format' => 'd-M-Y','allowed_values' => null,'custom' => 'check_doj','params' => ['row']],'basic_pay' => ['col_idx' => 8,'col_cell_name' => 'I','col_name' => 'Basic Pay','is_mandatory' => 'custom','data_type' => 'str','format' => null,'allowed_values' => null,'custom' => 'check_basic_pay','params' => ['row']],'band_grade' => ['col_idx' => 9,'col_cell_name' => 'J','col_name' => 'Band/Grade','is_mandatory' => 'custom','data_type' => 'str','format' => null,'allowed_values' => null,'custom' => 'check_employee_band','params' => ['row']],'designation' => ['col_idx' => 10,'col_cell_name' => 'K','col_name' => 'Designation','is_mandatory' => false,'data_type' => 'str','format' => null,'allowed_values' => null],'phone' => ['col_idx' => 11,'col_cell_name' => 'L','col_name' => 'Phone','is_mandatory' => false,'data_type' => 'str','format' => null,'allowed_values' => null],'email' => ['col_idx' => 12,'col_cell_name' => 'M','col_name' => 'Email','is_mandatory' => false,'data_type' => 'str','format' => null,'allowed_values' => null],'pre_existing_ailments' => ['col_idx' => 13,'col_cell_name' => 'N','col_name' => 'PRE EXISTING AILMENTS','is_mandatory' => true,'data_type' => 'str','format' => null,'allowed_values' => ['0','1']]];
public function __construct()
{
// helper('utility');
set_session_context('EmployeeService');
$this->myLogger = \Config\Services::mylogger();
$this->employeeModel = new EmployeeModel();
$this->employeePolicyModel = new EmployeePolicyModel();
$this->clientModel = new ClientModel();
$this->fileModel = new FileModel();
}
public function fileFormatValidationController($file_id)
{
helper('excel_util_helper');
//get file name
// check_dob_diff('4-APr-1990');die();
$file = $this->fileModel->find($file_id);
// dd($file);
$return = [];
if(!isset($file))
{
//file not found in DB
return array('status' => false, 'msg' => 'file not found in DB');
}
$file_name_with_path = WRITEPATH."/uploads/excel/".$file['file_name'];
//check physical file
if(!file_exists($file_name_with_path))
{
//file not found update status and reason
$this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => 'file not found'])->update();
return array('status' => false, 'msg' => 'file not found');
}
//start validation process
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path);
$sheet = $spreadsheet->getActiveSheet();
$highestRowAndColumn = $sheet->getHighestRowAndColumn();
// $highestRow = $sheet->getHighestDataRow(); // 1048576, should be 2
// $highestColumn = $sheet->getHighestDataColumn(); // L, should be B
// print_r($highestRowAndColumn);
// echo $highestRow;echo ' - ' . $highestColumn;
$excel_data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
// $data = array_filter($data);
// echo '<pre>';
// dd($excel_data);
// echo "</pre>";die();
// foreach ($excel_data as $key => $value)
// {
// }
$columns_to_check = [];
if($file['action'] == 'inception'){ $columns_to_check = $this->inception_excel_columns; }
//check no of columns in excel
$total_defined_columns = count($columns_to_check);
$excel_columns = ($excel_data[0]);
// var_dump($excel_columns);die();
$excel_columns_count = count($excel_columns);
if($total_defined_columns != $excel_columns_count)
{
//columns count mismatch
$message = "columns count mismatch. Expected - $total_defined_columns and received - $excel_columns";
echo $message;
$this->myLogger->logme('error',($message . ' for file id ' . $file_id));
//$model->where('id', $file_id)->set(['status' => 'failed','reason' => $message])->update();
return array('status' => false, 'msg' => $message);
}
//check columns order in excel
$column_count_res = check_columns_count($columns_to_check,$excel_columns);
if(isset($column_count_res) && count($column_count_res))
{
//columns count mismatch
$message = implode("\n", $column_count_res);
echo $message;
$this->myLogger->logme('error',($message . ' for file id ' . $file_id));
// $model->where('id', $file_id)->set(['status' => 'failed','reason' => $message])->update();
return array('status' => false, 'msg' => $message);
}
//start other checks
$result = [];
$keys = array_keys($this->inception_excel_columns);
// print_r($keys);die();
//remove header
unset($excel_data[0]);
$relationship = $this->general_relationships;
foreach ($excel_data as $row_key => $row)
{
//1. avoid empty rows
if(check_row_is_empty_or_null($row))
{
break;
}
//iterate each row
foreach ($row as $col_key => $col)
{
//mandatory check
if(isset($this->inception_excel_columns[$keys[$col_key]]['custom']))
{
$custom_function = $this->inception_excel_columns[$keys[$col_key]]['custom'];
// echo $custom_function;
$binding_params = $this->inception_excel_columns[$keys[$col_key]]['params'];
//convert string params into PHP variables
// Create an array of variables to pass custom helper funcitons
$param_values = [];
foreach($binding_params as $bkey => $bparam) { $param_values[] = ($$bparam); }
// dd(($param_values));//die();
$res = call_user_func_array($custom_function,$param_values);
// print_r($res['status']);die();
if($res['status'] === false) { $result[$row_key][$keys[$col_key]][] = $res['error']; }
}
if($this->inception_excel_columns[$keys[$col_key]]['is_mandatory'] === true)
{
if($col == "" || $col == NULL) { $result[$row_key][$keys[$col_key]][] = 'value is mandatory'; }
}
//format check
if(isset($this->inception_excel_columns[$keys[$col_key]]['format']))
{
$format_error = check_excel_date_format($col,$this->inception_excel_columns[$keys[$col_key]]['format']);
if(!$format_error['status'])
{
$result[$row_key][$keys[$col_key]][] = $format_error['error'];
}
}
//allowed values check
if(isset($this->inception_excel_columns[$keys[$col_key]]['allowed_values']) && is_array($this->inception_excel_columns[$keys[$col_key]]['allowed_values']))
{
$allowed_values = $this->inception_excel_columns[$keys[$col_key]]['allowed_values'];
if(!in_array($col,$allowed_values))
{
$result[$row_key][$keys[$col_key]][] = "Value not allowed: Expected ".implode(",",$allowed_values)." and received $col";
}
}
}
}
return $result;
}
}