예제 #1
0
 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;
 }
예제 #2
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) {
         $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;
     }
 }
예제 #3
0
 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);
 }