diff --git a/app/Commands/TestEmployeeGlobalSearch.php b/app/Commands/TestEmployeeGlobalSearch.php new file mode 100644 index 0000000..625a82c --- /dev/null +++ b/app/Commands/TestEmployeeGlobalSearch.php @@ -0,0 +1,189 @@ + 'Client id (md5 hash or numeric)', + '--client_policy_id' => 'Client policy id', + '--client_branch_id' => 'Client branch id', + '--search' => 'Global search term', + '--limit' => 'Max rows to print (default 10)', + ]; + + public function run(array $params) + { + $clientId = $this->optionValue($params, 'client_id', 'cede2d63a7c04ebd4cb55a2228c7141a'); + $clientPolicyId = $this->optionValue($params, 'client_policy_id', '5769'); + $clientBranchId = $this->optionValue($params, 'client_branch_id', '444'); + $search = $this->optionValue($params, 'search', ''); + $limit = (int) $this->optionValue($params, 'limit', '10'); + + $model = new EmployeePolicyModel(); + + $withoutSearch = $model->getEmployeePolicy( + client_id: $clientId, + policy_id: $clientPolicyId, + status: 0, + branch_id: $clientBranchId, + status_type: 'hr' + ); + + // Fallback to a real active employee_policy row when defaults have no data + if (count($withoutSearch) === 0 && !$this->hasExplicitIds($params)) { + $fixture = $this->findSampleIds(); + if ($fixture === null) { + CLI::error('No active employee_policy rows found to test against.'); + + return; + } + + $clientId = (string) $fixture['client_id']; + $clientPolicyId = (string) $fixture['client_policy_id']; + $clientBranchId = (string) $fixture['client_branch_id']; + + CLI::write('Default IDs had no rows. Using sample from DB:', 'yellow'); + CLI::write(" client_id : {$clientId}"); + CLI::write(" client_policy_id : {$clientPolicyId}"); + CLI::write(" client_branch_id : {$clientBranchId}"); + CLI::newLine(); + + $withoutSearch = $model->getEmployeePolicy( + client_id: $clientId, + policy_id: $clientPolicyId, + status: 0, + branch_id: $clientBranchId, + status_type: 'hr' + ); + } + + if ($search === '' && count($withoutSearch) > 0) { + $search = (string) ($withoutSearch[0]['emp_code'] ?? $withoutSearch[0]['name'] ?? 'Self'); + CLI::write("No --search given. Using sample term from first row: {$search}", 'yellow'); + CLI::newLine(); + } + + CLI::write('Params:', 'yellow'); + CLI::write(" client_id : {$clientId}"); + CLI::write(" client_policy_id : {$clientPolicyId}"); + CLI::write(" client_branch_id : {$clientBranchId}"); + CLI::write(' search : ' . ($search !== '' ? $search : '(none)')); + CLI::newLine(); + + $withSearch = $model->getEmployeePolicy( + client_id: $clientId, + policy_id: $clientPolicyId, + status: 0, + branch_id: $clientBranchId, + status_type: 'hr', + search: $search + ); + + CLI::write('Result counts:', 'green'); + CLI::write(' without search : ' . count($withoutSearch)); + CLI::write(' with search : ' . count($withSearch)); + CLI::newLine(); + + $rows = $search !== '' ? $withSearch : $withoutSearch; + $preview = array_slice($rows, 0, max(1, $limit)); + + CLI::write('Preview fields (emp_code | name | mobile | email | tpa_id | dob | relationship | status):', 'yellow'); + foreach ($preview as $i => $row) { + CLI::write(sprintf( + '%d) %s | %s | %s | %s | %s | %s | %s | %s', + $i + 1, + $row['emp_code'] ?? '', + $row['name'] ?? '', + $row['mobile'] ?? '', + $row['email_corporate'] ?? '', + $row['tpa_id'] ?? '', + $row['formatted_dob'] ?? ($row['dob'] ?? ''), + $row['relationship'] ?? '', + $row['status'] ?? ($row['emp_status'] ?? '') + )); + } + + if (count($rows) > $limit) { + CLI::write('... ' . (count($rows) - $limit) . ' more row(s)'); + } + + if ($search !== '' && count($withSearch) > 0) { + CLI::newLine(); + CLI::write('Matched sample JSON:', 'green'); + CLI::write(json_encode(array_map(static function ($row) { + return [ + 'emp_code' => $row['emp_code'] ?? null, + 'name' => $row['name'] ?? null, + 'mobile' => $row['mobile'] ?? null, + 'email_corporate' => $row['email_corporate'] ?? null, + 'tpa_id' => $row['tpa_id'] ?? null, + 'dob' => $row['dob'] ?? null, + 'formatted_dob' => $row['formatted_dob'] ?? null, + 'relationship' => $row['relationship'] ?? null, + 'status' => $row['status'] ?? null, + 'emp_status' => $row['emp_status'] ?? null, + ]; + }, array_slice($withSearch, 0, 3)), JSON_PRETTY_PRINT)); + } elseif ($search !== '') { + CLI::write('No rows matched the search term.', 'red'); + } + } + + private function optionValue(array $params, string $name, string $default = ''): string + { + $value = $params[$name] ?? CLI::getOption($name); + if ($value === null || $value === false || $value === '') { + return $default; + } + + return (string) $value; + } + + private function hasExplicitIds(array $params): bool + { + foreach (['client_id', 'client_policy_id', 'client_branch_id'] as $key) { + $value = $params[$key] ?? CLI::getOption($key); + if ($value !== null && $value !== false && $value !== '') { + return true; + } + } + + return false; + } + + private function findSampleIds(): ?array + { + $db = Database::connect('default'); + $row = $db->query( + "SELECT emp.client_id, ep.client_policy_id, emp.client_branch_id + FROM employee_polices ep + JOIN employees emp ON emp.id = ep.employee_id + WHERE ep.is_active = 1 + AND emp.is_active = 1 + ORDER BY ep.id DESC + LIMIT 1" + )->getRowArray(); + + return $row ?: null; + } +} diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0fce77a..393db46 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -363,7 +363,7 @@ $routes->group("/util", ["filter" => "authMVC"], function ($routes) { $routes->post('checkSIMapping','ClientController::checkSIMapping'); $routes->post('deleteMapping','ClientController::deleteMapping'); $routes->get('removeLevelContacts','ClientController::removeLevelContacts'); - + $routes->get("downloadEmployeeListExcel", "EmployeeRestController::downloadEmployeeListExcel"); }); $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) { @@ -488,6 +488,7 @@ $routes->group("employeeRest", ['filter' => [ 'GlobalPostFileUploadGuard', 'appS $routes->post("iAgreeForAddOn", "EmployeeRestController::iAgreeForAddOn"); $routes->get("exportDataByClientPolicyId", "EmployeeRestController::exportDataByClientPolicyId"); + $routes->get("downloadEmployeeListExcel", "EmployeeRestController::downloadEmployeeListExcel"); $routes->get("getPolicyLevelEmployeeSummaryData", "EmployeeRestController::getPolicyLevelEmployeeSummaryData"); $routes->get("exportCashDepositData", "EmployeeRestController::exportCashDepositData"); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 502a6d5..5a11e11 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -691,7 +691,14 @@ class EmployeeRestController extends AdminController public function getEmployeeAndDependenceByClientId() { try { - $empData = $this->employeePolicyModel->getEmployeePolicy( client_id:$this->request->getGet('client_id'),policy_id: $this->request->getGet('client_policy_id'),status:0,branch_id:$this->request->getGet('client_branch_id'), status_type: 'hr'); + $empData = $this->employeePolicyModel->getEmployeePolicy( + client_id: $this->request->getGet('client_id'), + policy_id: $this->request->getGet('client_policy_id'), + status: 0, + branch_id: $this->request->getGet('client_branch_id'), + status_type: 'hr', + search: trim((string) ($this->request->getGet('search') ?? '')) + ); if ($empData) { return $this->respond(['status' => 'success', 'code' => 200, 'data' => $empData], 200); @@ -783,6 +790,101 @@ class EmployeeRestController extends AdminController } + /** + * Download employee + dependent list Excel. + * Filters: client_id, branch_id, policy_id only (same as getEmployeePolicy base filters). + * Columns: Emp Code, Name, TPA ID, Relationship, Date Of Birth, Gender, Mobile, Email, Status + */ + public function downloadEmployeeListExcel() + { + try { + $clientId = $this->request->getGet('client_id'); + $branchId = $this->request->getGet('branch_id'); + $policyId = $this->request->getGet('policy_id'); + + $empData = $this->employeePolicyModel->getEmployeePolicy( + client_id: $clientId, + policy_id: $policyId, + status: 0, + branch_id: $branchId + ); + + if (!count($empData)) { + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => [], 'message' => 'No employee data found'], 200); + } + + $headers = [ + 'Emp Code' => 'emp_code', + 'Name' => 'name', + 'TPA ID' => 'tpa_id', + 'Relationship' => 'relationship', + 'Date Of Birth' => 'formatted_dob', + 'Gender' => 'gender', + 'Mobile' => 'mobile', + 'Email' => 'email_corporate', + 'Status' => 'status', + ]; + + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->setTitle('Employee List'); + + $column = 'A'; + foreach ($headers as $header => $dbField) { + $sheet->setCellValue($column . '1', $header); + $column++; + } + + $headerRange = 'A1:I1'; + $sheet->getStyle($headerRange)->getFont()->setBold(true); + $sheet->getStyle($headerRange)->getFill() + ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) + ->getStartColor()->setRGB('BDD7EE'); + + $row = 2; + foreach ($empData as $employee) { + $column = 'A'; + foreach ($headers as $dbField) { + $value = $employee[$dbField] ?? ''; + + if ($dbField === 'formatted_dob' && empty($value) && !empty($employee['dob'])) { + $value = date('d/m/Y', strtotime($employee['dob'])); + } + + if ($dbField === 'gender' && $value !== '') { + $genderLower = strtolower(trim((string) $value)); + if ($genderLower === 'male' || $genderLower === 'm') { + $value = 'M'; + } elseif ($genderLower === 'female' || $genderLower === 'f') { + $value = 'F'; + } + } + + $sheet->setCellValue($column . $row, $value); + $column++; + } + $row++; + } + + foreach (range('A', 'I') as $col) { + $sheet->getColumnDimension($col)->setAutoSize(true); + } + + $policyName = $empData[0]['policy_name'] ?? 'Employee'; + $filename = preg_replace('/[^A-Za-z0-9_\-]/', '_', $policyName) . '-Employee-List.xlsx'; + + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + header('Content-Disposition: attachment;filename="' . $filename . '"'); + header('Cache-Control: max-age=0'); + + $writer = new Xlsx($spreadsheet); + $writer->save('php://output'); + exit; + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + //not in use public function getClientPolicy() { diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php index 834f295..05db7b6 100755 --- a/app/Models/EmployeePolicyModel.php +++ b/app/Models/EmployeePolicyModel.php @@ -82,7 +82,7 @@ class EmployeePolicyModel extends Model } // ---------------------------------------------------------------------------------------------------------- - public function getEmployeePolicy($client_id = 0, $policy_id = 0, $status = [], $branch_id = 0, $emp_code = "", $emp_name = "", $status_type = "") + public function getEmployeePolicy($client_id = 0, $policy_id = 0, $status = [], $branch_id = 0, $emp_code = "", $emp_name = "", $status_type = "", $search = "") { // dd($status); @@ -301,6 +301,10 @@ class EmployeePolicyModel extends Model $result->like('emp.name', $emp_name); } + if (!empty($search)) { + $this->applyEmployeeGlobalSearch($result, $search); + } + // Always check these conditions $result->where('employee_polices.is_active', 1) ->where('emp.is_active', 1); @@ -311,6 +315,30 @@ class EmployeePolicyModel extends Model return $res; } + + /** + * Global search across emp_code, name, mobile, email, tpa_id, dob, relationship, status. + */ + protected function applyEmployeeGlobalSearch($builder, string $search) + { + $search = trim($search); + if ($search === '') { + return $builder; + } + + return $builder->groupStart() + ->like('emp.emp_code', $search) + ->orLike('emp.name', $search) + ->orLike('emp.mobile', $search) + ->orLike('emp.email_corporate', $search) + ->orLike('employee_polices.tpa_id', $search) + ->orLike('emp.dob', $search) + ->orWhere("DATE_FORMAT(emp.dob, '%d/%m/%Y') LIKE " . $this->db->escape('%' . $search . '%'), null, false) + ->orLike('emp.relationship', $search) + ->orLike('employee_polices.status', $search) + ->orLike('emp.emp_status', $search) + ->groupEnd(); + } // public function getEmployeePolicy($client_id = 0, $policy_id = 0, $status = [], $branch_id = 0, $emp_code = "", $emp_name = "") // {