From 9f6dd050fdae6415812e0226048050e14800e66c Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Fri, 28 Nov 2025 15:06:36 +0530 Subject: [PATCH] CHANGE_NEW_BDS_REPORT --- .../PolicyTransactionController.php | 2 +- app/Models/PolicyTransactionModel.php | 743 ++++++++++++------ app/Views/report_bds.php | 7 +- 3 files changed, 526 insertions(+), 226 deletions(-) diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index e970d9b5..35425655 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -500,7 +500,7 @@ $data['calc_policy_issue_date'][$index] = change_date_format($data['calc_policy_issue_date'][$index]); } - $co_share_type_value = $data['co_share'] == 1 ? $data['co_share_type'][$index] ?? 1 : 1; + $co_share_type_value = (($data['co_share'] ?? 0) == 1) ? ($data['co_share_type'][$index] ?? 1) : 1; // Prepare each co-share detail entry $coShareDetails[] = [ diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index b5015dbd..52e6a89b 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -593,7 +593,7 @@ // return $result; // } - public function getBDSReportList($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 = []) + public function getBDSReportList1($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 = ''; @@ -652,7 +652,7 @@ 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_velmurugan, - ROUND(COALESCE(tep_gst_amt, 0) + COALESCE(bp_gst_amt, 0) + COALESCE(tp_amt, 0),2) AS gst_amount, + ROUND(COALESCE(tep_gst_amt, 0) + COALESCE(bp_gst_amt, 0) + COALESCE(tp_gst_amt, 0),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(COALESCE(tep_gst_amt, 0) + COALESCE(bp_gst_amt, 0) + COALESCE(tp_amt, 0),2), @@ -665,6 +665,7 @@ (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, + ROUND( ( SELECT @@ -686,6 +687,7 @@ ), 2 ) AS total_irda_amt, + ROUND( ( SELECT @@ -725,7 +727,6 @@ AND insurer_statements.is_active = 1 AND insurer_statements.invoice_status IS NOT NULL $date_condition - ), 2 ) AS billed_amt, @@ -772,7 +773,9 @@ ), 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 @@ -1874,7 +1877,7 @@ return $result[0]; } - public function reportBDSNew( + public function reportBDSNew1( $start_date = 0, $end_date = 0, $client_id = 0, @@ -1887,8 +1890,9 @@ $client_policy_id = 0, $user_id = 0, $where = [] - ) { - + ) + { + $date_condition = ''; // ============================== @@ -1896,9 +1900,9 @@ // ============================== if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) { $date_condition = " - AND insurer_statements_oq.month >= '{$start_date}' - AND insurer_statements_oq.month <= '{$end_date}' - "; + AND insurer_statements_oq.month >= '{$start_date}' + AND insurer_statements_oq.month <= '{$end_date}' + "; } // ============================== @@ -1913,92 +1917,92 @@ $builder2 = $this->db->table('policy_transaction') ->select(" - policy_transaction.id AS id, - policy_transaction.endorsement_no AS endorsement_no, - policy_transaction.ref AS ref, - DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, + policy_transaction.id AS id, + policy_transaction.endorsement_no AS endorsement_no, + policy_transaction.ref AS ref, + DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, - DATE_FORMAT( policy_transaction.policy_issue_date, '%b %Y') AS policy_issue_month, + DATE_FORMAT( policy_transaction.policy_issue_date, '%b %Y') AS policy_issue_month, - CASE - WHEN 1 = 1 THEN 'no statement uploaded' - ELSE 'statement uploaded' - END AS statement_uploaded , + CASE + WHEN 1 = 1 THEN 'no statement uploaded' + ELSE 'statement uploaded' + END AS statement_uploaded , - CASE - WHEN clients.client_type = 1 THEN 'Group' - WHEN clients.client_type = 2 THEN 'Retail' - ELSE '-' - END AS client_type, + 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.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, + 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, + 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.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, + 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 - -- Alias Agreed Amount ...!! - -- ============================== - ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt, + -- ============================== + -- SUBQUERY: Total IRDA Amount + -- Alias Agreed Amount ...!! + -- ============================== + ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt, - -- Reward Only - ROUND(0, 2) AS reward, + -- Reward Only + ROUND(0, 2) AS reward, - -- Billed Amount - ROUND(0, 2) AS billed_amt, + -- Billed Amount + ROUND(0, 2) AS billed_amt, - created_user.first_name AS user_name, + 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 - ") + 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 @@ -2016,15 +2020,16 @@ ->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') - + ->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) - // ======================================================== - // GROUP BY Insurer Statement Months - // ======================================================= - ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id '); + // ======================================================== + // GROUP BY Insurer Statement Months + // ======================================================= + // ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ') + ; // ============================== // ROLE-BASED FILTERS @@ -2055,7 +2060,7 @@ $endDate = date('Y-m-d 23:59:59', strtotime($end_date)); $builder2->where("policy_transaction.{$date_type} >=", $startDate) - ->where("policy_transaction.{$date_type} <=", $endDate); + ->where("policy_transaction.{$date_type} <=", $endDate); } @@ -2075,10 +2080,10 @@ // DEFAULT 90-DAY FILTER // ============================== if ( - $client_id == 0 && - $insurer_id == 0 && - $policy_type_id == 0 && - $date_type == 0 && + $client_id == 0 && + $insurer_id == 0 && + $policy_type_id == 0 && + $date_type == 0 && $issuer == 0 ) { $fromDate = date('Y-m-d', strtotime('-90 days')); @@ -2086,7 +2091,7 @@ if (empty($where)) { $builder2->where('policy_transaction.created_at >=', $fromDate) - ->where('policy_transaction.created_at <=', $toDate); + ->where('policy_transaction.created_at <=', $toDate); } } @@ -2116,123 +2121,123 @@ $builder = $this->db->table('policy_transaction') ->select(" - policy_transaction.id AS id, - policy_transaction.endorsement_no AS endorsement_no, - policy_transaction.ref AS ref, - DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, - DATE_FORMAT( - IF(insurer_statements_oq.month IS NULL, - policy_transaction.policy_issue_date, - insurer_statements_oq.month - ), '%b %Y' - ) AS policy_issue_month, + policy_transaction.id AS id, + policy_transaction.endorsement_no AS endorsement_no, + policy_transaction.ref AS ref, + DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, + DATE_FORMAT( + IF(insurer_statements_oq.month IS NULL, + policy_transaction.policy_issue_date, + insurer_statements_oq.month + ), '%b %Y' + ) AS policy_issue_month, - CASE - WHEN insurer_statements_oq.month IS NULL THEN 'no statement uploaded' - ELSE 'statement uploaded' - END AS statement_uploaded , + CASE + WHEN insurer_statements_oq.month IS NULL THEN 'no statement uploaded' + ELSE 'statement uploaded' + END AS statement_uploaded , - CASE - WHEN clients.client_type = 1 THEN 'Group' - WHEN clients.client_type = 2 THEN 'Retail' - ELSE '-' - END AS client_type, + 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.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, + 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, + 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.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, + 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 - -- Alias Agreed Amount ...!! - -- ============================== - ROUND(pt_co_share_details.exp_amt, 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 - AND insurer_statements.month = insurer_statements_oq.month - {$date_condition} - GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') - ), 2) AS reward, + -- ============================== + -- SUBQUERY: Total IRDA Amount + -- Alias Agreed Amount ...!! + -- ============================== + ROUND(pt_co_share_details.exp_amt, 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 + AND insurer_statements.month = insurer_statements_oq.month + {$date_condition} + GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') + ), 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 - AND insurer_statements.month = insurer_statements_oq.month - {$date_condition} - GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') - ), 2) AS billed_amt, + -- 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 + AND insurer_statements.month = insurer_statements_oq.month + {$date_condition} + GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') + ), 2) AS billed_amt, - created_user.first_name AS user_name, + 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 - ") + 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 @@ -2250,12 +2255,12 @@ ->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') - - - ->join('co_share_stmt_details', 'pt_co_share_details.id = co_share_stmt_details.co_share_id','left') - ->join('insurer_statements insurer_statements_oq','insurer_statements_oq.id = co_share_stmt_details.statement_id','left') - + ->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left') + + + ->join('co_share_stmt_details', 'pt_co_share_details.id = co_share_stmt_details.co_share_id', 'left') + ->join('insurer_statements insurer_statements_oq', 'insurer_statements_oq.id = co_share_stmt_details.statement_id', 'left') + ->where('policy_transaction.is_active', 1) ->where('pt_co_share_details.is_active', 1) ->where('co_share_stmt_details.is_active', 1) @@ -2263,10 +2268,11 @@ ->where('insurer_statements_oq.id IS NOT NULL') - // ======================================================== - // GROUP BY Insurer Statement Months - // ======================================================= - ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ,insurer_statements_oq.month'); + // ======================================================== + // GROUP BY Insurer Statement Months + // ======================================================= + // ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ,insurer_statements_oq.month') + ; // ============================== // ROLE-BASED FILTERS @@ -2297,7 +2303,7 @@ $endDate = date('Y-m-d 23:59:59', strtotime($end_date)); $builder->where("policy_transaction.{$date_type} >=", $startDate) - ->where("policy_transaction.{$date_type} <=", $endDate); + ->where("policy_transaction.{$date_type} <=", $endDate); } // JOIN FOR STATEMENT MONTH FILTER @@ -2306,11 +2312,11 @@ $endDate = date('Y-m-d', strtotime($end_date)); $builder->join('co_share_stmt_details cssdfilter', 'pt_co_share_details.id = cssdfilter.co_share_id', 'left') - ->join('insurer_statements', 'cssdfilter.statement_id = insurer_statements.id', 'left') - ->where('insurer_statements.is_active', 1) - ->where('insurer_statements.month >=', $startDate) - ->where('insurer_statements.month <=', $endDate) - ->groupBy('cssdfilter.co_share_id'); + ->join('insurer_statements', 'cssdfilter.statement_id = insurer_statements.id', 'left') + ->where('insurer_statements.is_active', 1) + ->where('insurer_statements.month >=', $startDate) + ->where('insurer_statements.month <=', $endDate) + ->groupBy('cssdfilter.co_share_id'); } // ============================== @@ -2329,10 +2335,10 @@ // DEFAULT 90-DAY FILTER // ============================== if ( - $client_id == 0 && - $insurer_id == 0 && - $policy_type_id == 0 && - $date_type == 0 && + $client_id == 0 && + $insurer_id == 0 && + $policy_type_id == 0 && + $date_type == 0 && $issuer == 0 ) { $fromDate = date('Y-m-d', strtotime('-90 days')); @@ -2340,7 +2346,7 @@ if (empty($where)) { $builder->where('policy_transaction.created_at >=', $fromDate) - ->where('policy_transaction.created_at <=', $toDate); + ->where('policy_transaction.created_at <=', $toDate); } } @@ -2351,18 +2357,309 @@ $sql1 = $builder2->getCompiledSelect(); $sql2 = $builder->getCompiledSelect(); - $finalSql = "($sql1) UNION ALL ($sql2) - ORDER BY policy_no DESC, insurer_branch_name ASC, statement_uploaded ASC, - STR_TO_DATE(policy_issue_month, '%b %Y') ASC"; + $finalSql = "($sql1) UNION ALL ($sql2) + ORDER BY policy_no DESC, insurer_branch_name ASC, statement_uploaded ASC, + STR_TO_DATE(policy_issue_month, '%b %Y') ASC"; $result = $this->db->query($finalSql)->getResultArray(); + // dd($this->db->getLastQuery()); + return $result; + } + + public function getBDSReportList($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 = ''; + 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 . "' + "; + } + + $default_date_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)) { + + $default_date_filter = " + AND pt.created_at >= '" . $fromDate . "' + AND pt.created_at <= '" . $toDate . "' + "; + } + } + + $sql = " + SELECT * FROM ( + + SELECT + pt.id AS id, + pt.endorsement_no, + pt.ref, + DATE_FORMAT(pt.policy_issue_date, '%d %b %Y') AS policy_issue_date, + DATE_FORMAT(pcsd.pt_policy_issue_date, '%b %Y') AS policy_issue_month, + pt.month as statement_month, + + 'no statement uploaded' AS statement_uploaded, + + CASE + WHEN c.client_type = 1 THEN 'Group' + WHEN c.client_type = 2 THEN 'Retail' + ELSE '-' + END AS client_type, + + CASE + WHEN pt.revenue_type = 'NA' THEN 'Fresh' + ELSE 'Renewal' + END AS revenue_type, + + CASE + WHEN pt.action_type = 'inception' THEN 'Policy' + ELSE 'Endorsement' + END AS action_type, + + c.client_name, + c.short_name AS client_short_name, + cb.branch_name AS client_branch_name, + cb.address1 AS client_address, + ptype.policy_type, + ptype.bap, + ins.name AS insurer_name, + ins.short_name AS insurer_short_name, + ib.branch_name AS insurer_branch_name, + ib.branch_code AS insurer_branch_code, + + created_user.first_name AS user_name, + v.vehicle_no, + tpa.name AS tpa_name, + + pcsd.remark AS remarks, + pcsd.cop_amt AS bp_amt, + pcsd.exp_amt, + pcsd.id AS pt_id, + + su.first_name AS salse_person_name, + se.first_name AS service_person_name, + + ROUND(pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt, 2) AS premium_wo_gst, + ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 0.18, 2) AS gst_amount, + ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 1.18, 2) AS total_premium, + + ROUND(pcsd.cotp_amt + pcsd.cotep_amt, 2) AS tp_or_ter, + DATEDIFF(pt.policy_end_date, CURDATE()) AS days, + (pcsd.agreed_tp_per + pcsd.agreed_tep_per) AS agreed_tp_or_ter_per, + pcsd.agreed_bp_per, + + ROUND(pcsd.exp_amt, 2) AS total_irda_amt, + 0.00 AS reward, + 0.00 AS billed_amt, + + CASE + WHEN pcsd.co_share_type IN (0,1) THEN pt.policy_no + WHEN pcsd.co_share_type > 1 THEN + IFNULL(NULLIF(pcsd.follower_policy_no,''), pt.policy_no) + ELSE pt.policy_no + END AS policy_no + + FROM policy_transaction pt + LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id + JOIN clients c ON c.id = pt.client_id + LEFT JOIN client_branch cb ON pt.client_branch_id = cb.id + LEFT JOIN client_policy cp ON pt.client_policy_id = cp.id + LEFT JOIN user_profiles up ON pt.created_by = up.id + LEFT JOIN vehicle v ON pt.vehicle_id = v.id + LEFT JOIN policy_type ptype ON pt.policy_type_id = ptype.id + LEFT JOIN insurers ins ON pcsd.insurer_id = ins.id + LEFT JOIN insurer_branch ib ON pcsd.insurer_branch_id = ib.id + LEFT JOIN tpa ON pt.tpa_id = tpa.id + LEFT JOIN tpa_branch tb ON pt.tpa_branch_id = tb.id + LEFT JOIN user_profiles su ON pt.sales_generated_by = su.id + LEFT JOIN user_profiles se ON pt.serviced_by = se.id + LEFT JOIN user_profiles created_user ON pt.created_by = created_user.id + + WHERE pt.is_active = 1 + AND pcsd.is_active = 1 + $default_date_filter + + GROUP BY pt.policy_no, pcsd.insurer_id + + UNION ALL + + SELECT + pt.id AS id, + pt.endorsement_no, + pt.ref, + DATE_FORMAT(pt.policy_issue_date, '%d %b %Y') AS policy_issue_date, + DATE_FORMAT(IF(insq.month IS NULL, pcsd.pt_policy_issue_date, insq.month),'%b %Y') AS policy_issue_month, + insq.month as statement_month, + + 'statement uploaded' AS statement_uploaded, + + CASE + WHEN c.client_type = 1 THEN 'Group' + WHEN c.client_type = 2 THEN 'Retail' + ELSE '-' + END AS client_type, + + CASE + WHEN pt.revenue_type = 'NA' THEN 'Fresh' + ELSE 'Renewal' + END AS revenue_type, + + CASE + WHEN pt.action_type = 'inception' THEN 'Policy' + ELSE 'Endorsement' + END AS action_type, + + c.client_name, + c.short_name AS client_short_name, + cb.branch_name AS client_branch_name, + cb.address1 AS client_address, + ptype.policy_type, + ptype.bap, + ins.name AS insurer_name, + ins.short_name AS insurer_short_name, + ib.branch_name AS insurer_branch_name, + ib.branch_code AS insurer_branch_code, + + CASE + WHEN + IFNULL(cssd.actual_tep_brokerage_amt,0) = 0 AND + IFNULL(cssd.actual_tp_brokerage_amt,0) = 0 AND + IFNULL(cssd.actual_bp_brokerage_amt,0) = 0 AND + IFNULL(cssd.reward,0) != 0 + THEN 'rewards' + ELSE created_user.first_name + END AS user_name, + + v.vehicle_no, + tpa.name AS tpa_name, + + pcsd.remark AS remarks, + pcsd.cop_amt AS bp_amt, + pcsd.exp_amt, + pcsd.id AS pt_id, + + su.first_name AS salse_person_name, + se.first_name AS service_person_name, + + ROUND(pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt,2) AS premium_wo_gst, + ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 0.18,2) AS gst_amount, + ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 1.18,2) AS total_premium, + + ROUND(pcsd.cotp_amt + pcsd.cotep_amt,2) AS tp_or_ter, + DATEDIFF(pt.policy_end_date, CURDATE()) AS days, + (pcsd.agreed_tp_per + pcsd.agreed_tep_per) AS agreed_tp_or_ter_per, + pcsd.agreed_bp_per, + + CASE + WHEN insq.month = pt.month THEN pcsd.exp_amt + ELSE 0 + END AS total_irda_amt_2, + + COALESCE(( + SELECT SUM(cs.reward) + FROM co_share_stmt_details cs + JOIN insurer_statements i ON cs.statement_id = i.id + WHERE cs.co_share_id = pcsd.id AND i.month = insq.month + AND i.is_active = 1 + AND cs.is_active = 1 + GROUP BY pt.policy_no, i.month, pcsd.insurer_id + ),0) AS reward, + + COALESCE(( + SELECT SUM( + COALESCE(cs.actual_bp_brokerage_amt,0) + + COALESCE(cs.actual_tp_brokerage_amt,0) + + COALESCE(cs.actual_tep_brokerage_amt,0) + + COALESCE(cs.reward,0) + ) + FROM co_share_stmt_details cs + JOIN insurer_statements i ON cs.statement_id = i.id + WHERE cs.co_share_id = pcsd.id + AND i.invoice_status IS NOT NULL + AND i.month = insq.month + AND i.is_active = 1 + AND cs.is_active = 1 + GROUP BY pt.policy_no, i.month, pcsd.insurer_id + ),0) AS billed_amt, + + CASE + WHEN pcsd.co_share_type IN (0,1) THEN pt.policy_no + WHEN pcsd.co_share_type > 1 + THEN IFNULL(NULLIF(pcsd.follower_policy_no,''), pt.policy_no) + ELSE pt.policy_no + END AS policy_no + + FROM policy_transaction pt + LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id + LEFT JOIN co_share_stmt_details cssd ON pcsd.id = cssd.co_share_id + LEFT JOIN insurer_statements insq ON cssd.statement_id = insq.id + JOIN clients c ON c.id = pt.client_id + LEFT JOIN client_branch cb ON pt.client_branch_id = cb.id + LEFT JOIN client_policy cp ON pt.client_policy_id = cp.id + LEFT JOIN user_profiles up ON pt.created_by = up.id + LEFT JOIN vehicle v ON pt.vehicle_id = v.id + LEFT JOIN policy_type ptype ON pt.policy_type_id = ptype.id + LEFT JOIN insurers ins ON pcsd.insurer_id = ins.id + LEFT JOIN insurer_branch ib ON pcsd.insurer_branch_id = ib.id + LEFT JOIN tpa ON pt.tpa_id = tpa.id + LEFT JOIN tpa_branch tb ON pt.tpa_branch_id = tb.id + LEFT JOIN user_profiles su ON pt.sales_generated_by = su.id + LEFT JOIN user_profiles se ON pt.serviced_by = se.id + LEFT JOIN user_profiles created_user ON pt.created_by = created_user.id + + WHERE pt.is_active = 1 + AND pcsd.is_active = 1 + AND cssd.is_active = 1 + AND insq.is_active = 1 + $default_date_filter + + GROUP BY pt.policy_no, pcsd.insurer_id, insq.month + + ) AS final_result + + -- GROUP BY statement_month, insurer_name, policy_no + + ORDER BY + policy_no ASC, + insurer_branch_name ASC, + statement_uploaded ASC, + statement_month ASC, + STR_TO_DATE(policy_issue_month, '%b %Y') ASC + "; + $query = $this->db->query($sql); + $result = $query->getResultArray(); + + $keys = []; + $filtered = []; + + foreach ($result as $row) { + $key = $row['statement_month'].'|'.$row['insurer_name'].'|'.$row['policy_no']; + + // If uploaded record exists, always keep it and override previous + if ($row['statement_uploaded'] === 'statement uploaded') { + $filtered[$key] = $row; // overwrite no-statement row if it exists + $keys[$key] = true; + + } + // Keep "no statement uploaded" only if uploaded one not added yet + elseif (!isset($keys[$key])) { + $filtered[$key] = $row; + } + } + + $result = array_values($filtered); return $result; - } + diff --git a/app/Views/report_bds.php b/app/Views/report_bds.php index 19e071c7..f1016e6f 100644 --- a/app/Views/report_bds.php +++ b/app/Views/report_bds.php @@ -79,7 +79,7 @@ table.dataTable tbody td { S. No - User + User Month Business Type Client Type @@ -123,7 +123,7 @@ table.dataTable tbody td { echo base_url('policy_tranction/endorsement/list') . '?pt_id=' . $row['id'] ; } ?>" class="mdi mdi-pencil" > - + @@ -175,6 +175,9 @@ table.dataTable tbody td { // REF : Velmurugan but he told handle in query // Date : 6/11/25 12:50 $unbilled_amt = $total_irda_amt - $row['billed_amt']; + if($total_irda_amt == "0.00"){ + $unbilled_amt = abs($unbilled_amt); + } $unbilled_amt = $unbilled_amt == 0 && $row['billed_amt'] == 0 ? $total_irda_amt : $unbilled_amt ; ?>