reuse form
This commit is contained in:
@@ -16,11 +16,21 @@ class AdminController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
||||
$search = $request->input('search');
|
||||
$flagSend = $request->input('flag_send');
|
||||
|
||||
['users' => $users, 'topReceivedUser' => $topReceivedUser, 'topSentUser' => $topSentUser]
|
||||
= $this->adminService->getUserListWithStats($selectedMonth);
|
||||
= $this->adminService->getUserListWithStats($selectedMonth, $search, $flagSend);
|
||||
|
||||
return view('admin.users.index', compact('users', 'selectedMonth', 'topReceivedUser', 'topSentUser'));
|
||||
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(),
|
||||
'table' => view('admin.users.partials.table', compact('users'))->render(),
|
||||
]);
|
||||
}
|
||||
|
||||
return view('admin.users.index', compact('users', 'selectedMonth', 'topReceivedUser', 'topSentUser', 'search', 'flagSend'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
|
||||
@@ -14,39 +14,38 @@ use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class AdminService implements AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(string $selectedMonth): array
|
||||
public function getUserListWithStats(string $selectedMonth, ?string $search = null, ?string $flagSend = null): array
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->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');
|
||||
$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])
|
||||
]);
|
||||
|
||||
$user->total_sent = Administration::where('sender', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
$topReceivedUser = (clone $usersQuery)->orderByDesc('total_received')->first();
|
||||
$topSentUser = (clone $usersQuery)->orderByDesc('total_sent')->first();
|
||||
|
||||
return $user;
|
||||
});
|
||||
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) . '%']);
|
||||
});
|
||||
}
|
||||
|
||||
$topReceivedUser = $allActiveUsers->sortByDesc('total_received')->first();
|
||||
$topSentUser = $allActiveUsers->sortByDesc('total_sent')->first();
|
||||
if (!is_null($flagSend) && $flagSend !== '') {
|
||||
$usersQuery->where('flag_send', intval($flagSend));
|
||||
}
|
||||
|
||||
$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;
|
||||
});
|
||||
$users = $usersQuery->paginate(10)->withQueryString();
|
||||
|
||||
return compact('users', 'topReceivedUser', 'topSentUser');
|
||||
}
|
||||
@@ -56,9 +55,7 @@ class AdminService implements AdminServiceInterface
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
return Administration::where(function ($q) use ($msnv) {
|
||||
$q->where('sender', $msnv)->orWhere('receiver', $msnv);
|
||||
})
|
||||
return Administration::where('msnv', $msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->orderBy('date', 'desc')
|
||||
->paginate(10)
|
||||
|
||||
@@ -6,7 +6,7 @@ use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(string $selectedMonth): array;
|
||||
public function getUserListWithStats(string $selectedMonth, ?string $search = null, ?string $flagSend = null): array;
|
||||
|
||||
public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user