268 lines
10 KiB
PHP
268 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Helpers\MailHelper;
|
|
use App\Helpers\sendMailNotification;
|
|
|
|
use App\Models\EmployeeModel;
|
|
use App\Models\EmployeePolicyModel;
|
|
use App\Models\ClientModel;
|
|
use App\Models\ClientRMModel;
|
|
use App\Models\PolicesModel;
|
|
use App\Models\RelationshipModel;
|
|
use App\Models\FileModel;
|
|
use App\Models\ClientPolicyModel;
|
|
use App\Models\PolicyPremium1Model;
|
|
use App\Models\PolicyPremium2Model;
|
|
use App\Models\PolicyTypeModel;
|
|
use App\Models\NotificationModel;
|
|
use App\Models\UserModel;
|
|
use App\Models\FEContentModel;
|
|
use App\Models\AddImgModel;
|
|
use App\Models\ChatBotModel;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
class ChatBotController extends AdminController
|
|
{
|
|
|
|
use ResponseTrait;
|
|
|
|
protected $myLogger;
|
|
protected $employeeModel;
|
|
protected $employeePolicyModel;
|
|
protected $clientModel;
|
|
protected $clientRMModel;
|
|
protected $fileModel;
|
|
protected $policesModel;
|
|
protected $relationshipModel;
|
|
protected $clientPolicyModel;
|
|
protected $policyPremium1Model;
|
|
protected $policyPremium2Model;
|
|
protected $policyTypeModel;
|
|
protected $notificationModel;
|
|
protected $userModel;
|
|
protected $feContentModel;
|
|
protected $addImgModel;
|
|
protected $ChatBotModel;
|
|
|
|
public function __construct()
|
|
{
|
|
|
|
set_session_context('Employee');
|
|
$this->myLogger = \Config\Services::mylogger();
|
|
$this->employeeModel = new EmployeeModel();
|
|
$this->employeePolicyModel = new EmployeePolicyModel();
|
|
$this->clientModel = new ClientModel();
|
|
$this->clientRMModel = new ClientRMModel();
|
|
$this->policesModel = new PolicesModel();
|
|
$this->relationshipModel = new RelationshipModel();
|
|
$this->fileModel= new FileModel();
|
|
$this->clientPolicyModel = new ClientPolicyModel();
|
|
$this->policyPremium1Model = new PolicyPremium1Model();
|
|
$this->policyPremium2Model = new PolicyPremium2Model();
|
|
$this->policyTypeModel = new PolicyTypeModel();
|
|
$this->notificationModel = new NotificationModel();
|
|
$this->userModel = new UserModel();
|
|
$this->feContentModel = new FEContentModel();
|
|
$this->addImgModel = new AddImgModel();
|
|
$this->ChatBotModel = new ChatBotModel();
|
|
}
|
|
|
|
|
|
public function getChatResponse()
|
|
{
|
|
try {
|
|
$request_for = $this->request->getGet('request_for');
|
|
$option = $this->request->getGet('option');
|
|
$is_option = $this->request->getGet('is_option');
|
|
$custom_options = $this->request->getGet('custom_options');
|
|
|
|
$responseData = $this->ChatBotModel->where('request', $request_for)
|
|
->where('is_active', 1 )
|
|
->first();
|
|
|
|
if ($responseData) {
|
|
|
|
|
|
if(isset($option) && $option != 0){
|
|
|
|
$decodedData = json_decode($responseData['options'],true);
|
|
|
|
$requestFor = $decodedData[$option];
|
|
|
|
$responseData = $this->ChatBotModel->where('request', $requestFor)
|
|
->where('is_active', 1 )
|
|
->first();
|
|
}
|
|
|
|
if(isset($custom_options))
|
|
{
|
|
|
|
}
|
|
|
|
if(isset($is_option) && $is_option == 0){
|
|
|
|
$text = $this->request->getGet('text');
|
|
$value = $this->request->getGet('value');
|
|
$Data = $this->ChatBotModel->where('options', $text)->where('is_active', 1 )->first();
|
|
if($Data){
|
|
|
|
if($Data['options'] == 'Please Enter Your Mobile Number To View Your Policies')
|
|
{
|
|
$result = $this->getAllPolicyByMobileNumber($value,'text');
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result ],200);
|
|
}else if($Data['options'] == 'Please Enter Your Mobile Number To Raise Ticket'){
|
|
$result = $this->getAllPolicyByMobileNumber($value,'options');
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result ],200);
|
|
}
|
|
|
|
}else{
|
|
|
|
$result = $this->sendDefaultMessage();
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result ],200);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = [
|
|
'request_for' => $responseData['request'],
|
|
'options' => json_decode($responseData['options'], true),
|
|
'text' => $responseData['is_option'] != 1 ? $responseData['options'] : null,
|
|
'is_option' => $responseData['is_option']
|
|
];
|
|
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result ],200);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => 'No Data'],404);
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
|
|
public function sendDefaultMessage()
|
|
{
|
|
$responseData = $this->ChatBotModel->where('id', 1)->where('is_active', 1 )->first();
|
|
return [
|
|
'request_for' => $responseData['request'],
|
|
'options' => json_decode($responseData['options'], true),
|
|
'text' => $responseData['is_option'] != 1 ? $responseData['options'] : null,
|
|
'is_option' => $responseData['is_option']
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
public function getAllPolicyByMobileNumber($mobileNo,$action)
|
|
{
|
|
$employee = $this->employeeModel->where('mobile', $mobileNo)->where('is_active', 1 )->first();
|
|
if($employee){
|
|
|
|
$employeeData = $this->employeeModel->where('emp_code', $employee['emp_code'])
|
|
->where('client_id',$employee['client_id'])
|
|
->where('client_branch_id',$employee['client_branch_id'])
|
|
->where('is_active', 1 )->findAll();
|
|
$whereArrayForId = [];
|
|
foreach ( $employeeData as $key => $value) { array_push($whereArrayForId, $value['id']); }
|
|
|
|
$ClientPolicyData = $this->clientPolicyModel->select('client_policy.* , policies.name as policy_name , policy_type.policy_type as policy_type')
|
|
->join('policies', 'client_policy.policy_id = policies.id', 'left')
|
|
->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left')
|
|
->where('client_policy.client_id', $employee['client_id'] )
|
|
->where('client_policy.client_branch_id', $employee['client_branch_id'] )
|
|
->where('client_policy.is_active', 1 )
|
|
->where('client_policy.policy_status', 1)
|
|
->orderby('client_policy.id' , 'ASC')
|
|
->findAll();
|
|
$options = [];
|
|
$text = '';
|
|
if(count($ClientPolicyData) > 0 && count($employeeData) > 0)
|
|
{
|
|
|
|
foreach ($ClientPolicyData as $key => $ClientPolicyValue) {
|
|
|
|
$data['policy_name'] = $ClientPolicyValue['policy_name'];
|
|
$data['policy_type'] = $ClientPolicyValue['policy_type'];
|
|
$data['policy_no'] = $ClientPolicyValue['policy_no'];
|
|
|
|
|
|
|
|
$employee_policy = $this->employeePolicyModel->select('employees.*,employee_polices.employee_id , employee_polices.basic_cover_si , employee_polices.premium , employee_polices.gst , employee_polices.tpa_id , employee_polices.rand_string , employee_polices.uhid as uhid')
|
|
->join('employees', 'employee_polices.employee_id = employees.id', 'left')
|
|
->whereIn('employee_polices.employee_id',$whereArrayForId)
|
|
->where('employee_polices.client_policy_id',$ClientPolicyValue['id'])
|
|
->where('employee_polices.is_active', 1 )->findAll();
|
|
if(count($employee_policy) > 0)
|
|
{
|
|
$policy[$ClientPolicyValue['id']] = $ClientPolicyValue['policy_name']."(".$ClientPolicyValue['policy_type'].")";
|
|
array_push($options, $policy);
|
|
$text .= $ClientPolicyValue['policy_name']."(".$ClientPolicyValue['policy_type'].") % ";
|
|
}else{
|
|
$text .= "There is no any active policies ";
|
|
}
|
|
|
|
}
|
|
if($action == 'options'){
|
|
return [
|
|
'request_for' => $this->request->getGet('request_for'),
|
|
'options' => $options[count($options) - 1],
|
|
'text' => null,
|
|
'is_option' => "1",
|
|
'custom_options'=>'create_ticket'
|
|
];
|
|
}else{
|
|
return [
|
|
'request_for' => $this->request->getGet('request_for'),
|
|
'options' => null,
|
|
'text' => $text,
|
|
'is_option' => "0"
|
|
];
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
return [
|
|
'request_for' => $this->request->getGet('request_for'),
|
|
'options' => null,
|
|
'text' => "Dear ".$employee['name'].", % Policy not yet bound for you.",
|
|
'is_option' => "0"
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
return [
|
|
'request_for' => $this->request->getGet('request_for'),
|
|
'options' => null,
|
|
'text' => "Invalid Mobile Number",
|
|
'is_option' => "0"
|
|
];
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|