316 lines
12 KiB
PHP
316 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
use Config\BdsConfig;
|
|
|
|
class BdsPlacementModel extends Model
|
|
{
|
|
protected $table = 'bds_installment_payment_details';
|
|
protected $primaryKey = 'id';
|
|
protected $allowedFields = [
|
|
'id',
|
|
'pt_id',
|
|
'lead_id',
|
|
'installment_amount',
|
|
'payment_date',
|
|
'utr_no',
|
|
'created_at',
|
|
'created_by',
|
|
'updated_at',
|
|
'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 getClientInstallmentDetails()
|
|
{
|
|
// 1. Fetch common data ONCE (Heads, Admins, Business Team)
|
|
// This saves hundreds of redundant queries
|
|
$heads = $this->db->table('user_profiles')
|
|
->select('email')->where(['role' => 5, 'is_active' => 1])
|
|
->get()->getResultArray();
|
|
|
|
$admins = $this->db->table('user_profiles')
|
|
->select('email')->where(['role' => 1, 'is_active' => 1])
|
|
->get()->getResultArray();
|
|
|
|
$businessTeam = $this->db->table("user_teams ut")
|
|
->select("up.email")
|
|
->join('user_profiles up', 'up.id = ut.user_id AND up.is_active = 1')
|
|
->where(["ut.team_id" => 7, "ut.is_active" => 1])
|
|
->get()->getResultArray();
|
|
|
|
// 2. Main Query
|
|
$builder = $this->db->table("bds_installment_payment_details bipd")
|
|
->select("bipd.*, ct.client_name, ct.short_name, cb.branch_name, leads.salse_person_id, cp.policy_no")
|
|
->join("policy_transaction pt", "pt.id = bipd.pt_id")
|
|
->join("clients ct", "ct.id = pt.client_id")
|
|
->join("client_branch cb", "cb.id = pt.client_branch_id")
|
|
->join("leads", "leads.id = bipd.lead_id")
|
|
->join("client_policy cp", "cp.id = pt.client_policy_id")
|
|
->where("bipd.is_active", 1)
|
|
->where("bipd.payment_date", date('Y-m-d', strtotime('+15 days')));
|
|
|
|
$data = $builder->get()->getResultArray();
|
|
|
|
// Loop through to extract sales person details
|
|
// 3. Simple Loop for Sales Person (Since it relies on a JSON ID)
|
|
foreach ($data as &$row) {
|
|
$sales_person_ids = json_decode($row['salse_person_id'], true);
|
|
$sales_person_id = $sales_person_ids[0] ?? null;
|
|
|
|
if ($sales_person_id) {
|
|
$user = $this->db->table('user_profiles')
|
|
->select('email') // or whatever field
|
|
->where('id', (int)$sales_person_id) // Cast to int for extra safety
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$row['sales_person'] = $user['email'] ?? 'N/A';
|
|
} else {
|
|
$row['sales_person'] = 'Not Assigned';
|
|
}
|
|
|
|
// Assign the pre-fetched data
|
|
$row['heads'] = $heads;
|
|
$row['admins'] = $admins;
|
|
$row['buisness_team'] = $businessTeam;
|
|
}
|
|
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function addBusinessDays(int $days, ?string $fromDate = null): string
|
|
{
|
|
$date = new \DateTime($fromDate ?? 'today');
|
|
$added = 0;
|
|
|
|
while ($added < $days) {
|
|
$date->modify('+1 day');
|
|
if ((int) $date->format('N') < 6) {
|
|
$added++;
|
|
}
|
|
}
|
|
|
|
return $date->format('Y-m-d');
|
|
}
|
|
|
|
public function getLeadInstallmentDetails()
|
|
{
|
|
/** @var BdsConfig $config */
|
|
$config = config(BdsConfig::class);
|
|
|
|
if (!$config->shouldFetchInstallmentRemindersToday()) {
|
|
return [];
|
|
}
|
|
|
|
// $heads = $this->db->table('user_profiles')
|
|
// ->select('email')->where(['role' => 5, 'is_active' => 1])
|
|
// ->get()->getResultArray();
|
|
|
|
// $admins = $this->db->table('user_profiles')
|
|
// ->select('email')->where(['role' => 1, 'is_active' => 1])
|
|
// ->get()->getResultArray();
|
|
|
|
// $businessTeam = $this->db->table("user_teams ut")
|
|
// ->select("up.email")
|
|
// ->join('user_profiles up', 'up.id = ut.user_id AND up.is_active = 1')
|
|
// ->where(["ut.team_id" => 7, "ut.is_active" => 1])
|
|
// ->get()->getResultArray();
|
|
|
|
$builder = $this->db->table("lead_installment_payment_details lipd")
|
|
->select("
|
|
lipd.*,
|
|
COALESCE(ct.client_name, leads.client_name) as client_name,
|
|
COALESCE(ct.short_name, leads.client_short_name) as short_name,
|
|
COALESCE(cb.branch_name, leads.branch_name) as branch_name,
|
|
leads.salse_person_id,
|
|
leads.contact_person_email,
|
|
leads.contact_person_name,
|
|
leads.contact_person_mobile,
|
|
COALESCE(cp.policy_no, cp_src.policy_no) as policy_no,
|
|
COALESCE(cp.policy_start_date, cp_src.policy_start_date) as policy_start_date,
|
|
COALESCE(cp.policy_end_date, cp_src.policy_end_date) as policy_end_date,
|
|
COALESCE(ins.name, ins_src.name) as insurer_name,
|
|
COALESCE(pt.policy_type, pt_src.policy_type) as policy_type,
|
|
leads.acm_id,
|
|
leads.is_policy_created
|
|
")
|
|
->join("leads", "leads.id = lipd.lead_id")
|
|
->join("clients ct", "ct.id = leads.client_id", "left")
|
|
->join("client_branch cb", "cb.id = leads.client_branch_id", "left")
|
|
->join("client_policy cp", "cp.id = leads.is_policy_created", "left")
|
|
->join("client_policy cp_src", "cp_src.id = leads.source_policy_id", "left")
|
|
->join("insurers ins", "ins.id = cp.insurer_id", "left")
|
|
->join("insurers ins_src", "ins_src.id = cp_src.insurer_id", "left")
|
|
->join("policy_type pt", "pt.id = cp.policy_type_id", "left")
|
|
->join("policy_type pt_src", "pt_src.id = cp_src.policy_type_id", "left")
|
|
->where("lipd.is_active", 1)
|
|
->groupStart()
|
|
->where('lipd.utr_no IS NULL')
|
|
->orWhere('lipd.utr_no', '')
|
|
->groupEnd();
|
|
|
|
$targetPaymentDate = $this->addBusinessDays($config->installmentReminderBusinessDays);
|
|
|
|
if ($config->installmentReminderFetchOverduePendingUtr) {
|
|
$builder->groupStart()
|
|
->where('lipd.payment_date', $targetPaymentDate)
|
|
->orWhere('lipd.payment_date <', date('Y-m-d'))
|
|
->groupEnd();
|
|
} else {
|
|
$builder->where('lipd.payment_date', $targetPaymentDate);
|
|
}
|
|
|
|
$data = $builder->get()->getResultArray();
|
|
|
|
foreach ($data as &$row) {
|
|
$sales_person_ids = json_decode($row['salse_person_id'], true);
|
|
$sales_person_id = $sales_person_ids[0] ?? null;
|
|
$acm_id = $row['acm_id'] ?? null;
|
|
|
|
if ($acm_id) {
|
|
$acm_user = $this->db->table('user_profiles')
|
|
->select('email, first_name, last_name')
|
|
->where('id', (int) $acm_id)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$row['acm_email'] = $acm_user['email'] ?? 'N/A';
|
|
$row['acm_name'] = trim(
|
|
($acm_user['first_name'] ?? '') . ' ' . ($acm_user['last_name'] ?? '')
|
|
) ?: 'Not Assigned';
|
|
} else {
|
|
$row['acm_email'] = 'N/A';
|
|
$row['acm_name'] = 'Not Assigned';
|
|
}
|
|
|
|
if ($sales_person_id) {
|
|
$user = $this->db->table('user_profiles')
|
|
->select('email, first_name, last_name')
|
|
->where('id', (int) $sales_person_id)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$row['sales_person'] = $user['email'] ?? 'N/A';
|
|
$row['sales_person_name'] = trim(
|
|
($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')
|
|
) ?: 'Not Assigned';
|
|
} else {
|
|
$row['sales_person'] = 'Not Assigned';
|
|
$row['sales_person_name'] = 'Not Assigned';
|
|
}
|
|
|
|
// $row['heads'] = $heads;
|
|
// $row['admins'] = $admins;
|
|
// $row['buisness_team'] = $businessTeam;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getClientInstallmentDetails_olds()
|
|
{
|
|
|
|
$builder = $this->db->table("bds_installment_payment_details bipd")
|
|
->select("bipd.*, ct.client_name,ct.short_name, cb.branch_name, leads.salse_person_id,cp.policy_no")
|
|
->join("policy_transaction pt", "pt.id = bipd.pt_id")
|
|
->join("clients ct", "ct.id = pt.client_id")
|
|
->join("client_branch cb", "cb.id = pt.client_branch_id")
|
|
->join("leads", "leads.id = bipd.lead_id")
|
|
->join("client_policy cp","cp.id = pt.client_policy_id")
|
|
->where("bipd.is_active", 1)
|
|
->where("bipd.payment_date", date('Y-m-d', strtotime('+15 days')));
|
|
|
|
$data = $builder->get()->getResultArray();
|
|
|
|
// Loop through to extract sales person details
|
|
foreach ($data as &$row) {
|
|
$sales_person_ids = json_decode($row['salse_person_id'], true);
|
|
$sales_person_id = $sales_person_ids[0] ?? null;
|
|
|
|
if ($sales_person_id) {
|
|
$user = $this->db->table('user_profiles')
|
|
->select('email') // or whatever field
|
|
->where('id', $sales_person_id)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$row['sales_person'] = $user['email'] ?? 'N/A';
|
|
} else {
|
|
$row['sales_person'] = 'Not Assigned';
|
|
}
|
|
|
|
$head = $this->db->table('user_profiles')
|
|
->select('email') // or whatever field
|
|
->where('role', 5)
|
|
->where('is_active',1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
|
|
$row['heads'] = $head;
|
|
|
|
$admin = $this->db->table('user_profiles')
|
|
->select('email')
|
|
->where("is_active",1)
|
|
->where("role",1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$row['admins'] = $admin;
|
|
|
|
$buisenessTeam = $this->db->table("user_teams ut")
|
|
->select("up.email")
|
|
->join('user_profiles up','up.id = ut.user_id and up.is_active = 1')
|
|
->where("ut.team_id",7)
|
|
->where("ut.is_active",1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$row['buisness_team'] = $buisenessTeam;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
}
|
|
}
|