FEAT_EMP_MODULE_CONTD
This commit is contained in:
parent
d5180f495f
commit
752c715997
188
app/Controllers/EmployeeServiceController.php
Normal file
188
app/Controllers/EmployeeServiceController.php
Normal file
@ -0,0 +1,188 @@
|
||||
<?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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
159
app/Helpers/excel_util_helper.php
Normal file
159
app/Helpers/excel_util_helper.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
if (!function_exists('check_columns_count')) {
|
||||
function check_columns_count($definedColumns,$excelColumns)
|
||||
{
|
||||
$mismatchedColumns = [];
|
||||
|
||||
foreach ($definedColumns as $colName => $definedCol) {
|
||||
$definedColIdx = $definedCol['col_idx'];
|
||||
$definedColName = $definedCol['col_name'];
|
||||
|
||||
if ($excelColumns[$definedColIdx] !== $definedColName) {
|
||||
$mismatchedColumns[] = "Column order conflict. Column order no ".($definedColIdx + 1)." expected : ".$definedColName." and received : ".$excelColumns[$definedColIdx];
|
||||
}
|
||||
}
|
||||
|
||||
return $mismatchedColumns;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('check_row_is_empty_or_null')) {
|
||||
|
||||
function check_row_is_empty_or_null($arr) {
|
||||
unset($arr[0]);// unset SNO in the array, becoz we need to check only datapoints are empty not SNo, it may added by accidentally
|
||||
foreach ($arr as $item) {
|
||||
if ($item !== null && !empty($item)) {
|
||||
return false; // If any item is not empty or not null, return false
|
||||
}
|
||||
}
|
||||
return true; // If all items are empty or null, return true
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('check_excel_date_format')) {
|
||||
|
||||
function check_excel_date_format($dateString,$format)
|
||||
{
|
||||
|
||||
if($dateString == ""){ return array('status' => true); }
|
||||
$date = DateTime::createFromFormat('d-M-Y', $dateString);
|
||||
|
||||
if ($date !== false && !is_array($date::getLastErrors())) {
|
||||
return array('status' => true);
|
||||
} else {
|
||||
return array('status' => false,'error' => "wrong date format: Expected 'd-M-Y' and received $dateString");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!function_exists('check_relationship'))
|
||||
{
|
||||
function check_relationship($row,$relationship)
|
||||
{
|
||||
// print_r($col);print_r($relationship);
|
||||
// echo '<br>';
|
||||
if($row[5] != null && $row[4] != null)
|
||||
{
|
||||
$slug = \Config\Services::slug();
|
||||
$col = $slug->slugify($row[5]);
|
||||
// echo $col;die();
|
||||
if($col != 'self' && $col != 'spouse')
|
||||
{
|
||||
if($relationship[ $col ]['sex'] != $row[4])
|
||||
{
|
||||
$error = "Gender relationship conflict: Expected ".$relationship[ $col ]['sex'].", received $row[4]";
|
||||
return array('status' => false,'error' => $error);
|
||||
}
|
||||
|
||||
}
|
||||
return array('status' => true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return array('status' => false,'error' => 'Rule Conflict: Both Relationship or Gender required');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!function_exists('check_doj'))
|
||||
{
|
||||
function check_doj($row)
|
||||
{
|
||||
$slug = \Config\Services::slug();
|
||||
$relationship = $slug->slugify($row[5]);
|
||||
// echo $relationship;echo $row[7];
|
||||
if($relationship == 'self' && $row[7] == "") { return array('status' => false,'error' => "DOJ mandantory for self"); }
|
||||
return array('status' => true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!function_exists('check_employee_band'))
|
||||
{
|
||||
function check_employee_band($row)
|
||||
{
|
||||
$slug = \Config\Services::slug();
|
||||
$relationship = $slug->slugify($row[5]);
|
||||
// echo $relationship;echo $row[7];
|
||||
if($relationship == 'self' && $row[9] == "") { return array('status' => false,'error' => "Band mandantory for self"); }
|
||||
return array('status' => true);
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('check_basic_pay'))
|
||||
{
|
||||
function check_basic_pay($row)
|
||||
{
|
||||
$slug = \Config\Services::slug();
|
||||
$relationship = $slug->slugify($row[5]);
|
||||
// echo $relationship;echo $row[7];
|
||||
if($relationship == 'self' && $row[8] == "") { return array('status' => false,'error' => "Basic pay mandantory for self"); }
|
||||
return array('status' => true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('check_dob_diff'))
|
||||
{
|
||||
function check_dob_diff($row,$relationships) {
|
||||
// echo 'called';
|
||||
if($row[3] != null && $row[5] != null)
|
||||
{
|
||||
$dob = change_date_format($row[3],'d-M-Y','Y-m-d');
|
||||
// echo $row[3].' - '.$dob;echo '<br>';
|
||||
$currentDateTime = new DateTime();//die();
|
||||
$passedDateTime = new DateTime($dob);
|
||||
$interval = $currentDateTime->diff($passedDateTime);
|
||||
|
||||
$slug = \Config\Services::slug();
|
||||
$relationship = $slug->slugify($row[5]);
|
||||
$age_min = $relationships[$relationship]['age_min'];
|
||||
$age_max = $relationships[$relationship]['age_max'];
|
||||
// echo $relationship.','.$age_min.'-'.$age_max;
|
||||
if($age_min !== null && $age_min > $interval->y)
|
||||
{
|
||||
return array('status' => false,'error' => "Age conflict : minimum $age_min yrs allowed, received $interval->y");
|
||||
}
|
||||
if($age_max !== null && $age_max < $interval->y)
|
||||
{
|
||||
return array('status' => false,'error' => "Age conflict : maximum $age_max yrs allowed, received $interval->y");
|
||||
}
|
||||
return array('status' => true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return array('status' => false,'error' => 'Rule Conflict: Both DOB or Relationship required');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user