119 lines
4.2 KiB
PHP
119 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin\Inventory;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Requests\Inventory\StorePackagingItemRequest;
|
||
use App\Http\Requests\Inventory\UpdatePackagingItemRequest;
|
||
use App\Models\PackagingItem;
|
||
use App\Models\PackagingMaterial;
|
||
use App\Models\Supplier;
|
||
use App\Repositories\PackagingItemRepository;
|
||
use Illuminate\Http\Request;
|
||
|
||
class PackagingItemController extends Controller
|
||
{
|
||
public function __construct(
|
||
protected PackagingItemRepository $packagingItemRepository
|
||
) {}
|
||
|
||
public function index(Request $request)
|
||
{
|
||
$category = $request->get('category', 'packaging');
|
||
$isShipping = $category === 'shipping';
|
||
|
||
$query = PackagingItem::query()
|
||
->with(['packagingMaterial', 'supplier'])
|
||
->orderBy('name');
|
||
|
||
if ($isShipping) {
|
||
$query->where('category', 'shipping');
|
||
} else {
|
||
$query->where('category', 'packaging');
|
||
}
|
||
|
||
return view('admin.inventory.packaging-items.index', [
|
||
'values' => $query->get(),
|
||
'category' => $category,
|
||
'pageTitle' => $isShipping ? __('Versandverpackung') : __('Produktverpackung'),
|
||
]);
|
||
}
|
||
|
||
public function create(Request $request)
|
||
{
|
||
$category = $request->query('category', 'packaging');
|
||
if (! in_array($category, ['packaging', 'shipping'], true)) {
|
||
$category = 'packaging';
|
||
}
|
||
|
||
return view('admin.inventory.packaging-items.form', [
|
||
'model' => new PackagingItem(['active' => true, 'category' => $category === 'shipping' ? 'shipping' : 'packaging', 'weight_grams' => 0]),
|
||
'packagingMaterials' => PackagingMaterial::query()->orderBy('pos')->orderBy('name')->get(),
|
||
'suppliers' => Supplier::query()->orderBy('name')->get(),
|
||
'category' => $category,
|
||
]);
|
||
}
|
||
|
||
public function store(StorePackagingItemRequest $request)
|
||
{
|
||
$validated = $request->validated();
|
||
|
||
\Log::debug('PackagingItem STORE – raw input', [
|
||
'category_raw' => $request->input('category'),
|
||
'category_hex' => bin2hex((string) $request->input('category')),
|
||
'all_input' => $request->all(),
|
||
]);
|
||
\Log::debug('PackagingItem STORE – validated', $validated);
|
||
|
||
$item = $this->packagingItemRepository->create($validated);
|
||
|
||
\Session::flash('alert-save', '1');
|
||
|
||
$category = $item->category === 'shipping' ? 'shipping' : 'packaging';
|
||
|
||
return redirect()->route('admin.inventory.packaging-items.index', ['category' => $category]);
|
||
}
|
||
|
||
public function edit(PackagingItem $packagingItem)
|
||
{
|
||
$category = $packagingItem->category === 'shipping' ? 'shipping' : 'packaging';
|
||
|
||
return view('admin.inventory.packaging-items.form', [
|
||
'model' => $packagingItem,
|
||
'packagingMaterials' => PackagingMaterial::query()->orderBy('pos')->orderBy('name')->get(),
|
||
'suppliers' => Supplier::query()->orderBy('name')->get(),
|
||
'category' => $category,
|
||
]);
|
||
}
|
||
|
||
public function update(UpdatePackagingItemRequest $request, PackagingItem $packagingItem)
|
||
{
|
||
$validated = $request->validated();
|
||
|
||
\Log::debug('PackagingItem UPDATE – raw input', [
|
||
'category_raw' => $request->input('category'),
|
||
'category_hex' => bin2hex((string) $request->input('category')),
|
||
'all_input' => $request->all(),
|
||
]);
|
||
\Log::debug('PackagingItem UPDATE – validated', $validated);
|
||
|
||
$this->packagingItemRepository->update($packagingItem, $validated);
|
||
|
||
\Session::flash('alert-save', '1');
|
||
|
||
$category = $packagingItem->category === 'shipping' ? 'shipping' : 'packaging';
|
||
|
||
return redirect()->route('admin.inventory.packaging-items.index', ['category' => $category]);
|
||
}
|
||
|
||
public function destroy(PackagingItem $packagingItem)
|
||
{
|
||
$category = $packagingItem->category === 'shipping' ? 'shipping' : 'packaging';
|
||
|
||
$packagingItem->delete();
|
||
|
||
\Session::flash('alert-success', __('Eintrag gelöscht'));
|
||
|
||
return redirect()->route('admin.inventory.packaging-items.index', ['category' => $category]);
|
||
}
|
||
}
|