13-05-2026 Waren Wirtschaft

This commit is contained in:
Kevin Adametz 2026-05-13 18:09:20 +02:00
parent 9ce711d6b2
commit ca3eb663fe
40 changed files with 1000 additions and 189 deletions

View file

@ -11,7 +11,28 @@ class PackagingItemRepository
*/
public function create(array $data): PackagingItem
{
return PackagingItem::create($this->extractAttributes($data));
$attrs = $this->extractAttributes($data);
\DB::enableQueryLog();
try {
$item = PackagingItem::create($attrs);
} catch (\Throwable $e) {
$queries = \DB::getQueryLog();
$lastQ = end($queries);
\Log::error('PackagingItem CREATE FAILED', [
'error' => $e->getMessage(),
'sql' => $lastQ['query'] ?? 'unknown',
'bindings' => $lastQ['bindings'] ?? [],
'binding_types' => array_map(fn($v) => gettype($v) . ':' . json_encode($v), $lastQ['bindings'] ?? []),
'attributes_passed' => $attrs,
]);
throw $e;
}
\DB::disableQueryLog();
return $item;
}
/**
@ -30,15 +51,20 @@ class PackagingItemRepository
*/
protected function extractAttributes(array $data): array
{
return collect($data)->only([
$attrs = collect($data)->only([
'packaging_material_id',
'supplier_id',
'name',
'category',
'weight_grams',
'min_stock_alert',
'url',
'product_id',
'active',
])->all();
$attrs['weight_grams'] = $attrs['weight_grams'] ?? 0;
return $attrs;
}
}

View file

@ -11,6 +11,7 @@ use App\Models\ProductCategory;
use App\Models\ProductImage;
use App\Models\ProductIngredient;
use App\Services\Slim;
use Illuminate\Database\Eloquent\Collection;
class ProductRepository extends BaseRepository
{
@ -345,7 +346,6 @@ class ProductRepository extends BaseRepository
$this->model->wp_number = null;
$this->model->save();
// categories
foreach ($model->categories as $category) {
ProductCategory::create([
'product_id' => $this->model->id,
@ -353,7 +353,6 @@ class ProductRepository extends BaseRepository
]);
}
// attributes
foreach ($model->attributes as $attribute) {
ProductAttribute::create([
'product_id' => $this->model->id,
@ -361,6 +360,15 @@ class ProductRepository extends BaseRepository
'attribute_id' => $attribute->attribute_id,
]);
}
foreach ($model->attribute_variants as $variant) {
ProductAttribute::create([
'product_id' => $this->model->id,
'type_id' => $variant->type_id,
'attribute_id' => $variant->attribute_id,
]);
}
foreach ($model->p_ingredients()->orderByPivot('pos')->get() as $ing) {
ProductIngredient::create([
'product_id' => $this->model->id,
@ -392,16 +400,38 @@ class ProductRepository extends BaseRepository
}
$this->model->packagings()->sync($packSync);
// images
foreach ($model->images as $image) {
foreach ($model->country_prices as $cp) {
CountryPrice::create([
'country_id' => $cp->country_id,
'product_id' => $this->model->id,
'c_price' => $cp->c_price,
'c_tax' => $cp->c_tax,
'c_price_old' => $cp->c_price_old,
'c_currency' => $cp->c_currency,
]);
}
$this->copyImages($model->images, 'product');
$this->copyImages($model->whitelabel_images, 'wllogo');
return $this->model;
}
/**
* @param Collection<int, ProductImage> $images
*/
protected function copyImages($images, string $type): void
{
foreach ($images as $image) {
$name = Slim::sanitizeFileName($image->original_name);
$name = uniqid().'_'.$name;
// copy
$data = \Storage::disk('public')->copy(
'images/product/'.$image->product_id.'/'.$image->filename,
'images/product/'.$this->model->id.'/'.$name
);
$sourcePath = 'images/product/'.$image->product_id.'/'.$image->filename;
$targetPath = 'images/product/'.$this->model->id.'/'.$name;
if (\Storage::disk('public')->exists($sourcePath)) {
\Storage::disk('public')->copy($sourcePath, $targetPath);
}
ProductImage::create([
'product_id' => $this->model->id,
@ -411,12 +441,11 @@ class ProductRepository extends BaseRepository
'ext' => $image->ext,
'mine' => $image->mine,
'size' => $image->size,
'pos' => $image->pos,
'active' => $image->active ?? true,
'attributes' => $image->attributes,
]);
}
return $this->model;
}
public function delete() {}