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

132 lines
4.2 KiB
PHP

<?php
namespace App\Livewire\Studio\Master\Warehouse;
use App\Models\Warehouse;
use App\Traits\Authorization\WithAuthorization;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Detail Gudang')]
class Show extends Component
{
use WithAuthorization;
public Warehouse $warehouse;
public string $search = '';
public array $type = [];
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);
}
public function render(): View
{
return view('livewire.studio.master.warehouse.show', [
'pageTitle' => 'Detail Gudang',
'stats' => $this->stats,
]);
}
}