diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index a6e465d3..000d9931 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -5033,7 +5033,7 @@ $policy_transaction_data = [ 'issuer' => 2, - 'issuer_branch' => $current_nhance_branch['id'] ?? null, + 'issuer_branch' => $salse['nhance_branch_id'] ?? null, 'client_id' => $client_id, 'policy_type_id' => 8, 'insurer_id' => $current_insurer_branch_data['insurer_id'] ?? null, @@ -5053,10 +5053,12 @@ 'month' => change_date_format($policy_issue_date, 'd/M/Y', 'Y-m-d'), 'sales_generated_by' => $salse['id'] ?? null, 'serviced_by' => $service['id'] ?? null, + 'salse_person_manager_id' => $salse['rm_id'] ?? null, + 'service_person_manager_id' => $service['rm_id'] ?? null, + 'service_person_branch_id' => $service['nhance_branch_id'] ?? null, 'agent_id' => $current_agent_data['id'] ?? null, 'agent_code' => $current_agent_data['agent_code'] ?? null, 'file_id' => $params['file_id'], - 'endorsement_no' => null, 'client_branch_id' => null, 'client_policy_id' => null, diff --git a/app/Helpers/excel_util_helper.php b/app/Helpers/excel_util_helper.php index 918a04c3..f1269f3d 100755 --- a/app/Helpers/excel_util_helper.php +++ b/app/Helpers/excel_util_helper.php @@ -3041,6 +3041,11 @@ if (!function_exists('validate_indian_vehicle_number')) { function validate_indian_vehicle_number($number) { $number = strtoupper(trim($number)); + $new_vehicle = strtolower(trim($number)); + + if($new_vehicle == "new"){ + return ['status' => true, 'error' => null]; + } // Normal Format: // 2 letters (state) + 2 digits (district) + 1 or 2 letters (series) + 4 digits @@ -3120,9 +3125,18 @@ if (!function_exists('check_agent_exist')) { if (!function_exists('check_rto_data')) { function check_rto_data($row, $rto_master) - { + { // Vehicle number from uploaded row $vehicle_no = strtoupper(trim($row[2])); // Example: TN10AB1234 + $new_vehicle = strtolower(trim($row[2])); + + if($new_vehicle == "new"){ + return [ + 'status' => true, + 'error' => null, + 'rto_data' => [] + ]; + } // Must be at least 4 characters to extract RTO if (strlen($vehicle_no) < 4) { diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index 97798b57..f6822911 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -2716,20 +2716,10 @@ END AS revenue_type, 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' - - WHEN pt.action_type = 'inception' - THEN 'Policy' - + WHEN pt.action_type = 'inception' THEN 'Policy' ELSE 'Endorsement' END AS action_type, - pt.action_type as action_type_string, c.client_name, @@ -2770,22 +2760,13 @@ 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, + 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) + COALESCE(cs.actual_tep_brokerage_amt,0) ) FROM co_share_stmt_details cs JOIN insurer_statements i ON cs.statement_id = i.id @@ -2886,11 +2867,184 @@ GROUP BY pt.policy_no, pt.endorsement_no, pcsd.insurer_id, insq.month + + UNION ALL + + SELECT + + pt.id AS id, + pt.endorsement_no, + pt.ref, + + pt.data_received_date, + pt.renewal_date, + pt.installment, + nhance_branch.branch_name as nhance_branch, + + 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 IFNULL(cssd.reward,0) <> 0 THEN 'Rewards' + ELSE 'Rewards' + END AS action_type, + + + pt.action_type as action_type_string, + + 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.id AS insurer_id, + 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, + + 0 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, + + 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, + + CASE + WHEN pt.co_share = 1 THEN 'Yes' + ELSE 'No' + END AS co_share, + + CASE + WHEN pt.bro_payable_by = 1 THEN 'Yes' + ELSE 'No' + END AS bro_payable_by, + + pcsd.non_comm_per_amt, + + pcsd.bp_igst, + pcsd.bp_sgst, + pcsd.bp_cgst, + + pcsd.agreed_bp_per, + ROUND(pcsd.agreed_tp_per + pcsd.agreed_tep_per,2) AS agreed_tp_per, + + pcsd.standerd_bp_per, + ROUND(pcsd.standerd_tp_per + pcsd.standerd_tep_per,2) AS standerd_tp_per, + + cssd.actual_bp_amt, + ROUND(cssd.actual_tp_amt + cssd.actual_tep_amt,2) AS actual_tp_amt, + + cssd.actual_bp_per, + ROUND(cssd.actual_tp_per + cssd.actual_tep_per,2) AS actual_tp_per, + + cssd.actual_tep_brokerage_amt, + ROUND(cssd.actual_tp_brokerage_amt + cssd.actual_bp_brokerage_amt,2) AS actual_tp_brokerage_amt, + + service_branch.branch_name as service_branch, + + salse_manager.first_name as salse_manager_name, + service_manager.first_name as service_manager_name, + cd_master.cd_ac_no, + pt.rollover_date, + pt.policy_holder_name, + + CASE + WHEN pt.same_as_proposer = 1 THEN 'Yes' + ELSE 'No' + END AS same_as_proposer, + + pcsd.follower_policy_no, + pcsd.co_share_per, + pcsd.stamp_duty + + 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 + LEFT JOIN nhance_branch ON pt.issuer_branch = nhance_branch.id + + LEFT JOIN nhance_branch service_branch ON pt.service_person_branch_id = service_branch.id + LEFT JOIN user_profiles salse_manager ON pt.salse_person_manager_id = salse_manager.id + LEFT JOIN user_profiles service_manager ON pt.service_person_manager_id = service_manager.id + LEFT JOIN cd_master ON pt.cd_ac_pk = cd_master.id + + WHERE pt.is_active = 1 + AND pcsd.is_active = 1 + AND cssd.is_active = 1 + AND insq.is_active = 1 + $default_date_filter + $conditions + ) AS final_result $statement_month_condition -- GROUP BY statement_month, insurer_name, policy_no - -- WHERE id = 6063 + -- WHERE id = 6143 ORDER BY id DESC, @@ -2905,33 +3059,91 @@ // dd($result); // dd($this->db->getLastQuery()); - $keys = []; - $filtered = []; - foreach ($result as $row) { + foreach ($result as $key => &$row) { - $endorsement_number = "-"; - if(!empty($row['endorsement_no'])){ - $endorsement_number = $row['endorsement_no']; + // If both rewards and billed amount are zero, remove the row + if ( + ($row['reward'] ?? 0) == 0.00 && + ($row['billed_amt'] ?? 0) == 0.00 + ) { + unset($result[$key]); // ✅ correct way } - $key = $row['statement_month'].'|'.$row['insurer_name'].'|'.$row['policy_no'] . '|' . $endorsement_number; + // If rewards exist, zero the IRDA amount + // if (($row['reward'] ?? 0) != 0.00) { + // $row['total_irda_amt'] = '0.00'; + // $row['billed_amt'] = $row['reward']; + // } - // 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; + $reward = (float) ($row['reward'] ?? 0); - } - // Keep "no statement uploaded" only if uploaded one not added yet - elseif (!isset($keys[$key])) { - $filtered[$key] = $row; + if ($reward > 0) { + $row['total_irda_amt'] = '0.00'; + $row['billed_amt'] = $reward; } + } + unset($row); + + // $result = array_values($result); + + usort($result, function ($a, $b) { + + // First: group by policy_issue_date + if ($a['policy_issue_month'] === $b['policy_issue_month']) { + + // Priority order for action_type + $priority = [ + 'Policy' => 1, + 'Endorsement' => 2, + 'Rewards' => 3 + ]; + + $aPriority = $priority[$a['action_type']] ?? 99; + $bPriority = $priority[$b['action_type']] ?? 99; + + return $aPriority <=> $bPriority; + } + + // Otherwise sort by policy_issue_date + return strtotime($a['policy_issue_month']) <=> strtotime($b['policy_issue_month']); + }); - $result = array_values($filtered); // dd($result); + // -------------------------------------------------------------------------------------------------------- + + // $keys = []; + // $filtered = []; + + // foreach ($result as $row) { + + // $endorsement_number = "-"; + // if(!empty($row['endorsement_no'])){ + // $endorsement_number = $row['endorsement_no']; + // } + + // $key = $row['statement_month'].'|'.$row['insurer_name'].'|'.$row['policy_no'] . '|' . $endorsement_number; + + // // 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); + // dd($result); + + // -------------------------------------------------------------------------------------------------------- + + // $runningBilled = []; // $totalIrdaMap = []; // $final = []; @@ -2958,6 +3170,8 @@ // $final[] = $row; // } + // -------------------------------------------------------------------------------------------------------- + $totalBilled = []; $totalIrdaMap = []; @@ -2974,6 +3188,8 @@ } } + // dd($totalBilled, $totalIrdaMap); + $ptSeen = []; $final = []; @@ -2982,12 +3198,12 @@ $ptId = $row['pt_id'].'-'.$row['insurer_id']; if (!isset($ptSeen[$ptId])) { // First entry → set unbilled amount - $row['unbilled_amount'] = ($totalIrdaMap[$ptId] ?? 0) - ($totalBilled[$ptId] ?? 0); + $row['unbilled_amount'] = (float) (($totalIrdaMap[$ptId] ?? 0) - ($totalBilled[$ptId] ?? 0)) ?? '0.00'; $ptSeen[$ptId] = true; } else { // Other entries → zero - $row['unbilled_amount'] = 0; + $row['unbilled_amount'] = '0.00'; } $final[] = $row; @@ -2995,7 +3211,7 @@ $result = $final; - // print_rr($result); die; + // dd($result); die; return $result; } diff --git a/app/Views/report_bds.php b/app/Views/report_bds.php index 1d459049..15cd5a00 100644 --- a/app/Views/report_bds.php +++ b/app/Views/report_bds.php @@ -211,7 +211,8 @@ table.dataTable tbody td { $exp_amt = isset($row['exp_amt']) && $row['exp_amt'] !== '' ? (float)$row['exp_amt'] : 0; // $max_amount = max($irda_amt, $exp_amt); // $total_irda_amt = number_format($max_amount, 2, '.', ''); - $total_irda_amt = empty($row['total_irda_amt']) ? $row['exp_amt'] : $row['total_irda_amt']; + // $total_irda_amt = empty($row['total_irda_amt']) ? $row['exp_amt'] : $row['total_irda_amt']; + $total_irda_amt = $row['total_irda_amt'] ?? '0.00'; ?> @@ -232,7 +233,7 @@ table.dataTable tbody td { @@ -691,7 +692,8 @@ $(document).ready(function() { $('#total_premium').text(totalPremium.toFixed(2)); $('#total_rewards').text(totalRewards.toFixed(2)); $('#total_irda').text(totalIrda.toFixed(2)); - $('#total_revenue').text(totalRevenue.toFixed(2)); + // $('#total_revenue').text(totalRevenue.toFixed(2)); + $('#total_revenue').text(totalIrda.toFixed(2)); $('#total_billed').text(totalBilled.toFixed(2)); // $('#total_unbilled').text(totalUnbilled.toFixed(2)); var totalUnbilled = getUniqueUnbilled(27);