nhance/app/Helpers/excel_util_helper.php
2024-03-04 09:24:49 +05:30

368 lines
13 KiB
PHP

<?php
use App\Models\EmployeeModel;
if (!function_exists('check_columns_name')) {
function check_columns_name($definedColumns,$excelColumns)
{
$mismatchedColumns = [];
foreach ($definedColumns as $colName => $definedCol) {
$definedColIdx = $definedCol['col_idx'];
$definedColName = $definedCol['col_name'];
if (strtolower(trim($excelColumns[$definedColIdx])) !== strtolower($definedColName)) {
$mismatchedColumns[] = "Column order conflict. Column order no ".($definedColIdx + 1)." expected : ".$definedColName." and received : ".$excelColumns[$definedColIdx]."<br/>";
}
}
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[17] != null && in_array(strtoupper($row[17]), ['I','A','DA']))// check rule only of action column data available
{
if($row[5] != null && $row[4] != null)//$row[5] = relationship $row[4] = Gender
{
$slug = \Config\Services::slug();
$col = $slug->slugify($row[5]);
// echo $col;die();
if($col != 'self' && $col != 'spouse')
{
if($relationship[ $col ]['gender'] != $row[4])
{
$error = "Gender relationship conflict: Expected ".$relationship[ $col ]['gender'].", received $row[4]";
return array('status' => false,'error' => $error);
}
}
return array('status' => true);
}
else
{
return array('status' => false,'error' => 'Rule Conflict: Both Relationship and Gender required for check relationship conflict');
}
}
else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if(!function_exists('check_doj'))
{
function check_doj($row)
{
if($row[17] != null && in_array(strtoupper($row[17]), ['I','A','DA']))// check rule only of action column data available
{
$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);
}
else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if(!function_exists('check_employee_band'))
{
function check_employee_band($row)
{
if($row[17] != null && in_array(strtoupper($row[17]), ['I','A','DA']))// check rule only of action column data available
{
$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);
}else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if(!function_exists('check_basic_pay'))
{
function check_basic_pay($row)
{
if($row[17] != null && in_array(strtoupper($row[17]), ['I','A','DA']))// check rule only of action column data available
{
$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);
}else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if (!function_exists('check_dob_diff'))
{
function check_dob_diff($row,$relationships) {
// echo 'called';
if($row[17] != null && in_array(strtoupper($row[17]), ['I','A','DA']))// check rule only of action column data available
{
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 and Relationship required for age check');
}
}else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if (!function_exists('data_group_by_family'))
{
function data_group_by_family($emp_data)
{
$result = [];
foreach ($emp_data as $rkey => $row)
{
if(!check_row_is_empty_or_null($row))
{
$result[$row[1]][] = $row;
}
}
return $result;
}
}
if (!function_exists('name_dup_check_within_family'))
{
function name_dup_check_within_family($family_data)
{
$result = [];
$temp = [];
foreach ($family_data as $rkey => $row)
{
if(in_array($row[2],$temp))
{
array_push($result,$row[0]);
}
else
{
array_push($temp, $row[2]);
}
}
return $result;
}
}
if (!function_exists('name_and_empid_check_in_db'))
{
function name_and_empid_check_in_db($family_data,$client_id,$policy_id)
{
$employeeModel = new EmployeeModel();
$result = ['del' => [],'i' => []];
foreach ($family_data as $rkey => $row)
{
$res = $employeeModel
->join('client_policy cp',"employees.client_id = cp.client_id")
->join('employee_polices ep',"cp.id = ep.client_policy_id AND employees.id = ep.employee_id")
->where("cp.policy_id",$policy_id)
->where("employees.client_id",$client_id)
->like('name',$row[2],'both')->like('emp_code',$row[1],'both')
->findAll();
if(count($res))
{
if($row[17] == 'D')
{
array_push($result['del'],$row[0]);
}
else
{
array_push($result['i'],$row[0]);
}
}
}
return $result;
}
}
if (!function_exists('check_dependent_conflict'))
{
function check_dependent_conflict($family_data,$policy_terms)
{
$result = ['status' => true];
$self_gender = null;
$spouse_gender = null;
$allowed_spouse_count = 0;
$received_spouse_count = 0;
$allowed_child_count = 0;
$received_child_count = 0;
$allowed_parents_count = 0;
$received_parents_count = 0;
$allowed_parent_in_laws_count = 0;
$received_parent_in_laws_count = 0;
$overall_famility_relationships = [];
$temp_counts = [2,3,4,5,6,7,8,9,10]; //temp variable for check relation repetaed count
$slug = \Config\Services::slug();
if($policy_terms['family_floater'] == true)
{
// $temp_string = implode(" ",$policy_terms['family_floaters']);
$temp = $policy_terms['family_floaters'];
$allowed_child_count = count(preg_grep('/child/',$temp));
$allowed_spouse_count = count(preg_grep('/spouse/',$temp));
$allowed_parents_count = count(preg_grep('/parent[12]/',$temp));
$allowed_parent_in_laws_count = count(preg_grep('/parent_in_law[12]/',$temp));
foreach ($family_data as $key => $row)
{
$relationship = $slug->slugify($row[5]);
if($row[17] != 'D' && $relationship != 'son' && $relationship != 'daughter')
{
array_push($overall_famility_relationships, $relationship);
}
if($relationship == 'self')
{
$self_gender = $row[4];
}
if($relationship == 'spouse')
{
$spouse_gender = $row[4];
$received_spouse_count = $received_spouse_count + 1;
}
if($relationship == 'son' || $relationship == 'daughter')
{
$received_child_count = $received_child_count + 1;
}
if($relationship == 'father' || $relationship == 'mother')
{
$received_parents_count = $received_parents_count + 1;
}
if($relationship == 'father-in-law' || $relationship == 'mother-in-law')
{
$received_parent_in_laws_count = $received_parent_in_laws_count + 1;
}
}
// print_r($overall_famility_relationships);
// echo '<br/>';
if($row[17] != 'D')
{
$repeated_relationships_count = array_count_values($overall_famility_relationships);
// print_r($repeated_relationships_count);
// echo '<br/>';
$repeated_count = array_intersect($repeated_relationships_count,$temp_counts);
if(is_array($repeated_count) && count($repeated_count))
{
$result['status'] = false;
$result['error_data'][] = ['code' => 13,'col_name' => 'relationship','msg' => 'Rule conflict: Twofold relationship found within family'];
}
}
// print_r($repeated_count);
// echo "===============";
// echo "<br/>";
// print_r(array_values(array_count_values($overall_famility_relationships)));
// print_r(in_array(2,array_values(array_count_values($overall_famility_relationships))));
if($self_gender == $spouse_gender)
{
$result['status'] = false;
$result['error_data'][] = ['code' => 4,'col_name' => 'gender','msg' => 'Rule conflict: Self and Spouse cannot be same gender'];
}
if(($allowed_spouse_count < $received_spouse_count) || ($allowed_child_count < $received_child_count) || ($allowed_parents_count < $received_parents_count) || ($allowed_parent_in_laws_count < $received_parent_in_laws_count))
{
$result['status'] = false;
$result['error_data'][] = ['code' => 12,'col_name' => null,'msg' => 'Rule conflict: Dependent count greater than allowed dependent count'];//dependent count mismatch
}
}
else
{
if(count($family_data) > 1)
{
$result['status'] = false;
$result['error_data'][] = ['code' => 11,'col_name' => null,'msg' => 'Rule conflict: Dependents not allowed'];//dependents not allowed
}
}
return $result;
}
}