diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 4ca4505d..063e4b58 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -4714,7 +4714,27 @@ class ClientController extends AdminController // $LeadsController = new LeadsController(); - // $LeadsController->constructNonEbExcelToSaveTemp(92, 2, "Proposal 2-ICICIPRU-ICICI001"); + // $path = $LeadsController->constructNonEbExcelToSaveTemp(106, 2, "Proposal 2-ICICIPRU-ICICI001"); + // $filepath = $path['filePath']; + + // if (file_exists($filepath)) { + // // Set headers to force download + // header('Content-Description: File Transfer'); + // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + // header('Content-Disposition: attachment; filename="' . basename($filepath) . '"'); + // header('Content-Length: ' . filesize($filepath)); + // header('Pragma: public'); + + // // Output the file content + // readfile($filepath); + + // // Delete the file after download + // unlink($filepath); + + // exit; + // } else { + // echo "File does not exist."; + // } } // ------------------------------------------------------------------------------------------------------- diff --git a/app/Controllers/LeadsController.php b/app/Controllers/LeadsController.php index 8d926761..28a7725f 100644 --- a/app/Controllers/LeadsController.php +++ b/app/Controllers/LeadsController.php @@ -2652,6 +2652,7 @@ class LeadsController extends BaseController // Initialize max width tracking variables $maxWidthB = 0; + //Lead Data foreach ($lead_data as $key => $value) { $formattedKey = ucwords(str_replace('_', ' ', $key)); @@ -2692,9 +2693,113 @@ class LeadsController extends BaseController $sheet->getColumnDimension('C')->setWidth($maxWidthB * 1.2); $rowNumber += 2; + //Policy Registration + if (!empty($policy_registration_data)) { + foreach ($policy_registration_data as $key => $value) { - // Kint::dump($jsonData); + $startRow = $rowNumber; // Track where the block starts + // Title Row + $titleMergeRangeKey = "A{$rowNumber}:C{$rowNumber}"; + $sheet->mergeCells($titleMergeRangeKey); + $sheet->setCellValue("A{$rowNumber}", ucfirst($key) . " Policy Details"); + + // Title Style + $sheet->getStyle($titleMergeRangeKey)->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => 'ADD8E6'], + ], + ]); + + $rowNumber++; // Move to next row + + // Policy Type + if (isset($value['policyType'])) { + $sheet->mergeCells("A{$rowNumber}:B{$rowNumber}"); + $sheet->setCellValue("A{$rowNumber}", "Policy Type"); + $sheet->setCellValue("C{$rowNumber}", $value['policyType']); + + $sheet->getStyle("A{$rowNumber}:C{$rowNumber}")->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + ]); + + $rowNumber++; + }else{ + $sheet->mergeCells("A{$rowNumber}:B{$rowNumber}"); + $sheet->setCellValue("A{$rowNumber}", "Policy Type"); + $sheet->setCellValue("C{$rowNumber}", ucfirst($key) . " Policy"); + + $sheet->getStyle("A{$rowNumber}:C{$rowNumber}")->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + ]); + + $rowNumber++; + } + + // Type of Business & Nature of Business + if (isset($value['productSelection']) && !empty($value['productSelection'])) { + // Type of Business + $sheet->mergeCells("A{$rowNumber}:B{$rowNumber}"); + $sheet->setCellValue("A{$rowNumber}", "Type of Business"); + $sheet->setCellValue("C{$rowNumber}", $this->buisnessType[$value['productSelection']['type_of_buisness']] ?? ''); + + $sheet->getStyle("A{$rowNumber}")->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + ]); + + $rowNumber++; + + // Nature of Business + $sheet->mergeCells("A{$rowNumber}:B{$rowNumber}"); + $sheet->setCellValue("A{$rowNumber}", "Nature of Business"); + $sheet->setCellValue("C{$rowNumber}", $value['productSelection']['buisness_nature'] ?? ''); + + $sheet->getStyle("A{$rowNumber}")->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, + 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, + ], + ]); + + $rowNumber++; + } + + $endRow = $rowNumber - 1; // Block ends at previous row + + // Apply border to the whole block + $sheet->getStyle("A{$startRow}:C{$endRow}")->applyFromArray([ + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, + 'color' => ['argb' => '000000'], + ], + ], + ]); + + $rowNumber += 2; // Add space before the next block + } + } + + //Table Data foreach ($jsonData as $key => $data) { // Kint::dump($data, 'First'); @@ -2812,13 +2917,17 @@ class LeadsController extends BaseController $rowNumber = $subHeaderRow + 2; $column_data = $data['table_data']['data']; + // dd($column_data); $serial_no = 1; $maxColumnWidths = []; - + $RowSpanEnable = false; + // Add table data rows foreach ($column_data as $dataRow) { $columnLetter = 'A'; + $hasPolicy = in_array('Policy', array_column($dataRow['data'], 'parentth')); + // Kint::dump($hasPolicy); foreach ($dataRow['data'] as $cellData) { @@ -2837,15 +2946,30 @@ class LeadsController extends BaseController $sheet->getStyle("{$columnLetter}{$mergeStart}:{$columnLetter}{$mergeEnd}") ->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } else { - $sheet->setCellValue("{$columnLetter}{$rowNumber}", $cellData['display_content']); + if(!$hasPolicy && $key == 0){ + $RowSpanEnable = true; + $columnLetter++; + $sheet->setCellValue("{$columnLetter}{$rowNumber}", $cellData['display_content']); + }else{ + $sheet->setCellValue("{$columnLetter}{$rowNumber}", $cellData['display_content']); + } } - $columnLetter++; + if (!($key === 0 && !$hasPolicy)) { + $columnLetter++; + } + } $rowNumber++; $serial_no++; } + if($key == 0 && $RowSpanEnable == true){ + $columnLetter++; + } + + // dd($columnLetter); + $dataRange = "A" . ($subHeaderRow + 1) . ":" . chr(ord($columnLetter) - 1) . ($rowNumber - 1); $sheet->getStyle($dataRange)->applyFromArray([ 'borders' => [ @@ -2991,8 +3115,12 @@ class LeadsController extends BaseController } // Row-wise Check: Remove rows if qcr == 0 for actions + $groupedRows = []; foreach ($first_json['table_data']['data'] as $rowKey => $rowData) { - foreach ($rowData['data'] as $data) { + + $groupedRows[$rowData['row_id']][] = $rowData; + + foreach ($rowData['data'] as $keyIndex => $data) { if ($indexOfTheTable == 0) { @@ -3013,6 +3141,42 @@ class LeadsController extends BaseController } } + $finalRows = []; + + if (!empty($groupedRows)) { + + // Loop through each row_id group and check for Policy status + foreach ($groupedRows as $rowId => $rows) { + $hasPolicy = false; + $isChecked = false; + + foreach ($rows as $row) { + foreach ($row['data'] as $cell) { + if ($cell['parentth'] === 'Policy') { + $hasPolicy = true; + if (trim(strtolower($cell['input_value'])) === 'checked') { + $isChecked = true; + } + break 2; // Found Policy, no need to continue further + } + } + } + + // Step 3: If parent Policy is "Checked", retain the entire group + if (!$hasPolicy || $isChecked) { + foreach ($rows as $r) { + $finalRows[] = $r; + } + } + } + + if (!empty($finalRows)) { + // Update the original data + $first_json['table_data']['data'] = array_values($finalRows); + } + } + + // Reindex arrays to maintain proper structure $first_json['table_data']['headers'] = array_values($first_json['table_data']['headers']); $first_json['table_data']['data'] = array_values($first_json['table_data']['data']); @@ -3033,6 +3197,7 @@ class LeadsController extends BaseController } //Remove insurer Row wise data for RFQ + $groupedRows = []; foreach ($first_json['table_data']['data'] as &$row) { // Filter the inner data array @@ -3044,11 +3209,48 @@ class LeadsController extends BaseController ); $row['data'] = array_values($row['data']); - } + $groupedRows[$row['row_id']][] = $row; + } // Ensure to unset the reference after the loop unset($row); + + $finalRows = []; + + if (!empty($groupedRows)) { + + // Loop through each row_id group and check for Policy status + foreach ($groupedRows as $rowId => $rows) { + $hasPolicy = false; + $isChecked = false; + + foreach ($rows as $row) { + foreach ($row['data'] as $cell) { + if ($cell['parentth'] === 'Policy') { + $hasPolicy = true; + if (trim(strtolower($cell['input_value'])) === 'checked') { + $isChecked = true; + } + break 2; // Found Policy, no need to continue further + } + } + } + + // Step 3: If parent Policy is "Checked", retain the entire group + if (!$hasPolicy || $isChecked) { + foreach ($rows as $r) { + $finalRows[] = $r; + } + } + } + + if (!empty($finalRows)) { + // Update the original data + $first_json['table_data']['data'] = array_values($finalRows); + } + } + // Remove insurers from Proposal Data key for RFQ foreach ($proposeldata['proposal_data']['over_all_column_data'] as $key => &$proposal) { if (isset($proposal['insurers'])) { @@ -3060,8 +3262,8 @@ class LeadsController extends BaseController // Ensure to reset the reference unset($proposal); } - return $first_json; + return $first_json; return null; } diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php index 960f37d0..98271bb7 100755 --- a/app/Models/EmployeePolicyModel.php +++ b/app/Models/EmployeePolicyModel.php @@ -81,6 +81,146 @@ class EmployeePolicyModel extends Model } // ---------------------------------------------------------------------------------------------------------- + // public function getEmployeePolicy($client_id = 0, $policy_id = 0, $status = [], $branch_id = 0, $emp_code = "", $emp_name = "") + // { + // // dd($status); + + // $result = $this->select([ + // 'employee_polices.*', + // 'policy_type.policy_type as policy_name', + // 'im.short_name as insurer_short_name', + // 'ib.branch_name as insurer_branch_name', + // 'ib.branch_code as insurer_branch_code', + // 'tpam.name as tpa_name', + // 'tpam.short_name as tpa_short_name', + // 'tpab.branch_code as tpa_branch_code', + // 'cm.client_name', + // 'cm.short_name as client_short_name', + // // 'emp.id as employee_primary_id', + // 'emp.relationship', + // 'emp.relationship_code', + // 'emp.change_event', + // 'emp.emp_code', + // 'emp.name', + // 'emp.email_corporate', + // 'emp.dob', + // 'DATE_FORMAT(emp.dob, "%d/%m/%Y") AS formatted_dob', + // 'emp.gender', + // 'emp.emp_status', + // 'emp.is_active as emp_is_active', + // 'emp.mobile as mobile', + // 'emp.doj', + // 'emp.basic_pay', + // 'emp.band as grade', + // 'policy_type.policy_type', + // 'client_branch.branch_name as client_branch_name', + // 'client_branch.branch_code as client_branch_code', + // 'cp.policy_no', + // 'cp.policy_type_id', + + // // Name audit trail + // '(SELECT old_value FROM auditing_history WHERE pk = emp.id AND field_name = "name" AND table_name = "employees" ORDER BY id ASC LIMIT 1) AS name_first_old', + // '(SELECT new_value FROM auditing_history WHERE pk = emp.id AND field_name = "name" AND table_name = "employees" ORDER BY id DESC LIMIT 1) AS name_last_new', + + // // DOB audit trail + // '(SELECT old_value FROM auditing_history WHERE pk = emp.id AND field_name = "dob" AND table_name = "employees" ORDER BY id ASC LIMIT 1) AS dob_first_old', + // '(SELECT new_value FROM auditing_history WHERE pk = emp.id AND field_name = "dob" AND table_name = "employees" ORDER BY id DESC LIMIT 1) AS dob_last_new', + + // // Gender audit trail + // '(SELECT old_value FROM auditing_history WHERE pk = emp.id AND field_name = "gender" AND table_name = "employees" ORDER BY id ASC LIMIT 1) AS gender_first_old', + // '(SELECT new_value FROM auditing_history WHERE pk = emp.id AND field_name = "gender" AND table_name = "employees" ORDER BY id DESC LIMIT 1) AS gender_last_new', + + // '(CASE + // WHEN emp.relationship = "Self" + // THEN (SELECT COUNT(id) FROM employees WHERE emp_code = emp.emp_code AND is_active = 0 AND emp_status != "truncated") + // ELSE NULL + // END) AS removed_count', + + // '(CASE + // WHEN emp.relationship != "Self" AND emp.created_at != ( + // SELECT created_at + // FROM employees + // WHERE emp_code = emp.emp_code AND relationship = "Self" AND is_active = 1 + // LIMIT 1 + // ) + // THEN "Newly Added" + // ELSE NULL + // END) AS newly_added', + + // ]) + // ->join('employees emp', 'employee_polices.employee_id = emp.id') + // ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') //cp - client policy + // ->join('policies pm', 'cp.policy_id = pm.id', 'left') //pm - policy master + // ->join('policy_type', 'policy_type.id = cp.policy_type_id') + // ->join('insurers im', 'cp.insurer_id = im.id') //im - insurer master + // ->join('insurer_branch ib', 'cp.insurer_branch_id = ib.id') //ib - insurer branch + // ->join('tpa tpam', 'cp.tpa_id = tpam.id', 'left') //tpam - tpa master + // ->join('tpa_branch tpab', 'cp.tpa_branch_id = tpab.id', 'left') //tpab - tpa branch + // ->join('clients cm', 'cp.client_id = cm.id') //cm - client master + // ->join('client_branch', 'emp.client_branch_id = client_branch.id') //cm - client master + // ->orderBy('emp.emp_code', 'ASC') + // ->orderBy('employee_polices.employee_id', 'ASC'); + + + // // Conditionally add where clauses + // if ($client_id != 0 && !empty($client_id)) { + // $result->where('emp.client_id', $client_id); + // } + // if ($branch_id != 0 && !empty($branch_id)) { + // $result->where('emp.client_branch_id', $branch_id); + // } + // if ($policy_id != 0 && !empty($policy_id)) { + // $result->where('employee_polices.client_policy_id', $policy_id); + // } + + // if (is_array($status) && count($status) > 0) { + + // $result->where('employee_polices.status !=', 'expired'); + + // if (in_array("active", $status)) { + + // $result->where('employee_polices.tpa_id IS NOT NULL'); + // $result->where('employee_polices.uhid IS NOT NULL'); + // $result->whereIn('employee_polices.status', $status); + // } elseif (in_array("pending", $status)) { + + // $result->where('employee_polices.tpa_id IS NULL'); + // $result->where('employee_polices.uhid IS NULL'); + // $result->whereIn('employee_polices.status', array_merge($status, ['active'])); + // } else { + + // $result->whereIn('employee_polices.status', $status); + // } + + // // if($status == 'active'){ + // // $result->where('employee_polices.tpa_id IS NOT NULL'); + // // $result->where('employee_polices.uhid IS NOT NULL'); + // // }else if($status == 'pending'){ + // // $status = 'active'; + // // $result->where('employee_polices.status', $status); + // // }else{ + // // $result->where('employee_polices.status', $status); + // // } + // } + + // if (!empty($emp_code)) { + // $result->where('emp.emp_code', $emp_code); + // } + // if (!empty($emp_name)) { + // $result->like('emp.name', $emp_name); + // } + + // // Always check these conditions + // $result->where('employee_polices.is_active', 1) + // ->where('emp.is_active', 1); + + // $res = $result->findAll(); + + // // dd($this->db->getLastQuery()); + + // return $res; + // } // remarks do not remove + public function getEmployeePolicy($client_id = 0, $policy_id = 0, $status = [], $branch_id = 0, $emp_code = "", $emp_name = "") { // dd($status);