diff --git a/app/Controllers/ExcelExportController.php b/app/Controllers/ExcelExportController.php index 508542a..350ebeb 100644 --- a/app/Controllers/ExcelExportController.php +++ b/app/Controllers/ExcelExportController.php @@ -1518,6 +1518,14 @@ class ExcelExportController extends ResourceController try{ $managerId = $this->request->getGet('manager_id'); $search = $this->request->getGet('search'); + $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'); // --- ADDED VALIDATION (Simplified logic for clarity) --- @@ -1527,22 +1535,21 @@ class ExcelExportController extends ResourceController return $this->response->setJSON(['status' => 'error', 'code' => 400, 'data' => [], 'message' => 'Missing required parameters', 'ref' => $ref])->setStatusCode(200); } - $data = $this->getDataEndorsement($managerId,$search); + // $data = $this->getDataEndorsement($managerId,$search); + $data = $this->getDataEndorsement($managerId,$search,$agent_id,$policy_number,$from_date,$to_date,$endorsement_type,$insurer_id,$status,$verification); if (empty($data)) { throw new \RuntimeException('No Endorsement Details found.', 404); } - // Define column headers - - $header = [ 'S.No', 'Created Date', 'Internal/External', 'Created By', 'Policy Number', + 'Start Date', 'End Date', 'Vehicle No', 'Type Of Endorsement', 'Endorsement Number', @@ -1553,6 +1560,7 @@ class ExcelExportController extends ResourceController 'Contact Person', 'Remarks', 'Is Financial', + 'Status','Verification','Pending Days', 'Endorsement Premium Amount', 'Commission Amount', ]; @@ -2468,67 +2476,174 @@ class ExcelExportController extends ResourceController } - private function getDataEndorsement($managerId, $search = ''): array + private function getDataEndorsement($managerId, $search = '', + $agent_id = null, + $policy_number = null, + $from_date = null, + $to_date = null, + $endorsement_type = null, + $insurer_id = null, + $status = null, + $verification = null + ): array { try { + $builder = $this->db->table('partner_endorsement_request per'); + $builder->select(" + DATE_FORMAT(per.created_at, '%d-%m-%Y %h:%i %p') AS received_date, + per.policy_from, + ps.name AS staff_name, + per.policy_number, + CASE + WHEN per.policy_start_date = '0000-00-00' + OR YEAR(per.policy_start_date) < 2000 + THEN NULL + ELSE DATE_FORMAT(per.policy_start_date, '%Y-%m-%d') + END AS policy_start_date, + CASE + WHEN per.policy_end_date = '0000-00-00' + OR YEAR(per.policy_end_date) < 2000 + THEN NULL + ELSE DATE_FORMAT(per.policy_end_date, '%Y-%m-%d') + END AS policy_end_date, + per.reg_no, + petm.endorsement_type, + per.endorsement_no, + per.insured_name, + CONCAT(I.name, ' - ', I.short_name) AS insurer_name, + pb.name AS broker_name, + CONCAT(pa.agent_code, ' - ', pa.name) AS agent_details, + per.contact_person, + per.endorsement_description, + per.financia_or_non_financial, + per.status, + CASE + WHEN per.is_data_accuracy_checked = 0 THEN 'To Verify' + ELSE 'Verified' + END AS verification_status, + CASE + WHEN per.created_at IS NULL + THEN 0 + ELSE DATEDIFF(CURDATE(), DATE(per.created_at)) + END AS pending_days, + per.endorsement_premium, + per.commission_amount, + "); - $sql = "SELECT - DATE_FORMAT(per.created_at, '%d-%m-%Y %h:%i %p') AS received_date, - per.policy_from, - ps.name AS staff_name, - per.policy_number AS policy_number, - per.reg_no, - petm.endorsement_type AS endorsement_type, - per.endorsement_no, - per.insured_name, - I.name AS insurer_name, - pb.name AS broker_name, - CONCAT(pa.agent_code, ' - ', pa.name) AS agent_details, - per.contact_person, - per.endorsement_description, - per.financia_or_non_financial, - per.endorsement_premium, - per.commission_amount - - + // ✅ Correct joins (NO duplicates) + $builder->join('partner_policy pp', 'pp.policy_number = per.policy_number', 'left'); + $builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left'); + $builder->join('partner_agent pa', 'pa.id = per.agent_id', 'left'); + $builder->join('partner_staff ps', 'ps.id = per.created_by', 'left'); + $builder->join('insurers I', 'I.id = per.insurer_id', 'left'); + $builder->join('partner_endorsement_type_master petm', 'petm.id = per.endorsement_type', 'left'); + $builder->join('partner_brokers pb', 'pb.id = per.broker_id', 'left'); + $builder->join('clients c', 'c.id = per.client_id', 'left'); + $builder->join('partner_payment_mode_master pm', 'pm.id = per.payment_mode_id', 'left'); + $builder->where('per.manager_id', $managerId); + $builder->where('per.is_active', 1); - FROM partner_endorsement_request per - LEFT JOIN partner_policy pp ON pp.agent_id = per.agent_id - LEFT JOIN partner_enquiry pe ON pe.agent_id = per.agent_id - LEFT JOIN partner_agent pa ON pa.id = per.agent_id - LEFT JOIN partner_staff ps ON ps.id = per.created_by - LEFT JOIN insurers I ON I.id = pe.insurer_id - LEFT JOIN partner_endorsement_type_master petm ON petm.id = per.endorsement_type - LEFT JOIN partner_brokers pb ON pb.id = per.broker_id - WHERE per.manager_id = ? - AND per.is_active = 1"; + // Filters + if (!empty($agent_id)) { + $builder->where('per.agent_id', $agent_id); + } - $bindings = [$managerId]; + if (!empty($policy_number)) { + $builder->where('per.policy_number', $policy_number); + } - if (!empty($search)) { + if (!empty($from_date) && !empty($to_date)) { + $builder->where('per.created_at >=', $from_date . ' 00:00:00'); + $builder->where('per.created_at <=', $to_date . ' 23:59:59'); + } - $searchTerm = '%' . strtolower(trim($search)) . '%'; + if (!empty($endorsement_type) && $endorsement_type != 'All') { + $builder->where('per.endorsement_type', $endorsement_type); + } - $sql .= " AND ( - LOWER(per.policy_from) LIKE ? OR - LOWER(per.status) LIKE ? OR - LOWER(pe.reg_no) LIKE ? OR - LOWER(I.name) LIKE ? OR - LOWER(pp.policy_number) LIKE ? OR - LOWER(petm.endorsement_type) LIKE ? OR - LOWER(per.endorsement_no) LIKE ? - )"; + if (!empty($insurer_id)) { + $builder->where('per.insurer_id', $insurer_id); + } - for ($i = 0; $i < 7; $i++) { - $bindings[] = $searchTerm; + if (!empty($status) && $status != 'All') { + $builder->where('per.status', $status); + } + + if (!empty($verification) && $verification != 'All') { + if ($verification == 'Verified') { + $builder->where('per.is_data_accuracy_checked', 1); + } elseif ($verification == 'To Verify') { + $builder->where('per.is_data_accuracy_checked', 0); } } - // GROUP BY must be LAST - $sql .= " GROUP BY per.id"; + // Search filter + // if (!empty($search)) { + // $search = strtolower(trim($search)); - $query = $this->db->query($sql, $bindings); - $results = $query->getResultArray(); + // $builder->groupStart() + // ->like('LOWER(per.policy_from)', $search) + // ->orLike('LOWER(per.status)', $search) + // ->orLike('LOWER(per.reg_no)', $search) + // ->orLike('LOWER(per.policy_start_date)', $search) + // ->orLike('LOWER(per.policy_end_date)', $search) + // ->orLike('LOWER(I.name)', $search) + // ->orLike('LOWER(I.insurer_short_name)', $search) + // ->orLike('LOWER(per.policy_number)', $search) + // ->orLike('LOWER(petm.endorsement_type)', $search) + // ->orLike('LOWER(per.endorsement_no)', $search) + // ->groupEnd(); + // } + if (!empty($search)) { + $search = strtolower(trim($search)); + + $builder->groupStart() + + // Normal columns + ->like('LOWER(per.policy_from)', $search) + ->orLike('LOWER(per.status)', $search) + ->orLike('LOWER(per.reg_no)', $search) + ->orLike('LOWER(per.policy_number)', $search) + ->orLike('LOWER(per.endorsement_no)', $search) + + // Insurer + ->orLike('LOWER(I.name)', $search) + ->orLike('LOWER(I.short_name)', $search) + + // Endorsement type + ->orLike('LOWER(petm.endorsement_type)', $search) + + // ✅ Broker name + ->orLike('LOWER(pb.name)', $search) + + // ✅ Agent code + name (CONCAT) + ->orLike("LOWER(CONCAT(pa.agent_code, ' - ', pa.name))", $search) + ->orLike("LOWER(pa.agent_code)", $search) + ->orLike("LOWER(pa.name)", $search) + + // ✅ Verification (CASE condition) + ->orLike(" + LOWER( + CASE + WHEN per.is_data_accuracy_checked = 0 THEN 'to verify' + ELSE 'verified' + END + )", $search) + + // ✅ Received date (formatted) + ->orLike("DATE_FORMAT(per.created_at, '%d-%m-%Y %h:%i %p')", $search) + + // ✅ Policy dates (formatted) + ->orLike("DATE_FORMAT(per.policy_start_date, '%Y-%m-%d')", $search) + ->orLike("DATE_FORMAT(per.policy_end_date, '%Y-%m-%d')", $search) + + ->groupEnd(); + } + + + $builder->groupBy('per.id'); + + $results = $builder->get()->getResultArray(); // Convert associative rows to indexed rows (Excel-friendly) $data = [];