diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php index 9ad372f8..8bc4d0e2 100755 --- a/app/Controllers/EmployeeController.php +++ b/app/Controllers/EmployeeController.php @@ -316,11 +316,11 @@ class EmployeeController extends AdminController $policy_id = isset($post_data['policy_id']) ? $post_data['policy_id'] : $this->request->getPost('policy_id'); $branch_id = isset($post_data['client_branch_id']) ? $post_data['client_branch_id'] : $this->request->getPost('branch_id'); $action = isset($post_data['file_action']) ? $post_data['file_action'] : $this->request->getPost('upload-action-type'); - - if(empty($post_data)){ + + if (empty($post_data)) { $hr_file_id = $this->request->getPost('hr_file_id') ?? null; - }else{ - $hr_file_id = $post_data['hr_file_id'] ?? null ; + } else { + $hr_file_id = $post_data['hr_file_id'] ?? null; } $hr_id = $post_data['created_by'] ?? null; @@ -342,9 +342,9 @@ class EmployeeController extends AdminController $this->myLogger->logme("error", '{file_id} is less than 1MB, validating on the fly', ['file_id' => $file_id]); //endof validation process if (isset($result['error_summary']) && count($result['error_summary'])) { - if(!empty($post_data)){ + if (!empty($post_data)) { return ['status' => false, 'message' => 'File rejected with errors', 'file_id' => $file_id]; - }else{ + } else { return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'File rejected with errors'], 200); } } @@ -363,6 +363,9 @@ class EmployeeController extends AdminController } } + $get_data = $this->request->getGet(); + // dd($get_data); + $data['tab_name'] = 'View Inception'; $data['page_name'] = 'View Inception'; @@ -371,7 +374,7 @@ class EmployeeController extends AdminController //for inception upload $data['actions'] = ['all' => 'All', 'inception' => 'Inception (Employee + Dependents)', 'missed_inception' => 'Missed Inception (Employee + Dependents)', 'addition' => 'Addition (Employee + Dependents)', 'dependent_addition' => 'Dependent Addition (Only Dependents)', 'deletion' => 'Deletion', 'correction' => 'Correction', 'si_enhancement' => 'SI Enhancement']; // $data['actions'] = ['inception' => 'Inception (Employee + Dependents)', 'missed_inception' => 'Missed Inception (Employee + Dependents)', 'addition' => 'Addition (Employee + Dependents)', 'dependent_addition' => 'Dependent Addition (Only Dependents)', 'deletion' => 'Deletion', 'correction' => 'Correction', 'si_enhancement' => 'SI Enhancement','enrollment' => 'Enrolment']; - + // $data['import_or_export'] = ['import' => 'Import', 'export' => 'Export']; $data['import_or_export'] = ['import' => 'Upload', 'export' => 'Download']; $data['insurer_or_tpa'] = ['insurer' => 'Insurer', 'tpa' => 'TPA']; @@ -388,49 +391,90 @@ class EmployeeController extends AdminController $user_id = get_session_userid(); $query = $this->fileModel - ->select([ - 'files.id','files.file_name','files.created_by','files.created_at','files.is_active','files.status','files.client_id','files.policy_id','files.client_branch_id','files.action','files.uploaded_by', - 'up.emp_code', - // 'up.first_name', - 'c.short_name', - 'cb.branch_name', - 'cp.id as client_policy_id', - '"0" as employee_count', - '"0" as total', - 'policy_type.policy_type' , - 'cp.policy_no' , - "CASE - WHEN files.hr_id IS NOT NULL THEN - CONCAT( - ( - SELECT lc.name - FROM level_contacts lc - WHERE lc.id = files.hr_id - LIMIT 1 - ), - ' (HR)' - ) - ELSE up.first_name - END AS first_name - " - ]) - ->join('user_profiles up', 'files.created_by = up.id', 'left') - ->join('client_policy cp', 'files.policy_id = cp.id', 'left') - ->join('client_branch cb', 'files.client_branch_id = cb.id', 'left') - ->join('policy_type', 'policy_type.id = cp.policy_type_id') - ->join('clients c', 'files.client_id = c.id and files.client_id = cp.client_id', 'left') - ->join("client_rm cr","cr.client_id = c.id and cr.is_active = 1",'left') - ->where('files.is_active', 1); - if (in_array($role_id, [2, 3])) { - - // If the user is a Account Manager or Manager, filter by user_id - $query->where('cr.user_id', (int)$user_id); - } - // ->where('files.created_by', get_session_userid()) - $data['fileList'] = $query->groupBy("files.id")->orderBy('files.created_at', 'desc') - // $data['fileList'] = $query - ->limit(2000) - ->find(); + ->select([ + 'files.id', + 'files.file_name', + 'files.created_by', + 'files.created_at', + 'files.is_active', + 'files.status', + 'files.client_id', + 'files.policy_id', + 'files.client_branch_id', + 'files.action', + 'files.uploaded_by', + 'up.emp_code', + // 'up.first_name', + 'c.short_name', + 'cb.branch_name', + 'cp.id as client_policy_id', + '"0" as employee_count', + '"0" as total', + 'policy_type.policy_type', + 'cp.policy_no', + "CASE + WHEN files.hr_id IS NOT NULL THEN + CONCAT( + ( + SELECT lc.name + FROM level_contacts lc + WHERE lc.id = files.hr_id + LIMIT 1 + ), + ' (HR)' + ) + ELSE up.first_name + END AS first_name + " + ]) + ->join('user_profiles up', 'files.created_by = up.id', 'left') + ->join('client_policy cp', 'files.policy_id = cp.id', 'left') + ->join('client_branch cb', 'files.client_branch_id = cb.id', 'left') + ->join('policy_type', 'policy_type.id = cp.policy_type_id') + ->join('clients c', 'files.client_id = c.id and files.client_id = cp.client_id', 'left') + ->join("client_rm cr", "cr.client_id = c.id and cr.is_active = 1", 'left') + ->where('files.is_active', 1); + + if (!empty($get_data['start_date'] ?? null) && !empty($get_data['end_date'] ?? null)) { + // 1. If dates are provided, use the user's filter + $start = change_date_format($get_data['start_date'], 'd/m/Y', 'Y-m-d') . ' 00:00:00'; + $end = change_date_format($get_data['end_date'], 'd/m/Y', 'Y-m-d') . ' 23:59:59'; + + $query->where('files.created_at >=', $start) + ->where('files.created_at <=', $end); + } else { + if(empty($get_data['tab_type'] ?? null)){ + // 2. Default: If no dates are selected, show last 90 days (or all data) + $query->where('files.created_at >=', date('Y-m-d 00:00:00', strtotime('-90 days'))) + ->where('files.created_at <=', date('Y-m-d 23:59:59')); + } + } + + if(!empty($get_data['client_id'] ?? null)){ + $query->where('files.client_id', $get_data['client_id']); + } + + if(!empty($get_data['client_branch_id'] ?? null)){ + $query->where('files.client_branch_id', $get_data['client_branch_id']); + } + + if(!empty($get_data['policy_id'] ?? null)){ + $query->where('files.policy_id', $get_data['policy_id']); + } + + if(!empty($get_data['event_type'] ?? null)){ + $query->where('files.action', $get_data['event_type']); + } + + if (in_array($role_id, [2, 3])) { + // If the user is a Account Manager or Manager, filter by user_id + $query->where('cr.user_id', (int)$user_id); + } + + $data['fileList'] = $query->groupBy("files.id") + ->orderBy('files.created_at', 'desc') + ->find(); + // dd($this->fileModel->getLastQuery()); // dd($data['fileList']); @@ -467,22 +511,62 @@ class EmployeeController extends AdminController policy_type.policy_type ") - ->join('client_policy', 'client_policy.id = batch_files.client_policy_id') - ->join('client_branch', 'client_branch.id = batch_files.client_branch_id') - ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') - ->join('clients', 'clients.id = client_policy.client_id') - ->join("client_rm cr","cr.client_id = clients.id and cr.is_active = 1",'left') - ->where('batch_files.is_active', 1); - if (in_array($role_id, [2, 3])) { - - // If the user is a Account Manager or Manager, filter by user_id - $query2->where('cr.user_id', (int)$user_id); - } - // ->where('files.created_by', get_session_userid()) - $data['batch_list'] = $query2->groupBy("batch_files.id")->orderBy('batch_files.id', 'desc') - ->limit(1500) - ->find(); - + ->join('client_policy', 'client_policy.id = batch_files.client_policy_id') + ->join('client_branch', 'client_branch.id = batch_files.client_branch_id') + ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') + ->join('clients', 'clients.id = client_policy.client_id') + ->join("client_rm cr", "cr.client_id = clients.id and cr.is_active = 1", 'left') + ->where('batch_files.is_active', 1); + + if (!empty($get_data['start_date'] ?? null) && !empty($get_data['end_date'] ?? null)) { + // 1. If dates are provided, use the user's filter + $start = change_date_format($get_data['start_date'], 'd/m/Y', 'Y-m-d') . ' 00:00:00'; + $end = change_date_format($get_data['end_date'], 'd/m/Y', 'Y-m-d') . ' 23:59:59'; + + $query2->where('batch_files.created_at >=', $start) + ->where('batch_files.created_at <=', $end); + + } else { + if(empty($get_data['tab_type'] ?? null)){ + // 2. Default: If no dates are selected, show last 90 days (or all data) + $query2->where('batch_files.created_at >=', date('Y-m-d 00:00:00', strtotime('-90 days'))) + ->where('batch_files.created_at <=', date('Y-m-d 23:59:59')); + } + } + + if(!empty($get_data['client_id'] ?? null)){ + $query2->where('batch_files.client_id', $get_data['client_id']); + } + + if(!empty($get_data['client_branch_id'] ?? null)){ + $query2->where('batch_files.client_branch_id', $get_data['client_branch_id']); + } + + if(!empty($get_data['policy_id'] ?? null)){ + $query2->where('batch_files.client_policy_id', $get_data['policy_id']); + } + + if(!empty($get_data['event_type'] ?? null)){ + $query2->where('batch_files.event_type', $get_data['event_type']); + } + + if(!empty($get_data['insurer_or_tpa'] ?? null)){ + $query2->where('batch_files.insurer_or_tpa', $get_data['insurer_or_tpa']); + } + + if(!empty($get_data['action_type'] ?? null)){ + $query2->where('batch_files.actions', $get_data['action_type']); + } + + if (in_array($role_id, [2, 3])) { + + // If the user is a Account Manager or Manager, filter by user_id + $query2->where('cr.user_id', (int)$user_id); + } + + $data['batch_list'] = $query2->groupBy("batch_files.id") + ->orderBy('batch_files.id', 'desc') + ->find(); // dd($data['fileList']);die(); diff --git a/app/Views/batch_list.php b/app/Views/batch_list.php index 8468b459..7b1b0f6f 100755 --- a/app/Views/batch_list.php +++ b/app/Views/batch_list.php @@ -1,7 +1,4 @@