Exemple #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param bool $id
  * @param bool $method
  *
  * @return \Illuminate\Http\Response
  */
 public function postStore()
 {
     $response['status'] = 'error';
     $response['message'] = trans('orders.not_saved');
     $cart = session()->get('cart');
     $total = session()->get('total');
     if (!empty($_POST) && !empty($cart)) {
         $error = FALSE;
         if (empty(trim(Input::get('city')))) {
             $response['message'] = trans('orders.city_required');
             $error = TRUE;
         }
         if (empty(trim(Input::get('address')))) {
             $response['message'] = trans('orders.address_required');
             $error = TRUE;
         }
         if (empty(trim(Input::get('phone')))) {
             $response['message'] = trans('orders.phone_required');
             $error = TRUE;
         }
         if (empty(trim(Input::get('last_name')))) {
             $response['message'] = trans('orders.last_name_required');
             $error = TRUE;
         }
         if (empty(trim(Input::get('name')))) {
             $response['message'] = trans('orders.name_required');
             $error = TRUE;
         }
         if (empty(trim(Input::get('email')))) {
             $response['message'] = trans('orders.email_required');
             $error = TRUE;
         } else {
             if (!filter_var(trim(Input::get('email')), FILTER_VALIDATE_EMAIL)) {
                 $response['message'] = trans('orders.invalid_email');
                 $error = TRUE;
             }
         }
         if (empty(trim(Input::get('delivery_type')))) {
             $response['message'] = trans('orders.delivery_type_required');
             $error = TRUE;
         }
         if ($error === FALSE) {
             $data = ['name' => trim(Input::get('name')), 'last_name' => trim(Input::get('last_name')), 'email' => trim(Input::get('email')), 'phone' => trim(Input::get('phone')), 'address' => trim(Input::get('address')), 'city' => trim(Input::get('city')), 'state' => trim(Input::get('state')), 'post_code' => trim(Input::get('post_code')), 'comment' => trim(Input::get('comment')), 'status' => 'pending', 'delivery_type' => Input::get('delivery_type'), 'total' => $total, 'created_at' => date('Y-m-d H:i:s'), 'cart' => json_encode($cart)];
             if (($id = Model_Orders::insertOrder($data)) > 0) {
                 if (!empty($cart) && is_array($cart)) {
                     $product_ids = [];
                     foreach ($cart as $key => $item) {
                         $product_ids[] = $item['product_id'];
                     }
                     $orig_prices = Model_Orders::getOrigPrices($product_ids);
                     foreach ($cart as $key => $item) {
                         $data = ['order_id' => $id, 'product_id' => $item['product_id'], 'size' => $item['size'], 'quantity' => intval($item['quantity']), 'original_price' => !empty($orig_prices[$item['product_id']]) ? floatval($orig_prices[$item['product_id']]) : 0, 'price' => floatval($item['price']), 'total' => floatval($item['subtotal'])];
                         //Discount
                         if (!empty($item['active_discount']) && $item['active_discount'] == TRUE) {
                             $data['discount'] = $item['discount_price'];
                             $data['total'] = $item['discount_price'];
                         }
                         //If quantity more then one
                         if (intval($data['quantity']) > 1) {
                             $data['total'] = $data['quantity'] * $data['total'];
                         }
                         if (Model_Orders::insertProduct($data) === TRUE) {
                             $product = Model_Main::getProducts($item['product_id'], ['sizes']);
                             if (!empty($product[$item['product_id']]) && is_array($product[$item['product_id']])) {
                                 $product = $product[$item['product_id']];
                             }
                             //Discard product sizes
                             if (!empty($product['sizes'])) {
                                 $product['sizes'] = json_decode($product['sizes'], TRUE);
                             }
                             foreach ($product['sizes'] as $size_name => $product_size) {
                                 if ($product_size['name'] == $item['size'] && intval($item['quantity']) > 0) {
                                     $product['sizes'][$size_name]['quantity'] = $product_size['quantity'] - $item['quantity'];
                                 }
                             }
                             $sizes = json_encode($product['sizes']);
                             if (intval($item['quantity']) > 0) {
                                 $product['quantity'] = intval($product['quantity']) - intval($item['quantity']);
                             }
                             Model_Orders::manageQuantities($product['id'], $sizes, $product['quantity']);
                         }
                     }
                 }
                 session()->forget('cart');
                 session()->forget('total');
                 session()->forget('items_count');
                 session()->put('order_id', $id);
                 $response['status'] = 'success';
                 $response['message'] = trans('orders.saved');
                 $response['id'] = $id;
                 //Send mail
                 $this->sendMail($id);
             }
         }
     }
     return response()->json($response);
 }