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

77 lines
2.5 KiB
PHP

<?php
namespace App\Livewire\Studio\Stock\InventoryHistory;
use App\Enums\AuditStockStatus;
use App\Models\Bottle;
use App\Models\Perfume;
use App\Models\Product;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
#[Title('Riwayat Stok')]
class Show extends Component
{
public $item;
public string $type;
public $activityLogs = [];
public function mount(string $type, string $hash): void
{
$this->type = $type;
$this->item = match ($type) {
'perfume' => Perfume::byHash($hash),
'product' => Product::byHash($hash),
'bottle' => Bottle::byHash($hash),
default => null,
};
if (! $this->item) {
abort(404);
}
$this->activityLogs = Activity::where('subject_type', get_class($this->item))
->where('subject_id', $this->item->id)
->latest()
->get()
->map(function (Activity $activity) {
$status = match ($activity->event) {
'increase' => AuditStockStatus::INCREASED,
'decrease' => AuditStockStatus::DECREASED,
'created' => AuditStockStatus::INCREASED,
'updated' => AuditStockStatus::UPDATED,
'deleted' => AuditStockStatus::DELETED,
default => AuditStockStatus::tryFrom($activity->event),
};
$color = $status?->color() ?? 'gray';
$label = $status?->label() ?? ucfirst($activity->event);
$classes = match ($color) {
'emerald' => 'bg-emerald-200 text-emerald-800 dark:bg-emerald-800 dark:text-emerald-200',
'orange' => 'bg-orange-200 text-orange-800 dark:bg-orange-800 dark:text-orange-200',
'yellow' => 'bg-yellow-200 text-yellow-800 dark:bg-yellow-800 dark:text-yellow-200',
'red' => 'bg-red-200 text-red-800 dark:bg-red-800 dark:text-red-200',
default => 'bg-gray-200 text-gray-800 dark:bg-gray-800 dark:text-gray-200',
};
$activity->event = [
'label' => $label,
'color' => $color,
'classes' => $classes,
];
return $activity;
});
}
public function render(): View
{
return view('livewire.studio.stock.inventory-history.show');
}
}