Example #1
0
 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 listJtProducts()
 {
     if (!Request::ajax()) {
         return App::abort(404);
     }
     $start = Input::has('start') ? (int) Input::get('start') : 0;
     $length = Input::has('length') ? Input::get('length') : 10;
     $search = Input::has('search') ? Input::get('search') : [];
     $products = JTProduct::select('_id', 'code', 'sku', 'name', 'product_type', 'product_category', 'oum', 'sell_by', 'sell_price')->where('deleted', false)->where('name', 'not regexp', '/^blank/i');
     if (!empty($search)) {
         foreach ($search as $key => $value) {
             if (empty($value)) {
                 continue;
             }
             $value = ltrim(rtrim($value));
             if ($key == 'code') {
                 $products->where($key, (int) $value);
             } else {
                 $products->where($key, 'like', '%' . $value . '%');
             }
         }
     }
     $order = Input::has('order') ? Input::get('order') : [];
     if (!empty($order)) {
         $columns = Input::has('columns') ? Input::get('columns') : [];
         foreach ($order as $value) {
             $column = $value['column'];
             if (!isset($columns[$column]['name']) || empty($columns[$column]['name'])) {
                 continue;
             }
             $products->orderBy($columns[$column]['name'], $value['dir'] == 'asc' ? 'asc' : 'desc');
         }
     }
     $count = $products->count();
     if ($length > 0) {
         $products = $products->skip($start)->take($length);
     }
     $arrProducts = $products->get()->toArray();
     $arrReturn = ['draw' => Input::has('draw') ? Input::get('draw') : 1, 'recordsTotal' => JTProduct::where('deleted', false)->count(), 'recordsFiltered' => $count, 'data' => []];
     if (!empty($arrProducts)) {
         foreach ($arrProducts as $product) {
             $arrReturn['data'][] = array(++$start, $product['_id'], isset($product['code']) ? $product['code'] : '', $product['sku'], isset($product['name']) ? $product['name'] : '', isset($product['product_type']) ? $product['product_type'] : '', isset($product['product_category']) ? $product['product_category'] : '', isset($product['oum']) ? $product['oum'] : '', isset($product['sell_by']) ? $product['sell_by'] : '', number_format((double) $product['sell_price'], 2));
         }
     }
     $response = Response::json($arrReturn);
     $response->header('Content-Type', 'application/json');
     return $response;
 }
 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;
 }