feat: Add voucher visibility functionality and refactor quota/available count display in the voucher table.

This commit is contained in:
Yoga Pangestu 2025-12-18 14:47:15 +07:00
parent d25dd7b9c3
commit bc9e929a50
4 changed files with 46 additions and 17 deletions

View File

@ -41,55 +41,64 @@ public function columns(): array
->sortable(),
Column::make('Min. Belanja', 'min_purchase')
->format(fn($value) => formatCurrencyNumber($value, 'Rp'))
->format(fn ($value) => formatCurrencyNumber($value, 'Rp'))
->searchable()
->sortable(),
ArrayColumn::make('Tier')
->data(
fn($value, $row) => $row->tiers->pluck('name')->toArray() ?: ['Semua Tier']
fn ($value, $row) => $row->tiers->pluck('name')->toArray() ?: ['Semua Tier']
)
->outputFormat(fn($index, $value) => Blade::render('<span class="text-xs text-gray-400 border border-gray-400 rounded px-1">' . $value . '</span>'))
->outputFormat(fn ($index, $value) => Blade::render('<span class="text-xs text-gray-400 border border-gray-400 rounded px-1">'.$value.'</span>'))
->flexRow(['class' => 'gap-2 flex-wrap']),
Column::make('Jumlah Diskon', 'discount_amount')
->label(fn($row, $column) => formatDiscount($row->discount_amount, $row->type))
->label(fn ($row, $column) => formatDiscount($row->discount_amount, $row->type))
->searchable()
->sortable(),
Column::make('Maks. Diskon')
->label(fn($row, $column) => formatCurrencyNumber($row->max_discount, 'Rp'))
->label(fn ($row, $column) => formatCurrencyNumber($row->max_discount, 'Rp'))
->searchable()
->sortable(),
Column::make('Kuota', 'quota')
->format(fn($value) => $value ? formatCurrencyNumber($value) : 'Tidak Terbatas')
->searchable()
->sortable(),
Column::make('Kuota / Sisa Voucher')
->label(function ($record) {
$quota = $record->quota
? formatCurrencyNumber($record->quota)
: 'Tidak Terbatas';
Column::make('Sisa Voucher', 'available_count')
->format(fn($value) => $value ? formatCurrencyNumber($value) : 'Tidak Terbatas')
$available = $record->available_count
? formatCurrencyNumber($record->available_count)
: 'Tidak Terbatas';
return "{$quota} / {$available}";
})
->searchable()
->sortable(),
Column::make('Batas Per Pembali', 'limit_per_user')
->format(fn($value) => $value ? $value : 'Tidak Terbatas')
->format(fn ($value) => $value ? $value : 'Tidak Terbatas')
->searchable()
->sortable(),
ArrayColumn::make('Outlet')
->data(
fn($value, $row) => $row->outlets->map(fn($outlet) => [
fn ($value, $row) => $row->outlets->map(fn ($outlet) => [
'name' => $outlet->name,
'stock' => formatCurrencyNumber($outlet->pivot->stock, ''),
])->toArray()
)
->outputFormat(fn($index, $value) => Blade::render('
->outputFormat(fn ($index, $value) => Blade::render('
<div class="flex items-center space-x-2 bg-gray-50 border border-gray-200 rounded-lg px-2 py-1 text-xs">
<span class="font-medium text-gray-700">{{ $value["name"] }}</span>
</div>', ['value' => $value]))
->flexRow(['class' => 'gap-2 flex-wrap']),
Column::make('Visibilitas', 'is_show')
->label(fn ($row, $column) => Blade::render('<flux:badge color="'.$row->is_show->color().'">'.$row->is_show->label().'</flux:badge>'))
->html(),
Column::make('Tanggal')
->label(function ($row) {
$startDate = formatDateLocalized($row->start_date);
@ -141,11 +150,11 @@ public function columns(): array
public function builder(): Builder
{
return Voucher::select('vouchers.id', 'vouchers.name', 'code', 'tags', 'min_purchase', 'discount_amount', 'max_discount', 'quota', 'available_count', 'limit_per_user', 'start_date', 'end_date', 'type', 'vouchers.created_at')
return Voucher::select('vouchers.id', 'vouchers.name', 'code', 'tags', 'min_purchase', 'discount_amount', 'max_discount', 'quota', 'available_count', 'limit_per_user', 'start_date', 'end_date', 'type', 'is_show', 'vouchers.created_at')
->with(['outlets', 'tiers'])
->whereHas(
'outlets',
fn($q) => $q->whereIn('outlets.id', auth()->user()->outlets()->pluck('outlets.id'))
fn ($q) => $q->whereIn('outlets.id', auth()->user()->outlets()->pluck('outlets.id'))
);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Livewire\Forms\Studio\Loyalty;
use App\Enums\IsShow;
use App\Enums\VoucherType;
use App\Models\Voucher;
use App\Rules\UnsignedInteger;
@ -41,6 +42,8 @@ class VoucherForm extends Form
public array $outlet_ids = [];
public int $is_show = 1;
public function rules(): array
{
return [
@ -79,6 +82,7 @@ public function rules(): array
'tier_ids.*' => Rule::exists('tiers', 'id'),
'outlet_ids' => ['required', 'array', 'min:1'],
'outlet_ids.*' => Rule::exists('outlets', 'id'),
'is_show' => ['required', Rule::in(IsShow::values())],
];
}
@ -100,6 +104,7 @@ public function validationAttributes(): array
'tier_ids.*' => 'tier',
'outlet_ids' => 'outlet',
'outlet_ids.*' => 'outlet',
'is_show' => 'visibilitas',
];
}
@ -121,7 +126,8 @@ public function setVoucher(Voucher $voucher): void
$this->summary = $this->voucher->summary;
$this->start_date = $this->voucher->start_date;
$this->end_date = $this->voucher->end_date;
$this->is_show = $this->voucher->is_show->value;
$this->tier_ids = $this->voucher->tiers->pluck('id')->toArray();
$this->outlet_ids = $this->voucher->outlets->pluck('id')->toArray();
@ -178,6 +184,7 @@ private function prepareSavedData(): array
'summary' => $this->summary,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'is_show' => $this->is_show,
];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Enums\IsShow;
use App\Enums\VoucherType;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
@ -28,6 +29,7 @@ protected function casts(): array
'quota' => 'int',
'available_count' => 'int',
'limit_per_user' => 'int',
'is_show' => IsShow::class,
];
}

View File

@ -154,6 +154,17 @@
<flux:error name="form.tier_ids" />
</flux:field>
<flux:field>
<flux:label>Visibilitas <span class="text-red-500 ms-1">*</span></flux:label>
<flux:radio.group wire:model="form.is_show" variant="buttons" class="w-full *:flex-1">
@foreach (\App\Enums\IsShow::cases() as $item)
<flux:radio value="{{ $item->value }}">
{{ $item->label() }}
</flux:radio>
@endforeach
</flux:radio.group>
<flux:error name="form.is_show" />
</flux:field>
</flux:card>
</div>
</div>