예제 #1
0
 public function action_create_order()
 {
     if (!\Input::is_ajax()) {
         die;
     }
     // We need order ID - save order if not exists
     $order_id = \Session::get('order.id', false);
     if (!is_numeric($order_id)) {
         $order = \Order\Model_Order::forge(array('finished' => 0));
         $order->save();
         \Session::set('order.id', $order->id);
     }
     echo $order_id;
 }
예제 #2
0
 public function action_clear_discount()
 {
     $order = \Order\Model_Order::forge();
     if (is_numeric(\Session::get('order.id'))) {
         $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
         $order->set(array('id_discount' => NULL, 'discount_amount' => 0));
         $order->save();
     }
 }
예제 #3
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;
     }
 }
예제 #4
0
 public function action_get_discount_value()
 {
     $discount_action = 'Apply';
     $valid = false;
     $f_discount = 0.0;
     $success = false;
     $grand_total = 0.0;
     $gst_price = \Input::post('gst_price');
     if (\Input::post('discount_action') == 'Remove') {
         $method = \Input::post('method') == 'pickup' ? false : true;
         $order = \Order\Model_Order::forge();
         $shipping_price = $order->shipping_price(null, null, true, $method);
         $gst_price = number_format((\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
         $grand_total = number_format(\Input::post('total_price') + $shipping_price + (\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
         $success = true;
         if (is_numeric(\Session::get('admin_order.id'))) {
             $order = \Order\Model_Order::find_one_by_id(\Session::get('admin_order.id'));
             $order->set(array('id_discount' => NULL, 'discount_amount' => 0, 'total_price' => $grand_total, 'gst_price' => $gst_price));
             $order->save();
         }
     } else {
         $discount = \Discountcode\Model_Discountcode::find_one_by_code(\Input::post('discount_code'));
         if ($discount && $discount->status == 'active') {
             // check for expiry date
             if ($discount->active_from != '0000-00-00' && $discount->active_to != '0000-00-00') {
                 $current_date = strtotime(date('Y-m-d'));
                 $from = strtotime($discount->active_from);
                 $to = strtotime($discount->active_to);
                 if ($current_date >= $from && $current_date <= $to) {
                     $valid = true;
                 }
             } else {
                 $valid = true;
             }
             if ($valid) {
                 $valid = false;
                 // check for single/multiple use
                 if ($discount->use_type == 'multi use') {
                     $valid = true;
                 } else {
                     // check if discount code was already used.
                     $order_exists = \Order\Model_Order::find_one_by_id_discount($discount->id);
                     if (is_null($order_exists)) {
                         $valid = true;
                     }
                 }
             }
             if ($valid) {
                 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();
                 }
                 $method = \Input::post('method') == 'pickup' ? false : true;
                 $shipping_price = $order->shipping_price(null, null, true, $method);
                 // compute value of discount
                 switch ($discount->type) {
                     case 'free shipping':
                         $f_discount = \Input::post('method') == 'pickup' ? 0 : $shipping_price;
                         break;
                     case 'percentage':
                         $f_discount = \Input::post('total_price') * ($discount->type_value / 100);
                         break;
                     case 'value':
                         $f_discount = $discount->type_value;
                         break;
                 }
                 $gst_price = number_format((\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
                 $grand_total = number_format(\Input::post('total_price') + $shipping_price + (\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
                 $order->set(array('finished' => 0, 'id_discount' => $discount->id, 'discount_amount' => $f_discount, 'shipping_price' => $shipping_price, 'gst_price' => $gst_price, 'total_price' => $grand_total));
                 $order->save();
                 \Session::set('admin_order.id', $order->id);
                 $discount_action = 'Remove';
                 $success = true;
             }
         }
     }
     echo json_encode(array('success' => $success, 'discount_action' => $discount_action, 'discount_amount' => $f_discount));
 }