nhance/app/Models/CDMasterModel.php

137 lines
4.9 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class CDMasterModel extends Model
{
protected $table = 'cd_master';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'id',
'client_id',
'insurer_id',
'insurer_branch_id',
'cd_ac_no',
'opening_bal',
'cd_balance_low_alert_amount',
'opening_date',
'created_at',
'updated_at',
'created_by',
'updated_by',
'is_active',
];
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $afterInsert = [];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function checkAndADDCreatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['created_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['updated_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['updated_by'] = get_session_userid();
}
return $data;
}
public function getCDMasterList()
{
$query = $this->db->table('cd_master')
->select('cd_master.*, clients.client_name, clients.short_name, insurers.name AS insurer_name, user_profiles.first_name AS user_name, IFNULL(cd_ac_counts.cd_ac_no_count, 0) AS cd_ac_no_count, IFNULL(cd_ac_counts_for_cdt.cd_ac_no_count_cd_tranction, 0) AS cd_ac_no_count_cd_tranction, insurer_branch.branch_name as insurer_branch_name')
->join('clients', 'clients.id = cd_master.client_id')
->join('insurers', 'insurers.id = cd_master.insurer_id')
->join('insurer_branch', 'cd_master.insurer_branch_id = insurer_branch.id', "left")
->join('user_profiles', 'user_profiles.id = cd_master.created_by')
->join(
'(SELECT cd_master.cd_ac_no, cd_master.id, COUNT(client_policy.cd_ac_no) AS cd_ac_no_count
FROM cd_master
JOIN client_policy ON client_policy.cd_ac_pk = cd_master.id
WHERE client_policy.is_active = 1
AND cd_master.is_active = 1
GROUP BY cd_master.id
) AS cd_ac_counts',
'cd_ac_counts.id = cd_master.id',
'left'
)
->join(
'(SELECT cd_master.cd_ac_no, cd_master.id, COUNT(cash_deposit.cd_ac_no) AS cd_ac_no_count_cd_tranction
FROM cd_master
JOIN cash_deposit ON cash_deposit.cd_ac_pk = cd_master.id
WHERE cash_deposit.is_active = 1
AND cd_master.is_active = 1
GROUP BY cd_master.id
) AS cd_ac_counts_for_cdt',
'cd_ac_counts_for_cdt.id = cd_master.id',
'left'
)
->where('cd_master.is_active', 1)
->where('clients.is_active', 1)
->where('insurers.is_active', 1)
->where('user_profiles.is_active', 1)
->orderBy('cd_master.id', "desc")
->get();
$result = $query->getResultArray();
return $result;
}
/**
* Active CD master rows that have a low-balance alert threshold configured.
*
* @return array<int, array<string, mixed>>
*/
public function getCdMastersWithLowAlertThreshold(): array
{
return $this->db->table('cd_master')
->select('cd_master.*, clients.client_name, clients.short_name, insurers.name AS insurer_name, insurer_branch.branch_name AS insurer_branch_name')
->join('clients', 'clients.id = cd_master.client_id')
->join('insurers', 'insurers.id = cd_master.insurer_id')
->join('insurer_branch', 'insurer_branch.id = cd_master.insurer_branch_id', 'left')
->where('cd_master.is_active', 1)
->where('clients.is_active', 1)
->where('insurers.is_active', 1)
->where('cd_master.cd_balance_low_alert_amount IS NOT NULL', null, false)
->where('cd_master.cd_balance_low_alert_amount >', 0)
->orderBy('cd_master.id', 'desc')
->get()
->getResultArray();
}
}