model = $model; } /** * refresh. */ public function update($data) { $data['active'] = isset($data['active']) ? 1 : 0; $data['shipping_addon'] = isset($data['shipping_addon']) ? 1 : 0; $data['no_commission'] = isset($data['no_commission']) ? 1 : 0; $data['no_free_shipping'] = isset($data['no_free_shipping']) ? 1 : 0; $data['free_shipping_consultant'] = isset($data['free_shipping_consultant']) ? 1 : 0; $data['is_membership_only'] = isset($data['is_membership_only']) ? 1 : 0; $data['buying_restriction'] = isset($data['buying_restriction']) ? 1 : 0; $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') { $this->model = Product::create($data); } 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'] : []); $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 = []) { $ProductIngredient = $this->model->p_ingredients()->pluck('ingredient_id')->toArray(); // set attr if (is_array($data)) { foreach ($data as $id) { // not use if (! in_array($id, $ProductIngredient)) { ProductIngredient::create([ 'product_id' => $this->model->id, 'ingredient_id' => $id, ]); } } } return true; } /** * 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) { 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) { ProductAttribute::create([ 'product_id' => $this->model->id, 'attribute_id' => $id, ]); } } 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(); // categories foreach ($model->categories as $category) { ProductCategory::create([ 'product_id' => $this->model->id, 'category_id' => $category->category_id, ]); } // attributes foreach ($model->attributes as $attribute) { ProductAttribute::create([ 'product_id' => $this->model->id, 'attribute_id' => $attribute->attribute_id, ]); } // images foreach ($model->images as $image) { $name = \App\Services\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 ); ProductImage::create([ 'product_id' => $this->model->id, 'filename' => $name, 'original_name' => $image->original_name, 'ext' => $image->ext, 'mine' => $image->mine, 'size' => $image->size, ]); } return $this->model; } public function delete() {} }