public function guardarDatos($request, $id) { $rules = ['name' => 'required', 'price' => 'required', 'id_category' => 'required']; $this->validate($request, $rules); if ($id != null) { $producto = Product::findOrFail($id); } else { $producto = new Product(); } $producto->name = $request->input('name'); $producto->description = $request->input('description'); $producto->price = $request->input('price'); $producto->id_category = $request->input('id_category'); $producto->save(); $colors = Color::orderBy('name', 'asc')->get(); foreach ($colors as $col) { $checkColor = $request->input('color' . $col->id); if (isset($checkColor)) { $products_colors = ProductColor::where('id_color', $col->id)->where('id_product', $producto->id)->first(); if (!isset($products_colors->id)) { $products_colors = new ProductColor(); } $products_colors->id_color = $col->id; $products_colors->id_product = $producto->id; $products_colors->save(); } else { $products_colors = ProductColor::where('id_color', $col->id)->where('id_product', $producto->id)->first(); if (isset($products_colors->id)) { $products_colors->delete(); } } } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->all(); $product = Product::find($data['product_id']); if (!$product) { return redirect()->back(); } //exc.: no exist product if (!$product->price && !array_key_exists('size_id', $data)) { return redirect()->back(); } //exc. no size selected if ($product->price && count($product->colors) && !array_key_exists('color_id', $data)) { return redirect()->back(); } //exc. no color selected $options = ['discount' => 0, 'product' => $product]; if (count($product->colors)) { $color = ProductColor::find($data['color_id']); if (!$color) { return redirect()->back(); } //exc. no exist color echo 'error no exist color'; // $options['color'] = $color; } if (count($product->sizes)) { $size = ProductSize::find($data['size_id']); if (!$size) { return redirect()->back(); } //exc. no exist size echo 'error no exist size'; // $options['size'] = $size; if (count($size->colors)) { if (!array_key_exists('color_id', $data)) { return redirect()->back(); } //exc. no color selected $color = ProductColor::find($data['color_id']); if (!$color) { return redirect()->back(); } //exc. no exist color echo 'error no exist color in size'; // $options['color'] = $color; } } if (isset($size)) { $options['size'] = $size; } if (isset($color)) { $options['color'] = $color; } if ($product->promoPrice() + 0) { $options['discount'] = $product->promo; $options['discount_type'] = $product->promo_type; } $price = $product->currentPrice(0); Cart::instance('shopping')->add($product->id, $product->name, array_key_exists('qty', $data) ? $data['qty'] : 1, $price, $options); return redirect()->back(); }
/** * Run the database seeds. * * @return void */ public function run() { $colors = ['Trắng', 'Đen', 'Xanh', 'Đỏ', 'Tím', 'Vàng']; foreach ($colors as $key => $color) { $productColor = ProductColor::create(['key' => Common::createKeyURL($color), 'parent_id' => 0, 'priority' => 0, 'is_publish' => 1, 'created_by' => 'phantsang', 'updated_by' => 'phantsang']); ProductColorTranslation::create(['product_color_id' => $productColor->id, 'locale' => 'vi', 'name' => $color, 'summary' => $color]); } }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { DB::transaction(function () use($id) { $user = Auth::user(); $productColor = ProductColor::findOrFail($id); $productColor->updated_by = $user->name; $productColor->deleted_by = $user->name; $productColor->key = $productColor->key . '-' . microtime(true); $productColor->save(); // soft delete $productColor->delete(); }); }
/** * Execute the job. * * @return void */ public function handle() { $filepath = 'data_upload/product_upload/' . $this->product_upload->id . '/' . $this->product_upload->filename; $contents = Storage::get($filepath); $modelnames = ['Product', 'ProductCategory', 'ProductCollection', 'ProductVariant', 'ProductColor', 'ProductMaterial']; $checked = array(); $ok = false; foreach ($modelnames as $modelname) { $checked[$modelname] = []; } $success = 0; $message = ''; $lines = explode("\n", $contents); //debug('Lines: '.count($lines)); $first_line_ok = true; $products_saved = 0; if (count($lines) < 2) { $message = trans('data_upload.line_count_too_low'); } else { foreach ($lines as $n => $line) { $line = trim(utf8_encode($line)); $fields = explode(';', $line); if ($n == 0) { if (count($fields) != config('data_upload.products.count_fields')) { $message = trans('data_upload.column_count_not_matching'); $first_line_ok = false; } } else { if ($first_line_ok) { if (count($fields) == config('data_upload.products.count_fields')) { $ok = true; $german_product_category = TextHelp::unQuote($fields[5]); $english_product_category = TextHelp::unQuote($fields[6]); $slug = trim(strtolower(str_slug($english_product_category))); if (strlen($slug) > 2) { $german_slug = strtolower(str_slug($german_product_category)); $product_category_id = 0; $dontsave_cat = false; if (array_key_exists($slug, $checked['ProductCategory'])) { $product_category_id = $checked['ProductCategory'][$slug]; $dontsave_cat = true; } else { $product_category_translation = ProductCategoryTranslation::where('slug', $slug)->first(); if ($product_category_translation) { $product_category_id = $product_category_translation->product_category_id; } } if (!$product_category_id) { $product_category = ProductCategory::create(); $product_category_id = $product_category->id; } else { $product_category = ProductCategory::find($product_category_id); } if (!$dontsave_cat) { $product_category->translateOrNew('de')->slug = $german_slug; $product_category->translateOrNew('de')->name = $german_product_category; $product_category->translateOrNew('en')->slug = $slug; $product_category->translateOrNew('en')->name = $english_product_category; $product_category->save(); } if (!array_key_exists($slug, $checked['ProductCategory'])) { $checked['ProductCategory'][$slug] = $product_category_id; } $product_collection_name = TextHelp::unQuote($fields[1]); $slug = strtolower(str_slug($product_collection_name)); $german_text = TextHelp::unQuote($fields[7]); $english_text = TextHelp::unQuote($fields[8]); $german_short_text = TextHelp::unQuote($fields[9]); $english_short_text = TextHelp::unQuote($fields[10]); $product_collection_id = 0; $dontsave_col = false; if (array_key_exists($slug . '_' . $product_category_id, $checked['ProductCollection'])) { $product_collection_id = $checked['ProductCollection'][$slug . '_' . $product_category_id]; $dontsave_col = true; } else { $product_collection = ProductCollection::whereSlug($slug)->first(); if ($product_collection) { $product_collection_id = $product_collection->id; } } if (!$product_collection_id) { $product_collection = ProductCollection::create(['product_category_id' => $product_category_id]); $product_collection_id = $product_collection->id; } else { $product_collection = ProductCollection::find($product_collection_id); } if (!$dontsave_col) { $product_collection->slug = $slug; $product_collection->translateOrNew('de')->name = $product_collection_name; $product_collection->translateOrNew('de')->description = $german_text; $product_collection->translateOrNew('de')->teaser_text = $german_short_text; $product_collection->translateOrNew('en')->name = $product_collection_name; $product_collection->translateOrNew('en')->description = $english_text; $product_collection->translateOrNew('en')->teaser_text = $english_short_text; $product_collection->save(); } if (!array_key_exists($slug . '_' . $product_category_id, $checked['ProductCollection'])) { $checked['ProductCollection'][$slug . '_' . $product_category_id] = $product_collection_id; } $product_collection_images = ProductCollectionImage::whereProductCollectionSlug($slug)->get(); if ($product_collection_images) { foreach ($product_collection_images as $product_collection_image) { $product_collection_image->product_collection_id = $product_collection_id; $product_collection_image->save(); } } $product_variant_name = TextHelp::unQuote($fields[2]); $slug = strtolower(str_slug($product_variant_name)); $dimensions = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[13])); $german_text = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[11])); $english_text = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[12])); $german_features = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[14])); $english_features = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[15])); $design_number = TextHelp::unQuote($fields[19]); if ($design_number) { //$design_number = substr($design_number, 0, strlen($design_number)-1); //$design_number = trim($design_number); } $product_variant_id = 0; $dontsave_var = false; if (array_key_exists($slug . '_' . $product_collection_id, $checked['ProductVariant'])) { $product_variant_id = $checked['ProductVariant'][$slug . '_' . $product_collection_id]; $dontsave_var = true; } else { $product_variant = ProductVariant::whereSlug($slug)->first(); if ($product_variant) { $product_variant_id = $product_variant->id; } } if (!$product_variant_id) { $product_variant = ProductVariant::create(['product_collection_id' => $product_collection_id]); $product_variant_id = $product_variant->id; } else { $product_variant = ProductVariant::find($product_variant_id); } if (!$dontsave_var) { $product_variant->design_number = $design_number; $product_variant->dimensions = $dimensions; $product_variant->slug = $slug; $product_variant->translateOrNew('de')->name = $product_variant_name; $product_variant->translateOrNew('de')->description = $german_text; $product_variant->translateOrNew('de')->features = $german_features; $product_variant->translateOrNew('en')->name = $product_variant_name; $product_variant->translateOrNew('en')->description = $english_text; $product_variant->translateOrNew('en')->features = $english_features; $product_variant->save(); } if (!array_key_exists($slug . '_' . $product_collection_id, $checked['ProductVariant'])) { $checked['ProductVariant'][$slug . '_' . $product_collection_id] = $product_variant_id; } $product_variant_images = ProductVariantImage::whereProductVariantSlug($slug)->get(); if ($product_variant_images) { foreach ($product_variant_images as $product_variant_image) { $product_variant_image->product_variant_id = $product_variant_id; $product_variant_image->save(); } } $product_color_name = TextHelp::unQuote($fields[3]); $slug = strtolower(str_slug($product_color_name)); $product_color_id = 0; $dontsave_color = false; if ($slug) { if (array_key_exists($slug, $checked['ProductColor'])) { $product_color_id = $checked['ProductColor'][$slug]; $dontsave_color = true; } else { $product_color = ProductColor::where('slug', $slug)->first(); if ($product_color) { $product_color_id = $product_color->id; } } if (!$product_color_id) { $product_color = ProductColor::create(['slug' => $slug]); $product_color_id = $product_color->id; } else { $product_color = ProductColor::find($product_color_id); } if (!$dontsave_color) { $product_color->slug = $slug; $product_color->translateOrNew('en')->name = $product_color_name; $product_color->translateOrNew('de')->name = $product_color_name; $product_color->save(); } if (!array_key_exists($slug, $checked['ProductColor'])) { $checked['ProductColor'][$slug] = $product_color_id; } } $english_name = TextHelp::unQuote($fields[17]); $german_name = TextHelp::unQuote($fields[16]); $slug = strtolower(str_slug($english_name)); $product_material_id = 0; $dontsave_mat = false; if ($slug) { if (array_key_exists($slug, $checked['ProductMaterial'])) { $product_material_id = $checked['ProductMaterial'][$slug]; $dontsave_mat = true; } else { $product_material_translation = ProductMaterialTranslation::where('name', $english_name)->first(); if ($product_material_translation) { $product_material_id = $product_material_translation->product_material_id; } } if (!$product_material_id) { $product_material = ProductMaterial::create(); $product_material_id = $product_material->id; } else { $product_material = ProductMaterial::find($product_material_id); } if (!$dontsave_mat) { $product_material->translateOrNew('en')->name = $english_name; $product_material->translateOrNew('de')->name = $german_name; $product_material->save(); } if (!array_key_exists($slug, $checked['ProductMaterial'])) { $checked['ProductMaterial'][$slug] = $product_material_id; } } $product_number = strtolower(str_slug(TextHelp::unQuote($fields[0]))); $price_brut = floatval(str_replace(',', '.', TextHelp::unQuote($fields[4]))); $merchant_price = floatval(str_replace(',', '.', TextHelp::unQuote($fields[21]))); $merchant_percentage = floatval(str_replace(',', '.', TextHelp::unQuote($fields[22]))); $available = intval(TextHelp::unQuote($fields[18])); $slug = $product_number; $product_id = 0; if (array_key_exists($slug, $checked['Product'])) { $product_id = $checked['Product'][$slug]; } else { $product = Product::where('product_number', $product_number)->first(); if ($product) { $product_id = $product->id; } } if (!$product_id) { $product = Product::create(['product_variant_id' => $product_variant_id, 'product_color_id' => $product_color_id, 'product_material_id' => $product_material_id, 'product_number' => $product_number]); $product_id = $product->id; } else { $product = Product::find($product_id); } $product->product_variant_id = $product_variant_id; $product->product_color_id = $product_color_id; $product->product_material_id = $product_material_id; $product->product_number = $product_number; $product->price_brut = $price_brut * 100; $product->merchant_price = $merchant_price * 100; $product->merchant_percentage = $merchant_percentage; $product->available = $available; $product->save(); $product_images = ProductImage::whereProductNumber($product_number)->get(); if ($product_images) { foreach ($product_images as $product_image) { $product_image->product_id = $product_id; $product_image->save(); } } $product_top_images = ProductTopImage::whereProductNumber($product_number)->get(); if ($product_top_images) { foreach ($product_top_images as $product_top_image) { $product_top_image->product_id = $product_id; $product_top_image->save(); } } $success = 1; $products_saved++; if (!array_key_exists($slug, $checked['Product'])) { $checked['Product'][$slug] = $product_id; } } } } } } } if ($success) { $product_categories = ProductCategory::all(); foreach ($product_categories as $product_category) { foreach ($product_category->product_collections as $product_collection) { foreach ($product_collection->product_variants as $product_variant) { foreach ($product_variant->products as $product) { $slug = strtolower(str_slug($product->product_number)); if (!array_key_exists($slug, $checked['Product'])) { $product->delete(); } } if (!array_key_exists($product_variant->translateOrNew('en')->slug . '_' . $product_collection->id, $checked['ProductVariant'])) { //$product_variant->delete(); } } if (!array_key_exists($product_collection->translateOrNew('en')->slug . '_' . $product_category->id, $checked['ProductCollection'])) { //$product_collection->delete(); } } if (!array_key_exists($product_category->translateOrNew('en')->slug, $checked['ProductCategory'])) { //$product_category->delete(); } } } if ($message) { $message .= ' '; } $message .= $products_saved . ' products saved.'; $this->product_upload->success = $success; $this->product_upload->message = $message; $this->product_upload->save(); $this->dispatch(new PrepareProductCsv($this->product_upload)); $this->dispatch(new SendProductUploadNotification($this->product_upload)); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if (!Auth::check()) { return redirect()->to('panel/login'); } $color = ProductColor::find($id); $existChecker = self::isColorExist($color, $id); if ($existChecker['error']) { return view('panel.errors.404', $existChecker['data']); } $color->delete(); return redirect()->back(); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // $validator = \Validator::make($request->all(), ['name' => 'required', 'sku' => 'required', 'image' => 'sometimes|image|max:3000', 'colors' => 'required_if:checkColors,on', 'inks' => 'required_if:checkInks,on', 'equipments' => 'required_if:checkEquipment,on', 'parent_id' => 'required|numeric']); if ($validator->fails()) { return redirect()->route('Products::edit', $id)->withErrors($validator)->withInput()->with('error', '¡Ops! Algo ha salido mal, por favor atiende a los siguientes mensajes:'); } $product = Product::findOrFail($id); $product->update(['name' => $request->input('name'), 'sku' => $request->input('sku'), 'stock' => $request->has('inStock') ? 1 : 0, 'parent_id' => $request->input('parent_id'), 'colors' => $request->has('checkColors') ? 1 : 0, 'ink' => $request->has('checkInks') ? 1 : 0, 'equipment' => $request->has('checkEquipment') ? 1 : 0, 'description' => empty(strip_tags($request->input('description'))) ? null : $request->input('description'), 'user_id' => Auth::id()]); if ($request->hasFile('image')) { if (File::exists($this->admin_content_path() . $this->products_path . $product->image) && File::exists($this->app_content_path() . $this->products_path . $product->image)) { File::delete($this->admin_content_path() . $this->products_path . $product->image); File::delete($this->app_content_path() . $this->products_path . $product->image); } $img = $request->file('image'); $ext = $img->getClientOriginalExtension(); $image_name = "image" . str_random(16) . "." . $ext; $product->image = $image_name; Image::make($img)->fit(300, 300)->save($this->admin_content_path() . $this->products_path . $image_name, 100); Image::make($img)->fit(300, 300)->save($this->app_content_path() . $this->products_path . $image_name, 100); } $tree = $this->tree($product, 'desc'); $product->category_id = count($tree) > 1 ? $tree[1]['id'] : $tree[0]['id']; $product->save(); if ($product->colors) { $check = ProductColor::where('product_id', $id)->get(); if (count($check) > 0) { ProductColor::where('product_id', $id)->update(['colors_ar' => $request->input('colors')]); } else { ProductColor::create(['product_id' => $product->id, 'colors_ar' => $request->input('colors')]); } } if ($product->ink) { $check = ProductInk::where('product_id', $id)->get(); if (count($check) > 0) { ProductInk::where('product_id', $id)->update(['inks_ar' => $request->input('inks')]); } else { ProductInk::create(['product_id' => $product->id, 'inks_ar' => $request->input('inks')]); } } if ($product->equipment) { $check = ProductEquip::where('product_id', $id)->get(); if (count($check) > 0) { ProductEquip::where('product_id', $id)->update(['equip_ar' => $request->input('equipments')]); } else { ProductEquip::create(['product_id' => $product->id, 'equip_ar' => $request->input('equipments')]); } } return redirect()->route('Products::edit', $id)->with('status', '¡El producto se ha actualizado exitosamente!'); }