From a90cd91ee7a841e176b951d53e0a876af167554d Mon Sep 17 00:00:00 2001 From: Tin Date: Mon, 16 Sep 2024 23:39:46 +0200 Subject: [PATCH] add discount function and search --- app/Filament/Resources/BadgeResource.php | 14 ++++++-------- app/Http/Controllers/BadgeController.php | 2 +- app/Observers/BadgeObserver.php | 13 ++++++++++++- app/Policies/BadgePolicy.php | 9 +++++---- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/Filament/Resources/BadgeResource.php b/app/Filament/Resources/BadgeResource.php index 09ed962..52ef087 100644 --- a/app/Filament/Resources/BadgeResource.php +++ b/app/Filament/Resources/BadgeResource.php @@ -61,10 +61,16 @@ public static function table(Table $table): Table { return $table ->columns([ + Tables\Columns\TextColumn::make('custom_id') + ->sortable() + ->searchable() + ->label('Fursuit'), Tables\Columns\TextColumn::make('fursuit.user.attendee_id') ->sortable() + ->searchable() ->label('Fursuit'), Tables\Columns\TextColumn::make('fursuit.name') + ->searchable() ->label('Fursuit'), Tables\Columns\TextColumn::make('status')->badge()->colors([ Pending::$name => 'default', @@ -77,14 +83,6 @@ public static function table(Table $table): Table Tables\Filters\SelectFilter::make('status') ->options(BadgeStatusState::getStateMapping()->keys()->mapWithKeys(fn($key) => [ucfirst($key) => $key])) ->label('Badge Status'), - Tables\Filters\SelectFilter::make('fursuit_status') - ->options(FursuitStatusState::getStateMapping()->keys()->mapWithKeys(fn($key) => [$key => ucfirst($key)])) - ->query(function (Builder $query, array $data) { - $query->when($data, fn($query, $data) => $query->whereHas('fursuit', function (Builder $query) use ($data) { - $query->where('status', $data); - })); - }) - ->label('Fursuit Status'), // Duplex Bool Filter Tables\Filters\TernaryFilter::make('dual_side_print') ->label('Double Sided'), diff --git a/app/Http/Controllers/BadgeController.php b/app/Http/Controllers/BadgeController.php index 62aa448..aafdff7 100644 --- a/app/Http/Controllers/BadgeController.php +++ b/app/Http/Controllers/BadgeController.php @@ -178,7 +178,7 @@ public function update(BadgeUpdateRequest $request, Badge $badge) $badge->total = round($total); $badge->subtotal = round($total / 1.19); $badge->tax = round($badge->total - $badge->subtotal); - $badge->save(); + $badge->saveQuietly(); // Difference needs to be paid if ($previousTotal !== $total) { $request->user()->forcePay($badge); diff --git a/app/Observers/BadgeObserver.php b/app/Observers/BadgeObserver.php index 6d176a0..f10f526 100644 --- a/app/Observers/BadgeObserver.php +++ b/app/Observers/BadgeObserver.php @@ -3,6 +3,8 @@ namespace App\Observers; use App\Models\Badge\Badge; +use App\Models\Fursuit\Fursuit; +use Illuminate\Routing\Route; class BadgeObserver { @@ -12,7 +14,16 @@ public function updated(Badge $badge): void if ($badge->isDirty('total')) { $badge->subtotal = round($badge->total / 1.19,); $badge->tax = round($badge->total - $badge->subtotal); - $badge->save(); + + $user = $badge->fursuit->user; + $originalTotal = $badge->getOriginal(); + $newTotal = $badge->total; + $badge->total = $originalTotal; + $user->refund($badge); + $badge->total = $newTotal; + $user->forcePay($badge); + + $badge->saveQuietly(); } } } diff --git a/app/Policies/BadgePolicy.php b/app/Policies/BadgePolicy.php index caf5721..770e914 100644 --- a/app/Policies/BadgePolicy.php +++ b/app/Policies/BadgePolicy.php @@ -49,14 +49,15 @@ public function create(User $user): bool public function update(User $user, Badge $badge): bool { + // Admin can override + if ($user->is_admin && request()->routeIs('filament.*', 'livewire.*')) { + return true; + } + // Copies cannot be edited if ($badge->extra_copy_of !== null) { return false; } - // Admin can override - if ($user->is_admin && request()->routeIs('filament.*')) { - return true; - } $event = \App\Models\Event::getActiveEvent(); return $this->delete($user, $badge);