select box

This commit is contained in:
antv
2026-07-13 16:28:44 +07:00
parent f0a25b840c
commit 47a8892623
24 changed files with 889 additions and 302 deletions
+85 -11
View File
@@ -16,6 +16,9 @@ use App\DTOs\UserFilterDto;
class AdminService implements AdminServiceInterface
{
public function __construct(
private readonly \App\Services\User\Contracts\UserServiceInterface $userService
) {}
public function getUserListWithStats(
string|UserFilterDto $monthOrFilters,
?string $search = null,
@@ -136,6 +139,19 @@ class AdminService implements AdminServiceInterface
: User::where('msnv', $userOrMsnv)->firstOrFail();
DB::transaction(function () use ($data, $user) {
// Update profile fields (name & avatar) via UserService
$this->userService->updateProfile($user, [
'name' => $data['name'] ?? null,
], request()->file('avatar'));
if (isset($data['mail'])) {
$user->mail = $data['mail'];
}
if (isset($data['departments'])) {
$user->departments = $data['departments'];
}
$newCard = $user->card;
if (isset($data['num_card'])) {
$newCard = $user->card + intval($data['num_card']);
@@ -147,12 +163,21 @@ class AdminService implements AdminServiceInterface
if ($newCard > $oldCard) {
$diff = $newCard - $oldCard;
AddCard::create([
'buyer' => $user->msnv,
'num_card' => $diff,
'seller' => Auth::user()->msnv,
'date' => Carbon::today(),
]);
$todayRecord = AddCard::where('buyer', $user->msnv)
->where('seller', Auth::user()->msnv)
->whereDate('date', Carbon::today())
->first();
if ($todayRecord) {
$todayRecord->num_card += $diff;
$todayRecord->save();
} else {
AddCard::create([
'buyer' => $user->msnv,
'num_card' => $diff,
'seller' => Auth::user()->msnv,
'date' => Carbon::today(),
]);
}
}
$user->card = $newCard;
@@ -173,14 +198,16 @@ class AdminService implements AdminServiceInterface
public function resetAllCards(): void
{
User::where('status', config('constants.STATUS_ACTIVE'))->update(['card' => 0]);
User::where('status', config('constants.STATUS_ACTIVE'))->update([
'card' => 0,
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
]);
}
public function updateAddCard(int $id, array $data): void
{
$addCard = AddCard::findOrFail($id);
$currentMonth = Carbon::now()->format('Y-m');
$recordMonth = Carbon::parse($addCard->date)->format('Y-m');
@@ -195,15 +222,25 @@ class AdminService implements AdminServiceInterface
$user = User::where('msnv', $addCard->buyer)->firstOrFail();
DB::transaction(function () use ($addCard, $user, $data) {
$startOfMonth = Carbon::now()->startOfMonth();
$endOfMonth = Carbon::now()->endOfMonth();
$totalSentThisMonth = Administration::where('msnv', $user->msnv)
->whereBetween('date', [$startOfMonth, $endOfMonth])
->sum('sent');
DB::transaction(function () use ($addCard, $user, $data, $totalSentThisMonth) {
$oldNumCard = intval($addCard->num_card);
$newNumCard = intval($data['num_card']);
$diff = $newNumCard - $oldNumCard;
if ($user->card + $diff < 0) {
$newCardBalance = $user->card + $diff;
if ($newCardBalance < 0) {
throw new \Exception('Số lượng card cập nhật không hợp lệ vì tổng số card của user không được nhỏ hơn 0.');
}
if ($newCardBalance < $totalSentThisMonth) {
throw new \Exception('Số lượng card cập nhật không hợp lệ vì tổng số card của user không được nhỏ hơn số card đã gửi trong tháng (' . $totalSentThisMonth . ' card).');
}
$user->card += $diff;
$user->save();
@@ -214,6 +251,43 @@ class AdminService implements AdminServiceInterface
});
}
public function destroyAddCard(int $id): void
{
$addCard = AddCard::findOrFail($id);
$currentMonth = Carbon::now()->format('Y-m');
$recordMonth = Carbon::parse($addCard->date)->format('Y-m');
if ($recordMonth !== $currentMonth) {
throw new \Exception('Không cho phép xóa dữ liệu card của các tháng trước.');
}
$user = User::where('msnv', $addCard->buyer)->firstOrFail();
$startOfMonth = Carbon::now()->startOfMonth();
$endOfMonth = Carbon::now()->endOfMonth();
$totalSentThisMonth = Administration::where('msnv', $user->msnv)
->whereBetween('date', [$startOfMonth, $endOfMonth])
->sum('sent');
DB::transaction(function () use ($addCard, $user, $totalSentThisMonth) {
$newCardBalance = $user->card - intval($addCard->num_card);
if ($newCardBalance < 0) {
throw new \Exception('Không thể xóa lịch sử cấp phát thẻ này vì tổng số card của user không được nhỏ hơn 0.');
}
if ($newCardBalance < $totalSentThisMonth) {
throw new \Exception('Không thể xóa lịch sử cấp phát thẻ này vì tổng số card của user không được nhỏ hơn số card đã gửi trong tháng (' . $totalSentThisMonth . ' card).');
}
$user->card = $newCardBalance;
$user->save();
$addCard->delete();
});
}
public function getUserByMsnv(string $msnv): User
{
return User::where('msnv', $msnv)->firstOrFail();
@@ -27,6 +27,8 @@ interface AdminServiceInterface
public function updateAddCard(int $id, array $data): void;
public function destroyAddCard(int $id): void;
public function getUserByMsnv(string $msnv): \App\Models\User;
public function getAddCardsHistory(string $buyerMsnv, ?string $selectedMonth = null): \Illuminate\Database\Eloquent\Collection;