53 lines
1.1 KiB
PHP
53 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Order;
|
|
|
|
use App\Models\OrderItem;
|
|
use Flux\Flux;
|
|
use Livewire\Attributes\On;
|
|
|
|
trait WithUpdateItem
|
|
{
|
|
public string $modalTitle = 'Ubah Item';
|
|
|
|
#[On('modal:open')]
|
|
public function openModal(OrderItem $item): void
|
|
{
|
|
$this->modalTitle = $item->orderable->name;
|
|
|
|
$this->form->item_id = $item->id;
|
|
|
|
$this->form->quantity_edit = formatCurrencyNumber($item->quantity, '');
|
|
}
|
|
|
|
public function closeModal(): void
|
|
{
|
|
$this->reset('form.item_id');
|
|
}
|
|
|
|
public function updateItem(): void
|
|
{
|
|
$item = OrderItem::find($this->form->item_id);
|
|
|
|
if (! $item) {
|
|
$this->toast('Item tidak ditemukan. Silakan coba lagi.', 'Gagal', 'danger');
|
|
|
|
return;
|
|
}
|
|
|
|
$item->update(['quantity' => parseRupiahToInt($this->form->quantity_edit)]);
|
|
|
|
$item->refresh();
|
|
|
|
$this->items = $this->items->map(fn ($i) => $i->id === $item->id ? $item : $i);
|
|
|
|
$this->subtotal = $this->getSubTotal();
|
|
|
|
$this->total = $this->getTotal();
|
|
|
|
$this->toast('Item berhasil diperbarui.');
|
|
|
|
Flux::modals('form-modal')->close();
|
|
}
|
|
}
|