nhance/app/Helpers/excel_import_export_helper.php

400 lines
14 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
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);
}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) {
// 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,
$obj->difference_premium,
$obj->pro_rata_premimum,
$obj->gst,
$obj->total ,
$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 += $row[18];
$proRataPremiumSum += $row[19];
$totalSum += $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) {
$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);
// Generate file name
$file_name = $client_short_name. '_' . $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
$formattedEvent = ucwords(str_replace('_', ' ', $word));
return $formattedEvent;
}
}