nhance/app/Models/ClientPolicyModel.php

389 lines
14 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",
"inception_type",
"base_policy",
"created_by",
"updated_by",
"Is_active",
"date_of_exit",
"reason_for_exit",
"policy_no",
"client_branch_id",
"cd_ac_no",
"gst",
"enrolment_visibility",
"is_active",
];
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('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')
->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){
return $this->db->table('client_policy')
->select('client_policy.*, cd_master.cd_ac_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 = 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, client_policy.policy_no')
// ->select('policies.name as policy_name')
->select('policy_type.policy_type')
->select('user_profiles.first_name as username')
->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('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)
->where('cash_deposit.is_active', 1)
->orderBy('cash_deposit.id', 'DESC')
->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];
}
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}";
$result = $this->db->query($query)->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();
}
}