193 lines
5.1 KiB
PHP
193 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Feed\FeedPurchases\Traits;
|
|
|
|
use App\Models\Feed;
|
|
use App\Models\FeedPurchaseItem;
|
|
|
|
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;
|
|
|
|
$this->cart = FeedPurchaseItem::where('feed_purchase_id', $id)
|
|
->get()
|
|
->pluck('quantity', 'feed_id')
|
|
->map(fn ($qty) => ['qty' => (int) $qty])
|
|
->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($qty = null): void
|
|
{
|
|
if ($qty !== null) {
|
|
$this->modalQty = $qty;
|
|
}
|
|
|
|
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);
|
|
if (! $feed) {
|
|
return;
|
|
}
|
|
|
|
$id = isset($this->record) ? $this->record->id : null;
|
|
|
|
$item = FeedPurchaseItem::where('feed_purchase_id', $id)
|
|
->where('feed_id', $feedId)
|
|
->first();
|
|
|
|
if ($item) {
|
|
$item->increment('quantity', 1);
|
|
$item->update(['subtotal' => $item->quantity * $item->unit_price]);
|
|
} else {
|
|
FeedPurchaseItem::create([
|
|
'feed_purchase_id' => $id,
|
|
'feed_id' => $feedId,
|
|
'quantity' => 1,
|
|
'unit_price' => $feed->price,
|
|
'subtotal' => $feed->price,
|
|
]);
|
|
}
|
|
|
|
// Update stock if editing
|
|
if ($id) {
|
|
$feed->increment('stock', 1);
|
|
}
|
|
|
|
$this->loadCartFromDb();
|
|
$this->dispatch('cart-updated');
|
|
}
|
|
|
|
public function decreaseQty(int $feedId): void
|
|
{
|
|
$id = isset($this->record) ? $this->record->id : null;
|
|
|
|
$item = FeedPurchaseItem::where('feed_purchase_id', $id)
|
|
->where('feed_id', $feedId)
|
|
->first();
|
|
|
|
if ($item) {
|
|
if ($item->quantity > 1) {
|
|
$item->decrement('quantity', 1);
|
|
$item->update(['subtotal' => $item->quantity * $item->unit_price]);
|
|
} else {
|
|
$item->delete();
|
|
}
|
|
|
|
// Update stock if editing
|
|
if ($id) {
|
|
$item->feed?->decrement('stock', 1);
|
|
}
|
|
|
|
$this->loadCartFromDb();
|
|
$this->dispatch('cart-updated');
|
|
}
|
|
}
|
|
|
|
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;
|
|
$item = FeedPurchaseItem::find($itemId);
|
|
|
|
if ($item && $item->feed_purchase_id == $id) {
|
|
// Revert stock if editing
|
|
if ($id) {
|
|
$item->feed?->decrement('stock', $item->quantity);
|
|
}
|
|
|
|
$item->delete();
|
|
}
|
|
|
|
$this->loadCartFromDb();
|
|
$this->dispatch('cart-updated');
|
|
}
|
|
}
|