store/app/Http/Controllers/Admin/Manage/Stock/StockHistoryController.php

48 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Manage\Stock;
use App\Http\Controllers\Controller;
use App\Models\ProductVariant;
use App\Models\RawMaterialPrice;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class StockHistoryController extends Controller
{
public function __invoke(Request $request): Response
{
$stockableType = $request->string('stockable_type')->toString();
$stockableId = (int) $request->string('stockable_id')->toString();
$modelClass = match ($stockableType) {
'product-variant' => ProductVariant::class,
'raw-material-price' => RawMaterialPrice::class,
default => abort(404, 'Tipe stok tidak valid.'),
};
$stockable = $modelClass::with(
$stockableType === 'product-variant'
? ['product', 'stockMutations.user.profile', 'stockMutations.source']
: ['rawMaterial', 'stockMutations.user.profile', 'stockMutations.source']
)->findOrFail($stockableId);
$mutations = $stockable->stockMutations()
->with(['user.profile', 'source'])
->latest()
->paginate(50);
$title = $stockableType === 'product-variant'
? "{$stockable->product?->name} - {$stockable->name}"
: "{$stockable->rawMaterial?->name} - {$stockable->variant}";
return Inertia::render('admin/manage/stock/History', [
'title' => $title,
'stockableType' => $stockableType,
'stockableId' => $stockable->id,
'mutations' => $mutations,
]);
}
}