975 lines
36 KiB
PHP
Executable File
975 lines
36 KiB
PHP
Executable File
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as SpreadsheetReaderException;
|
|
|
|
|
|
|
|
if (!function_exists('generate_random_string')) {
|
|
/**
|
|
* Generate a random string of specified length and type.
|
|
*
|
|
* @param int $length The length of the random string.
|
|
* @param string $type (optional) The type of characters to include in the random string.
|
|
* Possible values: 'numeric', 'alphabetic', 'alphanumeric'.
|
|
* Default is 'numeric'.
|
|
*
|
|
* @return string The generated random string.
|
|
*/
|
|
function generate_random_string($length, $type = 'numeric') {
|
|
$characters = '';
|
|
|
|
// Define character sets based on the type
|
|
switch ($type) {
|
|
case 'numeric':
|
|
$characters = '0123456789';
|
|
break;
|
|
case 'alphabetic':
|
|
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
break;
|
|
case 'alphanumeric':
|
|
default:
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
break;
|
|
}
|
|
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
|
|
// Generate random string
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
|
|
return $randomString;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('generate_excel')) {
|
|
|
|
/*
|
|
This function generates an Excel file using the given headers and data and saves it with the specified filename.
|
|
It utilizes the PhpSpreadsheet library to create and manipulate Excel files.
|
|
|
|
Parameters:
|
|
- $headers: An array containing the column headers for the Excel sheet.
|
|
- $data: An array containing the data to be inserted into the Excel sheet.
|
|
- $filename: The name of the file to be saved.
|
|
- $totals (optional): A flag indicating whether to include total calculations in the Excel sheet.
|
|
*/
|
|
|
|
|
|
function generate_excel($headers, $data, $filename, $totals = null)
|
|
{
|
|
// Create new Spreadsheet object
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Set worksheet title
|
|
$spreadsheet->getActiveSheet()->setTitle('Sheet 1');
|
|
|
|
// Set headers into the spreadsheet
|
|
$spreadsheet->getActiveSheet()->fromArray([$headers], null, 'A1');
|
|
|
|
// Set data into the spreadsheet
|
|
$spreadsheet->getActiveSheet()->fromArray($data, null, 'A2');
|
|
|
|
if($totals == 1){
|
|
// Call the helper function for Calculate GST, Pro Rata Premium, and Total sums for inception
|
|
add_totals_row($spreadsheet, $data, $headers);
|
|
}else if($totals == 2){
|
|
add_totals_for_deletion($spreadsheet, $data);
|
|
}
|
|
|
|
// Create Excel writer
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
try {
|
|
// Save Excel file to the specified path
|
|
$writer->save($filename);
|
|
// $filePath = WRITEPATH."/uploads/import_excel/" . $filename;
|
|
// $writer->save($filePath);
|
|
return true; // Return true if file was successfully saved
|
|
} catch (\Exception $e) {
|
|
// Log or handle the exception
|
|
return false; // Return false if there was an error saving the file
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('transform_objects_to_array_for_inception')) {
|
|
function transform_objects_to_array_for_inception($objects) {
|
|
|
|
// Define an array to store the transformed data
|
|
$data = [];
|
|
$TPAID = "";
|
|
$UHID = "";
|
|
|
|
// Initialize serial number
|
|
$serialNumber = 1;
|
|
|
|
// Iterate through each object
|
|
foreach ($objects as $obj) {
|
|
|
|
$TPAID = isset($obj->tpa_id) && $obj->tpa_id !== '' ? $obj->tpa_id : '';
|
|
$UHID = isset($obj->uhid) && $obj->uhid !== '' ? $obj->uhid : '';
|
|
|
|
// Extract all values for the object
|
|
$rowData = [
|
|
$serialNumber++, // Serial Number
|
|
$obj->emp_name, // Employee Name
|
|
$obj->emp_code, // Employee Code
|
|
$obj->emp_type, // Employee Type
|
|
$obj->emp_relationship_code, // Relationship Code
|
|
$obj->emp_dob, // Date of Birth
|
|
$obj->emp_gender, // Employee Gender
|
|
$obj->pre_existing_alignments, // Pre-existing Alignments
|
|
$obj->basic_cover_si, // Basic Cover SI
|
|
$obj->date_coverage, // Date Coverage
|
|
$obj->emp_age, // Employee Age
|
|
$obj->emp_relationship, // Employee Relationship
|
|
$obj->change_event, // Change Event
|
|
$obj->policy_end_date, // Policy End Date
|
|
$obj->days, // Days
|
|
$TPAID, //EMPTY FIELD FOR TPA ID
|
|
$UHID, //EMPTY FIELD FOR UHID
|
|
$obj->premium, // Premium
|
|
$obj->rata_premimum, // Rata Premium
|
|
$obj->gst, // GST
|
|
$obj->total // Total
|
|
];
|
|
|
|
// Append the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('transform_objects_to_array_for_correction')) {
|
|
function transform_objects_to_array_for_correction($objects) {
|
|
|
|
// Define an array to store the transformed data
|
|
$data = [];
|
|
$endorsement_id = "";
|
|
|
|
// Iterate through each object
|
|
foreach ($objects as $obj) {
|
|
// Extract all values for the object
|
|
$rowData = [
|
|
$obj->emp_code,
|
|
$obj->uhid,
|
|
$obj->emp_name,
|
|
$obj->emp_type,
|
|
$obj->relationship_code,
|
|
$obj->emp_dob,
|
|
$obj->emp_gender,
|
|
$obj->old_value,
|
|
$obj->new_value,
|
|
$obj->remarks,
|
|
$endorsement_id,
|
|
];
|
|
|
|
// Append the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('transform_objects_to_array_for_si_enhancement')) {
|
|
function transform_objects_to_array_for_si_enhancement($objects) {
|
|
|
|
// Define an array to store the transformed data
|
|
$data = [];
|
|
$endorsement_id = "";
|
|
|
|
// Initialize serial number
|
|
$serialNumber = 1;
|
|
|
|
// Iterate through each object
|
|
foreach ($objects as $obj) {
|
|
// Extract all values for the object
|
|
$rowData = [
|
|
$serialNumber++,
|
|
$obj->emp_name,
|
|
$obj->emp_code,
|
|
$obj->emp_type,
|
|
$obj->emp_relationship_code,
|
|
$obj->emp_dob,
|
|
$obj->emp_gender,
|
|
$obj->pre_existing_alignments,
|
|
$obj->new_basic_cover_si,
|
|
$obj->old_basic_cover_si,
|
|
$obj->date_of_coverage,
|
|
$obj->policy_end_date,
|
|
$obj->no_of_days,
|
|
$obj->old_si_premium,
|
|
$obj->new_si_premium,
|
|
round($obj->difference_premium, 2),
|
|
$obj->pro_rata_premimum,
|
|
$obj->gst,
|
|
round($obj->total, 2),
|
|
$endorsement_id,
|
|
];
|
|
|
|
// Append the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('transform_objects_to_array_for_deletion')) {
|
|
function transform_objects_to_array_for_deletion($objects) {
|
|
|
|
// Define an array to store the transformed data
|
|
$data = [];
|
|
$claim_status = "";
|
|
$endorsement_id = "";
|
|
|
|
// Initialize serial number
|
|
$serialNumber = 1;
|
|
|
|
// Iterate through each object
|
|
foreach ($objects as $obj) {
|
|
// Extract all values for the object
|
|
$rowData = [
|
|
$serialNumber++,
|
|
$obj->emp_code,
|
|
$obj->emp_name,
|
|
$obj->emp_dob,
|
|
$obj->emp_gender,
|
|
$obj->emp_relationship,
|
|
$obj->basic_cover_si,
|
|
$obj->dateofexit,
|
|
$obj->policy_end_date,
|
|
$obj->no_of_days,
|
|
$obj->premium,
|
|
$obj->pro_rata_premium,
|
|
$obj->gst,
|
|
$obj->total,
|
|
$claim_status,
|
|
$endorsement_id
|
|
|
|
];
|
|
|
|
// Append the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('add_totals_row')) {
|
|
function add_totals_row(Spreadsheet $spreadsheet, array $data)
|
|
{
|
|
// Calculate GST, Pro Rata Premium, and Total sums
|
|
$gstSum = 0;
|
|
$proRataPremiumSum = 0;
|
|
$totalSum = 0;
|
|
|
|
foreach ($data as $row) {
|
|
$gstSum += floatval($row[18]);
|
|
$proRataPremiumSum += floatval($row[19]);
|
|
$totalSum += floatval($row[20]);
|
|
}
|
|
|
|
// Add a new row with sums
|
|
$lastRow = count($data) + 1; // To get the last row number
|
|
$spreadsheet->getActiveSheet()->setCellValue('R' . ($lastRow + 1), 'TOTALS');
|
|
$spreadsheet->getActiveSheet()->setCellValue('S' . ($lastRow + 1), $gstSum);
|
|
$spreadsheet->getActiveSheet()->setCellValue('T' . ($lastRow + 1), $proRataPremiumSum);
|
|
$spreadsheet->getActiveSheet()->setCellValue('U' . ($lastRow + 1), $totalSum);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('add_totals_for_deletion')) {
|
|
function add_totals_for_deletion(Spreadsheet $spreadsheet, array $data)
|
|
{
|
|
// Calculate GST, Pro Rata Premium, and Total sums
|
|
$gstSum = 0;
|
|
$proRataPremiumSum = 0;
|
|
$totalSum = 0;
|
|
|
|
foreach ($data as $row) {
|
|
$proRataPremiumSum += $row[11];
|
|
$gstSum += $row[12];
|
|
$totalSum += $row[13];
|
|
}
|
|
|
|
// Add a new row with sums
|
|
$lastRow = count($data) + 1; // To get the last row number
|
|
$spreadsheet->getActiveSheet()->setCellValue('K' . ($lastRow + 1), 'TOTALS');
|
|
$spreadsheet->getActiveSheet()->setCellValue('L' . ($lastRow + 1), $proRataPremiumSum);
|
|
$spreadsheet->getActiveSheet()->setCellValue('M' . ($lastRow + 1), $gstSum);
|
|
$spreadsheet->getActiveSheet()->setCellValue('N' . ($lastRow + 1), $totalSum);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('read_excel_file_to_array')) {
|
|
function read_excel_file_to_array($file)
|
|
{
|
|
// Load the Excel file
|
|
$spreadsheet = IOFactory::load($file);
|
|
|
|
// Get the active sheet
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
// Get the highest row and column numbers
|
|
$highestRow = $sheet->getHighestRow();
|
|
$highestColumn = $sheet->getHighestColumn();
|
|
|
|
$data = [];
|
|
|
|
// Iterate through each row
|
|
for ($row = 1; $row <= $highestRow; $row++) {
|
|
// Initialize the row data array
|
|
$rowData = [];
|
|
|
|
// Iterate through each column in the row
|
|
for ($col = 'A'; $col <= $highestColumn; $col++) {
|
|
// Get the cell value
|
|
$value = $sheet->getCell($col . $row)->getValue();
|
|
|
|
// Add the cell value to the row data array
|
|
$rowData[] = $value;
|
|
}
|
|
|
|
// Add the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
// Return the array containing data from the Excel file
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('generate_filename')) {
|
|
function generate_filename($client_short_name, $event_type, $actions, $insurer_or_tpa, $policy_name, $branch_code) {
|
|
|
|
$evenTypeLabel = '';
|
|
if($event_type == 'inception'){
|
|
$evenTypeLabel = 'I';
|
|
}else if($event_type == 'deletion'){
|
|
$evenTypeLabel = 'D';
|
|
}else if($event_type == 'correction'){
|
|
$evenTypeLabel = 'C';
|
|
}else if($event_type == 'si_enhancement'){
|
|
$evenTypeLabel = 'SI';
|
|
}else if($event_type == 'addition'){
|
|
$evenTypeLabel = 'A';
|
|
}else if($event_type == 'dependent_addition'){
|
|
$evenTypeLabel = 'DA';
|
|
}
|
|
|
|
$insurer_or_tpa_lable = "";
|
|
if($insurer_or_tpa == 'insurer'){
|
|
$insurer_or_tpa_lable = 'I';
|
|
}else if($insurer_or_tpa == 'tpa'){
|
|
$insurer_or_tpa_lable = 'T';
|
|
}
|
|
|
|
$actions = 'E';
|
|
$currentDateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
|
|
$formattedDateTime = $currentDateTime->format('d-m-Y_H-i-s');
|
|
$policy_name = str_replace(' ', '_', $policy_name);
|
|
$branch_code = isset($branch_code) ? str_replace(' ', '_', $branch_code) : '_';
|
|
|
|
// Generate file name
|
|
$file_name = $client_short_name. '_' . $branch_code . '_' . $policy_name . '_' . $insurer_or_tpa_lable . $actions . $evenTypeLabel . '_' . $formattedDateTime . '.xlsx';
|
|
return $file_name;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('remove_underscore_capitalize_first_letter')) {
|
|
function remove_underscore_capitalize_first_letter($word) {
|
|
// Remove underscore and capitalize first letter of each word
|
|
$pattern = '/dob/i';
|
|
if (preg_match($pattern, $word)) {
|
|
$formattedEvent = 'DOB';
|
|
return $formattedEvent;
|
|
}
|
|
$formattedEvent = ucwords(str_replace('_', ' ', $word));
|
|
return $formattedEvent;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('formatDateOrReturn')) {
|
|
|
|
/**
|
|
* Formats a given value as a date or returns the original value if it's not a valid date.
|
|
*
|
|
* This function checks if the provided value is a valid date. If it is, it formats
|
|
* the date as 'd-M-Y' (day-month-year) format and returns it. If the value is not
|
|
* a valid date, it returns the original value unchanged.
|
|
*
|
|
* @param mixed $value The value to be formatted as a date or returned unchanged.
|
|
* @return string|mixed The formatted date if the value is a valid date, otherwise the original value.
|
|
*/
|
|
|
|
|
|
function formatDateOrReturn($value)
|
|
{
|
|
// Check if the value is a valid date
|
|
$timestamp = strtotime($value);
|
|
if ($timestamp !== false) {
|
|
// Format the date
|
|
return date('d-M-Y', $timestamp);
|
|
} else {
|
|
// Return the original value
|
|
return $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('format_Excel_BasedOn_Client'))
|
|
{
|
|
function format_Excel_BasedOn_Client($objects, $client_excel_header)
|
|
{
|
|
|
|
$data = [];
|
|
|
|
// Initialize serial number
|
|
$serialNumber = 1;
|
|
|
|
// Iterate through each object
|
|
foreach ($objects as $obj)
|
|
{
|
|
// Extract all values for the object based on the header order
|
|
$rowData = [];
|
|
foreach ($client_excel_header as $header) {
|
|
|
|
$normalizedHeader = strtolower(str_replace(' ', '', $header));
|
|
|
|
switch ($normalizedHeader) {
|
|
case 's.no':
|
|
$rowData[] = $serialNumber++; // Serial Number
|
|
break;
|
|
case 'empid':
|
|
$rowData[] = $obj->emp_code; // Employee Code
|
|
break;
|
|
case 'nameofemp/dep':
|
|
$rowData[] = $obj->emp_name; // Employee Name
|
|
break;
|
|
case 'emp/deptype':
|
|
$rowData[] = $obj->emp_type; // Employee Type
|
|
break;
|
|
case 'relation':
|
|
$rowData[] = $obj->emp_relationship_code; // RELATION
|
|
break;
|
|
case 'dob':
|
|
$rowData[] = $obj->emp_dob; // Employee DOB
|
|
break;
|
|
case 'gender':
|
|
$rowData[] = $obj->emp_gender; //Employee GENDER
|
|
break;
|
|
case 'preexistingailments':
|
|
$rowData[] = $obj->pre_existing_alignments; // PRE EXISTING AILMENTS
|
|
break;
|
|
case 'basiccoversi':
|
|
$rowData[] = $obj->basic_cover_si; // Employee BASIC COVER SI
|
|
break;
|
|
case 'dateofcoverage':
|
|
$rowData[] = $obj->date_coverage; // DATE OF COVERAGE
|
|
break;
|
|
case 'age':
|
|
$rowData[] = $obj->emp_age; // Employee AGE
|
|
break;
|
|
case 'relationship':
|
|
$rowData[] = $obj->emp_relationship; // Employee RELATIONSHIP
|
|
break;
|
|
case 'remarks':
|
|
$rowData[] = $obj->change_event; // REMARKS
|
|
break;
|
|
case 'policyenddate':
|
|
$rowData[] = $obj->policy_end_date; // POLICY END DATE
|
|
break;
|
|
case 'noofdays':
|
|
$rowData[] = $obj->days; // NO OF DAYS
|
|
break;
|
|
case 'tpaid':
|
|
$rowData[] = ''; // TPA ID
|
|
break;
|
|
case 'uhid':
|
|
$rowData[] = ''; // UHID
|
|
break;
|
|
case 'premium':
|
|
$rowData[] = $obj->premium; // PREMIUM
|
|
break;
|
|
case 'proratapremium':
|
|
$rowData[] = $obj->rata_premimum; // PR0 RATA PREMIUM
|
|
break;
|
|
case 'gst':
|
|
$rowData[] = $obj->gst; // GST
|
|
break;
|
|
case 'total':
|
|
$rowData[] = $obj->total; // TOTAL
|
|
break;
|
|
default:
|
|
// If the header doesn't match any property, add an empty string
|
|
$rowData[] = '';
|
|
break;
|
|
}
|
|
}
|
|
// Append the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if(!function_exists('generate_excel_second_stage')){
|
|
|
|
function generate_excel_second_stage($headers, $data, $filename, $totals = null)
|
|
{
|
|
// Create a new Spreadsheet object
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Set the active sheet title
|
|
$spreadsheet->getActiveSheet()->setTitle('Sheet 1');
|
|
|
|
// Set headers into the spreadsheet
|
|
$headerValues = [];
|
|
foreach ($headers as $header) {
|
|
$headerValues[] = $header['column_name'];
|
|
}
|
|
$spreadsheet->getActiveSheet()->fromArray([$headerValues], null, 'A1');
|
|
|
|
// Set data into the spreadsheet
|
|
$dataValues = [];
|
|
$serialNumber = 0; // Initialize serial number
|
|
foreach ($data as $row) {
|
|
$serialNumber++; // Increment serial number for each row
|
|
|
|
$rowData = [];
|
|
foreach ($headers as $header) {
|
|
$fieldName = $header['header_name'];
|
|
|
|
if ($fieldName === 'index') {
|
|
$value = $serialNumber;
|
|
} else {
|
|
$value = $row->$fieldName ?? '';
|
|
}
|
|
|
|
$rowData[] = $value;
|
|
}
|
|
$dataValues[] = $rowData;
|
|
}
|
|
// dd($dataValues, $headerValues);
|
|
$spreadsheet->getActiveSheet()->fromArray($dataValues, null, 'A2');
|
|
|
|
// Add totals if specified
|
|
if ($totals === 1) {
|
|
// Call function to add totals row for inception
|
|
add_totals_row($spreadsheet, $data, $headers);
|
|
} else if ($totals === 2) {
|
|
// Call function to add totals row for deletion
|
|
add_totals_for_deletion($spreadsheet, $data);
|
|
}
|
|
|
|
// Create Excel writer
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
try {
|
|
// Save Excel file to the specified path
|
|
$writer->save($filename);
|
|
return true; // Return true if file was successfully saved
|
|
} catch (\Exception $e) {
|
|
// Log or handle the exception
|
|
return false; // Return false if there was an error saving the file
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if(!function_exists('generate_excel_third_stage')){
|
|
|
|
function generate_excel_third_stage($headers, $data, $filename, $totals = null)
|
|
{
|
|
// Create a new Spreadsheet object
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Set the active sheet title
|
|
$spreadsheet->getActiveSheet()->setTitle('Sheet 1');
|
|
|
|
$spreadsheet->getActiveSheet()->fromArray([$headers], null, 'A1');
|
|
|
|
$spreadsheet->getActiveSheet()->fromArray($data, null, 'A2');
|
|
|
|
// Add totals if specified
|
|
if ($totals === 1) {
|
|
// Call function to add totals row for inception
|
|
add_totals_row($spreadsheet, $data, $headers);
|
|
} else if ($totals === 2) {
|
|
// Call function to add totals row for deletion
|
|
add_totals_for_deletion($spreadsheet, $data);
|
|
}
|
|
|
|
// Create Excel writer
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
try {
|
|
// Save Excel file to the specified path
|
|
$writer->save($filename);
|
|
return true; // Return true if file was successfully saved
|
|
} catch (\Exception $e) {
|
|
// Log or handle the exception
|
|
return false; // Return false if there was an error saving the file
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if(!function_exists('generate_insurer_based_excel')){
|
|
|
|
function generate_insurer_based_excel($excel_header_array, $excel_need_data){
|
|
|
|
usort($excel_header_array, function($a, $b) {
|
|
return $a['column_index'] <=> $b['column_index'];
|
|
});
|
|
|
|
|
|
//construct HEADER
|
|
$headerValues = [];
|
|
foreach ($excel_header_array as $header) {
|
|
$headerValues[] = $header['column_name'];
|
|
}
|
|
|
|
//construct EXCEL DATA
|
|
$dataValues = [];
|
|
$serialNumber = 0; // Initialize serial number
|
|
foreach ($excel_need_data as $row) {
|
|
$serialNumber++;
|
|
|
|
$rowData = [];
|
|
foreach ($excel_header_array as $header) {
|
|
$fieldName = $header['db_column_name'];
|
|
|
|
if ($fieldName === 'index') {
|
|
$value = $serialNumber;
|
|
} else {
|
|
$value = $row->$fieldName ?? '';
|
|
}
|
|
|
|
$rowData[] = $value;
|
|
}
|
|
$dataValues[] = $rowData;
|
|
}
|
|
|
|
return ['header' => $headerValues, 'data' => $dataValues];
|
|
}
|
|
}
|
|
|
|
|
|
if(!function_exists('read_first_row_of_excel')){
|
|
|
|
function read_first_row_of_excel($file)
|
|
{
|
|
if ($file->isValid() && !$file->hasMoved()) {
|
|
try {
|
|
$reader = IOFactory::createReaderForFile($file->getPathname());
|
|
$spreadsheet = $reader->load($file->getPathname());
|
|
|
|
// Get the active sheet
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
// Initialize the data array
|
|
$data = [];
|
|
|
|
// Iterate through rows to read data (only the first row)
|
|
$row = $sheet->getRowIterator()->current(); // Get the first row
|
|
$rowData = [];
|
|
foreach ($row->getCellIterator() as $cell) {
|
|
$rowData[] = $cell->getValue();
|
|
}
|
|
$data = $rowData; // Assign the first row data to $data
|
|
|
|
return $data;
|
|
|
|
} catch (SpreadsheetReaderException $e) {
|
|
error_log('PhpSpreadsheet reader exception: ' . $e->getMessage());
|
|
return $e->getMessage();
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if(!function_exists('query_construct_for_emp_policy_id')){
|
|
function query_construct_for_emp_policy_id($employees, $col_index){
|
|
|
|
// Load the database connection
|
|
$db = \Config\Database::connect();
|
|
|
|
// Start building the query
|
|
$subQuery = '';
|
|
foreach ($employees as $index => $employee) {
|
|
if ($index > 1) {
|
|
$subQuery .= " UNION ALL ";
|
|
}
|
|
if($index == 1){
|
|
$row_id_alise = ' AS row_id,';
|
|
$emp_name_alise = 'AS name,';
|
|
$emp_code_alise = 'AS emp_code,';
|
|
$emp_dob_alise = 'AS emp_dob,';
|
|
$emp_gender_alise = 'AS emp_gender,';
|
|
$pre_existing_alignments_alise = 'AS pre_existing_alignments,';
|
|
$basic_cover_si_alise = 'AS basic_cover_si,';
|
|
$emp_relationship_alise = 'AS emp_relationship,';
|
|
$policy_end_date_alise = 'AS policy_end_date,';
|
|
$days_alise = 'AS days,';
|
|
$premium_alise = 'AS premium,';
|
|
$rata_premimum_alise = 'AS rata_premimum,';
|
|
$gst_alise = 'AS gst';
|
|
}else{
|
|
$row_id_alise = ',';
|
|
$emp_name_alise = ',';
|
|
$emp_code_alise = ',';
|
|
$emp_dob_alise = ',';
|
|
$emp_gender_alise = ',';
|
|
$pre_existing_alignments_alise = ',';
|
|
$basic_cover_si_alise = ',';
|
|
$emp_relationship_alise = ',';
|
|
$policy_end_date_alise = ',';
|
|
$days_alise = ',';
|
|
$premium_alise = ',';
|
|
$rata_premimum_alise = ',';
|
|
$gst_alise = '';
|
|
}
|
|
$subQuery .= "SELECT " . intval($index) . $row_id_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_name']] . "'" . $emp_name_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_code']] . "'" . $emp_code_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_dob']] . "'" . $emp_dob_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_gender']] . "'" . $emp_gender_alise;
|
|
$subQuery .= "'" . $employee[$col_index['pre_existing_alignments']] . "'" . $pre_existing_alignments_alise;
|
|
$subQuery .= "'" . $employee[$col_index['basic_cover_si']] . "'" . $basic_cover_si_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_relationship']] . "'" . $emp_relationship_alise;
|
|
$subQuery .= "'" . $employee[$col_index['policy_end_date']] . "'" . $policy_end_date_alise;
|
|
$subQuery .= "'" . $employee[$col_index['days']] . "'" . $days_alise;
|
|
$subQuery .= "'" . $employee[$col_index['premium']] . "'" . $premium_alise;
|
|
$subQuery .= "'" . $employee[$col_index['rata_premimum']] . "'" . $rata_premimum_alise;
|
|
$subQuery .= "'" . $employee[$col_index['gst']] . "'" . $gst_alise;
|
|
}
|
|
|
|
// Complete the query
|
|
$mainQuery = "
|
|
SELECT
|
|
c.row_id,
|
|
c.emp_code,
|
|
c.name,
|
|
c.emp_dob,
|
|
c.emp_gender ,
|
|
c.emp_relationship,
|
|
c.pre_existing_alignments,
|
|
c.basic_cover_si ,
|
|
c.policy_end_date ,
|
|
c.days ,
|
|
c.premium ,
|
|
c.rata_premimum ,
|
|
c.gst,
|
|
|
|
e.emp_code AS db_emp_code,
|
|
e.name AS db_name ,
|
|
e.dob as db_dob,
|
|
e.gender as db_gender,
|
|
e.relationship as db_emp_relationship,
|
|
ep.pre_existing_alignments as db_pre_existing_alignments,
|
|
ep.basic_cover_si as db_basic_cover_si,
|
|
ep.policy_end_date as db_policy_end_date,
|
|
ep.days as db_days,
|
|
ep.premium as db_premium,
|
|
ep.rata_premimum as db_rata_premimum,
|
|
ep.gst as db_gst
|
|
|
|
FROM ({$subQuery}) AS c
|
|
LEFT JOIN employees e
|
|
ON c.emp_code = e.emp_code
|
|
AND c.name = e.name
|
|
AND c.emp_dob = e.dob
|
|
AND c.emp_gender = e.gender
|
|
AND c.emp_relationship = e.relationship
|
|
|
|
LEFT JOIN employee_polices ep
|
|
ON c.pre_existing_alignments = ep.pre_existing_alignments
|
|
AND c.basic_cover_si = ep.basic_cover_si
|
|
AND c.policy_end_date = ep.policy_end_date
|
|
AND c.days = ep.days
|
|
AND c.premium = ep.premium
|
|
AND c.rata_premimum = ep.rata_premimum
|
|
AND c.gst = ep.gst
|
|
|
|
WHERE e.emp_code IS NOT NULL
|
|
AND e.name IS NOT NULL
|
|
AND e.dob IS NOT NULL
|
|
AND e.gender IS NOT NULL
|
|
AND e.relationship IS NOT NULL
|
|
AND ep.pre_existing_alignments IS NOT NULL
|
|
AND ep.basic_cover_si IS NOT NULL
|
|
AND ep.policy_end_date IS NOT NULL
|
|
AND ep.days IS NOT NULL
|
|
AND ep.premium IS NOT NULL
|
|
AND ep.rata_premimum IS NOT NULL
|
|
AND ep.gst IS NOT NULL
|
|
";
|
|
|
|
|
|
// return $mainQuery;
|
|
// Execute the query
|
|
$query = $db->query($mainQuery);
|
|
$result = $query->getResultArray();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
if(!function_exists('query_construct_second_stage')){
|
|
function query_construct_second_stage($employees, $col_index){
|
|
|
|
// Load the database connection
|
|
$db = \Config\Database::connect();
|
|
|
|
// Start building the query
|
|
$subQuery = '';
|
|
foreach ($employees as $index => $employee) {
|
|
if ($index > 1) {
|
|
$subQuery .= " UNION ALL ";
|
|
}
|
|
if($index == 1){
|
|
$row_id_alise = ' AS row_id,';
|
|
$emp_name_alise = 'AS name,';
|
|
$emp_code_alise = 'AS emp_code,';
|
|
$emp_dob_alise = 'AS emp_dob,';
|
|
$emp_gender_alise = 'AS emp_gender,';
|
|
$pre_existing_alignments_alise = 'AS pre_existing_alignments,';
|
|
$basic_cover_si_alise = 'AS basic_cover_si,';
|
|
$emp_relationship_alise = 'AS emp_relationship,';
|
|
$policy_end_date_alise = 'AS policy_end_date,';
|
|
$days_alise = 'AS days,';
|
|
$premium_alise = 'AS premium,';
|
|
$rata_premimum_alise = 'AS rata_premimum,';
|
|
$gst_alise = 'AS gst';
|
|
}else{
|
|
$row_id_alise = ',';
|
|
$emp_name_alise = ',';
|
|
$emp_code_alise = ',';
|
|
$emp_dob_alise = ',';
|
|
$emp_gender_alise = ',';
|
|
$pre_existing_alignments_alise = ',';
|
|
$basic_cover_si_alise = ',';
|
|
$emp_relationship_alise = ',';
|
|
$policy_end_date_alise = ',';
|
|
$days_alise = ',';
|
|
$premium_alise = ',';
|
|
$rata_premimum_alise = ',';
|
|
$gst_alise = '';
|
|
}
|
|
$subQuery .= "SELECT " . intval($index) . $row_id_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_name']] . "'" . $emp_name_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_code']] . "'" . $emp_code_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_dob']] . "'" . $emp_dob_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_gender']] . "'" . $emp_gender_alise;
|
|
$subQuery .= "'" . $employee[$col_index['pre_existing_alignments']] . "'" . $pre_existing_alignments_alise;
|
|
$subQuery .= "'" . $employee[$col_index['basic_cover_si']] . "'" . $basic_cover_si_alise;
|
|
$subQuery .= "'" . $employee[$col_index['emp_relationship']] . "'" . $emp_relationship_alise;
|
|
$subQuery .= "'" . $employee[$col_index['policy_end_date']] . "'" . $policy_end_date_alise;
|
|
$subQuery .= "'" . $employee[$col_index['days']] . "'" . $days_alise;
|
|
$subQuery .= "'" . $employee[$col_index['premium']] . "'" . $premium_alise;
|
|
$subQuery .= "'" . $employee[$col_index['rata_premimum']] . "'" . $rata_premimum_alise;
|
|
$subQuery .= "'" . $employee[$col_index['gst']] . "'" . $gst_alise;
|
|
}
|
|
|
|
// Complete the query
|
|
$mainQuery = "
|
|
SELECT
|
|
c.row_id,
|
|
c.emp_code,
|
|
c.name,
|
|
c.emp_dob,
|
|
c.emp_gender ,
|
|
c.emp_relationship,
|
|
c.pre_existing_alignments,
|
|
c.basic_cover_si ,
|
|
c.policy_end_date ,
|
|
c.days ,
|
|
c.premium ,
|
|
c.rata_premimum ,
|
|
c.gst,
|
|
|
|
e.emp_code AS db_emp_code,
|
|
e.name AS db_name ,
|
|
e.dob as db_dob,
|
|
e.gender as db_gender,
|
|
e.relationship as db_emp_relationship,
|
|
ep.pre_existing_alignments as db_pre_existing_alignments,
|
|
ep.basic_cover_si as db_basic_cover_si,
|
|
ep.policy_end_date as db_policy_end_date,
|
|
ep.days as db_days,
|
|
ep.premium as db_premium,
|
|
ep.rata_premimum as db_rata_premimum,
|
|
ep.gst as db_gst
|
|
|
|
FROM ({$subQuery}) AS c
|
|
LEFT JOIN employees e
|
|
ON c.emp_code = e.emp_code
|
|
AND c.name = e.name
|
|
AND c.emp_dob = e.dob
|
|
AND c.emp_gender = e.gender
|
|
AND c.emp_relationship = e.relationship
|
|
|
|
LEFT JOIN employee_polices ep
|
|
ON c.pre_existing_alignments = ep.pre_existing_alignments
|
|
AND c.basic_cover_si = ep.basic_cover_si
|
|
AND c.policy_end_date = ep.policy_end_date
|
|
AND c.days = ep.days
|
|
AND c.premium = ep.premium
|
|
AND c.rata_premimum = ep.rata_premimum
|
|
AND c.gst = ep.gst
|
|
|
|
WHERE e.emp_code IS NULL
|
|
AND e.name IS NULL
|
|
AND e.dob IS NULL
|
|
AND e.gender IS NULL
|
|
AND e.relationship IS NULL
|
|
AND ep.pre_existing_alignments IS NULL
|
|
AND ep.basic_cover_si IS NULL
|
|
AND ep.policy_end_date IS NULL
|
|
AND ep.days IS NULL
|
|
AND ep.premium IS NULL
|
|
AND ep.rata_premimum IS NULL
|
|
AND ep.gst IS NULL
|
|
";
|
|
|
|
|
|
// return $mainQuery;
|
|
// Execute the query
|
|
$query = $db->query($mainQuery);
|
|
$result = $query->getResultArray();
|
|
return $result;
|
|
}
|
|
}
|
|
|