253 lines
8.9 KiB
PHP
253 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
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()
|
|
{
|
|
$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, cp.policy_no")
|
|
->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.source_policy_id", "left")
|
|
->where("lipd.is_active", 1)
|
|
->where("lipd.utr_no IS NULL")
|
|
->where("lipd.payment_date", $this->addBusinessDays(5));
|
|
|
|
$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;
|
|
|
|
if ($sales_person_id) {
|
|
$user = $this->db->table('user_profiles')
|
|
->select('email')
|
|
->where('id', (int) $sales_person_id)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$row['sales_person'] = $user['email'] ?? 'N/A';
|
|
} else {
|
|
$row['sales_person'] = '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;
|
|
}
|
|
}
|