with(['ingredient', 'packagingItem', 'location', 'stockEntry', 'user']) ->latest('disposed_at') ->latest('id'); if (in_array($request->query('type'), ['ingredient', 'packaging'], true)) { $query->where('disposal_type', $request->query('type')); } return view('admin.inventory.stock-disposals.index', [ 'values' => $query->limit(500)->get(), 'typeFilter' => $request->query('type'), ]); } public function create(Request $request): View|RedirectResponse { if (! auth()->user()->isAdmin()) { return redirect()->route('home'); } $prefill = [ 'disposal_type' => 'ingredient', 'ingredient_id' => null, 'ingredient_label' => null, ]; $ingredientId = (int) $request->query('ingredient_id'); if ($ingredientId > 0 && ($ingredient = Ingredient::query()->find($ingredientId))) { $prefill['ingredient_id'] = $ingredient->id; $prefill['ingredient_label'] = $ingredient->inci ? $ingredient->name.' ('.$ingredient->inci.')' : $ingredient->name; } return view('admin.inventory.stock-disposals.create', [ 'locations' => Location::query()->where('active', true)->orderBy('name')->get(), 'reasons' => $this->reasons(), 'prefill' => $prefill, ]); } public function store(StoreStockDisposalRequest $request): RedirectResponse { $data = $request->validatedPayload(); $data['user_id'] = (int) $request->user()->id; StockDisposal::query()->create($data); \Session::flash('alert-save', '1'); return redirect()->route('admin.inventory.stock-disposals.index'); } public function ingredientCharges(Ingredient $ingredient): JsonResponse { $remainingByLocation = $this->inventoryService->remainingByLocationForIngredient($ingredient->id); $charges = StockEntry::query() ->where('status', 'received') ->where('entry_type', 'ingredient') ->where('ingredient_id', $ingredient->id) ->with('location') ->orderBy('best_before') ->get(['id', 'location_id', 'batch_number', 'best_before', 'received_quantity']); $results = $charges->map(function (StockEntry $charge) { $label = $charge->batch_number ? __('Charge').' '.$charge->batch_number : __('Charge #:id', ['id' => $charge->id]); if ($charge->location) { $label .= ' · '.$charge->location->name; } if ($charge->best_before) { $label .= ' · MHD '.$charge->best_before->format('d.m.Y'); } return [ 'id' => $charge->id, 'location_id' => $charge->location_id, 'text' => $label, ]; })->values()->all(); return response()->json([ 'charges' => $results, 'remaining_by_location' => $remainingByLocation, ]); } /** * Aktive Ausschuss-Gründe aus den Einstellungen (Warenwirtschaft → Allgemein). * * @return array */ protected function reasons(): array { return DisposalReason::query() ->active() ->orderBy('pos') ->orderBy('label') ->pluck('label') ->all(); } }