parfum/app/Livewire/Studio/Master/Warehouse/Show.php

169 lines
5.4 KiB
PHP

<?php
namespace App\Livewire\Studio\Master\Warehouse;
use App\Livewire\Forms\Studio\Master\Warehouse\AdjustmentStockForm;
use App\Models\Warehouse;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithCloseModal;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\Notification\WithSubscribeNotification;
use App\Traits\Utilities\WithUpdatedData;
use Flux\Flux;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Detail Gudang')]
class Show extends Component
{
use WithAuthorization, WithCloseModal, WithConfirmation, WithSubscribeNotification, WithToast, WithUpdatedData;
public AdjustmentStockForm $form;
public Warehouse $warehouse;
public string $search = '';
public array $type = [];
public string $method = 'saveAdjustment';
public string $modalTitle = '';
public function mount(Warehouse $warehouse): void
{
$this->warehouse = $warehouse;
}
public function getStatsProperty(): array
{
$perfumesCount = $this->warehouse->perfumes()->count();
$productsCount = $this->warehouse->products()->count();
$bottlesCount = $this->warehouse->bottles()->count();
$totalItems = $perfumesCount + $productsCount + $bottlesCount;
return [
[
'title' => 'Total Item',
'value' => number_format($totalItems, 0, ',', '.'),
],
[
'title' => 'Parfum',
'value' => number_format($perfumesCount, 0, ',', '.'),
],
[
'title' => 'Produk',
'value' => number_format($productsCount, 0, ',', '.'),
],
[
'title' => 'Botol',
'value' => number_format($bottlesCount, 0, ',', '.'),
],
];
}
public function getItemsProperty()
{
$warehouse = $this->warehouse;
$search = $this->search;
$typeFilter = $this->type;
$perfumes = collect();
$products = collect();
$bottles = collect();
// Load perfumes if no type filter or 'perfume' is selected
if (empty($typeFilter) || in_array('perfume', $typeFilter)) {
$perfumes = $warehouse->perfumes()
->when(! empty($search), function ($query) use ($search) {
return $query->where('name', 'like', '%'.$search.'%');
})
->get()
->map(function ($item) {
return [
'id' => 'perfume_'.$item->id,
'hash_id' => $item->hash,
'type' => 'Parfum',
'type_key' => 'perfume',
'name' => $item->name,
'stock' => $item->pivot->stock ?? 0,
];
});
}
// Load products if no type filter or 'product' is selected
if (empty($typeFilter) || in_array('product', $typeFilter)) {
$products = $warehouse->products()
->when(! empty($search), function ($query) use ($search) {
return $query->where('name', 'like', '%'.$search.'%');
})
->get()
->map(function ($item) {
return [
'id' => 'product_'.$item->id,
'hash_id' => $item->hash,
'type' => 'Produk',
'type_key' => 'product',
'name' => $item->name,
'stock' => $item->pivot->stock ?? 0,
];
});
}
// Load bottles if no type filter or 'bottle' is selected
if (empty($typeFilter) || in_array('bottle', $typeFilter)) {
$bottles = $warehouse->bottles()
->when(! empty($search), function ($query) use ($search) {
return $query->where('name', 'like', '%'.$search.'%');
})
->get()
->map(function ($item) {
return [
'id' => 'bottle_'.$item->id,
'hash_id' => $item->hash,
'type' => 'Botol',
'type_key' => 'bottle',
'name' => $item->name,
'stock' => $item->pivot->stock ?? 0,
];
});
}
return $perfumes->concat($products)->concat($bottles);
}
#[On('modal:open')]
public function openModal(string $method, string $modalTitle, string $itemId, string $type, string $name, int $stock): void
{
$this->resetValidation();
$this->resetErrorBag();
$this->method = $method;
$this->modalTitle = $modalTitle;
$this->form->setAdjustment($itemId, $type, $name, $stock);
}
public function saveAdjustment(): void
{
$this->canOrAbort('manage adjustment');
$this->form->save($this->warehouse);
$this->toast('Stok berhasil diperbarui.');
Flux::modals()->close();
}
public function render(): View
{
return view('livewire.studio.master.warehouse.show', [
'pageTitle' => 'Detail Gudang',
'stats' => $this->stats,
]);
}
}