feat(order): memperbaiki logika untuk menggunakan voucher

- menambahkan kolom ordered_at
- nullable kan kolom quantity di user_voucher
This commit is contained in:
Yoga Pangestu 2025-11-11 00:40:33 +07:00
parent 89fffb2cb1
commit 9d2bbbd2c8
4 changed files with 52 additions and 43 deletions

View File

@ -29,23 +29,30 @@ public function members()
->toArray();
}
public function updatedFormMemberId(?string $member_id = null)
public function updatedFormMemberId(?string $memberId = null)
{
$customer = Customer::with('user')->find($member_id);
$customer = Customer::with('user')->find($memberId);
if (! $customer) {
return;
}
$memberId = $customer->user?->id;
$outletId = $this->form->outlet_id;
$this->loadVouchers($customer->user?->id, $this->form->outlet_id);
}
$this->vouchers = Voucher::whereHas('users', function ($q) use ($memberId) {
$q->where('user_id', $memberId);
})
->whereHas('outlets', function ($q) use ($outletId) {
$q->where('outlet_id', $outletId);
})
public function updatedFormOutletId(?string $outletId = null)
{
$customer = Customer::with('user')->find($this->form->member_id);
if (! $customer) {
return;
}
$this->loadVouchers($customer->user?->id, $outletId);
}
private function loadVouchers(?string $memberId, ?string $outletId)
{
$this->vouchers = Voucher::whereHas('users', fn ($q) => $q->where('user_id', $memberId))
->whereHas('outlets', fn ($q) => $q->where('outlet_id', $outletId))
->active()
->select('id', 'name', 'type', 'discount_amount', 'max_discount')
->orderByRaw("

View File

@ -26,6 +26,7 @@ public function up(): void
$table->unsignedInteger('total');
$table->enum('channel', [OrderChannel::values()])->default(OrderChannel::OUTLET)->comment(OrderChannel::comment());
$table->enum('status', [OrderStatus::values()])->default(OrderStatus::PENDING)->comment(OrderStatus::comment());
$table->dateTime('ordered_at');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes();

View File

@ -15,7 +15,7 @@ public function up(): void
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('voucher_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('quantity');
$table->unsignedInteger('quantity')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
});

View File

@ -272,42 +272,43 @@ class="text-xs text-gray-400">{{ currency($item->quantity, '') }}
</div>
</div>
</flux:card>
<flux:card class="space-y-6 p-6">
<flux:field>
<flux:label>Member</flux:label>
<flux:description>Silakan ketikkan nomor telepon.</flux:description>
<flux:select variant="combobox" placeholder="Pilih Member"
wire:model.live.debounce.500ms="form.member_id" clear>
<x-slot name="input">
<flux:select.input wire:model.live.debounce.250ms="searchMember" clearable/>
</x-slot>
@foreach ($this->members as $member)
<flux:select.option value="{{ $member['id'] }}" key="{{ $member['id'] }}">
{{ $member['name'] }} - {{ $member['phone_number'] }}
</flux:select.option>
@endforeach
</flux:select>
<flux:error name="form.member_id" />
</flux:field>
@if (!empty($vouchers))
@if ($this->form->outlet_id !== '' && $items->isNotEmpty())
<flux:card class="space-y-6 p-6">
<flux:field>
<flux:label>Voucher</flux:label>
<flux:description>Voucher diurutkan berdasarkan dengan diskon yang paling besar.
</flux:description>
<flux:select variant="listbox" placeholder="Pilih Voucher"
wire:model.live.debounce.500ms="form.voucher_id" searchable clearable>
@foreach ($vouchers as $key => $name)
<flux:select.option value="{{ $key }}" key="{{ $key }}">
{{ $name }}
<flux:label>Member</flux:label>
<flux:description>Silakan ketikkan nomor telepon.</flux:description>
<flux:select variant="combobox" placeholder="Pilih Member"
wire:model.live.debounce.500ms="form.member_id" clear>
<x-slot name="input">
<flux:select.input wire:model.live.debounce.250ms="searchMember" clearable />
</x-slot>
@foreach ($this->members as $member)
<flux:select.option value="{{ $member['id'] }}" key="{{ $member['id'] }}">
{{ $member['name'] }} - {{ $member['phone_number'] }}
</flux:select.option>
@endforeach
</flux:select>
<flux:error name="form.voucher_id" />
<flux:error name="form.member_id" />
</flux:field>
@endif
</flux:card>
@if (!empty($vouchers))
<flux:field>
<flux:label>Voucher</flux:label>
<flux:description>Voucher diurutkan berdasarkan dengan diskon yang paling besar.
</flux:description>
<flux:select variant="listbox" placeholder="Pilih Voucher"
wire:model.live.debounce.500ms="form.voucher_id" searchable clearable>
@foreach ($vouchers as $key => $name)
<flux:select.option value="{{ $key }}" key="{{ $key }}">
{{ $name }}
</flux:select.option>
@endforeach
</flux:select>
<flux:error name="form.voucher_id" />
</flux:field>
@endif
</flux:card>
@endif
</div>
</div>