fill_weight decimal(10,3) + float-Cast; deutsche Eingabe normalisiert, positiv validiert, Fehler im Formular sichtbar. Tests für F-07/F-08. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
274 lines
9.9 KiB
PHP
Executable file
274 lines
9.9 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Country;
|
|
use App\Models\Ingredient;
|
|
use App\Models\PackagingItem;
|
|
use App\Models\Product;
|
|
use App\Models\ProductImage;
|
|
use App\Models\ProductIngredient;
|
|
use App\Models\StorageRoom;
|
|
use App\Models\StorageShelf;
|
|
use App\Models\StorageSlot;
|
|
use App\Repositories\ProductRepository;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Request;
|
|
use Validator;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
protected $productRepo;
|
|
|
|
public function __construct(ProductRepository $productRepo)
|
|
{
|
|
$this->middleware('copyreader');
|
|
$this->productRepo = $productRepo;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if (Request::get('show_active_products')) {
|
|
set_user_attr('show_active_products', Request::get('show_active_products'));
|
|
}
|
|
if (get_user_attr('show_active_products') === 'true') {
|
|
$values = Product::where('active', true)->orderBy('pos', 'DESC')->orderBy('id', 'DESC')->get();
|
|
} else {
|
|
$values = Product::orderBy('pos', 'DESC')->orderBy('id', 'DESC')->get();
|
|
|
|
}
|
|
$data = [
|
|
'values' => $values,
|
|
];
|
|
|
|
return view('admin.product.index', $data);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
if ($id === 'new') {
|
|
$model = new Product;
|
|
$model->active = true;
|
|
} else {
|
|
$model = Product::findOrFail($id);
|
|
$model->load(['packagings.packagingMaterial', 'setItems']);
|
|
}
|
|
|
|
$country_for_prices = Country::where('own_eur', '=', true)->orWhere('currency', '=', true)->get();
|
|
$data = [
|
|
'product' => $model,
|
|
'country_for_prices' => $country_for_prices,
|
|
'ingredient_catalog' => Ingredient::query()->where('active', true)->with('materialQuality')->orderBy('name')->get(['id', 'name', 'inci', 'effect', 'default_factor', 'material_quality_id']),
|
|
'packaging_catalog' => PackagingItem::query()->where('active', true)->with('packagingMaterial')->orderBy('name')->get(),
|
|
'set_product_catalog' => $this->setProductCatalog($model),
|
|
'storageRooms' => StorageRoom::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
'storageShelves' => StorageShelf::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
'storageSlots' => StorageSlot::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
];
|
|
|
|
return view('admin.product.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Auswählbare Set-Bestandteile: aktive Einzelprodukte (keine Sets, nicht das Produkt selbst).
|
|
*
|
|
* @return Collection<int, Product>
|
|
*/
|
|
protected function setProductCatalog(Product $model)
|
|
{
|
|
return Product::query()
|
|
->where('active', true)
|
|
->singleProducts()
|
|
->when($model->id, fn ($q) => $q->where('id', '!=', $model->id))
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'number']);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
|
|
$data = Request::all();
|
|
|
|
// Ohne WP-Nummer wird die Artikelnummer übernommen, reduziert auf Ziffern
|
|
// (wp_number ist eine Integer-Spalte). Vor der Validierung, damit die
|
|
// Unique-Regel auch den abgeleiteten Wert prüft.
|
|
if (trim((string) ($data['wp_number'] ?? '')) === '' && trim((string) ($data['number'] ?? '')) !== '') {
|
|
$derived = preg_replace('/\D+/', '', (string) $data['number']);
|
|
if ($derived !== '') {
|
|
Request::merge(['wp_number' => $derived]);
|
|
$data['wp_number'] = $derived;
|
|
}
|
|
}
|
|
|
|
// F-08: Füllgewicht deutsch normalisieren („39,5“) und positiv validieren.
|
|
if (Request::has('fill_weight')) {
|
|
$fillRaw = trim((string) Request::input('fill_weight'));
|
|
$fillNormalized = $fillRaw === '' ? null : reFormatWawiNumber($fillRaw);
|
|
// Unbrauchbare Eingabe („abc“) nicht zu leer normalisieren, sonst greift nullable.
|
|
Request::merge(['fill_weight' => $fillNormalized === '' || $fillNormalized === false ? $fillRaw : $fillNormalized]);
|
|
}
|
|
|
|
$rules = [
|
|
'name' => 'required',
|
|
'fill_weight' => ['nullable', 'numeric', 'gt:0'],
|
|
];
|
|
/*if(isset($data['number']) && $data['number'] != ""){
|
|
$rules['number'] = 'int';
|
|
}*/
|
|
if (isset($data['wp_number'])) {
|
|
if ($data['id'] !== 'new') {
|
|
$model = Product::findOrFail($data['id']);
|
|
$rules['wp_number'] = 'unique:products,wp_number,'.$model->id;
|
|
} else {
|
|
$rules['wp_number'] = 'unique:products,wp_number';
|
|
|
|
}
|
|
}
|
|
$validator = Validator::make(Request::all(), $rules);
|
|
|
|
if ($data['id'] === 'new') {
|
|
$model = new Product;
|
|
} else {
|
|
$model = Product::findOrFail($data['id']);
|
|
$model->load(['packagings.packagingMaterial', 'setItems']);
|
|
}
|
|
$country_for_prices = Country::where('own_eur', '=', true)->orWhere('currency', '=', true)->get();
|
|
|
|
$this->validateSetItems($validator, Request::all(), $model);
|
|
|
|
$data = [
|
|
'product' => $model,
|
|
'country_for_prices' => $country_for_prices,
|
|
'ingredient_catalog' => Ingredient::query()->where('active', true)->with('materialQuality')->orderBy('name')->get(['id', 'name', 'inci', 'effect', 'default_factor', 'material_quality_id']),
|
|
'packaging_catalog' => PackagingItem::query()->where('active', true)->with('packagingMaterial')->orderBy('name')->get(),
|
|
'set_product_catalog' => $this->setProductCatalog($model),
|
|
'storageRooms' => StorageRoom::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
'storageShelves' => StorageShelf::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
'storageSlots' => StorageSlot::query()->active()->orderBy('pos')->orderBy('label')->get(),
|
|
];
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return view('admin.product.edit', $data)->withErrors($validator);
|
|
|
|
} else {
|
|
$product = $this->productRepo->update(Request::all());
|
|
\Session()->flash('alert-save', true);
|
|
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set-Validierung: Set braucht mindestens ein Einzelprodukt als Bestandteil
|
|
* (keine Sets, nicht das Produkt selbst).
|
|
*
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
protected function validateSetItems($validator, array $data, Product $model): void
|
|
{
|
|
if (! isset($data['is_set'])) {
|
|
return;
|
|
}
|
|
|
|
$validator->after(function ($validator) use ($data, $model) {
|
|
$componentIds = collect($data['set_component_id'] ?? [])
|
|
->map(fn ($id) => (int) $id)
|
|
->filter(fn (int $id) => $id > 0 && $id !== (int) $model->id)
|
|
->unique()
|
|
->values();
|
|
|
|
if ($componentIds->isEmpty()) {
|
|
$validator->errors()->add('set_component_id', __('Ein Set benötigt mindestens ein Einzelprodukt als Bestandteil.'));
|
|
|
|
return;
|
|
}
|
|
|
|
$components = Product::query()->whereIn('id', $componentIds)->get(['id', 'is_set']);
|
|
|
|
if ($components->count() !== $componentIds->count()) {
|
|
$validator->errors()->add('set_component_id', __('Mindestens ein gewählter Bestandteil existiert nicht.'));
|
|
}
|
|
|
|
if ($components->where('is_set', true)->isNotEmpty()) {
|
|
$validator->errors()->add('set_component_id', __('Set-Bestandteile dürfen selbst keine Sets sein.'));
|
|
}
|
|
});
|
|
}
|
|
|
|
public function copy($id)
|
|
{
|
|
$model = Product::findOrFail($id);
|
|
$product = $this->productRepo->copy($model);
|
|
\Session()->flash('alert-success', 'Eintrag kopiert');
|
|
|
|
return redirect(route('admin_product_show'));
|
|
}
|
|
|
|
public function delete($id, $do = 'product', $did = null)
|
|
{
|
|
if ($do === 'product') {
|
|
$model = Product::findOrFail($id);
|
|
$model->delete();
|
|
\Session()->flash('alert-success', 'Eintrag gelöscht');
|
|
|
|
return redirect(route('admin_product_show'));
|
|
}
|
|
|
|
if ($do === 'ingredient') {
|
|
$model = Product::findOrFail($id);
|
|
$productIngredient = ProductIngredient::where('ingredient_id', $did)->where('product_id', $model->id)->first();
|
|
if ($productIngredient) {
|
|
$productIngredient->delete();
|
|
\Session()->flash('alert-success', 'Eintrag gelöscht');
|
|
}
|
|
|
|
return redirect(route('admin_product_edit', [$model->id]));
|
|
}
|
|
|
|
abort(404);
|
|
}
|
|
|
|
// Upload FILE -----------------------------------------------------------------------------------------------------------------------
|
|
public function imageUpload()
|
|
{
|
|
|
|
if (Request::has('product_id')) {
|
|
$product = Product::findOrFail(Request::get('product_id'));
|
|
|
|
return \App\Services\ProductImage::imageUpload('product', $product, Request::get('upload_type'));
|
|
}
|
|
|
|
}
|
|
|
|
public function imageDelete($product_image_id, $product_id)
|
|
{
|
|
$product = Product::findOrFail($product_id);
|
|
|
|
return \App\Services\ProductImage::imageDelete('product', $product, $product_image_id);
|
|
|
|
}
|
|
|
|
public function imageAttribute($product_id, $attr, $val = false)
|
|
{
|
|
// Audit M-7: nur erlaubte Spalten — $attr kommt aus der URL und würde
|
|
// sonst die $fillable-Whitelist per dynamischer Property umgehen.
|
|
if (! in_array($attr, ['active', 'pos'], true) || ! is_numeric($val)) {
|
|
abort(400);
|
|
}
|
|
|
|
if ($val < 0) {
|
|
$val = 0;
|
|
}
|
|
|
|
$product_image = ProductImage::findOrFail($product_id);
|
|
|
|
$product_image->{$attr} = $val;
|
|
$product_image->save();
|
|
|
|
\Session()->flash('alert-success', 'Wert gespeichert');
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
}
|