23-01-2026
This commit is contained in:
parent
a939cd51ef
commit
a8b395e20d
248 changed files with 29342 additions and 4805 deletions
|
|
@ -2,24 +2,21 @@
|
|||
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
|
||||
use App\Models\CountryPrice;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductAttribute;
|
||||
use App\Models\ProductBundle;
|
||||
use App\Models\ProductCategory;
|
||||
use App\Models\ProductImage;
|
||||
use App\Models\ProductIngredient;
|
||||
|
||||
class ProductRepository extends BaseRepository {
|
||||
|
||||
|
||||
class ProductRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(Product $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* refresh.
|
||||
*/
|
||||
|
|
@ -34,34 +31,33 @@ class ProductRepository extends BaseRepository {
|
|||
$data['sponsor_buying_points'] = isset($data['sponsor_buying_points']) ? 1 : 0;
|
||||
$data['show_on'] = isset($data['show_on']) ? $data['show_on'] : null;
|
||||
|
||||
if($data['id'] === "new"){
|
||||
if ($data['id'] === 'new') {
|
||||
$this->model = Product::create($data);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
$this->model = $this->getById($data['id']);
|
||||
$this->model->slug = null;
|
||||
$this->model->fill($data);
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
$this->updateCategories(isset($data['categories']) ? $data['categories'] : array());
|
||||
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array());
|
||||
$this->updateIngredients(isset($data['product_ingredients']) ? $data['product_ingredients'] : array());
|
||||
$this->updateCategories(isset($data['categories']) ? $data['categories'] : []);
|
||||
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : []);
|
||||
$this->updateIngredients(isset($data['product_ingredients']) ? $data['product_ingredients'] : []);
|
||||
$this->updateBundles(isset($data['product_bundles']) ? $data['product_bundles'] : []);
|
||||
$this->updateBundleQuantities(isset($data['bundle_quantities']) ? $data['bundle_quantities'] : []);
|
||||
$this->updateCountryPrices($data);
|
||||
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
public function updateIngredients($data = array())
|
||||
public function updateIngredients($data = [])
|
||||
{
|
||||
$ProductIngredient = $this->model->p_ingredients()->pluck('ingredient_id')->toArray();
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
//not use
|
||||
if(!in_array($id, $ProductIngredient)){
|
||||
// not use
|
||||
if (! in_array($id, $ProductIngredient)) {
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $id,
|
||||
|
|
@ -69,20 +65,99 @@ class ProductRepository extends BaseRepository {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateCategories($data = array())
|
||||
/**
|
||||
* Aktualisiert Bundle-Produkte (Set/Kit-Inhalte)
|
||||
* Fügt nur neue Bundle-Items hinzu, löscht keine bestehenden
|
||||
*
|
||||
* @param array $data Array von Produkt-IDs die zum Bundle hinzugefügt werden sollen
|
||||
* @return bool
|
||||
*/
|
||||
public function updateBundles($data = [])
|
||||
{
|
||||
$existingBundleIds = $this->model->bundleItems()->pluck('bundle_product_id')->toArray();
|
||||
|
||||
if (is_array($data)) {
|
||||
$pos = count($existingBundleIds);
|
||||
foreach ($data as $bundleProductId) {
|
||||
// Nur hinzufügen wenn noch nicht vorhanden und nicht das Produkt selbst
|
||||
if (! in_array($bundleProductId, $existingBundleIds) && $bundleProductId != $this->model->id) {
|
||||
ProductBundle::create([
|
||||
'product_id' => $this->model->id,
|
||||
'bundle_product_id' => $bundleProductId,
|
||||
'quantity' => 1,
|
||||
'pos' => $pos++,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert die Menge eines Bundle-Items
|
||||
*
|
||||
* @param int $bundleProductId ID des enthaltenen Produkts
|
||||
* @param int $quantity Neue Menge
|
||||
* @return bool
|
||||
*/
|
||||
public function updateBundleQuantity($bundleProductId, $quantity)
|
||||
{
|
||||
$bundle = ProductBundle::where('product_id', $this->model->id)
|
||||
->where('bundle_product_id', $bundleProductId)
|
||||
->first();
|
||||
|
||||
if ($bundle) {
|
||||
$bundle->quantity = max(1, intval($quantity));
|
||||
$bundle->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert die Mengen aller Bundle-Items
|
||||
*
|
||||
* @param array $data Array mit bundle_product_id => quantity Zuordnungen
|
||||
* @return bool
|
||||
*/
|
||||
public function updateBundleQuantities($data = [])
|
||||
{
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $bundleProductId => $item) {
|
||||
if (isset($item['qty']) && isset($item['id'])) {
|
||||
$bundle = ProductBundle::where('product_id', $this->model->id)
|
||||
->where('bundle_product_id', $item['id'])
|
||||
->first();
|
||||
|
||||
if ($bundle) {
|
||||
$bundle->quantity = max(1, intval($item['qty']));
|
||||
$bundle->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateCategories($data = [])
|
||||
{
|
||||
foreach ($this->model->categories as $category) {
|
||||
if(($pos = array_search($category->category_id, $data)) !== FALSE){
|
||||
if (($pos = array_search($category->category_id, $data)) !== false) {
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
} else {
|
||||
$category->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
ProductCategory::create([
|
||||
'product_id' => $this->model->id,
|
||||
|
|
@ -90,20 +165,21 @@ class ProductRepository extends BaseRepository {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateAttributes($data = array())
|
||||
public function updateAttributes($data = [])
|
||||
{
|
||||
foreach ($this->model->attributes as $attribute) {
|
||||
if(($pos = array_search($attribute->attribute_id, $data)) !== FALSE){
|
||||
if (($pos = array_search($attribute->attribute_id, $data)) !== false) {
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
} else {
|
||||
$attribute->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
ProductAttribute::create([
|
||||
'product_id' => $this->model->id,
|
||||
|
|
@ -111,92 +187,79 @@ class ProductRepository extends BaseRepository {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateCountryPrices($data)
|
||||
{
|
||||
if(!isset($data['country_prices']) || !is_array($data['country_prices'])){
|
||||
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,
|
||||
]);
|
||||
|
||||
$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->name = 'Kopie: ' . $this->model->name;
|
||||
$this->model->wp_number = null;
|
||||
$this->model->save();
|
||||
|
||||
//categories
|
||||
foreach ($model->categories as $category){
|
||||
// categories
|
||||
foreach ($model->categories as $category) {
|
||||
ProductCategory::create([
|
||||
'product_id' => $this->model->id,
|
||||
'category_id' => $category->category_id,
|
||||
]);
|
||||
}
|
||||
|
||||
//attributes
|
||||
foreach ($model->attributes as $attribute){
|
||||
// attributes
|
||||
foreach ($model->attributes as $attribute) {
|
||||
ProductAttribute::create([
|
||||
'product_id' => $this->model->id,
|
||||
'attribute_id' => $attribute->attribute_id,
|
||||
]);
|
||||
}
|
||||
|
||||
//images
|
||||
foreach ($model->images as $image){
|
||||
// images
|
||||
foreach ($model->images as $image) {
|
||||
$name = \App\Services\Slim::sanitizeFileName($image->original_name);
|
||||
$name = uniqid() . '_' . $name;
|
||||
|
||||
//copy
|
||||
// copy
|
||||
$data = \Storage::disk('public')->copy(
|
||||
'images/product/'.$image->product_id.'/'.$image->filename,
|
||||
'images/product/'.$this->model->id.'/'.$name
|
||||
'images/product/' . $image->product_id . '/' . $image->filename,
|
||||
'images/product/' . $this->model->id . '/' . $name
|
||||
);
|
||||
|
||||
|
||||
ProductImage::create([
|
||||
'product_id' => $this->model->id,
|
||||
'filename' => $name,
|
||||
'original_name' => $image->original_name,
|
||||
'ext' => $image->ext,
|
||||
'mine' => $image->mine,
|
||||
'size' => $image->size
|
||||
'size' => $image->size,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function delete()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public function delete() {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue