startOfMonth(); $endOfMonth = Carbon::parse($selectedMonth)->endOfMonth(); $usersQuery = User::select('user.*') ->where('status', User::STATUS_ACTIVE) ->addSelect([ 'total_received' => Administration::selectRaw('COALESCE(SUM(received), 0)') ->whereColumn('msnv', 'user.msnv') ->whereBetween('date', [$startOfMonth, $endOfMonth]), 'total_sent' => Administration::selectRaw('COALESCE(SUM(sent), 0)') ->whereColumn('msnv', 'user.msnv') ->whereBetween('date', [$startOfMonth, $endOfMonth]) ]); $topReceivedUser = (clone $usersQuery)->orderByDesc('total_received')->first(); $topSentUser = (clone $usersQuery)->orderByDesc('total_sent')->first(); if (!is_null($search) && trim($search) !== '') { $search = trim($search); $usersQuery->where(function ($q) use ($search) { $q->whereRaw('LOWER(msnv) LIKE ?', ['%' . strtolower($search) . '%']) ->orWhereRaw('LOWER(mail) LIKE ?', ['%' . strtolower($search) . '%']); }); } if (!is_null($flagSend) && $flagSend !== '') { $usersQuery->where('flag_send', intval($flagSend)); } $users = $usersQuery->paginate(10)->withQueryString(); 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('msnv', $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' => abs(crc32($user->msnv)) % 1000000, 'num_card' => $data['num_card'], 'seller' => abs(crc32(Auth::user()->msnv)) % 1000000, '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]); } }