FIX_ExPort XL Completed
This commit is contained in:
parent
56362a4e98
commit
c961719b27
@ -94,7 +94,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
$routes->get('enquiry/getQuickQuoteEnquiryList', 'EnquiryController::getQuickQuoteEnquiryList');
|
||||
$routes->get('enquiry/updateEnquiryInProgress', 'EnquiryController::updateEnquiryInProgress');
|
||||
$routes->get('enquiry/updateEnquiryStatus', 'EnquiryController::updateEnquiryStatus');
|
||||
$routes->get('enquiry/checkVehicleDuplicate', 'EnquiryController::checkVehicleDuplicate');
|
||||
$routes->get('enquiry/checkVehicleDuplicate', 'EnquiryController::checkVehicleDuplicate'); // VALIDATION
|
||||
|
||||
|
||||
//Proposal
|
||||
@ -132,13 +132,24 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
$routes->get('dashboard/staffDashboard', 'DashboardController::staffDashboard');
|
||||
$routes->get('dashboard/agentDashboard', 'DashboardController::agentDashboard');
|
||||
|
||||
//DASHBOARD Season 2
|
||||
$routes->get("dashboard/businessDashboard", "DashboardController::businessDashboard");
|
||||
$routes->get("dashboard/partnerDashboard", "DashboardController::partnerDashboard");
|
||||
$routes->get("dashboard/productivityDashboard", "DashboardController::productivityDashboard");
|
||||
|
||||
$routes->get("dashboard/downloadBrokerPoliciesExcel", "DashboardController::downloadBrokerPoliciesExcel");
|
||||
$routes->get("dashboard/downloadInsurerPoliciesExcel", "DashboardController::downloadInsurerPoliciesExcel");
|
||||
$routes->get("dashboard/downloadProductPoliciesExcel", "DashboardController::downloadProductPoliciesExcel");
|
||||
//DASHBOARD Season 3 EXPORT EXCEL
|
||||
$routes->get("dashboard/downloadBrokerPoliciesExcel", "ExcelExportController::downloadBrokerPoliciesExcel");
|
||||
$routes->get("dashboard/downloadInsurerPoliciesExcel", "ExcelExportController::downloadInsurerPoliciesExcel");
|
||||
$routes->get("dashboard/downloadProductPoliciesExcel", "ExcelExportController::downloadProductPoliciesExcel");
|
||||
|
||||
//DASHBOARD Season 4 EXPORT EXCEL
|
||||
$routes->get("dashboard/downloadAgentMonthlyPoliciesExcel", "ExcelExportController::downloadAgentMonthlyPoliciesExcel");
|
||||
$routes->get("dashboard/downloadLowPremiumAgentExcel", "ExcelExportController::downloadLowPremiumAgentExcel");
|
||||
$routes->get("dashboard/downloadAgentsWithoutPoliciesExcel", "ExcelExportController::downloadAgentsWithoutPoliciesExcel");
|
||||
|
||||
//DASHBOARD Season 5 EXPORT EXCEL
|
||||
$routes->get("dashboard/downloadStaffAndProductPoliciesExcel", "ExcelExportController::downloadStaffAndProductPoliciesExcel");
|
||||
|
||||
|
||||
|
||||
//invoice
|
||||
@ -149,6 +160,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
$routes->post('invoice/commission-rate-list', 'InvoiceController::getCommissionRateList');
|
||||
|
||||
|
||||
// SALES EXECUTIVE
|
||||
$routes->get('salesExecutive/executiveList', 'SalesExecutive::salesExecutiveList');
|
||||
$routes->get('salesExecutive/findExecutive', 'SalesExecutive::findSalesExecutive');
|
||||
$routes->post('salesExecutive/createExecutive', 'SalesExecutive::createSalesExecutive');
|
||||
@ -156,7 +168,6 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
$routes->post('salesExecutive/changeExecutiveStatus', 'SalesExecutive::changeSalesExecutiveStatus');
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -167,7 +178,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
|
||||
$routes->get("policyCommissionCalculationAndGeminiPolicyRead", "PolicyController::policyCommissionCalculationAndGeminiPolicyRead");
|
||||
|
||||
});
|
||||
|
||||
|
||||
$routes->cli('cli/processjob', 'JobWorker::processJob');
|
||||
$routes->cli('cli/processjobs', 'JobWorker::processJobs');
|
||||
$routes->get("processjob", "JobWorker::processJob");
|
||||
@ -192,4 +203,3 @@ $routes->get("processjob", "JobWorker::processJob");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
470
app/Controllers/ExcelExportController.php
Normal file
470
app/Controllers/ExcelExportController.php
Normal file
@ -0,0 +1,470 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||
|
||||
class ExcelExportController extends ResourceController
|
||||
{
|
||||
protected $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = \Config\Database::connect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Centralized function to create the spreadsheet, apply styling,
|
||||
* set headers, and stream the file output.
|
||||
*
|
||||
* @param array $header Column headers (1D array)
|
||||
* @param array $data Policy data including the total row (2D array)
|
||||
* @param string $title Spreadsheet title
|
||||
* @param string $fileName Output file name
|
||||
* @param string $totalLabelCol Column letter for the 'TOTAL:' label (e.g., 'C' or 'E')
|
||||
* @param string $totalValueCol Column letter for the total value (e.g., 'D' or 'F')
|
||||
* @return void
|
||||
*/
|
||||
private function streamExcelFile(
|
||||
array $header,
|
||||
array $data,
|
||||
string $title,
|
||||
string $fileName,
|
||||
string $totalLabelCol,
|
||||
string $totalValueCol
|
||||
): void
|
||||
{
|
||||
// Ensure no output buffering is active before streaming the file
|
||||
while (ob_get_level() > 0) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
// Create Spreadsheet
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 1. Title (Merged across the header columns)
|
||||
$lastCol = $sheet->getHighestColumn();
|
||||
$sheet->mergeCells("A1:{$lastCol}1");
|
||||
$sheet->setCellValue('A1', $title);
|
||||
$sheet->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
||||
|
||||
// 2. Headers (Row 2)
|
||||
$sheet->fromArray($header, NULL, 'A2');
|
||||
|
||||
// 3. Data (Starting from Row 3)
|
||||
// Note: The total row MUST be included in the $data array passed to this function.
|
||||
$sheet->fromArray($data, NULL, 'A3');
|
||||
|
||||
// 4. Total Row Styling
|
||||
// Total row number is the count of rows in the $data array (which includes the header, plus 2 for title/headers)
|
||||
$totalRowNumber = count($data) + 2;
|
||||
|
||||
// Apply bold formatting to the label and the sum
|
||||
$sheet->getStyle($totalLabelCol . $totalRowNumber . ':' . $totalValueCol . $totalRowNumber)->getFont()->setBold(true);
|
||||
|
||||
// Align the label to the right
|
||||
$sheet->getStyle($totalLabelCol . $totalRowNumber)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT);
|
||||
|
||||
// Auto-size columns for better presentation
|
||||
for ($col = 'A'; $col <= $lastCol; $col++) {
|
||||
$sheet->getColumnDimension($col)->setAutoSize(true);
|
||||
}
|
||||
|
||||
// 5. HTTP Headers
|
||||
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
header("Content-Disposition: attachment; filename=\"{$fileName}\"");
|
||||
header("Cache-Control: max-age=0");
|
||||
|
||||
// 6. Output File
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
$writer->save('php://output');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $brokerId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getBrokerPoliciesData($managerId, $brokerId, $month): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
// Note: I removed 'broker_id' from SELECT as it's redundant in the Excel sheet details
|
||||
$builder->select('pb.name AS broker_name, DATE_FORMAT(pp.issued_date, "%d-%m-%Y") AS issued_date,pp.policy_number, pp.premium_amount');
|
||||
$builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left');
|
||||
$builder->join('partner_brokers pb', 'pb.id = pe.broker_id', 'left');
|
||||
|
||||
// --- Correction 2: Use Robust Date Range Filtering ---
|
||||
if ($month === "current") {
|
||||
// WHERE issued_date >= start_of_current_month AND issued_date < start_of_next_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
else if ($month === "previous") {
|
||||
// WHERE issued_date >= start_of_previous_month AND issued_date < start_of_current_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
|
||||
// Broker and Manager Filter
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pb.id', $brokerId);
|
||||
$builder->orderBy('pp.issued_date', 'DESC');
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param string $vehicleType
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getProductPoliciesData($managerId, $vehicleType, $month): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
// Note: I removed 'broker_id' from SELECT as it's redundant in the Excel sheet details
|
||||
$builder->select('pp.vehicle_type, DATE_FORMAT(pp.issued_date, "%d-%m-%Y") AS issued_date,pp.policy_number, pp.premium_amount');
|
||||
|
||||
|
||||
// --- Correction 2: Use Robust Date Range Filtering ---
|
||||
if ($month === "current") {
|
||||
// WHERE issued_date >= start_of_current_month AND issued_date < start_of_next_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
else if ($month === "previous") {
|
||||
// WHERE issued_date >= start_of_previous_month AND issued_date < start_of_current_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
|
||||
// Broker and Manager Filter
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pp.vehicle_type', $vehicleType);
|
||||
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $insurerId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getInsurerPoliciesData($managerId, $insurerId, $month): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
// Note: I removed 'broker_id' from SELECT as it's redundant in the Excel sheet details
|
||||
$builder->select('i.name AS insurer_name,i.short_name,pp.insured_name, DATE_FORMAT(pp.issued_date, "%d-%m-%Y") AS issued_date,pp.policy_number, pp.premium_amount');
|
||||
|
||||
$builder->join('partner_quotation pq', 'pq.id = pp.quotation_id', 'left');
|
||||
$builder->join('insurers i', 'i.id = pq.insurer_id', 'left');
|
||||
|
||||
|
||||
// --- Correction 2: Use Robust Date Range Filtering ---
|
||||
if ($month === "current") {
|
||||
// WHERE issued_date >= start_of_current_month AND issued_date < start_of_next_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
else if ($month === "previous") {
|
||||
// WHERE issued_date >= start_of_previous_month AND issued_date < start_of_current_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
|
||||
// Broker and Manager Filter
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('i.id', $insurerId);
|
||||
$builder->where('i.is_active',1);
|
||||
$builder->orderBy('pp.issued_date', 'DESC');
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $brokerId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getStaffAndProductPoliciesExcel($managerId, $executiveId,$vehicleType, $month): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
|
||||
$builder->select('pse.name as sales_executive_name, pp.vehicle_type AS vechile_type,pp.policy_number,DATE_FORMAT(pp.issued_date, "%d-%m-%Y") AS issued_date,pp.premium_amount');
|
||||
$builder->join('partner_agent pa', 'pa.id = pp.agent_id', 'left');
|
||||
$builder->join('partner_sales_executive pse', 'pse.id = pa.sales_executive_id', 'left');
|
||||
|
||||
// --- Correction 2: Use Robust Date Range Filtering ---
|
||||
if ($month === "current") {
|
||||
// WHERE issued_date >= start_of_current_month AND issued_date < start_of_next_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
else if ($month === "previous") {
|
||||
// WHERE issued_date >= start_of_previous_month AND issued_date < start_of_current_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
|
||||
// Broker and Manager Filter
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pa.sales_executive_id', $executiveId);
|
||||
$builder->where('pa.sales_executive_id IS NOT NULL');
|
||||
$builder->where('pse.is_active', 1);
|
||||
$builder->where('pp.vehicle_type', $vehicleType);
|
||||
$builder->orderBy('pp.issued_date', 'DESC');
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $AgentId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getAgentMonthlyPoliciesExcel($managerId, $AgentId, $month): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
// Note: I removed 'broker_id' from SELECT as it's redundant in the Excel sheet details
|
||||
$builder->select('pa.name AS agent_name,pa.agent_code,pp.policy_number,DATE_FORMAT(pp.issued_date, "%d-%m-%Y") AS issued_date,pp.premium_amount');
|
||||
|
||||
$builder->join('partner_agent pa', 'pp.agent_id = pa.id', 'inner');
|
||||
$builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left');
|
||||
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pa.id', $AgentId);
|
||||
$builder->where('pp.policy_number IS NOT NULL');
|
||||
|
||||
// --- Correction 2: Use Robust Date Range Filtering ---
|
||||
if ($month === "current") {
|
||||
// WHERE issued_date >= start_of_current_month AND issued_date < start_of_next_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
else if ($month === "previous") {
|
||||
// WHERE issued_date >= start_of_previous_month AND issued_date < start_of_current_month
|
||||
$builder->where("pp.issued_date >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01')", NULL, FALSE);
|
||||
$builder->where("pp.issued_date < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')", NULL, FALSE);
|
||||
}
|
||||
|
||||
$builder->orderBy('pp.issued_date', 'DESC');
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $brokerId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getAgentsWithoutPoliciesData($managerId): array
|
||||
{
|
||||
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_agent pa');
|
||||
// Note: I removed 'broker_id' from SELECT as it's redundant in the Excel sheet details
|
||||
$builder->select("pa.name AS agent_name,pa.agent_code,pa.email,pa.mobile");
|
||||
|
||||
$builder->join(
|
||||
'partner_policy pp',
|
||||
"pa.id = pp.agent_id AND pp.issued_date >= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)",
|
||||
'left'
|
||||
);
|
||||
|
||||
// Broker and Manager Filter
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pp.id', null); // No policy in last 10 days
|
||||
$builder->orderBy('pa.name', 'ASC');
|
||||
// echo $builder->getCompiledSelect(); die;
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the detailed policy query and extracts the data for the spreadsheet.
|
||||
* @param string $managerId
|
||||
* @param int $brokerId
|
||||
* @param string $month ('current' or 'previous')
|
||||
* @return array
|
||||
*/
|
||||
private function getLowPremiumAgentData($managerId): array
|
||||
{
|
||||
// --- Correction 1: Initialize Database Connection ---
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$builder = $this->db->table('partner_policy pp');
|
||||
|
||||
$builder->select("
|
||||
pa.name AS agent_name,
|
||||
pa.agent_code,
|
||||
CASE
|
||||
WHEN pa.is_active = 1 THEN 'Active Agent'
|
||||
ELSE 'Inactive Agent'
|
||||
END AS agent_status,
|
||||
pp.policy_number,
|
||||
DATE_FORMAT(pp.issued_date, '%d-%m-%Y') AS issued_date,
|
||||
pp.premium_amount
|
||||
");
|
||||
|
||||
$builder->join('partner_agent pa', 'pa.id = pp.agent_id', 'inner');
|
||||
// WHERE conditions
|
||||
$builder->where('pp.manager_id', $managerId);
|
||||
$builder->where('pa.id', $agentId);
|
||||
$builder->where('pp.premium_amount <', 50000);
|
||||
$builder->orderBy('pp.issued_date', 'DESC');
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
// Restructure data for PhpSpreadsheet (2D array of values)
|
||||
foreach ($results as $row) {
|
||||
$data[] = array_values($row);
|
||||
}
|
||||
|
||||
} catch (DatabaseException $e) {
|
||||
log_message('error', 'Database Error during Excel fetch: ' . $e->getMessage());
|
||||
// Re-throw the exception so it is caught by the main function's catch block
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user