diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index 52e6a89b..972ae908 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -1877,7 +1877,7 @@ return $result[0]; } - public function reportBDSNew1( + public function reportBDSNew( $start_date = 0, $end_date = 0, $client_id = 0, @@ -2370,11 +2370,11 @@ 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 = ''; + $statement_month_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 . "' + $statement_month_condition = " + WHERE statement_month >= '" . $start_date . "' + AND statement_month <= '" . $end_date . "' "; } @@ -2393,6 +2393,79 @@ } } + $conditions = ""; // start safely + + // 1. Role-based restrictions + 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())) { + $conditions .= " AND pt.created_by = " . get_session_userid(); + } + } + + // 2. Dynamic $where array + if (!empty($where)) { + foreach ($where as $column => $value) { + $value = addslashes($value); + $conditions .= " AND `$column` = '$value' "; + } + } + + // 3. Date condition (except statement_month) + 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)); + + if ($date_type === "policy_issue_date") { + $conditions .= " AND pcsd.pt_policy_issue_date >= '$startDate' "; + $conditions .= " AND pcsd.pt_policy_issue_date <= '$endDate' "; + } else { + $conditions .= " AND pt.$date_type >= '$startDate' "; + $conditions .= " AND pt.$date_type <= '$endDate' "; + } + } + + // 4. Common filters + if ($client_id != 0) { + $conditions .= " AND pt.client_id = $client_id "; + } + + if ($insurer_id != 0) { + $conditions .= " AND pt.insurer_id = $insurer_id "; + } + + if ($client_branch_id != 0) { + $conditions .= " AND pt.client_branch_id = $client_branch_id "; + } + + if ($insurer_branch_id != 0) { + $conditions .= " AND pt.insurer_branch_id = $insurer_branch_id "; + } + + if ($client_policy_id != 0) { + $conditions .= " AND pt.client_policy_id = $client_policy_id "; + } + + if ($user_id != 0) { + $conditions .= " AND pt.created_by = $user_id "; + } + + if ($policy_type_id != 0) { + $conditions .= " AND pt.policy_type_id = $policy_type_id "; + } + + if ($issuer != 0) { + $conditions .= " AND pt.issuer = $issuer "; + } + + $sql = " SELECT * FROM ( @@ -2422,6 +2495,8 @@ ELSE 'Endorsement' 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, @@ -2484,6 +2559,7 @@ WHERE pt.is_active = 1 AND pcsd.is_active = 1 $default_date_filter + $conditions GROUP BY pt.policy_no, pcsd.insurer_id @@ -2515,6 +2591,8 @@ ELSE 'Endorsement' 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, @@ -2618,14 +2696,18 @@ AND cssd.is_active = 1 AND insq.is_active = 1 $default_date_filter + $conditions GROUP BY pt.policy_no, pcsd.insurer_id, insq.month ) AS final_result - + + $statement_month_condition -- GROUP BY statement_month, insurer_name, policy_no + -- WHERE id = 6063 ORDER BY + id DESC, policy_no ASC, insurer_branch_name ASC, statement_uploaded ASC, @@ -2655,6 +2737,35 @@ $result = array_values($filtered); + $runningBilled = []; + $totalIrdaMap = []; + $final = []; + + foreach ($result as $row) { + $ptId = $row['pt_id']; + + // Store total_irda_amt only once (first non-zero value) + if (!isset($totalIrdaMap[$ptId]) && $row['total_irda_amt'] > 0) { + $totalIrdaMap[$ptId] = $row['total_irda_amt']; + } + + // Initialize running sum + if (!isset($runningBilled[$ptId])) { + $runningBilled[$ptId] = 0; + } + + // Add billed amount to running total + $runningBilled[$ptId] += (float)$row['billed_amt']; + + // Calculate unbilled amount + $row['unbilled_amount'] = ($totalIrdaMap[$ptId] ?? 0) - $runningBilled[$ptId] ; + + $final[] = $row; + } + + $result = $final; + + // print_rr($result); die; return $result; } diff --git a/app/Views/report_bds.php b/app/Views/report_bds.php index 3f876825..fc51a5c8 100644 --- a/app/Views/report_bds.php +++ b/app/Views/report_bds.php @@ -180,7 +180,12 @@ table.dataTable tbody td { } $unbilled_amt = $unbilled_amt == 0 && $row['billed_amt'] == 0 ? $total_irda_amt : $unbilled_amt ; ?> -