parfum/app/Livewire/Studio/Stock/InventoryHistory/Index.php

70 lines
2.4 KiB
PHP

<?php
namespace App\Livewire\Studio\Stock\InventoryHistory;
use App\Models\Bottle;
use App\Models\Perfume;
use App\Models\Product;
use App\Traits\Authorization\WithAuthorization;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
#[Title('Riwayat Stok')]
class Index extends Component
{
use WithAuthorization;
#[Url]
public string $search = '';
#[Url]
public string $typeFilter = 'all';
public function render(): View
{
$perfumes = Perfume::with(['outlets', 'warehouses'])
->when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%'))
->when($this->typeFilter !== 'all' && $this->typeFilter !== 'perfume', fn ($q) => $q->whereNull('id'))
->get()
->map(function ($item) {
$item->type_label = 'Parfum';
$item->type = 'perfume';
$item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock');
return $item;
});
$products = Product::with(['outlets', 'warehouses'])
->when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%'))
->when($this->typeFilter !== 'all' && $this->typeFilter !== 'product', fn ($q) => $q->whereNull('id'))
->get()
->map(function ($item) {
$item->type_label = 'Produk';
$item->type = 'product';
$item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock');
return $item;
});
$bottles = Bottle::with(['outlets', 'warehouses'])
->when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%'))
->when($this->typeFilter !== 'all' && $this->typeFilter !== 'bottle', fn ($q) => $q->whereNull('id'))
->get()
->map(function ($item) {
$item->type_label = 'Botol';
$item->type = 'bottle';
$item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock');
return $item;
});
$items = $perfumes->concat($products)->concat($bottles)->sortBy('name');
return view('livewire.studio.stock.inventory-history.index', [
'items' => $items,
]);
}
}