Ejemplo n.º 1
0
 /**
  * Get product data (code, price, attributes, images)
  * 
  * @access  public
  * @param   $product   object
  * @return  
  */
 public static function product_data($product = false, $attributes_json = null, $post_select = false, $attributeid = false)
 {
     if (!$product) {
         return;
     }
     $out['current_attributes'] = null;
     $out['code'] = null;
     $out['images'] = null;
     $out['select'] = null;
     $out['select_name'] = null;
     $out['sx'] = null;
     // select (attribute) order
     $out['retail_price'] = null;
     $out['sale'] = null;
     $out['price'] = 0;
     $out['price_type'] = null;
     $current_attributes = array();
     $select = array();
     $select_name = array();
     $sx = array();
     $options = array();
     if (json_decode($attributes_json) != null) {
         $current_attributes = Model_Attribute::find(array('where' => array('attributes' => $attributes_json, 'product_id' => $product->id, 'active' => 1), 'limit' => 1));
     }
     //get attributes per product
     $prod_attributes = \Product\Model_Attribute::find_by_product_id($product->id);
     $a_attribute_ids = array();
     if ($prod_attributes) {
         foreach ($prod_attributes as $attr_id) {
             array_push($a_attribute_ids, $attr_id->id);
         }
     }
     // NRB-Gem: if current URL is Pricing Group -> Products
     // Get pricing_attribute_price from current Pricing Group
     // Products can belong to multiple Pricing Groups
     $a_groups = \Sentry::user()->groups();
     if ($a_attribute_ids) {
         if (\Sentry::check()) {
             $a_cond = array(array('product_attribute_id', 'in', $a_attribute_ids));
             if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && $a_groups[0]) {
                 $a_cond['user_group_id'] = $a_groups[0]['id'];
             }
             if (\Uri::segment(3) == 'group' && \Uri::segment(4) == 'products') {
                 $a_cond['pricing_group_id'] = \Uri::segment(5);
             }
             $product_sale_price = \Product\Model_Attribute_Price::find(array('where' => $a_cond));
         }
     }
     // NRB-Gem: check if logged in user can buy product for discounted rate
     if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && !\Sentry::user()->is_admin() && $a_groups[0]) {
         $o_product_group_options = \DB::select_array(array('able_to_buy'))->from('product_group_options')->join('product_to_groups')->on('product_group_options.product_group_id', '=', 'product_to_groups.group_id')->where('product_group_options.user_group_id', $a_groups[0]['id'])->where('able_to_buy', 1)->where('product_to_groups.product_id', $product->id)->execute();
         $a_result = $o_product_group_options->as_array();
         $product_sale_price = $a_result && $a_result[0]['able_to_buy'] ? $product_sale_price : false;
     }
     // Get all product attributes
     $product_attributes = Model_Product::get_product_attributes($product);
     // Product with attributes
     if ($product_attributes) {
         // Set current attribute
         if ($current_attributes && isset($product_attributes[$current_attributes[0]->id])) {
             $current_attributes = $product_attributes[$current_attributes[0]->id];
         } elseif ($post_select && count($post_select) > 1) {
             foreach ($post_select as $sk => $sv) {
                 if (!isset($post_select_pop)) {
                     $post_select_pop = $post_select;
                     array_pop($post_select_pop);
                 }
                 $count = count($post_select_pop);
                 foreach ($product_attributes as $pk => $pv) {
                     $i = 0;
                     $not_exists = array();
                     foreach ($pv as $pkk => $pvv) {
                         if ($i >= $count) {
                             continue;
                         }
                         if (!isset($post_select_pop[$pvv->attribute->id]) || $post_select_pop[$pvv->attribute->id] != $pvv->option->id) {
                             $not_exists[] = true;
                         }
                         $i++;
                     }
                     if (empty($not_exists)) {
                         $current_attributes = $product_attributes[$pk];
                         break 2;
                     }
                 }
                 if (empty($current_attributes) && $count > 1) {
                     array_pop($post_select_pop);
                 }
             }
             if (empty($current_attributes)) {
                 $current_attributes = reset($product_attributes);
             }
         } elseif ($product->default_attributes) {
             $current_attributes = $product_attributes[reset($product->default_attributes)->id];
         } else {
             $current_attributes = reset($product_attributes);
         }
         foreach ($current_attributes as $k => $v) {
             $options[] = $v->option->id;
         }
         foreach ($product_attributes as $k => $v) {
             if (is_array($v)) {
                 foreach ($v as $kk => $vv) {
                     $select_name[$vv->attribute->id] = $vv->attribute->name != '' ? $vv->attribute->name : $vv->attribute->title;
                     $select_tmp[$k][$vv->attribute->id][$vv->option->id] = $vv->option->title;
                     foreach ($options as $option_key => $option_value) {
                         if ($kk == $option_key) {
                             $select[$vv->attribute->id][$vv->option->id] = $vv->option->title;
                             if ($vv->option->id != $option_value) {
                                 continue 3;
                             }
                         }
                     }
                 }
             }
         }
         if ($select) {
             $sx = array_keys($select_name);
         }
         $b_use_original_price = false;
         if (!empty($current_attributes)) {
             $attr_obj = $current_attributes[0]->product_attribute;
             $out['current_attributes'] = $current_attributes;
             $out['code'] = $attr_obj->product_code;
             if (!empty($attr_obj->images)) {
                 $out['images'] = $attr_obj->images;
             }
             $out['select'] = $select;
             $out['select_name'] = $select_name;
             $out['sx'] = $sx;
             $out['stock_quantity'] = $attr_obj->stock_quantity;
             $out['retail_price'] = $attr_obj->retail_price;
             // NRB-Gem: changed condition from: (!empty($product_sale_price) && $product_sale_price[0]->price != 0)
             // fix for attributes with sale price are not displayed
             if (!empty($product_sale_price)) {
                 $out['with_attr'] = array('attribute' => array());
                 foreach ($product_sale_price as $with_attr) {
                     $out['with_attr']['able_discount'][$with_attr->product_attribute_id] = $with_attr->able_discount;
                     if ($out['with_attr']['able_discount'][$with_attr->product_attribute_id]) {
                         $out['with_attr']['discount'][$with_attr->product_attribute_id] = $with_attr->discount;
                         $out['with_attr']['price'][$with_attr->product_attribute_id] = $with_attr->price;
                     }
                     $out['with_attr']['attribute'][$with_attr->product_attribute_id] = '';
                     foreach ($product_attributes as $o_prod_attr) {
                         $a_attr = array();
                         foreach ($o_prod_attr as $o_attr) {
                             if ($with_attr->product_attribute_id == $o_attr->product_attribute->id) {
                                 $a_attr[] = $o_attr->option->title;
                             }
                         }
                         $out['with_attr']['attribute'][$with_attr->product_attribute_id] .= implode(' | ', $a_attr);
                     }
                     // NRB-Gem: do not display inactive attribute
                     if (empty($out['with_attr']['attribute'][$with_attr->product_attribute_id])) {
                         unset($out['with_attr']['attribute'][$with_attr->product_attribute_id]);
                         unset($out['with_attr']['able_discount'][$with_attr->product_attribute_id]);
                         unset($out['with_attr']['discount'][$with_attr->product_attribute_id]);
                         unset($out['with_attr']['price'][$with_attr->product_attribute_id]);
                     }
                 }
                 // NRB-Gem: get default prices
                 foreach ($prod_attributes as $attr) {
                     if (isset($out['with_attr']['discount'][$attr->id]) && $out['with_attr']['discount'][$attr->id] > 0) {
                         $out['with_attr']['price'][$attr->id] = $attr->retail_price - $attr->retail_price * ($out['with_attr']['discount'][$attr->id] / 100);
                     } else {
                         if (isset($out['with_attr']['price'][$attr->id]) && !$out['with_attr']['price'][$attr->id]) {
                             $out['with_attr']['price'][$attr->id] = $attr->sale_price ? $attr->sale_price : $attr->retail_price;
                         }
                     }
                 }
                 $out['sale'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->discount);
                 $out['price'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->price);
                 $out['price_type'] = 'sale_price';
                 // NRB-Gem: Set discounted rate from product_attribute_price Product attributes
                 if ($attributes_json) {
                     foreach ($current_attributes as $o_attribute) {
                         if ($o_attribute->product_attribute->attributes == $attributes_json) {
                             if ($out['with_attr']['able_discount'][$o_attribute->product_attribute->id]) {
                                 if ($out['with_attr']['discount'][$o_attribute->product_attribute->id] > 0) {
                                     $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$o_attribute->product_attribute->id] / 100);
                                 } else {
                                     $out['price'] = $out['with_attr']['price'][$o_attribute->product_attribute->id];
                                 }
                             } else {
                                 $b_use_original_price = true;
                             }
                         }
                     }
                 }
             } else {
                 $out['price'] = \Product\Model_Product::calculate_discount($product, $attr_obj->retail_price);
                 $out['price_type'] = 'retail_price';
                 $b_use_original_price = true;
             }
             // NRB-Gem: Display original price if Discount price is not enabled (used by non-ajax request)
             if (\Uri::segment(1) == 'product' && !$attributes_json) {
                 $a_prod_attr_keys = array_keys($product_attributes);
                 $attr_id = $a_prod_attr_keys[0];
                 if (isset($out['with_attr']['able_discount'][$attr_id])) {
                     if (isset($out['with_attr']['discount']) && $out['with_attr']['discount'][$attr_id] > 0) {
                         $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$attr_id] / 100);
                     } else {
                         $out['price'] = isset($out['with_attr']['price']) ? $out['with_attr']['price'][$attr_id] : '';
                     }
                 } else {
                     $b_use_original_price = true;
                     $attributes_json = true;
                 }
             }
             // NRB-Gem: Set original discounted rate for Product attributes
             if ($b_use_original_price && $attributes_json) {
                 if ($attr_obj->sale_price > 0) {
                     $out['price'] = $attr_obj->sale_price;
                     $out['price_type'] = 'sale_price';
                 } else {
                     $out['price'] = $attr_obj->retail_price;
                     unset($out['price_type']);
                 }
             }
         }
     } else {
         $p = $product->attributes[0];
         $out['current_attributes'] = null;
         $out['code'] = $p->product_code;
         $out['images'] = $p->images;
         $out['retail_price'] = $p->retail_price;
         $out['stock_quantity'] = $p->stock_quantity;
         $out['able_discount'] = isset($product_sale_price[0]) ? $product_sale_price[0]->able_discount : 0;
         if (!empty($product_sale_price) && $product_sale_price[0]->price != 0) {
             if ($out['able_discount']) {
                 $out['sale'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->discount);
                 $out['price'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->price);
             }
             $out['price_type'] = 'sale_price';
         } else {
             $out['sale'] = isset($product_sale_price[0]) ? $product_sale_price[0]->discount : 0;
             $out['price'] = \Product\Model_Product::calculate_discount($product, $p->retail_price);
             $out['price_type'] = 'retail_price';
         }
         // NRB-Gem: Display original price if Discount price is not enabled
         if ($out['able_discount']) {
             if ($product_sale_price[0]->discount > 0) {
                 $out['price'] = $p->retail_price - $p->retail_price * ($product_sale_price[0]->discount / 100);
             }
         } else {
             if ($p->sale_price > 0) {
                 $out['price'] = $p->sale_price;
                 $out['price_type'] = 'sale_price';
             } else {
                 unset($out['price_type']);
                 $out['price'] = $p->retail_price;
             }
         }
     }
     $prod_category = Model_Product_To_Categories::find_by_product_id($product->id);
     $category = \Product\Model_Category::find_one_by_id($prod_category[0]->category_id);
     $out['category_id'] = $prod_category ? $prod_category[0]->category_id : null;
     $out['category'] = $category ? $category->title : null;
     // Product without attributes
     if (empty($out['current_attributes'])) {
         $out['code'] = $product->code;
     }
     // Set images from product if there is no images in product attribute
     if (empty($out['images']) && !empty($product->images)) {
         $out['images'] = $product->images;
     }
     return $out;
 }
Ejemplo n.º 2
0
 /**
  * Get additional content data selected
  * 
  * @param $result = Query result
  */
 public static function post_find($result)
 {
     if ($result !== null) {
         if (is_array($result)) {
             $tier_price = static::$tier_price;
             $user_group = static::$user_group;
             foreach ($result as $item) {
                 // Get all prices
                 $item->get_prices = static::lazy_load(function () use($item) {
                     return \Product\Model_Attribute_Price::find(function ($query) use($item) {
                         return $query->where('product_attribute_id', $item->id);
                     });
                 }, $item->id, 'prices');
                 // Get unit price
                 $item->get_unit_price = static::lazy_load(function () use($item, $user_group) {
                     return \Product\Model_Attribute_Price::find(function ($query) use($item, $user_group) {
                         $query->where('product_attribute_id', $item->id);
                         $query->and_where('type', 'unit_price');
                         return $query->and_where('user_group_id', $user_group);
                     });
                 }, $item->id, 'unit_price');
                 // Get sale price
                 $item->get_sale_price = static::lazy_load(function () use($item, $user_group) {
                     return \Product\Model_Attribute_Price::find(function ($query) use($item, $user_group) {
                         $query->where('product_attribute_id', $item->id);
                         $query->and_where('type', 'sale_price');
                         return $query->and_where('user_group_id', $user_group);
                     });
                 }, $item->id, 'sale_price');
                 // Get tier price
                 $item->get_tier_price = static::lazy_load(function () use($item, $user_group, $tier_price) {
                     return \Product\Model_Attribute_Price::find(function ($query) use($item, $user_group, $tier_price) {
                         $query->where('product_attribute_id', $item->id);
                         $query->and_where('type', 'tier_price');
                         $query->and_where('user_group_id', $user_group);
                         return $query->and_where('product_group_discount_id', $tier_price);
                     });
                 }, $item->id, 'tier_price');
                 // Get content images
                 $item->get_images = static::lazy_load(function () use($item) {
                     return \Product\Model_Attribute_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'images');
                 // Get attribute group
                 $item->get_attribute_group = static::lazy_load(function () use($item) {
                     // Return 0 for no attribute group
                     if ($item->attribute_group_id == 0) {
                         $tmp = new \stdClass();
                         $tmp->title = 'No Attributes';
                         return array(0 => $tmp);
                     }
                     // Return null for not yet selected group
                     return \Attribute\Model_Attribute_Group::find(function ($query) use($item) {
                         $query->where('id', $item->attribute_group_id);
                     });
                 }, $item->id, 'attribute_group');
             }
         }
     }
     // return the result
     return $result;
 }