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.*') ->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.client_id', $client_id) ->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 getPolicyTermsJson($client_id,$policy_id) { return $this->select('policy_terms') ->where('client_id',$client_id) ->where('policy_id',$policy_id) ->get() ->getResult(); } public function getinsurerswithclientid($id){ 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 client_name') ->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', $id) // Group by insurer_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 getDepositData($clientId, $insurerId) { // Fetch the deposit data based on client and insurer IDs return $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') ->select('user_profiles.first_name as username') ->join('user_profiles','user_profiles.id=cash_deposit.created_by') ->join('insurers', 'insurers.id = cash_deposit.insurer_id') ->join('clients', 'clients.id = cash_deposit.client_id') ->where('cash_deposit.client_id', $clientId) ->where('cash_deposit.insurer_id', $insurerId) // Add this line to filter by insurer_id ->get() ->getResult(); } public function getDepositSummary($clientId, $insurerId) { // Fetch the sum of credit and debit transactions and calculate the balance return $this->db->table('cash_deposit') ->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) ->get() ->getRow(); } // 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; $balances[$insurerId] = $summary; } return $balances; } public function getDepositlistsummary($id) { return $this->db->table('cash_deposit') ->select('insurer_id') ->select('SUM(CASE WHEN transaction_type = "Credit" THEN amount ELSE -amount END) AS balance') ->where('client_id', $id) ->groupBy('insurer_id') ->get() ->getResult(); } }