nhance/app/Helpers/excel_import_export_helper.php

261 lines
8.9 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')) {
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){
// Call the helper function for Calculate GST, Pro Rata Premium, and Total sums for inception
add_totals_row($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('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('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('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;
}
}
// app/Helpers/filename_helper.php
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';
}
$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;
}
}