Ejemplo n.º 1
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.º 2
0
 public function action_artwork_uploaded()
 {
     $artwork_table = '';
     if (\Input::post()) {
         $item = \Cart::item(\Input::post('cart_uid'));
         $artwork = \Order\Model_Artwork::find_one_by_file_id(\Input::post('file_id'));
         if ($item && $artwork) {
             $artwork_table .= '<tr>';
             $artwork_table .= '<td>';
             $artwork_table .= $artwork->type;
             $artwork_table .= '</td>';
             $artwork_table .= '<td>';
             $artwork_table .= $artwork->name;
             $artwork_table .= '</td>';
             $artwork_table .= '<td>';
             $artwork_table .= \Form::open(array('action' => \Uri::create('order/cart/cart_update'), 'method' => 'post', 'class' => 'edit_cart_form'));
             $artwork_table .= \Form::select("artwork_quantity[{$artwork->id}]", $artwork->quantity, \Helper::numbers_array(1, $item->get('quantity')), array('class' => 'select_init w_60'));
             $artwork_table .= \Form::close();
             $artwork_table .= '</td>';
             $artwork_table .= '<td>';
             $artwork_table .= '<a href="' . \Uri::create('order/cart/delete_artwork/' . $artwork->file_id) . '" class="remove_item"></a>';
             $artwork_table .= '</td>';
             $artwork_table .= '</tr>';
         }
     }
     echo $artwork_table;
 }
Ejemplo n.º 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) {
         // 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;
     }
 }
Ejemplo n.º 4
0
 public function delete_artwork($file_id = null, $return = false)
 {
     $out = array();
     $storage = new \StorageAPIs(\Session::get('ysi.sHost'), \Session::get('ysi.sApp'), \Session::get('ysi.sTr'));
     if (strlen(\Session::get('ysi.sToken')) == 0 || strlen($file_id) == 0) {
         $out = array('errorcode' => '', 'errormessage' => 'Internal server error, please try again.');
     } else {
         $storage->setAuthToken(\Session::get('ysi.sToken'));
         $responseObject = $storage->deleteFile($file_id);
         $current = $responseObject->getErrorStatus();
         if (!empty($current)) {
             $out = array('errorcode' => '', 'errormessage' => 'Internal server error, please try again.');
             //echo " Error code: ".$current->getCode(). "\n";
             //echo " Error message: ".$current->getMessage()."\n";
         } else {
             // Delete artwork from db
             if ($artwork = \Order\Model_Artwork::find_one_by_file_id($file_id)) {
                 $unique_id = $artwork->unique_id;
                 $order_id = $artwork->order_id;
                 if ($artwork->delete()) {
                     // Reorder artworks
                     if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order_id, 'deleted_at' => 0), 'order_by' => array('type' => 'asc')))) {
                         foreach ($artworks as $key => $artwork_item) {
                             $artwork_item->type = $key + 1;
                             $artwork_item->save();
                         }
                     }
                 }
             }
             $current = $responseObject->getStatus();
             if (!empty($current)) {
                 $out['data']['file_id'] = $file_id;
             } else {
                 $out['data']['file_id'] = $file_id;
             }
         }
     }
     if ($return) {
         return $out;
     }
     echo json_encode($out);
 }