diff --git a/app/Filament/Resources/Feed/FeedPurchases/Traits/HasCart.php b/app/Filament/Resources/Feed/FeedPurchases/Traits/HasCart.php index edfcc55..34833e5 100644 --- a/app/Filament/Resources/Feed/FeedPurchases/Traits/HasCart.php +++ b/app/Filament/Resources/Feed/FeedPurchases/Traits/HasCart.php @@ -9,6 +9,14 @@ trait HasCart { public array $cart = []; + public bool $isQtyModalOpen = false; + + public ?int $modalFeedId = null; + + public ?int $modalQty = null; + + public ?string $modalFeedName = null; + public function loadCartFromDb(): void { $id = isset($this->record) ? $this->record->id : null; @@ -20,6 +28,31 @@ public function loadCartFromDb(): void ->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 { $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 { $id = isset($this->record) ? $this->record->id : null; diff --git a/resources/views/filament/resources/manage/feed-purchases/feed-items-grid.blade.php b/resources/views/filament/resources/manage/feed-purchases/feed-items-grid.blade.php index 1b3d860..64b4548 100644 --- a/resources/views/filament/resources/manage/feed-purchases/feed-items-grid.blade.php +++ b/resources/views/filament/resources/manage/feed-purchases/feed-items-grid.blade.php @@ -29,7 +29,10 @@ class="w-8 h-8 rounded-full border border-gray-200 dark:border-gray-600 flex ite - - + {{ $this->cart[$feed->id]['qty'] ?? 0 }} @@ -42,6 +45,36 @@ class="w-8 h-8 rounded-full flex items-center justify-center text-white text-lg + @endforeach + + + Atur Jumlah: {{ $this->modalFeedName }} + + +
+ + + +
+ + +
+ + Batal + + + Simpan + +
+
+
+