From 135a11da8356d4b68004345053f84bd89a3a642b Mon Sep 17 00:00:00 2001 From: vadivelJ96 Date: Fri, 17 Oct 2025 12:49:04 +0530 Subject: [PATCH] CHANGE_live_ui_fix_in_qcr - VADIVEL J 2025-10-17 --- app/Config/Routes.php | 1 + .../PolicyTransactionController.php | 112 +++++++ app/Models/PolicyTransactionModel.php | 303 ++++++++++++++++++ 3 files changed, 416 insertions(+) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 877ba46e..52b921dc 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -427,6 +427,7 @@ $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) { $routes->group("report", ["filter" => "authMVC"], function ($routes) { $routes->match(['get', 'post'],"list", "PolicyTransactionController::reportBDS"); + $routes->match(['get', 'post'],"listNew", "PolicyTransactionController::reportBDSNew"); $routes->get("report-varience-list", "PolicyTransactionController::reportVarience"); $routes->get("report-business-list", "PolicyTransactionController::reportBusinessList"); $routes->get("report-finance-list", "PolicyTransactionController::reportFinanceList"); diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index b8f21a84..39390a83 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -3168,4 +3168,116 @@ class PolicyTransactionController extends BaseController return [$policyList, $policyListByClient]; } + + +public function reportBDSNew() +{ + // ๐Ÿงญ Basic Page Info + $data['tab_name'] = 'BDS Report'; + $data['page_name'] = 'BDS Report'; + + // ๐Ÿ“‹ Dropdown Data + $data['issuer'] = [1 => 'JIBS', 2 => 'Nhance']; + $data['client_type'] = [1 => 'Group', 2 => 'Individual']; + $data['issuing_type'] = [1 => 'Fresh', 2 => 'Renewal', 3 => 'Roll Over']; + $data['policy_status'] = [ + 'pending' => 'Pending', + 'exported_to_insurer' => 'Exported to Insurer', + 'imported_from_insurer'=> 'Imported from Insurer', + 'exported_to_tpa' => 'Exported to TPA', + 'imported_from_tpa' => 'Imported from TPA', + 'completed' => 'Completed' + ]; + $data['invoice_status_array'] = [ + 'yet_to_generate' => 'Yet to Generate', + 'generated' => 'Generated', + 'send' => 'Send', + 'recived' => 'Recived', + ]; + $data['date_type'] = [ + 'policy_issue_date' => 'Policy Issue Date', + 'policy_start_date' => 'Policy Start Date', + 'policy_end_date' => 'Policy End Date', + 'data_received_date' => 'Data Received Date', + 'closure_date' => 'Closure Date', + 'statement_month' => 'Statement Month', + ]; + + // ๐Ÿข Fetch Active Data + $data['insurer'] = $this->insurerModel->where('is_active', 1)->findAll(); + $data['policy_types'] = $this->policyTypeModel->where('is_active', 1)->findAll(); + $data['clients'] = $this->clientModel->where('is_active', 1)->findAll(); + $data['users'] = $this->userModel->where('is_active', 1)->findAll(); + $data['policy_count'] = $this->policyTransactionModel->where('is_active', 1)->countAllResults(); + + // ๐Ÿ• Filters + $start_date = $this->request->getGet('start_date'); + $end_date = $this->request->getGet('end_date'); + $client_id = $this->request->getGet('client_id'); + $insurer_id = $this->request->getGet('insurer_id'); + $policy_type_id = $this->request->getGet('policy_type_id'); + $date_type = $this->request->getGet('date_type'); + $issuer = $this->request->getGet('issuer'); + $client_branch_id = $this->request->getGet('client_branch_id'); + $insurer_branch_id = $this->request->getGet('insurer_branch_id'); + $client_policy_id = $this->request->getGet('client_policy_id'); + $user_id = $this->request->getGet('user_id'); + + // Handle statement month range + if ($date_type == 'statement_month') { + $start_date = (string) date('Y-m-01', strtotime($start_date)); + $end_date = (string) date('Y-m-31', strtotime($end_date)); + } + + // Ensure default values + $start_date = $start_date ?: 0; + $end_date = $end_date ?: 0; + $client_id = $client_id ?: 0; + $insurer_id = $insurer_id ?: 0; + $policy_type_id = $policy_type_id ?: 0; + $date_type = $date_type ?: 0; + $issuer = $issuer ?: 0; + $client_branch_id = $client_branch_id ?: 0; + $insurer_branch_id = $insurer_branch_id ?: 0; + $client_policy_id = $client_policy_id ?: 0; + $user_id = $user_id ?: 0; + + // ๐Ÿงพ Handle POST requests (Dashboard filters) + if ($this->request->is('post')) { + $isFromDashboard = $this->request->getPost('is_dashboard'); + + if (!empty($isFromDashboard) && $isFromDashboard == 1) { + $ids = array_filter(explode(',', $this->request->getPost('ids'))); + + if (!empty($ids)) { + $idsStr = implode(',', array_map('intval', $ids)); // sanitize IDs + $where = "policy_transaction.id IN ($idsStr)"; + } else { + $where = []; // No valid IDs + } + } + } + + // ๐Ÿ“Š Fetch report data + $data['report_list'] = $this->policyTransactionModel->getBDSReportListNew( + $start_date, + $end_date, + $client_id, + $insurer_id, + $policy_type_id, + $date_type, + $issuer, + $client_branch_id, + $insurer_branch_id, + $client_policy_id, + $user_id, + $where ?? '' + ); + + // ๐Ÿงฉ Load View + $this->loadLayout('report_bds_filter', $data); +} + + + } diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index 2e935ead..0004a738 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -1806,4 +1806,307 @@ class PolicyTransactionModel extends Model return $result[0]; } + + public function getBDSReportListNew( + $start_date = 0, + $end_date = 0, + $client_id = 0, + $insurer_id = 0, + $policy_type_id = 0, + $date_type = 0, + $issuer = 0, + $client_branch_id = 0, + $insurer_branch_id = 0, + $client_policy_id = 0, + $user_id = 0, + $where = [] + ) { + + $date_condition = ''; + + // ============================== + // DATE FILTER FOR STATEMENT MONTH + // ============================== + if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) { + $date_condition = " + AND insurer_statements.month >= '{$start_date}' + AND insurer_statements.month <= '{$end_date}' + "; + } + + // ============================== + // BASE QUERY + // ============================== + $builder = $this->db->table('policy_transaction') + ->select(" + policy_transaction.*, + DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, + DATE_FORMAT( + IF(policy_transaction.month IS NULL, + policy_transaction.policy_issue_date, + policy_transaction.month + ), '%b %Y' + ) AS policy_issue_month, + + CASE + WHEN clients.client_type = 1 THEN 'Group' + WHEN clients.client_type = 2 THEN 'Retail' + ELSE '-' + END AS client_type, + + CASE + WHEN policy_transaction.revenue_type = 'NA' THEN 'Fresh' + ELSE 'Renewal' + END AS revenue_type, + + CASE + WHEN policy_transaction.action_type = 'inception' THEN 'Policy' + ELSE 'Endorsement' + END AS action_type, + + clients.client_name AS client_name, + clients.short_name AS client_short_name, + client_branch.branch_name AS client_branch_name, + client_branch.address1 AS client_address, + policy_type.policy_type, + policy_type.bap, + insurers.name AS insurer_name, + insurers.short_name AS insurer_short_name, + insurer_branch.branch_name AS insurer_branch_name, + insurer_branch.branch_code AS insurer_branch_code, + user_profiles.first_name AS user_name, + vehicle.vehicle_no, + tpa.name AS tpa_name, + pt_co_share_details.remark AS remarks, + pt_co_share_details.cop_amt AS bp_amt, + pt_co_share_details.exp_amt, + pt_co_share_details.id AS pt_id, + sales_user.first_name AS salse_person_name, + service_user.first_name AS service_person_name, + + ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS premium_wo_gst, + ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2) AS gst_amount, + ROUND( + ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) + + ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2), + 2) AS total_premium, + + ROUND(pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS tp_or_ter, + DATEDIFF(policy_transaction.policy_end_date, CURDATE()) AS days, + (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per) AS agreed_tp_or_ter_per, + pt_co_share_details.agreed_bp_per, + + -- ============================== + -- SUBQUERY: Total IRDA Amount + -- ============================== + ROUND(( + SELECT ( + SUM(co_share_stmt_details.actual_bp_brokerage_amt) + + SUM(co_share_stmt_details.actual_tp_brokerage_amt) + + SUM(co_share_stmt_details.actual_tep_brokerage_amt) + + SUM(co_share_stmt_details.reward) + ) + FROM co_share_stmt_details + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND insurer_statements.is_active = 1 + {$date_condition} + ), 2) AS total_irda_amt, + + -- Reward Only + ROUND(( + SELECT SUM(co_share_stmt_details.reward) + FROM co_share_stmt_details + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND insurer_statements.is_active = 1 + {$date_condition} + ), 2) AS reward, + + -- Billed Amount + ROUND(( + SELECT SUM( + COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.reward, 0) + ) + FROM co_share_stmt_details + JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND pt_table.is_active = 1 + AND insurer_statements.is_active = 1 + AND insurer_statements.invoice_status IS NOT NULL + {$date_condition} + ), 2) AS billed_amt, + + -- Unbilled Amount + ROUND( + ( + ( + SELECT + SUM( + COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.reward, 0) + ) + FROM co_share_stmt_details + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + {$date_condition} + ) + - + ( + SELECT + SUM( + COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.reward, 0) + ) + FROM co_share_stmt_details + JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND pt_table.is_active = 1 + AND insurer_statements.is_active = 1 + AND insurer_statements.invoice_status IS NULL + {$date_condition} + ) + ), 2) AS unbilled_amt, + + created_user.first_name AS user_name, + + CASE + WHEN pt_co_share_details.co_share_type IN (0, 1) THEN policy_transaction.policy_no + WHEN pt_co_share_details.co_share_type > 1 THEN + CASE + WHEN pt_co_share_details.follower_policy_no IS NULL OR pt_co_share_details.follower_policy_no = '' + THEN policy_transaction.policy_no + ELSE pt_co_share_details.follower_policy_no + END + ELSE policy_transaction.policy_no + END AS policy_no + ") + + // ============================== + // JOINS + // ============================== + ->join('pt_co_share_details', 'policy_transaction.id = pt_co_share_details.pt_id', 'left') + ->join('clients', 'clients.id = policy_transaction.client_id') + ->join('client_branch', 'policy_transaction.client_branch_id = client_branch.id', 'left') + ->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left') + ->join('user_profiles', 'policy_transaction.created_by = user_profiles.id', 'left') + ->join('vehicle', 'policy_transaction.vehicle_id = vehicle.id', 'left') + ->join('policy_type', 'policy_transaction.policy_type_id = policy_type.id', 'left') + ->join('insurers', 'pt_co_share_details.insurer_id = insurers.id', 'left') + ->join('insurer_branch', 'pt_co_share_details.insurer_branch_id = insurer_branch.id', 'left') + ->join('tpa', 'policy_transaction.tpa_id = tpa.id', 'left') + ->join('tpa_branch', 'policy_transaction.tpa_branch_id = tpa_branch.id', 'left') + ->join('user_profiles AS sales_user', 'policy_transaction.sales_generated_by = sales_user.id', 'left') + ->join('user_profiles AS service_user', 'policy_transaction.serviced_by = service_user.id', 'left') + ->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left') + ->where('policy_transaction.is_active', 1) + ->where('pt_co_share_details.is_active', 1); + + // ============================== + // ROLE-BASED FILTERS + // ============================== + if ( + (!in_array(get_role_id(), [1, 5])) && + !( + in_array(MANAGEMENT_TEAM_ID, user_team()) || + in_array(FINANCE_TEAM_ID, user_team()) || + in_array(BUSINESS_TEAM_ID, user_team()) + ) + ) { + if (get_role_id() == 4 && in_array(POS_TEAM_ID, user_team())) { + $builder->where('policy_transaction.created_by', get_session_userid()); + } + } + + // ============================== + // ADDITIONAL FILTERS + // ============================== + if (!empty($where)) { + log_message('info', 'Where condition: ' . json_encode($where)); + $builder->where($where); + } + + if ($start_date != 0 && $end_date != 0 && $date_type != 0 && $date_type != 'statement_month') { + $startDate = date('Y-m-d 00:00:00', strtotime($start_date)); + $endDate = date('Y-m-d 23:59:59', strtotime($end_date)); + + $builder->where("policy_transaction.{$date_type} >=", $startDate) + ->where("policy_transaction.{$date_type} <=", $endDate); + } + + // JOIN FOR STATEMENT MONTH FILTER + if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) { + $startDate = date('Y-m-d', strtotime($start_date)); + $endDate = date('Y-m-d', strtotime($end_date)); + + $builder->join('co_share_stmt_details', 'pt_co_share_details.id = co_share_stmt_details.co_share_id', 'left') + ->join('insurer_statements', 'co_share_stmt_details.statement_id = insurer_statements.id', 'left') + ->where('insurer_statements.is_active', 1) + ->where('insurer_statements.month >=', $startDate) + ->where('insurer_statements.month <=', $endDate) + ->groupBy('co_share_stmt_details.co_share_id'); + } + + // ============================== + // FILTER BY IDs + // ============================== + if ($client_id != 0) $builder->where('policy_transaction.client_id', $client_id); + if ($insurer_id != 0) $builder->where('policy_transaction.insurer_id', $insurer_id); + if ($client_branch_id != 0) $builder->where('policy_transaction.client_branch_id', $client_branch_id); + if ($insurer_branch_id != 0) $builder->where('policy_transaction.insurer_branch_id', $insurer_branch_id); + if ($client_policy_id != 0) $builder->where('policy_transaction.client_policy_id', $client_policy_id); + if ($user_id != 0) $builder->where('policy_transaction.created_by', $user_id); + if ($policy_type_id != 0) $builder->where('client_policy.policy_type_id', $policy_type_id); + if ($issuer != 0) $builder->where('policy_transaction.issuer', $issuer); + + // ============================== + // DEFAULT 90-DAY FILTER + // ============================== + if ( + $client_id == 0 && + $insurer_id == 0 && + $policy_type_id == 0 && + $date_type == 0 && + $issuer == 0 + ) { + $fromDate = date('Y-m-d', strtotime('-90 days')); + $toDate = date('Y-m-d 23:59:59'); + + if (empty($where)) { + $builder->where('policy_transaction.created_at >=', $fromDate) + ->where('policy_transaction.created_at <=', $toDate); + } + } + + // ============================== + // ORDER & EXECUTION + // ============================== + $builder->orderBy('policy_transaction.id', 'desc'); + + $result = $builder->get()->getResultArray(); + + // Uncomment if you need to debug SQL + // dd($this->db->getLastQuery()); + + return $result; + } + + + + + }