Пример #1
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);
 }
Пример #2
0
 private function add_attribute_price($post, $data)
 {
     $a_products = Model_Product_To_Groups::find_by(array('product_id' => $post['product_id']));
     if ($a_products) {
         foreach ($a_products as $o_product) {
             // Insert or update option
             $a_attr = Model_Attribute_Price::find_by(array('product_attribute_id' => $data['attr_id'], 'pricing_group_id' => $o_product->group_id));
             if ($a_attr) {
                 $insert_price = $a_attr[0];
             } else {
                 $insert_price = Model_Attribute_Price::forge();
             }
             //$insert_price->price = $post[$post['price_type'] . '_new'][$data['key']];
             //$insert_price->active = $post['active_new'][$key];
             // NRB-Gem: Set customer groups for this product
             // product_group_options.user_group_id => product_group_options.product_group_id = $o_product->group_id
             $o_group = Model_Group_Options::find_by(array('product_group_id' => $o_product->group_id, 'active' => 1), null, 1);
             $data = array('product_attribute_id' => $data['attr_id'], 'type' => $post['price_type'], 'product_group_discount_id' => $post['product_group_discount_id'], 'pricing_group_id' => $o_product->group_id, 'user_group_id' => $o_group ? $o_group[0]->user_group_id : null);
             $insert_price->set($data);
             $insert_price->save();
         }
     }
 }
Пример #3
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) {
                 // It will first check if we already have result in temporary result,
                 // and only execute query if we dont. That way we dont have duplicate queries
                 // Get product categories
                 $item->get_categories = static::lazy_load(function () use($item) {
                     $categories = Model_Product_To_Categories::find(function ($query) use($item) {
                         return $query->where('product_id', $item->id);
                     }, 'category_id');
                     if (!empty($categories)) {
                         $categories = '(' . implode(',', array_keys($categories)) . ')';
                         return Model_Category::find(function ($query) use($categories) {
                             return $query->where('id', 'IN', \DB::expr($categories));
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'categories');
                 // Get product infotabs
                 $item->get_infotabs = static::lazy_load(function () use($item) {
                     return Model_Product_To_Infotabs::find(function ($query) use($item) {
                         $query->order_by('sort', 'asc');
                         return $query->where('product_id', $item->id);
                     });
                 }, $item->id, 'infotabs');
                 // Get product groups
                 $item->get_groups = static::lazy_load(function () use($item) {
                     $groups = Model_Product_To_Groups::find(function ($query) use($item) {
                         return $query->where('product_id', $item->id);
                     }, 'group_id');
                     if (!empty($groups)) {
                         $groups = '(' . implode(',', array_keys($groups)) . ')';
                         return Model_Group::find(function ($query) use($groups, $item) {
                             $query->where('id', 'IN', \DB::expr($groups));
                             $query->order_by('sort', 'asc');
                             return $query;
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'groups');
                 // Get product groups
                 $item->get_pricing_group = static::lazy_load(function () use($item) {
                     $groups = Model_Product_To_Groups::find(function ($query) use($item) {
                         return $query->where('product_id', $item->id);
                     }, 'group_id');
                     if (!empty($groups)) {
                         $groups = '(' . implode(',', array_keys($groups)) . ')';
                         $groups = Model_Group::find(function ($query) use($groups, $item) {
                             $query->where('type', 'pricing');
                             $query->where('id', 'IN', \DB::expr($groups));
                             $query->order_by('sort', 'asc');
                             return $query;
                         });
                         return !empty($groups) ? $groups[0] : false;
                     }
                     return false;
                 }, $item->id, 'pricing_group', 'object');
                 // Get related products
                 $item->get_related_products = static::lazy_load(function () use($item) {
                     $related = Model_Product_To_Related::find(function ($query) use($item) {
                         return $query->where('product_id', $item->id);
                     }, 'related_id');
                     if (!empty($related)) {
                         $related = '(' . implode(',', array_keys($related)) . ')';
                         return Model_Product::find(function ($query) use($related, $item) {
                             $query->where('id', 'IN', \DB::expr($related));
                             $query->where('id', '<>', $item->id);
                             $query->order_by('sort', 'asc');
                             return $query;
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'related_products');
                 // Get upsell products
                 $item->get_upsell_products = static::lazy_load(function () use($item) {
                     $upsell = Model_Product_To_Upsell::find(function ($query) use($item) {
                         return $query->where('product_id', $item->id);
                     }, 'upsell_id');
                     if (!empty($upsell)) {
                         $upsell = '(' . implode(',', array_keys($upsell)) . ')';
                         return Model_Product::find(function ($query) use($upsell, $item) {
                             $query->select(Model_Product_To_Upsell::get_protected('_table_name') . '.discount', Model_Product::get_protected('_table_name') . '.*');
                             $query->join(Model_Product_To_Upsell::get_protected('_table_name'));
                             $query->on(Model_Product_To_Upsell::get_protected('_table_name') . '.upsell_id', '=', Model_Product::get_protected('_table_name') . '.id');
                             $query->where(Model_Product::get_protected('_table_name') . '.id', 'IN', \DB::expr($upsell));
                             $query->where(Model_Product::get_protected('_table_name') . '.id', '<>', $item->id);
                             $query->order_by(Model_Product::get_protected('_table_name') . '.sort', 'asc');
                             return $query;
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'upsell_products');
                 // Get content images
                 $item->get_images = static::lazy_load(function () use($item) {
                     return Model_Product_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'images');
                 // Get content files
                 $item->get_files = static::lazy_load(function () use($item) {
                     return Model_Product_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'files');
                 // Get content videos
                 $item->get_videos = static::lazy_load(function () use($item) {
                     return Model_Product_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'videos');
                 // Get content children
                 $item->get_seo = static::lazy_load(function () use($item) {
                     return Model_Product_Seo::find_one_by_content_id($item->id);
                 }, $item->id, 'seo', 'object');
                 // Get attributes
                 $item->get_attributes = static::lazy_load(function () use($item) {
                     return Model_Attribute::find(array('where' => array('product_id' => $item->id)));
                 }, $item->id, 'attributes');
                 // Get default attribute
                 $item->get_default_attributes = static::lazy_load(function () use($item) {
                     return Model_Attribute::find(array('where' => array('product_id' => $item->id, 'default' => 1, 'active' => 1)));
                 }, $item->id, 'default_attributes');
                 // Get all active prices
                 $item->get_all_prices = static::lazy_load(function () use($item) {
                     $product_attributes = Model_Attribute::find(function ($query) use($item) {
                         $query->join(Model_Attribute_Price::get_protected('_table_name'));
                         $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id');
                         $query->where('product_id', $item->id);
                         $query->and_where('attributes', '!=', '');
                         $query->and_where('active', 1);
                         return $query;
                     }, 'id');
                     return $product_attributes;
                 }, $item->id, 'unit_price');
                 // Get prices for product
                 $item->get_prices = static::lazy_load(function () use($item) {
                     $product_attributes = Model_Attribute::find(function ($query) use($item) {
                         $query->join(Model_Attribute_Price::get_protected('_table_name'));
                         $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id');
                         $query->where('product_id', $item->id);
                         return $query;
                     }, 'id');
                     return $product_attributes;
                 }, $item->id, 'unit_price');
                 // Get unit price
                 $item->get_unit_price = static::lazy_load(function () use($item) {
                     $product_attributes = Model_Attribute::find(function ($query) use($item) {
                         $query->join(Model_Attribute_Price::get_protected('_table_name'));
                         $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id');
                         $query->where('product_id', $item->id);
                         $query->and_where('type', 'unit_price');
                         $query->and_where('attributes', '!=', '');
                         $query->and_where('active', 1);
                         $query->limit(1);
                         return $query;
                     }, 'id');
                     return $product_attributes;
                 }, $item->id, 'unit_price');
                 // Get sale price
                 $item->get_sale_price = static::lazy_load(function () use($item) {
                     $product_attributes = Model_Attribute::find(function ($query) use($item) {
                         $query->join(Model_Attribute_Price::get_protected('_table_name'));
                         $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id');
                         $query->where('product_id', $item->id);
                         $query->and_where('type', 'sale_price');
                         $query->and_where('attributes', '!=', '');
                         $query->and_where('active', 1);
                         $query->limit(1);
                         return $query;
                     }, 'id');
                     return $product_attributes;
                 }, $item->id, 'sale_price');
                 // Get default price (RRP or Sale price)
                 $item->get_default_price = static::lazy_load(function () use($item) {
                     $out = array(0);
                     $product_attributes = Model_Attribute::find(function ($query) use($item) {
                         $query->join(Model_Attribute_Price::get_protected('_table_name'));
                         $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id');
                         $query->where('product_id', $item->id);
                         $query->and_where('type', 'sale_price');
                         $query->and_where('attributes', '!=', '');
                         $query->and_where('active', 1);
                         $query->and_where('default', 1);
                         $query->limit(1);
                         return $query;
                     }, 'id');
                     if ($product_attributes) {
                         $obj = reset($product_attributes);
                         if ($obj->price > 0) {
                             $out[0] = $obj->price;
                             $out[1] = 'sale_price';
                         } else {
                             $out[0] = $obj->retail_price;
                             $out[1] = 'retail_price';
                         }
                     }
                     return $out;
                 }, $item->id, 'default_price');
                 // Get active attribute group
                 $item->get_active_attribute_group = static::lazy_load(function () use($item) {
                     if ($attribute = Model_Attribute::find_one_by_product_id($item->id)) {
                         return $attribute->attribute_group;
                     }
                     return false;
                 }, $item->id, 'active_attribute_group');
                 // Get product data
                 $item->get_data = static::lazy_load(function () use($item) {
                     return \Product\Model_Product::product_data($item);
                 }, $item->id, 'data');
             }
         }
     }
     // return the result
     return $result;
 }