diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0417071..e1d2923 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -39,6 +39,7 @@ $routes->group('api', ['filter' => ["jwtAuth:1,2,3,4,agent","appSignature"] ], f $routes->get('master/getClaimMaster', 'MasterController::getClaimMaster'); $routes->get('master/getEndorsementMaster', 'MasterController::getEndorsementMaster'); $routes->get('master/getInsurersMaster', 'MasterController::getInsurersMaster'); + $routes->post('master/saveInsurer', 'MasterController::saveInsurer'); $routes->get('master/getStaffMaster', 'MasterController::getStaffMaster'); $routes->get('master/getAllBrokers', 'MasterController::getAllBrokers'); $routes->post('master/createBroker', 'MasterController::createBroker'); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index ef723ba..073f587 100644 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -114,6 +114,78 @@ class MasterController extends ResourceController return $this->respond(['status' => 200,'message' => 'success','data' => $data]); } + public function saveInsurer() + { + $input = $this->request->getPost(); + + if (empty($input['name'])) { + return $this->respond(['status' => 404, 'message' => 'Insurer name is required', 'data' => []]); + } + + if (empty($input['short_name'])) { + return $this->respond(['status' => 404, 'message' => 'Insurer short name is required', 'data' => []]); + } + + $id = !empty($input['id']) ? (int) $input['id'] : null; + + // Duplicate name check (case-insensitive) + $nameQuery = $this->InsurersModel + ->where('LOWER(name)', strtolower($input['name'])) + ->where('category', 'general'); + + if ($id) { + $nameQuery->where('id !=', $id); + } + + if ($nameQuery->first()) { + return $this->respond(['status' => 404, 'message' => 'Insurer name already exists', 'data' => []]); + } + + // Duplicate short name check (case-insensitive) + $shortNameQuery = $this->InsurersModel + ->where('LOWER(short_name)', strtolower($input['short_name'])) + ->where('category', 'general'); + + if ($id) { + $shortNameQuery->where('id !=', $id); + } + + if ($shortNameQuery->first()) { + return $this->respond(['status' => 404, 'message' => 'Insurer short name already exists', 'data' => []]); + } + + if ($id) { + if (!$this->InsurersModel->find($id)) { + return $this->respond(['status' => 404, 'message' => 'Record not found', 'data' => []]); + } + + $data = [ + 'name' => $input['name'], + 'short_name' => $input['short_name'], + 'updated_by' => $input['updated_by'] ?? null, + ]; + + $this->InsurersModel->update($id, $data); + + $record = $this->InsurersModel->select('id,name,short_name')->find($id); + + return $this->respond(['status' => 200, 'message' => 'Insurer updated successfully', 'data' => $record]); + } + + $data = [ + 'name' => $input['name'], + 'short_name' => $input['short_name'], + 'category' => 'general', + 'is_active' => 1, + 'created_by' => $input['created_by'] ?? null, + ]; + + $insertId = $this->InsurersModel->insert($data); + $record = $this->InsurersModel->select('id,name,short_name')->find($insertId); + + return $this->respond(['status' => 200, 'message' => 'Insurer created successfully', 'data' => $record]); + } + public function getStaffMaster() { $data = $this->StaffModel->select('id,name')->where('is_active',1)->where('role_id',2)->findAll();