parfum/app/Livewire/Home/Voucher.php

80 lines
2.0 KiB
PHP

<?php
namespace App\Livewire\Home;
use App\Models\Outlet;
use App\Models\Voucher as VoucherModel;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;
#[Layout('components.layouts.home', [
'title' => 'Voucher',
])]
class Voucher extends Component
{
use WithPagination;
public $outlets;
public string $search = '';
public ?string $outlet_id = null;
public ?string $sort = null;
public function mount()
{
$this->outlets = Outlet::query()->operational()->pluck('name', 'id');
}
public function updatedSearch()
{
$this->resetPage();
}
public function updatedOutletId()
{
$this->resetPage();
}
public function updatedSort()
{
$this->resetPage();
}
public function render()
{
$vouchers = VoucherModel::where(function ($query) {
$query->where('name', 'like', '%'.$this->search.'%')
->orWhere('code', 'like', '%'.$this->search.'%');
})
->when($this->outlet_id, function ($query) {
$query->whereHas('outlets', function ($q) {
$q->where('outlets.id', $this->outlet_id);
});
})
->when($this->sort, function ($query) {
match ($this->sort) {
'latest' => $query->orderBy('created_at', 'desc'),
'biggest' => $query->orderBy('discount_amount', 'desc'),
'ending-soon' => $query->orderByRaw('end_date IS NULL')
->orderBy('end_date', 'asc'),
};
})
->active()
->latest()
->paginate(3)
->through(function (VoucherModel $voucher) {
$voucher->min_purchase_formatted = currency($voucher->min_purchase, 'Rp');
$voucher->end_date = formatDate($voucher->end_date);
return $voucher;
});
return view('livewire.home.vouchers', [
'vouchers' => $vouchers,
]);
}
}