diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index d69dade..d6d93f9 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -64,7 +64,7 @@ class AdminController extends Controller */ public function create(): View { - return view('admin.users.create'); + return view('admin.users.form', ['mode' => 'create']); } /** @@ -85,21 +85,29 @@ class AdminController extends Controller $user = $this->adminService->getUserByMsnv($msnv); $selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m')); $administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth); - $addCards = $this->adminService->getAddCardsHistory($user->msnv); + $addCards = $this->adminService->getAddCardsHistory($user->msnv, $selectedMonth); $admins = $this->adminService->getActiveAdmins(); - return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins')); + return view('admin.users.form', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'))->with('mode', 'edit'); } /** * Update the specified user in storage. */ - public function update(UpdateUserRequest $request, string $msnv): RedirectResponse + public function update(UpdateUserRequest $request, string $msnv) { $user = $this->adminService->getUserByMsnv($msnv); $this->adminService->updateUser($user, $request->validated()); + if ($request->expectsJson()) { + return response()->json([ + 'success' => true, + 'message' => __('messages.user_update_success'), + 'card_balance' => $user->fresh()->card, + ]); + } + return redirect()->back()->with('success', __('messages.user_update_success')); } @@ -116,12 +124,24 @@ class AdminController extends Controller /** * Update card allocation history record. */ - public function updateAddCard(UpdateAddCardRequest $request, int $id): RedirectResponse + public function updateAddCard(UpdateAddCardRequest $request, int $id) { try { $this->adminService->updateAddCard($id, $request->validated()); + if ($request->expectsJson()) { + return response()->json([ + 'success' => true, + 'message' => 'Cập nhật lịch sử cấp phát thẻ thành công.', + ]); + } return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.'); } catch (\Exception $e) { + if ($request->expectsJson()) { + return response()->json([ + 'success' => false, + 'message' => $e->getMessage(), + ], 422); + } return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } diff --git a/app/Services/Admin/AdminService.php b/app/Services/Admin/AdminService.php index 37c8ec3..c4715a4 100644 --- a/app/Services/Admin/AdminService.php +++ b/app/Services/Admin/AdminService.php @@ -219,11 +219,18 @@ class AdminService implements AdminServiceInterface return User::where('msnv', $msnv)->firstOrFail(); } - public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection + public function getAddCardsHistory(string $buyerMsnv, ?string $selectedMonth = null): \Illuminate\Database\Eloquent\Collection { - return AddCard::with('sellerUser') - ->where('buyer', $buyerMsnv) - ->orderBy('date', 'desc') + $query = AddCard::with('sellerUser') + ->where('buyer', $buyerMsnv); + + if ($selectedMonth) { + $startOfMonth = Carbon::parse($selectedMonth)->startOfMonth()->format('Y-m-d'); + $endOfMonth = Carbon::parse($selectedMonth)->endOfMonth()->format('Y-m-d'); + $query->whereBetween('date', [$startOfMonth, $endOfMonth]); + } + + return $query->orderBy('date', 'desc') ->orderBy('id', 'desc') ->get(); } diff --git a/app/Services/Admin/Contracts/AdminServiceInterface.php b/app/Services/Admin/Contracts/AdminServiceInterface.php index eb4e80c..898bc86 100644 --- a/app/Services/Admin/Contracts/AdminServiceInterface.php +++ b/app/Services/Admin/Contracts/AdminServiceInterface.php @@ -29,7 +29,7 @@ interface AdminServiceInterface public function getUserByMsnv(string $msnv): \App\Models\User; - public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection; + public function getAddCardsHistory(string $buyerMsnv, ?string $selectedMonth = null): \Illuminate\Database\Eloquent\Collection; public function getActiveAdmins(): \Illuminate\Database\Eloquent\Collection; } \ No newline at end of file diff --git a/resources/views/admin/users/create.blade.php b/resources/views/admin/users/create.blade.php deleted file mode 100644 index ef1ea9e..0000000 --- a/resources/views/admin/users/create.blade.php +++ /dev/null @@ -1,17 +0,0 @@ -@extends('layouts.app') -@section('title', 'Thêm User Mới') - -@section('content') -
- Tạo tài khoản nhân viên mới và thiết lập các thông tin cơ bản. -
-- Quản lý thông tin thẻ và theo dõi lịch sử nhận gửi của nhân viên. -
-+ {{ $mode === 'create' ? 'Tạo tài khoản nhân viên mới và thiết lập các thông tin cơ bản.' : 'Quản lý thông tin thẻ và theo dõi lịch sử nhận gửi của nhân viên.' }} +
+