public static function getSmallestPrice($product, $includeSize = false) { $sizew = $sizeh = $bleed = 0; if (!isset($product->size_lists)) { $smallestSize = SizeList::where('product_id', $product->id)->cacheTags(['size_lists', 'products'])->orderBy('size_lists.sizew', 'asc')->orderBy('size_lists.sizeh', 'asc')->remember(30)->first(); if (is_object($smallestSize)) { $sizew = $smallestSize->sizew; $sizeh = $smallestSize->sizeh; } } else { $min = []; foreach ($product->size_lists as $size_list) { if (!isset($min->sizew) || $min->sizew > $size_list->sizew) { $min = $size_list; } } } $smallestDepth = ProductOption::select('name')->whereIn('id', function ($query) use($product) { $query->select('optionable_id')->from('optionables')->where('product_id', $product->id)->where('optionable_type', 'ProductOption'); })->whereIn('option_group_id', function ($query) { $query->select('id')->from('option_groups')->where('key', 'depth'); })->orderBy('key', 'asc')->first(); if (is_object($smallestDepth)) { $bleed = (double) str_replace('" Box', '', $smallestDepth->name); } if ($sizew && $sizeh || $bleed) { $product->sizew = $sizew; $product->sizeh = $sizeh; $product->bleed = $bleed; $price = JTProduct::getPrice($product); $sellPrice = $price['sell_price']; } else { $sellPrice = JTProduct::getDefaultPrice($product); } if ($includeSize) { return ['sell_price' => $sellPrice, 'sizew' => $sizew, 'sizeh' => $sizeh]; } return $sellPrice; }
public function updateProduct() { if (Input::has('pk')) { if (!Request::ajax()) { return App::abort(404); } return self::updateQuickEdit(); } $prevURL = Request::header('referer'); if (!Request::isMethod('post')) { return App::abort(404); } if (Input::has('id')) { $create = false; try { $product = Product::findorFail((int) Input::get('id')); } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) { return App::abort(404); } $message = 'has been updated successful'; } else { $create = true; $product = new Product(); $message = 'has been created successful'; } if (!Input::has('categories')) { return Redirect::to($prevURL)->with('flash_error', ['Please choose at least one category.'])->withInput(); } $product->name = Input::get('name'); $product->short_name = Str::slug($product->name); $product->sku = Input::get('sku'); $product->sell_price = Input::has('sell_price') ? round((double) str_replace(',', '', Input::get('sell_price')), 2) : 0; $product->margin_up = (double) Input::get('margin_up'); $product->product_type_id = (int) Input::get('product_type_id'); $product->order_no = (int) Input::get('order_no'); $product->custom_size = Input::has('custom_size') ? 1 : 0; $product->active = Input::has('active') ? 1 : 0; $product->meta_title = e(Input::get('meta_title')); $product->meta_description = e(Input::get('meta_description')); $product->short_description = e(Input::get('short_description')); $product->description = Input::get('description'); $product->default_view = json_encode(Input::has('default_view') ? Input::get('default_view') : []); $product->svg_layout_id = Input::has('svg_layout_id') ? Input::get('svg_layout_id') : 0; $pass = $product->valid(); if (Input::hasFile('svg_file')) { $file = Input::file('svg_file'); // if( $file->getMimeType() === 'image/svg+xml' ) { $path = public_path() . DS . 'assets' . DS . 'svg'; if (!File::exists($path)) { File::makeDirectory($path); } $fileName = md5($file->getClientOriginalName()) . '.svg'; if ($file->move($path, $fileName)) { if (isset($product->svg_file) && File::exists(public_path() . DS . $product->svg_file)) { File::delete($product->svg_file); } $product->svg_file = 'assets/svg/' . $fileName; } /*} else { return Redirect::to($prevURL)->with('flash_error',['SVG file is not valid.'])->withInput(); }*/ } if ($pass->passes()) { $product->save(); // Category, Option Group, Option ================================================================== $arr = ['categories' => 'categories', 'optionGroups' => 'option_group_id', 'options' => 'option_id']; foreach ($arr as $key => $value) { $old = $product->{$key}->toArray(); $arrData = Input::has($value) ? Input::get($value) : []; $arrOld = $remove = $add = []; if (!empty($old)) { foreach ($old as $val) { $arrOld[] = $val['id']; if (!in_array($val['id'], $arrData)) { $remove[] = $val['id']; } } } foreach ($arrData as $id) { if (!in_array($id, $arrOld)) { $add[] = $id; } } if (!empty($remove)) { $product->{$key}()->detach($remove); } if (!empty($add)) { $product->{$key}()->attach($add); } } // End ============================================================================================== // Images =========================================================================================== $path = public_path() . DS . 'assets' . DS . 'images' . DS . 'products'; $removeImages = $createImages = []; if (Input::has('images')) { $images = Input::get('images'); foreach ($images as $key => $image) { $data = ['main' => 0, 'view' => []]; if (isset($image['delete']) && $image['delete']) { $removeImages[] = $image['id']; unset($images[$key]); continue; } if (isset($image['view']) && !empty($image['view'])) { foreach ($image['view'] as $optionGroupID => $optionID) { if (!$optionID) { continue; } $data['view'][$optionID] = $optionID; } $data['view'] = array_values($data['view']); } if (isset($image['main'])) { $data['main'] = 1; } $data = json_encode($data); $image_id = 0; if (Input::hasFile("images.{$key}.file")) { $file = Input::file("images.{$key}.file"); $image_id = VIImage::upload($file, $path, 500); } else { if (isset($image['choose_image'])) { $image_id = $image['choose_image']; } } if ($image_id != 0) { if (isset($image['id'])) { $product->images()->updateExistingPivot($image['id'], ['image_id' => $image_id, 'option' => $data]); } else { $createImages[$image_id] = ['option' => $data, 'imageable_id' => $product->id, 'imageable_type' => 'Product']; } } else { if (isset($image['new'])) { continue; } else { $product->images()->updateExistingPivot($image['id'], ['option' => $data]); } } } if (!empty($removeImages)) { $product->images()->detach($removeImages); } if (!empty($createImages)) { $product->images()->attach($createImages); } } // End ============================================================================================== // Price breaks ===================================================================================== $removePB = $createPB = []; if (Input::has('price_breaks')) { $price_breaks = Input::get('price_breaks'); foreach ($price_breaks as $id => $pk) { $pk['sell_price'] = str_replace(',', '', $pk['sell_price']); if (isset($pk['id'])) { if (isset($pk['delete']) && $pk['delete']) { $removePB[] = $pk['id']; } else { $product->priceBreaks()->where('price_breaks.id', $pk['id'])->update(['range_from' => (int) $pk['range_from'], 'range_to' => (int) $pk['range_to'], 'sell_price' => (double) $pk['sell_price']]); } } else { $createPB[] = new PriceBreak(['range_from' => (int) $pk['range_from'], 'range_to' => (int) $pk['range_to'], 'sell_price' => (double) $pk['sell_price']]); } } if (!empty($removePB)) { PriceBreak::destroy($removePB); } if (!empty($createPB)) { $product->priceBreaks()->saveMany($createPB); } } // End ============================================================================================== // Price lists ====================================================================================== $removeSL = $createSL = []; if (Input::has('price_lists')) { $size_lists = Input::get('price_lists'); foreach ($size_lists as $id => $sl) { $arr = []; foreach (['sizew', 'sizeh', 'cost_price', 'sell_price', 'bigger_price', 'sell_percent', 'bigger_percent'] as $float) { $sl[$float] = (double) str_replace(',', '', $sl[$float]); $arr[$float] = $sl[$float]; } $arr['default'] = 0; if (isset($sl['default'])) { $arr['default'] = 1; } if (isset($sl['id'])) { if (isset($sl['delete']) && $sl['delete']) { $removeSL[] = $sl['id']; } else { $product->sizeLists()->where('size_lists.id', $sl['id'])->update($arr); } } else { $createSL[] = new SizeList($arr); } } if (!empty($removeSL)) { SizeList::destroy($removeSL); } if (!empty($createSL)) { $product->sizeLists()->saveMany($createSL); } } // End ============================================================================================== if (Input::has('continue')) { if ($create) { $prevURL = URL . '/admin/products/edit-product/' . $product->id; } return Redirect::to($prevURL)->with('flash_success', "<b>{$product->name}</b> {$message}."); } return Redirect::to(URL . '/admin/products')->with('flash_success', "<b>{$product->name}</b> {$message}."); } return Redirect::to($prevURL)->with('flash_error', $pass->messages()->all())->withInput(); }