113 lines
4.0 KiB
PHP
113 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\AddCard;
|
|
use App\Models\Administration;
|
|
use App\Models\User;
|
|
use App\Services\Admin\Contracts\AdminServiceInterface;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class AdminService implements AdminServiceInterface
|
|
{
|
|
public function getUserListWithStats(string $selectedMonth): 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');
|
|
|
|
$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' => 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]);
|
|
}
|
|
} |