nhance/app/Helpers/excel_util_helper.php
2024-04-18 15:18:16 +05:30

1018 lines
54 KiB
PHP

<?php
use App\Models\EmployeeModel;
if(!function_exists('calculate_days_bw_dates'))
{
function calculate_days_bw_dates(string $from_date = "", string $to_date ="")
{
if($to_date == ""){ $currentDateTime = new DateTime(); }
else{ $currentDateTime = new DateTime($to_date); }
if($from_date == ""){ $passedDateTime = new DateTime(); }
else{ $passedDateTime = new DateTime($from_date); }
return $interval = $currentDateTime->diff($passedDateTime);
}
}
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(strip_tags(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,$policy_terms)
{
// print_r($relationship);
// echo '<br>';
if($row['current_action'] != null && in_array(strtoupper($row['current_action']), ['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(!isset($relationship[ $col ]))
{
return array('status' => false,'error' => 'Rule Conflict: Unknown Relationship');
}
$family_floaters = isset($policy_terms['family_floaters']);
if($family_floaters)
{
if(isset($relationship[ $col ]) && $relationship[ $col ]['gender'] != $row[4])
{
$error = "Gender relationship conflict: Expected ".$relationship[ $col ]['gender'].", received $row[4]";
return array('status' => false,'error' => $error);
}
}
else // if family_floaters not set the its GPA, reject all other dependents
{
if($col != 'self')
{
$error = "Dependent(s) are not allowed";
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['current_action'] != null && in_array(strtoupper($row['current_action']), ['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' => "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,$policy_terms,$slab_details)
{
if($row['current_action'] != null && in_array(strtoupper($row['current_action']), ['I','A','DA']))// check rule only of action column data available
{
$is_emp_band_needed = $slab_details['grid_master']['emp_band'];
$slug = \Config\Services::slug();
$relationship = $slug->slugify($row[5]);
// echo $relationship;echo $row[7];
if($is_emp_band_needed && $relationship == 'self' && $row[10] == "") { return array('status' => false,'error' => "Band mandantory for self"); }
else if($is_emp_band_needed && $relationship == 'self' && $row[10] != "")
{
$received_grade = $row[10];
$found = false;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['grade'] == $received_grade)
{
$found = true;
break;
}
}
if(!$found) { return array('status' => false,'error' => "Emp band/Grade not found"); }
}
return array('status' => true);
}else { return array('status' => true); } // in else condition no need to check rule, just return true
}
}
if(!function_exists('check_si'))
{
function check_si($row,$policy_terms,$slab_details)
{
if($row['current_action'] != null && in_array(strtoupper($row['current_action']), ['I','A','DA','SI']))// check rule only of action column data available
{
$received_si = $row['current_action'] == 'SI' ? $row[3] : $row[6];
$found = false;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $received_si)
{
$found = true;
break;
}
}
if(!$found) { return array('status' => false,'error' => "Sum insured not found"); }
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,$policy_terms,$slab_details)
{
if($row['current_action'] != null && in_array(strtoupper($row['current_action']), ['I','A','DA']))// check rule only of action column data available
{
$is_basic_pay_needed = $slab_details['grid_master']['basicpay'];
$slug = \Config\Services::slug();
$relationship = $slug->slugify($row[5]);
// echo $relationship;echo $row[7];
if($is_basic_pay_needed && $relationship == 'self' && $row[9] == "") { 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['current_action'] != null && in_array(strtoupper($row['current_action']), ['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 = isset($relationships[$relationship]['age_min']) ? $relationships[$relationship]['age_min'] : NULL;
$age_max = isset($relationships[$relationship]['age_max']) ? $relationships[$relationship]['age_max'] : NULL;
// 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,$data_source = 'excel')
{
$result = [];
foreach ($emp_data as $rkey => $row)
{
if($data_source == 'excel')
{
if(!check_row_is_empty_or_null($row))
{
$result[$row[1]][] = $row;
}
}else if($data_source = 'db')
{
$result[$row['emp_code']][] = $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('check_self_available_in_family'))
{
function check_self_available_in_family($family_data)
{
$is_self_found = false;
foreach ($family_data as $rkey => $row)
{
if($row[5] != null && strtolower($row[5]) == 'self')//$row[5] = relationship $row[4] = Gender
{
$is_self_found = true;
break;
}
}
return ['is_self_found' => $is_self_found,'emp_code' => $family_data[0][1]];
}
}
if (!function_exists('name_and_empid_check_in_db'))
{
function name_and_empid_check_in_db($family_data,$actionArr)
{
$employeeModel = new EmployeeModel();
$result = ['del' => [],'i' => []];
$client_id = $actionArr['client_id'];
$policy_id = $actionArr['policy_id'];
$current_action = $actionArr['action'];
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.id",$policy_id)
->where("employees.client_id",$client_id)
// ->where("ep.client_id",$client_id)
->where('name',$row[2])->where('emp_code',$row[1])
->findAll();
// dd($employeeModel->getLastQuery());
if(($current_action == 'deletion' || $current_action == 'correction' || $current_action == 'si_enhancement') && !count($res))
{
array_push($result['del'],$row[0]);
}
if(($current_action == 'inception' || $current_action == 'dependent_addition' || $current_action == 'addition') && count($res))
{
array_push($result['i'],$row[0]);
}
}
return $result;
}
}
if (!function_exists('check_dependent_conflict'))
{
function check_dependent_conflict($family_data,$policy_terms,$actionArr)
{
$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 = [];
$self_emp_row_id = null;
$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'];
$temp = is_array($temp) ? $temp : (is_object($temp) ? (array)$temp : []);
$allowed_child_count = $temp['childrens'];
$allowed_spouse_count = $temp['spouse'];
$allowed_adults = $temp['either-parents-pil'];
$allowed_parents_count = $temp['either-parents-pil'] == 0 ? $temp['parents'] : 0;
$allowed_parent_in_laws_count = $temp['either-parents-pil'] == 0 ? $temp['parents-in-law'] : 0;
// dd($allowed_adults == 0);
foreach ($family_data as $key => $row)
{
// dd($family_data[0][0]);
$relationship = $slug->slugify($row[5]);
if($actionArr != 'deletion' && $relationship != 'son' && $relationship != 'daughter')
{
array_push($overall_famility_relationships, $relationship);
}
if($relationship == 'self')
{
$self_gender = $row[4];
$self_emp_row_id = $row[0];
}
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($actionArr != 'deletion')
{
$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','row_id' => $family_data[0][0]];
}
}
// 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 && $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','row_id' => (isset($self_emp_row_id) ? $self_emp_row_id : $family_data[0][0])];
}
if(($allowed_spouse_count < $received_spouse_count) || ($allowed_child_count < $received_child_count) )
{
$result['status'] = false;
$result['error_data'][] = ['code' => 12,'col_name' => 'sno','msg' => 'Rule conflict: Dependent count greater than allowed dependent count','row_id' => (isset($self_emp_row_id) ? $self_emp_row_id : $family_data[0][0])];//dependent count mismatch
}
// || ($allowed_parents_count < $received_parents_count) || ($allowed_parent_in_laws_count < $received_parent_in_laws_count)
if($allowed_adults == 1 && $received_parents_count && $received_parent_in_laws_count )
{
$result['status'] = false;
$result['error_data'][] = ['code' => 12,'col_name' => 'sno','msg' => 'Rule conflict: Parents and Parents in law both not allowed','row_id' => (isset($self_emp_row_id) ? $self_emp_row_id : $family_data[0][0])];
if((2 < $received_parents_count) || (2 < $received_parent_in_laws_count))
{
$result['status'] = false;
$result['error_data'][] = ['code' => 12,'col_name' => 'sno','msg' => 'Rule conflict: Dependent count greater than allowed dependent count','row_id' => (isset($self_emp_row_id) ? $self_emp_row_id : $family_data[0][0])];//dependent count mismatch
}
}
// var_dump($allowed_adults == 0);
// dd($allowed_adults == 0 && (($allowed_parents_count < $received_parents_count) || ($allowed_parent_in_laws_count < $received_parent_in_laws_count)));
if($allowed_adults == 0 && (($allowed_parents_count < $received_parents_count) || ($allowed_parent_in_laws_count < $received_parent_in_laws_count) ))
{
// dd($allowed_adults);
$result['status'] = false;
$result['error_data'][] = ['code' => 12,'col_name' => 'sno','msg' => 'Rule conflict: Dependent count greater than allowed dependent count','row_id' => (isset($self_emp_row_id) ? $self_emp_row_id : $family_data[0][0])];//dependent count mismatch
}
// }
// else
// {
// if(count($family_data) > 1)
// {
// $result['status'] = false;
// $result['error_data'][] = ['code' => 11,'col_name' => 'sno','msg' => 'Rule conflict: Dependents not allowed','row_id' => $row[0]];//dependents not allowed
// }
// }
return $result;
}
}
if (!function_exists('generate_relationship_code'))
{
function generate_relationship_code($arr)
{
$gender = ['M' => 1,'F' => 2];
$relationship = ['self' => 1,'spouse' => 2,'son' => 3,'daughter' => 4,'father' => 5,'mother' => 6,'father-in-law' => 7,'mother-in-law' => 8];
$slug = \Config\Services::slug();
$relationship_code = $slug->slugify($arr['relationship']);
$relationship_code = $gender[ $arr['gender'] ] . $relationship[ $relationship_code ] ;
return $relationship_code;
}
}
if (!function_exists('calculate_premimum'))
{
function calculate_premimum($family_data,$policy_terms,$slab_details,$fileArr,$default_si = null)
{
//
// dd($fileArr);
// grid type
// 1 = premium => si
$result = [];
$grid_type = $slab_details['grid_master']['ui_type'];
$fileArr['grid_type'] = $grid_type;
//this variable for store emp id and their band for emp band level premium calculation for all famility members (especially for grid type 9) also store max age and max count of a familiy for grid type 10,11
$emp_details_with_empband_max_age_max_count = [];
//max age amoung familiy
$max_age = max(array_map(function($item) {return calculate_days_bw_dates(from_date: $item[3])->y; }, $family_data));
foreach($family_data as $fkey => $member)
{
//transform as db row column
$transformed_familiy_member_data = transform_excel_data_to_db($member,$fileArr);
// dd($transformed_familiy_member_data);
//store emp id and emp band and attach it to their familiy members where band is always empty
$emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['band'] = null;
if(strtolower($transformed_familiy_member_data['relationship']) == 'self')
{
$emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['band'] = $transformed_familiy_member_data['band'];
}
//end of store emp id and emp band and attach it to their familiy members where band is always empty
$emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['maxcount'] =count($family_data);
$emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['maxage'] = $max_age;
//generate relationship code
if($transformed_familiy_member_data['temp']['action'] != 'D' && $transformed_familiy_member_data['temp']['action'] != 'C' && $transformed_familiy_member_data['temp']['action'] != 'SI')
{
$transformed_familiy_member_data['relationship_code'] = generate_relationship_code($transformed_familiy_member_data);
}
//calculate primimum based on grid type
if($transformed_familiy_member_data['temp']['action'] != 'D' && $transformed_familiy_member_data['temp']['action'] != 'C' && $transformed_familiy_member_data['temp']['action'] != 'SI')
{
//before call premium_calculation_manager attach band,max age and max count into the array temporarly for grid 9,10 and 11
$transformed_familiy_member_data['temp']['band'] = $emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['band'];
$transformed_familiy_member_data['temp']['maxage'] = $emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['maxage'];
$transformed_familiy_member_data['temp']['maxcount'] = $emp_details_with_empband_max_age_max_count[ $transformed_familiy_member_data['emp_code'] ]['maxcount'];
if( $fileArr['id'] == null || (in_array($grid_type,[10,11]) || $transformed_familiy_member_data['temp']['source'] == 'excel'))//if file id is null then data coming from enrollment (from DB) otherwise data coming from xcel, so calculate only data from excel (new entry) note: even data coming from excel for event dependet additon we fetch other dependts from db and calculate premium for whole family
{
$transformed_familiy_member_data = premium_calculation_manager($transformed_familiy_member_data,$policy_terms,$slab_details,$default_si);
$result[] = $transformed_familiy_member_data;
}
}
}
return $result;
}
}
if (!function_exists('transform_excel_data_to_db'))
{
function transform_excel_data_to_db($memArr,$actionArr)
{
$current_column_action = null;
if($actionArr['action'] == 'inception'){ $current_column_action = 'I'; }
else if($actionArr['action'] == 'addition'){ $current_column_action = 'A'; }
else if($actionArr['action'] == 'dependent_addition'){ $current_column_action = 'DA'; }
else if($actionArr['action'] == 'deletion'){ $current_column_action = 'D'; }
else if($actionArr['action'] == 'correction'){ $current_column_action = 'C'; }
else if($actionArr['action'] == 'si_enhancement'){ $current_column_action = 'SI'; }
if($actionArr['action'] == 'inception' || $actionArr['action'] == 'addition' || $actionArr['action'] == 'dependent_addition')
{
$policy['basic_cover_si'] = $memArr[6];
$policy['pre_existing_alignments'] = $memArr[14];
$policy['date_of_exit'] = isset($memArr[16]) ? change_date_format($memArr[16],'d-M-Y','Y-m-d') : NULL;
$policy['reason_for_exit'] = $memArr[17];
$policy['client_policy_id'] = $actionArr['policy_id'];
$policy['date_coverage'] = isset($memArr[7]) ? change_date_format($memArr[7],'d-M-Y','Y-m-d') : NULL;;
$policy['policy_end_date'] = null;
$policy['days'] = null;
$policy['premium'] = null;
$policy['rata_premimum'] = null;
$policy['gst'] = null;
$result['emp_code'] = $memArr[1];
$result['name'] = $memArr[2];
$result['dob'] = isset($memArr[3]) ? change_date_format($memArr[3],'d-M-Y','Y-m-d') : NULL;
$result['gender'] = $memArr[4];
$result['relationship'] = $memArr[5];
$result['relationship_code'] = $memArr[5];
$result['doj'] = isset($memArr[8]) ? change_date_format($memArr[8],'d-M-Y','Y-m-d') : NULL;
$result['basic_pay'] = $memArr[9];
$result['band'] = $memArr[10];
$result['designation'] = $memArr[11];
$result['mobile'] = $memArr[12];
$result['email_corporate'] = $memArr[13];
$result['file_id'] = $actionArr['id'];
$result['client_id'] = $actionArr['client_id'];
$result['change_event'] = $memArr[15];
$result['temp']['grid_type'] = $actionArr['grid_type'];
$result['temp']['action'] = isset($memArr['current_action']) ? $memArr['current_action'] : $current_column_action;
$result['temp']['source'] = isset($memArr['temp']['source']) ? $memArr['temp']['source'] : 'excel';
$result['temp']['emp_id'] = isset($memArr['temp']['emp_id']) ? $memArr['temp']['emp_id'] : null;
$result['temp']['emp_policy_id'] = isset($memArr['temp']['emp_policy_id']) ? $memArr['temp']['emp_policy_id'] : null;
$result['policy_details'] = $policy;
return $result;
}
}
}
if (!function_exists('premium_calculation_manager'))
{
function premium_calculation_manager($emp_data,$policy_terms,$slab_details,$default_si = null)
{
// grid type
// 1 = premium => si
$grid_type = $slab_details['grid_master']['ui_type'];
switch ($grid_type) {
case "1":
//GPA - Sum Insured (SI) * Multiplier
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si)
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "2":
//GPA - Flat Rate for all SI
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si)
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "3":
//GMC - SI
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si)
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "4":
//GMC - Employees Age band
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
// dd($employee_received_si);
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
$age = calculate_days_bw_dates(from_date: $emp_data['dob'])->y;
if($slab_value['si'] == $employee_received_si && ($slab_value['age_from'] <= $age && $slab_value['age_to'] >= $age))
{
// dd($slab_value);
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "5":
//GMC - Employees Age + SI
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
$age = calculate_days_bw_dates(from_date: $emp_data['dob'])->y;
if($slab_value['si'] == $employee_received_si && ($slab_value['age_from'] <= $age && $slab_value['age_to'] >= $age))
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "6":
//GMC - Employees + Dependent Age band
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
$age = calculate_days_bw_dates(from_date: $emp_data['dob'])->y;
if($slab_value['si'] == $employee_received_si && ($slab_value['age_from'] <= $age && $slab_value['age_to'] >= $age))
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($emp_data['policy_details']['date_coverage'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "7":
//GMC - Employees + Dependent Age + SI
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
$age = calculate_days_bw_dates(from_date: $emp_data['dob'])->y;
if($slab_value['si'] == $employee_received_si && ($slab_value['age_from'] <= $age && $slab_value['age_to'] >= $age))
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "8":
//GMC - SI as per Grade or Band
$employee_received_band = $emp_data['temp']['band'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['grade'] == $employee_received_band && $slab_value['si'] == $employee_received_si)
{
// $emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "9":
//GMC - Flat Rate for all
$employee_received_band = $emp_data['temp']['band'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['grade'] == $employee_received_band && $slab_value['si'] == $employee_received_si)
{
$emp_data['policy_details']['basic_cover_si'] = $slab_value['si'];
$emp_data['policy_details']['date_coverage'] = (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']);
$emp_data['policy_details']['policy_end_date'] = $policy_terms['policy_end_date'];
$emp_data['policy_details']['days'] = calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days;
$emp_data['policy_details']['premium'] = $slab_value['premium'];
$emp_data['policy_details']['rata_premimum'] = calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']);
$emp_data['policy_details']['gst'] = (float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','');
break;
}
}
break;
case "10":
//GMC - Maximum age of Dependents
$max_age = $emp_data['temp']['maxage'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
$emp_data['policy_details']['basic_cover_si'] = null;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si && ($slab_value['age_from'] <= $max_age && $slab_value['age_to'] >= $max_age))
{
$emp_data['policy_details']['basic_cover_si'] = (strtolower($emp_data['relationship']) == 'self' ? $employee_received_si : null);
$emp_data['policy_details']['date_coverage'] = (strtolower($emp_data['relationship']) == 'self' ? (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']) : "");
$emp_data['policy_details']['policy_end_date'] = (strtolower($emp_data['relationship']) == 'self' ? $policy_terms['policy_end_date'] : "");
$emp_data['policy_details']['days'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days : 0);
$emp_data['policy_details']['premium'] = (strtolower($emp_data['relationship']) == 'self' ? $slab_value['premium'] : 0);
$emp_data['policy_details']['rata_premimum'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']) : 0);
$emp_data['policy_details']['gst'] = (strtolower($emp_data['relationship']) == 'self' ? ((float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','')) : 0);
break;
}
}
break;
case "11":
//GMC - Maximum count per Family
$max_count = $emp_data['temp']['maxcount'];
$employee_received_band = $emp_data['temp']['band'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
$emp_data['policy_details']['basic_cover_si'] = null;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si && ($slab_value['grade'] == $employee_received_band))
{
//calculate premium based on count
$familiy_si_covered = $employee_received_si * $max_count;
$familiy_si_covered = ($familiy_si_covered >= $slab_value['max_si'] ? $slab_value['max_si'] : $familiy_si_covered);
$emp_data['policy_details']['basic_cover_si'] = (strtolower($emp_data['relationship']) == 'self' ? $familiy_si_covered : null);
$emp_data['policy_details']['date_coverage'] = (strtolower($emp_data['relationship']) == 'self' ? (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']) : "");
$emp_data['policy_details']['policy_end_date'] = (strtolower($emp_data['relationship']) == 'self' ? $policy_terms['policy_end_date'] : "");
$emp_data['policy_details']['days'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days : 0);
$emp_data['policy_details']['premium'] = (strtolower($emp_data['relationship']) == 'self' ? $slab_value['premium'] : 0);
$emp_data['policy_details']['rata_premimum'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']) : 0);
$emp_data['policy_details']['gst'] = (strtolower($emp_data['relationship']) == 'self' ? ((float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','')) : 0);
break;
}
}
break;
case "12":
//GMC - Employee + relationship
$max_count = $emp_data['temp']['maxcount'];
$employee_received_band = $emp_data['temp']['band'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
$emp_data['policy_details']['basic_cover_si'] = null;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si && ($slab_value['grade'] == $employee_received_band))
{
//calculate premium based on count
$familiy_si_covered = $employee_received_si * $max_count;
$familiy_si_covered = ($familiy_si_covered >= $slab_value['max_si'] ? $slab_value['max_si'] : $familiy_si_covered);
$emp_data['policy_details']['basic_cover_si'] = (strtolower($emp_data['relationship']) == 'self' ? $familiy_si_covered : null);
$emp_data['policy_details']['date_coverage'] = (strtolower($emp_data['relationship']) == 'self' ? (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']) : "");
$emp_data['policy_details']['policy_end_date'] = (strtolower($emp_data['relationship']) == 'self' ? $policy_terms['policy_end_date'] : "");
$emp_data['policy_details']['days'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days : 0);
$emp_data['policy_details']['premium'] = (strtolower($emp_data['relationship']) == 'self' ? $slab_value['premium'] : 0);
$emp_data['policy_details']['rata_premimum'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']) : 0);
$emp_data['policy_details']['gst'] = (strtolower($emp_data['relationship']) == 'self' ? ((float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','')) : 0);
break;
}
}
break;
case "13":
//GMC - Employee + relationship + age
$max_count = $emp_data['temp']['maxcount'];
$employee_received_band = $emp_data['temp']['band'];
$employee_received_si = $default_si == null ? $emp_data['policy_details']['basic_cover_si'] : $default_si;
$emp_data['policy_details']['basic_cover_si'] = null;
foreach ($slab_details['slab_rates'] as $skey => $slab_value)
{
if($slab_value['si'] == $employee_received_si && ($slab_value['grade'] == $employee_received_band))
{
//calculate premium based on count
$familiy_si_covered = $employee_received_si * $max_count;
$familiy_si_covered = ($familiy_si_covered >= $slab_value['max_si'] ? $slab_value['max_si'] : $familiy_si_covered);
$emp_data['policy_details']['basic_cover_si'] = (strtolower($emp_data['relationship']) == 'self' ? $familiy_si_covered : null);
$emp_data['policy_details']['date_coverage'] = (strtolower($emp_data['relationship']) == 'self' ? (isset($emp_data['policy_details']['date_coverage']) ? $emp_data['policy_details']['date_coverage'] : $policy_terms['policy_start_date']) : "");
$emp_data['policy_details']['policy_end_date'] = (strtolower($emp_data['relationship']) == 'self' ? $policy_terms['policy_end_date'] : "");
$emp_data['policy_details']['days'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_days_bw_dates($policy_terms['policy_start_date'],$policy_terms['policy_end_date'])->days : 0);
$emp_data['policy_details']['premium'] = (strtolower($emp_data['relationship']) == 'self' ? $slab_value['premium'] : 0);
$emp_data['policy_details']['rata_premimum'] = (strtolower($emp_data['relationship']) == 'self' ? calculate_pro_rata_premimum($slab_value['premium'],$emp_data['policy_details']['days']) : 0);
$emp_data['policy_details']['gst'] = (strtolower($emp_data['relationship']) == 'self' ? ((float) number_format(($emp_data['policy_details']['rata_premimum'] * (18/100)),2,'.','')) : 0);
break;
}
}
break;
default:
echo "Not a valid grid";
}
// unset($emp_data['temp']);//remove temp data
return $emp_data;
}
}
if (!function_exists('calculate_pro_rata_premimum'))
{
function calculate_pro_rata_premimum($premium,$days)
{
return (float) number_format(($premium / 365) * $days,2,'.','');
}
}
//transform db columns into excel rows with indexs
if (!function_exists('transform_db_data_to_excel'))
{
function transform_db_data_to_excel($familiyArr,$actionArr = [])
{
// if($actionArr['action'] == 'dependent_addition')
// {
$return_data = [];
foreach ($familiyArr as $key => $value)
{
// dd($value);
$row = [];
$row[0] = ($key + 1);
$row[6] = $value['basic_cover_si'];
$row[14] = $value['pre_existing_alignments'];
$row[16] = isset($value['date_of_exit']) ? change_date_format($value['date_of_exit'], 'Y-m-d', 'd-M-Y') : NULL;
$row[17] = $value['reason_for_exit'];
$row[7] = isset($value['date_coverage']) ? change_date_format($value['date_coverage'], 'Y-m-d', 'd-M-Y') : NULL;
$row[1] = $value['emp_code'];
$row[2] = $value['name'];
$row[3] = isset($value['dob']) ? change_date_format($value['dob'], 'Y-m-d', 'd-M-Y') : NULL;
$row[4] = $value['gender'];
$row[5] = $value['relationship'];
$row[8] = isset($value['doj']) ? change_date_format($value['doj'], 'Y-m-d', 'd-M-Y') : NULL;
$row[9] = $value['basic_pay'];
$row[10] = $value['band'];
$row[11] = $value['designation'];
$row[12] = $value['mobile'];
$row[13] = $value['email_corporate'];
// $actionArr['id'] = $value['file_id'];
// $actionArr['client_id'] = $value['client_id'];
$row[15] = $value['change_event'];
$row['current_action'] = 'DA';
$row['temp']['source'] = 'db';
$row['temp']['emp_id'] = $value['emp_id'];
$row['temp']['emp_policy_id'] = $value['emp_policy_id'];
array_push($return_data, ($row));
}
return $return_data;
// }
}
}
//comparre excel cell names and return range of chars between them
if (!function_exists('compare_excel_cell_range'))
{
function compare_excel_cell_range($highestColumnAllowed, $highestColumnReceivedInExcel) {
$highestColumnAllowed = strtoupper($highestColumnAllowed);
$highestColumnReceivedInExcel = strtoupper($highestColumnReceivedInExcel);
if ($highestColumnAllowed < $highestColumnReceivedInExcel) {
$range = range($highestColumnAllowed, $highestColumnReceivedInExcel);
return $range;
}
return [];
}
}