diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 9043252..77dc786 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -59,6 +59,8 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) { $routes->post('staff/createStaff', 'StaffController::createStaff'); $routes->post('staff/updateStaff', 'StaffController::updateStaff'); $routes->post('staff/changeStaffStatus', 'StaffController::changeStaffStatus'); + $routes->get('staff/managerIncentiveFileList', 'StaffController::managerIncentiveFileList'); + $routes->get('staff/downloadManagerIncentiveFile', 'StaffController::downloadManagerIncentiveFile'); //Enquiry diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 8ee4c34..291cdfa 100644 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -61,24 +61,28 @@ class DashboardController extends ResourceController // --- Performance Agents (top 5, last 30 days) --- $performanceAgents = $this->db->table('partner_policy pp') - ->select('pa.id AS agent_id, pa.name AS agent_name, COUNT(pp.id) AS policies_issued, COALESCE(SUM(pp.premium_amount),0) AS premium_value') - ->join('partner_agent pa', 'pa.id = pp.agent_id', 'inner') - ->where('pp.created_on >=', $last30) - ->groupBy('pa.id') - ->orderBy('policies_issued', 'DESC') - ->limit(5) - ->get() - ->getResultArray(); + ->select('pa.id AS agent_id, pa.name AS agent_name, COUNT(pp.id) AS policies_issued, COALESCE(SUM(pp.premium_amount),0) AS premium_value') + ->join('partner_agent pa', 'pa.id = pp.agent_id', 'inner') + ->where('pp.created_on >=', $last30) + ->groupBy('pa.id') + ->orderBy('policies_issued', 'DESC') + ->limit(5) + ->get() + ->getResultArray(); + // --- Non-Performing Agents (bottom 5, last 30 days) --- $nonPerformingAgents = $this->db->table('partner_agent pa') - ->select('pa.id AS agent_id, pa.name AS agent_name, COUNT(pp.id) AS policies_issued, IF(pa.is_active=1,"Active","Inactive") AS status') - ->join('partner_policy pp', 'pp.agent_id = pa.id AND ', 'left') - ->groupBy('pa.id') - ->orderBy('policies_issued', 'ASC') - ->limit(5) - ->get() - ->getResultArray(); + ->select('pa.id AS agent_id, pa.name AS agent_name, COUNT(pp.id) AS policies_issued, IF(COUNT(pp.id)>0,"Active","Inactive") AS status') + ->join('partner_policy pp', 'pp.agent_id = pa.id', 'left') + ->where('pp.created_on >=', $last30) + ->groupBy('pa.id') + ->orderBy('policies_issued', 'ASC') + ->limit(5) + ->get() + ->getResultArray(); + + dd($nonPerformingAgents); // --- Quotations Pending --- $quotationsPending = $this->db->table('partner_quotation pq') @@ -89,6 +93,8 @@ class DashboardController extends ResourceController ->groupBy('ps.id') ->limit(10) ->get()->getResultArray(); + + dd($quotationsPending); // --- Policies Pending --- $policiesPending = $this->db->table('partner_enquiry pe') diff --git a/app/Controllers/EndorsementController.php b/app/Controllers/EndorsementController.php index 0754be9..30f94e3 100644 --- a/app/Controllers/EndorsementController.php +++ b/app/Controllers/EndorsementController.php @@ -25,6 +25,119 @@ class EndorsementController extends ResourceController $this->EnquiryModel = new EnquiryModel(); } + + public function EndorsementList() + { + try { + $id = $this->request->getGet('id'); // endorsement id + $manager_id = $this->request->getGet('manager_id'); + $agent_id = $this->request->getGet('agent_id'); + + $builder = $this->EndorsementModel + ->select('partner_endorsement_request.*, + i.name as insurer_name, + ib.branch_name as insurer_branch, + c.client_name, c.phone as client_phone, c.email as client_email, + pa.name as agent_name') + ->join('insurers i', 'i.id = partner_endorsement_request.insurer_id', 'left') + ->join('insurer_branch ib', 'ib.id = partner_endorsement_request.insurer_branch_id', 'left') + ->join('clients c', 'c.id = partner_endorsement_request.client_id', 'left') + ->join('partner_agent pa', 'pa.id = partner_endorsement_request.agent_id', 'left') + ->where('partner_endorsement_request.is_active', 1); + + // If ID is provided, fetch single record + if (!empty($id)) { + $builder->where('partner_endorsement_request.id', $id); + $record = $builder->get()->getRowArray(); + + if (!$record) { + return $this->respond(['status' => 'failed','code' => 404,'data' => 'Not Found'], 404); + } + + return $this->respond(['status' => 'success','code' => 200,'data' => $record ], 200); + } + + // Apply filters + if (!empty($manager_id)) { + $builder->where('partner_endorsement_request.manager_id', $manager_id); + } + + if (!empty($agent_id)) { + $builder->where('partner_endorsement_request.agent_id', $agent_id); + } + + $data = $builder->get()->getResultArray(); + + return $this->respond(['status' => 'success','code' => 200,'data' => $data], 200); + } catch (\Exception $e) { + return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500); + } + } + + + public function createEndorsement() +{ + try { + $reqData = $this->request->getJSON(true); + + // 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.*, E.insurer_id, E.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', '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' => 'Policy not found'], 200); + } + + // Prepare endorsement data + $endorsementData = [ + '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, + 'agent_id' => $policy['agent_id'] ?? null, + 'manager_id' => $policy['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', + 'is_active' => 1 + ]; + + // ✅ Insert endorsement + 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); + } +} + + diff --git a/app/Controllers/StaffController.php b/app/Controllers/StaffController.php index 17a3c04..6f7ae4f 100644 --- a/app/Controllers/StaffController.php +++ b/app/Controllers/StaffController.php @@ -4,14 +4,17 @@ namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; use App\Controllers\BaseController; use App\Models\StaffModel; +use App\Models\ManagerIncentiveFileModel; class StaffController extends ResourceController { protected $StaffModel; + protected $ManagerIncentiveFileModel; public function __construct() { $this->StaffModel = new StaffModel(); + $this->ManagerIncentiveFileModel = new ManagerIncentiveFileModel(); } // List of all Staffs @@ -152,6 +155,59 @@ class StaffController extends ResourceController } + // List of all manager incentive files + public function managerIncentiveFileList() + { + try{ + + $manager_id = $this->request->getGet('manager_id'); + + $data = $this->ManagerIncentiveFileModel->where('manager_id',$manager_id)->where('is_active',1)->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); + } + + } + + + + // Download manager incentive file (proxy to external app) + public function downloadManagerIncentiveFile() + { + try { + $file_name = $this->request->getGet('file_name'); + + + // External API URL + $url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($file_name); + + // Use CURL request to fetch the file + $client = \Config\Services::curlrequest(); + $response = $client->get($url); + + if ($response->getStatusCode() === 200) { + // Pass the file contents back as download + return $this->response + ->setHeader('Content-Type', $response->getHeaderLine('Content-Type')) + ->setHeader('Content-Disposition', 'attachment; filename="' . basename($file_name) . '"') + ->setBody($response->getBody()); + } else { + return $this->respond([ + 'status' => 'failed', + 'code' => $response->getStatusCode(), + 'message' => 'Unable to fetch file from source' + ], $response->getStatusCode()); + } + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500,'data' => $e->getMessage()], 500); + } + } + + + } diff --git a/app/Models/EndorsementModel.php b/app/Models/EndorsementModel.php index b5eaf28..4843815 100644 --- a/app/Models/EndorsementModel.php +++ b/app/Models/EndorsementModel.php @@ -6,7 +6,7 @@ use CodeIgniter\Model; class EndorsementModel extends Model { - protected $table = 'endorsement'; + protected $table = 'partner_endorsement_request'; protected $primaryKey = 'id'; protected $returnType = 'array'; protected $useSoftDeletes = false; @@ -15,10 +15,14 @@ class EndorsementModel extends Model 'client_id', 'client_policy_id', 'insurer_id', - 'tpa_id', - 'file_id', + 'insurer_branch_id', + 'manager_id', + 'agent_id', + 'policy_number', 'endorsement_no', 'endorsement_type', + 'endorsement_description', + 'status', 'created_at', 'updated_by', 'updated_at', diff --git a/app/Models/ManagerIncentiveFileModel.php b/app/Models/ManagerIncentiveFileModel.php new file mode 100644 index 0000000..4f3aec5 --- /dev/null +++ b/app/Models/ManagerIncentiveFileModel.php @@ -0,0 +1,39 @@ + 'required|integer', + 'incentive_month' => 'required|valid_date', + 'incentive_file_name'=> 'required|string|max_length[150]', + ]; + protected $validationMessages = []; + protected $skipValidation = false; +}