public function run()
 {
     $faker = Faker::create();
     foreach (range(1, 10) as $index) {
         $name = $faker->sentence;
         ProductOptionGroup::create(['name' => $name, 'key' => Str::slug($name), 'description' => $faker->paragraph]);
     }
 }
 public function editProduct($productId)
 {
     try {
         $product = Product::with('images')->with('categories')->with('optionGroups')->with('options')->with('priceBreaks')->with('sizeLists')->findorFail($productId);
     } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
         return App::abort(404);
     }
     $product = $product->toArray();
     foreach (['option_groups', 'options'] as $value) {
         $tmpData = [];
         if (!empty($product[$value])) {
             foreach ($product[$value] as $v) {
                 $tmpData[] = $v['id'];
             }
         }
         $product[$value] = $tmpData;
         unset($tmpData);
     }
     $product['sell_price'] = number_format($product['sell_price'], 2);
     $arrCategories = [];
     if (!empty($product['categories'])) {
         foreach ($product['categories'] as $category) {
             $arrCategories[] = $category['id'];
         }
     }
     $this->layout->title = 'Edit Product';
     $this->layout->content = View::make('admin.products-one')->with(['product' => $product, 'arrCategories' => ProductCategory::getSource(false, 0, true), 'arrChosenCategories' => $arrCategories, 'types' => ProductType::getSource(), 'option_groups' => ProductOptionGroup::getSource(false, true), 'layouts' => array()]);
 }
 public function caculatePrice()
 {
     $sku = Input::has('sku') ? Input::get('sku') : 'ROL-663';
     $sizew = Input::has('sizew') ? Input::get('sizew') : 0;
     $sizeh = Input::has('sizeh') ? Input::get('sizeh') : 0;
     $quantity = Input::has('order_qty') ? Input::get('order_qty') : 1;
     $product = new VIImage();
     $product->sizew = $sizew;
     $product->sizeh = $sizeh;
     $groups = ProductOptionGroup::select('id', 'name', 'key')->get();
     foreach ($groups as $value) {
         $option_key = Input::has($value->key) ? Input::get($value->key) : '';
         if ($option_key != '') {
             //echo 'option_key: '.$option_key.'<br/>';
             $obj_option = ProductOption::where('key', $option_key)->first();
             $option = floatval($obj_option->name);
             if ($value->key == 'depth') {
                 $product->bleed = $option;
             } else {
                 $key = $value->key;
                 $product->{$key} = $option;
             }
         }
     }
     // echo '<pre>';
     // print_r($product);
     // echo '</pre>';
     // exit;
     $product->quantity = $quantity;
     $product->sku = $sku;
     $price = JTProduct::getPrice($product);
     if ($product->margin_up) {
         $biggerPrice = $price['sell_price'] * (1 + $product->margin_up / 100);
         $arrReturn = ['sell_price' => $price['sell_price'], 'bigger_price' => VIImage::viFormat($biggerPrice), 'amount' => $price['sub_total']];
     } else {
         $arrReturn = ['sell_price' => $price['sell_price'], 'amount' => $price['sub_total']];
     }
     if (Request::ajax()) {
         $arrReturn = ['status' => 'ok', 'message' => '', 'data' => $arrReturn];
         $response = Response::json($arrReturn);
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     return false;
 }
 public function deleteProductOptionGroup($id)
 {
     if (Request::ajax()) {
         $arrReturn = ['status' => 'error', 'message' => 'Please refresh and try again.'];
         try {
             $optionGroup = ProductOptionGroup::findorFail($id);
         } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
             return App::abort(404);
         }
         $name = $optionGroup->name;
         if ($optionGroup->delete()) {
             $arrReturn = ['status' => 'ok', 'message' => "<b>{$name}</b> has been deleted."];
         }
         $response = Response::json($arrReturn);
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     return App::abort(404);
 }
 public function index()
 {
     $this->layout->title = 'Product Options';
     $this->layout->content = View::make('admin.product-options-all')->with(['option_group' => ProductOptionGroup::getSource(true)]);
 }