nhance_partner_be/app/Controllers/QuotationController.php
2025-09-06 12:15:29 +05:30

215 lines
8.4 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Models\QuotationModel;
use App\Models\EnquiryModel;
class QuotationController extends ResourceController
{
protected $QuotationModel;
protected $EnquiryModel;
public function __construct()
{
$this->QuotationModel = new QuotationModel();
$this->EnquiryModel = new EnquiryModel();
}
// List quotations
public function quotationList()
{
try {
$manager_id = $this->request->getGet('manager_id');
$data = $this->QuotationModel->select('partner_quotation.* , IT.insurance_plan_type')
->join('partner_insurance_plan_type_master IT', 'IT.id = partner_quotation.insurance_plan_type_id', 'left')
->where('partner_quotation.manager_id',$manager_id)
->findAll();
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Find single quotation
public function findQuotation()
{
try {
$id = $this->request->getGet('id');
$record = $this->QuotationModel->find($id);
if (!$record) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => []], 200);
}
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $record], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Create quotation
public function createQuotation()
{
try {
$data = $this->request->getPost();
$uploadFile = $this->request->getFile('additional_uploaded_file_name');
$uploadedFileName = null;
if ($uploadFile && $uploadFile->isValid()) {
$uploadPath = WRITEPATH . 'uploads/quotation/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0777, true);
}
$uploadedFileName = time() . '_' . $uploadFile->getRandomName();
$uploadFile->move($uploadPath, $uploadedFileName);
}
$insertData = [
'enquiry_id' => $data['enquiry_id'],
'insured_declared_value' => $data['insured_declared_value'],
'premium_amount' => $data['premium_amount'],
'insurance_plan_type_id' => $data['insurance_plan_type_id'],
'additional_uploaded_file_name' => $uploadedFileName,
'created_by' => $data['created_by'],
'manager_id' => $data['manager_id']
];
$this->QuotationModel->insert($insertData);
//update enquiry status
$enquiryData = $this->EnquiryModel->where('id',$data['enquiry_id'])->where('status','Awaiting Quotation')->first();
if (!empty($enquiryData)){ $this->EnquiryModel->update($data['enquiry_id'], ['status'=>'Quotation Created']); }
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Update quotation
public function updateQuotation()
{
try {
$data = $this->request->getPost();
if (!isset($data['id'])) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'ID Required'], 200);
}
$id = $data['id'];
$quotation = $this->QuotationModel->find($id);
if (!$quotation) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Data Not Found'], 200);
}
$updateData = [
'enquiry_id' => $data['enquiry_id'] ?? $quotation['enquiry_id'],
'insured_declared_value' => $data['insured_declared_value'] ?? $quotation['insured_declared_value'],
'premium_amount' => $data['premium_amount'] ?? $quotation['premium_amount'],
'insurance_plan_type_id' => $data['insurance_plan_type_id'] ?? $quotation['insurance_plan_type_id'],
'status' => $data['status'] ?? $quotation['status'],
'reject_reason' => $data['reject_reason'] ?? $quotation['reject_reason'],
'updated_by' => $data['updated_by'] ?? null
];
// File upload
$uploadFile = $this->request->getFile('additional_uploaded_file_name');
if ($uploadFile && $uploadFile->isValid()) {
$uploadPath = WRITEPATH . 'uploads/quotation/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0777, true);
}
$uploadedFileName = time() . '_' . $uploadFile->getRandomName();
$uploadFile->move($uploadPath, $uploadedFileName);
$updateData['additional_uploaded_file_name'] = $uploadedFileName;
}
$this->QuotationModel->update($id, $updateData);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Quotation status update
public function acceptOrRejectQuotation()
{
try {
$data = $this->request->getJSON(true);
if (!isset($data['id'])) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'ID Required'], 200);
}
$id = $data['id'];
$quotation = $this->QuotationModel->find($id);
if (!$quotation) {
return $this->respond(['status' => 'failed', 'code' => 200, 'data' => 'Data Not Found'], 200);
}
$newStatus = $data['status'];
$this->QuotationModel->update($id, ['status' => $newStatus]);
//update enquiry status
$enquiryData = $this->EnquiryModel->where('id',$quotation['enquiry_id'])->where('status','Quotation Created')->first();
$enqStatus = 'Quotation '.$data['status'];
if (!empty($enquiryData)){
$this->EnquiryModel->update($data['enquiry_id'], ['status'=> $enqStatus]);
if( $newStatus == 'Accepted'){
$this->QuotationModel
->where('enquiry_id', $quotation['enquiry_id'])
->where('id !=', $id)
->set(['status' => 'Rejected', 'updated_on' => date('Y-m-d H:i:s')])
->update();
}
}
return $this->respond(['status' => 'success', 'code' => 200, 'data' => 'Updated Successfully'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
// Download additional file
public function downloadAdditionalUploadedFile()
{
try {
$id = $this->request->getGet('id');
if (!$id) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 200);
}
// Fetch record from DB
$fileRecord = $this->QuotationModel->where('is_active',1)->find($id);
if (!$fileRecord) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 200);
}
$filePath = WRITEPATH . 'uploads/quotation/' . $fileRecord['incentive_file_name'];
if (!file_exists($filePath)) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200);
}
// Force file download
return $this->response->download($filePath, null);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
}