gruene-seele/app/Repositories/ProductRepository.php

452 lines
16 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\Attribute;
use App\Models\CountryPrice;
use App\Models\Ingredient;
use App\Models\Product;
use App\Models\ProductAttribute;
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
{
public function __construct(Product $model)
{
$this->model = $model;
}
/**
* refresh.
*/
public function update($data)
{
$data['active'] = isset($data['active']) ? 1 : 0;
$data['single_commission'] = isset($data['single_commission']) ? 1 : 0;
$data['amount_commission'] = isset($data['amount_commission']) ? 1 : 0;
$data['exclude_stats_sales'] = isset($data['exclude_stats_sales']) ? 1 : 0;
$data['whitelabel'] = isset($data['whitelabel']) ? 1 : 0;
$data['shipping_addon'] = isset($data['shipping_addon']) ? 1 : 0;
$data['max_buy'] = isset($data['max_buy']) ? 1 : 0;
$data['show_on'] = isset($data['show_on']) ? $data['show_on'] : null;
if (array_key_exists('shelf_life_type', $data)) {
if ($data['shelf_life_type'] === '' || $data['shelf_life_type'] === null) {
$data['shelf_life_type'] = null;
$data['shelf_life_months'] = null;
} elseif ($data['shelf_life_type'] === 'pao') {
$data['shelf_life_months'] = null;
} elseif ($data['shelf_life_type'] === 'fixed' && array_key_exists('shelf_life_months', $data) && $data['shelf_life_months'] !== '' && $data['shelf_life_months'] !== null) {
$data['shelf_life_months'] = (int) $data['shelf_life_months'];
}
}
if ($data['id'] === 'new') {
$this->model = Product::create($data);
} else {
$this->model = $this->getById($data['id']);
$this->model->fill($data);
$this->model->save();
}
$this->updateCategories(isset($data['categories']) ? $data['categories'] : []);
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : []);
$this->updateWLVariants(isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
$this->updateWLImageAttributs(isset($data['image_wl_attributes']) ? $data['image_wl_attributes'] : [], isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
$this->updateIngredients($data);
$this->updateManufacturerIngredients($data);
$this->updatePackagings($data);
$this->updateCountryPrices($data);
return $this->model;
}
public function updatePackagings(array $data = []): bool
{
if (! isset($data['pp_packaging_item_id']) || ! is_array($data['pp_packaging_item_id'])) {
$this->model->packagings()->detach();
return true;
}
$ids = $data['pp_packaging_item_id'];
$quantities = $data['pp_quantity'] ?? [];
$syncData = [];
foreach ($ids as $index => $packagingItemId) {
$pid = (int) $packagingItemId;
if ($pid <= 0) {
continue;
}
$qtyRaw = $quantities[$index] ?? null;
$qty = $this->parseNullableDecimal($qtyRaw);
if ($qty === null || $qty <= 0) {
$qty = 1.0;
}
$syncData[$pid] = [
'quantity' => $qty,
'pos' => (int) $index,
];
}
$this->model->packagings()->sync($syncData);
return true;
}
public function updateIngredients(array $data = []): bool
{
if (! array_key_exists('product_inci_sync_sent', $data)) {
if (isset($data['product_ingredients']) && is_array($data['product_ingredients'])) {
$ids = array_values(array_unique(array_filter(array_map('intval', $data['product_ingredients']), fn (int $id) => $id > 0)));
$defaults = Ingredient::whereIn('id', $ids)->pluck('default_factor', 'id');
ProductIngredient::where('product_id', $this->model->id)
->where('recipe_type', 'product')
->delete();
foreach ($ids as $index => $ingredientId) {
$factor = $defaults[$ingredientId] ?? 1.10;
ProductIngredient::create([
'product_id' => $this->model->id,
'ingredient_id' => $ingredientId,
'pos' => $index,
'gram' => null,
'factor' => $factor,
'recipe_type' => 'product',
]);
}
}
return true;
}
if (isset($data['pi_ingredient_id']) && is_array($data['pi_ingredient_id'])) {
$ids = $data['pi_ingredient_id'];
$grams = $data['pi_gram'] ?? [];
$factors = $data['pi_factor'] ?? [];
ProductIngredient::where('product_id', $this->model->id)
->where('recipe_type', 'product')
->delete();
foreach ($ids as $index => $ingredientId) {
$ingredientId = (int) $ingredientId;
if ($ingredientId <= 0) {
continue;
}
ProductIngredient::create([
'product_id' => $this->model->id,
'ingredient_id' => $ingredientId,
'pos' => $index,
'gram' => $this->parseNullableDecimal($grams[$index] ?? null),
'factor' => $this->parseFactor($factors[$index] ?? null, $ingredientId),
'recipe_type' => 'product',
]);
}
return true;
}
ProductIngredient::where('product_id', $this->model->id)
->where('recipe_type', 'product')
->delete();
return true;
}
public function updateManufacturerIngredients(array $data = []): bool
{
if (! array_key_exists('manufacturer_inci_sync_sent', $data)) {
return true;
}
if (isset($data['mfg_ingredient_id']) && is_array($data['mfg_ingredient_id'])) {
$ids = $data['mfg_ingredient_id'];
$grams = $data['mfg_gram'] ?? [];
$factors = $data['mfg_factor'] ?? [];
ProductIngredient::where('product_id', $this->model->id)
->where('recipe_type', 'manufacturer')
->delete();
foreach ($ids as $index => $ingredientId) {
$ingredientId = (int) $ingredientId;
if ($ingredientId <= 0) {
continue;
}
ProductIngredient::create([
'product_id' => $this->model->id,
'ingredient_id' => $ingredientId,
'pos' => $index,
'gram' => $this->parseNullableDecimal($grams[$index] ?? null),
'factor' => $this->parseFactor($factors[$index] ?? null, $ingredientId),
'recipe_type' => 'manufacturer',
]);
}
return true;
}
ProductIngredient::where('product_id', $this->model->id)
->where('recipe_type', 'manufacturer')
->delete();
return true;
}
private function parseNullableDecimal(mixed $value): ?float
{
if ($value === null || $value === '') {
return null;
}
if (is_numeric($value)) {
return (float) $value;
}
$normalized = reFormatNumber((string) $value);
return $normalized !== null && $normalized !== '' ? (float) $normalized : null;
}
private function parseFactor(mixed $value, int $ingredientId): float
{
if ($value === null || $value === '') {
$default = Ingredient::whereKey($ingredientId)->value('default_factor');
return $default !== null ? (float) $default : 1.10;
}
if (is_numeric($value)) {
return (float) $value;
}
$normalized = reFormatNumber((string) $value);
return $normalized !== null && $normalized !== '' ? (float) $normalized : 1.10;
}
public function updateCategories($data = [])
{
foreach ($this->model->categories as $category) {
if (($pos = array_search($category->category_id, $data)) !== false) {
unset($data[$pos]);
} else {
$category->delete();
}
}
// set attr
if (is_array($data)) {
foreach ($data as $id) {
ProductCategory::create([
'product_id' => $this->model->id,
'category_id' => $id,
]);
}
}
return true;
}
public function updateAttributes($data = [])
{
foreach ($this->model->attributes as $attribute) {
if (($pos = array_search($attribute->attribute_id, $data)) !== false) {
unset($data[$pos]);
} else {
$attribute->delete();
}
}
// set attr
if (is_array($data)) {
foreach ($data as $id) {
$attribute = Attribute::findOrFail($id);
ProductAttribute::create([
'product_id' => $this->model->id,
'type_id' => $attribute->attribute_type_id,
'attribute_id' => $attribute->id,
]);
}
}
return true;
}
public function updateWLVariants($data = [])
{
foreach ($this->model->attribute_variants as $variant) {
if (($pos = array_search($variant->attribute_id, $data)) !== false) {
unset($data[$pos]);
} else {
$variant->delete();
}
}
// set attr
if (is_array($data)) {
foreach ($data as $id) {
$attribute = Attribute::findOrFail($id);
ProductAttribute::create([
'product_id' => $this->model->id,
'type_id' => $attribute->attribute_type_id,
'attribute_id' => $attribute->id,
]);
}
}
return true;
}
public function updateWLImageAttributs($attributes = [], $variants = [])
{
// abgleich der attributes gegen die variants
foreach ($attributes as $image => $value) {
foreach ($value as $k => $val) {
if (! is_array($variants) || ! in_array($val, $variants)) {
unset($attributes[$image][$k]);
}
}
}
foreach ($this->model->whitelabel_images as $image) {
$image->update([
'attributes' => isset($attributes[$image->id]) ? $attributes[$image->id] : null,
]);
}
return true;
}
public function updateCountryPrices($data)
{
if (! isset($data['country_prices']) || ! is_array($data['country_prices'])) {
return false;
}
foreach ($data['country_prices'] as $k => $country_id) {
$cp = CountryPrice::updateOrCreate([
'country_id' => $country_id,
'product_id' => $this->model->id,
],
[
'c_price' => isset($data['c_price'][$country_id]) ? reFormatNumber($data['c_price'][$country_id]) : null,
'c_tax' => isset($data['c_tax'][$country_id]) ? reFormatNumber($data['c_tax'][$country_id]) : null,
'c_price_old' => isset($data['c_price_old'][$country_id]) ? reFormatNumber($data['c_price_old'][$country_id]) : null,
'c_currency' => isset($data['c_currency'][$country_id]) ? reFormatNumber($data['c_currency'][$country_id]) : null,
]);
}
return true;
}
public function copy($model)
{
$this->model = $model->replicate();
$this->model->name = 'Kopie: '.$this->model->name;
$this->model->wp_number = null;
$this->model->save();
foreach ($model->categories as $category) {
ProductCategory::create([
'product_id' => $this->model->id,
'category_id' => $category->category_id,
]);
}
foreach ($model->attributes as $attribute) {
ProductAttribute::create([
'product_id' => $this->model->id,
'type_id' => $attribute->type_id,
'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,
'ingredient_id' => $ing->id,
'pos' => (int) ($ing->pivot->pos ?? 0),
'gram' => $ing->pivot->gram,
'factor' => $ing->pivot->factor !== null ? (float) $ing->pivot->factor : 1.10,
'recipe_type' => 'product',
]);
}
foreach ($model->manufacturer_ingredients()->orderByPivot('pos')->get() as $ing) {
ProductIngredient::create([
'product_id' => $this->model->id,
'ingredient_id' => $ing->id,
'pos' => (int) ($ing->pivot->pos ?? 0),
'gram' => $ing->pivot->gram,
'factor' => $ing->pivot->factor !== null ? (float) $ing->pivot->factor : 1.10,
'recipe_type' => 'manufacturer',
]);
}
$packSync = [];
foreach ($model->packagings()->orderByPivot('pos')->get() as $pack) {
$packSync[$pack->id] = [
'quantity' => $pack->pivot->quantity !== null ? (float) $pack->pivot->quantity : 1.0,
'pos' => (int) ($pack->pivot->pos ?? 0),
];
}
$this->model->packagings()->sync($packSync);
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;
$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,
'type' => $image->type,
'filename' => $name,
'original_name' => $image->original_name,
'ext' => $image->ext,
'mine' => $image->mine,
'size' => $image->size,
'pos' => $image->pos,
'active' => $image->active ?? true,
'attributes' => $image->attributes,
]);
}
}
public function delete() {}
}