nhance/app/Controllers/LeadsController.php

222 lines
7.8 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;
class LeadsController extends AdminController
{
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;
//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->issuer = [1 => 'JIBS', 2 => 'Nhance'];
$this->clientType = [1 => 'Group', 2 => 'Individual'];
$this->leadType = [1 => 'Fresh', 2 => 'Renewal'];
$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';
$data['issuer'] = $this->issuer ;
$data['client_type'] = $this->clientType;
$data['lead_type'] = $this->leadType;
$data['lead_status'] = $this->leadsStatus;
$data['policy_type'] = $this->policyTypeModel->where('is_active', 1)->findAll();
$data['entity'] = $this->kycEntityTypeModel->where('is_active', 1)->findAll();
$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();
$data['lead_data_list'] = $this->leadsModel->getLeadDataForLising();
// dd($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);
return $data;
}
private function prepareMultipleLeadData($data)
{
$processedData = [];
foreach($data['policy_type_id'] as $index => $value){
$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'],
'cost_center' => $data['cost_center'],
'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'],
'client_branch_id' => $data['client_branch_id'],
'source_policy_id' => $data['source_policy_id'],
'policy_type_id' => $value,
'salse_person_id' => $data['salse_person_id'],
'status' => $data['status'],
'notes' => $data['notes'],
'created_by' => $data['created_by'],
'updated_by' => $data['updated_by'],
'is_active' => $data['is_active'],
];
}
return $processedData;
}
private function insertNewLead($data)
{
$insert = $this->leadsModel->insert($data);
if ($insert) {
$this->insertLeadStatus($insert, $data['status'], 3);
return $this->respond(['status' => true, 'lead_id' => $insert, 'message' => 'New Lead created successfully', 'data' =>$data], 200);
}
return $this->respond(['status' => true, 'lead_id' => $insert, 'message' => "Failed to create Lead", 'data' =>$data], 200);
}
private function updateOldLead($id, $data)
{
if ($this->leadsModel->update($id, $data)) {
$this->insertLeadStatus($id, $data['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($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);
}
}