Example #1
0
 public function before()
 {
     parent::before();
     //Visits
     $visited = \Cookie::get('visited', false);
     if (!$visited) {
         \Dashboard::log_visitor();
         \Cookie::set("visited", true, time() + 86400);
     }
     // Cart
     \Config::load('cart', true);
     $cartManager = new \Cart\Manager(\Config::get('cart'));
     \CartManager::init($cartManager);
     \CartManager::context('Cart');
     // Set Visitors group default
     \Product\Model_Attribute::set_user_group(3);
     \Theme::instance()->active('frontend');
     \Theme::instance()->set_template($this->template);
     // Set a global variable so views can use it
     $seo = array('meta_title' => '', 'meta_description' => '', 'meta_keywords' => '', 'canonical_links' => '', 'meta_robots_index' => 1, 'meta_robots_follow' => 1);
     \View::set_global('seo', $seo, false);
     \View::set_global('theme', \Theme::instance(), false);
     \View::set_global('logged', $this->check_logged(), false);
     \View::set_global('guest', $this->check_guest(), false);
     \View::set_global('logged_type', $this->check_logged_type(), false);
 }
Example #2
0
 /**
  * Get additional content data selected
  * 
  * @param $result = Query result
  */
 public static function post_find($result)
 {
     if ($result !== null) {
         if (is_array($result)) {
             foreach ($result as $item) {
                 // Get content children
                 $item->get_attribute = static::lazy_load(function () use($item) {
                     return \Product\Model_Attribute::find(array('where' => array('id' => $item->product_attribute_id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'attribute');
             }
         }
     }
     // return the result
     return $result;
 }
Example #3
0
 public function before()
 {
     parent::before();
     // Override a package class
     \Autoloader::add_classes(array('Hybrid\\Pagination' => APPPATH . 'core/pagination.php'));
     // Cart
     \Config::load('cart', true);
     $cartManager = new \Cart\Manager(\Config::get('cart'));
     \CartManager::init($cartManager);
     \CartManager::context('Cart');
     // Set Visitors group default
     \Product\Model_Attribute::set_user_group(3);
     \Theme::instance()->active('frontend');
     \Theme::instance()->set_template($this->template);
     // Set a global variable so views can use it
     \View::set_global('theme', \Theme::instance(), false);
     \View::set_global('logged', $this->check_logged(), false);
     \View::set_global('guest', $this->check_guest(), false);
     \View::set_global('logged_type', $this->check_logged_type(), false);
     \View::set_global('my_categories', $this->my_categories(), false);
     // Pagination
     \Config::load('pagination', true);
 }
 public function savePaymentDetails($type, $order, $status, $statusDetail, $response = array())
 {
     // start - update number of stock for successful payment
     if ($status == 'Completed') {
         foreach ($order->products as $product) {
             if ($product->attributes_id) {
                 $get_attributes = \Product\Model_Attribute::find_one_by(array('product_id' => $product->product_id, 'attributes' => $product->attributes_id));
                 $get_attributes->set(array('stock_quantity' => $get_attributes->stock_quantity - $product->quantity));
                 $get_attributes->save();
             } else {
                 $get_attributes = \Product\Model_Attribute::find_one_by_product_id($product->product_id);
                 $get_attributes->set(array('stock_quantity' => $get_attributes->stock_quantity - $product->quantity));
                 $get_attributes->save();
             }
         }
     }
     // end - update number of stock for successful payment
     $exists = \Payment\Model_Payment::find_one_by_order_id($order->id);
     if ($exists) {
         $this->payment = $exists;
     }
     $this->payment->set(array('order_id' => $order->id, 'total_price' => $order->total_price + $order->shipping_price - $order->discount_amount, 'method' => $type, 'status' => $status, 'status_detail' => $statusDetail, 'response' => json_encode($response)));
     $this->savePayment();
 }
Example #5
0
 public function action_get_image_box($id = null, $key = null, $attribute_group = 0)
 {
     $item = null;
     if (is_numeric($id) && $id > 0) {
         $item = \Product\Model_Attribute::find_one_by_id($id);
     }
     echo \Theme::instance()->view($this->view_dir . '_image_box')->set('item', $item)->set('key', $key)->set('attribute_group', $attribute_group);
     exit;
 }
Example #6
0
 public function action_products_assigned($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/product/group/list');
     }
     // Get group to edit
     if (!($item = Model_Group::find_one_by_id($id))) {
         \Response::redirect('admin/product/group/list');
     }
     \View::set_global('title', 'Edit Group Products');
     // Update group products
     if (\Input::post()) {
         $add = \Input::post('products.add', array());
         $remove = \Input::post('products.remove', array());
         $update = \Input::post('products.update', array());
         if (\Input::post('add', false)) {
             foreach ($add as $value) {
                 $related = Model_Product_To_Groups::forge(array('group_id' => $item->id, 'product_id' => $value));
                 $related->save();
                 // NRB-Gem: Get current Customer Group for this Pricing Group
                 $product_group_option = \Product\Model_Group_Options::find_one_by(array('active' => 1, 'product_group_id' => $id));
                 $user_group_id = null;
                 if ($product_group_option) {
                     $user_group_id = $product_group_option->user_group_id;
                 }
                 // NRB-Gem: add attributes
                 $a_attributes = \Product\Model_Attribute::find(function ($query) use($value) {
                     $query->where('product_id', '=', $value);
                     $query->where('active', '=', 1);
                 });
                 $a_attribute_ids = array();
                 if (count($a_attributes)) {
                     foreach ($a_attributes as $o_attr) {
                         $o_attr_price = new Model_Attribute_Price(array('product_attribute_id' => $o_attr->id, 'pricing_group_id' => $item->id, 'user_group_id' => $user_group_id, 'type' => 'sale_price'));
                         $o_attr_price->save();
                     }
                 }
             }
             \Messages::success('Products successfully added to group.');
         } else {
             if (\Input::post('remove', false)) {
                 foreach ($remove as $value) {
                     $related = Model_Product_To_Groups::find_one_by(array(array('group_id', '=', $item->id), array('product_id', '=', $value)));
                     if (!is_null($related)) {
                         $related->delete();
                         // NRB-Gem: remove attributes
                         $a_attributes = \Product\Model_Attribute::find(function ($query) use($value) {
                             $query->where('product_id', '=', $value);
                         });
                         $a_attribute_ids = array();
                         if (count($a_attributes)) {
                             foreach ($a_attributes as $o_attr) {
                                 $a_attribute_ids[] = $o_attr->id;
                             }
                         }
                         if (count($a_attribute_ids)) {
                             \DB::delete('product_attribute_price')->where('product_attribute_id', 'IN', \DB::expr('(' . implode(',', $a_attribute_ids) . ')'))->where('pricing_group_id', '=', $item->id)->execute();
                         }
                     }
                 }
                 \Messages::success('Products successfully removed from group.');
             }
         }
         if (!empty($update)) {
             foreach ($update['discount'] as $key => $value) {
                 if (isset($update['attr_id'][$key])) {
                     foreach ($update['attr_id'][$key] as $attribute_id) {
                         $update_sale_price = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '=', $item->id)));
                         if ($update_sale_price) {
                             $update_sale_price->set(array('discount' => $update['discount'][$key][$attribute_id], 'able_discount' => $update['able_discount'][$key][$attribute_id], 'price' => $update['price'][$key][$attribute_id]));
                             $update_sale_price->save();
                         }
                     }
                 } else {
                     $prod_attributes = \Product\Model_Attribute::find_by_product_id($key);
                     $attribute_id = $prod_attributes[0]->id;
                     $o_attr_price_list = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '<>', $item->id)));
                     if ($o_attr_price_list) {
                         $o_attr_price_list->delete();
                     }
                     $update_sale_price = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '=', $item->id)));
                     if ($update_sale_price) {
                         $update_sale_price->set(array('discount' => $update['discount'][$key], 'able_discount' => $update['able_discount'][$key], 'price' => $update['price'][$key]));
                         $update_sale_price->save();
                     }
                 }
             }
             \Messages::success('Products successfully updated.');
         }
         // if(\Input::is_ajax())
         // {
         // 	echo \Messages::display('left', false);
         // 	exit;
         // }
         // else
         // {
         \Response::redirect(\Input::referrer(\Uri::admin('current')));
         // }
     }
     $group = Model_Group::find_one_by_id($id);
     /************ Get non related infotabs ***********/
     $items = Model_Product::find(function ($query) use($item) {
         $product_ids = array();
         // NRB-Gem: Comment out, to enable multiple pricing groups for a product
         // Products can be in only ONE discounted group
         // But can exist in MULTIPLE standard groups
         // if($item->type == 'pricing')
         // {
         // 	$discounted_products = Model_Product::in_group_type('pricing');
         // 	foreach($discounted_products as $product)
         // 		array_push($product_ids, $product->id);
         // }
         foreach ($item->products as $product) {
             array_push($product_ids, $product->id);
         }
         if (!empty($product_ids)) {
             $product_ids = '(' . implode(',', $product_ids) . ')';
             $query->where('id', 'NOT IN', \DB::expr($product_ids));
         }
         // Filters
         foreach (\Input::get() as $key => $value) {
             if (!empty($value) || $value == '0') {
                 switch ($key) {
                     case 'title':
                         $query->where($key, 'like', "%{$value}%");
                         break;
                     case 'status':
                         if (is_numeric($value)) {
                             $query->where($key, $value);
                         }
                         break;
                     case 'category_id':
                         if (is_numeric($value)) {
                             $products = Model_Product_To_Categories::find(function ($query) use($value) {
                                 return $query->where('category_id', $value);
                             }, 'product_id');
                             if (empty($products)) {
                                 $products = array(0);
                             }
                             $query->where('id', 'IN', array_keys($products));
                         }
                         break;
                 }
             }
         }
         // Order query
         $query->order_by('sort', 'asc');
         $query->order_by('id', 'asc');
     });
     $group->not_related_products = null;
     $not_related_products = $items ? $items : array();
     // product_group_options
     $customer_groups = Model_Group_Options::find_by(array('active' => 1, 'product_group_id' => $id));
     $customer_group = array();
     if ($customer_groups) {
         $customer_group = Model_Customer_Group::find_one_by_id($customer_groups[0]->user_group_id);
     }
     // Reset to empty array if there are no result found by query
     if (is_null($items)) {
         $items = array();
     }
     // Initiate pagination
     $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null));
     // Remove unwanted items, and show only required ones
     $items = array_slice($items, $pagination->offset, $pagination->per_page);
     $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period');
     \Theme::instance()->set_partial('content', $this->view_dir . 'products_assigned')->set('group', $group, false)->set('items', $items, false)->set('pagination', $pagination, false)->set('status', $status, false)->set('customer_group', $customer_group, false);
 }
 protected function save_order()
 {
     if (!$this->check_logged()) {
         \Messages::error('You must be logged in if you want to continue with your order.');
         \Response::redirect(\Uri::create('order/checkout/address'));
     }
     // Save order
     $user = false;
     $order = false;
     $items = \Cart::items();
     if (\Sentry::check()) {
         $user = \Sentry::user();
     }
     if (\Input::post() && $items && $user) {
         $group_id = $user['groups'][0]['id'];
         $item_with_discount = array();
         foreach ($items as $item) {
             $id = $item->get('id');
             $product_groups = \Product\Model_Product_To_Groups::find_by_product_id($item->get('id'));
             foreach ($product_groups as $group) {
                 $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $group_id, 'product_group_id' => $group->group_id), null, null, null);
                 foreach ($all_discounts as $discount) {
                     $discount = (int) $item_with_discount[$id]['discount'] + $discount->discount;
                     $sub_total = $item->totalPrice(true) - (int) $discount / $item->totalPrice(true) * 100;
                     $item_with_discount[$id] = array('product_group_id' => $group->product_id, 'user_group_id' => $group->group_id, 'discount' => $discount, 'sub_total' => $sub_total);
                 }
             }
             $item_with_discount['total_discount'] = (int) $item_with_discount['total_discount'] + (int) $item_with_discount[$id]['total_discount'];
             $item_with_discount['total_price'] = (double) $item_with_discount['total_price'] + (double) $item_with_discount[$id]['sub_total'];
         }
         // check for a valid CSRF token
         if (!\Security::check_token()) {
             \Messages::error('CSRF attack or expired CSRF token.');
             \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost')));
         }
         try {
             // Update or create order
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $shipping_price = $order->shipping_price(null, null, true);
             $metadata = $user['metadata'];
             if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) {
                 foreach ($billing as $key => $value) {
                     $order->{$key} = $metadata[$key];
                     unset($metadata[$key]);
                 }
             }
             foreach ($metadata as $key => $value) {
                 $order->{$key} = $value;
             }
             $order->email = $user->get('email');
             $order->user_id = $user->get('id');
             $order->status = 'Pending';
             $order->discount_amount = $item_with_discount['total_discount'];
             //\Cart::getTotal('price');
             $order->total_price = $item_with_discount['total_price'];
             //\Cart::getTotal('price');
             $order->finished = 1;
             $order->guest = $metadata['guest'] ? 1 : 0;
             $order->accepted = $metadata['master'] == 1 ? 1 : 0;
             $order->credit_account = $metadata['credit_account'] == 1 ? 1 : 0;
             $order->shipping_price = $shipping_price;
             // Save order, add products to order products
             if ($order->save()) {
                 foreach ($items as $item) {
                     $product_data = null;
                     if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                         $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                     }
                     if ($product_data) {
                         $order_products = \Order\Model_Products::forge();
                         $order_products->order_id = $order->id;
                         $order_products->title = $product->title;
                         $order_products->code = $product_data['code'];
                         $order_products->price = $item->singlePrice(true);
                         $order_products->price_type = $product_data['price_type'];
                         $order_products->quantity = $item->get('quantity');
                         $order_products->product_id = $product->id;
                         $order_products->artwork_required = $product->artwork_required;
                         $order_products->artwork_free_over = $product->artwork_free_over;
                         $order_products->subtotal = $item_with_discount[$item->get('id')]['sub_total'];
                         //$item->totalPrice(true);
                         $order_products->attributes = json_encode(\Product\Model_Attribute::get_combination($item->get('attributes')));
                         if (!empty($product->categories)) {
                             $categories = array();
                             foreach ($product->categories as $category) {
                                 $categories[] = $category->title;
                             }
                             if ($categories) {
                                 $order_products->product_category = implode(',', $categories);
                             }
                         }
                         $order_products->save();
                         // Find artworks
                         if ($unique_id = $item->get('unique_id')) {
                             if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) {
                                 $ysi = \Yousendit\Base::forge();
                                 // Artworks (update, delete)
                                 foreach ($artworks as $artwork) {
                                     // Remove deleted artwork
                                     if ($artwork->deleted_at > 0) {
                                         $ysi->delete_artwork($artwork->file_id);
                                         $artwork->delete();
                                     } else {
                                         $artwork->order_product_id = $order_products->id;
                                         $artwork->save();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($order) {
                 return $order;
             } else {
                 return false;
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('There was an error while trying to save your order.');
             // Uncomment lines below to show database errors
             $errors = $e->getMessage();
             \Messages::error($errors);
             \Response::redirect(\Uri::create('order/checkout/cost'));
         }
         return false;
     }
 }
Example #8
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;
 }
Example #9
0
 protected function save_order()
 {
     if (!$this->check_logged()) {
         \Messages::error('You must be logged in if you want to continue with your order.');
         \Response::redirect(\Uri::create('order/checkout/address'));
     }
     // Save order
     $user = false;
     $order = false;
     $items = \Cart::items();
     if (\Sentry::check()) {
         $user = \Sentry::user();
     }
     if (\Input::post() && $items && $user) {
         // check for a valid CSRF token
         if (!\Security::check_token()) {
             \Messages::error('CSRF attack or expired CSRF token.');
             \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost')));
         }
         try {
             // Update or create order
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $cart_total = 0;
             foreach ($items as $item) {
                 $product_data = null;
                 if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                     $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                 }
                 $total_price = $item->totalDiscountedPrice(true);
                 if (isset($product_data["price"]) && $product_data["price"] != 0) {
                     $total_price = $product_data["price"] * $item->get('quantity');
                 }
                 $cart_total += $total_price;
             }
             $method = \Input::post('delivery') == 'pickup' ? false : true;
             $shipping_price = $order->shipping_price(null, null, true, $method);
             $metadata = $user['metadata'];
             $data = array('email' => $user->get('email'), 'user_id' => $user->get('id'), 'status' => 'Pending', 'total_price' => $cart_total, 'finished' => 1, 'guest' => $metadata['guest'] ? 1 : 0, 'accepted' => $metadata['master'] == 1 ? 1 : 0, 'credit_account' => $metadata['credit_account'] == 1 ? 1 : 0, 'shipping_method' => \Input::post('delivery'), 'shipping_price' => $shipping_price, 'gst_price' => ($cart_total + $shipping_price) * 0.1);
             // $order->discount_amount = $item_with_discount['total_discount'];//\Cart::getTotal('price');
             //\Cart::getDiscountedTotal('price');//\Cart::getTotal('price');
             if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) {
                 foreach ($billing as $key => $value) {
                     $data[$key] = $metadata[$key];
                     unset($metadata[$key]);
                 }
             }
             foreach ($metadata as $key => $value) {
                 $data[$key] = $value;
             }
             $order->set($data);
             // Save order, add products to order products
             if ($order->save()) {
                 foreach ($items as $item) {
                     $product_data = null;
                     if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                         $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                     }
                     $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id)));
                     if ($product_data && !$item_exists) {
                         $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $item->get('quantity'), 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_data['price'] * $item->get('quantity'), 'attributes' => json_encode(\Product\Model_Attribute::get_combination($item->get('attributes'))), 'attributes_id' => $item->get('attributes')));
                         //$item->singleDiscountedPrice(true);//$item->singlePrice(true);
                         //$item->totalDiscountedPrice(true);//$item->totalPrice(true);
                         if (!empty($product->categories)) {
                             $categories = array();
                             foreach ($product->categories as $category) {
                                 $categories[] = $category->title;
                             }
                             if ($categories) {
                                 $order_products->product_category = implode(',', $categories);
                             }
                         }
                         // update stock quantity
                         \Product\Model_Attribute::update_stock_quantity($item->get('attributes'), $item->get('quantity'));
                         $order_products->save();
                         // Find artworks
                         if ($unique_id = $item->get('unique_id')) {
                             if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) {
                                 $ysi = \Yousendit\Base::forge();
                                 // Artworks (update, delete)
                                 foreach ($artworks as $artwork) {
                                     // Remove deleted artwork
                                     if ($artwork->deleted_at > 0) {
                                         $ysi->delete_artwork($artwork->file_id);
                                         $artwork->delete();
                                     } else {
                                         $artwork->set(array('order_product_id' => $order_products->id));
                                         $artwork->save();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($order) {
                 return $order;
             } else {
                 return false;
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('There was an error while trying to save your order.');
             // Uncomment lines below to show database errors
             $errors = $e->getMessage();
             \Messages::error($errors);
             \Response::redirect(\Uri::create('order/checkout/cost'));
         }
         return false;
     }
 }
Example #10
0
 public function action_delete($id = false)
 {
     if (is_numeric($id)) {
         // Get news item to edit
         if ($item = Model_Attribute_Option::find_one_by_id($id)) {
             // Delete item
             try {
                 $item->delete();
                 // NRB-Gem: remove from product_attributes and product_attribute_price
                 $a_attr = \Product\Model_Attribute::find_by(array(array('attributes', 'like', '%"' . $item->attribute_id . '":"' . $id . '"%')));
                 $a_attr_id = array();
                 foreach ($a_attr as $o_attr) {
                     $a_attr_id[] = $o_attr->id;
                 }
                 if (count($a_attr_id)) {
                     $s_ids = '(' . implode(',', $a_attr_id) . ')';
                     \DB::delete('product_attributes')->where('id', 'IN', \DB::expr($s_ids))->execute();
                     \DB::delete('product_attribute_price')->where('product_attribute_id', 'IN', \DB::expr($s_ids))->execute();
                 }
                 \Messages::success('Attribute option successfully deleted.');
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to delete attribute option</strong>');
                 // Uncomment lines below to show database errors
                 //$errors = $e->getMessage();
                 //\Messages::error($errors);
             }
         }
     }
     if (\Request::is_hmvc()) {
         \Messages::reset();
     } else {
         \Response::redirect(\Input::referrer(\Uri::create('admin/attribute/list')));
     }
 }
Example #11
0
 protected function save_order($user)
 {
     $order = false;
     try {
         // Update or create order
         if (is_numeric(\Session::get('admin_order.id'))) {
             $order = \Order\Model_Order::find_one_by_id(\Session::get('admin_order.id'));
         }
         if (!$order) {
             $order = \Order\Model_Order::forge();
         }
         $data = array('user_id' => $user->id, 'status' => \Input::post('status'), 'title' => $user->get('metadata.title'), 'first_name' => $user->get('metadata.first_name'), 'last_name' => $user->get('metadata.last_name'), 'company' => $user->get('metadata.company'), 'address' => $user->get('metadata.address'), 'address2' => null, 'suburb' => $user->get('metadata.suburb'), 'postcode' => $user->get('metadata.postcode'), 'country' => $user->get('metadata.country'), 'state' => $user->get('metadata.state'), 'phone' => $user->get('metadata.phone'), 'email' => $user->get('email'), 'guest' => 1, 'total_price' => \Input::post('products_total_hidden'), 'discount_amount' => \Input::post('discount_hidden'), 'shipping_method' => null, 'shipping_price' => \Input::post('shipping_hidden'), 'gst_price' => \Input::post('gst_amount_hidden'), 'finished' => 1, 'accepted' => 0, 'credit_account' => 0);
         $order->set($data);
         $product_id = \Input::post('product_id');
         $product_quantity = \Input::post('product_quantity');
         $product_price = \Input::post('product_price');
         // Save order, add products to order products
         if ($order->save()) {
             foreach ($product_id as $key => $item) {
                 $product_data = null;
                 if ($product = \Product\Model_Product::find_one_by_id($item)) {
                     $product_data = \Product\Model_Product::product_data($product, null);
                 }
                 $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id)));
                 if ($product_data && !$item_exists) {
                     $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $product_quantity[$key], 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_price[$key] * $product_quantity[$key], 'attributes' => json_encode(\Product\Model_Attribute::get_combination(0))));
                     if (!empty($product->categories)) {
                         $categories = array();
                         foreach ($product->categories as $category) {
                             $categories[] = $category->title;
                         }
                         if ($categories) {
                             $order_products->product_category = implode(',', $categories);
                         }
                     }
                     // update stock quantity
                     \Product\Model_Attribute::update_stock_quantity(0, $product_quantity[$key]);
                     $order_products->save();
                 }
             }
         }
         if ($order) {
             return $order;
         } else {
             return false;
         }
     } catch (\Database_Exception $e) {
         // show validation errors
         \Messages::error('There was an error while trying to save your order.');
         // Uncomment lines below to show database errors
         $errors = $e->getMessage();
         \Messages::error($errors);
         \Response::redirect(\Uri::create('admin/order/create'));
     }
     return false;
 }
Example #12
0
 public static function get_product_code($id)
 {
     $product_attribute = \Product\Model_Attribute::find(array('where' => array(array('id', '=', $id))));
     return $product_attribute[0]['product_code'];
 }
                ?>
                                                            <tr>
                                                                <td><?php 
                echo $i_ctr == 0 ? \Form::checkbox('products[remove][]', $related->id) : '';
                ?>
</td>
                                                                <td colspan="2"><?php 
                echo $i_ctr == 0 ? $related->title : '';
                ?>
</td>
                                                                <td><?php 
                echo $i_ctr == 0 ? $product_data['category'] : '';
                ?>
</td>
                                                                <td><?php 
                echo $i_ctr == 0 ? $product_data['code'] : \Product\Model_Attribute::get_product_code($key);
                ?>
</td>
                                                                <td><?php 
                echo $attribute;
                ?>
</td>
                                                                <td>
                                                                    <div class="custom_checkbox">
                                                                        <?php 
                echo \Form::checkbox('products[update][able_discount][' . $related->id . '][' . $key . ']', 1, $product_data['with_attr']['able_discount'][$key]);
                ?>
                                                                    </div>
                                                                </td>
                                                                <td><input style="width: 50%;" type="text" name="products[update][discount][<?php 
                echo $related->id;