nhance/app/Controllers/LeadsController.php

300 lines
12 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;
use App\Models\ClientModel;
use App\Models\ClientBranchModel;
use App\Models\ClientPolicyModel;
use App\Models\LevelContactModel;
use App\Models\LeadsModel;
use App\Models\PolicyTypeModel;
use App\Models\KYCEntityTypeModel;
use App\Models\PolicyTransactionStatusModel;
use App\Models\InsurerBranchModel;
use App\Models\TPABranchModel;
class LeadsController extends BaseController
{
use ResponseTrait;
//log message
protected $myLogger;
//models
protected $clientModel;
protected $userModel;
protected $clientBranchModel;
protected $clientPolicyModel;
protected $levelContactModel;
protected $leadsModel;
protected $policyTypeModel;
protected $kycEntityTypeModel;
protected $policyTransactionStatusModel;
protected $insurerBranchModel;
protected $tpaBranchModel;
//variables for storing array
protected $issuer;
protected $clientType;
protected $leadType;
protected $leadsStatus;
public function __construct()
{
set_session_context('Leads');
$this->myLogger = \Config\Services::mylogger();
$this->clientModel = new ClientModel();
$this->userModel = new UserModel();
$this->clientBranchModel = new ClientBranchModel();
$this->clientPolicyModel = new ClientPolicyModel();
$this->levelContactModel = new LevelContactModel();
$this->leadsModel = new LeadsModel();
$this->policyTypeModel = new PolicyTypeModel();
$this->kycEntityTypeModel = new KYCEntityTypeModel();
$this->policyTransactionStatusModel = new PolicyTransactionStatusModel();
$this->insurerBranchModel = new InsurerBranchModel();
$this->tpaBranchModel = new TPABranchModel();
$this->issuer = [1 => 'JIBS', 2 => 'Nhance'];
$this->clientType = [1 => 'Group', 2 => 'Individual'];
$this->leadType = [1 => 'Fresh', 2 => 'Renewal', 3 => 'Roll Over'];
$this->leadsStatus = [
'queued' => 'Queued',
'qcr_sent' => 'QCR sent',
'lost' => 'Lost',
'co_insurer_pending' => 'Co-Insurer Pending',
'won' => 'Won',
'completed_with_corrections' => 'Completed with Corrections',
'completed_without_corrections' => 'Completed w/o Corrections',
];
}
public function viewLeadsList()
{
$data['page_name'] = 'Leads';
// Set basic data
$data['issuer'] = $this->issuer;
$data['client_type'] = $this->clientType;
$data['lead_type'] = $this->leadType;
$data['lead_status'] = $this->leadsStatus;
// Fetch policy types and entity data
$data['policy_type'] = $this->policyTypeModel->where('is_active', 1)->findAll();
$data['entity'] = $this->kycEntityTypeModel->where('is_active', 1)->findAll();
// Fetch insurer and TPA branch data
$data['insurer'] = $this->insurerBranchModel->getInsurerBranchesWithInsurerNames();
$data['tpa'] = $this->tpaBranchModel->getTpaBranchesWithTpaNames();
// Fetch sales team members who are active in team 5
$data['salse_team'] = $this->userModel
->select('user_profiles.*')
->join('user_teams', 'user_profiles.id = user_teams.user_id')
->where('user_teams.team_id', 5)
->where('user_teams.is_active', 1)
->where('user_profiles.is_active', 1)
->findAll();
// dd($data);
// Fetch leads data
$data['lead_data_list'] = $this->leadsModel->getLeadDataForLising();
// Load layout and pass data
$this->loadLayout('leads_list', $data);
}
public function createLead()
{
// print_r($this->request->getPost()); die;
$id = $this->request->getPost('id');
$data = $this->prepareLeadData();
// print_r($this->request->getPost()); die;
if (!$id) {
return $this->insertNewLead($data);
} else {
return $this->updateOldLead($id, $data);
}
}
private function prepareLeadData()
{
$data = $this->request->getPost();
if($data['lead_type'] == 2){
$client_data = $this->clientModel->where('id', $data['client_id'])->where('is_active', 1)->first();
$data['client_name'] = $client_data['client_name'];
$data['client_short_name'] = $client_data['short_name'];
$data['entity_type_id'] = $client_data['entity_type_id'];
$data['client_type'] = $client_data['client_type'];
}else{
$data['client_id'] = 0;
$data['client_branch_id'] = 0;
$data['source_policy_id'] = 0;
}
$data['client_code'] = generate_client_code();
if($data['client_code'] == 2){
$data['client_code'] = generate_client_code('IC');
}
$data = $this->prepareMultipleLeadData($data);
// print_r($data); die;
return $data;
}
private function prepareMultipleLeadData($data)
{
$processedData = [];
foreach($data['policy_type_id'] as $index => $value){
// Separate the insurer and insurer branch
list($insurer_branch_id, $insurer_id) = explode('-',$data['insurer'][$index]);
// Separate the tpa and tpa branch
list($tpa_branch_id, $tpa_id) = explode('-',$data['tpa'][$index]);
// Separate the insurer and insurer branch, handle missing or invalid data
if (isset($data['proposed_insurer'][$index]) && strpos($data['proposed_insurer'][$index], '-') !== false) {
list($proposed_insurer_branch_id, $proposed_insurer_id) = explode('-', $data['proposed_insurer'][$index]);
} else {
$proposed_insurer_branch_id = 0;
$proposed_insurer_id = 0;
}
// Separate the TPA and TPA branch, handle missing or invalid data
if (isset($data['proposed_tpa'][$index]) && strpos($data['proposed_tpa'][$index], '-') !== false) {
list($proposed_tpa_branch_id, $proposed_tpa_id) = explode('-', $data['proposed_tpa'][$index]);
} else {
$proposed_tpa_branch_id = 0;
$proposed_tpa_id = 0;
}
if(!empty($data['policy_start_date'][$index])){
$policy_start_date = change_date_format($data['policy_start_date'][$index], 'd/m/Y', 'Y-m-d');
}else{
$policy_start_date = null;
}
if(!empty($data['policy_end_date'][$index])){
$policy_end_date = change_date_format($data['policy_end_date'][$index], 'd/m/Y', 'Y-m-d');
}else{
$policy_end_date = null;
}
$processedData[] = [
'lead_type' => $data['lead_type'],
'issuer' => $data['issuer'],
'client_type' => $data['client_type'],
'client_name' => $data['client_name'],
'client_short_name' => $data['client_short_name'],
'entity_type_id' => $data['entity_type_id'],
'client_code' => $data['client_code'],
'pan' => $data['pan'],
'gst' => $data['gst'],
'branch_name' => $data['branch_name'],
'branch_code' => $data['branch_code'],
'contact_person_name' => $data['contact_person_name'],
'contact_person_mobile' => $data['contact_person_mobile'],
'client_id' => $data['client_id'] ?? 0,
'client_branch_id' => $data['client_branch_id'] ?? 0,
'source_policy_id' => $data['source_policy_id'] ?? 0,
'policy_type_id' => $value,
'salse_person_id' => $data['salse_person_id'] ?? 0,
'insurer_id' => $insurer_id ?? 0,
'insurer_branch_id' => $insurer_branch_id ?? 0,
'tpa_id' => $tpa_id ?? 0,
'tpa_branch_id' => $tpa_branch_id ?? 0,
'policy_start_date' => $policy_start_date,
'policy_end_date' => $policy_end_date,
'no_of_lives' => $data['no_of_lives'][$index] ?? null,
'claims' => $data['claims'][$index] ?? null,
'location' => $data['location'][$index] ?? null,
'proposed_insurer_id' => $proposed_insurer_id ?? 0,
'proposed_insurer_branch_id' => $proposed_insurer_branch_id ?? 0,
'proposed_tpa_id' => $proposed_tpa_id ?? 0,
'proposed_tpa_branch_id' => $proposed_tpa_branch_id ?? 0,
'status' => $data['status'] ?? null,
'notes' => $data['notes'] ?? null,
];
}
return $processedData;
}
private function insertNewLead($data)
{
$insertCount = [];
foreach($data as $value){
$insert = $this->leadsModel->insert($value);
$insertCount[] = $insert;
$this->insertLeadStatus($insert, $value['status'], 3);
}
if (count($insertCount) > 0) {
return $this->respond(['status' => true, 'lead_id' => $insert, 'message' => 'New Lead created successfully', 'data' =>$data], 200);
}
return $this->respond(['status' => false, 'lead_id' => $insert, 'message' => "Failed to create Lead", 'data' =>$data], 200);
}
private function updateOldLead($id, $data)
{
if ($this->leadsModel->where('id', $id)->set($data[0])->update()) {
$this->insertLeadStatus($id, $data[0]['status'], 3);
return $this->respond(['status' => true, 'lead_id' => $id, 'message' => "Lead updated successfully", 'data' =>$data], 200);
}
return $this->respond(['status' => false, 'lead_id' => $id, 'message' => "Failed to update Lead", 'data' =>$data], 200);
}
// Get the Single Lead data for edit
public function getLeadDataForEdit($id)
{
$data = $this->leadsModel
->where('leads.id', $id)
->where('leads.is_active', 1)
->first();
if(!empty($data['policy_start_date'])){
$data['policy_start_date'] = change_date_format($data['policy_start_date'], 'Y-m-d', 'd/m/Y');
}else{
$data['policy_start_date'] = null;
}
if(!empty($data['policy_end_date'])){
$data['policy_end_date'] = change_date_format($data['policy_end_date'], 'Y-m-d', 'd/m/Y');
}else{
$data['policy_end_date'] = null;
}
if($data){
return $this->respond(['status' => true, 'data' => $data], 200);
}else{
return $this->respond(['status' => false], 200);
}
}
private function insertLeadStatus($primaryKey, $status, $statusType)
{
$statusData = [
'policy_tran_id' => $primaryKey,
'status' => $status,
'status_type' => $statusType,
'created_by' => get_session_userid(),
];
$this->policyTransactionStatusModel->insert($statusData);
}
}