diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0e0c59e7..dd373780 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -651,6 +651,7 @@ $routes->get("getPolicyLevelEmployeeSummaryData", "EmployeeRestController::getPo $routes->get("cdTransactionData", "EmployeeRestController::cdTransactionData"); $routes->match( ['get', 'post'], 'claimsSearch','EmployeeRestController::claimsSearch'); $routes->get("getBackToEnrolledDetails", "EmployeeRestController::getBackToEnrolledDetails"); +$routes->post("addEmpRetailPolicy", "EmployeeRestController::addEmpRetailPolicy"); // Ticketing System diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 6c655880..42706028 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -39,6 +39,7 @@ use App\Models\TicketMessageModel; use App\Models\HRAccessControlModel; use App\Models\InsurerModel; use App\Models\HrFileUploadModel; +use App\Models\EmployeeRetailPolicy; @@ -97,6 +98,7 @@ class EmployeeRestController extends AdminController protected $insurerModel; protected $hrFileUploadModel; protected $ticketMailTemplateModel; + protected $employeeRetailPolicy; public function __construct() @@ -124,6 +126,7 @@ class EmployeeRestController extends AdminController $this->claimStatusModel = new TicketClaimStatusModel(); $this->ticketMaster = new TicketMasterModel(); $this->ticketMessage = new TicketMessageModel(); + $this->employeeRetailPolicy = new EmployeeRetailPolicy(); $this->ticketController = new TicketController(); $this->hrAccessControlModel = new HRAccessControlModel(); @@ -2993,7 +2996,9 @@ class EmployeeRestController extends AdminController } - return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result, 'emp_name' => $employeeName, 'pre_policy_count' => $prePolicyCount], 200); + $emp_reatail_policy_data = $this->getEmpRetailPolicy($employeeSelfData); + + return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result, 'emp_name' => $employeeName, 'pre_policy_count' => $prePolicyCount, 'retail_policy_data' => $emp_reatail_policy_data], 200); } else { return $this->respond(['status' => 'failed', 'code' => 404, 'data' => []], 200); } @@ -4586,4 +4591,128 @@ class EmployeeRestController extends AdminController } catch (\Exception $e) { } } + + public function addEmpRetailPolicy() + { + $this->myLogger->logme("error", "EMPLOYEE-RETAIL-POLICY-CONTROLLER - addEmpRetailPolicy: Received payload = " . json_encode($this->request->getJSON() ?? [])); + + try { + + $payload = $this->request->getJSON(true); + $emp_id = $payload['emp_id'] ?? null; + + if (empty($emp_id)) { + $this->myLogger->logme("error", "addEmpRetailPolicy: emp_id is missing"); + return $this->respond(['status' => 'failed','code' => 400,'message' => 'emp_id is required' ], 200); + } + + $payload['created_by'] = $emp_id; + $emp_retail_policy_id = $this->employeeRetailPolicy->insert($payload); + + if ($emp_retail_policy_id) { + + $this->myLogger->logme("error", "addEmpRetailPolicy: Employee Retail Policy created successfully. Policy ID = " . $emp_retail_policy_id); + + return $this->respond([ + 'status' => 'success', + 'code' => 200, + 'message' => 'Employee Retail Policy created successfully', + 'data' => [ + 'emp_retail_policy_id' => $emp_retail_policy_id + ] + ], 200); + } else { + $this->myLogger->logme( "error", "addEmpRetailPolicy: Failed to create Employee Retail Policy. Insert returned false" ); + return $this->respond(['status' => 'failed','code' => 500,'message' => 'Failed to create Employee Retail Policy'], 500); + } + + } catch (\Throwable $th) { + + $this->myLogger->logme("error", "addEmpRetailPolicy: Exception occurred - " . $th->getMessage()); + return $this->respond(['status' => 'failed','code' => 500,'message' => 'Internal server error: ' . $th->getMessage()], 500); + } + } + + public function getEmpRetailPolicy($employeeData) + { + $this->myLogger->logme("error", "EMPLOYEE-REST-CONTROLLER - getEmpRetailPolicy: Given params: " . json_encode($employeeData ?? [])); + + try { + + $emp_id = $employeeData['id']; + $mobile_number = $employeeData['mobile'] ?? null; + + $emp_retail_policy_data = []; + if (!empty($emp_id)) { + + $emp_retail_policy_data = $this->employeeRetailPolicy + ->select(' + employee_retail_policies.emp_id, + employee_retail_policies.insurer_id, + employee_retail_policies.policy_type_id, + employee_retail_policies.policy_no, + employee_retail_policies.policy_start_date, + employee_retail_policies.policy_end_date, + policy_type.policy_type, + policy_type.long_name as policy_type_name, + insurers.name as insurer_name + ') + ->join('insurers', 'employee_retail_policies.insurer_id = insurers.id') + ->join('policy_type', 'employee_retail_policies.policy_type_id = policy_type.id') + ->where('is_active', 1) + ->where('emp_id', $emp_id) + ->findAll(); + } + + $emp_retail_client_data = []; + + if (!empty($mobile_number)) { + $emp_retail_client_data = $this->clientModel + ->select(" + $emp_id as emp_id + policy_transaction.insurer_id + policy_transaction.policy_type_id + policy_transaction.policy_no + policy_transaction.policy_start_date + policy_transaction.policy_end_date + policy_type.policy_type, + policy_type.long_name as policy_type_name, + insurers.name as insurer_name + ") + ->join('policy_transaction', 'policy_transaction.client_id = clients.id') + ->join('insurers', 'policy_transaction.insurer_id = insurers.id') + ->join('policy_type', 'policy_transaction.policy_type_id = policy_type.id') + ->where('policy_transaction.is_active', 1) + ->where('clients.is_active', 1) + ->where('clients.phone', $mobile_number) + ->findAll(); + } + + $complete_emp_retail_policy_data = array_values( + array_column( + array_merge($emp_retail_policy_data ?: [], $emp_retail_client_data ?: []), + null, + 'policy_no' + ) + ); + + return $complete_emp_retail_policy_data; + } catch (\Throwable $th) { + + $errorData = [ + 'message' => $th->getMessage(), + 'file' => $th->getFile(), + 'line' => $th->getLine(), + 'code' => $th->getCode(), + 'trace' => $th->getTraceAsString(), + 'trace_array' => $th->getTrace(), // full array version (optional) + 'function' => $th->getTrace()[0]['function'] ?? null, + 'class' => $th->getTrace()[0]['class'] ?? null, + ]; + + $this->myLogger->logme("error", "EMPLOYEE-REST-CONTROLLER - getEmpRetailPolicy: Exception: " . json_encode($errorData ?? [])); + + return []; + } + } } diff --git a/app/Models/EmployeeRetailPolicy.php b/app/Models/EmployeeRetailPolicy.php new file mode 100644 index 00000000..0b56e726 --- /dev/null +++ b/app/Models/EmployeeRetailPolicy.php @@ -0,0 +1,57 @@ +