178 lines
7.5 KiB
PHP
178 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\ClaimModel;
|
|
use App\Models\PolicyModel;
|
|
use App\Models\EnquiryModel;
|
|
use App\Models\QuotationModel;
|
|
use App\Models\ClaimStatusModel;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
|
|
class ClaimController extends ResourceController
|
|
{
|
|
protected $db;
|
|
protected $ClaimModel;
|
|
protected $PolicyModel;
|
|
protected $QuotationModel;
|
|
protected $EnquiryModel;
|
|
protected $ClaimStatusModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = db_connect();
|
|
$this->ClaimModel = new ClaimModel();
|
|
$this->PolicyModel = new PolicyModel();
|
|
$this->QuotationModel = new QuotationModel();
|
|
$this->EnquiryModel = new EnquiryModel();
|
|
$this->ClaimStatusModel = new ClaimStatusModel();
|
|
}
|
|
|
|
// Get all tickets
|
|
public function ClaimList()
|
|
{
|
|
try {
|
|
|
|
$id = $this->request->getGet('id'); // claim id
|
|
$manager_id = $this->request->getGet('manager_id');
|
|
$agent_id = $this->request->getGet('agent_id');
|
|
$policy_number = $this->request->getGet('policy_number');
|
|
|
|
|
|
$builder = $this->db->table('ticket_master tm')
|
|
->select('tm.*,
|
|
i.name as insurer_name,
|
|
c.client_name, c.phone as client_phone, c.email as client_email,
|
|
pct.claim_type as claim_type_value,
|
|
pa.name as agent_name,
|
|
tcs.claim_status as claim_status_value,
|
|
pp.start_date as policy_start_date , pp.end_date as policy_end_date,
|
|
v.vehicle_no as reg_no')
|
|
->join('insurers i', 'i.id = tm.insurer_id', 'left')
|
|
->join('clients c', 'c.id = tm.client_id', 'left')
|
|
->join('partner_claim_type_master pct', 'pct.id = tm.claim_type', 'left')
|
|
->join('partner_agent pa', 'pa.id = tm.agent_id', 'left')
|
|
->join('ticket_claim_status tcs', 'tcs.id = tm.claim_status_id AND tcs.ticket_type = 8', 'left')
|
|
->join('partner_policy pp', 'pp.policy_number = tm.policy_no', 'left')
|
|
->join('vehicle v', 'v.id = tm.vehicle_id', 'left')
|
|
->where('tm.ticket_type_id', 8);
|
|
|
|
// If ID is provided, fetch single record
|
|
if (!empty($id)) {
|
|
$builder->where('tm.id', $id);
|
|
$ticket = $builder->get()->getRowArray();
|
|
|
|
if (!$ticket) {
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Not Found'], 404);
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $ticket], 200);
|
|
}
|
|
|
|
// Apply filters
|
|
if (!empty($manager_id)) {
|
|
$builder->where('tm.manager_id', $manager_id);
|
|
}
|
|
|
|
if (!empty($agent_id)) {
|
|
$builder->where('tm.agent_id', $agent_id);
|
|
}
|
|
|
|
if (!empty($policy_number)) {
|
|
$builder->where('tm.policy_no', $policy_number);
|
|
}
|
|
|
|
$data = $builder->get()->getResultArray();
|
|
|
|
foreach ($data as $key => $row) {
|
|
$data[$key]['created_at'] = date('d-m-Y h:i A', strtotime($row['created_at']));
|
|
$data[$key]['updated_at'] = date('d-m-Y h:i A', strtotime($row['updated_at']));
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Create new ticket
|
|
public function createClaim()
|
|
{
|
|
try {
|
|
$reqData = $this->request->getPost();
|
|
|
|
// Validate required fields
|
|
if (empty($reqData['policy_number']) || empty($reqData['claim_description'] || empty($reqData['claim_type']))) {
|
|
return $this->respond(['status' => 'failed','code' => 400, 'data' => 'policy_number and claim_description and claim_type are required'], 200);
|
|
}
|
|
|
|
// Fetch policy details from partner_policy
|
|
$policy = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.insurer_branch_id,E.name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no')
|
|
->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id AND Q.status = "Accepted"', 'left')
|
|
->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left')
|
|
->where('partner_policy.policy_number',$reqData['policy_number'])
|
|
->first();
|
|
|
|
if (empty($policy)) {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 404,
|
|
'data' => 'Policy not found'
|
|
], 200);
|
|
}
|
|
|
|
$uploadFile = $this->request->getFile('claim_file_name');
|
|
$uploadedFileName = null;
|
|
|
|
if ($uploadFile && $uploadFile->isValid()) {
|
|
$uploadPath = WRITEPATH . 'uploads/claims/';
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0777, true);
|
|
}
|
|
$uploadedFileName = time() . '_' . $uploadFile->getRandomName();
|
|
$uploadFile->move($uploadPath, $uploadedFileName);
|
|
}
|
|
|
|
|
|
$status = $this->ClaimStatusModel->where('ticket_type', 8)->first();
|
|
|
|
// Prepare claim data for ticket_master
|
|
$claimData = [
|
|
'ticket_type_id' => 8,
|
|
'claim_status_id' => $status['id'],
|
|
'policy_no' => $policy['policy_number'],
|
|
'client_policy_id' => $policy['client_policy_id'],
|
|
'insurer_id' => $policy['insurer_id'],
|
|
'client_id' => $policy['client_id'] ?? null,
|
|
'agent_id' => $policy['agent_id'] ?? null,
|
|
'manager_id' => $policy['manager_id'] ?? null,
|
|
'vehicle_id' => $policy['vehicle_id'] ?? null,
|
|
'insured_name' => $policy['insured_name'] ?? null,
|
|
'emp_name' => $policy['client_name'] ?? null,
|
|
'emp_mobile' => $policy['client_mobile'] ?? null,
|
|
'emp_mail' => $policy['client_email'] ?? null,
|
|
'emp_personal_mail'=> $policy['client_email'] ?? null,
|
|
'claim_type' => $reqData['claim_type'],
|
|
'claim_description'=> $reqData['claim_description'],
|
|
'created_by' => $reqData['created_by'] ?? null,
|
|
'claim_file_name' => $uploadedFileName,
|
|
];
|
|
|
|
// Insert claim
|
|
if (!$this->ClaimModel->insert($claimData)) {
|
|
return $this->respond(['status' => 'failed','code' => 422,'data' => $this->ClaimModel->errors()], 422);
|
|
}
|
|
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => ['claim_id' => $this->ClaimModel->getInsertID()]], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|