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, $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 $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; } }