public static function calculate_discount($item, $total_price = 0) { $user = false; if (\Sentry::check()) { $user = \Sentry::user(); } if ($user) { $user_group_id = $user['groups'][0]['id']; $product_groups = \Product\Model_Product_To_Groups::find_by_product_id($item->id); $_discount = 0; if ($product_groups) { foreach ($product_groups as $group) { $retail__discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $user_group_id, 'product_group_id' => $group->group_id), null, null, null); $sale__discount = \Product\Model_Group_Options::find_by(array('user_group_id' => $user_group_id, 'product_group_id' => $group->group_id), null, null, null); if ($item->price_type == 'sale_price') { $the_discount = $sale__discount[0]->sale_discount; } else { $the_discount = $retail__discounts ? $retail__discounts[0]->discount : 0; } $_discount = (int) $_discount + $the_discount; } } $total_price = $total_price ? $total_price - (int) $_discount / $total_price * 100 : 0; } return $total_price; }
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); }
public function action_update($id = false) { if (!is_numeric($id)) { \Response::redirect('admin/user/group/list'); } // Get group to edit if (!($item = \Sentry::group((int) $id))) { \Response::redirect('admin/user/group/list'); } \View::set_global('title', 'Edit Group'); // Update group details if (\Input::post('details', false)) { $val = $this->validate('update'); if ($val->run()) { try { $update = $item->update(array('name' => \Input::post('title'))); if ($update) { // Update discounts foreach (\Input::post('action') as $product_group_id => $value) { /** * OPTIONS */ $options[$product_group_id]['user_group_id'] = $item->id; $options[$product_group_id]['product_group_id'] = $product_group_id; //$options[$product_group_id]['action'] = $value; $options[$product_group_id]['able_to_buy'] = \Input::post('able_to_buy.' . $product_group_id); $options[$product_group_id]['able_to_view'] = \Input::post('able_to_view.' . $product_group_id); $options[$product_group_id]['sale_discount'] = \Input::post('sale_discount.' . $product_group_id); $options[$product_group_id]['apply_tier_to_sale'] = \Input::post('apply_tier_to_sale.' . $product_group_id); // Insert or update option $option = \Product\Model_Group_Options::find_by(array('user_group_id' => $item->id, 'product_group_id' => $product_group_id), null, null, 1); if ($option) { $option = $option[0]; $option->set($options[$product_group_id]); } else { $option = \Product\Model_Group_Options::forge($options[$product_group_id]); } $option->save(); /** * DISCOUNTS */ // Delete old discounts $all_discounts_id = array(); $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $item->id, 'product_group_id' => $product_group_id), null, null, null); //if($all_discounts) foreach($all_discounts as $discount) $discount->delete(); if ($all_discounts) { foreach ($all_discounts as $discount) { $all_discounts_id[$discount->id] = $discount; } } // Update $discounts = array(); foreach (\Input::post('qty.' . $product_group_id, array()) as $key => $value) { // Ignore discounts with same qty. Only one discount per QTY number is allowed // We will insert first QTY in list. All other will be ignired if (!isset($discounts[$product_group_id][$value])) { if (isset($all_discounts_id[$key])) { unset($all_discounts_id[$key]); } $discounts[$product_group_id][$value]['id'] = $key; $discounts[$product_group_id][$value]['user_group_id'] = $item->id; $discounts[$product_group_id][$value]['product_group_id'] = $product_group_id; $discounts[$product_group_id][$value]['qty'] = $value; $discounts[$product_group_id][$value]['discount'] = \Input::post('discount.' . $product_group_id . '.' . $key); } } // Delete if ($all_discounts_id) { foreach ($all_discounts_id as $discount) { $discount->delete(); } } if (!empty($discounts)) { foreach ($discounts[$product_group_id] as $key => $value) { $id = $discounts[$product_group_id][$key]['id']; $discount = \Product\Model_Group_Discounts::find_one_by_id($id); if ($discount) { $discount->set($discounts[$product_group_id][$key]); $discount->save(); } } } // Insert $new_discounts = array(); foreach (\Input::post('new_qty.' . $product_group_id, array()) as $key => $value) { // Ignore discounts with same qty. Only one discount per QTY number is allowed // We will insert first QTY in list. All other will be ignired if (!isset($discounts[$product_group_id][$value])) { $new_discounts[$product_group_id][$value]['user_group_id'] = $item->id; $new_discounts[$product_group_id][$value]['product_group_id'] = $product_group_id; $new_discounts[$product_group_id][$value]['qty'] = $value; $new_discounts[$product_group_id][$value]['discount'] = \Input::post('new_discount.' . $product_group_id . '.' . $key); } } if (!empty($new_discounts)) { foreach ($new_discounts[$product_group_id] as $key => $value) { // Insert discount $discount = \Product\Model_Group_Discounts::forge($new_discounts[$product_group_id][$key]); $discount->save(); } } } // group was updated \Messages::success('Group successfully updated.'); \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/user/group/list/') : \Uri::admin('current')); } else { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); \Messages::error('Please try again.'); } } catch (\Sentry\SentryGroupException $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); } } else { if ($val->error() != array()) { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } } } $group = \Sentry::group((int) $id); // Find all product groups and set usergroup id if ($group->product_groups = \Product\Model_Group::find_by_type('pricing')) { foreach ($group->product_groups as $product_group) { $product_group->set_user_group_id($group->id); } } \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('group', $group); }