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; } }
/** * Order details * * @access public * @param int $id = Order ID * @return void */ public function action_order($id = false) { \View::set_global('title', 'Order'); \Config::load('order::order', 'order'); // Get current user $user = \Sentry::user(); $user_ids = array($user->id); $master_user = false; if (isset($user['metadata']['master']) && $user['metadata']['master']) { $master_user = true; } // Get all orders from subusers if this is master user if ($master_user) { $user_group = $user->groups(); $user_group = $user_group[0]; $users = \Sentry::group($user_group['id'])->users(); // Reset to empty array if there are no result found by query if (is_null($users)) { $users = array(); } foreach ($users as $subuser) { array_push($user_ids, $subuser['id']); } } // Get order $order = \Order\Model_Order::find(function ($query) use($user_ids, $id) { $query->where('id', $id); $query->and_where('user_id', 'IN', $user_ids); $query->and_where('finished', 1); }); if (!$order) { \Messages::error('Order with that ID does not exist or has been deleted.'); \Response::redirect(\Uri::front_create('user/account/orders')); } // Load order config \Config::load('order::order', 'order'); // Get order products $items = \Order\Model_Products::find(array('where' => array('order_id' => $id))); \Theme::instance()->set_partial('content', $this->view_dir . 'order')->set('items', $items, false)->set('order', $order[0], false)->set('order_status', \Config::get('order.status', array()), false)->set('user', $user, false); }
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) { // 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(); } $cart_total = 0; 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')); } $total_price = $item->totalDiscountedPrice(true); if (isset($product_data["price"]) && $product_data["price"] != 0) { $total_price = $product_data["price"] * $item->get('quantity'); } $cart_total += $total_price; } $method = \Input::post('delivery') == 'pickup' ? false : true; $shipping_price = $order->shipping_price(null, null, true, $method); $metadata = $user['metadata']; $data = array('email' => $user->get('email'), 'user_id' => $user->get('id'), 'status' => 'Pending', 'total_price' => $cart_total, 'finished' => 1, 'guest' => $metadata['guest'] ? 1 : 0, 'accepted' => $metadata['master'] == 1 ? 1 : 0, 'credit_account' => $metadata['credit_account'] == 1 ? 1 : 0, 'shipping_method' => \Input::post('delivery'), 'shipping_price' => $shipping_price, 'gst_price' => ($cart_total + $shipping_price) * 0.1); // $order->discount_amount = $item_with_discount['total_discount'];//\Cart::getTotal('price'); //\Cart::getDiscountedTotal('price');//\Cart::getTotal('price'); if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) { foreach ($billing as $key => $value) { $data[$key] = $metadata[$key]; unset($metadata[$key]); } } foreach ($metadata as $key => $value) { $data[$key] = $value; } $order->set($data); // 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')); } $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id))); if ($product_data && !$item_exists) { $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $item->get('quantity'), 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_data['price'] * $item->get('quantity'), 'attributes' => json_encode(\Product\Model_Attribute::get_combination($item->get('attributes'))), 'attributes_id' => $item->get('attributes'))); //$item->singleDiscountedPrice(true);//$item->singlePrice(true); //$item->totalDiscountedPrice(true);//$item->totalPrice(true); if (!empty($product->categories)) { $categories = array(); foreach ($product->categories as $category) { $categories[] = $category->title; } if ($categories) { $order_products->product_category = implode(',', $categories); } } // update stock quantity \Product\Model_Attribute::update_stock_quantity($item->get('attributes'), $item->get('quantity')); $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->set(array('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; } }
/** * Delete Order Product * * @param type $id = Product ID */ public function action_delete_product($order_id = false, $product_id = false) { if (is_numeric($order_id) && is_numeric($product_id)) { if ($order = \Order\Model_Order::find_one_by_id($order_id)) { if (count($order->products) > 1) { // Get product item to delete if ($product = \Order\Model_Products::find_one_by_id($product_id)) { try { if ($artworks = $product->artwork) { $ysi = \Yousendit\Base::forge(); $ysi->login(); foreach ($artworks as $artwork) { $ysi->delete_artwork($artwork->file_id); } } $product->delete(); \Messages::success('Order product successfully deleted.'); } catch (\Database_Exception $e) { \Messages::error('There was an error while trying to delete order product'); } } } else { \Messages::error('You can\'t delete all order products. Please delete order instead.'); } \Order\Model_Order::recalculate_order($order_id); } } if (is_numeric($order_id)) { \Response::redirect(\Input::referrer(\Uri::create('admin/order/update' . $order_id))); } else { \Response::redirect(\Input::referrer(\Uri::create('admin/order/list'))); } }