nhance/app/Models/ClientModel.php

227 lines
8.1 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
use App\Models\ClientPolicyModel;
use App\Models\ClientBranchModel;
class ClientModel extends Model
{
protected $table = 'clients';
protected $primaryKey = 'id';
protected $allowedFields = [
"id",
"entity_type_id",
"client_type",
"client_name",
"short_name",
"pan",
"gst",
"address1",
"address2",
"city",
"state",
"pincode",
"is_download_btn",
"client_logo",
"created_by",
"updated_by",
"is_active",
"common_mails",
"hr_mails",
"client_code",
"cost_center",
"dob",
"aadhar",
"reference",
"phone",
"email",
"reply_to",
"mail_domain",
"addon_subheading",
"parent_client_id",
];
//get client and its associated policies in same array
public function clientsWithPolicies()
{
$role_id = get_role_id();
$user_id = get_session_userid();
$columns = ['clients.id ', 'client_name','short_name'];
$clients = $this->select($columns)
->where('clients.is_active',1)
->findAll();
if($role_id == 2 || $role_id == 3){
$clients = $this->select($columns)
->join('client_rm', 'client_rm.client_id = clients.id')
->where('client_rm.user_id', $user_id)
->where('clients.is_active',1)
->findAll();
}
// $clients->findAll();
foreach ($clients as &$client) {
$clientPolicyModel = new ClientPolicyModel();
$clientPolicies = $clientPolicyModel
->select(['
client_branch.id as branch_id',
'client_branch.branch_name',
'client_branch.branch_code',
'client_branch.client_id',
'client_policy.id as client_policy_id',
'client_policy.policy_terms',
'client_policy.policy_no',
// 'p.name',
'pt.policy_type',
])
->join('client_branch', 'client_branch.id = client_policy.client_branch_id')
// ->join('policies p', 'p.id = client_policy.policy_id')
->join('policy_type pt','client_policy.policy_type_id = pt.id')
->where('client_policy.client_id',$client['id'])
->where('client_policy.is_active', 1)
->where('client_policy.policy_status', 1)
->findAll();
$client['policies'] = $clientPolicies;
$clientBranchModel = new ClientBranchModel();
$clientBranchs = $clientBranchModel->select(['client_branch.id','client_branch.branch_name','client_branch.branch_code', 'client_branch.client_id','client_branch.units'])
->where('client_branch.client_id',$client['id'])
->where('client_branch.is_active',1)
->findAll();
$client['branchs'] = $clientBranchs;
}
//print_r(($clients));die();
return $clients;
}
public function getCreatedByUserName(){
$role_id = get_role_id();
$user_id = get_session_userid();
$query = $this->db->table('clients')
->select('clients.*')
->join('client_rm', 'client_rm.client_id = clients.id','left')
->where('clients.is_active', 1);
if (in_array($role_id, [2, 3])) {
// If the user is a Account Manager or Manager, filter by user_id
$query->where('client_rm.user_id', $user_id);
}
$data = $query->groupBy("clients.id")->get()->getResult();
// dd($data);
return $data;
}
public function getClientData($client_id = null)
{
// Load the database if not already loaded
$db = \Config\Database::connect();
// Clients query
$clientQuery = $db->table('clients')
->groupStart() // Start grouping OR conditions
->where('client_type IS NULL OR client_type = ""')
->orWhere('entity_type_id IS NULL OR entity_type_id = ""')
->orWhere('client_name IS NULL OR client_name = ""')
->orWhere('short_name IS NULL OR short_name = ""')
->orWhere('pan IS NULL OR pan = ""')
->groupEnd(); // End grouping OR conditions
if ($client_id) {
// If client_id is passed, fetch data for this particular client
$clientQuery->where('id', $client_id);
}
$clients = $clientQuery->get()->getResultArray();
// Client Branch query
$branchQuery = $db->table('client_branch')
->groupStart()
->where('branch_name IS NULL OR branch_name = ""')
->orWhere('branch_code IS NULL OR branch_code = ""')
->orWhere('gst IS NULL OR gst = ""')
->orWhere('units IS NULL OR units = ""')
->groupEnd();
if ($client_id) {
// Add filter for particular client's branches if client_id is passed
$branchQuery->where('client_id', $client_id);
}
$clientBranches = $branchQuery->get()->getResultArray();
// Client Policy query
$policyQuery = $db->table('client_policy')
->groupStart()
->where('client_id IS NULL OR client_id = ""')
->orWhere('client_branch_id IS NULL OR client_branch_id = ""')
->orWhere('policy_id IS NULL OR policy_id = ""')
->orWhere('policy_type_id IS NULL OR policy_type_id = ""')
->orWhere('insurer_id IS NULL OR insurer_id = ""')
->orWhere('insurer_branch_id IS NULL OR insurer_branch_id = ""')
->orWhere('tpa_id IS NULL OR tpa_id = ""')
->orWhere('tpa_branch_id IS NULL OR tpa_branch_id = ""')
->orWhere('policy_start_date IS NULL OR policy_start_date = ""')
->orWhere('policy_end_date IS NULL OR policy_end_date = ""')
->orWhere('policy_no IS NULL OR policy_no = ""')
->orWhere('cd_ac_no IS NULL OR cd_ac_no = ""')
->orWhere('policy_terms IS NULL OR policy_terms = ""')
->orWhere('gst IS NULL OR gst = ""')
->groupEnd();
if ($client_id) {
// Add filter for particular client's policies if client_id is passed
$policyQuery->where('client_id', $client_id);
}
$clientPolicies = $policyQuery->get()->getResultArray();
// Client RM query
$rmQuery = $db->table('client_rm')
->groupStart()
->where('client_id IS NULL OR client_id = ""')
->orWhere('user_id IS NULL OR user_id = ""')
->orWhere('level IS NULL OR level = ""')
->groupEnd();
if ($client_id) {
// Add filter for particular client's RM if client_id is passed
$rmQuery->where('client_id', $client_id);
}
$clientRms = $rmQuery->get()->getResultArray();
// Combine all results into a single array
$result = [
'clients' => $clients,
'client_branches' => $clientBranches,
'client_policies' => $clientPolicies,
'client_rms' => $clientRms
];
return $result;
}
}