startOfMonth(); $endOfMonth = Carbon::parse($selectedMonth)->endOfMonth(); $allActiveUsers = User::where('status', User::STATUS_ACTIVE)->get()->map(function ($user) use ($startOfMonth, $endOfMonth) { $user->total_received = Administration::where('receiver', $user->msnv) ->whereBetween('date', [$startOfMonth, $endOfMonth]) ->sum('received'); $user->total_sent = Administration::where('sender', $user->msnv) ->whereBetween('date', [$startOfMonth, $endOfMonth]) ->sum('sent'); return $user; }); $topReceivedUser = $allActiveUsers->sortByDesc('total_received')->first(); $topSentUser = $allActiveUsers->sortByDesc('total_sent')->first(); $users = User::where('status', User::STATUS_ACTIVE)->paginate(10)->withQueryString(); $users->getCollection()->transform(function ($user) use ($startOfMonth, $endOfMonth) { $user->total_received = Administration::where('receiver', $user->msnv) ->whereBetween('date', [$startOfMonth, $endOfMonth]) ->sum('received'); $user->total_sent = Administration::where('sender', $user->msnv) ->whereBetween('date', [$startOfMonth, $endOfMonth]) ->sum('sent'); return $user; }); return compact('users', 'topReceivedUser', 'topSentUser'); } public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator { $startOfMonth = Carbon::parse($selectedMonth)->startOfMonth(); $endOfMonth = Carbon::parse($selectedMonth)->endOfMonth(); return Administration::where(function ($q) use ($msnv) { $q->where('sender', $msnv)->orWhere('receiver', $msnv); }) ->whereBetween('date', [$startOfMonth, $endOfMonth]) ->orderBy('date', 'desc') ->paginate(10) ->withQueryString(); } public function createUser(array $data): void { User::create([ 'msnv' => $data['msnv'], 'mail' => $data['mail'], 'pass' => Hash::make($data['password']), 'role' => $data['role'], 'status' => User::STATUS_ACTIVE, 'card' => 0, 'flag_send' => User::FLAG_SEND_DISABLED, 'first_login' => User::FIRST_LOGIN_TRUE, ]); } public function updateUser(string $msnv, array $data): void { $user = User::where('msnv', $msnv)->firstOrFail(); DB::transaction(function () use ($data, $user) { if (!empty($data['num_card'])) { AddCard::create([ 'buyer' => $user->msnv, 'num_card' => $data['num_card'], '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->save(); }); } public function deactivateUser(string $msnv): void { $user = User::where('msnv', $msnv)->firstOrFail(); $user->status = User::STATUS_INACTIVE; $user->save(); } public function resetAllCards(): void { User::where('status', User::STATUS_ACTIVE)->update(['card' => 0]); } }