Example #1
0
 public static function getPrice($data)
 {
     $arrReturn = ['sell_price' => 0, 'sub_total' => 0];
     if (isset($data->sku)) {
         $company = isset($data->company) ? $data->company : [];
         $sizeh = isset($data->sizeh) ? $data->sizeh : 0;
         $sizew = isset($data->sizew) ? $data->sizew : 0;
         $bleed = isset($data->bleed) ? $data->bleed : 0;
         $quantity = isset($data->quantity) ? $data->quantity : 1;
         $margin_up = isset($data->margin_up) ? $data->margin_up : 0;
         $options = [];
         $jt_product = self::select('_id', 'code', 'name', 'sku', 'sell_price', 'oum', 'oum_depend', 'sell_by', 'pricebreaks', 'sellprices', 'unit_price', 'options', 'products_upload', 'product_desciption', 'is_custom_size')->where('sku', $data->sku)->where('deleted', false)->first();
         if (is_object($jt_product)) {
             $jt_product = $jt_product->toArray();
             $jt_product['sizew_unit'] = $jt_product['sizeh_unit'] = 'in';
             $jt_product['quantity'] = $quantity;
             if (!isset($jt_product['options'])) {
                 $jt_product['options'] = [];
             }
             $jt_options = self::getOptionsByData($jt_product['options']);
             if (!empty($jt_options)) {
                 $arrOptions = isset($data->jt_options) ? $data->jt_options : [];
                 foreach ($jt_options as $key => $value) {
                     if (isset($value['deleted']) && $value['deleted']) {
                         continue;
                     }
                     if (!isset($value['product_id']) || !is_object($value['product_id'])) {
                         continue;
                     }
                     if ((!isset($value['require']) || !$value['require']) && !in_array($key, $arrOptions)) {
                         continue;
                     }
                     $tmpOpt = self::select('sku', 'code', 'name', 'sell_price', 'sell_by', 'pricebreaks', 'sellprices')->where('_id', '=', new MongoId($value['product_id']))->where('deleted', '=', false)->first()->toArray();
                     $tmpOpt['price_break'] = self::priceBreak($tmpOpt, $company);
                     $value = array_merge($value, $tmpOpt);
                     unset($tmpOpt);
                     $value['unit_price'] = isset($value['unit_price']) ? (double) $value['unit_price'] : 0;
                     $options[] = $value;
                 }
             }
             self::calJTBleed($jt_product, $options, $bleed);
             $jt_product['sizew'] = $sizew;
             $jt_product['sizeh'] = $sizeh;
             self::calPlusPrice($jt_product, $options);
             $jt_product['price_break'] = self::priceBreak($jt_product, $company);
             self::calPrice($jt_product);
             if (isset($company['net_discount'])) {
                 self::netDiscount($jt_product['sub_total'], $company['net_discount']);
             }
             if ($jt_product['total_other_line']) {
                 $jt_product['sub_total'] += $jt_product['total_other_line'];
             }
             $jt_product['sub_total'] += $jt_product['sub_total'] * $margin_up / 100;
             $jt_product['sell_price'] = $jt_product['sub_total'] / $jt_product['quantity'];
             $arrReturn = ['sell_price' => Product::viFormat($jt_product['sell_price']), 'sub_total' => Product::viFormat($jt_product['sub_total'])];
         }
     }
     return $arrReturn;
 }