From 0580c4e0ecd1900e8b91c995a4b063333005095a Mon Sep 17 00:00:00 2001 From: "sanjeev.p" Date: Sat, 6 Dec 2025 12:00:10 +0530 Subject: [PATCH] FIX_dashboard --- app/Config/Routes.php | 5 +- app/Controllers/DashboardController.php | 459 ++++++++++++++++++++++++ app/Controllers/EnquiryController.php | 52 +-- 3 files changed, 464 insertions(+), 52 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 17527ac..658b32c 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -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/checkDuplicate', 'EnquiryController::checkDuplicate'); + $routes->get('enquiry/checkVehicleDuplicate', 'EnquiryController::checkVehicleDuplicate'); //Proposal @@ -129,6 +129,9 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) { $routes->get('dashboard/handlerDashboard', 'DashboardController::handlerDashboard'); $routes->get('dashboard/staffDashboard', 'DashboardController::staffDashboard'); $routes->get('dashboard/agentDashboard', 'DashboardController::agentDashboard'); + + $routes->get("dashboard/businessDashboard", "DashboardController::businessDashboard"); + $routes->get("dashboard/partnerDashboard", "DashboardController::partnerDashboard"); //invoice $routes->get('invoice/list', 'InvoiceController::invoiceList'); diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 0453166..53d1e58 100644 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -423,5 +423,464 @@ class DashboardController extends ResourceController return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200); } + + //business dashboard = month-wise insurer,broker and product (PolicyCount , Premium) + public function businessDashboard() + { + // Initialize the details array to ensure the final output is always structured. + $details = [ 'brokerWise' => [], 'productWise' => [], 'insurerWise' => []]; + $code = 200; + $message = "Business Dashboard Data Retrieved Successfully"; + + try { + // 1. Fetch all data concurrently (or sequentially, as shown) + $details['brokerWise'] = $this->brokerWise(); + $details['productWise'] = $this->productWise(); + $details['insurerWise'] = $this->insurerWise(); + + // 2. Check for overall emptiness (optional, but good practice) + $is_empty = array_reduce($details, function ($carry, $item) { + return $carry && empty($item); + }, true); + + if ($is_empty) { + // Throw a 404 if ALL data sections are empty + throw new \RuntimeException('No Data Found', 404); + } + + // 3. Return a successful, combined JSON response + return $this->response->setJSON([ + 'status' => 'success', + 'data' => $details, // Contains the nested arrays: brokerWise, productWise, insurerWise + 'code' => 200, + 'message' => $message, + 'ref' => 'NIL' + ])->setStatusCode(200); + + } catch (\Throwable $e) { + // --- Centralized Error Handling Logic --- + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) + ? $e->getCode() : 500; + + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; + + if ($isDbError) { + // Log the full error, but return minimal info for security + $ref = $e->getFile() . " / LN : " . $e->getLine(); + } else { + // This catches the \RuntimeException for "No Data found" (code 404) + $ref = "Application exception occurred"; + } + + // Return an error JSON response + return $this->response->setJSON([ + 'status' => 'error', + 'data' => [], // Return empty data on error + 'code' => $e->getCode(), + 'message' => $e->getMessage(), + 'ref' => $ref, + ])->setStatusCode($code); + } + } + + //partner dashboard = month-wise PolicyCount , month-wise Premium, No Agent For Last 10 Days and below 50thouands Premium agentwise + public function partnerDashboard() + { + // Initialize the details array to ensure the final output is always structured. + $code = 200; + $message = "Partner Dashboard Data Retrieved Successfully"; + $details = [ 'monthlyPolicyCount' => [],'monthlyPremiumAmount' => [],'noPolicyTimeRange' => [],'below50tPremiumTimeRange' => []]; + // 'agentNoPolicyLast10Days' => [],// 'agentBelow50tPremiumLast10Days' => [], + + try { + // 1. Fetch all data concurrently (or sequentially, as shown) + $date = $this->request->getGet('date'); + $date = $date ?? date('Y-m-d'); + $limit = $limit ?? 50; + $value = $value ?? 10; + $unit = $unit ?? 'DAY'; + + $details['monthlyPolicyCount'] = $this->agentMonthlyPolicyCount($date,$limit); + $details['monthlyPremiumAmount'] = $this->agentMonthlyPremiumAmount($date,$limit); + $details['noPolicyTimeRange'] = $this->agentNoPolicyTimeRange($value,$unit); + $details['below50tPremiumTimeRange'] = $this->agentBelow50tPremiumTimeRange($value,$unit); + + + // 2. Check for overall emptiness (optional, but good practice) + $is_empty = array_reduce($details, function ($carry, $item) { + return $carry && empty($item); + }, true); + + if ($is_empty) { + throw new \RuntimeException('No Data Found', 404); + } + + // 3. Return a successful, combined JSON response + return $this->response->setJSON([ + 'status' => 'success', + 'data' => $details, // Contains the nested arrays: brokerWise, productWise, insurerWise + 'code' => 200, + 'message' => $message, + 'ref' => 'NIL' + ])->setStatusCode(200); + + } catch (\Throwable $e) { + // --- Centralized Error Handling Logic --- + $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) + ? $e->getCode() : 500; + + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; + + if ($isDbError) { + // Log the full error, but return minimal info for security + $ref = $e->getFile() . " / LN : " . $e->getLine(); + } else { + // This catches the \RuntimeException for "No Data found" (code 404) + $ref = "Application exception occurred"; + } + + // Return an error JSON response + return $this->response->setJSON([ + 'status' => 'error', + 'data' => [], // Return empty data on error + 'code' => $e->getCode(), + 'message' => $e->getMessage(), + 'ref' => $ref, + ])->setStatusCode($code); + } + + } + //Step 1.1 + public function insurerWise(){ + + $builder = $this->db->table('partner_policy pp'); + + $builder->select(" + DATE_FORMAT(CURRENT_DATE(), '%b') AS curr_month, + YEAR(CURRENT_DATE()) AS year_of_curr_month, + DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%b') AS pre_month, + YEAR(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH)) AS year_of_last_month, + i.name, + i.short_name, + i.id AS insurer_id, + pq.insurer_id AS PQ_insurer_id, + pq.insurer_id, + pp.insured_name, + + COUNT( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_current_month, + + SUM( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_current_month, + + COUNT( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_pre_month, + + SUM( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_pre_month + "); + + $builder->join('partner_quotation pq', 'pq.id = pp.quotation_id', 'left'); + $builder->join('insurers i', 'i.id = pq.insurer_id', 'left'); + + $builder->where(" + pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + ", null, false); + + $builder->where("pq.insurer_id != 0"); + $builder->groupBy('pq.insurer_id'); + $builder->orderBy('pq.insurer_id'); + + $query = $builder->get(); + $result = $query->getResultArray(); + return $result; + + } + + //Step 1.2 + public function brokerWise(){ + + $builder = $this->db->table('partner_policy pp'); + $builder->select(" + DATE_FORMAT(CURRENT_DATE(), '%b') AS curr_month, + YEAR(CURRENT_DATE()) AS year_of_curr_month, + DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%b') AS pre_month, + YEAR(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH)) AS year_of_last_month, + pb.name AS broker_name, + pb.id AS broker_id, + pe.broker_id AS PE_broker_id, + + COUNT( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_current_month, + + SUM( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_current_month, + + COUNT( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_pre_month, + + SUM( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_pre_month + "); + + $builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left'); + $builder->join('partner_brokers pb', 'pb.id = pe.broker_id', 'left'); + + $builder->where(" + pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + ", null, false); + + $builder->groupBy('pb.id'); + $builder->orderBy('pb.id'); + + $query = $builder->get(); + $result = $query->getResultArray(); + return $result; + } + + //Step 1.3 + public function productWise(){ + $builder = $this->db->table('partner_policy pp'); + $builder->select(" + DATE_FORMAT(CURRENT_DATE(), '%b') AS curr_month, + YEAR(CURRENT_DATE()) AS year_of_curr_month, + DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%b') AS pre_month, + YEAR(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH)) AS year_of_last_month, + pp.vehicle_type, + + COUNT( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_current_month, + + SUM( + CASE + WHEN MONTH(pp.created_on) = MONTH(CURRENT_DATE()) + AND YEAR(pp.created_on) = YEAR(CURRENT_DATE()) + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_current_month, + + COUNT( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.client_policy_id + ELSE NULL + END + ) AS total_policies_pre_month, + + SUM( + CASE + WHEN pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01') + THEN pp.premium_amount + ELSE 0 + END + ) AS total_premium_pre_month + "); + + $builder->where(" + pp.created_on >= DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + AND pp.created_on < DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), '%Y-%m-01') + ", null, false); + + $builder->groupBy('pp.vehicle_type'); + + $query = $builder->get(); + + $result = $query->getResultArray(); + return $result; + + } + + //Step 2.1 + public function agentMonthlyPolicyCount($customDate,$limit){ + + // 2.1 + // $customDate = $custom_date; // '2025-11-05' or CURRENT_DATE() + // $limit = 50; + + $builder = $this->db->table('partner_policy pp'); + + $builder->select(" + pp.agent_id AS PP_agent_id, + pa.id AS agent_id, + pa.name AS agent_name, + pa.agent_code, + COUNT(pp.id) AS policy_count + "); + + $builder->join('partner_agent pa', 'pp.agent_id = pa.id', 'inner'); + $builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left'); + + $builder->where("MONTH(pp.created_on) = MONTH('$customDate')", null, false); + $builder->where("YEAR(pp.created_on) = YEAR('$customDate')", null, false); + + $builder->groupBy(['pa.id']); + $builder->orderBy('policy_count', 'DESC'); + $builder->limit($limit); + + $result = $builder->get()->getResultArray(); + return $result; + + } + + //Step 2.2 + public function agentMonthlyPremiumAmount($customDate,$limit){ + + // 2.2 + // $customDate = $custom_date; // Example '2025-11-05' + // $limit = 50; + $builder = $this->db->table('partner_policy pp'); + + $builder->select(" + pp.agent_id AS PP_agent_id, + pa.id AS agent_id, + pa.name AS agent_name, + pa.agent_code, + SUM(pp.premium_amount) AS total_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("MONTH(pp.created_on) = MONTH('$customDate')", null, false); + $builder->where("YEAR(pp.created_on) = YEAR('$customDate')", null, false); + + // $builder->groupBy(['pa.id']); + $builder->orderBy('total_premium_amount', 'DESC'); + $builder->limit($limit); + $result = $builder->get()->getResultArray(); + return $result; + + } + + //Step 3 + public function agentNoPolicyTimeRange($value,$unit){ + + // $value = 10; // dynamic number (10, 50, 1, 2, 3, etc.) + // $unit = 'DAY'; // DAY, MONTH, YEAR (dynamic) + $interval = "DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL $value $unit)"; + + $builder = $this->db->table('partner_agent pa'); + $builder->select(' + pa.id AS agent_id, + pa.name AS agent_name, + pa.email, + pa.mobile, + pa.agent_code, + pa.is_active + '); + + $builder->join( + 'partner_policy pp', + "pa.id = pp.agent_id AND pp.created_on >= $interval", + 'left', + false // IMPORTANT → allows raw SQL + ); + + $builder->where('pp.id', null); + $builder->orderBy('pa.name'); + $result = $builder->get()->getResultArray(); + return $result; + + } + //Step 4 + public function agentBelow50tPremiumTimeRange($value,$unit){ + + // 4th + // $value = 10; // Dynamic (10, 50, 1, 2, 3...) + // $unit = 'DAY'; // DAY, MONTH, YEAR + $interval = "DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL $value $unit)"; + + + $builder = $this->db->table('partner_policy pp'); + + $builder->select(" + pp.premium_amount, + pp.agent_id AS PP_agent_id, + pa.id AS agent_id, + pa.name AS agent_name, + pa.agent_code AS agent_code, + CASE + WHEN pa.is_active = 1 THEN 'Active Agent' + ELSE 'Inactive Agent' + END AS agent_status, + DATE_FORMAT(pp.created_on, '%d-%m-%Y') AS created_date_ui_formatted, + pp.created_on AS created_date_sql_formatted + "); + + $builder->join( + 'partner_agent pa', + 'pa.id = pp.agent_id', + 'left' + ); + $builder->where('pp.premium_amount <', 50000); + $builder->where("pp.created_on >= $interval", null, false); // false = prevent escaping raw SQL + $builder->orderBy('pp.created_on', 'DESC'); + $result = $builder->get()->getResultArray(); + return $result; + + } } diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index d69ec3c..d24ddef 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -667,57 +667,7 @@ class EnquiryController extends ResourceController public function checkDuplicate() { - $request = $this->request->getGet(); - - // Input field => DB column mapping - $fieldMapping = [ - "reg_no" => "vehicle_no", // frontend → database - "mobile" => "mobile", - "email" => "email", - ]; - - $conditions = []; - $inputField = null; // what user sent - $dbField = null; // actual column - - // Identify which field user sent - foreach ($fieldMapping as $input => $column) { - if (!empty($request[$input])) { - $inputField = $input; - $dbField = $column; - $conditions[$column] = $request[$input]; - break; - } - } - - if (!$dbField) { - return $this->response->setJSON([ - 'status' => 'error', - 'message' => 'One of the following is required: email, reg_no, mobile' - ])->setStatusCode(400); - } - - // Query builder - $builder = $this->enquiryModel->where($dbField, $conditions[$dbField]); - - // Exclude this ID if provided - if (!empty($request['id'])) { - $builder->where('id !=', $request['id']); - } - - $enquiry = $builder->first(); - - if ($enquiry) { - return $this->response->setJSON([ - 'status' => 'exists', - 'field' => $inputField // return the field user sent - ]); - } else { - return $this->response->setJSON([ - 'status' => 'not', - 'field' => $inputField - ]); - } + $request = $this->request->getGet("reg_no"); }