diff --git a/app/Controllers/EndorsementController.php b/app/Controllers/EndorsementController.php index 73f0993..a4ff690 100644 --- a/app/Controllers/EndorsementController.php +++ b/app/Controllers/EndorsementController.php @@ -34,6 +34,14 @@ class EndorsementController extends ResourceController $agent_id = $this->request->getGet('agent_id'); $policy_number = $this->request->getGet('policy_number'); + $from_date = $this->request->getGet('from_date'); + $to_date = $this->request->getGet('to_date'); + $endorsement_type = $this->request->getGet('endorsement_type'); + $insurer_id = $this->request->getGet('insurer_id'); + $status = $this->request->getGet('status'); + $verification = $this->request->getGet('verification'); + $search = $this->request->getGet('search'); + $builder = $this->EndorsementModel ->select('partner_endorsement_request.*, i.name as insurer_name, @@ -78,6 +86,34 @@ class EndorsementController extends ResourceController $builder->where('partner_endorsement_request.policy_number', $policy_number); } + if (!empty($from_date) && !empty($to_date)) { + $builder->where('partner_endorsement_request.created_at >=', $from_date . ' 00:00:00') + ->where('partner_endorsement_request.created_at <=', $to_date . ' 23:59:59'); + } + + if (!empty($endorsement_type) && $endorsement_type != 'All') { + $builder->where('partner_endorsement_request.endorsement_type', $endorsement_type); + } + + if (!empty($insurer_id)) { + $builder->where('partner_endorsement_request.insurer_id', $insurer_id); + } + + if (!empty($status) && $status != 'All') { + $builder->where('partner_endorsement_request.status', $status); + } + + if (!empty($verification) && $verification != 'All') { + + if ($verification == 'Verified') { + $builder->where('partner_endorsement_request.is_data_accuracy_checked', 1); + } elseif ($verification == 'To Verify') { + $builder->where('partner_endorsement_request.is_data_accuracy_checked', 0); + } + } + + + $data = $builder->get()->getResultArray(); foreach ($data as $key => $row) { @@ -95,65 +131,117 @@ class EndorsementController extends ResourceController public function createEndorsement() { try { + $reqData = $this->request->getPost(); - // Validate required fields - if (empty($reqData['policy_number']) || empty($reqData['endorsement_type']) || empty($reqData['endorsement_description'])) { - return $this->respond(['status' => 'failed','code' => 400,'data' => 'policy_number, endorsement_type and endorsement_description are required' ], 200); + // Basic validation + if (empty($reqData['policy_from']) || empty($reqData['policy_number']) || empty($reqData['endorsement_type']) || empty($reqData['endorsement_description'])) + { + return $this->respond(['status' => 'failed','code' => 400,'data' => 'policy_from, policy_number, endorsement_type and endorsement_description are required' ], 400); } - // Fetch policy details - $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(); + $policy = null; - if (empty($policy) && $reqData['policy_from'] == 'Internal') { - return $this->respond(['status' => 'failed','code' => 404,'data' => 'Policy not found'], 200); + // INTERNAL POLICY FLOW + if ($reqData['policy_from'] === 'Internal') { + + $policy = $this->PolicyModel + ->select('partner_policy.*, + Q.insurer_id, Q.insurer_branch_id, + E.client_id, + E.name as insured_name, + E.mobile as client_mobile, + E.email as client_email, + E.reg_no, + E.broker_id') + ->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' => 'Internal policy not found'], 404); + } } - - $uploadFile = $this->request->getFile('endorsement_file_name'); + //FILE UPLOAD $uploadedFileName = null; + $uploadFile = $this->request->getFile('endorsement_file_name'); if ($uploadFile && $uploadFile->isValid()) { + $uploadPath = WRITEPATH . 'uploads/endorsement/'; if (!is_dir($uploadPath)) { mkdir($uploadPath, 0777, true); } + $uploadedFileName = time() . '_' . $uploadFile->getRandomName(); $uploadFile->move($uploadPath, $uploadedFileName); } - // Prepare endorsement data - $endorsementData = [ - 'policy_from' => $reqData['policy_from'], - 'policy_number' => $policy['policy_number'] ?? $reqData['policy_number'], - 'client_policy_id' => $policy['client_policy_id'] ?? null, - 'insurer_id' => $policy['insurer_id'] ?? null, - 'insurer_branch_id' => $policy['insurer_branch_id'] ?? null, - 'client_id' => $policy['client_id'] ?? null, - 'agent_id' => $policy['agent_id'] ?? null, - 'manager_id' => $reqData['manager_id'] ?? null, - 'endorsement_type' => $reqData['endorsement_type'], - 'endorsement_description'=> $reqData['endorsement_description'], - 'endorsement_no' => $reqData['endorsement_no'] ?? null, - 'created_by' => $reqData['created_by'] ?? null, - 'status' => 'Open', - 'endorsement_file_name' => $uploadedFileName, - 'is_active' => 1 - ]; + // PREPARE DATA + if ($reqData['policy_from'] === 'Internal') { + // Take values from DB + $endorsementData = [ + 'policy_from' => 'Internal', + 'policy_number' => $policy['policy_number'], + 'client_policy_id' => $policy['client_policy_id'] ?? null, + 'insurer_id' => $policy['insurer_id'] ?? null, + 'insurer_branch_id' => $policy['insurer_branch_id'] ?? null, + 'client_id' => $policy['client_id'] ?? null, + 'policy_start_date' => $policy['start_date'] ?? null, + 'policy_end_date' => $policy['end_date'] ?? null, + 'reg_no' => $policy['reg_no'] ?? null, + 'broker_id' => $policy['broker_id'] ?? null, + 'insured_name' => $policy['insured_name'] ?? null, + ]; - if (!$this->EndorsementModel->insert($endorsementData)) { - return $this->respond([ 'status' => 'failed', 'code' => 422, 'data' => $this->EndorsementModel->errors()], 422); + } else { + + // use values from Request + $endorsementData = [ + 'policy_from' => 'External', + 'policy_number' => $reqData['policy_number'], + 'insurer_id' => $reqData['insurer_id'] ?? null, + 'insurer_branch_id' => $reqData['insurer_branch_id'] ?? null, + 'client_id' => $reqData['client_id'] ?? null, + 'policy_start_date' => $reqData['policy_start_date'] ?? null, + 'policy_end_date' => $reqData['policy_end_date'] ?? null, + 'reg_no' => $reqData['reg_no'] ?? null, + 'broker_id' => $reqData['broker_id'] ?? null, + 'insured_name' => $reqData['insured_name'] ?? null, + ]; } - return $this->respond([ 'status' => 'success', 'code' => 200, 'data' => ['endorsement_id' => $this->EndorsementModel->getInsertID()] ], 200); + // Common fields + $endorsementData += [ + 'manager_id' => $reqData['manager_id'] ?? null, + 'agent_id' => $reqData['agent_id'] ?? null, + 'endorsement_type' => $reqData['endorsement_type'], + 'endorsement_description' => $reqData['endorsement_description'], + 'endorsement_no' => $reqData['endorsement_no'] ?? null, + 'contact_person' => $reqData['contact_person'] ?? null, + 'pending_days' => $reqData['pending_days'] ?? null, + 'financia_or_non_financial' => $reqData['financia_or_non_financial'] ?? null, + 'endorsement_premium' => $reqData['endorsement_premium'] ?? null, + 'commission_amount' => $reqData['commission_amount'] ?? null, + 'created_by' => $reqData['created_by'] ?? null, + 'status' => 'Open', + 'endorsement_file_name' => $uploadedFileName, + 'is_active' => 1 + ]; + + if (!$this->EndorsementModel->insert($endorsementData)) + { + return $this->respond(['status' => 'failed','code' => 422, 'data' => $this->EndorsementModel->errors() ], 422); + } + + return $this->respond(['status' => 'success','code' => 200,'data' => ['endorsement_id' => $this->EndorsementModel->getInsertID()]], 200); } catch (\Exception $e) { - return $this->respond([ 'status' => 'failed', 'code' => 500, 'data' => $e->getMessage() ], 500); + + return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500); } } @@ -163,22 +251,19 @@ class EndorsementController extends ResourceController $endorsementId = $this->request->getPost('id'); - if (!$endorsementId) { - return $this->respond([ 'status' => 'failed', 'code' => 400, 'data' => 'endorsementId is required' ], 200); - } + if (!$endorsementId) { return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'id is required'], 400);} $reqData = $this->request->getPost(); // Fetch existing record $endorsement = $this->EndorsementModel->find((int)$endorsementId); - if (empty($endorsement)) { - return $this->respond([ 'status' => 'failed', 'code' => 404, 'data' => 'Endorsement not found' ], 200); - } + if (!$endorsement) { return $this->respond([ 'status' => 'failed', 'code' => 404,'data' => 'Endorsement not found' ], 404); } - // Handle upload (endorsement completion file) - $uploadFile = $this->request->getFile('endorsement_completion_file'); - $uploadedCompletionFile = $endorsement['endorsement_completion_file']; // keep old if not replaced + // FILE UPLOAD + $uploadedCompletionFile = $endorsement['endorsement_file_name']; + + $uploadFile = $this->request->getFile('endorsement_file_name'); if ($uploadFile && $uploadFile->isValid()) { @@ -191,19 +276,85 @@ class EndorsementController extends ResourceController $uploadFile->move($uploadPath, $uploadedCompletionFile); } - // Prepare data to update - $updateData = [ - 'endorsement_type' => $reqData['endorsement_type'] ?? $endorsement['endorsement_type'], - 'endorsement_description' => $reqData['endorsement_description'] ?? $endorsement['endorsement_description'], - 'endorsement_no' => $reqData['endorsement_no'] ?? $endorsement['endorsement_no'], - 'status' => $reqData['status'] , - 'endorsement_completion_file' => $uploadedCompletionFile, - 'contact_person' => $reqData['contact_person'] , - 'financia_or_non_financial' => $reqData['financia_or_non_financial'] , - 'endorsement_premium' => $reqData['endorsement_premium'] , - 'pending_days' => $reqData['pending_days'] , - 'updated_by' => $reqData['updated_by'] ?? null, - ]; + //COMMON UPDATE FIELDS + + $updateData = []; + + // Update only if provided (avoid null overwrite) + if (isset($reqData['endorsement_type'])) { + $updateData['endorsement_type'] = $reqData['endorsement_type']; + } + + if (isset($reqData['endorsement_description'])) { + $updateData['endorsement_description'] = $reqData['endorsement_description']; + } + + if (isset($reqData['endorsement_no'])) { + $updateData['endorsement_no'] = $reqData['endorsement_no']; + } + + if (isset($reqData['status'])) { + $updateData['status'] = $reqData['status']; + } + + if (isset($reqData['contact_person'])) { + $updateData['contact_person'] = $reqData['contact_person']; + } + + if (isset($reqData['financia_or_non_financial'])) { + $updateData['financia_or_non_financial'] = $reqData['financia_or_non_financial']; + } + + if (isset($reqData['endorsement_premium'])) { + $updateData['endorsement_premium'] = $reqData['endorsement_premium']; + } + + if (isset($reqData['pending_days'])) { + $updateData['pending_days'] = $reqData['pending_days']; + } + + // Always update file (either old or new) + $updateData['endorsement_file_name'] = $uploadedCompletionFile; + + // External Policy Editable Fields + if ($endorsement['policy_from'] === 'External') { + + if (isset($reqData['insurer_id'])) { + $updateData['insurer_id'] = $reqData['insurer_id'] ?? null; + } + + if (isset($reqData['insurer_branch_id'])) { + $updateData['insurer_branch_id'] = $reqData['insurer_branch_id'] ?? null; + } + + if (isset($reqData['policy_start_date'])) { + $updateData['policy_start_date'] = $reqData['policy_start_date'] ?? null; + } + + if (isset($reqData['policy_end_date'])) { + $updateData['policy_end_date'] = $reqData['policy_end_date'] ?? null; + } + + if (isset($reqData['insured_name'])) { + $updateData['insured_name'] = $reqData['insured_name'] ?? null; + } + + if (isset($reqData['reg_no'])) { + $updateData['reg_no'] = $reqData['reg_no'] ?? null; + } + + if (isset($reqData['broker_id'])) { + $updateData['broker_id'] = $reqData['broker_id'] ?? null; + } + } + + $updateData['updated_by'] = $reqData['updated_by'] ?? null; + + /** + * =========================== + * UPDATE + * =========================== + */ if (!$this->EndorsementModel->update($endorsementId, $updateData)) { return $this->respond([ @@ -216,10 +367,14 @@ class EndorsementController extends ResourceController return $this->respond([ 'status' => 'success', 'code' => 200, - 'data' => ['endorsement_id' => $endorsementId, 'message' => 'Endorsement updated successfully'] + 'data' => [ + 'endorsement_id' => $endorsementId, + 'message' => 'Endorsement updated successfully' + ] ], 200); } catch (\Exception $e) { + return $this->respond([ 'status' => 'failed', 'code' => 500, @@ -229,6 +384,149 @@ class EndorsementController extends ResourceController } + // public function createEndorsement() + // { + // try { + // $reqData = $this->request->getPost(); + + // // Validate required fields + // if (empty($reqData['policy_number']) || empty($reqData['endorsement_type']) || empty($reqData['endorsement_description'])) { + // return $this->respond(['status' => 'failed','code' => 400,'data' => 'policy_number, endorsement_type and endorsement_description are required' ], 200); + // } + + // // Fetch policy details + // $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, E.broker_id') + // ->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) && $reqData['policy_from'] == 'Internal') { + // return $this->respond(['status' => 'failed','code' => 404,'data' => 'Policy not found'], 200); + // } + + + // $uploadFile = $this->request->getFile('endorsement_file_name'); + // $uploadedFileName = null; + + // if ($uploadFile && $uploadFile->isValid()) { + // $uploadPath = WRITEPATH . 'uploads/endorsement/'; + // if (!is_dir($uploadPath)) { + // mkdir($uploadPath, 0777, true); + // } + // $uploadedFileName = time() . '_' . $uploadFile->getRandomName(); + // $uploadFile->move($uploadPath, $uploadedFileName); + // } + + // // Prepare endorsement data + // $endorsementData = [ + // 'policy_from' => $reqData['policy_from'], + // 'policy_number' => $policy['policy_number'] ?? $reqData['policy_number'], + // 'client_policy_id' => $policy['client_policy_id'] ?? null, + // 'insurer_id' => $policy['insurer_id'] ?? null, + // 'insurer_branch_id' => $policy['insurer_branch_id'] ?? null, + // 'client_id' => $policy['client_id'] ?? null, + // 'agent_id' => $policy['agent_id'] ?? null, + // 'policy_start_date' => $policy['start_date'] ?? null, + // 'policy_end_date' => $policy['end_date'] ?? null, + // 'reg_no' => $policy['reg_no'] ?? null, + // 'broker_id' => $policy['broker_id'] ?? null, + // 'insured_name' => $policy['insured_name'] ?? null, + // 'commission_amount' => $reqData['agent_id'] ?? null, + // 'manager_id' => $reqData['manager_id'] ?? null, + // 'endorsement_type' => $reqData['endorsement_type'], + // 'endorsement_description'=> $reqData['endorsement_description'], + // 'endorsement_no' => $reqData['endorsement_no'] ?? null, + // 'created_by' => $reqData['created_by'] ?? null, + // 'status' => 'Open', + // 'endorsement_file_name' => $uploadedFileName, + // 'is_active' => 1 + // ]; + + + // if (!$this->EndorsementModel->insert($endorsementData)) { + // return $this->respond([ 'status' => 'failed', 'code' => 422, 'data' => $this->EndorsementModel->errors()], 422); + // } + + // return $this->respond([ 'status' => 'success', 'code' => 200, 'data' => ['endorsement_id' => $this->EndorsementModel->getInsertID()] ], 200); + + // } catch (\Exception $e) { + // return $this->respond([ 'status' => 'failed', 'code' => 500, 'data' => $e->getMessage() ], 500); + // } + // } + + // public function updateEndorsement() + // { + // try { + + // $endorsementId = $this->request->getPost('id'); + + // if (!$endorsementId) { + // return $this->respond([ 'status' => 'failed', 'code' => 400, 'data' => 'endorsementId is required' ], 200); + // } + + // $reqData = $this->request->getPost(); + + // // Fetch existing record + // $endorsement = $this->EndorsementModel->find((int)$endorsementId); + + // if (empty($endorsement)) { + // return $this->respond([ 'status' => 'failed', 'code' => 404, 'data' => 'Endorsement not found' ], 200); + // } + + // // Handle upload (endorsement completion file) + // $uploadFile = $this->request->getFile('endorsement_completion_file'); + // $uploadedCompletionFile = $endorsement['endorsement_completion_file']; // keep old if not replaced + + // if ($uploadFile && $uploadFile->isValid()) { + + // $uploadPath = WRITEPATH . 'uploads/endorsement/'; + // if (!is_dir($uploadPath)) { + // mkdir($uploadPath, 0777, true); + // } + + // $uploadedCompletionFile = time() . '_' . $uploadFile->getRandomName(); + // $uploadFile->move($uploadPath, $uploadedCompletionFile); + // } + + // // Prepare data to update + // $updateData = [ + // 'endorsement_type' => $reqData['endorsement_type'] ?? $endorsement['endorsement_type'], + // 'endorsement_description' => $reqData['endorsement_description'] ?? $endorsement['endorsement_description'], + // 'endorsement_no' => $reqData['endorsement_no'] ?? $endorsement['endorsement_no'], + // 'status' => $reqData['status'] , + // 'endorsement_completion_file' => $uploadedCompletionFile, + // 'contact_person' => $reqData['contact_person'] , + // 'financia_or_non_financial' => $reqData['financia_or_non_financial'] , + // 'endorsement_premium' => $reqData['endorsement_premium'] , + // 'pending_days' => $reqData['pending_days'] , + // 'updated_by' => $reqData['updated_by'] ?? null, + // ]; + + // if (!$this->EndorsementModel->update($endorsementId, $updateData)) { + // return $this->respond([ + // 'status' => 'failed', + // 'code' => 422, + // 'data' => $this->EndorsementModel->errors() + // ], 422); + // } + + // return $this->respond([ + // 'status' => 'success', + // 'code' => 200, + // 'data' => ['endorsement_id' => $endorsementId, 'message' => 'Endorsement updated successfully'] + // ], 200); + + // } catch (\Exception $e) { + // return $this->respond([ + // 'status' => 'failed', + // 'code' => 500, + // 'data' => $e->getMessage() + // ], 500); + // } + // } + + public function downloadEndorsementCompletionFile() { try { @@ -245,7 +543,7 @@ class EndorsementController extends ResourceController return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 200); } - $filePath = WRITEPATH . 'uploads/endorsement/' . $fileRecord['endorsement_completion_file']; + $filePath = WRITEPATH . 'uploads/endorsement/' . $fileRecord['endorsement_file_name']; if (!file_exists($filePath)) { return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200); diff --git a/app/Models/EndorsementModel.php b/app/Models/EndorsementModel.php index e9d8e85..5bc149b 100644 --- a/app/Models/EndorsementModel.php +++ b/app/Models/EndorsementModel.php @@ -30,6 +30,15 @@ class EndorsementModel extends Model 'financia_or_non_financial', 'endorsement_premium', 'pending_days', + 'policy_start_date', + 'policy_end_date', + 'reg_no', + 'broker_id', + 'insured_name', + 'is_data_accuracy_checked', + 'commission_amount', + 'policy_transaction_id', + 'pt_oc_share_details_id', 'created_at', 'updated_by', 'updated_at',