nhance_partner_be/app/Controllers/MasterController.php

368 lines
10 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Models\RoleMasterModel;
use App\Models\VehicleTypeMasterModel;
use App\Models\InsurancePlanTypeMasterModel;
use App\Models\ClaimTypeModel;
use App\Models\EndorsementTypeModel;
use App\Models\InsurersModel;
use App\Models\StaffModel;
use App\Models\PartnerBrokerModel;
use App\Models\PaymentModeModel;
class MasterController extends ResourceController
{
protected $format = 'json';
protected $myLogger;
protected $RoleMasterModel;
protected $VehicleTypeMasterModel;
protected $InsurancePlanTypeMasterModel;
protected $ClaimTypeModel;
protected $EndorsementTypeModel;
protected $InsurersModel;
protected $StaffModel;
protected $PartnerBrokerModel;
protected $PaymentModeModel;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->RoleMasterModel = new RoleMasterModel();
$this->VehicleTypeMasterModel = new VehicleTypeMasterModel();
$this->InsurancePlanTypeMasterModel = new InsurancePlanTypeMasterModel();
$this->ClaimTypeModel = new ClaimTypeModel();
$this->EndorsementTypeModel = new EndorsementTypeModel();
$this->InsurersModel = new InsurersModel();
$this->StaffModel = new StaffModel();
$this->PartnerBrokerModel = new PartnerBrokerModel();
$this->PaymentModeModel = new PaymentModeModel();
}
public function app_test()
{
return $this->respond(['status' => 200,'message' => 'success','data' => []]);
}
public function getRoleMaster()
{
$data = $this->RoleMasterModel->whereIn('id',[2,3])->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getVehicleTypeMaster()
{
$data = $this->VehicleTypeMasterModel->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getInsurancePlanTypeMaster()
{
$data = $this->InsurancePlanTypeMasterModel->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getClaimMaster()
{
$data = $this->ClaimTypeModel->select('id,claim_type')->where('is_active',1)->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getEndorsementMaster()
{
$data = $this->EndorsementTypeModel->select('id,endorsement_type')->where('is_active',1)->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getInsurersMaster()
{
$data = $this->InsurersModel->select('id,name,short_name')->where('is_active',1)->where('category','general')->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function getStaffMaster()
{
$data = $this->StaffModel->select('id,name')->where('is_active',1)->where('role_id',2)->findAll();
if (!$data) {
return $this->failNotFound('No data found');
}
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
//Broker CRUD---------------------------------------------------------------------------
public function getAllBrokers()
{
$data = $this->PartnerBrokerModel->where('is_active', 1)->findAll();
return $this->respond(['status' => 200,'message' => 'success','data' => $data]);
}
public function createBroker()
{
$input = $this->request->getJSON(true);
if (empty($input['name'])) {
return $this->response->setJSON(['status' => 404, 'message' => 'Value is required']);
}
// Duplicate check (case-insensitive)
$exists = $this->PartnerBrokerModel
->where('LOWER(name)', strtolower($input['name']))
->where('is_active', 1)
->first();
if ($exists) {
return $this->response->setJSON([
'status' => 404,
'message' => 'Broker name already exists'
]);
}
$data = [
'name' => $input['name'],
'is_active' => 1,
'created_by' => $input['created_by'] ?? null,
];
$this->PartnerBrokerModel->insert($data);
return $this->response->setJSON([
'status' => 200,
'message' => 'Broker name created successfully'
]);
}
public function updateBroker($id)
{
$input = $this->request->getJSON(true);
if (!$this->PartnerBrokerModel->find($id)) {
return $this->response->setJSON(['status' => 404, 'message' => 'Record not found']);
}
// Duplicate name check (excluding current record)
$checkDuplicate = $this->PartnerBrokerModel
->where('LOWER(name)', strtolower($input['name']))
->where('id !=', $id)
->where('is_active', 1)
->first();
if ($checkDuplicate) {
return $this->response->setJSON([
'status' => 404,
'message' => 'Broker name already exists'
]);
}
$data = [
'name' => $input['name'] ?? null,
'updated_by' => $input['updated_by'] ?? null,
];
$this->PartnerBrokerModel->update($id, $data);
return $this->response->setJSON([
'status' => 200,
'message' => 'Broker name updated successfully'
]);
}
public function updateBrokerStatus($id)
{
$input = $this->request->getJSON(true);
if (!isset($input['is_active'])) {
return $this->response->setJSON([
'status' => 404,
'message' => 'is_active field is required'
]);
}
$record = $this->PartnerBrokerModel->find($id);
if (!$record) {
return $this->response->setJSON(['status' => 404, 'message' => 'Record not found']);
}
$status = ($input['is_active'] == 1) ? 0 : 1;
$this->PartnerBrokerModel->update($id, [
'is_active' => $status,
'updated_by' => $input['updated_by'] ?? null,
]);
return $this->response->setJSON([
'status' => 200,
'message' => 'Status updated successfully'
]);
}
//---------------------------------------------------------------------------------------
//Payment Mode CRUD----------------------------------------------------------------------
public function getAllPaymentModelist()
{
$data = $this->PaymentModeModel->where('is_active', 1)->orderBy('id', 'DESC')->findAll();
return $this->response->setJSON(['status' => 200,'message' => 'success','data' => $data]);
}
public function createPaymentMode()
{
$input = $this->request->getJSON(true);
if (empty($input['value'])) {
return $this->response->setJSON(['status' => 404, 'message' => 'Value is required']);
}
// Duplicate check (case-insensitive)
$exists = $this->PaymentModeModel
->where('LOWER(value)', strtolower($input['value']))
->where('is_active', 1)
->first();
if ($exists) {
return $this->response->setJSON([
'status' => 404,
'message' => 'Payment Mode already exists'
]);
}
$data = [
'value' => $input['value'],
'is_active' => 1,
'created_by' => $input['created_by'] ?? null,
];
$this->PaymentModeModel->insert($data);
return $this->response->setJSON([
'status' => 200,
'message' => 'Payment mode created successfully'
]);
}
public function updatePaymentMode($id)
{
$input = $this->request->getJSON(true);
if (!$this->PaymentModeModel->find($id)) {
return $this->response->setJSON(['status' => 404, 'message' => 'Record not found']);
}
// Duplicate name check (excluding current record)
$checkDuplicate = $this->PaymentModeModel
->where('LOWER(value)', strtolower($input['value']))
->where('id !=', $id)
->where('is_active', 1)
->first();
if ($checkDuplicate) {
return $this->response->setJSON([
'status' => 404,
'message' => 'Payment Mode already exists'
]);
}
$data = [
'value' => $input['value'] ?? null,
'updated_by' => $input['updated_by'] ?? null,
];
$this->PaymentModeModel->update($id, $data);
return $this->response->setJSON([
'status' => 200,
'message' => 'Payment mode updated successfully'
]);
}
public function updatePaymentModeStatus($id)
{
$input = $this->request->getJSON(true);
if (!isset($input['is_active'])) {
return $this->response->setJSON([
'status' => 404,
'message' => 'is_active field is required'
]);
}
$record = $this->PaymentModeModel->find($id);
if (!$record) {
return $this->response->setJSON(['status' => 404, 'message' => 'Record not found']);
}
$status = ($input['is_active'] == 1) ? 0 : 1;
$this->PaymentModeModel->update($id, [
'is_active' => $status,
'updated_by' => $input['updated_by'] ?? null,
]);
return $this->response->setJSON([
'status' => 200,
'message' => 'Status updated successfully'
]);
}
//---------------------------------------------------------------------------------------
}