From e547cf84bc330b43f2a0ef8b2e47dd4ae706268d Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Fri, 19 Dec 2025 16:48:03 +0530 Subject: [PATCH] FIX_BDS_REPORT --- app/Models/PolicyTransactionModel.php | 125 ++++++++++++++++++-------- 1 file changed, 88 insertions(+), 37 deletions(-) diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index f6822911..51105f12 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -3060,55 +3060,55 @@ // dd($this->db->getLastQuery()); - foreach ($result as $key => &$row) { + // foreach ($result as $key => &$row) { - // 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 - } + // // 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 + // } - // 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 rewards exist, zero the IRDA amount + // // if (($row['reward'] ?? 0) != 0.00) { + // // $row['total_irda_amt'] = '0.00'; + // // $row['billed_amt'] = $row['reward']; + // // } - $reward = (float) ($row['reward'] ?? 0); + // $reward = (float) ($row['reward'] ?? 0); - if ($reward > 0) { - $row['total_irda_amt'] = '0.00'; - $row['billed_amt'] = $reward; - } + // if ($reward > 0) { + // $row['total_irda_amt'] = '0.00'; + // $row['billed_amt'] = $reward; + // } - } - unset($row); + // } + // unset($row); - // $result = array_values($result); + // // $result = array_values($result); - usort($result, function ($a, $b) { + // usort($result, function ($a, $b) { - // First: group by policy_issue_date - if ($a['policy_issue_month'] === $b['policy_issue_month']) { + // // 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 - ]; + // // Priority order for action_type + // $priority = [ + // 'Policy' => 1, + // 'Endorsement' => 2, + // 'Rewards' => 3 + // ]; - $aPriority = $priority[$a['action_type']] ?? 99; - $bPriority = $priority[$b['action_type']] ?? 99; + // $aPriority = $priority[$a['action_type']] ?? 99; + // $bPriority = $priority[$b['action_type']] ?? 99; - return $aPriority <=> $bPriority; - } + // return $aPriority <=> $bPriority; + // } - // Otherwise sort by policy_issue_date - return strtotime($a['policy_issue_month']) <=> strtotime($b['policy_issue_month']); - }); + // // Otherwise sort by policy_issue_date + // return strtotime($a['policy_issue_month']) <=> strtotime($b['policy_issue_month']); + // }); // dd($result); @@ -3141,6 +3141,57 @@ // $result = array_values($filtered); // dd($result); + $keys = []; + $filtered = []; + + foreach ($result as $row) { + + // Normalize endorsement number + $endorsement_number = !empty($row['endorsement_no'])? $row['endorsement_no'] : '-'; + + // $month = $row['policy_issue_month']; + $reward = (float) ($row['reward'] ?? 0); + // $billed_amt = (float) ($row['billed_amt'] ?? 0); + + // if ( + // ($monthCount[$month] ?? 0) > 1 && + // $reward == 0.00 && + // $billed_amt == 0.00 + // ) { + // continue; + // } + + if ($reward > 0 && ($row['billed_amt'] ?? 0) == 0.00) { + $row['total_irda_amt'] = '0.00'; + $row['billed_amt'] = $reward; + $rewardFlag = 'R'; // Reward row + } else { + $rewardFlag = 'N'; // Normal row + } + + // ✅ Stable & deterministic key + $key = implode('|', [ + $row['statement_month'], + $row['insurer_name'], + $row['policy_no'], + $endorsement_number, + $rewardFlag + ]); + + // Prefer "statement uploaded" + if ($row['statement_uploaded'] === 'statement uploaded') { + $filtered[$key] = $row; + $keys[$key] = true; + } + // Keep no-statement only if uploaded not present + elseif (!isset($keys[$key])) { + $filtered[$key] = $row; + } + } + + // Reindex for output + $result = array_values($filtered); + // --------------------------------------------------------------------------------------------------------