diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 65b805b4..a70782b5 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -660,18 +660,25 @@ class ClientController extends AdminController } // In your controller - public function deposit($id = null, $requestFrom = null, $policyId = null) + public function deposit($ClientId = null, $requestFrom = null, $policyId = null) { $headerData['tab_name'] = 'Client Deposit'; $headerData['page_name'] = 'Clients'; - $data['clientName'] = $this->clientModel->where('id', (int)$id)->find(); - $data['clientData'] = $this->clientPolicyModel->getinsurerswithclientid($id, $policyId); + if (is_string($ClientId) && preg_match('/^[a-f0-9]{32}$/i', $ClientId)) + { + $data['clientName'] = $this->clientModel->where('md5(id)', $ClientId)->find(); + }else { + $data['clientName'] = $this->clientModel->where('id', $ClientId)->find(); + } - $data['depositsummary'] = $this->clientPolicyModel->getDepositlistsummary($id); + + $data['clientData'] = $this->clientPolicyModel->getinsurerswithclientid($ClientId, $policyId); + + $data['depositsummary'] = $this->clientPolicyModel->getDepositlistsummary($ClientId); // Fetch associated insurer names and balances - $balances = $this->clientPolicyModel->getBalances($id); + $balances = $this->clientPolicyModel->getBalances($ClientId); $data['balances'] = $balances; // dd($data); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index d2c411b1..fa58834a 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -1665,10 +1665,10 @@ class EmployeeRestController extends AdminController { try { - $client = $this->clientModel->where('id', $this->request->getGet('client_id'))->first(); + $client = $this->clientModel->where('md5(id)', $this->request->getGet('client_id'))->first(); if (!empty($client)) { $client['client_logo'] = base_url() . 'public/uploads/logo/' . $client['client_logo']; - $clientPolicy = $this->clientPolicyModel->where('client_id', $this->request->getGet('client_id')) + $clientPolicy = $this->clientPolicyModel->where('md5(client_id)', $this->request->getGet('client_id')) ->where('client_branch_id', $this->request->getGet('client_branch_id'))->findAll(); return $this->respond(['status' => 'success', 'code' => 200, 'data' => ['client' => $client, 'client_policy' => $clientPolicy]], 200); } else { @@ -2307,7 +2307,7 @@ class EmployeeRestController extends AdminController $ClientPolicyData = $this->clientPolicyModel->select('client_policy.id as client_policy_id , client_policy.client_id as client_id, client_policy.policy_type_id as policy_type_id, client_policy.is_addon as is_addon , client_policy.open_for_enrollment as OpenForEnrollment , client_policy.inception_type as inception_type, client_policy.policy_no as policy_no, client_policy.insurer_id as insurer_id, DATE_FORMAT(client_policy.policy_end_date, "%d-%m-%Y") AS policy_expiry_date ') - ->where('client_policy.client_id', $this->request->getGet('client_id')) + ->where('md5(client_policy.client_id)', $this->request->getGet('client_id')) ->where('client_policy.client_branch_id', $this->request->getGet('client_branch_id')) ->where('client_policy.is_active', 1) ->where('client_policy.policy_status', $this->request->getGet('policy_status')) @@ -2329,7 +2329,7 @@ class EmployeeRestController extends AdminController // dd($employeeDetails); $activeCount = 0; $inactiveCount = 0; - if (count($employeeDetails)) { + if (count($employeeDetails)) { foreach ($employeeDetails as $item) { if ($item['emp_status'] === 'active') { $activeCount++; @@ -2555,7 +2555,7 @@ class EmployeeRestController extends AdminController $builder->where('tm.is_active', 1); if ($client_id > 0) { - $builder->where('tm.client_id', $client_id); + $builder->where('md5(tm.client_id)', $client_id); } if (!empty($from_date) && !empty($to_date)) { @@ -4468,7 +4468,21 @@ class EmployeeRestController extends AdminController return $this->respondCreated(['status' => false, 'message' => 'Client is required', 'data' => []]); } - $client_data = $this->clientModel->where('id', $post_data['client_id'])->first(); + // Handle raw client_id vs MD5 + if (is_string($post_data['client_id']) && preg_match('/^[a-f0-9]{32}$/i', $post_data['client_id'])) { + $client_data = $this->clientModel->where('MD5(id)', $post_data['client_id'])->first(); + } else { + $client_data = $this->clientModel->where('id', $post_data['client_id'])->first(); + } + + if(empty($client_data)){ + return $this->respondCreated(['status' => false, 'message' => 'Invalid Client Id', 'data' => []]); + } + + $post_data['client_id'] = $client_data['id']; + + + // print_r($client_data); die; if($client_data['hr_file_processed_by'] == 1){ diff --git a/app/Controllers/RestAuthenticationController.php b/app/Controllers/RestAuthenticationController.php index 9bfb87f9..b2d54fbb 100755 --- a/app/Controllers/RestAuthenticationController.php +++ b/app/Controllers/RestAuthenticationController.php @@ -759,6 +759,8 @@ class RestAuthenticationController extends AdminController $decoded = json_decode($HRAccessData['allowed_modules'], true); $getAllhrData[$key]['allowed_modules'] = $decoded; + $HRAccessData['pre_client_id'] = md5($HRAccessData['pre_client_id']); + $HRAccessData['post_client_id'] = md5($HRAccessData['post_client_id']); $token = JWTToken::encode($HRAccessData); $getAllhrData[$key]['token'] = $token; diff --git a/app/Models/ClientPolicyModel.php b/app/Models/ClientPolicyModel.php index 8f84def5..a466e712 100755 --- a/app/Models/ClientPolicyModel.php +++ b/app/Models/ClientPolicyModel.php @@ -183,9 +183,14 @@ class ClientPolicyModel extends Model ->join('clients', 'clients.id = client_policy.client_id') ->join('insurers', 'insurers.id = client_policy.insurer_id') ->join('insurer_branch', 'client_policy.insurer_branch_id = insurer_branch.id', 'left') - ->join('cd_master', 'cd_master.insurer_id = insurers.id AND cd_master.client_id = client_policy.client_id') - ->where('client_policy.client_id', $id) - ->where('cd_master.id = client_policy.cd_ac_pk') + ->join('cd_master', 'cd_master.insurer_id = insurers.id AND cd_master.client_id = client_policy.client_id'); + // ->where('client_policy.client_id', $id) + if (is_string($id) && preg_match('/^[a-f0-9]{32}$/i', $id)) { + $builder->where('MD5(client_policy.client_id)', $id); + } else { + $builder->where('client_policy.client_id', $id); + } + $builder->where('cd_master.id = client_policy.cd_ac_pk') ->where('cd_master.is_active', 1) ->groupBy('client_policy.client_id') ->groupBy('client_policy.insurer_id') @@ -248,14 +253,33 @@ class ClientPolicyModel extends Model } - public function getClientById($id) + // public function getClientById($id) + // { + // $query = $this->db->table('clients')->getWhere(['id' => $id]); + // // Debug statement + // return $query->getRow(); + // } + + public function getClientById($client_id) { - $query = $this->db->table('clients')->getWhere(['id' => $id]); - // Debug statement - return $query->getRow(); + if (empty($client_id)) { + return null; + } + + $builder = $this->db->table('clients'); + + // MD5 vs normal ID check + if (is_string($client_id) && preg_match('/^[a-f0-9]{32}$/i', $client_id)) { + $builder->where('MD5(id)', $client_id); + } else { + $builder->where('id', $client_id); + } + + return $builder->get()->getRow(); } + public function getDepositData($clientId, $insurerId, $cd_ac_pk = null, $subTypeOptions = null) { @@ -285,9 +309,16 @@ class ClientPolicyModel extends Model ->join('client_policy', 'client_policy.id = cash_deposit.client_policy_id', 'left') ->join('cd_master', 'cd_master.id = cash_deposit.cd_ac_pk', 'left') // ->join('policies', 'policies.id = client_policy.policy_id', 'left') - ->join('policy_type', 'policy_type.id = client_policy.policy_type_id', 'left') - ->where('cash_deposit.client_id', $clientId) - ->where('cash_deposit.insurer_id', $insurerId) + ->join('policy_type', 'policy_type.id = client_policy.policy_type_id', 'left'); + // ->where('cash_deposit.client_id', $clientId) + if (!empty($clientId)) { + if (is_string($clientId) && preg_match('/^[a-f0-9]{32}$/i', $clientId)) { + $query->where('MD5(cash_deposit.client_id)', $clientId); + } else { + $query->where('cash_deposit.client_id', $clientId); + } + } + $query->where('cash_deposit.insurer_id', $insurerId) ->where('cash_deposit.is_active', 1) ->where('cd_master.is_active', 1); // ->where('cash_deposit.cd_ac_pk = cd_master.id') @@ -300,12 +331,56 @@ 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 + // $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 sub_type = 3 THEN amount ELSE 0 END) AS total_refund') + // ->where('client_id', $clientId) + // ->where('insurer_id', $insurerId) + // ->where('cd_ac_pk', $cd_ac_pk) + // ->where('cash_deposit.is_active', 1) + // ->get() + // ->getRow(); + + // // dd($this->db->getLastQuery()); + // return $data; + // } + public function getDepositSummary($clientId, $insurerId, $cd_ac_pk) { + if (empty($clientId)) { + return null; + } + // Build SAFE client condition (NO BINDS!) + if (is_string($clientId) && preg_match('/^[a-f0-9]{32}$/i', $clientId)) { + $clientCondition = 'MD5(client_id) = ' . $this->db->escape($clientId); + } else { + $clientCondition = 'client_id = ' . $this->db->escape($clientId); + } + + /* ------------------------------------------------- + Subquery: latest balance + ------------------------------------------------- */ $subQuery = $this->db->table('cash_deposit') ->select('balance') - ->where('client_id', $clientId) + ->where($clientCondition, null, false) ->where('insurer_id', $insurerId) ->where('cd_ac_pk', $cd_ac_pk) ->where('is_active', 1) @@ -313,14 +388,15 @@ class ClientPolicyModel extends Model ->limit(1) ->getCompiledSelect(); - // Fetch the sum of credit and debit transactions and calculate the balance + /* ------------------------------------------------- + Main query: totals + ------------------------------------------------- */ $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 sub_type = 3 THEN amount ELSE 0 END) AS total_refund') - ->where('client_id', $clientId) + ->where($clientCondition, null, false) ->where('insurer_id', $insurerId) ->where('cd_ac_pk', $cd_ac_pk) ->where('cash_deposit.is_active', 1) @@ -330,6 +406,8 @@ class ClientPolicyModel extends Model // dd($this->db->getLastQuery()); return $data; } + + // Inside your clientPolicyModel // Inside your clientPolicyModel // public function getDepositDataWithBalance($clientId, $insurerId) @@ -402,6 +480,35 @@ class ClientPolicyModel extends Model // ORDER BY cd.id DESC // "; + // $sql = " + // SELECT cd.insurer_id, cd.cd_ac_pk, cd.balance + // FROM cash_deposit cd + // JOIN cd_master cdm ON cdm.id = cd.cd_ac_pk + // JOIN ( + // SELECT MAX(id) AS max_id + // FROM cash_deposit + // 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; + // "; + + // $binds = ['id'=>(int)$id]; + // return $this->db->query($sql,$binds)->getResult(); + + $whereClient = 'cd.client_id = :id:'; + $subWhereClient = 'client_id = :id:'; + + $binds = ['id' => $id]; + + // 🔐 MD5 handling + if (is_string($id) && preg_match('/^[a-f0-9]{32}$/i', $id)) { + $whereClient = 'MD5(cd.client_id) = :id:'; + $subWhereClient = 'MD5(client_id) = :id:'; + } + $sql = " SELECT cd.insurer_id, cd.cd_ac_pk, cd.balance FROM cash_deposit cd @@ -409,16 +516,17 @@ class ClientPolicyModel extends Model JOIN ( SELECT MAX(id) AS max_id FROM cash_deposit - WHERE is_active = 1 AND client_id = :id: + WHERE is_active = 1 AND {$subWhereClient} 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; + JOIN insurers ON cd.insurer_id = insurers.id + WHERE cd.is_active = 1 + AND {$whereClient} + AND cdm.is_active = 1 + ORDER BY cd.insurer_id "; - $binds = ['id'=>(int)$id]; - return $this->db->query($sql,$binds)->getResult(); + return $this->db->query($sql, $binds)->getResult(); } diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php index 036f2c19..70f0485c 100755 --- a/app/Models/EmployeePolicyModel.php +++ b/app/Models/EmployeePolicyModel.php @@ -295,7 +295,10 @@ class EmployeePolicyModel extends Model ->orderBy('emp.emp_code', 'ASC') ->orderBy('employee_polices.employee_id', 'ASC'); // Conditionally add where clauses - if ($client_id != 0 && !empty($client_id)) { + if (is_string($client_id) && preg_match('/^[a-f0-9]{32}$/i', $client_id)) { + // MD5 hash → compare using md5() + $result->where('md5(emp.client_id)', $client_id); + }else if ($client_id != 0 && !empty($client_id)) { $result->where('emp.client_id', $client_id); } if ($branch_id != 0 && !empty($branch_id)) { diff --git a/app/Models/InsurerModel.php b/app/Models/InsurerModel.php index dcb7fe8b..d264c231 100755 --- a/app/Models/InsurerModel.php +++ b/app/Models/InsurerModel.php @@ -37,27 +37,67 @@ class InsurerModel extends Model ->getResult(); } + // public function getInsurerName($insurerId, $client_id) + // { + // // Fetch the insurer name based on the insurer ID + // $query = $this->db->table('client_policy') + // ->select('insurers.id, insurers.name, cd_master.cd_ac_no as cd_master_account_no') + // ->join('insurers', 'insurers.id = client_policy.insurer_id') + // ->join('cd_master', 'cd_master.insurer_id = insurers.id AND cd_master.client_id = client_policy.client_id') + // ->where('client_policy.insurer_id', $insurerId) + // ->where('client_policy.client_id', $client_id) + // ->where('cd_master.id = client_policy.cd_ac_pk') + // ->where('cd_master.is_active', 1) + // ->get(); + + // if ($query->resultID->num_rows > 0) { + // $result = $query->getRow(); + // return $result; + // } + + // return null; // or handle accordingly if the insurer is not found + // } + public function getInsurerName($insurerId, $client_id) { - // Fetch the insurer name based on the insurer ID - $query = $this->db->table('client_policy') - ->select('insurers.id, insurers.name, cd_master.cd_ac_no as cd_master_account_no') - ->join('insurers', 'insurers.id = client_policy.insurer_id') - ->join('cd_master', 'cd_master.insurer_id = insurers.id AND cd_master.client_id = client_policy.client_id') - ->where('client_policy.insurer_id', $insurerId) - ->where('client_policy.client_id', $client_id) - ->where('cd_master.id = client_policy.cd_ac_pk') - ->where('cd_master.is_active', 1) - ->get(); + $builder = $this->db->table('client_policy') + ->select(' + insurers.id, + insurers.name, + cd_master.cd_ac_no AS cd_master_account_no + ') + ->join('insurers', 'insurers.id = client_policy.insurer_id') + ->join( + 'cd_master', + 'cd_master.insurer_id = insurers.id + AND cd_master.client_id = client_policy.client_id + AND cd_master.id = client_policy.cd_ac_pk + AND cd_master.is_active = 1' + ) + ->where('client_policy.insurer_id', $insurerId); - if ($query->resultID->num_rows > 0) { - $result = $query->getRow(); - return $result; + // Handle raw client_id vs MD5 + if (!empty($client_id)) { + if (is_string($client_id) && preg_match('/^[a-f0-9]{32}$/i', $client_id)) { + $builder->where('MD5(client_policy.client_id)', $client_id); + } else { + $builder->where('client_policy.client_id', $client_id); + } } - return null; // or handle accordingly if the insurer is not found + $result = $builder + ->groupBy([ + 'insurers.id', + 'cd_master.cd_ac_no' + ]) + ->limit(1) + ->get() + ->getRow(); + + return $result ?? null; } + public function getInsurerTemplateByInsurerID($insurer_id) { $insurer_data = $this->db->table('insurer_excel_export_template')