['name' => 'Self', 'gender' => 'M','age_min' => 18,'age_max' => null], 'spouse' => ['name' => 'Spouse', 'gender' => 'F','age_min' => 18,'age_max' => null], 'son' => ['name' => 'Son', 'gender' => 'M','age_min' => null,'age_max' => 25], 'daughter' => ['name' => 'Daughter', 'gender' => 'F','age_min' => null,'age_max' => 25], 'father' => ['name' => 'Father', 'gender' => 'M','age_min' => 18,'age_max' => null], 'mother' => ['name' => 'Mother', 'gender' => 'F','age_min' => 18,'age_max' => null], 'father-in-law' => ['name' => 'Father in Law', 'gender' => 'M','age_min' => 18,'age_max' => null], 'mother-in-law' => ['name' => 'Mother in Law', 'gender' => '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'=>['I','A','DA'],'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null,'custom'=>'check_dob_diff','params'=>['row','relationship']],'gender'=>['col_idx'=>4,'col_cell_name'=>'E','col_name'=>'Gender','is_mandatory'=>['I','A','DA'],'data_type'=>'str','format'=>null,'allowed_values'=>['M','F']],'relationship'=>['col_idx'=>5,'col_cell_name'=>'F','col_name'=>'RELATIONSHIP','is_mandatory'=>['I','A','DA'],'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'=>['I','A','DA'],'data_type'=>'str','format'=>null,'allowed_values'=>null,'custom' => 'check_si','params' => ['row','policy_terms','slab_details']],'doc'=>['col_idx'=>7,'col_cell_name'=>'H','col_name'=>'Date of Coverage','is_mandatory'=>['A','DA'],'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null,'params'=>['row']],'doj'=>['col_idx'=>8,'col_cell_name'=>'I','col_name'=>'DOJ','is_mandatory'=>false,'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null,'custom'=>'check_doj','params'=>['row']],'basic_pay'=>['col_idx'=>9,'col_cell_name'=>'J','col_name'=>'Basic Pay','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null,'custom'=>'check_basic_pay','params'=>['row','policy_terms','slab_details']],'band_grade'=>['col_idx'=>10,'col_cell_name'=>'K','col_name'=>'Band/Grade','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null,'custom'=>'check_employee_band','params'=>['row','policy_terms','slab_details']],'designation'=>['col_idx'=>11,'col_cell_name'=>'L','col_name'=>'Designation','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null],'phone'=>['col_idx'=>12,'col_cell_name'=>'M','col_name'=>'Phone','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null],'email'=>['col_idx'=>13,'col_cell_name'=>'N','col_name'=>'Email','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null],'pre_existing_ailments'=>['col_idx'=>14,'col_cell_name'=>'O','col_name'=>'PRE EXISTING AILMENTS','is_mandatory'=>['I','A','DA'],'data_type'=>'str','format'=>null,'allowed_values'=>['0','1']],'change_event'=>['col_idx'=>15,'col_cell_name'=>'P','col_name'=>'Change event','is_mandatory'=>['A','DA','D'],'data_type'=>'str','format'=>null,'allowed_values'=>null],'date_of_exit'=>['col_idx'=>16,'col_cell_name'=>'Q','col_name'=>'Date of exit','is_mandatory'=>['D'],'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null],'reason_for_exit'=>['col_idx'=>17,'col_cell_name'=>'R','col_name'=>'Reason for exit','is_mandatory'=>['D'],'data_type'=>'str','format'=>null,'allowed_values'=>null]]; protected $deletion_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],'change_event'=>['col_idx'=>3,'col_cell_name'=>'D','col_name'=>'Change event','is_mandatory'=>['D'],'data_type'=>'str','format'=>null,'allowed_values'=>null],'date_of_exit'=>['col_idx'=>4,'col_cell_name'=>'E','col_name'=>'Date of exit','is_mandatory'=>['D'],'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null],'reason_for_exit'=>['col_idx'=>5,'col_cell_name'=>'F','col_name'=>'Reason for exit','is_mandatory'=>['D'],'data_type'=>'str','format'=>null,'allowed_values'=>null]]; protected $correction_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],'field'=>['col_idx'=>3,'col_cell_name'=>'D','col_name'=>'Field','is_mandatory'=>true,'data_type'=>'str','format'=>null,'allowed_values'=>['name','dob','relationship']],'value'=>['col_idx'=>4,'col_cell_name'=>'E','col_name'=>'Value','is_mandatory'=>true,'data_type'=>'str','format'=>null,'allowed_values'=>null],'date_of_correction'=>['col_idx'=>5,'col_cell_name'=>'F','col_name'=>'Date of Correction','is_mandatory'=>true,'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null],'change_event'=>['col_idx'=>6,'col_cell_name'=>'G','col_name'=>'Change event','is_mandatory'=>true,'data_type'=>'str','format'=>null,'allowed_values'=>null],'remarks'=>['col_idx'=>7,'col_cell_name'=>'H','col_name'=>'Remarks','is_mandatory'=>false,'data_type'=>'str','format'=>null,'allowed_values'=>null]]; protected $si_enhance_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],'augmented_si'=>['col_idx'=>3,'col_cell_name'=>'D','col_name'=>'Augmented SI','is_mandatory'=>true,'data_type'=>'str','format'=>null,'allowed_values'=>null,'custom' => 'check_si','params' => ['row','policy_terms','slab_details']],'date_of_enhancement'=>['col_idx'=>4,'col_cell_name'=>'E','col_name'=>'Date of SI Enhancement','is_mandatory'=>true,'data_type'=>'str','format'=>'d-M-Y','allowed_values'=>null]]; 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(); $this->clientPolicyModel = new ClientPolicyModel(); $this->policiesModel = new PolicesModel(); $this->empEndorsementModel = new EmpEndorsementModel(); } public function excelFileFormatValidation($params) { helper('excel_util_helper'); //get file name // check_dob_diff('4-APr-1990');die(); $file_id = $params['file_id']; $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 $message = "Physcial file not found"; // echo $message; $this->myLogger->logme('error',($message . ' for file id ' . $file_id)); $this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => json_encode(['error_summary' => array_count_values([5]),'error_data' => $message])])->update(); return array('error_summary' => [5], 'error_data' => $message); } //start validation process $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); // dd($highestRowAndColumn); $columns_to_check = []; if($file['action'] == 'inception'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'addition'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'dependent_addition'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'deletion'){ $columns_to_check = $this->deletion_excel_columns; } if($file['action'] == 'correction'){ $columns_to_check = $this->correction_excel_columns; } if($file['action'] == 'si_enhancement'){ $columns_to_check = $this->si_enhance_excel_columns; } $result = ['error_type' => 1,'error_summary' => [], 'error_data' => [] ]; $keys = array_keys($columns_to_check); $allowedHighestColumn = end($columns_to_check); // dd($allowedHighestColumn); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); // !dd(array_chunk($excel_data,20)[0]); //check no of columns in excel $total_defined_columns = count($columns_to_check); $excel_columns = ($excel_data[0]); // var_dump($total_defined_columns);die(); $excel_columns_count = count($excel_columns); if($total_defined_columns != $excel_columns_count) { //columns count mismatch $message = "File columns count mismatch. Expected - $total_defined_columns and received - $excel_columns_count"; // echo $message; $this->myLogger->logme('error',($message . ' for file id ' . $file_id)); $this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => json_encode(['error_summary' => array_count_values([5]),'error_data' => $message])])->update(); return array('error_summary' => [5], 'error_data' => $message); } //check columns order in excel $column_count_res = check_columns_name($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)); $this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => json_encode(['error_summary' => array_count_values([6]), 'error_data' => $message])])->update(); return array('error_summary' => [6], 'error_data' => $message); } // dd($columns_to_check);die(); // get policy and rack details $policy_terms = $this->clientPolicyModel->getPolicyDetails($file['client_id'],$file['policy_id']); $policy_terms = (array) $policy_terms[0];// convert obj to array //get policy slab rates $slab_details = $this->policiesModel->getPolicySlabRatesForEmpOnboard($file['policy_id'],$file['client_id']); // dd($slab_details); //remove header unset($excel_data[0]); $relationship = $this->general_relationships; foreach ($excel_data as $row_key => $row) { //define row wise action/event in temporary variable $current_column_action = null; if($file['action'] == 'inception'){ $current_column_action = 'I'; } else if($file['action'] == 'addition'){ $current_column_action = 'A'; } else if($file['action'] == 'dependent_addition'){ $current_column_action = 'DA'; } else if($file['action'] == 'deletion'){ $current_column_action = 'D'; } else if($file['action'] == 'correction'){ $current_column_action = 'C'; } else if($file['action'] == 'si_enhancement'){ $current_column_action = 'SI'; } //1. avoid empty rows if(check_row_is_empty_or_null($row)) { break; } //iterate each row foreach ($row as $col_key => $col) { $is_mandatory = $columns_to_check[$keys[$col_key]]['is_mandatory']; $format = $columns_to_check[$keys[$col_key]]['format']; $allowed_values = $columns_to_check[$keys[$col_key]]['allowed_values']; $custom_function = isset($columns_to_check[$keys[$col_key]]['custom']) ? $columns_to_check[$keys[$col_key]]['custom'] : null; $binding_params = isset($columns_to_check[$keys[$col_key]]['params']) ? $columns_to_check[$keys[$col_key]]['params'] : null; $column_dispaly_name = $columns_to_check[$keys[$col_key]]['col_name']; $column_index = $columns_to_check[$keys[$col_key]]['col_idx']; $column_cell = $columns_to_check[$keys[$col_key]]['col_cell_name']; $row['current_action'] = $current_column_action; //mandatory check if(is_bool($is_mandatory) && $is_mandatory === true) { if($col == "" || $col == NULL) { array_push($result['error_summary'],1); //push error code for summary $result['error_data'][$row_key][$keys[$col_key]]['col_name'] = $column_dispaly_name; //push column name $result['error_data'][$row_key][$keys[$col_key]]['col_idx'] = $column_index; //push column index $result['error_data'][$row_key][$keys[$col_key]]['error'][] = 'Value is mandatory'; //push exact error desc } } //if is_mandatory is array (action based mandatory check) if($current_column_action != null && is_array($is_mandatory) && in_array(strtoupper(trim($current_column_action)),$is_mandatory)) { $allowed_actions = $columns_to_check[$keys[$col_key]]['is_mandatory']; if($col == "" || $col == NULL) { array_push($result['error_summary'],1); $result['error_data'][$row_key][$keys[$col_key]]['col_name'] = $column_dispaly_name; //push column name $result['error_data'][$row_key][$keys[$col_key]]['col_idx'] = $column_index; //push column index $result['error_data'][$row_key][$keys[$col_key]]['error'][] = 'Value is mandatory for this action/event'; } } //format check if(isset($format)) { $format_error = check_excel_date_format($col,$format); if(!$format_error['status']) { array_push($result['error_summary'],2); $result['error_data'][$row_key][$keys[$col_key]]['col_name'] = $column_dispaly_name; //push column name $result['error_data'][$row_key][$keys[$col_key]]['col_idx'] = $column_index; //push column index $result['error_data'][$row_key][$keys[$col_key]]['error'][] = $format_error['error']; } } //allowed values check if($is_mandatory === true && isset($allowed_values) && is_array($allowed_values)) { if(!in_array(strtolower(trim($col)),$allowed_values)) { array_push($result['error_summary'],3); $result['error_data'][$row_key][$keys[$col_key]]['col_name'] = $column_dispaly_name; //push column name $result['error_data'][$row_key][$keys[$col_key]]['col_idx'] = $column_index; //push column index $result['error_data'][$row_key][$keys[$col_key]]['error'][] = "Value not allowed: Expected ".implode(",",$allowed_values)." and received $col"; } } //custom function check if(isset($custom_function)) { //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) { array_push($result['error_summary'],4); $result['error_data'][$row_key][$keys[$col_key]]['col_name'] = $column_dispaly_name; //push column name $result['error_data'][$row_key][$keys[$col_key]]['col_idx'] = $column_index; //push column index $result['error_data'][$row_key][$keys[$col_key]]['error'][] = $res['error']; } } } } if(isset($result['error_summary']) && count($result['error_summary'])) { $result['error_summary'] = array_count_values($result['error_summary']); $status = 'failed'; $failure_reason = ((json_encode($result))); $this->fileModel->where('id', $file_id)->set(['status' => $status,'reason' => $failure_reason])->update(); $this->myLogger->logme("error",'{file_id} uploaded failed',['file_id' => $file_id]); //return $this->respond(['dataStatus' => true,'code' => 404,'data' => 'file upload failed with errors'], 200); // dd($failure_reason); //print_r($r); } else //trigger next data validation via job queue server { //proceed next data level validation in JOB queue $job_details = new Jobs(); $r = Jobs::addJob(['job_name' => 'excelFileDataValidation','payload' => ['file_id' => $file_id]]); // $jobWorker = new JobWorker(); // JobWorker::processJob($r); } return $result; } public function excelFileDataValidation($params) { helper('excel_util_helper'); //get file name $file_id = $params['file_id']; $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 $message = "Physical file not found"; // echo $message; $this->myLogger->logme('error',($message . ' for file id ' . $file_id)); $this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => json_encode(['error_summary' => array_count_values([5]),'error_data' => $message])])->update(); return array('error_summary' => [5], 'error_data' => $message); } $columns_to_check = []; if($file['action'] == 'inception'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'addition'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'dependent_addition'){ $columns_to_check = $this->inception_excel_columns; } if($file['action'] == 'deletion'){ $columns_to_check = $this->deletion_excel_columns; } if($file['action'] == 'correction'){ $columns_to_check = $this->correction_excel_columns; } if($file['action'] == 'si_enhancement'){ $columns_to_check = $this->si_enhance_excel_columns; } // get policy and rack details $policy_terms = $this->clientPolicyModel->getPolicyDetails($file['client_id'],$file['policy_id']); // dd($policy_terms); $policy_terms = json_decode($policy_terms[0]->policy_terms); $policy_terms = (array) $policy_terms;// convert obj to array // dd($policy_terms); //get excel data $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $result = ['error_type' => 2,'error_summary' => [], 'error_data' => [] ]; $keys = array_keys($columns_to_check); $allowedHighestColumn = end($columns_to_check); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); // print_r($keys);die(); //remove header unset($excel_data[0]); $relationship = $this->general_relationships; $employee_data_group_by_family = data_group_by_family($excel_data); // dd($employee_data_group_by_family); foreach ($employee_data_group_by_family as $emp_id => $family) { //if action is DA then get all familiy members,transfrom them into excel array, addd data source as db or excel if($file['action'] == 'dependent_addition') { $existing_famility_details = $this->employeeModel->getEmpFamilybyEmpCode(emp_code: $emp_id,client_id: $file['client_id'],client_policy_id: $file ['policy_id'],emp_status: 'active'); // dd($existing_famility_details); //transsform familiy details from db columns to exxcel row with column indexs for checking remaining functionalitites $existing_famility_details = transform_db_data_to_excel($existing_famility_details,$file); // dd($existing_famility_details); $family = array_merge($family,$existing_famility_details); } // kint::dump($family);die(); //check name dup within a family if($file['action'] == 'inception' || $file['action'] == 'addition' || $file['action'] == 'dependent_addition') { $res = name_dup_check_within_family($family,$file['action']); // dd($res); if(count($res)) { foreach($res as $k => $rowid) { array_push($result['error_summary'],7); // dup entry in excel file $result['error_data'][$rowid]['name_of_emp_dep']['error'][] = "Twofold Name found within family"; } } } if($file['action'] == 'dependent_addition' || $file['action'] == 'addition' || $file['action'] == 'inception') { $res = check_dependent_conflict($family,$policy_terms,$file['action']); // dd($res); if(!$res['status']) { foreach($res['error_data'] as $key => $value) { array_push($result['error_summary'],$value['code']); $result['error_data'][ $value['row_id'] ][($value['col_name'])]['error'][] = $value['msg']; } } } //check dup with empid and name with db $res = name_and_empid_check_in_db($family,$file); // dd($file); // echo '
'; // print_r($res); if(count($res['del'])) { foreach($res['del'] as $k => $rowid) { array_push($result['error_summary'],10); //record not avail for deletion $result['error_data'][$rowid]['name_of_emp_dep']['error'][] = "Record Not found "; } } if(count($res['i'])) { foreach($res['i'] as $k => $rowid) { array_push($result['error_summary'],9); //dup entry $result['error_data'][$rowid]['name_of_emp_dep']['error'][] = "Record already exists"; } } } // s($result); // die(); if(isset($result['error_summary']) && count($result['error_summary'])) { $result['error_summary'] = array_count_values($result['error_summary']); $status = 'failed'; $failure_reason = ((json_encode($result))); $this->fileModel->where('id', $file_id)->set(['status' => $status,'reason' => $failure_reason])->update(); $this->myLogger->logme("error",'{file_id} uploaded failed',['file_id' => $file_id]); //return $this->respond(['dataStatus' => true,'code' => 404,'data' => 'file upload failed with errors'], 200); // dd($failure_reason); } else if($file['action'] == 'deletion') { //proceed next data level validation in JOB queue $job_details = new Jobs(); $r = Jobs::addJob(['job_name' => 'employeeDisembark','payload' => ['file_id' => $file_id]]); } else if ($file['action'] == 'si_enhancement') { //proceed next data level validation in JOB queue $job_details = new Jobs(); $r = Jobs::addJob(['job_name' => 'employeesSIEnhanceProcess','payload' => ['file_id' => $file_id]]); } else if ($file['action'] == 'correction') { //proceed next data level validation in JOB queue $job_details = new Jobs(); $r = Jobs::addJob(['job_name' => 'employeesCorrectionProcess','payload' => ['file_id' => $file_id]]); } else //inception OR addition OR dependent addition { //proceed next data level validation in JOB queue $job_details = new Jobs(); $r = Jobs::addJob(['job_name' => 'employeesOnboardPreprocess','payload' => ['file_id' => $file_id]]); $jobWorker = new JobWorker(); JobWorker::processJob($r); } return $result; } // calculate data points and premium public function employeesOnboardPreprocess($params) { helper('excel_util_helper'); // dd($params); if(isset($params['file_id']))//handle data from excel to inception { //get file name $file_id = $params['file_id']; $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 $message = "Physical file not found"; // echo $message; $this->myLogger->logme('error',($message . ' for file id ' . $file_id)); $this->fileModel->where('id', $file_id)->set(['status' => 'failed','reason' => json_encode(['error_summary' => array_count_values([5]),'error_data' => $message])])->update(); return array('error_summary' => [5], 'error_data' => $message); } $columns_to_check = []; if($file['action'] == 'inception'){ $columns_to_check = $this->inception_excel_columns; } // get policy and rack details $policy_terms = $this->clientPolicyModel->getPolicyDetails($file['client_id'],$file['policy_id']); $policy_terms = (array) $policy_terms[0];// convert obj to array // $policy_terms = json_decode($policy_terms[0]->policy_terms); // dd($policy_terms); //get excel data $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $allowedHighestColumn = end($columns_to_check); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); unset($excel_data[0]); //get policy slab rates $slab_details = $this->policiesModel->getPolicySlabRatesForEmpOnboard($file['policy_id'],$file['client_id']); // dd($slab_details); $employee_data_group_by_family = data_group_by_family($excel_data); // dd($employee_data_group_by_family); foreach ($employee_data_group_by_family as $emp_id => $family) { //if action is DA then get all familiy members,transfrom them into excel array, addd data source as db or excel if($file['action'] == 'dependent_addition') { $existing_famility_details = $this->employeeModel->getEmpFamilybyEmpCode(emp_code: $emp_id,client_id: $file['client_id'],client_policy_id: $file ['policy_id'],emp_status: 'active'); // dd($existing_famility_details); //transsform familiy details from db columns to exxcel row with column indexs for checking remaining functionalitites $existing_famility_details = transform_db_data_to_excel($existing_famility_details,$file); // Kint::dump($existing_famility_details); $family = array_merge($family,$existing_famility_details); // dd($family); } // Kint::dump($family);die(); $data = calculate_premimum($family,$policy_terms,$slab_details,$file); // dd($data); $employee_data_group_by_family[$emp_id] = $data; //$this->employeesOnboardProcess(['familiy_data' => $data,'file' => $file]); } } else if(isset($params['client_policy_id']))//handle data from enrollment to inception { $client_policy_id = $params['client_policy_id']; //get client id $client_id = ($this->clientPolicyModel->select('client_id')->find($client_policy_id))['client_id']; // dd($client_id); // get policy and rack details $policy_terms = $this->clientPolicyModel->getPolicyDetails($client_id,$client_policy_id); $policy_terms = (array) $policy_terms[0];// convert obj to array //get policy slab rates $slab_details = $this->policiesModel->getPolicySlabRatesForEmpOnboard($client_policy_id,$client_id); $existing_famility_details = $this->employeeModel->getEmpFamilybyEmpCode(client_id: $client_id,client_policy_id: $client_policy_id,emp_status: 'draft'); $file = ['id' => null,'client_id' => $client_id,'policy_id' => $client_policy_id,'action' => 'inception']; // dd($this->employeeModel->getLastQuery()); // Kint::dump($existing_famility_details);die(); $employee_data_group_by_family = data_group_by_family($existing_famility_details,$data_source = 'db'); // dd($employee_data_group_by_family); foreach ($employee_data_group_by_family as $emp_id => $family) { $transformed_famility_details = transform_db_data_to_excel($family); $data = calculate_premimum($transformed_famility_details,$policy_terms,$slab_details,$file); // dd($data); $employee_data_group_by_family[$emp_id] = $data; $this->employeesOnboardProcess(['familiy_data' => $data,'file' => $file]); } // dd($employee_data_group_by_family); } // die(); return ($employee_data_group_by_family); } //deletion of emp public function employeeDisembark($params) { helper('excel_util_helper'); //get file name $file_id = $params['file_id']; $file = $this->fileModel->find($file_id); $file_name_with_path = WRITEPATH."/uploads/excel/".$file['file_name']; $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $allowedHighestColumn = end($this->deletion_excel_columns); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); // $excel_data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']); unset($excel_data[0]); // dd($excel_data); $endorsement_data = []; //make closure funciton which is going to use only by this method $endorsement = function($data,$file,$row){ $group_key = rand(100000, 999999); //for emp table $this->empEndorsementModel->save(['pk' => (int)$data['emp_id'],'emp_code' => $data['emp_code'],'table_name' => 'employees','actions' => 'd','name' => $data['name'],'field_name' => 'emp_status','old_value' => $data['emp_status'],'new_value' => 'deactivate','created_by' => $file['created_by'],'remarks' => 'general deletion','group_key' => $group_key]); // dd( $this->empEndorsementModel->getLastQuery()); // $this->empEndorsementModel->save(['pk' => (int)$data['emp_id'],'emp_code' => $data['emp_code'],'table_name' => 'employees','actions' => 'd','name' => $data['name'],'field_name' => 'change_event','old_value' => $data['change_event'],'new_value' => 'deletion','created_by' => $file['created_by'],'remarks' => 'general deletion']); // for employee policy table $this->empEndorsementModel->save(['pk' => (int)$data['emp_policy_id'],'emp_code' => $data['emp_code'],'table_name' => 'employee_polices','actions' => 'd','name' => $data['name'],'field_name' => 'date_of_exit','old_value' => $data['date_of_exit'],'new_value' => change_date_format($row[4],'d-M-Y','Y-m-d'),'created_by' => $file['created_by'],'remarks' => 'general deletion','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$data['emp_policy_id'],'emp_code' => $data['emp_code'],'table_name' => 'employee_polices','actions' => 'd','name' => $data['name'],'field_name' => 'reason_for_exit','old_value' => $data['reason_for_exit'],'new_value' => $row[5],'created_by' => $file['created_by'],'remarks' => 'general deletion','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$data['emp_policy_id'],'emp_code' => $data['emp_code'],'table_name' => 'employee_polices','actions' => 'd','name' => $data['name'],'field_name' => 'status','old_value' => $data['status'],'new_value' => 'deactivate','created_by' => $file['created_by'],'remarks' => 'general deletion','group_key' => $group_key]); }; //iterate each row foreach ($excel_data as $col_key => $row) { if(check_row_is_empty_or_null($row)) { break; } // echo '
START- ' . $row[2]; $employee = $this->employeeModel->where('emp_code', $row[1])->where('name',$row[2])->where('client_id',$file['client_id'])->first(); $existing_endorsements = $this->empEndorsementModel->where('actions','d') ->where('table_name','employees') ->where('endorsement_id is null') ->where('emp_code',$employee['emp_code']) ->where('name',$employee['name']) ->where('field_name','emp_status') ->findAll(); if(!count($existing_endorsements)) { if(strtolower($employee['relationship']) != 'self') { if(!in_array($employee['id'],$endorsement_data))//make entry in endorsement firsttime only with checking that PK of emp exisintg in variable $endorsement_data { $employee_policy = $this->employeePolicyModel->where('employee_id',$employee['id'])->where('client_policy_id',$file['policy_id'])->first(); $data = ['emp_id' => $employee['id'],'name' => $employee['name'],'emp_status' => $employee['emp_status'],'emp_policy_id' => $employee_policy['id'],'emp_code' => $employee['emp_code'],'change_event' => $employee['change_event'],'date_of_exit' => $employee_policy['date_of_exit'],'reason_for_exit' => $employee_policy['reason_for_exit'],'status' => $employee_policy['status']]; $endorsement($data,$file,$row); $endorsement_data[] = $employee['id']; } } else { $family = $this->employeeModel->getEmpFamilybyEmpCode(emp_code: $row[1],client_id: $file['client_id'],client_policy_id: $file ['policy_id'],emp_status: 'active'); // Kint::dump($family); foreach ($family as $key => $emp) { if(!in_array($emp['emp_id'],$endorsement_data)) { $endorsement($emp,$file,$row); $endorsement_data[] = $emp['emp_id']; } } } } // print_r($endorsement_data); } return $endorsement_data; } //correction of emp public function employeesCorrectionProcess($params) { helper('excel_util_helper'); //get file name $file_id = $params['file_id']; $file = $this->fileModel->find($file_id); $file_name_with_path = WRITEPATH."/uploads/excel/".$file['file_name']; $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $allowedHighestColumn = end($this->correction_excel_columns); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); // $excel_data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']); unset($excel_data[0]); // dd($excel_data); //iterate each row foreach ($excel_data as $col_key => $row) { if(check_row_is_empty_or_null($row)) { break; } // echo '
START- ' . $row[2]; $field_name = $row[3]; $field_value = ($field_name == 'dob' ? change_date_format($row[4],'d-M-Y','Y-m-d') : $row[4] ); $employee = $this->employeeModel->where('emp_code', $row[1])->where('name',$row[2])->where('client_id',$file['client_id'])->first(); $existing_endorsements = $this->empEndorsementModel->where('actions','c') ->where('table_name','employees') ->where('endorsement_id is null') ->where('emp_code',$employee['emp_code']) ->where('name',$employee['name']) ->where('field_name',$field_name) ->findAll(); // dd($existing_endorsements); //make entry in endorsement table if(!count($existing_endorsements)) { $group_key = rand(100000, 999999); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employees','actions' => 'c','name' => $employee['name'],'field_name' => $field_name,'old_value' => $employee[ $field_name ],'new_value' => $field_value,'created_by' => $file['created_by'],'remarks' => $row[7],'date_of_correction' => change_date_format($row[5],'d-M-Y','Y-m-d'),'group_key' => $group_key]); } } return true; } //enhance of emp public function employeesSIEnhanceProcess($params) { helper('excel_util_helper'); //get file name $file_id = $params['file_id']; $file = $this->fileModel->find($file_id); $file_name_with_path = WRITEPATH."/uploads/excel/".$file['file_name']; $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $allowedHighestColumn = end($this->si_enhance_excel_columns); $excel_data = $sheet->rangeToArray('A1:' . $allowedHighestColumn['col_cell_name'] . $highestRowAndColumn['row']); // $excel_data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']); unset($excel_data[0]); // dd($excel_data); //get policy slab rates $slab_details = $this->policiesModel->getPolicySlabRatesForEmpOnboard($file['policy_id'],$file['client_id']); //iterate each row // dd($slab_details); foreach ($excel_data as $col_key => $row) { if(check_row_is_empty_or_null($row)) { break; } // echo '
START- ' . $row[2]; $employee = $this->employeeModel->where('emp_code', $row[1])->where('name',$row[2])->where('client_id',$file['client_id'])->first(); $employee_policy = $this->employeePolicyModel->where('employee_id',$employee['id'])->where('client_policy_id',$file['policy_id'])->first(); $existing_endorsements = $this->empEndorsementModel->where('actions','si') ->where('table_name','employee_polices') ->where('endorsement_id is null') ->where('emp_code',$employee['emp_code']) ->where('name',$employee['name']) ->where('field_name','basic_cover_si') ->findAll(); // dd($existing_endorsements); //make entry in endorsement table if(!count($existing_endorsements)) { foreach ($slab_details['slab_rates'] as $skey => $slab_value) { if($slab_value['si'] == $row[3]) { $group_key = rand(100000, 999999); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'basic_cover_si','old_value' => $employee_policy['basic_cover_si'],'new_value' => $row[3],'created_by' => $file['created_by'],'remarks' => 'general si enhancement','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'premium','old_value' => $employee_policy['premium'],'new_value' => $slab_value['premium'],'created_by' => $file['created_by'],'remarks' => 'general si enhancement','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'si_enhancement_date','old_value' => null,'new_value' => change_date_format($row[4],'d-M-Y','Y-m-d'),'created_by' => $file['created_by'],'remarks' => 'general si enhancement','group_key' => $group_key]); break; } } } } return true; } // insert or update in db for inception addition depent addition public function employeesOnboardProcess($params) { $familiy_data = $params['familiy_data']; $file = $params['file']; // dd($file); // print_r($familiy_data); // echo '------------------------------------------------------'; foreach($familiy_data as $fkey => $value) { if($file['id'] == null || ($value['temp']['source'] == 'excel' || (in_array($value['temp']['grid_type'],[10,11]) && strtolower($value['relationship']) == 'self'))) //insert data come from excel and from db { $employee = $this->employeeModel->checkExistingEmp($value); $policy_data = $value['policy_details']; $temp = $value['temp']; unset($value['policy_details']); unset($value['temp']); if($file['id'] == null && (strtolower($value['relationship']) == 'self' && $temp['source'] == 'db' && $temp['emp_id'] != null && $temp['emp_policy_id'] != null)) { //check existing si and new si $employee_policy = $this->employeePolicyModel->where('employee_id',$employee['id'])->where('client_policy_id',$file['policy_id'])->first(); if($employee_policy['basic_cover_si'] != $policy_data['basic_cover_si']) { //check any existing si enhancement pending $existing_endorsements = $this->empEndorsementModel->where('actions','si') ->where('table_name','employee_polices') ->where('endorsement_id is null') ->where('emp_code',$employee['emp_code']) ->where('name',$employee['name']) ->where('field_name','basic_cover_si') ->findAll(); if(!count($existing_endorsements)) { $slab_details = $this->policiesModel->getPolicySlabRatesForEmpOnboard($file['policy_id'],$file['client_id']); foreach ($slab_details['slab_rates'] as $skey => $slab_value) { if($slab_value['si'] == $policy_data['si']) { $group_key = rand(100000, 999999); //add new premimum details in endorsement table $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'basic_cover_si','old_value' => $employee_policy['basic_cover_si'],'new_value' => $policy_data['premium'],'created_by' => $file['created_by'],'remarks' => 'da auto si enhancement','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'premium','old_value' => $employee_policy['premium'],'new_value' => $slab_value['premium'],'created_by' => $file['created_by'],'remarks' => 'da auto si enhancement','group_key' => $group_key]); $this->empEndorsementModel->save(['pk' => (int)$employee['id'],'emp_code' => $employee['emp_code'],'table_name' => 'employee_polices','actions' => 'si','name' => $employee['name'],'field_name' => 'si_enhancement_date','old_value' => null,'new_value' => ($policy_data['date_coverage']),'created_by' => $file['created_by'],'remarks' => 'da auto si enhancement','group_key' => $group_key]); break; } } } } break; }// end of self employee si enhance ment //save employee table if(count($employee)) { $value['updated_by'] = $file['created_by']; $value['id'] = $employee[0]['id']; $log_message = 'Update Employee - '.$employee[0]['name'].'('.$employee[0]['emp_code'].') with PK '.$employee[0]['id']; // $this->myLogger->logme('error',('Update - ' . $employee[0]['id'].' - '. $employee[0]['emp_code'] .' - '.$employee[0]['name'])); } else { $value['emp_status'] = 'active'; $value['created_by'] = $file['created_by']; $log_message = 'Insert Employee- '.$value['name'] .'('.$value['emp_code'] .') with PK '; // $this->myLogger->logme('error',('Insert - ' . $value['emp_code'] .' - '. $value['name'])); } $this->employeeModel->save($value); $emp_id = $this->employeeModel->getInsertID(); if($emp_id != 0){ $log_message .= $emp_id; } else{ $emp_id = $employee[0]['id']; } $this->myLogger->logme('error',$log_message); //save policy table $employee_policy = $this->employeePolicyModel->checkExistingEmpPolicy(['employee_id' => $emp_id,'client_policy_id' => $file['policy_id']]); if(count($employee_policy)) { $policy_data['updated_by'] = $file['created_by']; $policy_data['id'] = $employee_policy[0]['id']; $policy_data['status'] = 'active'; $log_message = 'Update Employee Policy for '. $value['name'].'('. $value['emp_code'].') with PK- ' . $employee_policy[0]['id'] .' client policy ID '.$employee_policy[0]['client_policy_id'].' with SI '.$policy_data['basic_cover_si']; } else { $policy_data['employee_id'] = $emp_id; $policy_data['client_policy_id'] = $file['policy_id']; $policy_data['created_by'] = $file['created_by']; $policy_data['status'] = 'active'; $log_message = 'Insert Employee Policy for '. $value['name'].'('. $value['emp_code'].') - client policy id -'. $file['policy_id'].' - with SI '.$policy_data['basic_cover_si']; } $this->employeePolicyModel->save($policy_data); $emp_policy_id = $this->employeePolicyModel->getInsertID(); if($emp_policy_id != 0){ $log_message .= ' with PK ' . $emp_policy_id; } else{ $emp_policy_id = $employee_policy[0]['id']; } $this->myLogger->logme('error',$log_message); }// if end }// for end }//function end // --------------------------------------------------------------------------------- public function getExcelErrorData($file_id){ $file = $this->fileModel->find($file_id); $error_data = json_decode($file['reason']); // return $error_data; $file_name_with_path = WRITEPATH."/uploads/excel/".$file['file_name']; //check the file exist or not if(!file_exists($file_name_with_path)) { $error_message = "File not found"; $this->myLogger->logme('error',($error_message . ' for file id ' . $file_id)); } $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path); $sheet = $spreadsheet->getActiveSheet(); $highestRowAndColumn = $sheet->getHighestRowAndColumn(); $excel_data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']); $excelErrorData['excel_header'] = $excel_data[0]; unset($excel_data[0]); // echo '
';

         if($error_data->error_type == 1){

            $finalArray = [];
            foreach ($error_data->error_data as $key => $value) {
    
                foreach ($value as $key2 => $value2) {
                    $error_data = $value2->error;
                    $data = ['value' => $excel_data[$key][$value2->col_idx], 'error'=>$error_data,];
                    $excel_data[$key][$value2->col_idx] = $data;
    
                }
                array_push($finalArray, $excel_data[$key]);
            }
    
            foreach ($finalArray as $fkey => $value){
                foreach ($value as $vkey => $arrayData){
                    if(!is_array($arrayData)){
                        $data = ['value' => $arrayData];
                        $finalArray[$fkey][$vkey] =  $data;
                    }
                }
            }
    
            $excelErrorData['excel_data'] = $finalArray;
            return $excelErrorData;

         }else if($error_data->error_type == 2){


            $allErrors = [];
            $typeTowArray = [];

            foreach ($error_data->error_data as $index => $item) {

                foreach ($item as $field) {
                    if (!isset($allErrors[$index])) {
                        $allErrors[$index] = [];
                    }
                    $allErrors[$index] = array_merge($allErrors[$index], $field->error);
                }
            }


            foreach ($allErrors as $key => $value){
                $data = ['value' => $excel_data[$key][1], 'error'=>$value,];
                $excel_data[$key][1] = $data;
                array_push($typeTowArray, $excel_data[$key]);
            }

            foreach ($typeTowArray as $fkey => $value){
                foreach ($value as $vkey => $arrayData){
                    if(!is_array($arrayData)){
                        $data = ['value' => $arrayData];
                        $typeTowArray[$fkey][$vkey] =  $data;
                    }
                }
            }

            $excelErrorData['excel_data'] = $typeTowArray;
            return $excelErrorData;

         }



 }


// ---------------------------------------------------------------------------------


    
}