From ce8308c898dcd17df5fa5c39316170e7ca205b4b Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Wed, 16 Jul 2025 22:24:43 +0530 Subject: [PATCH] FIX_CD_DEPOSIT_SUMMARY_AND_TRANSACTION : RV --- app/Controllers/EmployeeRestController.php | 5 +- app/Models/ClientPolicyModel.php | 53 +++++++++++++++++----- app/Views/client_deposit_list.php | 5 +- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 78e99a86..d8ec8b0e 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -2406,8 +2406,9 @@ class EmployeeRestController extends AdminController $temp['cd_ac_pk'] = $value->cd_ac_pk; $temp['insurer_name'] = $value->insurer_name; $temp['cd_master_account_no'] = $value->cd_master_account_no; - if (isset($result['balances'][$temp['insurer_id']])) { - $balance = $result['balances'][$temp['insurer_id']]->balance; + + if (isset($result['balances'][$temp['insurer_id'].'-'.$temp['cd_ac_pk']])) { + $balance = $result['balances'][$temp['insurer_id'].'-'.$temp['cd_ac_pk']]->balance; $temp['balance'] = $balance; } else { $temp['balance'] = "N/A"; diff --git a/app/Models/ClientPolicyModel.php b/app/Models/ClientPolicyModel.php index f571659e..1ad8e8bb 100755 --- a/app/Models/ClientPolicyModel.php +++ b/app/Models/ClientPolicyModel.php @@ -295,11 +295,23 @@ class ClientPolicyModel extends Model public function getDepositSummary($clientId, $insurerId, $cd_ac_pk) { + + $subQuery = $this->db->table('cash_deposit') + ->select('balance') + ->where('client_id', $clientId) + ->where('insurer_id', $insurerId) + ->where('cd_ac_pk', $cd_ac_pk) + ->where('is_active', 1) + ->orderBy('id', 'DESC') + ->limit(1) + ->getCompiledSelect(); + // Fetch the sum of credit and debit transactions and calculate the balance - return $this->db->table('cash_deposit') + $data = $this->db->table('cash_deposit') + ->select("($subQuery) AS balance", false) ->select('SUM(CASE WHEN transaction_type = "Credit" THEN amount ELSE 0 END) AS total_credit') ->select('SUM(CASE WHEN transaction_type = "Debit" THEN amount ELSE 0 END) AS total_withdraw') - ->select('SUM(CASE WHEN transaction_type = "Credit" THEN amount ELSE -amount END) AS balance') + // ->select('SUM(CASE WHEN transaction_type = "Credit" THEN amount ELSE -amount END) AS balance') ->select('SUM(CASE WHEN sub_type = 3 THEN amount ELSE 0 END) AS total_refund') ->where('client_id', $clientId) ->where('insurer_id', $insurerId) @@ -307,6 +319,9 @@ class ClientPolicyModel extends Model ->where('cash_deposit.is_active', 1) ->get() ->getRow(); + + // dd($this->db->getLastQuery()); + return $data; } // Inside your clientPolicyModel // Inside your clientPolicyModel @@ -347,7 +362,8 @@ class ClientPolicyModel extends Model foreach ($depositsummary as $summary) { $insurerId = $summary->insurer_id; - $balances[$insurerId] = $summary; + $cd_ac_pk = $summary->cd_ac_pk; + $balances[$insurerId . '-'. $cd_ac_pk] = $summary; } return $balances; @@ -366,20 +382,35 @@ class ClientPolicyModel extends Model // ->get() // ->getResult(); + // $sql = " + // SELECT cd.insurer_id, cd.cd_ac_pk, cd.balance + // FROM cash_deposit cd + // INNER JOIN ( + // SELECT insurer_id, MAX(id) AS max_id + // FROM cash_deposit + // WHERE client_id = ? + // AND is_active = 1 + // GROUP BY insurer_id + // ) latest ON cd.insurer_id = latest.insurer_id AND cd.id = latest.max_id + // ORDER BY cd.id DESC + // "; + $sql = " SELECT cd.insurer_id, cd.cd_ac_pk, cd.balance FROM cash_deposit cd - INNER JOIN ( - SELECT insurer_id, MAX(id) AS max_id + JOIN cd_master cdm ON cdm.id = cd.cd_ac_pk + JOIN ( + SELECT MAX(id) AS max_id FROM cash_deposit - WHERE client_id = ? - AND is_active = 1 - GROUP BY insurer_id - ) latest ON cd.insurer_id = latest.insurer_id AND cd.id = latest.max_id - ORDER BY cd.id DESC + WHERE is_active = 1 AND client_id = $id + GROUP BY insurer_id, cd_ac_pk + ) latest ON latest.max_id = cd.id + JOIN insurers on cd.insurer_id = insurers.id + WHERE cd.is_active = 1 AND cd.client_id = $id AND cdm.is_active = 1 + ORDER BY cd.insurer_id; "; - return $this->db->query($sql, [$id])->getResult(); + return $this->db->query($sql)->getResult(); } diff --git a/app/Views/client_deposit_list.php b/app/Views/client_deposit_list.php index ab076ebb..18f9fc29 100755 --- a/app/Views/client_deposit_list.php +++ b/app/Views/client_deposit_list.php @@ -38,9 +38,10 @@ ₹ insurer_id; + $cd_ac_pk = $value->cd_ac_pk; // Check if the balance information exists for the insurer - if (isset($balances[$insurerId])) { - $balance = $balances[$insurerId]->balance; + if (isset($balances[$insurerId . '-'. $cd_ac_pk])) { + $balance = $balances[$insurerId . '-'. $cd_ac_pk]->balance; echo $balance; } else { echo "N/A"; // Display "Not Available" if balance information is not present