nhance/app/Models/ClientPolicyModel.php

271 lines
9.7 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class ClientPolicyModel extends Model
{
protected $table = 'client_policy';
protected $primaryKey = 'id';
protected $allowedFields = [
"id",
"client_id",
"policy_id",
"insurer_id",
"insurer_branch_id",
"tpa_id",
"tpa_branch_id",
"policy_type_id",
"policy_start_date",
"policy_end_date",
"insured",
"no_of_employees",
"no_of_lives",
"no_lives_at_inception",
"premium_paid_at_inception",
"earned_premium_date",
"claims_incurred_date",
"incurred_claims_ratio",
"claims_experience_for_last_3_years",
"policy_status",
"earned_premium_amount",
"claims_incurred_amount",
"policy_terms",
"open_for_enrollment",
"is_addon",
"base_policy",
"created_by",
"updated_by",
"Is_active",
"date_of_exit",
"reason_for_exit"
];
public function getClientPolicyById($id){
return $this->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, 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')
->select('policy_type.policy_type as policy_type_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')
->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)
->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){
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();
// 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 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();
}
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();
return ['client'=>$client_count, 'emp' => $emp_count];
}
}