From 9699c6de7286ab409ecd86ee5f3f06b38dbe5378 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Tue, 29 Oct 2024 15:16:31 +0530 Subject: [PATCH] FEAT_RFQ_AND_QCR_MODULE : RV --- app/Config/Routes.php | 8 + app/Controllers/LeadsController.php | 270 ++ app/Models/LeadsModel.php | 19 +- app/Models/PolicyTypeModel.php | 1 + app/Models/RFQModel.php | 60 + app/Views/leads_list.php | 7 +- .../policy_transaction_inception_form.php | 29 +- app/Views/tpa_basic_info.php | 22 +- app/Views/view_rfq.php | 2825 +++++++++++++++++ 9 files changed, 3212 insertions(+), 29 deletions(-) create mode 100644 app/Models/RFQModel.php create mode 100644 app/Views/view_rfq.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index ca0cbe4f..3d943692 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -18,6 +18,7 @@ $routes->get("update-policy-terms-for-corrections", "ClientController::updatePol $routes->get("update_rack_rate_json", "ClientController::updateRackRateJson"); $routes->get("updatajson", "EmpDataServiceController::updatajson"); $routes->get("view", "EmployeeController::viewECard/$1"); +// $routes->get("exportQCRandRFQ", "LeadsController::exportQCRandRFQ"); @@ -343,6 +344,13 @@ $routes->group("leads", ["filter" => "authMVC"], function ($routes) { $routes->get("list", "LeadsController::viewLeadsList"); $routes->post("create", "LeadsController::createLead"); $routes->get("list/(:any)", "LeadsController::getLeadDataForEdit/$1"); + $routes->get("exportQCRandRFQ/(:any)", "LeadsController::exportQCRandRFQ/$1"); +}); + +$routes->group("rfq", ["filter" => "authMVC"], function ($routes) { + $routes->post("create", "LeadsController::createRFQ"); + $routes->post("createQCR", "LeadsController::createQCR"); + $routes->get("list/(:any)", "LeadsController::viewRFQ/$1"); }); $routes->get("driveListFiles", "GoogleDriveController::listFiles"); diff --git a/app/Controllers/LeadsController.php b/app/Controllers/LeadsController.php index 9c9d153f..737a686f 100644 --- a/app/Controllers/LeadsController.php +++ b/app/Controllers/LeadsController.php @@ -4,6 +4,9 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; + use App\Models\UserModel; use App\Models\ClientModel; use App\Models\ClientBranchModel; @@ -15,6 +18,8 @@ use App\Models\KYCEntityTypeModel; use App\Models\PolicyTransactionStatusModel; use App\Models\InsurerBranchModel; use App\Models\TPABranchModel; +use App\Models\RFQModel; +use App\Models\InsurerModel; class LeadsController extends BaseController { @@ -35,6 +40,8 @@ class LeadsController extends BaseController protected $policyTransactionStatusModel; protected $insurerBranchModel; protected $tpaBranchModel; + protected $RFQModel; + protected $insurerModel; //variables for storing array protected $issuer; @@ -58,6 +65,8 @@ class LeadsController extends BaseController $this->policyTransactionStatusModel = new PolicyTransactionStatusModel(); $this->insurerBranchModel = new InsurerBranchModel(); $this->tpaBranchModel = new TPABranchModel(); + $this->RFQModel = new RFQModel(); + $this->insurerModel = new InsurerModel(); $this->issuer = [1 => 'JIBS', 2 => 'Nhance']; $this->clientType = [1 => 'Group', 2 => 'Individual']; @@ -296,4 +305,265 @@ class LeadsController extends BaseController } + //--------RFQ----------------------------------------------------------------------------------------------- + + + public function viewRFQ($id, $type = 1){ + + $data['rfq_data'] = $this->RFQModel + ->where('lead_id', $id) + ->where('type', $type) + ->where('is_active', 1) + ->first(); + + $data['rfq_count'] = $this->RFQModel + ->where('lead_id', $id) + ->where('type', 1) + ->where('is_active', 1) + ->countAllResults(); + + $data['qcr_count'] = $this->RFQModel + ->where('lead_id', $id) + ->where('type', 2) + ->where('is_active', 1) + ->countAllResults(); + + // dd(count($data['rfq_data'])); + + $data['lead_id'] = $id; + $lead_data = $this->leadsModel + ->select('policy_type.question_json') + ->join('policy_type', 'leads.policy_type_id = policy_type.id') + ->where('leads.id', $id) + ->first(); + + $data['question_json'] = $lead_data['question_json']; + $data['page_name'] = isset($data['rfq_data']['type']) && $data['rfq_data']['type'] == 2 ? 'QCR' : 'RFQ'; + $data['insurer'] = $this->insurerBranchModel->getInsurerBranchesWithInsurerNames(); + + + // dd($data); + $this->loadLayout('view_rfq.php', $data); + } + + public function createRFQ(){ + + $data = $this->request->getPost(); + $lead_id = $data['lead_id']; + $data['type'] = 1; + $this->RFQModel + ->where('lead_id', $lead_id) + ->where('type', 1) + ->where('is_active', 1) + ->set('is_active', 0) + ->update(); + + $result = $this->RFQModel->insert($data); + + if ($result) { + return $this->respond(['status' => true, 'id' => $result, 'message' => 'New RFQ created successfully', 'data' =>$data], 200); + } + + return $this->respond(['status' => false, 'id' => $result, 'message' => "Failed to create RFQ", 'data' =>$data], 200); + + } + + public function createQCR(){ + + $data = $this->request->getPost(); + $lead_id = $data['lead_id']; + $data['type'] = 2; + + $this->RFQModel + ->where('lead_id', $lead_id) + ->where('type', 2) + ->where('is_active', 1) + ->set('is_active', 0) + ->update(); + + $result = $this->RFQModel->insert($data); + + if ($result) { + return $this->respond(['status' => true, 'id' => $result, 'message' => 'New QCR created successfully', 'data' =>$data], 200); + } + + return $this->respond(['status' => false, 'id' => $result, 'message' => "Failed to create QCR", 'data' =>$data], 200); + } + + //-----RFQ and QCR EXPORT------------------------------------------------------------------------------------------------ + + //export main route function + public function exportQCRandRFQ($lead_id, $type, $export_type){ + + if($export_type == 'mail'){ + $this-> exportMailForQCRandRFQ($lead_id, $type); + }else{ + $this-> exportExcelForQCRandRFQ($lead_id, $type); + } + } + + //FOR EXCEL + public function exportExcelForQCRandRFQ($lead_id, $type) + { + $filepath = $this->constructExcelToSaveTemp($lead_id, $type); + $filepath = $filepath['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."; + } + } + + //FOR MAIL + public function exportMailForQCRandRFQ($lead_id, $type){ + + $filepath = $this->constructExcelToSaveTemp($lead_id, $type); + + if ($filepath) { + return $this->respond(['status' => true, 'file_path' => $filepath, 'message' => 'Mail send successfully'], 200); + } + + return $this->respond(['status' => false, 'file_path' => $filepath, 'message' => "Mail send failed"], 200); + } + + //Construct excel file and save the file to the folder and return file path + public function constructExcelToSaveTemp($lead_id, $type) + { + $rfq_data = $this->RFQModel + ->select(' + leads.client_name, + leads.client_short_name, + insurers.name as insurer_name, + insurer_branch.branch_name as insurer_branch_name, + tpa.name as tpa_name, + tpa_branch.branch_name as tpa_branch_name, + policy_type.policy_type, + rfq.json + ') + ->join('leads', 'rfq.lead_id = leads.id') + ->join('policy_type', 'leads.policy_type_id = policy_type.id') + ->join('insurers', 'leads.insurer_id = insurers.id') + ->join('insurer_branch', 'leads.insurer_branch_id = insurer_branch.id') + ->join('tpa', 'leads.tpa_id = tpa.id') + ->join('tpa_branch', 'leads.tpa_branch_id = tpa_branch.id') + ->where('rfq.lead_id', $lead_id) + ->where('rfq.type', $type) + ->where('rfq.is_active', 1) + ->first(); + + $lead_data = [ + 'Insured' => $rfq_data['client_name'], + 'Insurer' => $rfq_data['insurer_name'] . ' - ' . $rfq_data['insurer_branch_name'], + 'TPA' => $rfq_data['tpa_name'] . ' - ' . $rfq_data['tpa_branch_name'], + ]; + + $jsonData = $rfq_data['json']; + $data = json_decode($jsonData, true); + + // Initialize PhpSpreadsheet + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + // Start with lead_data at the top + $rowNumber = 1; + foreach ($lead_data as $key => $value) { + $sheet->setCellValue("A{$rowNumber}", $key); + $sheet->setCellValue("B{$rowNumber}", $value); + + // Apply bold style to the key + $sheet->getStyle("A{$rowNumber}")->applyFromArray([ + 'font' => [ + 'bold' => true, + ], + ]); + + $rowNumber++; + } + + // Leave two blank rows + $rowNumber += 2; + + // Set headers and subheaders starting from current row + $headers = $data['table_data']['headers']; + $subHeaderRow = $rowNumber + 1; + $columnLetter = 'A'; + + // Add headers in the first row and subheaders in the second row + foreach ($headers as $header) { + if ($header['parentHeader'] === 'Item Key' || $header['parentHeader'] == 'Action') { + continue; // Skip "Item Key" and "Action" columns + } + + if($header['parentHeader'] === 'Sno'){ + $header['parentHeader'] = 'S.No.'; + } + + $sheet->setCellValue("{$columnLetter}{$rowNumber}", $header['parentHeader']); + + // Apply bold style to the header + $sheet->getStyle("{$columnLetter}{$rowNumber}")->applyFromArray([ + 'font' => [ + 'bold' => true, + ], + ]); + + foreach ($header['subHeaders'] as $subHeader) { + $sheet->setCellValue("{$columnLetter}{$subHeaderRow}", $subHeader); + + // Apply bold style to the subheader + $sheet->getStyle("{$columnLetter}{$subHeaderRow}")->applyFromArray([ + 'font' => [ + 'bold' => true, + ], + ]); + + $columnLetter++; + } + } + + // Move to next row for data entries + $rowNumber = $subHeaderRow + 1; + + // Add data rows + foreach ($data['table_data']['data'] as $dataRow) { + $columnLetter = 'A'; + foreach ($dataRow['data'] as $cellData) { + if ($cellData['parentth'] === 'Item Key' || $cellData['parentth'] == 'Action') { + continue; // Skip "Item Key" data + } + $sheet->setCellValue("{$columnLetter}{$rowNumber}", $cellData['value']); + $columnLetter++; + } + $rowNumber++; + } + + // Set filename based on type + $string = ($type == 2) ? 'QCR' : 'RFQ'; + $filename = $string . '_' . $rfq_data['client_short_name'] . '_' . $rfq_data['policy_type'] . '_' . date('Ymdhis') . '.xlsx'; + + // Save to temporary location + $uploadFilePath = WRITEPATH . 'tmp/' . $filename; + $writer = new Xlsx($spreadsheet); + $writer->save($uploadFilePath); + + return [ + 'filePath' => $uploadFilePath, + 'fileName' => $filename, + ]; // Return file path and file name + } + } diff --git a/app/Models/LeadsModel.php b/app/Models/LeadsModel.php index 239ce3c5..9de34d05 100644 --- a/app/Models/LeadsModel.php +++ b/app/Models/LeadsModel.php @@ -91,14 +91,27 @@ class LeadsModel extends Model leads.*, kyc_entity_type.name as entity_type, policy_type.policy_type, - user_profiles.first_name as salse_person_name + user_profiles.first_name as salse_person_name, + + ( + SELECT COUNT(*) AS qcr_count + FROM rfq + WHERE type = 2 + AND is_active = 1 and lead_id = leads.id + ) AS qcr_count, + + ( + SELECT COUNT(*) AS rfq_count + FROM rfq + WHERE type = 1 + AND is_active = 1 and lead_id = leads.id + + ) AS rfq_count ') ->join('kyc_entity_type', 'leads.entity_type_id = kyc_entity_type.id', 'left') ->join('user_profiles', 'leads.salse_person_id = user_profiles.id', 'left') ->join('policy_type', 'leads.policy_type_id = policy_type.id', 'left') ->where('leads.is_active', 1) - ->where('leads.is_active', 1) - ->where('leads.is_active', 1) ->orderBy('id', 'desc') ->findAll(); diff --git a/app/Models/PolicyTypeModel.php b/app/Models/PolicyTypeModel.php index 82c06d70..3edd01f1 100755 --- a/app/Models/PolicyTypeModel.php +++ b/app/Models/PolicyTypeModel.php @@ -22,5 +22,6 @@ class PolicyTypeModel extends Model "etp", "iep", "itp", + "question_json", ]; } diff --git a/app/Models/RFQModel.php b/app/Models/RFQModel.php new file mode 100644 index 00000000..f74a1313 --- /dev/null +++ b/app/Models/RFQModel.php @@ -0,0 +1,60 @@ +" onclick="getLeadsDataForEdit('')"> Edit - + RFQ + 0) { ?> + + QCR + + diff --git a/app/Views/policy_transaction_inception_form.php b/app/Views/policy_transaction_inception_form.php index c6d0e22a..c6e29bfe 100644 --- a/app/Views/policy_transaction_inception_form.php +++ b/app/Views/policy_transaction_inception_form.php @@ -799,7 +799,7 @@
- +
@@ -858,7 +858,7 @@ - +