diff --git a/app/Livewire/Datatable/Studio/Loyalty/VouchersTable.php b/app/Livewire/Datatable/Studio/Loyalty/VouchersTable.php index b78612e..1bd0450 100644 --- a/app/Livewire/Datatable/Studio/Loyalty/VouchersTable.php +++ b/app/Livewire/Datatable/Studio/Loyalty/VouchersTable.php @@ -56,13 +56,16 @@ public function columns(): array ->searchable() ->sortable(), + Column::make('Sisa Voucher', 'available_count') + ->format(fn ($value) => $value ? currency($value) : 'Tidak Terbatas') + ->searchable() + ->sortable(), + Column::make('Batas Per Pembali', 'limit_per_user') ->format(fn ($value) => $value ? $value : 'Tidak Terbatas') ->searchable() ->sortable(), - Column::make('Sisa Voucher')->label(fn ($row) => $row->quota ? $row->quota - $row->orders()->count() : 'Tidak Terbatas..'), - ArrayColumn::make('Outlet') ->data( fn ($value, $row) => $row->outlets->map(fn ($outlet) => [ @@ -127,6 +130,6 @@ public function columns(): array public function builder(): Builder { - return Voucher::select('id', 'name', 'code', 'min_purchase', 'discount_amount', 'max_discount', 'quota', 'limit_per_user', 'start_date', 'end_date', 'type', 'created_at')->with('outlets'); + return Voucher::select('id', 'name', 'code', 'min_purchase', 'discount_amount', 'max_discount', 'quota', 'available_count', 'limit_per_user', 'start_date', 'end_date', 'type', 'created_at')->with('outlets'); } } diff --git a/app/Livewire/Forms/Studio/Loyalty/VoucherForm.php b/app/Livewire/Forms/Studio/Loyalty/VoucherForm.php index 88f45bd..8a8a67f 100644 --- a/app/Livewire/Forms/Studio/Loyalty/VoucherForm.php +++ b/app/Livewire/Forms/Studio/Loyalty/VoucherForm.php @@ -149,6 +149,7 @@ private function prepareSavedData() 'min_purchase' => replaceCurrency($this->min_purchase), 'max_discount' => $this->type == VoucherType::PERCENTAGE->value ? replaceCurrency($this->max_discount) : null, 'quota' => $this->quota, + 'available_count' => $this->quota, 'limit_per_user' => $this->limit_per_user, 'terms_conditions' => $this->terms_conditions, 'start_date' => $this->start_date, diff --git a/app/Models/Voucher.php b/app/Models/Voucher.php index a3bd40a..20a5970 100644 --- a/app/Models/Voucher.php +++ b/app/Models/Voucher.php @@ -3,6 +3,8 @@ namespace App\Models; use App\Enums\VoucherType; +use Illuminate\Database\Eloquent\Attributes\Scope; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -24,10 +26,21 @@ protected function casts(): array 'min_purchase' => 'int', 'max_discount' => 'int', 'quota' => 'int', + 'available_count' => 'int', 'limit_per_user' => 'int', ]; } + #[Scope] + public function active(Builder $query): void + { + $query->whereDate('start_date', '<=', now()) + ->where(function ($q) { + $q->whereNull('end_date') + ->orWhereDate('end_date', '>=', now()); + }); + } + public function outlets(): BelongsToMany { return $this->belongsToMany(Outlet::class); diff --git a/database/migrations/2025_09_25_125559_create_vouchers_table.php b/database/migrations/2025_09_25_125559_create_vouchers_table.php index bbb80e1..714eeee 100644 --- a/database/migrations/2025_09_25_125559_create_vouchers_table.php +++ b/database/migrations/2025_09_25_125559_create_vouchers_table.php @@ -22,6 +22,7 @@ public function up(): void $table->unsignedInteger('min_purchase')->nullable(); $table->unsignedInteger('max_discount')->nullable(); $table->unsignedInteger('quota')->nullable(); + $table->unsignedInteger('available_count')->nullable(); $table->unsignedInteger('limit_per_user')->nullable(); $table->date('start_date'); $table->date('end_date')->nullable(); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d6947f4..95a75f4 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -16,7 +16,7 @@ public function run(): void VoucherSeeder::class, CategorySeeder::class, BrandSeeder::class, - CustomerSeeder::class, + // CustomerSeeder::class, PerfumeSeeder::class, ProductSeeder::class, BottleSeeder::class, diff --git a/database/seeders/VoucherSeeder.php b/database/seeders/VoucherSeeder.php index 016ed7b..16db2fe 100644 --- a/database/seeders/VoucherSeeder.php +++ b/database/seeders/VoucherSeeder.php @@ -20,6 +20,7 @@ public function run(): void 'min_purchase' => 50000, 'max_discount' => 20000, 'quota' => 100, + 'available_count' => 100, 'limit_per_user' => 2, 'start_date' => now()->subDays(5), 'end_date' => now()->addMonth(), @@ -32,6 +33,7 @@ public function run(): void 'min_purchase' => 100000, 'max_discount' => 50000, 'quota' => 50, + 'available_count' => 50, 'limit_per_user' => 1, 'start_date' => now(), 'end_date' => now()->addDays(30), @@ -44,6 +46,7 @@ public function run(): void 'min_purchase' => 100000, 'max_discount' => null, 'quota' => 200, + 'available_count' => 200, 'limit_per_user' => 3, 'start_date' => now(), 'end_date' => now()->addDays(15), @@ -56,6 +59,7 @@ public function run(): void 'min_purchase' => 200000, 'max_discount' => null, 'quota' => 80, + 'available_count' => 80, 'limit_per_user' => 1, 'start_date' => now()->addDays(3), 'end_date' => now()->addDays(60), @@ -68,6 +72,7 @@ public function run(): void 'min_purchase' => 50000, 'max_discount' => 15000, 'quota' => 500, + 'available_count' => 500, 'limit_per_user' => 5, 'start_date' => now(), 'end_date' => now()->addDays(90), @@ -80,6 +85,7 @@ public function run(): void 'min_purchase' => 75000, 'max_discount' => 40000, 'quota' => 30, + 'available_count' => 30, 'limit_per_user' => 1, 'start_date' => now()->subDay(), 'end_date' => now()->addDay(), @@ -92,6 +98,7 @@ public function run(): void 'min_purchase' => 50000, 'max_discount' => 25000, 'quota' => null, + 'available_count' => null, 'limit_per_user' => null, 'start_date' => now(), 'end_date' => null,