diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 22e812fc..2377fc38 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -579,6 +579,7 @@ $routes->group("/bdsReport", ["filter" => "authMVC"], function ($routes) { $routes->group("/ticket", ["filter" => "authMVC"], function ($routes) { $routes->match( ['get', 'post'], 'list','TicketController::ticketList'); $routes->get('feedback-list','TicketController::feedbackList'); + $routes->match(['get', 'post'], 'claim-upload','TicketController::claimDumpUpload'); $routes->get('remove','TicketController::removeTicket'); $routes->get('new/(:any)','TicketController::ticket_form/$1'); $routes->post('create','TicketController::createTicket'); diff --git a/app/Controllers/LeadsController.php b/app/Controllers/LeadsController.php index b1db3311..4d7e2170 100644 --- a/app/Controllers/LeadsController.php +++ b/app/Controllers/LeadsController.php @@ -44,6 +44,8 @@ use Google\Service\FactCheckTools\Resource\Claims; use GPBMetadata\Google\Type\Datetime; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; +use function PHPSTORM_META\type; + class LeadsController extends BaseController { use ResponseTrait; @@ -773,7 +775,7 @@ class LeadsController extends BaseController // dd($data); if ($data['lead_data']['lead_form_type'] == 1) { - + $data['demogrphy_html_data'] = $this->generateDemographyDataTable(['lead_id' => $id]); $this->loadLayout('view_rfq.php', $data); } else if ($data['lead_data']['lead_form_type'] == 2) { @@ -961,7 +963,7 @@ class LeadsController extends BaseController } - private function reorderProposalsByInsurerTotal(array $data): array + public function reorderProposalsByInsurerTotal(array $data): array { if (!isset($data['premium_data']['data'])) return $data; @@ -973,7 +975,7 @@ class LeadsController extends BaseController foreach ($original as $key => $value) { // Match only keys that look like 'Proposal X' - if (preg_match('/^(Proposal\s+\d+|Existing Renewal|Existing Rollover)$/', $key)) { + if (preg_match('/^(Proposal\s+\d+|Existing Renewal|Existing Rollover)$/', $key)) { // Get the insurer entry (not 'Quote Asked') foreach ($value as $subKey => $subVal) { if ($subKey !== 'Quote Asked' && isset($subVal['Total'])) { @@ -994,6 +996,18 @@ class LeadsController extends BaseController } } + foreach ($proposals as $proposalKey => &$proposal) { + $quoteAsked = $proposal['Quote Asked']; // Keep Quote Asked separately + unset($proposal['Quote Asked']); + + uasort($proposal, function ($a, $b) { + return $a['Total'] <=> $b['Total']; // Ascending by Total + }); + + // Add Quote Asked back at the top + $proposal = array_merge(['Quote Asked' => $quoteAsked], $proposal); + } + // Sort proposals by their insurer's total uasort($proposals, function ($a, $b) { $totalA = 0; @@ -1028,9 +1042,48 @@ class LeadsController extends BaseController return $data; } + $porposalData = $data['premium_data']['data']; $premiumProposals = array_keys($data['premium_data']['data']); $filteredProposals = []; + //Reorder the Insurer based on the Premium data insurer + foreach ($porposalData as $key => $value) { + + if(in_array($key, ['Particulars', ""])){ + continue; + } + + // Ensure the premium data is an array and not empty + if (!is_array($value) || empty($value)) { + continue; + } + + // Step 1: Get the premium display_name order (excluding "Quote Asked") + $premiumOrder = array_keys(array_diff_key($value, ["Quote Asked" => ""])); + + // Skip if no valid order found + if (empty($premiumOrder)) { + continue; + } + + // usort($data['proposal_data']['over_all_column_data'][$key]['insurers'], function($a, $b) use ($premiumOrder) { + // return array_search($a['display_name'], $premiumOrder) - array_search($b['display_name'], $premiumOrder); + // }); + + // Step 2: Reorder proposal insurers based on premium order + usort($data['proposal_data']['over_all_column_data'][$key]['insurers'], function($a, $b) use ($premiumOrder) { + $posA = array_search($a['display_name'] ?? '', $premiumOrder); + $posB = array_search($b['display_name'] ?? '', $premiumOrder); + + // If not found, push to the end + $posA = $posA === false ? PHP_INT_MAX : $posA; + $posB = $posB === false ? PHP_INT_MAX : $posB; + + return $posA - $posB; + }); + + } + // Collect proposal keys that match the pattern "Proposal X" foreach ($premiumProposals as $key) { if (preg_match('/^(Proposal\s+\d+|Existing Renewal|Existing Rollover)$/', $key) && isset($data['proposal_data']['over_all_column_data'][$key])) { @@ -1068,6 +1121,32 @@ class LeadsController extends BaseController } } + //Reorder the SubHeader of the Proposel + foreach ($sortedProposalOrder as $proposalKey => $proposalData) { + // Skip if insurers or subHeaders are missing + if ( + !isset($proposalData['insurers']) || + !is_array($proposalData['insurers']) || + !isset($proposalHeaders[$proposalKey]['subHeaders']) || + !is_array($proposalHeaders[$proposalKey]['subHeaders']) + ) { + continue; + } + + // Keep "Quote Asked" fixed at index 0 + $newSubHeaders = ["Quote Asked"]; + + // Add insurers in the sorted order + foreach ($proposalData['insurers'] as $insurer) { + if(in_array($insurer['display_name'], $proposalHeaders[$proposalKey]['subHeaders'])){ + $newSubHeaders[] = $insurer['display_name']; + } + } + + // Replace the subHeaders in the header array + $proposalHeaders[$proposalKey]['subHeaders'] = $newSubHeaders; + } + // dd($staticHeaders, $proposalHeaders, $sortedProposalOrder); // Step 2: Reorder headers @@ -1088,10 +1167,9 @@ class LeadsController extends BaseController } unset($value); - + //merge the all headers $reorderedHeaders = array_merge($staticHeaders, $reorderedHeaders, $actionHeader); - - + // Step 3: Reorder each row's `data` by matching parentth foreach ($dataRows as $dataRowIndex => &$row) { $staticData = []; @@ -1137,6 +1215,12 @@ class LeadsController extends BaseController } unset($value); + //Reorder the Row Data based on the Insurer + $reorderedProposalData = $this->reorderProposalRowData($reorderedProposalData, $proposalHeaders); + + + // print_rr($reorderedProposalData); die; + $row['data'] = array_merge($staticData, $reorderedProposalData, $actionData); } @@ -1151,6 +1235,44 @@ class LeadsController extends BaseController return $data; } + private function reorderProposalRowData(array $proposalRowData, array $proposalHeaderData): array + { + $newRowData = []; + + // Group rows by parentth + $groupedRows = []; + foreach ($proposalRowData as $row) { + if (!isset($row['parentth'])) { + continue; // skip invalid rows + } + $groupedRows[$row['parentth']][] = $row; + } + + // Reorder each group's rows based on header subHeaders + foreach ($proposalHeaderData as $proposalKey => $headerInfo) { + if ( + !isset($groupedRows[$proposalKey]) || + !isset($headerInfo['subHeaders']) || + !is_array($headerInfo['subHeaders']) + ) { + continue; + } + + $order = $headerInfo['subHeaders']; + $rows = $groupedRows[$proposalKey]; + + // Sort whole rows according to subth position in $order + usort($rows, function ($a, $b) use ($order) { + return array_search($a['subth'], $order) - array_search($b['subth'], $order); + }); + + // Append sorted rows to final array + $newRowData = array_merge($newRowData, $rows); + } + + return $newRowData; + } + private function renumberProposalKeys(array $input): array { $result = []; @@ -1284,7 +1406,7 @@ class LeadsController extends BaseController $lead_data = [ 'Insured' => $rfq_data['client_name'], 'No of Employees at Inception' => $rfq_data['incept_emp_count'], - 'Total Sum Insured at Inception ' => $rfq_data['total_si_at_incept'], + // 'Total Sum Insured at Inception ' => $rfq_data['total_si_at_incept'], 'Policy Period' => !empty($rfq_data['policy_start_date']) && !empty($rfq_data['policy_end_date']) ? date('d-m-Y', strtotime($rfq_data['policy_start_date'])) . ' to ' . date('d-m-Y', strtotime($rfq_data['policy_end_date'])) : "To be decided", // 'Insurer' => $rfq_data['insurer_name'] ?? " - ", // 'TPA' => $rfq_data['tpa_name'] ?? " - ", @@ -1455,7 +1577,20 @@ class LeadsController extends BaseController ], ], ]); - $subheader_count = array_sum(array_map(fn($header) => count($header['subHeaders']), $data['table_data']['headers'])); + + if($type == 2){ + $subheader_count = array_sum( + array_map( + fn($header) => count(array_filter( + $header['subHeaders'], + fn($sub) => $sub !== "Quote Asked" + )), + $data['table_data']['headers'] + ) + ); + }else{ + $subheader_count = array_sum(array_map(fn($header) => count($header['subHeaders']), $data['table_data']['headers'])); + } if ($is_placement == false) { $subheader_count = $subheader_count - 2; @@ -1484,7 +1619,8 @@ class LeadsController extends BaseController if ($type == 2) { $subHeaderRow = $rowNumber + 1; } else { - $subHeaderRow = $rowNumber_for_remove_quote_asked; + // $subHeaderRow = $rowNumber_for_remove_quote_asked; + $subHeaderRow = $rowNumber + 1; } $columnLetter = 'A'; @@ -1505,6 +1641,13 @@ class LeadsController extends BaseController if (!in_array($header['parentHeader'], ['Item Key', 'Action', 'Sno', 'Particulars'])) { $header_actual_count++; + + //Quote asked not showed in the QCR excel so some proposel has only one Quote Asked, So that case avoid the proposel name + $subHeaderCount = count($header['subHeaders'] ?? []) ?? 0; + if($subHeaderCount <= 1 && $type == 2 && $is_placement == false){ + // print_rr($header); + continue; + } } if ($header['parentHeader'] === 'Sno') { @@ -1525,7 +1668,14 @@ class LeadsController extends BaseController } $startColumn = $columnLetter; // Start of the current header range - $subHeaderCount = count($header['subHeaders']); // Number of subheaders for this parent header + if($type == 2){ + $subHeaderCount = count(array_filter($header['subHeaders'], function($subHeader) { + return $subHeader !== "Quote Asked"; + })); + }else{ + $subHeaderCount = count($header['subHeaders']); // Number of subheaders for this parent header + } + // Set parent header value if ($is_placement == true && !in_array($header['parentHeader'], ['Particulars', 'S.No.'])) { $sheet->setCellValue("{$startColumn}{$rowNumber}", "Terms"); @@ -1539,6 +1689,7 @@ class LeadsController extends BaseController $sheet->setCellValue("{$startColumn}{$rowNumber}", "Proposal Terms " . $headerIndex); } } + $sheet->getStyle("{$startColumn}{$rowNumber}")->applyFromArray([ 'font' => ['bold' => true], 'alignment' => [ @@ -1558,7 +1709,11 @@ class LeadsController extends BaseController // Add subheaders foreach ($header['subHeaders'] as $subHeader) { - if ($type == 2 && $is_placement == false) { + if($type == 2 && in_array($subHeader, ['Quote Asked'])){ + continue; + } + + if ($is_placement == false) { $sheet->setCellValue("{$columnLetter}{$subHeaderRow}", $subHeader); } @@ -1583,7 +1738,7 @@ class LeadsController extends BaseController $length++; } } - + if($header_actual_count == 1 && $type == 1){ $sheet->getColumnDimension('B')->setWidth(80); }else{ @@ -1625,6 +1780,10 @@ class LeadsController extends BaseController continue; } + if($type == 2 && in_array($cellData['subth'], ['Quote Asked'])){ + continue; + } + if ($cellData['parentth'] == 'Sno') { $sheet->setCellValue("{$columnLetter}{$rowNumber}", $serial_no); } else { @@ -1651,7 +1810,7 @@ class LeadsController extends BaseController //premium data if ($type == 2) { - if($is_placement == true){$columnLetterForPremium = "B";}else{$columnLetterForPremium = "C";} + if($is_placement == true){$columnLetterForPremium = "B";}else{$columnLetterForPremium = "B";} $rowNumber += 2; // Add premium data @@ -1665,20 +1824,20 @@ class LeadsController extends BaseController // $gstAmt = [$labelArray[1]]; // $total = [$labelArray[2]]; - if($is_placement == true){ + // if($is_placement == true){ $premium[] = $labelArray[0]; $gstAmt[] = $labelArray[1]; $total[] = $labelArray[2]; - } + // } foreach ($premiumData as $proposal => $insurers) { if ($proposal != 'Particulars') { foreach ($insurers as $insurer => $values) { if($insurer == 'Quote Asked'){ - $premium[] = count($insurers ?? []) > 1 ? $labelArray[0] : ""; + // $premium[] = count($insurers ?? []) > 1 ? $labelArray[0] : ""; // $gst[] = ""; - $gstAmt[] = count($insurers ?? []) > 1 ? $labelArray[1] : ""; - $total[] = count($insurers ?? []) > 1 ? $labelArray[2] : ""; + // $gstAmt[] = count($insurers ?? []) > 1 ? $labelArray[1] : ""; + // $total[] = count($insurers ?? []) > 1 ? $labelArray[2] : ""; }else{ $premium[] = formatIndianCurrency($values[$labelArray[0]]); // $gst[] = $values[$labelArray[1]]; @@ -1912,7 +2071,7 @@ class LeadsController extends BaseController return true; } - public function calculateMembersDemography($params) + public function calculateMembersDemography($params, $returnType = null) { $lead_id = $params['lead_id']; $lead_data = $this->leadsModel->find($lead_id); @@ -1965,12 +2124,20 @@ class LeadsController extends BaseController $this->myLogger->logme('error', ($message . $file_name_with_path)); return ['status' => 'failed', 'message' => $message]; } + try { $classifiers = $this->getDemographyData($members, $age_band_data, $members_heading, $available_col, $col_index); } catch (Exception $e) { return ['status' => 'fail', 'message' => $e->getMessage()]; } + //for this to view the demography in the RFQ and QCR page to using internal + if($returnType == "internal"){ + return ['data' => $classifiers]; + // print_rr($classifiers); die; + } + + try { $result = $this->generateClassifierSpreadsheet($classifiers, WRITEPATH . 'uploads/lead_files/'); if ($result['success']) { @@ -4668,4 +4835,55 @@ class LeadsController extends BaseController return []; } + + + public function generateDemographyDataTable($param) + { + $returnData = $this->calculateMembersDemography($param, "internal"); + + $html = ""; + + if (isset($returnData['data'])) { + + $data = $returnData['data']; + foreach ($data as $category => $records) { + // Extract all column headers (age groups) dynamically + $allColumns = []; + foreach ($records as $person => $ages) { + $allColumns = array_merge($allColumns, array_keys($ages)); + } + $allColumns = array_unique($allColumns); + $allColumns = array_values($allColumns); // reset index + + $category = ucfirst($category); + + // Start table with clean styling + $html .= "
| Relation | "; + foreach ($allColumns as $col) { + $html .= "{$col} | "; + } + $html .= "
|---|---|
| {$relation} | "; + foreach ($allColumns as $col) { + $value = isset($ages[$col]) ? $ages[$col] : "-"; + $html .= "{$value} | "; + } + $html .= "