From b9519c994a1949d6f26e8f09ed36fc9dde6869d9 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Wed, 16 Jul 2025 18:16:00 +0530 Subject: [PATCH] FEAT_HR_HISTORY AND_OTHERS : RV --- app/Controllers/UserController.php | 303 +++++++++++--- app/Helpers/excel_util_helper.php | 2 +- app/Views/client_onboarding.php | 621 ++++++++++++++++------------- app/Views/hr_activity_history.php | 120 ++++++ 4 files changed, 704 insertions(+), 342 deletions(-) create mode 100644 app/Views/hr_activity_history.php diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index e9440c1b..78d3a6c0 100755 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -8,6 +8,8 @@ use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; use CodeIgniter\API\ResponseTrait; +use App\Controllers\ClientController; + use App\Models\UserModel; use App\Models\RoleModel; @@ -295,65 +297,265 @@ class UserController extends AdminController // } + // public function getUserActivityHistory() + // { + // try { + // $client_id = $this->request->getVar('client_id'); + // $user_id = $this->request->getVar('user_id'); + // $pre_hr_id = $this->request->getVar('pre_hr_id'); + // $user_type = $this->request->getVar('user_type'); + + // $clientController = new ClientController; + + // if (empty($client_id)) { + // return $this->respond(['status' => false,'message' => 'Client Id is required'], 400); + // } + + // if (empty($user_type)) { + // return $this->respond(['status' => false,'message' => 'User type is required'], 400); + // } + + // $field_for_where_condition = 'user_id'; + + // if (empty($user_id) || $user_id == 0) { + // if (empty($pre_hr_id)) { + // return $this->respond(['status' => false,'message' => 'user_id or pre_hr_id must be provided'], 400); + // } + + // $field_for_where_condition = 'pre_hr_id'; + // $user_id = $pre_hr_id; + // } + + // // Get login/auth history + // if ($field_for_where_condition == 'user_id') { + // $auth_data = $this->authHistoryModel + // ->where('is_active', 1) + // ->where('user_id', $user_id) + // ->where('user_type', $user_type) + // ->orderBy('created_at', 'desc') + // ->findAll(); + // // print_r(db_connect()->getLastQuery()); die; + // } else { + // $preDB = \Config\Database::connect('preDB'); + // $auth_data = $preDB->table('auth_history') + // ->where('is_active', 1) + // ->where('user_id', $user_id) + // ->where('user_type', $user_type) + // ->orderBy('created_at', 'desc') + // ->get() + // ->getResultArray(); + // } + + // // Get activity history + // $activity_data = $this->userActivityHistoryModel + // ->where($field_for_where_condition, $user_id) + // ->where('user_type', $user_type) + // ->findAll(); + + // $merged_data = $this->getMergedUserHistory($auth_data, $activity_data); + + // return $this->respond([ + // 'status' => true, + // 'message' => 'User history fetched successfully', + // 'auth_history' => $auth_data, + // 'activity_log' => $activity_data, + // 'merged_data' => $merged_data, + // ]); + + // } catch (\Exception $e) { + // $this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine() . '----' . $e->getTraceAsString())); + // return $this->respond([ + // 'status' => false, + // 'message' => 'Error: ' . $e->getMessage() + // ], 500); + // } + // } + public function getUserActivityHistory() { try { - $user_id = $this->request->getVar('user_id'); - $pre_hr_id = $this->request->getVar('pre_hr_id'); - $user_type = $this->request->getVar('user_type'); + $this->myLogger->logme("error", "User activity history fetch initiated."); - if (empty($user_type)) { - return $this->respond(['status' => false,'message' => 'User type is required'], 400); + $client_id = $this->request->getVar('client_id'); + $startDate = $this->request->getVar('startDate'); + $endDate = $this->request->getVar('endDate'); + $user_type = 'hr'; + $clientController = new ClientController; + + // Convert to DB format with full time range + $startDateTime = null; + $endDateTime = null; + + if (!empty($startDate)) { + $startDateTime = \DateTime::createFromFormat('d-m-Y', $startDate)->format('Y-m-d 00:00:00'); + $endDateTime = \DateTime::createFromFormat('d-m-Y', $endDate)->format('Y-m-d 23:59:59'); } - $field_for_where_condition = 'user_id'; - if (empty($user_id) || $user_id == 0) { - if (empty($pre_hr_id)) { - return $this->respond(['status' => false,'message' => 'user_id or pre_hr_id must be provided'], 400); + $this->myLogger->logme("error", "Received client_id: " . $client_id); + + if (empty($client_id)) { + $this->myLogger->logme("warning", "Client ID is missing."); + return $this->respond(['status' => false, 'message' => 'Client Id is required'], 400); + } + + $hr_access_data = $clientController->getHrAccessData($client_id)['hr_access_data'] ?? []; + $this->myLogger->logme("error", "Fetched HR access data: " . json_encode($hr_access_data)); + + if (empty($hr_access_data)) { + $this->myLogger->logme("error", "No HR access data found for client_id: $client_id"); + $html = view('hr_activity_history'); + return $this->respond(['status' => true, 'data' => $html, 'list_of_activity_data' => []], 200); + } + + $activityMap = [ + 'export_empdata' => 'Exporting post employee data', + 'export_cddata' => 'Exporting cd data', + 'export_cdsummary' => 'Exporting cd summary data', + 'export_preempdata' => 'Exporting pre employee data', + 'import_enrollempdata' => 'Import enrolment file', + ]; + + $list_of_activity_data = []; + + foreach ($hr_access_data as $key => $value) { + $this->myLogger->logme("error", "Processing HR user: " . json_encode($value)); + + $auth_data = []; + $activity_data = []; + + $hr_name = $value['hr_name'] ?? ''; + $hr_mail = $value['hr_mail'] ?? ''; + + if (!empty($value['pre_hr_id']) && !empty($value['post_hr_id'])) { + $this->myLogger->logme("error", "Both pre_hr_id and post_hr_id found. Using post DB for user_id: {$value['post_hr_id']}"); + + // Fetch Auth History + $auth_query = $this->authHistoryModel + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at") + ->where('is_active', 1) + ->where('user_id', $value['post_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $auth_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $auth_data = $auth_query->orderBy('created_at', 'desc')->findAll(); + + + // Fetch User Activity + $activity_query = $this->userActivityHistoryModel + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at, activity") + ->where('user_id', $value['post_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $activity_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $activity_data = $activity_query->findAll(); + + + } elseif (!empty($value['post_hr_id'])) { + $this->myLogger->logme("error", "Only post_hr_id found. user_id: {$value['post_hr_id']}"); + + $auth_query = $this->authHistoryModel + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at") + ->where('is_active', 1) + ->where('user_id', $value['post_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $auth_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $auth_data = $auth_query->orderBy('created_at', 'desc')->findAll(); + + $activity_query = $this->userActivityHistoryModel + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at, activity") + ->where('user_id', $value['post_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $activity_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $activity_data = $activity_query->findAll(); + + } elseif (!empty($value['pre_hr_id'])) { + $this->myLogger->logme("error", "Only pre_hr_id found. user_id: {$value['pre_hr_id']}"); + + $preDB = \Config\Database::connect('preDB'); + + $auth_query = $preDB->table('auth_history') + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at") + ->where('is_active', 1) + ->where('user_id', $value['pre_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $auth_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $auth_data = $auth_query->orderBy('created_at', 'desc')->get()->getResultArray(); + + $activity_query = $this->userActivityHistoryModel + ->select("DATE_FORMAT(created_at, '%d-%m-%Y %r') AS created_at, activity") + ->where('pre_hr_id', $value['pre_hr_id']) + ->where('user_type', $user_type); + + if (!empty($startDate)) { + $activity_query->where('created_at >=', $startDateTime) + ->where('created_at <=', $endDateTime); + } + + $activity_data = $activity_query->findAll(); } - $field_for_where_condition = 'pre_hr_id'; - $user_id = $pre_hr_id; + foreach ($auth_data as &$entry) { + $entry['user_name'] = $hr_name; + $entry['user_mail'] = $hr_mail; + $entry['activity'] = "Login"; + } + + foreach ($activity_data as &$entry) { + $entry['user_name'] = $hr_name; + $entry['user_mail'] = $hr_mail; + + if (!empty($entry['activity']) && isset($activityMap[$entry['activity']])) { + $entry['activity'] = $activityMap[$entry['activity']]; + } + } + + $merged_data = array_merge($auth_data, $activity_data); + $list_of_activity_data = array_merge($list_of_activity_data, $merged_data); + + $this->myLogger->logme("error", "Merged data count after processing: " . count($list_of_activity_data)); } - // Get login/auth history - if ($field_for_where_condition == 'pre_hr_id') { - $auth_data = $this->authHistoryModel - ->where('is_active', 1) - ->where('user_id', $user_id) - ->where('user_type', $user_type) - ->orderBy('created_at', 'desc') - ->findAll(); - } else { - $preDB = \Config\Database::connect('preDB'); - $auth_data = $preDB->table('auth_history') - ->where('is_active', 1) - ->where('user_id', $user_id) - ->where('user_type', $user_type) - ->orderBy('created_at', 'desc') - ->get() - ->getResultArray(); - } + usort($list_of_activity_data, function ($a, $b) { + return strtotime($b['created_at']) <=> strtotime($a['created_at']); + }); - // Get activity history - $activity_data = $this->userActivityHistoryModel - ->where($field_for_where_condition, $user_id) - ->where('user_type', $user_type) - ->findAll(); + $this->myLogger->logme("error", "Final sorted list_of_activity_data count: " . count($list_of_activity_data)); - $merged_data = $this->getMergedUserHistory($auth_data, $activity_data); + $html = view('hr_activity_history', ['data' => $list_of_activity_data]); return $this->respond([ 'status' => true, 'message' => 'User history fetched successfully', - 'auth_history' => $auth_data, - 'activity_log' => $activity_data, - 'merged_data' => $merged_data, + 'data' => $html, + 'list_of_activity_data' => $list_of_activity_data, ]); } catch (\Exception $e) { - $this->myLogger->logme("error", ($e->getMessage().' --- '.$e->getLine() . '----' . $e->getTraceAsString())); + $this->myLogger->logme("error", $e->getMessage() . ' --- ' . $e->getLine() . ' ---- ' . $e->getTraceAsString()); return $this->respond([ 'status' => false, 'message' => 'Error: ' . $e->getMessage() @@ -361,27 +563,4 @@ class UserController extends AdminController } } - public function getMergedUserHistory($auth_data, $activity_data) - { - // Tag each entry with its source - foreach ($auth_data as &$entry) { - $entry['source'] = 'auth'; - } - - foreach ($activity_data as &$entry) { - $entry['source'] = 'activity'; - } - - // Merge both arrays - $merged = array_merge($auth_data, $activity_data); - - // Sort by 'created_at' descending - usort($merged, function ($a, $b) { - return strtotime($b['created_at']) <=> strtotime($a['created_at']); - }); - - return $merged; - } - - } \ No newline at end of file diff --git a/app/Helpers/excel_util_helper.php b/app/Helpers/excel_util_helper.php index 00dc0a9d..a2695618 100755 --- a/app/Helpers/excel_util_helper.php +++ b/app/Helpers/excel_util_helper.php @@ -252,7 +252,7 @@ if (!function_exists('check_si')) { if (!$is_si_found && $slab_value['si'] == $received_si) //match si amount { - echo 'found - ' . $slab_value['si']. ' - ' . $received_si; + // echo 'found - ' . $slab_value['si']. ' - ' . $received_si; $is_si_found = true; } diff --git a/app/Views/client_onboarding.php b/app/Views/client_onboarding.php index 8e858801..ac83d0c8 100755 --- a/app/Views/client_onboarding.php +++ b/app/Views/client_onboarding.php @@ -34,224 +34,224 @@ body { - + .output-table { + font-family: monospace; + font-size: 0.85em; + white-space: pre-wrap; + background: white; + padding: 10px; + border-radius: 4px; + border: 1px solid #dee2e6; + } + @@ -314,6 +314,12 @@ body { HR Access Controll + + + + @@ -358,6 +375,7 @@ body { \ No newline at end of file