feat(voucher): menambahkan kolom baru untuk menghitung ketersediaan voucher dan menerapkan implementasi logikanya

This commit is contained in:
Yoga Pangestu 2025-11-07 14:37:01 +07:00
parent 41037a2f75
commit c6e295f788
6 changed files with 29 additions and 4 deletions

View File

@ -56,13 +56,16 @@ public function columns(): array
->searchable() ->searchable()
->sortable(), ->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') Column::make('Batas Per Pembali', 'limit_per_user')
->format(fn ($value) => $value ? $value : 'Tidak Terbatas') ->format(fn ($value) => $value ? $value : 'Tidak Terbatas')
->searchable() ->searchable()
->sortable(), ->sortable(),
Column::make('Sisa Voucher')->label(fn ($row) => $row->quota ? $row->quota - $row->orders()->count() : 'Tidak Terbatas..'),
ArrayColumn::make('Outlet') ArrayColumn::make('Outlet')
->data( ->data(
fn ($value, $row) => $row->outlets->map(fn ($outlet) => [ fn ($value, $row) => $row->outlets->map(fn ($outlet) => [
@ -127,6 +130,6 @@ public function columns(): array
public function builder(): Builder 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');
} }
} }

View File

@ -149,6 +149,7 @@ private function prepareSavedData()
'min_purchase' => replaceCurrency($this->min_purchase), 'min_purchase' => replaceCurrency($this->min_purchase),
'max_discount' => $this->type == VoucherType::PERCENTAGE->value ? replaceCurrency($this->max_discount) : null, 'max_discount' => $this->type == VoucherType::PERCENTAGE->value ? replaceCurrency($this->max_discount) : null,
'quota' => $this->quota, 'quota' => $this->quota,
'available_count' => $this->quota,
'limit_per_user' => $this->limit_per_user, 'limit_per_user' => $this->limit_per_user,
'terms_conditions' => $this->terms_conditions, 'terms_conditions' => $this->terms_conditions,
'start_date' => $this->start_date, 'start_date' => $this->start_date,

View File

@ -3,6 +3,8 @@
namespace App\Models; namespace App\Models;
use App\Enums\VoucherType; 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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@ -24,10 +26,21 @@ protected function casts(): array
'min_purchase' => 'int', 'min_purchase' => 'int',
'max_discount' => 'int', 'max_discount' => 'int',
'quota' => 'int', 'quota' => 'int',
'available_count' => 'int',
'limit_per_user' => '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 public function outlets(): BelongsToMany
{ {
return $this->belongsToMany(Outlet::class); return $this->belongsToMany(Outlet::class);

View File

@ -22,6 +22,7 @@ public function up(): void
$table->unsignedInteger('min_purchase')->nullable(); $table->unsignedInteger('min_purchase')->nullable();
$table->unsignedInteger('max_discount')->nullable(); $table->unsignedInteger('max_discount')->nullable();
$table->unsignedInteger('quota')->nullable(); $table->unsignedInteger('quota')->nullable();
$table->unsignedInteger('available_count')->nullable();
$table->unsignedInteger('limit_per_user')->nullable(); $table->unsignedInteger('limit_per_user')->nullable();
$table->date('start_date'); $table->date('start_date');
$table->date('end_date')->nullable(); $table->date('end_date')->nullable();

View File

@ -16,7 +16,7 @@ public function run(): void
VoucherSeeder::class, VoucherSeeder::class,
CategorySeeder::class, CategorySeeder::class,
BrandSeeder::class, BrandSeeder::class,
CustomerSeeder::class, // CustomerSeeder::class,
PerfumeSeeder::class, PerfumeSeeder::class,
ProductSeeder::class, ProductSeeder::class,
BottleSeeder::class, BottleSeeder::class,

View File

@ -20,6 +20,7 @@ public function run(): void
'min_purchase' => 50000, 'min_purchase' => 50000,
'max_discount' => 20000, 'max_discount' => 20000,
'quota' => 100, 'quota' => 100,
'available_count' => 100,
'limit_per_user' => 2, 'limit_per_user' => 2,
'start_date' => now()->subDays(5), 'start_date' => now()->subDays(5),
'end_date' => now()->addMonth(), 'end_date' => now()->addMonth(),
@ -32,6 +33,7 @@ public function run(): void
'min_purchase' => 100000, 'min_purchase' => 100000,
'max_discount' => 50000, 'max_discount' => 50000,
'quota' => 50, 'quota' => 50,
'available_count' => 50,
'limit_per_user' => 1, 'limit_per_user' => 1,
'start_date' => now(), 'start_date' => now(),
'end_date' => now()->addDays(30), 'end_date' => now()->addDays(30),
@ -44,6 +46,7 @@ public function run(): void
'min_purchase' => 100000, 'min_purchase' => 100000,
'max_discount' => null, 'max_discount' => null,
'quota' => 200, 'quota' => 200,
'available_count' => 200,
'limit_per_user' => 3, 'limit_per_user' => 3,
'start_date' => now(), 'start_date' => now(),
'end_date' => now()->addDays(15), 'end_date' => now()->addDays(15),
@ -56,6 +59,7 @@ public function run(): void
'min_purchase' => 200000, 'min_purchase' => 200000,
'max_discount' => null, 'max_discount' => null,
'quota' => 80, 'quota' => 80,
'available_count' => 80,
'limit_per_user' => 1, 'limit_per_user' => 1,
'start_date' => now()->addDays(3), 'start_date' => now()->addDays(3),
'end_date' => now()->addDays(60), 'end_date' => now()->addDays(60),
@ -68,6 +72,7 @@ public function run(): void
'min_purchase' => 50000, 'min_purchase' => 50000,
'max_discount' => 15000, 'max_discount' => 15000,
'quota' => 500, 'quota' => 500,
'available_count' => 500,
'limit_per_user' => 5, 'limit_per_user' => 5,
'start_date' => now(), 'start_date' => now(),
'end_date' => now()->addDays(90), 'end_date' => now()->addDays(90),
@ -80,6 +85,7 @@ public function run(): void
'min_purchase' => 75000, 'min_purchase' => 75000,
'max_discount' => 40000, 'max_discount' => 40000,
'quota' => 30, 'quota' => 30,
'available_count' => 30,
'limit_per_user' => 1, 'limit_per_user' => 1,
'start_date' => now()->subDay(), 'start_date' => now()->subDay(),
'end_date' => now()->addDay(), 'end_date' => now()->addDay(),
@ -92,6 +98,7 @@ public function run(): void
'min_purchase' => 50000, 'min_purchase' => 50000,
'max_discount' => 25000, 'max_discount' => 25000,
'quota' => null, 'quota' => null,
'available_count' => null,
'limit_per_user' => null, 'limit_per_user' => null,
'start_date' => now(), 'start_date' => now(),
'end_date' => null, 'end_date' => null,