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(); ->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) { if (! $customer) {
return; return;
} }
$memberId = $customer->user?->id; $this->loadVouchers($customer->user?->id, $this->form->outlet_id);
$outletId = $this->form->outlet_id; }
$this->vouchers = Voucher::whereHas('users', function ($q) use ($memberId) { public function updatedFormOutletId(?string $outletId = null)
$q->where('user_id', $memberId); {
}) $customer = Customer::with('user')->find($this->form->member_id);
->whereHas('outlets', function ($q) use ($outletId) { if (! $customer) {
$q->where('outlet_id', $outletId); 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() ->active()
->select('id', 'name', 'type', 'discount_amount', 'max_discount') ->select('id', 'name', 'type', 'discount_amount', 'max_discount')
->orderByRaw(" ->orderByRaw("

View File

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

View File

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

View File

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