feat: add modal to manually update feed item quantities in cart
This commit is contained in:
parent
9619912f2f
commit
2b7535000a
@ -9,6 +9,14 @@ trait HasCart
|
|||||||
{
|
{
|
||||||
public array $cart = [];
|
public array $cart = [];
|
||||||
|
|
||||||
|
public bool $isQtyModalOpen = false;
|
||||||
|
|
||||||
|
public ?int $modalFeedId = null;
|
||||||
|
|
||||||
|
public ?int $modalQty = null;
|
||||||
|
|
||||||
|
public ?string $modalFeedName = null;
|
||||||
|
|
||||||
public function loadCartFromDb(): void
|
public function loadCartFromDb(): void
|
||||||
{
|
{
|
||||||
$id = isset($this->record) ? $this->record->id : null;
|
$id = isset($this->record) ? $this->record->id : null;
|
||||||
@ -20,6 +28,31 @@ public function loadCartFromDb(): void
|
|||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function openQtyModal(int $feedId): void
|
||||||
|
{
|
||||||
|
$feed = Feed::find($feedId);
|
||||||
|
if (! $feed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->modalFeedId = $feedId;
|
||||||
|
$this->modalFeedName = $feed->name;
|
||||||
|
$this->modalQty = $this->cart[$feedId]['qty'] ?? 0;
|
||||||
|
$this->isQtyModalOpen = true;
|
||||||
|
|
||||||
|
$this->dispatch('open-modal', id: 'qty-modal');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveQtyModal(): void
|
||||||
|
{
|
||||||
|
if ($this->modalFeedId) {
|
||||||
|
$this->updateQty($this->modalFeedId, (int) $this->modalQty);
|
||||||
|
}
|
||||||
|
$this->isQtyModalOpen = false;
|
||||||
|
|
||||||
|
$this->dispatch('close-modal', id: 'qty-modal');
|
||||||
|
}
|
||||||
|
|
||||||
public function addFeed(int $feedId): void
|
public function addFeed(int $feedId): void
|
||||||
{
|
{
|
||||||
$feed = Feed::find($feedId);
|
$feed = Feed::find($feedId);
|
||||||
@ -81,6 +114,60 @@ public function decreaseQty(int $feedId): void
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateQty(int $feedId, $qty): void
|
||||||
|
{
|
||||||
|
$qty = (int) $qty;
|
||||||
|
$feed = Feed::find($feedId);
|
||||||
|
if (! $feed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = isset($this->record) ? $this->record->id : null;
|
||||||
|
|
||||||
|
$item = FeedPurchaseItem::where('feed_purchase_id', $id)
|
||||||
|
->where('feed_id', $feedId)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($qty <= 0) {
|
||||||
|
if ($item) {
|
||||||
|
// Revert stock if editing
|
||||||
|
if ($id) {
|
||||||
|
$item->feed?->decrement('stock', $item->quantity);
|
||||||
|
}
|
||||||
|
$item->delete();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($item) {
|
||||||
|
$oldQty = $item->quantity;
|
||||||
|
$item->update([
|
||||||
|
'quantity' => $qty,
|
||||||
|
'subtotal' => $qty * $item->unit_price,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Update stock if editing
|
||||||
|
if ($id) {
|
||||||
|
$feed->increment('stock', $qty - $oldQty);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FeedPurchaseItem::create([
|
||||||
|
'feed_purchase_id' => $id,
|
||||||
|
'feed_id' => $feedId,
|
||||||
|
'quantity' => $qty,
|
||||||
|
'unit_price' => $feed->price,
|
||||||
|
'subtotal' => $qty * $feed->price,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Update stock if editing
|
||||||
|
if ($id) {
|
||||||
|
$feed->increment('stock', $qty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->loadCartFromDb();
|
||||||
|
$this->dispatch('cart-updated');
|
||||||
|
}
|
||||||
|
|
||||||
public function removeCartItem(int $itemId): void
|
public function removeCartItem(int $itemId): void
|
||||||
{
|
{
|
||||||
$id = isset($this->record) ? $this->record->id : null;
|
$id = isset($this->record) ? $this->record->id : null;
|
||||||
|
|||||||
@ -29,7 +29,10 @@ class="w-8 h-8 rounded-full border border-gray-200 dark:border-gray-600 flex ite
|
|||||||
-
|
-
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<span class="text-sm font-bold w-6 text-center text-gray-900 dark:text-gray-100">
|
<span
|
||||||
|
wire:click="openQtyModal({{ $feed->id }})"
|
||||||
|
class="text-sm font-bold w-6 text-center text-gray-900 dark:text-gray-100 cursor-pointer hover:text-teal-500 transition-colors"
|
||||||
|
>
|
||||||
{{ $this->cart[$feed->id]['qty'] ?? 0 }}
|
{{ $this->cart[$feed->id]['qty'] ?? 0 }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
@ -42,6 +45,36 @@ class="w-8 h-8 rounded-full flex items-center justify-center text-white text-lg
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
||||||
|
<x-filament::modal id="qty-modal" wire:model.live="isQtyModalOpen" width="sm">
|
||||||
|
<x-slot name="heading">
|
||||||
|
Atur Jumlah: {{ $this->modalFeedName }}
|
||||||
|
</x-slot>
|
||||||
|
|
||||||
|
<div class="py-2">
|
||||||
|
<x-filament::input.wrapper>
|
||||||
|
<x-filament::input
|
||||||
|
type="number"
|
||||||
|
wire:model="modalQty"
|
||||||
|
wire:keydown.enter="saveQtyModal"
|
||||||
|
placeholder="Masukan jumlah..."
|
||||||
|
autofocus
|
||||||
|
/>
|
||||||
|
</x-filament::input.wrapper>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<x-slot name="footer">
|
||||||
|
<div class="flex justify-end gap-3">
|
||||||
|
<x-filament::button color="gray" wire:click="$set('isQtyModalOpen', false)" size="sm">
|
||||||
|
Batal
|
||||||
|
</x-filament::button>
|
||||||
|
<x-filament::button wire:click="saveQtyModal" size="sm">
|
||||||
|
Simpan
|
||||||
|
</x-filament::button>
|
||||||
|
</div>
|
||||||
|
</x-slot>
|
||||||
|
</x-filament::modal>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user