diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index e00fcd5..da982d0 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -18,18 +18,23 @@ class AdminController extends Controller { $selectedMonth = $request->input('month', Carbon::now()->format('Y-m')); $search = $request->input('search'); - $status = $request->input('status', (string)User::STATUS_ACTIVE); + $status = $request->input('status', (string)config('constants.STATUS_ACTIVE')); $role = $request->input('role'); $department = $request->input('department'); $flagSend = $request->input('flag_send'); - ['users' => $users, 'topReceivedUser' => $topReceivedUser, 'topSentUser' => $topSentUser] - = $this->adminService->getUserListWithStats($selectedMonth, $search, $status, $role, $department, $flagSend); + [ + 'users' => $users, + 'topReceivedUser' => $topReceivedUser, + 'topSentUser' => $topSentUser, + 'topReceivedUsers' => $topReceivedUsers, + 'topSentUsers' => $topSentUsers + ] = $this->adminService->getUserListWithStats($selectedMonth, $search, $status, $role, $department, $flagSend); if ($request->ajax() || $request->wantsJson()) { return response()->json([ 'title' => view('admin.users.partials.title', compact('selectedMonth'))->render(), - 'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser'))->render(), + 'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers'))->render(), 'table' => view('admin.users.partials.table', compact('users'))->render(), ]); } @@ -39,6 +44,8 @@ class AdminController extends Controller 'selectedMonth', 'topReceivedUser', 'topSentUser', + 'topReceivedUsers', + 'topSentUsers', 'search', 'status', 'role', @@ -58,9 +65,9 @@ class AdminController extends Controller 'msnv' => 'required|integer|unique:user,msnv', 'name' => 'required|string|max:255', 'mail' => 'required|email|unique:user,mail', - 'departments' => 'required|integer|min:1|max:5', + 'departments' => 'required|string|in:' . implode(',', config('constants.DEPARTMENTS')), 'password' => 'required|string', - 'role' => 'required|in:' . User::ROLE_MEMBER . ',' . User::ROLE_ADMIN, + 'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'), ]); $this->adminService->createUser($request->only('msnv', 'name', 'mail', 'departments', 'password', 'role')); @@ -75,17 +82,30 @@ class AdminController extends Controller $administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth); - return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth')); + $addCards = \App\Models\AddCard::with('sellerUser') + ->where('buyer', $user->msnv) + ->orderBy('date', 'desc') + ->orderBy('id', 'desc') + ->get(); + + $admins = User::where('role', config('constants.ROLE_ADMIN')) + ->where('status', config('constants.STATUS_ACTIVE')) + ->get(); + + return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins')); } public function update(Request $request, $msnv) { $request->validate([ - 'num_card' => 'nullable|integer|min:1', - 'flag_send' => 'nullable|boolean', + 'card' => 'required|integer|min:0', + 'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'), + 'status' => 'required|in:' . config('constants.STATUS_INACTIVE') . ',' . config('constants.STATUS_ACTIVE'), + 'flag_send' => 'required|in:' . config('constants.FLAG_SEND_DISABLED') . ',' . config('constants.FLAG_SEND_ENABLED'), + 'first_login' => 'required|in:' . config('constants.FIRST_LOGIN_FALSE') . ',' . config('constants.FIRST_LOGIN_TRUE'), ]); - $this->adminService->updateUser($msnv, $request->only('num_card', 'flag_send')); + $this->adminService->updateUser($msnv, $request->only('card', 'role', 'status', 'flag_send', 'first_login')); return redirect()->back()->with('success', __('messages.user_update_success')); } @@ -97,6 +117,22 @@ class AdminController extends Controller return redirect()->route('admin.users.index')->with('success', __('messages.user_deactivate_success')); } + public function updateAddCard(Request $request, $id) + { + $request->validate([ + 'num_card' => 'required|integer|min:1', + 'seller' => 'required|exists:user,msnv', + 'date' => 'required|date', + ]); + + try { + $this->adminService->updateAddCard(intval($id), $request->only('num_card', 'seller', 'date')); + return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.'); + } catch (\Exception $e) { + return redirect()->back()->withErrors(['error' => $e->getMessage()]); + } + } + public function resetCards() { $this->adminService->resetAllCards(); diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 7f1cac0..41605d8 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -62,7 +62,7 @@ class AuthController extends Controller \Illuminate\Support\Facades\RateLimiter::clear($throttleKey); $user = $result['user']; - if ($user->role != \App\Models\User::ROLE_ADMIN && $user->first_login == \App\Models\User::FIRST_LOGIN_TRUE) { + if ($user->role != config('constants.ROLE_ADMIN') && $user->first_login == config('constants.FIRST_LOGIN_TRUE')) { return redirect()->route('login')->with('dialog_first_login', true); } diff --git a/app/Http/Controllers/User/UserController.php b/app/Http/Controllers/User/UserController.php index 9e05626..052d7c9 100644 --- a/app/Http/Controllers/User/UserController.php +++ b/app/Http/Controllers/User/UserController.php @@ -48,7 +48,7 @@ class UserController extends Controller $request->validate([ 'receiver' => [ 'required', - \Illuminate\Validation\Rule::exists('user', 'msnv')->where('status', \App\Models\User::STATUS_ACTIVE) + \Illuminate\Validation\Rule::exists('user', 'msnv')->where('status', config('constants.STATUS_ACTIVE')) ], 'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH, ]); @@ -84,7 +84,7 @@ class UserController extends Controller $user = Auth::user(); $this->userService->updatePassword($user, $request->password); - $route = $user->role == \App\Models\User::ROLE_ADMIN ? 'admin.dashboard' : 'user.dashboard'; + $route = $user->role == config('constants.ROLE_ADMIN') ? 'admin.dashboard' : 'user.dashboard'; return redirect()->route($route)->with('success', __('messages.password_change_success')); } } diff --git a/app/Http/Middleware/Admin/AdminMiddleware.php b/app/Http/Middleware/Admin/AdminMiddleware.php index d569a7f..85b7581 100644 --- a/app/Http/Middleware/Admin/AdminMiddleware.php +++ b/app/Http/Middleware/Admin/AdminMiddleware.php @@ -12,7 +12,7 @@ class AdminMiddleware { public function handle(Request $request, Closure $next): Response { - if (Auth::check() && Auth::user()->role == User::ROLE_ADMIN && Auth::user()->status == User::STATUS_ACTIVE) { + if (Auth::check() && Auth::user()->role == config('constants.ROLE_ADMIN') && Auth::user()->status == config('constants.STATUS_ACTIVE')) { return $next($request); } return redirect()->route('login'); diff --git a/app/Http/Middleware/ForceChangePasswordMiddleware.php b/app/Http/Middleware/ForceChangePasswordMiddleware.php index 7e71d1b..3152a52 100644 --- a/app/Http/Middleware/ForceChangePasswordMiddleware.php +++ b/app/Http/Middleware/ForceChangePasswordMiddleware.php @@ -12,7 +12,7 @@ class ForceChangePasswordMiddleware { public function handle(Request $request, Closure $next): Response { - if (Auth::check() && Auth::user()->role != User::ROLE_ADMIN && Auth::user()->first_login == User::FIRST_LOGIN_TRUE) { + if (Auth::check() && Auth::user()->role != config('constants.ROLE_ADMIN') && Auth::user()->first_login == config('constants.FIRST_LOGIN_TRUE')) { if (!$request->routeIs('user.change_password') && !$request->routeIs('user.update_password') && !$request->routeIs('logout')) { return redirect()->route('user.change_password'); } diff --git a/app/Http/Middleware/User/MemberMiddleware.php b/app/Http/Middleware/User/MemberMiddleware.php index 4f900fc..2baee42 100644 --- a/app/Http/Middleware/User/MemberMiddleware.php +++ b/app/Http/Middleware/User/MemberMiddleware.php @@ -12,7 +12,7 @@ class MemberMiddleware { public function handle(Request $request, Closure $next): Response { - if (Auth::check() && in_array(Auth::user()->role, [User::ROLE_MEMBER, User::ROLE_ADMIN]) && Auth::user()->status == User::STATUS_ACTIVE) { + if (Auth::check() && in_array(Auth::user()->role, [config('constants.ROLE_MEMBER'), config('constants.ROLE_ADMIN')]) && Auth::user()->status == config('constants.STATUS_ACTIVE')) { return $next($request); } return redirect()->route('login'); diff --git a/app/Models/AddCard.php b/app/Models/AddCard.php index 0706ebe..8076cdb 100644 --- a/app/Models/AddCard.php +++ b/app/Models/AddCard.php @@ -19,4 +19,9 @@ class AddCard extends Model 'seller', 'date', ]; + + public function sellerUser() + { + return $this->belongsTo(User::class, 'seller', 'msnv'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 15f1895..119acea 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,18 +10,6 @@ class User extends Authenticatable { use HasFactory, Notifiable; - const ROLE_MEMBER = 0; - const ROLE_ADMIN = 1; - - const STATUS_INACTIVE = 0; - const STATUS_ACTIVE = 1; - - const FLAG_SEND_DISABLED = 0; - const FLAG_SEND_ENABLED = 1; - - const FIRST_LOGIN_FALSE = 0; - const FIRST_LOGIN_TRUE = 1; - protected $table = 'user'; public $timestamps = false; diff --git a/app/Services/Admin/AdminService.php b/app/Services/Admin/AdminService.php index ee53128..b004add 100644 --- a/app/Services/Admin/AdminService.php +++ b/app/Services/Admin/AdminService.php @@ -35,8 +35,21 @@ class AdminService implements AdminServiceInterface ->whereBetween('date', [$startOfMonth, $endOfMonth]) ]); - $topReceivedUser = (clone $usersQuery)->where('status', User::STATUS_ACTIVE)->orderByDesc('total_received')->first(); - $topSentUser = (clone $usersQuery)->where('status', User::STATUS_ACTIVE)->orderByDesc('total_sent')->first(); + $activeUsers = (clone $usersQuery)->where('status', config('constants.STATUS_ACTIVE'))->get(); + + $maxReceived = $activeUsers->max('total_received') ?? 0; + $maxSent = $activeUsers->max('total_sent') ?? 0; + + $topReceivedUsers = $maxReceived > 0 + ? $activeUsers->filter(fn($u) => $u->total_received == $maxReceived)->values() + : collect(); + + $topSentUsers = $maxSent > 0 + ? $activeUsers->filter(fn($u) => $u->total_sent == $maxSent)->values() + : collect(); + + $topReceivedUser = $topReceivedUsers->first(); + $topSentUser = $topSentUsers->first(); if (!is_null($status) && $status !== '') { $usersQuery->where('status', intval($status)); @@ -47,7 +60,7 @@ class AdminService implements AdminServiceInterface } if (!is_null($department) && $department !== '') { - $usersQuery->where('departments', intval($department)); + $usersQuery->where('departments', $department); } if (!is_null($flagSend) && $flagSend !== '') { @@ -62,10 +75,9 @@ class AdminService implements AdminServiceInterface ->orWhereRaw('LOWER(mail) LIKE ?', ['%' . strtolower($search) . '%']); }); } - $users = $usersQuery->paginate(20)->withQueryString(); - return compact('users', 'topReceivedUser', 'topSentUser'); + return compact('users', 'topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers'); } public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator @@ -89,10 +101,10 @@ class AdminService implements AdminServiceInterface 'pass' => md5($data['password']), 'departments' => $data['departments'], 'role' => $data['role'], - 'status' => User::STATUS_ACTIVE, + 'status' => config('constants.STATUS_ACTIVE'), 'card' => 0, - 'flag_send' => User::FLAG_SEND_DISABLED, - 'first_login' => $data['role'] == User::ROLE_ADMIN ? User::FIRST_LOGIN_FALSE : User::FIRST_LOGIN_TRUE, + 'flag_send' => config('constants.FLAG_SEND_DISABLED'), + 'first_login' => $data['role'] == config('constants.ROLE_ADMIN') ? config('constants.FIRST_LOGIN_FALSE') : config('constants.FIRST_LOGIN_TRUE'), ]); } @@ -101,17 +113,24 @@ class AdminService implements AdminServiceInterface $user = User::where('msnv', $msnv)->firstOrFail(); DB::transaction(function () use ($data, $user) { - if (!empty($data['num_card'])) { + $newCard = intval($data['card']); + $oldCard = intval($user->card); + + if ($newCard > $oldCard) { + $diff = $newCard - $oldCard; AddCard::create([ 'buyer' => $user->msnv, - 'num_card' => $data['num_card'], + 'num_card' => $diff, 'seller' => Auth::user()->msnv, 'date' => Carbon::today(), ]); - $user->card += $data['num_card']; } - $user->flag_send = isset($data['flag_send']) ? User::FLAG_SEND_ENABLED : User::FLAG_SEND_DISABLED; + $user->card = $newCard; + $user->role = intval($data['role']); + $user->status = intval($data['status']); + $user->flag_send = intval($data['flag_send']); + $user->first_login = intval($data['first_login']); $user->save(); }); } @@ -119,12 +138,50 @@ class AdminService implements AdminServiceInterface public function deactivateUser(string $msnv): void { $user = User::where('msnv', $msnv)->firstOrFail(); - $user->status = User::STATUS_INACTIVE; + $user->status = config('constants.STATUS_INACTIVE'); $user->save(); } public function resetAllCards(): void { - User::where('status', User::STATUS_ACTIVE)->update(['card' => 0]); + User::where('status', config('constants.STATUS_ACTIVE'))->update(['card' => 0]); + } + + public function updateAddCard(int $id, array $data): void + { + $addCard = AddCard::findOrFail($id); + + + $currentMonth = Carbon::now()->format('Y-m'); + $recordMonth = Carbon::parse($addCard->date)->format('Y-m'); + + if ($recordMonth !== $currentMonth) { + throw new \Exception('Không cho phép chỉnh sửa dữ liệu card của các tháng trước.'); + } + + $newDateMonth = Carbon::parse($data['date'])->format('Y-m'); + if ($newDateMonth !== $currentMonth) { + throw new \Exception('Chỉ được phép chỉnh sửa dữ liệu của tháng hiện tại.'); + } + + $user = User::where('msnv', $addCard->buyer)->firstOrFail(); + + DB::transaction(function () use ($addCard, $user, $data) { + $oldNumCard = intval($addCard->num_card); + $newNumCard = intval($data['num_card']); + $diff = $newNumCard - $oldNumCard; + + if ($user->card + $diff < 0) { + throw new \Exception('Số lượng card cập nhật không hợp lệ vì tổng số card của user không được nhỏ hơn 0.'); + } + + $user->card += $diff; + $user->save(); + + $addCard->num_card = $newNumCard; + $addCard->seller = $data['seller']; + $addCard->date = $data['date']; + $addCard->save(); + }); } } \ No newline at end of file diff --git a/app/Services/Admin/Contracts/AdminServiceInterface.php b/app/Services/Admin/Contracts/AdminServiceInterface.php index e66bcfc..032165f 100644 --- a/app/Services/Admin/Contracts/AdminServiceInterface.php +++ b/app/Services/Admin/Contracts/AdminServiceInterface.php @@ -24,4 +24,6 @@ interface AdminServiceInterface public function deactivateUser(string $msnv): void; public function resetAllCards(): void; + + public function updateAddCard(int $id, array $data): void; } \ No newline at end of file diff --git a/app/Services/Auth/AuthService.php b/app/Services/Auth/AuthService.php index 67df2b9..b5d87d2 100644 --- a/app/Services/Auth/AuthService.php +++ b/app/Services/Auth/AuthService.php @@ -20,7 +20,7 @@ class AuthService implements AuthServiceInterface // Log the user in manually Auth::login($user); $request->session()->regenerate(); - if ($user->status != User::STATUS_ACTIVE) { + if ($user->status != config('constants.STATUS_ACTIVE')) { Auth::logout(); return ['success' => false, 'error_key' => 'permission', 'error_msg' => 'Bạn không có quyền truy cập. Vui lòng liên hệ quản trị viên']; } @@ -37,11 +37,11 @@ class AuthService implements AuthServiceInterface public function getRedirectRouteForUser(User $user): string { - if ($user->role == User::ROLE_ADMIN) { + if ($user->role == config('constants.ROLE_ADMIN')) { return route('admin.dashboard'); } - if ($user->first_login == User::FIRST_LOGIN_TRUE) { + if ($user->first_login == config('constants.FIRST_LOGIN_TRUE')) { return route('user.change_password'); } diff --git a/app/Services/User/UserService.php b/app/Services/User/UserService.php index f595a91..53b7d7f 100644 --- a/app/Services/User/UserService.php +++ b/app/Services/User/UserService.php @@ -27,19 +27,19 @@ class UserService implements UserServiceInterface public function getOtherActiveUsers(User $currentUser): Collection { - return User::where('status', User::STATUS_ACTIVE) + return User::where('status', config('constants.STATUS_ACTIVE')) ->where('msnv', '!=', $currentUser->msnv) ->get(); } public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void { - if ($sender->flag_send == User::FLAG_SEND_DISABLED) { + if ($sender->flag_send == config('constants.FLAG_SEND_DISABLED')) { throw new \RuntimeException(__('messages.error.no_send_permission')); } $receiver = User::where('msnv', $receiverMsnv)->first(); - if (!$receiver || $receiver->status != User::STATUS_ACTIVE) { + if (!$receiver || $receiver->status != config('constants.STATUS_ACTIVE')) { throw new \RuntimeException(__('messages.error.receiver_invalid')); } @@ -91,7 +91,7 @@ class UserService implements UserServiceInterface public function updatePassword(User $user, string $newPassword): void { $user->pass = md5($newPassword); - $user->first_login = User::FIRST_LOGIN_FALSE; + $user->first_login = config('constants.FIRST_LOGIN_FALSE'); $user->save(); } @@ -109,7 +109,7 @@ class UserService implements UserServiceInterface ->sum('sent'); // Calculate current rank (based on received) - $rankings = User::where('status', User::STATUS_ACTIVE) + $rankings = User::where('status', config('constants.STATUS_ACTIVE')) ->addSelect([ 'score' => Administration::selectRaw('COALESCE(SUM(received), 0)') ->whereColumn('msnv', 'user.msnv') @@ -156,7 +156,7 @@ class UserService implements UserServiceInterface $column = $type === 'sent' ? 'sent' : 'received'; - $allActiveUsers = User::where('status', User::STATUS_ACTIVE) + $allActiveUsers = User::where('status', config('constants.STATUS_ACTIVE')) ->addSelect([ 'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)') ->whereColumn('msnv', 'user.msnv') diff --git a/config/constants.php b/config/constants.php new file mode 100644 index 0000000..2743af2 --- /dev/null +++ b/config/constants.php @@ -0,0 +1,33 @@ + 0, + 'ROLE_ADMIN' => 1, + + 'STATUS_INACTIVE' => 0, + 'STATUS_ACTIVE' => 1, + + 'FLAG_SEND_DISABLED' => 0, + 'FLAG_SEND_ENABLED' => 1, + + 'FIRST_LOGIN_FALSE' => 0, + 'FIRST_LOGIN_TRUE' => 1, + + 'DEPARTMENTS' => [ + 'BIZ-IID - Internet Infra Business Division / BIZ-IID - Sales Enterprise Team (HCM)', + 'BIZ-ITOVN - Vietnam Business Division / BIZ-ITOVN - Ho Chi Minh', + 'BIZ-SSD - Smart Solutions Business Division / BIZ-SSD - Ho Chi Minh', + 'CoE - Center of Excellence / CoE - Data (Ho Chi Minh)', + 'CoE - Center of Excellence / CoE - R&D (Ho Chi Minh)', + 'CoE - Center of Excellence / CoE - UI/UX (Ho Chi Minh)', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch)', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch) / DL-ITOHCM - BrSE Team', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch) / DL-ITOHCM-DU1 - Delivery Unit 1', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch) / DL-ITOHCM-DU2 - Delivery Unit 2', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch) / DL-ITOHCM-DU3 - Delivery Unit 3', + 'DL-ITOHCM - ITO Delivery Division (HCM Branch) / DL-ITOHCM-DU4 - Delivery Unit 4', + 'DL-SSD - Smart Solutions Delivery Division / DL-SSD - Ho Chi Minh', + 'FC-HRD - Human Resources Division / FC-HRD - Ho Chi Minh', + 'FC-RMD - Risk Management Division / FC-RMD - Ho Chi Minh' + ] +]; diff --git a/database/seeders/MockDataSeeder.php b/database/seeders/MockDataSeeder.php index f4b21b5..93037ce 100644 --- a/database/seeders/MockDataSeeder.php +++ b/database/seeders/MockDataSeeder.php @@ -35,11 +35,11 @@ class MockDataSeeder extends Seeder 'mail' => 'admin@runsystem.net', 'pass' => $password, 'departments' => 1, - 'role' => User::ROLE_ADMIN, - 'status' => User::STATUS_ACTIVE, + 'role' => config('constants.ROLE_ADMIN'), + 'status' => config('constants.STATUS_ACTIVE'), 'card' => 0, - 'flag_send' => User::FLAG_SEND_ENABLED, - 'first_login' => User::FIRST_LOGIN_FALSE, + 'flag_send' => config('constants.FLAG_SEND_ENABLED'), + 'first_login' => config('constants.FIRST_LOGIN_FALSE'), ]); User::create([ @@ -48,20 +48,20 @@ class MockDataSeeder extends Seeder 'mail' => 'admin2@runsystem.net', 'pass' => $password, 'departments' => 1, - 'role' => User::ROLE_ADMIN, - 'status' => User::STATUS_ACTIVE, + 'role' => config('constants.ROLE_ADMIN'), + 'status' => config('constants.STATUS_ACTIVE'), 'card' => 0, - 'flag_send' => User::FLAG_SEND_ENABLED, - 'first_login' => User::FIRST_LOGIN_FALSE, + 'flag_send' => config('constants.FLAG_SEND_ENABLED'), + 'first_login' => config('constants.FIRST_LOGIN_FALSE'), ]); // Active Users (Initialize with specific users needed for logs/transactions) $users = [ - ['msnv' => 1001, 'name' => 'Nguyễn Văn A', 'mail' => 'nguyenvana@runsystem.net', 'card' => 10, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE], - ['msnv' => 1002, 'name' => 'Trần Thị Ngọc', 'mail' => 'tranthingoc@runsystem.net', 'card' => 3, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE], - ['msnv' => 1003, 'name' => 'Lê Kim Thư', 'mail' => 'lekiemthu@runsystem.net', 'card' => 6, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_TRUE], // First-time login - ['msnv' => 1004, 'name' => 'Phòng Nhân Sự', 'mail' => 'phongnhansu@runsystem.net', 'card' => 0, 'flag' => User::FLAG_SEND_DISABLED, 'first_login' => User::FIRST_LOGIN_FALSE], // Disabled sending permission - ['msnv' => 1005, 'name' => 'Bản Giang', 'mail' => 'bangiang@runsystem.net', 'card' => 36, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE], + ['msnv' => 1001, 'name' => 'Nguyễn Văn A', 'mail' => 'nguyenvana@runsystem.net', 'card' => 10, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')], + ['msnv' => 1002, 'name' => 'Trần Thị Ngọc', 'mail' => 'tranthingoc@runsystem.net', 'card' => 3, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')], + ['msnv' => 1003, 'name' => 'Lê Kim Thư', 'mail' => 'lekiemthu@runsystem.net', 'card' => 6, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_TRUE')], // First-time login + ['msnv' => 1004, 'name' => 'Phòng Nhân Sự', 'mail' => 'phongnhansu@runsystem.net', 'card' => 0, 'flag' => config('constants.FLAG_SEND_DISABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')], // Disabled sending permission + ['msnv' => 1005, 'name' => 'Bản Giang', 'mail' => 'bangiang@runsystem.net', 'card' => 36, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')], ]; // Generate additional users to reach exactly 100 member users @@ -76,8 +76,8 @@ class MockDataSeeder extends Seeder 'name' => $randomName, 'mail' => 'dev' . str_pad($i, 3, '0', STR_PAD_LEFT) . '@runsystem.net', 'card' => rand(0, 30), - 'flag' => rand(0, 5) > 0 ? User::FLAG_SEND_ENABLED : User::FLAG_SEND_DISABLED, - 'first_login' => rand(0, 4) == 0 ? User::FIRST_LOGIN_TRUE : User::FIRST_LOGIN_FALSE + 'flag' => rand(0, 5) > 0 ? config('constants.FLAG_SEND_ENABLED') : config('constants.FLAG_SEND_DISABLED'), + 'first_login' => rand(0, 4) == 0 ? config('constants.FIRST_LOGIN_TRUE') : config('constants.FIRST_LOGIN_FALSE') ]; } @@ -88,8 +88,8 @@ class MockDataSeeder extends Seeder 'mail' => $u['mail'], 'pass' => $password, 'departments' => rand(1, 5), - 'role' => User::ROLE_MEMBER, - 'status' => User::STATUS_ACTIVE, + 'role' => config('constants.ROLE_MEMBER'), + 'status' => config('constants.STATUS_ACTIVE'), 'card' => $u['card'], 'flag_send' => $u['flag'], 'first_login' => $u['first_login'], @@ -115,11 +115,11 @@ class MockDataSeeder extends Seeder 'mail' => 'nghiduy@runsystem.net', 'pass' => $password, 'departments' => 2, - 'role' => User::ROLE_MEMBER, - 'status' => User::STATUS_INACTIVE, + 'role' => config('constants.ROLE_MEMBER'), + 'status' => config('constants.STATUS_INACTIVE'), 'card' => 0, - 'flag_send' => User::FLAG_SEND_DISABLED, - 'first_login' => User::FIRST_LOGIN_FALSE, + 'flag_send' => config('constants.FLAG_SEND_DISABLED'), + 'first_login' => config('constants.FIRST_LOGIN_FALSE'), ]); // 2. CREATE CARD ADDITION LOGS (add_card table) @@ -195,5 +195,49 @@ class MockDataSeeder extends Seeder 'date' => $t['date'] ]); } + + // 4. CREATE 10 USERS FOR TIE-RANKING IN CURRENT MONTH + $currentMonth = Carbon::now()->format('Y-m'); + $currentDate = Carbon::now()->format('Y-m-d'); + $depts = config('constants.DEPARTMENTS'); + + for ($i = 1; $i <= 10; $i++) { + $tieMsnv = 8000 + $i; + User::create([ + 'msnv' => $tieMsnv, + 'name' => "Top 1 User {$i}", + 'mail' => "top1_user_{$i}@runsystem.net", + 'pass' => $password, + 'departments' => $depts[($i - 1) % count($depts)], + 'role' => config('constants.ROLE_MEMBER'), + 'status' => config('constants.STATUS_ACTIVE'), + 'card' => 100, + 'flag_send' => config('constants.FLAG_SEND_ENABLED'), + 'first_login' => config('constants.FIRST_LOGIN_FALSE'), + ]); + + $senderMsnv = 8000 + $i; + $receiverMsnv = 8000 + ($i % 10 + 1); + + // Sender record + Administration::create([ + 'msnv' => $senderMsnv, + 'received' => 0, + 'sender' => null, + 'sent' => 100, + 'receiver' => $receiverMsnv, + 'date' => $currentDate, + ]); + + // Receiver record + Administration::create([ + 'msnv' => $receiverMsnv, + 'received' => 100, + 'sender' => $senderMsnv, + 'sent' => 0, + 'receiver' => null, + 'date' => $currentDate, + ]); + } } } diff --git a/refactor.php b/refactor.php index 4c7fd92..9a0d913 100644 --- a/refactor.php +++ b/refactor.php @@ -29,79 +29,79 @@ file_put_contents('app/Models/Administration.php', $content); // 3. Update app/Http/Middleware/AdminMiddleware.php $content = file_get_contents('app/Http/Middleware/AdminMiddleware.php'); $content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content); -$content = str_replace('Auth::user()->role == 1', 'Auth::user()->role == User::ROLE_ADMIN', $content); -$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == User::STATUS_ACTIVE', $content); +$content = str_replace('Auth::user()->role == 1', 'Auth::user()->role == config('constants.ROLE_ADMIN')', $content); +$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == config('constants.STATUS_ACTIVE')', $content); file_put_contents('app/Http/Middleware/AdminMiddleware.php', $content); // 4. Update app/Http/Middleware/MemberMiddleware.php $content = file_get_contents('app/Http/Middleware/MemberMiddleware.php'); $content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content); -$content = str_replace('Auth::user()->role == 0', 'Auth::user()->role == User::ROLE_MEMBER', $content); -$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == User::STATUS_ACTIVE', $content); +$content = str_replace('Auth::user()->role == 0', 'Auth::user()->role == config('constants.ROLE_MEMBER')', $content); +$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == config('constants.STATUS_ACTIVE')', $content); file_put_contents('app/Http/Middleware/MemberMiddleware.php', $content); // 5. Update app/Http/Middleware/ForceChangePasswordMiddleware.php $content = file_get_contents('app/Http/Middleware/ForceChangePasswordMiddleware.php'); $content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content); -$content = str_replace('Auth::user()->first_login == 1', 'Auth::user()->first_login == User::FIRST_LOGIN_TRUE', $content); +$content = str_replace('Auth::user()->first_login == 1', 'Auth::user()->first_login == config('constants.FIRST_LOGIN_TRUE')', $content); file_put_contents('app/Http/Middleware/ForceChangePasswordMiddleware.php', $content); // 6. Update app/Http/Controllers/AuthController.php $content = file_get_contents('app/Http/Controllers/AuthController.php'); $content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content); -$content = str_replace('$user->status != 1', '$user->status != User::STATUS_ACTIVE', $content); -$content = str_replace('$user->first_login == 1', '$user->first_login == User::FIRST_LOGIN_TRUE', $content); -$content = str_replace('$user->role == 1', '$user->role == User::ROLE_ADMIN', $content); +$content = str_replace('$user->status != 1', '$user->status != config('constants.STATUS_ACTIVE')', $content); +$content = str_replace('$user->first_login == 1', '$user->first_login == config('constants.FIRST_LOGIN_TRUE')', $content); +$content = str_replace('$user->role == 1', '$user->role == config('constants.ROLE_ADMIN')', $content); file_put_contents('app/Http/Controllers/AuthController.php', $content); // 7. Update app/Http/Controllers/AdminController.php $content = file_get_contents('app/Http/Controllers/AdminController.php'); -$content = str_replace("where('status', 1)", "where('status', User::STATUS_ACTIVE)", $content); -$content = str_replace("'role' => 'required|in:0,1'", "'role' => 'required|in:' . User::ROLE_MEMBER . ',' . User::ROLE_ADMIN", $content); -$content = str_replace("'status' => 1", "'status' => User::STATUS_ACTIVE", $content); -$content = str_replace("'flag_send' => 0", "'flag_send' => User::FLAG_SEND_DISABLED", $content); -$content = str_replace("'first_login' => 1", "'first_login' => User::FIRST_LOGIN_TRUE", $content); -$content = str_replace("\$request->has('flag_send') ? 1 : 0", "\$request->has('flag_send') ? User::FLAG_SEND_ENABLED : User::FLAG_SEND_DISABLED", $content); -$content = str_replace("\$user->status = 0", "\$user->status = User::STATUS_INACTIVE", $content); +$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content); +$content = str_replace("'role' => 'required|in:0,1'", "'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN')", $content); +$content = str_replace("'status' => 1", "'status' => config('constants.STATUS_ACTIVE')", $content); +$content = str_replace("'flag_send' => 0", "'flag_send' => config('constants.FLAG_SEND_DISABLED')", $content); +$content = str_replace("'first_login' => 1", "'first_login' => config('constants.FIRST_LOGIN_TRUE')", $content); +$content = str_replace("\$request->has('flag_send') ? 1 : 0", "\$request->has('flag_send') ? config('constants.FLAG_SEND_ENABLED') : config('constants.FLAG_SEND_DISABLED')", $content); +$content = str_replace("\$user->status = 0", "\$user->status = config('constants.STATUS_INACTIVE')", $content); file_put_contents('app/Http/Controllers/AdminController.php', $content); // 8. Update app/Http/Controllers/UserController.php $content = file_get_contents('app/Http/Controllers/UserController.php'); -$content = str_replace("where('status', 1)", "where('status', User::STATUS_ACTIVE)", $content); +$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content); $content = str_replace("max:5'", "max:' . Administration::MAX_SEND_CARD_PER_MONTH", $content); -$content = str_replace("\$sender->flag_send == 0", "\$sender->flag_send == User::FLAG_SEND_DISABLED", $content); +$content = str_replace("\$sender->flag_send == 0", "\$sender->flag_send == config('constants.FLAG_SEND_DISABLED')", $content); $content = str_replace("> 5)", "> Administration::MAX_SEND_CARD_PER_MONTH)", $content); $content = str_replace("tối đa 5 card", 'tối đa " . Administration::MAX_SEND_CARD_PER_MONTH . " card', $content); -$content = str_replace("\$user->first_login = 0", "\$user->first_login = User::FIRST_LOGIN_FALSE", $content); -$content = str_replace("\$user->role == 1", "\$user->role == User::ROLE_ADMIN", $content); +$content = str_replace("\$user->first_login = 0", "\$user->first_login = config('constants.FIRST_LOGIN_FALSE')", $content); +$content = str_replace("\$user->role == 1", "\$user->role == config('constants.ROLE_ADMIN')", $content); file_put_contents('app/Http/Controllers/UserController.php', $content); // 9. Update app/Console/Commands/ResetCardsCommand.php $content = file_get_contents('app/Console/Commands/ResetCardsCommand.php'); -$content = str_replace("where('status', 1)", "where('status', User::STATUS_ACTIVE)", $content); +$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content); file_put_contents('app/Console/Commands/ResetCardsCommand.php', $content); // 10. Update resources/views/layouts/app.blade.php $content = file_get_contents('resources/views/layouts/app.blade.php'); -$content = str_replace("Auth::user()->role == 1", "Auth::user()->role == \App\Models\User::ROLE_ADMIN", $content); +$content = str_replace("Auth::user()->role == 1", "Auth::user()->role == config('constants.ROLE_ADMIN')", $content); file_put_contents('resources/views/layouts/app.blade.php', $content); // 11. Update resources/views/admin/users/create.blade.php $content = file_get_contents('resources/views/admin/users/create.blade.php'); -$content = str_replace('value="0"', 'value="{{ \App\Models\User::ROLE_MEMBER }}"', $content); -$content = str_replace('value="1"', 'value="{{ \App\Models\User::ROLE_ADMIN }}"', $content); -$content = str_replace("old('role') == '0'", "old('role') == \App\Models\User::ROLE_MEMBER", $content); -$content = str_replace("old('role') == '1'", "old('role') == \App\Models\User::ROLE_ADMIN", $content); +$content = str_replace('value="0"', 'value="{{ config('constants.ROLE_MEMBER') }}"', $content); +$content = str_replace('value="1"', 'value="{{ config('constants.ROLE_ADMIN') }}"', $content); +$content = str_replace("old('role') == '0'", "old('role') == config('constants.ROLE_MEMBER')", $content); +$content = str_replace("old('role') == '1'", "old('role') == config('constants.ROLE_ADMIN')", $content); file_put_contents('resources/views/admin/users/create.blade.php', $content); // 12. Update resources/views/admin/users/edit.blade.php $content = file_get_contents('resources/views/admin/users/edit.blade.php'); -$content = str_replace("\$user->role == 1", "\$user->role == \App\Models\User::ROLE_ADMIN", $content); +$content = str_replace("\$user->role == 1", "\$user->role == config('constants.ROLE_ADMIN')", $content); file_put_contents('resources/views/admin/users/edit.blade.php', $content); // 13. Update resources/views/user/change_password.blade.php $content = file_get_contents('resources/views/user/change_password.blade.php'); -$content = str_replace("Auth::user()->first_login == 1", "Auth::user()->first_login == \App\Models\User::FIRST_LOGIN_TRUE", $content); +$content = str_replace("Auth::user()->first_login == 1", "Auth::user()->first_login == config('constants.FIRST_LOGIN_TRUE')", $content); file_put_contents('resources/views/user/change_password.blade.php', $content); echo "Refactored successfully!"; diff --git a/resources/views/admin/users/partials/stats.blade.php b/resources/views/admin/users/partials/stats.blade.php index 708b74b..1a298c2 100644 --- a/resources/views/admin/users/partials/stats.blade.php +++ b/resources/views/admin/users/partials/stats.blade.php @@ -1,45 +1,69 @@ -
| Thao tác | MSNV | Nhân viên | +Phòng ban | Đã Nhận | Đã Gửi | Số dư thẻ | Quyền gửi | +Vai trò | Trạng thái | -Thao tác | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| + Chỉnh sửa + | {{ $u->msnv }} |
@@ -31,6 +36,20 @@
{{ $u->mail }}
|
+
+ @php
+ $deptVal = $u->departments;
+ if (is_numeric($deptVal)) {
+ $idx = ((int)$deptVal) - 1;
+ $deptName = config('constants.DEPARTMENTS')[$idx] ?? 'Team';
+ } else {
+ $deptName = $deptVal ?? 'Team';
+ }
+ @endphp
+
+ {{ $deptName }}
+
+ |
+{{ $u->total_received }} | {{ $u->total_sent }} | {{ $u->card }} | @@ -42,19 +61,23 @@ @endif- @if($u->status == \App\Models\User::STATUS_ACTIVE) + @if($u->role == config('constants.ROLE_ADMIN')) + Admin + @else + Member + @endif + | ++ @if($u->status == config('constants.STATUS_ACTIVE')) Đang làm việc @else Đã nghỉ việc @endif | -- Quản lý - | ||||||||
| Chưa có user nào đang hoạt động. | +Chưa có user nào đang hoạt động. | ||||||||||||||||
| Ngày cấp | +Người cấp (Admin) | +Số lượng thẻ | +Hành động | +
|---|---|---|---|
| {{ Carbon\Carbon::parse($addCard->date)->format('d/m/Y') }} | ++ @if($addCard->sellerUser) + {{ $addCard->sellerUser->name }} (MSNV: {{ $addCard->seller }}) + @else + MSNV: {{ $addCard->seller }} + @endif + | ++{{ $addCard->num_card }} | ++ @if($isCurrentMonth) + + @else + Tháng trước + @endif + | +
| + Chưa có lịch sử cấp phát thẻ nào. + | +|||