only([ 'q', 'asset_status_id', 'asset_class_id', 'asset_category_id', 'asset_location_id', 'department_id', 'asset_user_id', 'person_in_charge_id', 'warranty_id', ]); $assets = Asset::with(['status', 'class', 'category', 'unit', 'department', 'personInCharge', 'user', 'location', 'warranty']) ->forUser($request->user()) ->when($filters['q'] ?? null, function ($query, $q) { $query->where(function ($qBuilder) use ($q) { $qBuilder->where('code', 'like', "%{$q}%") ->orWhere('name', 'like', "%{$q}%") ->orWhere('serial_number', 'like', "%{$q}%"); }); }) ->when($filters['asset_status_id'] ?? null, fn($q, $v) => $q->where('asset_status_id', $v)) ->when($filters['asset_class_id'] ?? null, fn($q, $v) => $q->where('asset_class_id', $v)) ->when($filters['asset_category_id'] ?? null, fn($q, $v) => $q->where('asset_category_id', $v)) ->when($filters['asset_location_id'] ?? null, fn($q, $v) => $q->where('asset_location_id', $v)) ->when($filters['department_id'] ?? null, fn($q, $v) => $q->where('department_id', $v)) ->when($filters['asset_user_id'] ?? null, fn($q, $v) => $q->where('asset_user_id', $v)) ->when($filters['person_in_charge_id'] ?? null, fn($q, $v) => $q->where('person_in_charge_id', $v)) ->when($filters['warranty_id'] ?? null, fn($q, $v) => $q->where('warranty_id', $v)) ->orderBy('created_at', 'desc') ->paginate(config('system.ui.table_page_size', 15)) ->withQueryString(); return view('assets.index', [ 'assets' => $assets, 'statuses' => AssetStatus::orderBy('name')->get(), 'classes' => AssetClass::orderBy('name')->get(), 'categories' => AssetCategory::orderBy('name')->get(), 'units' => Unit::orderBy('name')->get(), 'departments' => Department::orderBy('name')->get(), 'peopleInCharge' => PersonInCharge::orderBy('name')->get(), 'users' => AssetUser::orderBy('name')->get(), 'locations' => AssetLocation::orderBy('name')->get(), 'warranties' => Warranty::orderBy('duration_months')->get(), 'filters' => $filters, ]); } public function store(Request $request): RedirectResponse { $data = $this->validateRequest($request); $asset = $this->assets->create($data); return back()->with('success', "Aset {$asset->code} berhasil ditambahkan."); } public function update(Request $request, Asset $asset): RedirectResponse { abort_unless($asset->isVisibleTo($request->user()), 403); $data = $this->validateRequest($request, $asset->id); $this->assets->update($asset, $data); return back()->with('success', "Aset {$asset->code} berhasil diperbarui."); } public function show(Asset $asset): View { abort_unless($asset->isVisibleTo(request()->user()), 403); $asset->load([ 'status','class','category','unit','department', 'personInCharge','user','location','warranty', 'movements.fromLocation','movements.toLocation','movements.fromDepartment','movements.toDepartment', 'disposals', ]); return view('assets.show', compact('asset')); } public function history(Asset $asset): View { abort_unless($asset->isVisibleTo(request()->user()), 403); $asset->load('histories'); return view('assets.history', compact('asset')); } private function validateRequest(Request $request, ?string $assetId = null): array { return $request->validate([ 'name' => ['required', 'string', 'max:255'], 'serial_number' => ['nullable', 'string', 'max:255'], 'description' => ['nullable', 'string'], 'asset_status_id' => ['nullable', 'uuid', 'exists:asset_statuses,id'], 'asset_class_id' => ['nullable', 'uuid', 'exists:asset_classes,id'], 'asset_category_id' => ['nullable', 'uuid', 'exists:asset_categories,id'], 'unit_id' => ['nullable', 'uuid', 'exists:units,id'], 'department_id' => ['nullable', 'uuid', 'exists:departments,id'], 'person_in_charge_id' => ['nullable', 'uuid', 'exists:person_in_charges,id'], 'asset_user_id' => ['nullable', 'uuid', 'exists:asset_users,id'], 'asset_location_id' => ['nullable', 'uuid', 'exists:asset_locations,id'], 'warranty_id' => ['nullable', 'uuid', 'exists:warranties,id'], 'purchase_date' => ['nullable', 'date'], 'warranty_end' => ['nullable', 'date'], 'cost' => ['nullable', 'numeric', 'min:0'], 'metadata' => ['nullable', 'array'], ]); } }