diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 21f0afa5..71bf3c98 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -4776,6 +4776,15 @@ class ClientController extends AdminController // dd($finalJson); + // $baseWhere = [ + // 'client_id' => 159, + // 'client_policy_id' => 336, + // 'insurer_id' => 1, + // 'cd_ac_pk' => 56, + // 'event_name' => "addition", + // ]; + // $return_value = check_cd_entry_exist($baseWhere); + // dd($return_value); } diff --git a/app/Controllers/EmpDataServiceController.php b/app/Controllers/EmpDataServiceController.php index 76d44b73..c7f9d689 100755 --- a/app/Controllers/EmpDataServiceController.php +++ b/app/Controllers/EmpDataServiceController.php @@ -4105,6 +4105,9 @@ class EmpDataServiceController extends BaseController AND emp_endorsement.pk IN (" . implode(',', $arrayData['employeeIds']) . ") AND employee_polices.claim_status = 0 AND emp_endorsement.field_name = 'date_of_exit' + AND emp_endorsement.is_active = 1 + AND emp_endorsement.actions = 'd' + AND emp_endorsement.status != 'truncated'; ")->getRow(); // dd(db_connect()->getLastQuery(), $amount); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 4e1271f8..02409c2c 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -1422,11 +1422,12 @@ class MasterController extends AdminController if ($insert) { //for CD Tranction Table + $cd_tranction_data['cd_ac_pk'] = $this->CDMasterModel->insertID(); $response = DepositHelper::saveDeposit($cd_tranction_data, $loggedInUserID); $cd_data = $this->CDMasterModel->where('client_id', $data['client_id'])->where('insurer_id', $data['insurer_id'])->findAll(); - return $this->respond(['status' => true, 'data' => $cd_data, 'cd_ac_no'=>$data['cd_ac_no'], 'message' => 'CD Account number created successfully'], 200); + return $this->respond(['status' => true, 'data' => $cd_data, 'cd_ac_no'=>$data['cd_ac_no'], 'message' => 'CD Account number created successfully', "FOR BDS PURPOSE"], 200); }else{ return $this->respond(['status' => false, 'message' => 'Failed to created CD Account number'], 200); } diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 3f7f36df..a4721770 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -306,6 +306,12 @@ class PolicyTransactionController extends BaseController $data['policy_with_corr'] = 1; } + if (!isset($data['is_cd_reduce_from_bds'])) { + $data['is_cd_reduce_from_bds'] = 0; + } elseif ($data['is_cd_reduce_from_bds']) { + $data['is_cd_reduce_from_bds'] = 1; + } + if($data['ct_type'] == ""){ $data['ct_type'] = 1; } @@ -342,7 +348,7 @@ class PolicyTransactionController extends BaseController $this->policyTransactionModel->update($insert, ['client_policy_id' => $client_policy_id]); $emp_policy_insert = $this->InsertIndividualEmpPolicyTable($emp_data, $client_policy_id); - if ($data['client_type'] == 1 && $data['status'] == 'completed') { + if ($data['client_type'] == 1 && $data['status'] == 'completed' && $data['is_cd_reduce_from_bds'] == 1) { $this->processCompletedStatus($data, $client_policy_id, $data['insurer_id']); } } @@ -362,6 +368,7 @@ class PolicyTransactionController extends BaseController private function updateInceptionPolicy($id, $data) { + // print_r($data); die; if ($this->policyTransactionModel->update($id, $data)) { $this->insertTransactionStatus($id, $data, 1); @@ -391,7 +398,7 @@ class PolicyTransactionController extends BaseController } if ($data['status'] == 'completed' && $data['ct_type'] == 2) { - if($data['client_type'] == 1){ + if($data['client_type'] == 1 && $data['is_cd_reduce_from_bds'] == 1){ $this->processCompletedStatus($data, $data['client_policy_id'], $data['insurer_id']); } } @@ -426,7 +433,12 @@ class PolicyTransactionController extends BaseController foreach ($data['follow_insurer_id'] as $index => $insurer) { // Separate the insurer and insurer branch - list($insurer_branch_id, $insurer_id) = explode('-', $insurer); + if(isset($insurer) && !empty($insurer)){ + list($insurer_branch_id, $insurer_id) = explode('-', $insurer); + }else{ + $insurer_branch_id = null; + $insurer_id = null; + } // Prepare each co-share detail entry $coShareDetails[] = [ @@ -547,7 +559,7 @@ class PolicyTransactionController extends BaseController private function processCompletedStatus($data, $client_policy_id, $insurer_id) { $totalAmount = (int)$data['total'][0] ?? 0; - $description = 'The following amount of Rs. ' . $totalAmount . '/- has been debited for the ' . $data['emp_count'] . ' employees at Inception.'; + $description = 'The following amount of Rs. ' . $totalAmount . '/- has been debited for the ' . $data['emp_count'] . ' employees at Inception (BDS).'; $cdTransactionData = [ 'amount' => $totalAmount, @@ -562,8 +574,9 @@ class PolicyTransactionController extends BaseController 'updated_by' => get_session_userid(), 'event_name' => 'inception', 'is_active' => 1, + 'cd_ac_pk' => $data['cd_ac_pk'] ]; - + DepositHelper::saveDeposit($cdTransactionData, get_session_userid()); } @@ -987,7 +1000,6 @@ class PolicyTransactionController extends BaseController } - //------------------------------------------------------------------------------------------------ // Policy Transaction Endorsement @@ -1102,6 +1114,12 @@ class PolicyTransactionController extends BaseController $data['policy_with_corr'] = 1; } + if (!isset($data['is_cd_reduce_from_bds'])) { + $data['is_cd_reduce_from_bds'] = 0; + } elseif ($data['is_cd_reduce_from_bds']) { + $data['is_cd_reduce_from_bds'] = 1; + } + if(empty($data['data_received_date'])){ $data['data_received_date'] = null; }else{ @@ -1171,8 +1189,8 @@ class PolicyTransactionController extends BaseController 'policy_type_id' => $issue_type['policy_type_id'] ?? null, 'issue_type' => $issue_type['issue_type'] ?? null, 'source_client_policy_id' => $issue_type['source_client_policy_id'] ?? null, - 'cd_ac_no' => $issue_type['cd_ac_no'] ?? null, - 'cd_ac_pk' => $issue_type['cd_ac_pk'] ?? null, + 'cd_ac_no' => isset( $data['cd_ac_no']) && !empty($data['cd_ac_no']) ? $data['cd_ac_no'] : $issue_type['cd_ac_no'] ?? null, + 'cd_ac_pk' => isset( $data['cd_ac_pk']) && !empty($data['cd_ac_pk']) ? $data['cd_ac_pk'] : $issue_type['cd_ac_pk'] ?? null, // 'policy_issue_date' => $issue_type['policy_issue_date'] ?? null, 'policy_start_date' => $issue_type['policy_start_date'] ?? null, 'policy_end_date' => $issue_type['policy_end_date'] ?? null, @@ -1231,6 +1249,7 @@ class PolicyTransactionController extends BaseController $update = $this->policyTransactionModel->where('id', $id)->set($data)->update(); if ($update) { + $this->insertTransactionStatus($id, $data, 1); $this->handleCompletedStatus($data, $id); $this->insertOrUpdateCoShareDetails($data, $id); @@ -1243,13 +1262,13 @@ class PolicyTransactionController extends BaseController } private function handleCompletedStatus($data, $policy_tran_id) - { - if ($data['status'] == 'completed' && $data['ct_type'] == 2) { + { + if ($data['client_type'] == 1 && $data['status'] == 'completed' && $data['is_cd_reduce_from_bds'] == 1) { $tolamt = $data['total'][0] ?? 0; $description = 'The following amount of Rs. ' . round($tolamt, 2) . '/- has been' . - ($data['action_type'] == 'deletion' ? ' Credit ' : ' Debit ') . 'from the policy transaction'; + ($data['action_type'] == 'deletion' ? ' Credit ' : ' Debit ') . 'from the policy transaction (BDS)'; $cd_tranction_data = [ 'amount' => $tolamt, @@ -1264,6 +1283,7 @@ class PolicyTransactionController extends BaseController 'updated_by' => get_session_userid(), 'event_name' => $data['action_type'], 'is_active' => 1, + 'cd_ac_pk' => $data['cd_ac_pk'] ?? null, ]; DepositHelper::saveDeposit($cd_tranction_data, get_session_userid()); @@ -1589,6 +1609,7 @@ class PolicyTransactionController extends BaseController ->select(" pt_co_share_details.*, policy_transaction.bro_payable_by, + policy_transaction.cd_ac_pk, ( select cd_ac_no from cd_master @@ -1616,9 +1637,12 @@ class PolicyTransactionController extends BaseController ->where('is_active', 1) ->first(); + $cd_ac_no = db_connect()->table('cd_master')->where('id', $is_copay_yes['cd_ac_pk'] ?? null)->get()->getRowArray(); + if ($totalCount) { - return $this->respond(['status' => true, 'code' => 200, 'data' => $totalCount, 'is_copay_yes' => $is_copay_yes], 200); + return $this->respond(['status' => true, 'code' => 200, 'data' => $totalCount, 'is_copay_yes' => $is_copay_yes, "cd_master_data" => $cd_ac_no], 200); } else { + $insurer_data = $this->clientPolicyModel->where('id', $client_policy_id)->where('is_active', 1)->first(); return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to get data'], 200); } diff --git a/app/Helpers/DepositHelper.php b/app/Helpers/DepositHelper.php index d225f71f..5137cc5c 100755 --- a/app/Helpers/DepositHelper.php +++ b/app/Helpers/DepositHelper.php @@ -32,8 +32,11 @@ class DepositHelper */ public static function saveDeposit(array $data, int $loggedInUserID): array { + + log_message("error", 'saveDeposit function called '. json_encode($data)); + // Retrieve the last known balance - $lastBalance = self::calculateLastBalance($data['client_id'], $data['insurer_id'], $data['cd_ac_no']); + $lastBalance = self::calculateLastBalance($data['client_id'], $data['insurer_id'], $data['cd_ac_no'], $data['cd_ac_pk'] ?? null); // Calculate the new balance based on the transaction type $newBalance = self::calculateBalance( @@ -91,15 +94,21 @@ class DepositHelper * * @return float The last known balance. */ - public static function calculateLastBalance(int $clientId, int $insurerId, $cd_ac_no): float + public static function calculateLastBalance(int $clientId, int $insurerId, $cd_ac_no, $cd_ac_pk): float { $model = new ClientDepositModel(); // $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE client_id = ? AND insurer_id = ? ORDER BY created_at DESC LIMIT 1"; // $getLastBalanceParams = [$clientId, $insurerId]; - $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_no = ? AND is_active = 1 ORDER BY created_at DESC LIMIT 1"; - $getLastBalanceParams = [$cd_ac_no]; + if(empty($cd_ac_pk)){ + $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_no = ? AND is_active = 1 ORDER BY created_at DESC LIMIT 1"; + $getLastBalanceParams = [$cd_ac_no]; + }else{ + $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_pk = ? AND is_active = 1 ORDER BY created_at DESC LIMIT 1"; + $getLastBalanceParams = [$cd_ac_pk]; + } + $lastBalance = $model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->balance ?? 0; diff --git a/app/Helpers/utility_helper.php b/app/Helpers/utility_helper.php index 54a5883a..33f504c0 100755 --- a/app/Helpers/utility_helper.php +++ b/app/Helpers/utility_helper.php @@ -677,3 +677,69 @@ if (!function_exists('is_json_string')) { } } +if (!function_exists('check_cd_entry_exist')) { + function check_cd_entry_exist($params) + { + $db = db_connect(); + + $client_id = $params['client_id']; + $client_policy_id = $params['client_policy_id']; + $insurer_id = $params['insurer_id']; + $cd_ac_pk = $params['cd_ac_pk']; + $event_name = $params['event_name']; + + // Check if truncated entry (sub_type = 8) exists + $has_truncated = $db->table('cash_deposit') + ->where('client_id', $client_id) + ->where('insurer_id', $insurer_id) + ->where('cd_ac_pk', $cd_ac_pk) + ->where('client_policy_id', $client_policy_id) + ->where('event_name', $event_name) + ->where('sub_type', 8) + ->where('is_active', 1) + ->countAllResults(); + + if ($has_truncated) { + + echo "has_truncated"; + // Get all entries with same details (including truncated) + $entries = $db->table('cash_deposit') + ->where('client_id', $client_id) + ->where('insurer_id', $insurer_id) + ->where('cd_ac_pk', $cd_ac_pk) + ->where('client_policy_id', $client_policy_id) + ->where('event_name', $event_name) + ->where('is_active', 1) + ->where('sub_type !=', 8) + ->get() + ->getResultArray(); + + if (count($entries) > 1) { + // Only one entry found (truncated) + return true; + }else{ + return false; + } + } else { + // Check if any other entry exists + $entry = $db->table('cash_deposit') + ->where('client_id', $client_id) + ->where('insurer_id', $insurer_id) + ->where('cd_ac_pk', $cd_ac_pk) + ->where('client_policy_id', $client_policy_id) + ->where('event_name', $event_name) + ->where('is_active', 1) + ->get() + ->getRowArray(); + + if (!empty($entry)) { + return true; + } + } + + return false; + } +} + + + diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index 2a77d201..22e45e9c 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -79,6 +79,7 @@ class PolicyTransactionModel extends Model 'cd_ac_pk', 'install_due_date', 'policy_with_corr', + 'is_cd_reduce_from_bds', ]; diff --git a/app/Views/notification.php b/app/Views/notification.php index aca7bfe6..1b4c7072 100755 --- a/app/Views/notification.php +++ b/app/Views/notification.php @@ -426,7 +426,7 @@ + @@ -310,6 +311,15 @@ +
+ + + +
+
@@ -585,6 +595,14 @@ $(document).ready(function(){ var cd_ac_no = $(this).find('option:selected').attr('data-cd'); var start_date = $(this).find('option:selected').attr('data-sd'); var end_date = $(this).find('option:selected').attr('data-ed'); + var policy_type_id = $(this).find('option:selected').attr('data-ptid'); + + if(policy_type_id == 1 || policy_type_id == 2 || policy_type_id == 3 || policy_type_id == 4 || policy_type_id == 5 || policy_type_id == 6 || policy_type_id == 7){ + $('#is_cd_reduce_from_bds').prop('disabled',true).prop('checked', false) + }else{ + $('#is_cd_reduce_from_bds').prop('disabled',false) + } + // console.log('client_id', client_id); // console.log('client_policy_id', client_policy_id); @@ -596,7 +614,7 @@ $(document).ready(function(){ $('#policy_no').val(policy_no); $('#insurer_id').val(insurer); $('#tpa').val(tpa).change(); - $('#cd_ac_no').val(cd_ac_no); + // $('#cd_ac_no').val(cd_ac_no); $('#policy_start_date').val(start_date); $('#policy_end_date').val(end_date); @@ -619,6 +637,9 @@ $(document).ready(function(){ if(res.status == true){ if(res.data.length > 0){ + $('#ct_type').val(2) + $('#cd_ac_pk').val(res.is_copay_yes.cd_ac_pk); + $('#cd_ac_no').val(res.cd_master_data.cd_ac_no ?? ""); $('#bro_payable_by').val(res.data[0].bro_payable_by) if(res.is_copay_yes && res.is_copay_yes.co_share == 1){ @@ -633,9 +654,11 @@ $(document).ready(function(){ }else{ + $('#ct_type').val(1) addInsurerColumn() } }else{ + $('#ct_type').val(1) addInsurerColumn() } }, @@ -914,6 +937,9 @@ function getPolicyTransactionDataForEndorsementEdit(input){ $('#last_action_date').val(res.data.last_action_date); $('#install_due_date').val(res.data.install_due_date); $('#bro_payable_by').val(res.data.bro_payable_by); + $('#cd_ac_pk').val(res.data.cd_ac_pk); + $('#cd_ac_no').val(res.data.cd_ac_no); + $('#ct_type').val(res.data.ct_type); if (res.data.policy_with_corr == 1) { $('#policy_with_corr').prop('checked', true); @@ -921,6 +947,12 @@ function getPolicyTransactionDataForEndorsementEdit(input){ $('#policy_with_corr').prop('checked', false); } + if (res.data.is_cd_reduce_from_bds == 1) { + $('#is_cd_reduce_from_bds').prop('checked', true).prop('checked', false); + } else { + $('#is_cd_reduce_from_bds').prop('checked', false); + } + if (res.data.action_type == 'policy_instalment') { $('.install_due_date_div').show() }else{ @@ -937,6 +969,13 @@ function getPolicyTransactionDataForEndorsementEdit(input){ addInsurerColumn(); } + if(res.data.policy_type_id == 1 || res.data.policy_type_id == 2 || res.data.policy_type_id == 3 || res.data.policy_type_id == 4 || res.data.policy_type_id == 5 || res.data.policy_type_id == 6 || res.data.policy_type_id == 7){ + $('#is_cd_reduce_from_bds').prop('disabled',true).prop('checked', false); + }else{ + $('#is_cd_reduce_from_bds').prop('disabled',false); + } + + }else{ console.log('No data found'); } diff --git a/app/Views/policy_transaction_endorsement_list.php b/app/Views/policy_transaction_endorsement_list.php index 9259dc90..9c321e2e 100644 --- a/app/Views/policy_transaction_endorsement_list.php +++ b/app/Views/policy_transaction_endorsement_list.php @@ -698,6 +698,7 @@ document.addEventListener("DOMContentLoaded", function () { 'data-itp' : item.itp, 'data-bap' : item.bap, 'data-tpa' : item.tpa_branch_id + '-' + item.tpa_id, + 'data-ptid' : item.policy_type_id, }); $('#client_policy_id').append(option); }); diff --git a/app/Views/policy_transaction_inception_form.php b/app/Views/policy_transaction_inception_form.php index 11f601cc..67c84cf0 100644 --- a/app/Views/policy_transaction_inception_form.php +++ b/app/Views/policy_transaction_inception_form.php @@ -123,6 +123,12 @@ color: white !important; margin-right: 5px; } + +:disabled { + background-color: #e0e0e0; + color: #666; +} +
@@ -422,6 +428,16 @@
+
+ + + +
+