Ejemplo n.º 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;
 }
Ejemplo n.º 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;
     }
 }
Ejemplo n.º 3
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/product/list');
     }
     // Get news item to edit
     if (!($item = Model_Product::find_one_by_id($id))) {
         \Response::redirect('admin/product/list');
     }
     \View::set_global('title', 'Edit Product');
     if (\Input::post()) {
         $val = Model_Product::validate('update');
         // Upload image and display errors if there are any
         $image = $this->upload_image();
         if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)) {
             // No previous images and image is not selected and it is required
             \Messages::error('<strong>There was an error while trying to upload content image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload content image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) {
             /** IMAGES **/
             // Get all alt texts to update if there is no image change
             foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) {
                 if (strpos($image_id, 'new_') === false) {
                     $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, '')));
                 }
             }
             // Save images if new files are submitted
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     chmod($image_data['saved_to'] . $image_data['saved_as'], 0755);
                     chmod($image_data['saved_to'] . 'large/' . $image_data['saved_as'], 0755);
                     chmod($image_data['saved_to'] . 'medium/' . $image_data['saved_as'], 0755);
                     chmod($image_data['saved_to'] . 'thumbs/' . $image_data['saved_as'], 0755);
                     $cover_count = count($item->images);
                     if (strpos($image_data['field'], 'new_') === false) {
                         // Update existing image
                         if (str_replace('image_', '', $image_data['field']) != 0) {
                             $image_id = (int) str_replace('image_', '', $image_data['field']);
                             $cover_count--;
                             $item_images[$image_id] = array('id' => $image_id, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, '')));
                             $this->delete_image(\Input::post('image_db_' . $image_id, ''));
                         }
                     } else {
                         // Save new image
                         $image_tmp = str_replace('image_new_', '', $image_data['field']);
                         $item_images[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                     }
                 }
             }
             Model_Product::bind_images($item_images);
             /** END OF IMAGES **/
             // Get POST values
             $insert = \Input::post();
             // Prepare some values
             //				$insert['published_at'] = !empty($insert['published_at']) ? strtotime($insert['published_at']) : $news->created_at;
             $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL;
             $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL;
             if ($insert['status'] != 2) {
                 unset($insert['active_from']);
                 unset($insert['active_to']);
             }
             // Check if product code is not exists
             if (!$this->check_code_exist($item->id, $insert['code'])) {
                 $item->set($insert);
                 try {
                     $item->save();
                     // First delete old categories
                     $tmp_categories = Model_Product_To_Categories::find_by_product_id($item->id);
                     foreach ($tmp_categories as $tmp_cat) {
                         $tmp_cat->delete();
                     }
                     foreach ($insert['cat_ids'] as $parent_category) {
                         $item_categories = Model_Product_To_Categories::forge(array('product_id' => $item->id, 'category_id' => $parent_category));
                         $item_categories->save();
                     }
                     /**  PROPERTY GROUPS  **/
                     // First delete old groups
                     $tmp_categories = \Product\Model_Product_To_Groups::find_by_product_id($item->id);
                     if ($tmp_categories) {
                         foreach ($tmp_categories as $tmp_cat) {
                             $tmp_cat->delete();
                         }
                     }
                     // Insert product property groups
                     $a_group_ids = isset($insert['group_ids']) ? $insert['group_ids'] : array();
                     foreach ($a_group_ids as $parent_group) {
                         $related = \Product\Model_Product_To_Groups::forge(array('group_id' => $parent_group, 'product_id' => $item->id));
                         $related->save();
                     }
                     /**  END OF: PROPERTY GROUPS  **/
                     /**  PRICING GROUPS  **/
                     // Insert product pricing group
                     if (\Input::post('group', false)) {
                         $pricing = \Product\Model_Product_To_Groups::forge(array('group_id' => \Input::post('group'), 'product_id' => $item->id));
                         $pricing->save();
                     }
                     /**  END OF: PRICING GROUPS  **/
                     \Messages::success('Product successfully updated.');
                     \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/product/list/') : \Uri::admin('current'));
                 } catch (\Database_Exception $e) {
                     // show validation errors
                     \Messages::error('<strong>There was an error while trying to update product</strong>');
                     // Uncomment lines below to show database errors
                     //$errors = $e->getMessage();
                     //\Messages::error($errors);
                 }
             } else {
                 \Messages::error('<strong>Product code already exists</strong>');
             }
         } else {
             // Delete uploaded images if there is product saving error
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     $this->delete_image($image_data['saved_as']);
                 }
             }
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update product</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $product = Model_Product::find_one_by_id($id);
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('product', $product);
 }