FEAT_EXCEL MERGE HELPER ADDED
This commit is contained in:
parent
2094e680d7
commit
e8dd72da0e
@ -126,6 +126,10 @@ class JobWorker extends AdminController
|
|||||||
'employeesEnrollmentInsert' => [
|
'employeesEnrollmentInsert' => [
|
||||||
'type' => 'CC', // Handler Category
|
'type' => 'CC', // Handler Category
|
||||||
'handler' => 'App\Controllers\EmployeeServiceController',
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
||||||
|
],
|
||||||
|
'calculateMembersDemography' => [
|
||||||
|
'type' => 'CC', // Handler Category
|
||||||
|
'handler' => 'App\Controllers\LeadsController',
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -4,10 +4,14 @@ namespace App\Controllers;
|
|||||||
|
|
||||||
use CodeIgniter\API\ResponseTrait;
|
use CodeIgniter\API\ResponseTrait;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||||
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||||
|
|
||||||
use App\Models\UserModel;
|
use App\Models\UserModel;
|
||||||
use App\Models\ClientModel;
|
use App\Models\ClientModel;
|
||||||
@ -24,6 +28,8 @@ use App\Models\RFQModel;
|
|||||||
use App\Models\InsurerModel;
|
use App\Models\InsurerModel;
|
||||||
|
|
||||||
use App\Helpers\MailHelper;
|
use App\Helpers\MailHelper;
|
||||||
|
use App\Helpers\ExcelMergeHelper;
|
||||||
|
use Google\Service\CloudSearch\PushItem;
|
||||||
use Kint;
|
use Kint;
|
||||||
|
|
||||||
|
|
||||||
@ -92,7 +98,9 @@ class LeadsController extends BaseController
|
|||||||
{
|
{
|
||||||
|
|
||||||
// $d = $this->constructExcelToSaveTemp(24, 1, $propsal_and_insurer = null);
|
// $d = $this->constructExcelToSaveTemp(24, 1, $propsal_and_insurer = null);
|
||||||
// $d = $this->calculateMembersDemography(['lead_id' => 34]);
|
$job_details = new Jobs();
|
||||||
|
// $r = Jobs::addJob(['job_name' => 'calculateMembersDemography', 'payload' => ['lead_id' => 24]]);
|
||||||
|
// $d = $this->calculateMembersDemography(['lead_id' => 24]);
|
||||||
//$this->mergeQuoteExcelFileWithMembersListExcelFile(24, 1, $propsal_and_insurer = null);
|
//$this->mergeQuoteExcelFileWithMembersListExcelFile(24, 1, $propsal_and_insurer = null);
|
||||||
// dd($d);
|
// dd($d);
|
||||||
$data['page_name'] = 'Leads';
|
$data['page_name'] = 'Leads';
|
||||||
@ -315,7 +323,6 @@ class LeadsController extends BaseController
|
|||||||
} else {
|
} else {
|
||||||
return $this->respond(['status' => false], 200);
|
return $this->respond(['status' => false], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function insertLeadStatus($primaryKey, $status, $statusType)
|
private function insertLeadStatus($primaryKey, $status, $statusType)
|
||||||
@ -333,7 +340,8 @@ class LeadsController extends BaseController
|
|||||||
//--------RFQ-----------------------------------------------------------------------------------------------
|
//--------RFQ-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
public function viewRFQ($id, $type = 1){
|
public function viewRFQ($id, $type = 1)
|
||||||
|
{
|
||||||
|
|
||||||
$data['rfq_data'] = $this->RFQModel
|
$data['rfq_data'] = $this->RFQModel
|
||||||
->where('lead_id', $id)
|
->where('lead_id', $id)
|
||||||
@ -380,7 +388,8 @@ class LeadsController extends BaseController
|
|||||||
$this->loadLayout('view_rfq.php', $data);
|
$this->loadLayout('view_rfq.php', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createRFQ(){
|
public function createRFQ()
|
||||||
|
{
|
||||||
|
|
||||||
$data = $this->request->getPost();
|
$data = $this->request->getPost();
|
||||||
$lead_id = $data['lead_id'];
|
$lead_id = $data['lead_id'];
|
||||||
@ -399,10 +408,10 @@ class LeadsController extends BaseController
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $this->respond(['status' => false, 'id' => $result, 'message' => "Failed to create RFQ", 'data' => $data], 200);
|
return $this->respond(['status' => false, 'id' => $result, 'message' => "Failed to create RFQ", 'data' => $data], 200);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createQCR(){
|
public function createQCR()
|
||||||
|
{
|
||||||
|
|
||||||
$data = $this->request->getPost();
|
$data = $this->request->getPost();
|
||||||
$lead_id = $data['lead_id'];
|
$lead_id = $data['lead_id'];
|
||||||
@ -429,14 +438,15 @@ class LeadsController extends BaseController
|
|||||||
|
|
||||||
|
|
||||||
//export main route function
|
//export main route function
|
||||||
public function exportQCRandRFQ($lead_id, $type, $export_type){
|
public function exportQCRandRFQ($lead_id, $type, $export_type)
|
||||||
|
{
|
||||||
$this->exportExcelForQCRandRFQ($lead_id, $type);
|
$this->exportExcelForQCRandRFQ($lead_id, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
//FOR EXCEL
|
//FOR EXCEL
|
||||||
public function exportExcelForQCRandRFQ($lead_id, $type)
|
public function exportExcelForQCRandRFQ($lead_id, $type)
|
||||||
{
|
{
|
||||||
//$filepath = $this->constructExcelToSaveTemp($lead_id, $type);
|
$filepath = $this->constructExcelToSaveTemp($lead_id, $type);
|
||||||
$filepath = $filepath['filePath'];
|
$filepath = $filepath['filePath'];
|
||||||
|
|
||||||
if (file_exists($filepath)) {
|
if (file_exists($filepath)) {
|
||||||
@ -645,7 +655,6 @@ class LeadsController extends BaseController
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-size columns
|
// Auto-size columns
|
||||||
@ -673,9 +682,7 @@ class LeadsController extends BaseController
|
|||||||
{
|
{
|
||||||
$rfq_data = $this->RFQModel->getRFQTableDataWithLeadIDAndType($lead_id, $type);
|
$rfq_data = $this->RFQModel->getRFQTableDataWithLeadIDAndType($lead_id, $type);
|
||||||
|
|
||||||
if($source_file_path && $rfq_data['file_name'])
|
if ($source_file_path && $rfq_data['file_name']) {
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -685,19 +692,16 @@ class LeadsController extends BaseController
|
|||||||
{
|
{
|
||||||
$lead_id = $params['lead_id'];
|
$lead_id = $params['lead_id'];
|
||||||
$lead_data = $this->leadsModel->find($lead_id);
|
$lead_data = $this->leadsModel->find($lead_id);
|
||||||
// print_rr($lead_data);
|
if ($lead_data['file_name']) {
|
||||||
if($lead_data['file_name'])
|
|
||||||
{
|
|
||||||
$file_name_with_path = WRITEPATH . "/uploads/lead_files/" . $lead_data['file_name'];
|
$file_name_with_path = WRITEPATH . "/uploads/lead_files/" . $lead_data['file_name'];
|
||||||
|
|
||||||
//check physical file
|
//check physical file
|
||||||
if(!file_exists($file_name_with_path))
|
if (!file_exists($file_name_with_path)) {
|
||||||
{
|
|
||||||
//file not found update status and reason
|
//file not found update status and reason
|
||||||
$message = "Lead Physcial file not found";
|
$message = "Lead Physcial file not found";
|
||||||
// echo $message;
|
// echo $message;
|
||||||
$this->myLogger->logme('error', ($message . ' for file ' . $file_name_with_path));
|
$this->myLogger->logme('error', ($message . ' for file ' . $file_name_with_path));
|
||||||
return ['status' => 'failed','message' => 'no physcial file'];
|
return ['status' => 'failed', 'message' => 'no physical file'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path);
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name_with_path);
|
||||||
@ -707,17 +711,16 @@ class LeadsController extends BaseController
|
|||||||
$highestRowAndColumn = $members_sheet->getHighestRowAndColumn();
|
$highestRowAndColumn = $members_sheet->getHighestRowAndColumn();
|
||||||
// dd($highestRowAndColumn);
|
// dd($highestRowAndColumn);
|
||||||
$members = $members_sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
|
$members = $members_sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
|
||||||
// dd($members);
|
// dd($members[6]);
|
||||||
|
|
||||||
//get age band data
|
//get age band data
|
||||||
$age_band_sheet = $spreadsheet->getSheet(1);
|
$age_band_sheet = $spreadsheet->getSheet(1);
|
||||||
$highestRowAndColumn = $age_band_sheet->getHighestRowAndColumn();
|
$highestRowAndColumn = $age_band_sheet->getHighestRowAndColumn();
|
||||||
$age_band_data = $age_band_sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
|
$age_band_data = $age_band_sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
|
||||||
print_r($age_band_data);
|
|
||||||
|
|
||||||
//check age or dob column
|
//check age or dob column
|
||||||
$members_heading = $members[0];
|
$members_heading = $members[0];
|
||||||
Kint::dump($members_heading);
|
|
||||||
$available_col = null;
|
$available_col = null;
|
||||||
$col_index = null;
|
$col_index = null;
|
||||||
|
|
||||||
@ -734,16 +737,346 @@ class LeadsController extends BaseController
|
|||||||
$this->myLogger->logme('error', ($message . $file_name_with_path));
|
$this->myLogger->logme('error', ($message . $file_name_with_path));
|
||||||
return ['status' => 'failed', 'message' => $message];
|
return ['status' => 'failed', 'message' => $message];
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
//convert age slab data into array
|
$classifiers = $this->getDemographyData($members, $age_band_data, $members_heading, $available_col, $col_index);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return ['status' => 'fail', 'message' => $e->getMessage()];
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
try {
|
||||||
|
$result = $this->generateClassifierSpreadsheet($classifiers, 'writable/uploads/lead_files');
|
||||||
|
|
||||||
|
if ($result['success']) {
|
||||||
|
$this->myLogger->logme('error', "Spreadsheet generated successfully!");
|
||||||
|
echo "Location: " . $result['fullpath'] . "\n";
|
||||||
|
echo "Filename: " . $result['filename'] . "\n";
|
||||||
|
$filePaths = [
|
||||||
|
['file_path' => WRITEPATH . 'uploads/lead_files/' . $lead_data['file_name'], 'sheets' => [0, 1]],
|
||||||
|
['file_path' => 'writable/uploads/lead_files/' . $result['filename'], 'sheets' => []],
|
||||||
|
];
|
||||||
|
$outputPath = WRITEPATH . 'uploads/lead_files/Member_Data.xlsx';
|
||||||
|
$result = ExcelMergeHelper::mergeExcelFiles($filePaths, $outputPath);
|
||||||
|
if ($result) {
|
||||||
|
return ['status' => 'success', 'message' => 'Member_Data Merged Suceesfully'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Error generating spreadsheet: " . $result['error'];
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "Error: " . $e->getMessage();
|
||||||
|
return ['status' => 'fail', 'message' => $e->getMessage()];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$this->myLogger->logme('error', (' no file found ' . $file_name_with_path));
|
$this->myLogger->logme('error', (' no file found ' . $file_name_with_path));
|
||||||
return ['status' => 'failed', 'message' => 'no file found'];
|
return ['status' => 'failed', 'message' => 'no file found'];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDemographyData($members, $age_band_data, $members_heading, $available_col, $col_index)
|
||||||
|
{
|
||||||
|
$first_loop = 0;
|
||||||
|
$col_index_si = array_search('si enhancement', array_map('strtolower', $members_heading));
|
||||||
|
$col_index_relationship = array_search('relationship', array_map('strtolower', $members_heading));
|
||||||
|
$relations = [];
|
||||||
|
$si_amt = ['general']; // Initialize with 'general' as per first loop condition
|
||||||
|
|
||||||
|
// First pass - collect unique relations and SI amounts
|
||||||
|
foreach ($members as $member) {
|
||||||
|
if ($first_loop == 0) {
|
||||||
|
$first_loop++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($member == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array($member[$col_index_relationship], $relations) && $member[$col_index_relationship] != null) {
|
||||||
|
$relations[] = $member[$col_index_relationship];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array($member[$col_index_si], $si_amt) && $member[$col_index_si] != null) {
|
||||||
|
$si_amt[] = $member[$col_index_si];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Initialize classifier array
|
||||||
|
$classifiers = [];
|
||||||
|
foreach ($si_amt as $si) {
|
||||||
|
$classifiers[$si] = [];
|
||||||
|
// Initialize relation counts including Grand Total row
|
||||||
|
foreach ($relations as $relation) {
|
||||||
|
$classifiers[$si][$relation] = [];
|
||||||
|
foreach ($age_band_data as $age_bands) {
|
||||||
|
foreach ($age_bands as $age_interval) {
|
||||||
|
$classifiers[$si][$relation][$age_interval] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add Grand Total column for each relation echo('test passed 3.5');
|
||||||
|
|
||||||
|
$classifiers[$si][$relation]['Grand Total'] = 0;
|
||||||
|
}
|
||||||
|
// Initialize Grand Total row
|
||||||
|
$classifiers[$si]['Grand Total'] = [];
|
||||||
|
foreach ($age_band_data as $age_bands) {
|
||||||
|
foreach ($age_bands as $age_interval) {
|
||||||
|
$classifiers[$si]['Grand Total'][$age_interval] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add grand total of grand totals
|
||||||
|
$classifiers[$si]['Grand Total']['Grand Total'] = 0;
|
||||||
|
}
|
||||||
|
// Process members and count them
|
||||||
|
foreach ($members as $member) {
|
||||||
|
if ($first_loop == 0) {
|
||||||
|
$first_loop++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Get age
|
||||||
|
$age = $this->getAge($available_col, $member, $col_index);
|
||||||
|
|
||||||
|
|
||||||
|
$relation = $member[$col_index_relationship];
|
||||||
|
$member_si = $member[$col_index_si];
|
||||||
|
|
||||||
|
// Find the correct age band (only once per member)
|
||||||
|
$found_band = false;
|
||||||
|
foreach ($age_band_data as $age_bands) {
|
||||||
|
if ($found_band) break;
|
||||||
|
|
||||||
|
foreach ($age_bands as $age_band) {
|
||||||
|
//get Min and Max Age
|
||||||
|
list($min_age, $max_age) = $this->getAgeRange($age_band);
|
||||||
|
|
||||||
|
// If age fits in this band
|
||||||
|
if ($age >= $min_age && $age <= $max_age) {
|
||||||
|
// Update counts for general category
|
||||||
|
if (isset($classifiers['general'][$relation][$age_band])) {
|
||||||
|
// Increment count for specific relation and age band
|
||||||
|
$classifiers['general'][$relation][$age_band]++;
|
||||||
|
// Update row total (Grand Total column)
|
||||||
|
$classifiers['general'][$relation]['Grand Total']++;
|
||||||
|
// Update column total (Grand Total row)
|
||||||
|
$classifiers['general']['Grand Total'][$age_band]++;
|
||||||
|
// Update grand total of grand totals
|
||||||
|
$classifiers['general']['Grand Total']['Grand Total']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update counts for specific SI category
|
||||||
|
if (isset($classifiers[$member_si][$relation][$age_band])) {
|
||||||
|
// Increment count for specific relation and age band
|
||||||
|
$classifiers[$member_si][$relation][$age_band]++;
|
||||||
|
// Update row total (Grand Total column)
|
||||||
|
$classifiers[$member_si][$relation]['Grand Total']++;
|
||||||
|
// Update column total (Grand Total row)
|
||||||
|
$classifiers[$member_si]['Grand Total'][$age_band]++;
|
||||||
|
// Update grand total of grand totals
|
||||||
|
$classifiers[$member_si]['Grand Total']['Grand Total']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$found_band = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $classifiers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateClassifierSpreadsheet($classifiers, $outputDir = 'exports')
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!file_exists($outputDir)) {
|
||||||
|
if (!mkdir($outputDir, 0755, true)) {
|
||||||
|
throw new Exception("Failed to create directory: $outputDir");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if directory is writable
|
||||||
|
if (!is_writable($outputDir)) {
|
||||||
|
throw new Exception("Directory is not writable: $outputDir");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate unique filename
|
||||||
|
$timestamp = date('Y-m-d_His');
|
||||||
|
$filename = "member_classification_{$timestamp}.xlsx";
|
||||||
|
$filepath = $outputDir . DIRECTORY_SEPARATOR . $filename;
|
||||||
|
|
||||||
|
// Check if file already exists (shouldn't happen with timestamp, but just in case)
|
||||||
|
if (file_exists($filepath)) {
|
||||||
|
$counter = 1;
|
||||||
|
while (file_exists($outputDir . DIRECTORY_SEPARATOR . "member_classification_{$timestamp}_{$counter}.xlsx")) {
|
||||||
|
$counter++;
|
||||||
|
}
|
||||||
|
$filename = "member_classification_{$timestamp}_{$counter}.xlsx";
|
||||||
|
$filepath = $outputDir . DIRECTORY_SEPARATOR . $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$spreadsheet = new Spreadsheet();
|
||||||
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
|
|
||||||
|
// Get all age bands
|
||||||
|
$age_bands = array_keys(reset($classifiers['general']));
|
||||||
|
array_pop($age_bands); // Remove 'Grand Total'
|
||||||
|
$age_bands[] = 'Grand Total'; // Add it back at the end
|
||||||
|
|
||||||
|
$currentRow = 5; // Start from row 5 to match the example
|
||||||
|
|
||||||
|
// Function to write section data
|
||||||
|
$writeSectionData = function ($data, $sheet, &$currentRow) use ($age_bands) {
|
||||||
|
// Set headers
|
||||||
|
$sheet->setCellValue('B' . $currentRow, 'Relationship');
|
||||||
|
$col = 'C';
|
||||||
|
foreach ($age_bands as $band) {
|
||||||
|
$sheet->setCellValue($col . $currentRow, $band);
|
||||||
|
$col++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style headers
|
||||||
|
$lastCol = chr(ord('B') + count($age_bands));
|
||||||
|
$headerRange = 'B' . $currentRow . ':' . $lastCol . $currentRow;
|
||||||
|
$sheet->getStyle($headerRange)->applyFromArray([
|
||||||
|
'font' => ['bold' => true],
|
||||||
|
'borders' => [
|
||||||
|
'allBorders' => [
|
||||||
|
'borderStyle' => Border::BORDER_THIN,
|
||||||
|
'color' => ['rgb' => '000000'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'alignment' => [
|
||||||
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$currentRow++;
|
||||||
|
|
||||||
|
// Write data rows
|
||||||
|
foreach ($data as $relation => $values) {
|
||||||
|
if ($relation !== 'Grand Total') {
|
||||||
|
$sheet->setCellValue('B' . $currentRow, $relation);
|
||||||
|
$col = 'C';
|
||||||
|
foreach ($age_bands as $band) {
|
||||||
|
$value = $values[$band] ?: ''; // Convert 0 to empty string
|
||||||
|
$sheet->setCellValue($col . $currentRow, $value);
|
||||||
|
$col++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style data row
|
||||||
|
$dataRange = 'B' . $currentRow . ':' . $lastCol . $currentRow;
|
||||||
|
$sheet->getStyle($dataRange)->applyFromArray([
|
||||||
|
'borders' => [
|
||||||
|
'allBorders' => [
|
||||||
|
'borderStyle' => Border::BORDER_THIN,
|
||||||
|
'color' => ['rgb' => '000000'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'alignment' => [
|
||||||
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$currentRow++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Grand Total row
|
||||||
|
$sheet->setCellValue('B' . $currentRow, 'Grand Total');
|
||||||
|
$col = 'C';
|
||||||
|
foreach ($age_bands as $band) {
|
||||||
|
$sheet->setCellValue($col . $currentRow, $data['Grand Total'][$band]);
|
||||||
|
$col++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style Grand Total row
|
||||||
|
$totalRange = 'B' . $currentRow . ':' . $lastCol . $currentRow;
|
||||||
|
$sheet->getStyle($totalRange)->applyFromArray([
|
||||||
|
'font' => ['bold' => true],
|
||||||
|
'borders' => [
|
||||||
|
'allBorders' => [
|
||||||
|
'borderStyle' => Border::BORDER_THIN,
|
||||||
|
'color' => ['rgb' => '000000'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'fill' => [
|
||||||
|
'fillType' => Fill::FILL_SOLID,
|
||||||
|
'startColor' => ['rgb' => 'F2F2F2'],
|
||||||
|
],
|
||||||
|
'alignment' => [
|
||||||
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$currentRow += 3; // Add gap after each section
|
||||||
|
};
|
||||||
|
|
||||||
|
// Write each SI section with gaps
|
||||||
|
foreach ($classifiers as $si_type => $data) {
|
||||||
|
$writeSectionData($data, $sheet, $currentRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-size columns
|
||||||
|
foreach (range('B', chr(ord('B') + count($age_bands))) as $col) {
|
||||||
|
$sheet->getColumnDimension($col)->setAutoSize(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Excel file
|
||||||
|
try {
|
||||||
|
// Create Excel file
|
||||||
|
$writer = new Xlsx($spreadsheet);
|
||||||
|
$writer->save($filepath);
|
||||||
|
|
||||||
|
// Verify file was created successfully
|
||||||
|
if (!file_exists($filepath)) {
|
||||||
|
throw new Exception("Failed to create file: $filepath");
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'filepath' => $filepath,
|
||||||
|
'filename' => $filename,
|
||||||
|
'fullpath' => realpath($filepath)
|
||||||
|
];
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'error' => $e->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAge($available_col, $member, $col_index)
|
||||||
|
{
|
||||||
|
if ($available_col == 'age') {
|
||||||
|
$age = $member[$col_index];
|
||||||
|
} else {
|
||||||
|
$dob = $member[$col_index];
|
||||||
|
if ($dob == 'DOB') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$dobDate = \DateTime::createFromFormat('d-M-Y', $dob);
|
||||||
|
$currentDate = new \DateTime();
|
||||||
|
if ($dobDate == false) {
|
||||||
|
$this->myLogger->logme('error', $dob . 'is not valid');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$age = $currentDate->diff($dobDate)->y;
|
||||||
|
}
|
||||||
|
return $age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAgeRange($age_band)
|
||||||
|
{
|
||||||
|
$age_band = trim($age_band);
|
||||||
|
// Parse age range
|
||||||
|
if (strpos($age_band, '-') === false) {
|
||||||
|
$min_age = (int)filter_var($age_band, FILTER_SANITIZE_NUMBER_INT);
|
||||||
|
$max_age = PHP_INT_MAX;
|
||||||
|
} else {
|
||||||
|
$parts = explode("-", $age_band);
|
||||||
|
$min_age = (int)$parts[0];
|
||||||
|
$max_age = (int)$parts[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return array($min_age, $max_age);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -752,7 +1085,7 @@ class LeadsController extends BaseController
|
|||||||
{
|
{
|
||||||
helper('excel_util_helper');
|
helper('excel_util_helper');
|
||||||
helper('MailHelper');
|
helper('MailHelper');
|
||||||
|
helper('ExcelMergeHelper');
|
||||||
$params = $this->request->getGet();
|
$params = $this->request->getGet();
|
||||||
// print_r($params); die;
|
// print_r($params); die;
|
||||||
$lead_id = $params['lead_id'];
|
$lead_id = $params['lead_id'];
|
||||||
@ -801,7 +1134,6 @@ class LeadsController extends BaseController
|
|||||||
if (empty($cc_mails)) {
|
if (empty($cc_mails)) {
|
||||||
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => '', 'message' => 'No valid CC mail addresses found!'], 200);
|
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => '', 'message' => 'No valid CC mail addresses found!'], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Handle case where param_cc_mail is not valid
|
// Handle case where param_cc_mail is not valid
|
||||||
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => '', 'message' => 'CC mail not found!'], 200);
|
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => '', 'message' => 'CC mail not found!'], 200);
|
||||||
@ -817,12 +1149,28 @@ class LeadsController extends BaseController
|
|||||||
|
|
||||||
//get file path to attach
|
//get file path to attach
|
||||||
$file_info = $this->constructExcelToSaveTemp($lead_id, ($file_type == 'rfq' ? 1 : 2), $propsal_and_insurer);
|
$file_info = $this->constructExcelToSaveTemp($lead_id, ($file_type == 'rfq' ? 1 : 2), $propsal_and_insurer);
|
||||||
|
if ($lead_data['file_name'] != null && $lead_data['file_name'] != '') {
|
||||||
|
$temp_file_path = $file_info['filePath'];
|
||||||
|
$temp_file_name = $file_info['fileName'];
|
||||||
|
$lead_file_path = WRITEPATH . 'uploads/lead_files/' . $lead_data['file_name'];
|
||||||
|
if ($lead_file_path) {
|
||||||
|
$filePaths = [
|
||||||
|
['file_path' => $temp_file_path, 'sheets' => []],
|
||||||
|
['file_path' => $lead_file_path, 'sheets' => []]
|
||||||
|
];
|
||||||
|
$outputPath = dirname($temp_file_path) . '/' . 'merged_' . $temp_file_name;
|
||||||
|
$result = ExcelMergeHelper::mergeExcelFiles($filePaths, $outputPath);
|
||||||
|
// print_rr($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// print_rr($lead_data);
|
||||||
|
// print_rr($file_info);
|
||||||
// $file_path = WRITEPATH."uploads/excel/sample/correction.xls";
|
// $file_path = WRITEPATH."uploads/excel/sample/correction.xls";
|
||||||
// $file_name = $file_type.'.xlsx';
|
// $file_name = $file_type.'.xlsx';
|
||||||
// !dd($file_info);
|
// !dd($file_info);
|
||||||
|
|
||||||
$file_path = $file_info['filePath'];
|
$file_path = $result;
|
||||||
$file_name = $file_info['fileName'];
|
$file_name = basename($result);
|
||||||
$attachments = [['fileName' => $file_name, 'filePath' => $file_path]];
|
$attachments = [['fileName' => $file_name, 'filePath' => $file_path]];
|
||||||
|
|
||||||
//get recipient address
|
//get recipient address
|
||||||
@ -898,7 +1246,6 @@ class LeadsController extends BaseController
|
|||||||
} else {
|
} else {
|
||||||
return $this->respond(['status' => false], 200);
|
return $this->respond(['status' => false], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInsurerBranchContacts($insurer_and_branch_id)
|
public function getInsurerBranchContacts($insurer_and_branch_id)
|
||||||
@ -919,7 +1266,8 @@ class LeadsController extends BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformProposelData($data, $proposel, $insurer){
|
function transformProposelData($data, $proposel, $insurer)
|
||||||
|
{
|
||||||
|
|
||||||
// print_r($data['premium_data']['data']); die;
|
// print_r($data['premium_data']['data']); die;
|
||||||
|
|
||||||
@ -1024,7 +1372,6 @@ class LeadsController extends BaseController
|
|||||||
$data['premium_data']['data'] = $premiumData;
|
$data['premium_data']['data'] = $premiumData;
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertJsonForQCR($json, $type)
|
function convertJsonForQCR($json, $type)
|
||||||
@ -1268,29 +1615,82 @@ class LeadsController extends BaseController
|
|||||||
private function convertQCRJsonToPolicyTerms($data, $policy_type, $proposel_name, $insurer_name)
|
private function convertQCRJsonToPolicyTerms($data, $policy_type, $proposel_name, $insurer_name)
|
||||||
{
|
{
|
||||||
$GMC_Keys = [
|
$GMC_Keys = [
|
||||||
"sum_insured", "family_floater", "family_floaters", "age_ratio", "waiverofpreexistingdiseases",
|
"sum_insured",
|
||||||
"maternitycoverage", "twindelivery", "preandpostnatal", "babyday1cover", "9monthwaitingperiodwaived",
|
"family_floater",
|
||||||
"coverfromthedateofjoining", "waiverof1,2,3&4thyearexclusions", "waiverof30dayswaitingperiod",
|
"family_floaters",
|
||||||
"prehospitalizationcover", "congenitaldiseasesinternal", "copayzonewisecopay",
|
"age_ratio",
|
||||||
"bioabsorbablestenttoriclensmultifocallens", "roomrentlimit", "proportionatedeductionclause",
|
"waiverofpreexistingdiseases",
|
||||||
"ailmentcapping", "ambulancecharges", "airambulance", "familytransportationbenefit",
|
"maternitycoverage",
|
||||||
"reasonableandcustomarycharges", "ayudhtreatmentcover", "congenitaldiseasesexternal",
|
"twindelivery",
|
||||||
"optionalparentalcopay", "posthospitalizationcover", "corporatebuffer", "sublimitofcorporatebuffer",
|
"preandpostnatal",
|
||||||
"ayushTreatmentCoverData", "armdcovered", "suminsuredenhancement", "automaticsuminsuredreinstatement",
|
"babyday1cover",
|
||||||
"additionalsicknessbenefit", "lasiksurgery", "midterminclusion", "capd", "organdonorexpenses",
|
"9monthwaitingperiodwaived",
|
||||||
"moderntreatmentsasperirdai", "Wellness", "days_of_discharge", "days_from_dod",
|
"coverfromthedateofjoining",
|
||||||
"special_condition_label", "special_condition_input", "multiple_sum_insured",
|
"waiverof1,2,3&4thyearexclusions",
|
||||||
"cataract", "cataractData"
|
"waiverof30dayswaitingperiod",
|
||||||
|
"prehospitalizationcover",
|
||||||
|
"congenitaldiseasesinternal",
|
||||||
|
"copayzonewisecopay",
|
||||||
|
"bioabsorbablestenttoriclensmultifocallens",
|
||||||
|
"roomrentlimit",
|
||||||
|
"proportionatedeductionclause",
|
||||||
|
"ailmentcapping",
|
||||||
|
"ambulancecharges",
|
||||||
|
"airambulance",
|
||||||
|
"familytransportationbenefit",
|
||||||
|
"reasonableandcustomarycharges",
|
||||||
|
"ayudhtreatmentcover",
|
||||||
|
"congenitaldiseasesexternal",
|
||||||
|
"optionalparentalcopay",
|
||||||
|
"posthospitalizationcover",
|
||||||
|
"corporatebuffer",
|
||||||
|
"sublimitofcorporatebuffer",
|
||||||
|
"ayushTreatmentCoverData",
|
||||||
|
"armdcovered",
|
||||||
|
"suminsuredenhancement",
|
||||||
|
"automaticsuminsuredreinstatement",
|
||||||
|
"additionalsicknessbenefit",
|
||||||
|
"lasiksurgery",
|
||||||
|
"midterminclusion",
|
||||||
|
"capd",
|
||||||
|
"organdonorexpenses",
|
||||||
|
"moderntreatmentsasperirdai",
|
||||||
|
"Wellness",
|
||||||
|
"days_of_discharge",
|
||||||
|
"days_from_dod",
|
||||||
|
"special_condition_label",
|
||||||
|
"special_condition_input",
|
||||||
|
"multiple_sum_insured",
|
||||||
|
"cataract",
|
||||||
|
"cataractData"
|
||||||
];
|
];
|
||||||
|
|
||||||
$GPA_Keys = [
|
$GPA_Keys = [
|
||||||
"sumInsured2", "totalSumInsured", "age_ratio", "accidentalDeathBenefit", "permanentTotalDisablement",
|
"sumInsured2",
|
||||||
"permanentPartialDisablement", "temporaryTotalDisablementBenefit", "accidentalHospitalizationExpenses",
|
"totalSumInsured",
|
||||||
"childrenEducationWelfareFund", "compassionateVisitExpenses", "compassionateVisitExpensesData",
|
"age_ratio",
|
||||||
"brokenBoneExpenses", "brokenBoneExpensesData", "ambulanceCharges", "ambulanceChargesData",
|
"accidentalDeathBenefit",
|
||||||
"burnExpenses", "burnExpensesData", "carriageOfDeadBody", "carriageOfDeadBodyData",
|
"permanentTotalDisablement",
|
||||||
"animalSnakeInsectBite", "terrorism", "worldwideCover", "gpa_special_condition_label",
|
"permanentPartialDisablement",
|
||||||
"gpa_special_condition_input", "multiple_sum_insured"
|
"temporaryTotalDisablementBenefit",
|
||||||
|
"accidentalHospitalizationExpenses",
|
||||||
|
"childrenEducationWelfareFund",
|
||||||
|
"compassionateVisitExpenses",
|
||||||
|
"compassionateVisitExpensesData",
|
||||||
|
"brokenBoneExpenses",
|
||||||
|
"brokenBoneExpensesData",
|
||||||
|
"ambulanceCharges",
|
||||||
|
"ambulanceChargesData",
|
||||||
|
"burnExpenses",
|
||||||
|
"burnExpensesData",
|
||||||
|
"carriageOfDeadBody",
|
||||||
|
"carriageOfDeadBodyData",
|
||||||
|
"animalSnakeInsectBite",
|
||||||
|
"terrorism",
|
||||||
|
"worldwideCover",
|
||||||
|
"gpa_special_condition_label",
|
||||||
|
"gpa_special_condition_input",
|
||||||
|
"multiple_sum_insured"
|
||||||
];
|
];
|
||||||
|
|
||||||
// Select keys based on policy type
|
// Select keys based on policy type
|
||||||
@ -1322,8 +1722,10 @@ class LeadsController extends BaseController
|
|||||||
$value = $cellData['value'] ?? '';
|
$value = $cellData['value'] ?? '';
|
||||||
|
|
||||||
// Skip unwanted keys
|
// Skip unwanted keys
|
||||||
if (in_array($parentth, ['Sno', 'Item Key', 'Particulars', 'Action']) ||
|
if (
|
||||||
in_array($subth, ['Quote Asked'])) {
|
in_array($parentth, ['Sno', 'Item Key', 'Particulars', 'Action']) ||
|
||||||
|
in_array($subth, ['Quote Asked'])
|
||||||
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1383,7 +1785,5 @@ class LeadsController extends BaseController
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $this->respond(['status' => false, 'message' => 'Failed to create policy', 'data' => $data], 200);
|
return $this->respond(['status' => false, 'message' => 'Failed to create policy', 'data' => $data], 200);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -4,90 +4,215 @@ namespace App\Helpers;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
class ExcelMergeHelper
|
class ExcelMergeHelper
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Merge multiple Excel files into a single spreadsheet.
|
protected static $logger;
|
||||||
*
|
|
||||||
* @param array $filesToMerge Array of files to merge.
|
public static function initialize()
|
||||||
* @param string $outputPath Path to save the merged file.
|
{
|
||||||
* @return bool True if merge successful, false otherwise.
|
self::$logger = \Config\Services::mylogger();
|
||||||
*/
|
}
|
||||||
public static function mergeExcelFiles(array $filesToMerge, string $outputPath): bool
|
public static function mergeExcelFiles(array $filesToMerge, string $outputPath)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Validate input: If no files are provided, throw an error.
|
self::initialize();
|
||||||
if (empty($filesToMerge)) {
|
if (empty($filesToMerge)) {
|
||||||
throw new Exception("No files provided for merging");
|
throw new Exception("No files provided for merging.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the base spreadsheet (starting with null, will be created with the first file)
|
// Validate files exist and are readable
|
||||||
$spreadsheet = null;
|
foreach ($filesToMerge as $fileData) {
|
||||||
|
if (!file_exists($fileData['file_path'])) {
|
||||||
// Loop through the files to merge
|
throw new Exception("File not found: " . $fileData['file_path']);
|
||||||
foreach ($filesToMerge as $filePath => $sheetsToMerge) {
|
}
|
||||||
// Load the current file
|
if (!is_readable($fileData['file_path'])) {
|
||||||
$currentSpreadsheet = IOFactory::load($filePath);
|
throw new Exception("File not readable: " . $fileData['file_path']);
|
||||||
|
|
||||||
// Debugging: Log the number of sheets in the current file
|
|
||||||
error_log("File: $filePath has " . $currentSpreadsheet->getSheetCount() . " sheets.");
|
|
||||||
|
|
||||||
// If this is the first file, initialize the base spreadsheet
|
|
||||||
if ($spreadsheet === null) {
|
|
||||||
$spreadsheet = $currentSpreadsheet;
|
|
||||||
|
|
||||||
// If specific sheets are provided for the first file, remove the others
|
|
||||||
if (!is_null($sheetsToMerge) && !empty($sheetsToMerge)) {
|
|
||||||
$sheetCount = $spreadsheet->getSheetCount();
|
|
||||||
for ($i = $sheetCount - 1; $i >= 0; $i--) {
|
|
||||||
if (!in_array($i, $sheetsToMerge)) {
|
|
||||||
$spreadsheet->removeSheetByIndex($i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
self::$logger->logme('error',"Creating new spreadsheet...");
|
||||||
// If we are not working with the first file, we need to merge specified sheets
|
$mergedSpreadsheet = new Spreadsheet();
|
||||||
if (is_null($sheetsToMerge) || empty($sheetsToMerge)) {
|
$firstSheet = true;
|
||||||
// If no specific sheets, merge all sheets
|
|
||||||
$sheetsToMerge = range(0, $currentSpreadsheet->getSheetCount() - 1);
|
foreach ($filesToMerge as $fileIndex => $fileData) {
|
||||||
|
$filePath = $fileData['file_path'];
|
||||||
|
$sheetsToMerge = $fileData['sheets'];
|
||||||
|
self::$logger->logme('error',"Processing file $fileIndex: $filePath");
|
||||||
|
|
||||||
|
try {
|
||||||
|
self::$logger->logme('error',"Attempting to load file: $filePath");
|
||||||
|
$reader = IOFactory::createReaderForFile($filePath);
|
||||||
|
$reader->setReadDataOnly(true); // This can help with memory usage
|
||||||
|
self::$logger->logme('error',"Reader created successfully");
|
||||||
|
|
||||||
|
$currentSpreadsheet = $reader->load($filePath);
|
||||||
|
self::$logger->logme('error',"File loaded successfully");
|
||||||
|
|
||||||
|
$sheetCount = $currentSpreadsheet->getSheetCount();
|
||||||
|
self::$logger->logme('error',"File $filePath has $sheetCount sheets.");
|
||||||
|
|
||||||
|
if (empty($sheetsToMerge)) {
|
||||||
|
$sheetsToMerge = range(0, $sheetCount - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge the specified sheets from the current file
|
foreach ($sheetsToMerge as $sourceSheetIndex) {
|
||||||
foreach ($sheetsToMerge as $sheetIndex) {
|
self::$logger->logme('error',"Processing sheet index: $sourceSheetIndex");
|
||||||
// Check if sheet index is within bounds
|
|
||||||
if ($sheetIndex < 0 || $sheetIndex >= $currentSpreadsheet->getSheetCount()) {
|
if ($sourceSheetIndex < 0 || $sourceSheetIndex >= $sheetCount) {
|
||||||
throw new Exception("Invalid sheet index $sheetIndex in file $filePath");
|
self::$logger->logme('error',"Invalid sheet index $sourceSheetIndex in file $filePath");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the sheet to copy
|
try {
|
||||||
$sheetToCopy = $currentSpreadsheet->getSheet($sheetIndex);
|
$sourceSheet = $currentSpreadsheet->getSheet($sourceSheetIndex);
|
||||||
|
self::$logger->logme('error',"Source sheet loaded");
|
||||||
|
|
||||||
// Handle sheet name collision by renaming the sheet with a counter
|
$baseName = $sourceSheet->getTitle();
|
||||||
$baseName = $sheetToCopy->getTitle();
|
self::$logger->logme('error',"Processing sheet: $baseName");
|
||||||
$sheetName = $baseName;
|
|
||||||
|
$newSheetName = $baseName;
|
||||||
$counter = 1;
|
$counter = 1;
|
||||||
while ($spreadsheet->sheetNameExists($sheetName)) {
|
|
||||||
$sheetName = $baseName . '_merged_' . $counter;
|
while ($mergedSpreadsheet->sheetNameExists($newSheetName)) {
|
||||||
|
$newSheetName = $baseName . '_' . $fileIndex . '_' . $counter;
|
||||||
$counter++;
|
$counter++;
|
||||||
}
|
}
|
||||||
$sheetToCopy->setTitle($sheetName);
|
self::$logger->logme('error',"New sheet name will be: $newSheetName");
|
||||||
|
|
||||||
// Clone and add the sheet to the base spreadsheet
|
if ($firstSheet) {
|
||||||
$spreadsheet->addSheet(clone $sheetToCopy);
|
$targetSheet = $mergedSpreadsheet->getActiveSheet();
|
||||||
|
$targetSheet->setTitle($newSheetName);
|
||||||
|
$firstSheet = false;
|
||||||
|
self::$logger->logme('error',"Using first sheet");
|
||||||
|
} else {
|
||||||
|
$targetSheet = new Worksheet($mergedSpreadsheet, $newSheetName);
|
||||||
|
$mergedSpreadsheet->addSheet($targetSheet);
|
||||||
|
self::$logger->logme('error',"Created new sheet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Starting content copy for sheet: $newSheetName");
|
||||||
|
self::copyWorksheetContent($sourceSheet, $targetSheet);
|
||||||
|
self::$logger->logme('error',"Content copied successfully for sheet: $newSheetName");
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
self::$logger->logme('error',"Error processing sheet $sourceSheetIndex: " . $e->getMessage());
|
||||||
|
self::$logger->logme('error',"Error trace: " . $e->getTraceAsString());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the merged file
|
// Free up memory
|
||||||
$writer = new Xlsx($spreadsheet);
|
$currentSpreadsheet->disconnectWorksheets();
|
||||||
|
unset($currentSpreadsheet);
|
||||||
|
self::$logger->logme('error',"Cleaned up spreadsheet resources");
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
self::$logger->logme('error',"Error processing file $filePath: " . $e->getMessage());
|
||||||
|
self::$logger->logme('error',"Error trace: " . $e->getTraceAsString());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Current sheet names in merged spreadsheet:");
|
||||||
|
foreach ($mergedSpreadsheet->getSheetNames() as $name) {
|
||||||
|
self::$logger->logme('error'," - $name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($mergedSpreadsheet->getSheetCount() === 0) {
|
||||||
|
throw new Exception("No sheets were successfully merged.");
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Setting active sheet to index 0");
|
||||||
|
$mergedSpreadsheet->setActiveSheetIndex(0);
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Preparing to save merged file to: $outputPath");
|
||||||
|
|
||||||
|
// Ensure output directory exists
|
||||||
|
$outputDir = dirname($outputPath);
|
||||||
|
if (!is_dir($outputDir)) {
|
||||||
|
mkdir($outputDir, 0777, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_writable($outputDir)) {
|
||||||
|
throw new Exception("Output directory is not writable: $outputDir");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ob_get_length()) {
|
||||||
|
ob_end_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Creating Excel writer");
|
||||||
|
$writer = new Xlsx($mergedSpreadsheet);
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Saving file...");
|
||||||
$writer->save($outputPath);
|
$writer->save($outputPath);
|
||||||
|
|
||||||
return true; // Return true if merge was successful
|
// Free up memory
|
||||||
|
$mergedSpreadsheet->disconnectWorksheets();
|
||||||
|
unset($mergedSpreadsheet);
|
||||||
|
|
||||||
|
self::$logger->logme('error',"Merge successful. File saved at: $outputPath");
|
||||||
|
return $outputPath;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// Log the error (optional) and return false on failure
|
self::$logger->logme('error',"Excel Merge Error: " . $e->getMessage());
|
||||||
error_log("Excel Merge Error: " . $e->getMessage());
|
self::$logger->logme('error',"Error trace: " . $e->getTraceAsString());
|
||||||
return false;
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function copyWorksheetContent($sourceSheet, $targetSheet)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
self::$logger->logme('error',"Starting copyWorksheetContent");
|
||||||
|
|
||||||
|
// Get the highest row and column indexes
|
||||||
|
$highestRow = $sourceSheet->getHighestRow();
|
||||||
|
$highestColumn = $sourceSheet->getHighestColumn();
|
||||||
|
self::$logger->logme('error',"Copying data from range A1:{$highestColumn}{$highestRow}");
|
||||||
|
|
||||||
|
// Copy cell values only first for better performance
|
||||||
|
foreach ($sourceSheet->getRowIterator() as $row) {
|
||||||
|
$cellIterator = $row->getCellIterator();
|
||||||
|
$cellIterator->setIterateOnlyExistingCells(true);
|
||||||
|
foreach ($cellIterator as $cell) {
|
||||||
|
$column = $cell->getColumn();
|
||||||
|
$rowIndex = $cell->getRow();
|
||||||
|
$targetSheet->setCellValue($column . $rowIndex, $cell->getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self::$logger->logme('error',"Basic cell values copied");
|
||||||
|
|
||||||
|
// Copy column dimensions
|
||||||
|
foreach ($sourceSheet->getColumnDimensions() as $columnId => $sourceDim) {
|
||||||
|
$targetSheet->getColumnDimension($columnId)
|
||||||
|
->setWidth($sourceDim->getWidth())
|
||||||
|
->setVisible($sourceDim->getVisible())
|
||||||
|
->setAutoSize($sourceDim->getAutoSize());
|
||||||
|
}
|
||||||
|
self::$logger->logme('error',"Column dimensions copied");
|
||||||
|
|
||||||
|
// Copy row dimensions
|
||||||
|
foreach ($sourceSheet->getRowDimensions() as $rowId => $sourceDim) {
|
||||||
|
$targetSheet->getRowDimension($rowId)
|
||||||
|
->setRowHeight($sourceDim->getRowHeight())
|
||||||
|
->setVisible($sourceDim->getVisible());
|
||||||
|
}
|
||||||
|
self::$logger->logme('error',"Row dimensions copied");
|
||||||
|
|
||||||
|
// Copy basic worksheet properties
|
||||||
|
$targetSheet->setShowGridlines($sourceSheet->getShowGridlines());
|
||||||
|
$targetSheet->setShowRowColHeaders($sourceSheet->getShowRowColHeaders());
|
||||||
|
self::$logger->logme('error',"Worksheet properties copied");
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
self::$logger->logme('error',"Error in copyWorksheetContent: " . $e->getMessage());
|
||||||
|
self::$logger->logme('error',"Error trace: " . $e->getTraceAsString());
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -10,8 +10,11 @@ class ExcelMergeHelperTest extends TestCase
|
|||||||
{
|
{
|
||||||
// Use the paths you provided
|
// Use the paths you provided
|
||||||
$filePaths = [
|
$filePaths = [
|
||||||
'/home/venba/Downloads/Financial_Sample.xlsx' => [1,3,2],
|
// ['file_path' =>WRITEPATH.'uploads/lead_files/Member_Data.xlsx','sheets' => []],
|
||||||
'/home/venba/Downloads/enrollment.xlsx' => []
|
// ['file_path' =>WRITEPATH.'/tmp/RFQ_ABC_GMC_20241220140640.xlsx','sheets' => []],
|
||||||
|
|
||||||
|
['file_path' =>'/home/venba/Downloads/enrollment (1).xlsx','sheets' => []],
|
||||||
|
['file_path' =>'/home/venba/Downloads/Financial_Sample.xlsx','sheets' => []],
|
||||||
];
|
];
|
||||||
$outputPath = '/home/venba/Documents/merged_file.xlsx'; // Output path for the merged file
|
$outputPath = '/home/venba/Documents/merged_file.xlsx'; // Output path for the merged file
|
||||||
|
|
||||||
@ -23,3 +26,12 @@ class ExcelMergeHelperTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// $temp_file_path = WRITEPATH.'tmp/RFQ_ABC_GMC_20241219172307.xlsx';
|
||||||
|
// $temp_file_name = 'RFQ_ABC_GMC_20241219172307.xlsx';
|
||||||
|
// $lead_file_path = WRITEPATH.'uploads/lead_files/Member_Data.xlsx';
|
||||||
|
|
||||||
|
// $filePaths = [
|
||||||
|
// ['file_path' => $temp_file_path,'sheets' =>[]],
|
||||||
|
// ['file_path' =>$lead_file_path ,'sheets' =>[]]
|
||||||
|
// ];
|
||||||
|
// $outputPath = dirname($temp_file_path).'/'.$temp_file_name.'_merged';
|
||||||
|
|||||||
183
tests/unit/LeadsControllerTest.php
Normal file
183
tests/unit/LeadsControllerTest.php
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\unit;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use CodeIgniter\Test\CIUnitTestCase;
|
||||||
|
use App\Controllers\LeadsController;
|
||||||
|
|
||||||
|
class LeadsControllerTest extends CIUnitTestCase
|
||||||
|
{
|
||||||
|
protected $leadsController;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->leadsController = new LeadsController();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDemographyDataBasicScenario()
|
||||||
|
{
|
||||||
|
echo('passed 0');
|
||||||
|
// Arrange
|
||||||
|
$members = [
|
||||||
|
// Header row
|
||||||
|
['Name', 'Age', 'Relationship', 'SI Enhancement'],
|
||||||
|
// Data rows
|
||||||
|
['John', '25', 'Self', '100000'],
|
||||||
|
['Jane', '23', 'Spouse', '100000'],
|
||||||
|
['Kid', '5', 'Child', '50000']
|
||||||
|
];
|
||||||
|
echo('passed 1');
|
||||||
|
$age_band_data = [
|
||||||
|
['0-17', '18-35', '36-45', 'Above 46']
|
||||||
|
];
|
||||||
|
echo('passed 2');
|
||||||
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
||||||
|
$available_col = 'age';print_rr($available_col);
|
||||||
|
$col_index = 1;
|
||||||
|
echo('passed 3');
|
||||||
|
try{
|
||||||
|
echo('Inside try');
|
||||||
|
// Act
|
||||||
|
echo('passed 3.5');
|
||||||
|
$result = $this->leadsController->getDemographyData($members,$age_band_data,$members_heading,$available_col,$col_index);
|
||||||
|
|
||||||
|
echo('passed 4');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "Message: " . $e->getMessage() . "\n";
|
||||||
|
echo "File: " . $e->getFile() . "\n";
|
||||||
|
echo "Line: " . $e->getLine() . "\n";
|
||||||
|
// Optionally, print the full stack trace
|
||||||
|
echo "Stack trace: " . $e->getTraceAsString() . "\n";
|
||||||
|
}
|
||||||
|
var_dump($result);
|
||||||
|
// Assert
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertArrayHasKey('general', $result);
|
||||||
|
$this->assertArrayHasKey('100000', $result);
|
||||||
|
$this->assertArrayHasKey('50000', $result);
|
||||||
|
|
||||||
|
// Check counts for general category
|
||||||
|
$this->assertEquals(1, $result['general']['Self']['18-35']);
|
||||||
|
$this->assertEquals(1, $result['general']['Spouse']['18-35']);
|
||||||
|
$this->assertEquals(1, $result['general']['Child']['0-17']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDemographyDataWithDOB()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$members = [
|
||||||
|
// Header row
|
||||||
|
['Name', 'DOB', 'Relationship', 'SI Enhancement'],
|
||||||
|
// Data rows
|
||||||
|
['John', '01-Jan-1995', 'Self', '100000'],
|
||||||
|
['Jane', '01-Jan-1998', 'Spouse', '100000']
|
||||||
|
];
|
||||||
|
|
||||||
|
$age_band_data = [
|
||||||
|
['18-35', '36-45', '46+']
|
||||||
|
];
|
||||||
|
|
||||||
|
$members_heading = ['Name', 'DOB', 'Relationship', 'SI Enhancement'];
|
||||||
|
$available_col = 'dob';
|
||||||
|
$col_index = 1;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$result = $this->leadsController->getDemographyData(
|
||||||
|
$members,
|
||||||
|
$age_band_data,
|
||||||
|
$members_heading,
|
||||||
|
$available_col,
|
||||||
|
$col_index
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertArrayHasKey('general', $result);
|
||||||
|
$this->assertArrayHasKey('100000', $result);
|
||||||
|
|
||||||
|
// Both members should be in 18-35 age band
|
||||||
|
$this->assertEquals(1, $result['general']['Self']['18-35']);
|
||||||
|
$this->assertEquals(1, $result['general']['Spouse']['18-35']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDemographyDataWithEmptyMembers()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$members = [
|
||||||
|
// Only header row
|
||||||
|
['Name', 'Age', 'Relationship', 'SI Enhancement']
|
||||||
|
];
|
||||||
|
|
||||||
|
$age_band_data = [
|
||||||
|
['0-17', '18-35', '36-45', '46+']
|
||||||
|
];
|
||||||
|
|
||||||
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
||||||
|
$available_col = 'age';
|
||||||
|
$col_index = 1;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$result = $this->leadsController->getDemographyData(
|
||||||
|
$members,
|
||||||
|
$age_band_data,
|
||||||
|
$members_heading,
|
||||||
|
$available_col,
|
||||||
|
$col_index
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertArrayHasKey('general', $result);
|
||||||
|
|
||||||
|
// Check that all totals are zero
|
||||||
|
$this->assertEquals(0, $result['general']['Grand Total']['Grand Total']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDemographyDataWithMultipleSIBands()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$members = [
|
||||||
|
// Header row
|
||||||
|
['Name', 'Age', 'Relationship', 'SI Enhancement'],
|
||||||
|
// Data rows with different SI amounts
|
||||||
|
['John', '25', 'Self', '100000'],
|
||||||
|
['Jane', '23', 'Spouse', '200000'],
|
||||||
|
['Kid1', '5', 'Child', '100000'],
|
||||||
|
['Kid2', '7', 'Child', '200000']
|
||||||
|
];
|
||||||
|
|
||||||
|
$age_band_data = [
|
||||||
|
['0-17', '18-35', '36-45', '46+']
|
||||||
|
];
|
||||||
|
|
||||||
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
||||||
|
$available_col = 'age';
|
||||||
|
$col_index = 1;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$result = $this->leadsController->getDemographyData(
|
||||||
|
$members,
|
||||||
|
$age_band_data,
|
||||||
|
$members_heading,
|
||||||
|
$available_col,
|
||||||
|
$col_index
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertArrayHasKey('100000', $result);
|
||||||
|
$this->assertArrayHasKey('200000', $result);
|
||||||
|
|
||||||
|
// Check counts for different SI bands
|
||||||
|
$this->assertEquals(1, $result['100000']['Self']['18-35']);
|
||||||
|
$this->assertEquals(1, $result['200000']['Spouse']['18-35']);
|
||||||
|
$this->assertEquals(1, $result['100000']['Child']['0-17']);
|
||||||
|
$this->assertEquals(1, $result['200000']['Child']['0-17']);
|
||||||
|
|
||||||
|
// Check general category totals
|
||||||
|
$this->assertEquals(2, $result['general']['Child']['0-17']);
|
||||||
|
$this->assertEquals(2, $result['general']['Child']['Grand Total']);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user