nhance-enrollment/app/Models/LeadsModel.php

182 lines
5.4 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class LeadsModel extends Model
{
protected $table = 'leads';
protected $primaryKey = 'id';
protected $allowedFields = [
'id',
'lead_type',
'issuer',
'client_type',
'client_name',
'client_short_name',
'entity_type_id',
'client_code',
'cost_center',
'pan',
'gst',
'branch_name',
'branch_code',
'contact_person_name',
'contact_person_mobile',
'contact_person_email',
'client_id',
'client_branch_id',
'source_policy_id',
'policy_type_id',
'salse_person_id',
'insurer_id',
'insurer_branch_id',
'tpa_id',
'tpa_branch_id',
'policy_start_date',
'policy_end_date',
'no_of_lives',
'incurred_claims',
'location',
'proposed_insurer_id',
'proposed_insurer_branch_id',
'proposed_tpa_id',
'proposed_tpa_branch_id',
'proposel_data',
'status',
'notes',
'created_at',
'created_by',
'updated_at',
'updated_by',
'is_active',
'is_client_created',
'renewal_emp_count',
'renewal_dept_count',
'renewal_no_of_lives',
'incept_emp_count',
'incept_dept_count',
'incept_no_of_lives',
'exp_emp_count',
'exp_dept_count',
'exp_no_of_lives',
'file_name',
'incurred_claims_date',
'paid_claims',
'outstanding_claims',
'policy_run_days',
'premium_at_inception',
'premium_date',
'earned_premium',
'annualised_claims',
'incurred_claims_ratio',
'earned_claims_ratio',
'placement_date',
'utr_no',
'premium_amount',
'total_amount',
'cd_amount',
'total_si_at_incept',
'total_si_at_renewal',
'fin_years_claims',
];
// 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 getLeadDataForLising()
{
return $this->select('
leads.*,
kyc_entity_type.name as entity_type,
policy_type.policy_type,
user_profiles.first_name as salse_person_name,
(
SELECT COUNT(*) AS qcr_count
FROM rfq
WHERE type = 2
AND is_active = 1 and lead_id = leads.id
) AS qcr_count,
(
SELECT COUNT(*) AS rfq_count
FROM rfq
WHERE type = 1
AND is_active = 1 and lead_id = leads.id
) AS rfq_count
')
->join('kyc_entity_type', 'leads.entity_type_id = kyc_entity_type.id', 'left')
->join('user_profiles', 'leads.salse_person_id = user_profiles.id', 'left')
->join('policy_type', 'leads.policy_type_id = policy_type.id', 'left')
->where('leads.is_active', 1)
->orderBy('id', 'desc')
->findAll();
}
public function getLeadForInsertClientList($type = null, $client_id = null)
{
$query = $this->db->table('leads')
->select('leads.*, user_profiles.first_name as user_name')
->join('user_profiles', 'leads.created_by = user_profiles.id')
->where('leads.is_active', 1)
->where('leads.status', 'won')
->where("leads.proposel_data IS NOT NULL AND leads.proposel_data <> ''")
->where("(leads.is_client_created = '' OR leads.is_client_created IS NULL)");
if ($type) {
$query->where('leads.lead_type', $type);
}
if ($client_id) {
$query->where('leads.client_id', $client_id);
}
$result = $query->get()->getResultArray();
return $result;
}
}