db->table('client_policy') ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') ->select('tpa.name as tpa_name, tpa.short_name as tpa_short') ->select('policies.name as policy_name') ->select('insurer_branch.branch_name as insurer_branch_name, insurer_branch.branch_code as insurer_branch_code') ->select('tpa_branch.branch_name as tpa_branch_name, tpa_branch.branch_code as tpa_branch_code') ->join('insurers', 'insurers.id = client_policy.insurer_id') ->join('insurer_branch', 'client_policy.insurer_branch_id = insurer_branch.id') ->join('tpa', 'tpa.id = client_policy.tpa_id') ->join('tpa_branch', 'client_policy.tpa_branch_id = tpa_branch.id') ->join('policies', 'policies.id = client_policy.policy_id') ->where('client_policy.id', $id) ->get() ->getResult(); } public function getClientPolicyByClientId($client_id) { return $this->db->table('client_policy') ->select(' client_policy.*, CASE WHEN client_policy.policy_entry_from = 3 AND client_policy.cd_ac_pk IS NULL THEN leads.cd_amount ELSE NULL END AS lead_cd_amount ', false) ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') ->select('tpa.name as tpa_name, tpa.short_name as tpa_short') ->select('policy_type.policy_type as policy_type_name') ->select('insurer_branch.branch_name as insurer_branch_name, insurer_branch.branch_code as insurer_branch_code') ->select('tpa_branch.branch_name as tpa_branch_name, tpa_branch.branch_code as tpa_branch_code') ->select('policy_type.policy_type as policy_type_name') ->select('client_branch.branch_name as branch_name') ->join('insurers', 'insurers.id = client_policy.insurer_id') ->join('insurer_branch', 'client_policy.insurer_branch_id = insurer_branch.id') ->join('tpa', 'tpa.id = client_policy.tpa_id', 'left') ->join('tpa_branch', 'client_policy.tpa_branch_id = tpa_branch.id', 'left') ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id') ->join('leads', 'client_policy.is_from_lead = leads.id', 'left') ->where('client_policy.client_id', $client_id) // ->where('client_policy.policy_status', 1) ->where('client_policy.is_active', 1) ->get() ->getResult(); } public function getPolicyPremium($policy_id) { return $this->db->table('policies') ->select('policy_type.*') ->join('policy_type', 'policy_type.id = policies.policy_type_id') ->where('policies.id', $policy_id) ->get() ->getResult(); } public function getPolicyPremiumPolicyTypeId($policy_type_id) { return $this->db->table('policies') ->select('policy_type.*') ->join('policy_type', 'policy_type.id = policies.policy_type_id') ->where('policies.id', $policy_type_id) ->get() ->getResult(); } public function getPolicyDetails($client_id, $policy_id) { return $this->select('*') ->where('client_id', $client_id) ->where('id', $policy_id) ->get() ->getResult(); } public function getinsurerswithclientid($id, $policyId = null) { // dd($policyId); $builder = $this->db->table('client_policy') ->select('client_policy.*, cd_master.cd_ac_no as cd_master_account_no') ->select('insurers.name as insurer_name, insurers.short_name as insurer_short, insurer_branch.branch_name as insurer_branch_name') ->select('clients.client_name as client_name') ->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) 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') ->groupBy('client_policy.cd_ac_pk'); if ($policyId != null) { $builder->whereIn('client_policy.id', $policyId); } $data = $builder->get()->getResult(); // dd($this->db->getLastQuery()); return $data; // return $this->db->table('client_policy') // ->select('client_policy.*, cd_master.cd_ac_no as cd_master_account_no') // ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') // ->select('clients.client_name as client_name') // ->join('clients', 'clients.id=client_policy.client_id') // ->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.client_id', $id) // ->where('cd_master.id = client_policy.cd_ac_pk') // ->where('cd_master.is_active', 1) // ->groupBy('client_policy.client_id', $id) // Group by insurer_id // ->groupBy('client_policy.insurer_id') // ->groupBy('client_policy.cd_ac_pk') // ->get() // ->getResult(); // return $this->db->table('client_policy') // ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') // ->select('clients.client_name as client_name') // ->select('MAX(client_policy.id) as max_id') // or any other appropriate aggregate function // ->join('clients', 'clients.id = client_policy.client_id') // ->join('insurers', 'insurers.id = client_policy.insurer_id') // ->where('client_policy.client_id', $id) // ->groupBy('client_policy.insurer_id') // ->groupBy('client_policy.client_id') // ->get() // ->getResult(); } public function getinsurerswithinsurenceid($insurerId) { return $this->db->table('client_policy') ->select('client_policy.*') ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') ->select('clients.client_name as clientname, clients.short_name as clientshort') ->join('insurers', 'insurers.id = client_policy.insurer_id') ->join('clients', 'clients.id = client_policy.client_id') ->where('client_policy.insurer_id', $insurerId) ->groupBy('client_policy.client_id') // Group by insurer_id ->get() ->getResult(); } // public function getClientById($id) // { // $query = $this->db->table('clients')->getWhere(['id' => $id]); // // Debug statement // return $query->getRow(); // } public function getClientById($client_id) { 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) { if($subTypeOptions != null){ $caseSql = "CASE cash_deposit.sub_type"; foreach ($subTypeOptions as $key => $label) { $caseSql .= " WHEN {$key} THEN " . $this->db->escape($label); } $caseSql .= " ELSE 'Unknown' END AS sub_type_text"; }else{ $caseSql = ""; } $url = base_url('downloadCdSplitUpFile?id='); // Fetch the deposit data based on client and insurer IDs $query = $this->db->table('cash_deposit') ->select('cash_deposit.*') ->select('insurers.name as insurer_name, insurers.short_name as insurer_short') ->select('clients.client_name as clientname, clients.short_name as clientshort, client_policy.policy_no') ->select('cd_master.cd_ac_no as cd_master_account_no') // ->select('policies.name as policy_name') ->select('policy_type.policy_type') ->select('user_profiles.first_name as username') ->select("CASE WHEN cash_deposit.file_id IS NOT NULL AND cash_deposit.file_id != '' THEN CONCAT(" . $this->db->escape($url) . ", MD5(cash_deposit.file_id)) ELSE NULL END AS split_up_url", false) ->select($caseSql) ->join('user_profiles', 'user_profiles.id = cash_deposit.created_by', 'left') ->join('insurers', 'insurers.id = cash_deposit.insurer_id', 'left') ->join('clients', 'clients.id = cash_deposit.client_id', 'left') ->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) 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') if(!empty($cd_ac_pk)){ $query->where('cash_deposit.cd_ac_pk', $cd_ac_pk); } $query->orderBy('cash_deposit.id', 'DESC'); $data = $query->get()->getResult(); return $data; } // 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($clientCondition, null, false) ->where('insurer_id', $insurerId) ->where('cd_ac_pk', $cd_ac_pk) ->where('is_active', 1) ->orderBy('id', 'DESC') ->limit(1) ->getCompiledSelect(); /* ------------------------------------------------- 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 sub_type = 3 THEN amount ELSE 0 END) AS total_refund') ->where($clientCondition, null, false) ->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; } // Inside your clientPolicyModel // Inside your clientPolicyModel // public function getDepositDataWithBalance($clientId, $insurerId) // { // // Fetch deposit data with balance information // $builder = $this->db->table('cash_deposit'); // $builder->select('id, created_at, description, sub_type, amount, transaction_type'); // $builder->where('client_id', $clientId); // $builder->where('insurer_id', $insurerId); // $builder->orderBy('created_at', 'asc'); // $depositData = $builder->get()->getResult(); // $currentBalance = 0; // foreach ($depositData as $transaction) { // if ($transaction->transaction_type == 'Credit') { // $currentBalance += $transaction->amount; // } elseif ($transaction->transaction_type == 'Debit') { // $currentBalance -= $transaction->amount; // } // $transaction->balance = $currentBalance; // } // return $depositData; // } public function getBalances($clientId) { $depositsummary = $this->getDepositlistsummary($clientId); $balances = []; foreach ($depositsummary as $summary) { $insurerId = $summary->insurer_id; $cd_ac_pk = $summary->cd_ac_pk; $balances[$insurerId . '-'. $cd_ac_pk] = $summary; } return $balances; } public function getDepositlistsummary($id) { // return $this->db->table('cash_deposit') // ->select('insurer_id, cd_ac_pk, balance') // // ->select('SUM(CASE WHEN transaction_type = "Credit" THEN amount ELSE -amount END) AS balance') // ->where('client_id', $id) // ->where('is_active', 1) // ->orderBy('id', 'desc') // ->groupBy('insurer_id') // // ->groupBy('cd_ac_pk') // ->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 // 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 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 {$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 {$whereClient} AND cdm.is_active = 1 ORDER BY cd.insurer_id "; return $this->db->query($sql, $binds)->getResult(); } public function updateStatus() { // Update client_policy table $client = $this->db->query("UPDATE client_policy SET policy_status = 0 WHERE policy_end_date < CURDATE()"); $client_count = $this->db->affectedRows(); // Update employee_polices table $employee = $this->db->query("UPDATE employee_polices SET status = 'expired' WHERE policy_end_date < CURDATE()"); $emp_count = $this->db->affectedRows(); // Log the result log_message('error', "Client policies updated: {$client_count}"); log_message('error', "Employee policies updated: {$emp_count}"); return ['client' => $client_count, 'emp' => $emp_count]; } public function getpolicyWithPattern($client_id) { $query = $this->db->table('client_policy') ->select('client_policy.*, policies.name, policies.policy_type_id, policy_type.policy_type') ->join('policies', 'policies.id = client_policy.policy_id') ->join('policy_type', 'policy_type.id = policies.policy_type_id') // ->where('policy_type.policy_type', 'GMC') ->whereIn('policy_type.id', [2, 3]) ->where('client_policy.client_id', $client_id) ->get() ->getResult(); return $query; } public function getTopUpPolicy($client_id, $type) { $query = " SELECT client_policy.*, policies.name, policies.policy_type_id FROM client_policy JOIN policies ON policies.id = client_policy.policy_id JOIN policy_type ON policy_type.id = policies.policy_type_id WHERE policy_type.policy_type = :type: AND client_policy.client_id = :client_id:"; $binds = ['type' =>$type,'client_id'=>(int)$client_id]; $result = $this->db->query($query,$binds)->getResult(); return $result; } public function getClientPolicyByPolicyType($client_id, $type) { $query = $this->db->table('client_policy') ->select('client_policy.*, insurers.name as insurer_name, insurers.short_name as insurer_short') ->select('tpa.name as tpa_name, tpa.short_name as tpa_short') ->select('policies.name as policy_name, policies.policy_type_id') ->select('policy_type.policy_type as policy_type_name') ->select('insurer_branch.branch_name as insurer_branch_name, insurer_branch.branch_code as insurer_branch_code') ->select('tpa_branch.branch_name as tpa_branch_name, tpa_branch.branch_code as tpa_branch_code') ->join('insurers', 'insurers.id = client_policy.insurer_id') ->join('insurer_branch', 'client_policy.insurer_branch_id = insurer_branch.id') ->join('tpa', 'tpa.id = client_policy.tpa_id') ->join('tpa_branch', 'client_policy.tpa_branch_id = tpa_branch.id') ->join('policies', 'policies.id = client_policy.policy_id') ->join('policy_type', 'policy_type.id = policies.policy_type_id') ->where('client_policy.client_id', $client_id) ->where('client_policy.policy_status', 1); if ($type == 2) { $query->where('policy_type.policy_type', 'GMC - Top-up'); } else if ($type == 3) { $query->whereIn('policy_type.policy_type', ['GMC - Parents', 'GMC - Top-up(Parents)']); } $result = $query->get()->getResult(); return $result; } public function getPolicyTypeForPolicyBinding($client_id) { $result = $this->table('client_policy') ->select('policy_type.*, client_policy.id as client_policy_id, client_policy.policy_no') ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') ->where('client_policy.client_id', $client_id) ->where('client_policy.is_active', 1) ->whereIn('policy_type.id', [2, 3]) ->findAll(); return $result; } public function getCliendDataForExcelFileName($client_policy_id) { return $this->table('client_policy') ->select('clients.short_name, policy_type.policy_type, client_branch.branch_code') ->join('clients', 'clients.id = client_policy.client_id') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id') ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') ->where('client_policy.id', $client_policy_id) ->where('client_policy.is_active', 1) ->first(); } //for this using in CRONE JOB public function getPolicyDetailsForRemainder($client_id = null, $branch_id = null, $policy_id = null) { $builder = $this->table('client_policy') ->where('client_policy.is_active', 1) ->where('client_policy.policy_status', 1) // ->where('client_policy.is_addon', 1) // ->where('client_policy.inception_type', 2) ->where('client_policy.open_for_enrollment', 1); if ($client_id && $branch_id) { $builder->where('client_policy.client_id', $client_id) ->where('client_policy.client_branch_id', $branch_id); } if (empty($policy_id)) { $builder->whereIn('client_policy.policy_type_id', [2, 3, 4, 5]); } else { $builder->where('client_policy.id', $policy_id); } return $builder->get()->getResultArray(); } //for this using in CRONE JOB public function getPolicyDetailsForEnrollment($client_id = null, $branch_id = null) { $builder = $this->table('client_policy') ->where('client_policy.is_active', 1) ->where('client_policy.policy_status', 1) ->where('client_policy.open_date IS NOT NULL') ->where('client_policy.close_date IS NOT NULL'); if ($client_id && $branch_id) { $builder->where('client_policy.client_id', $client_id) ->where('client_policy.client_branch_id', $branch_id); } return $builder->get()->getResultArray(); } }