/**
  * Store a newly created resource in storage.
  *
  * @param bool $id
  * @param bool $method
  *
  * @return \Illuminate\Http\Response
  */
 public function postStore($id = FALSE, $method = FALSE)
 {
     $response['status'] = 'error';
     $response['message'] = trans('orders.not_saved');
     if (!empty($_POST)) {
         if ($method == FALSE) {
             $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('delivery_type')))) {
                 $response['message'] = trans('orders.delivery_type_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('status')))) {
                 $response['message'] = trans('orders.status_required');
                 $error = TRUE;
             }
             if ($error === FALSE) {
                 $data = ['user_id' => Input::get('user_id'), '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' => Input::get('status'), 'delivery_type' => Input::get('delivery_type'), 'created_at' => Input::get('created_at')];
                 if ($id == FALSE) {
                     if (($id = Model_Orders::insertOrder($data)) > 0) {
                         $response['status'] = 'success';
                         $response['message'] = trans('orders.saved');
                         $response['id'] = $id;
                     }
                 } elseif (is_numeric($id)) {
                     if (Model_Orders::updateOrder($id, $data)) {
                         //If updated check status and if it's canceled, return product quantities
                         if (Input::get('status') == 'canceled') {
                             $records = Model_Orders::getOrderProducts($id);
                             if (!empty($records) && is_array($records)) {
                                 foreach ($records as $record) {
                                     if (!empty($record['id'])) {
                                         Model_Orders::returnProduct($record['id']);
                                     }
                                 }
                             }
                         }
                         $response['status'] = 'success';
                         $response['message'] = trans('orders.saved');
                     }
                 }
             }
         } elseif ($method == 'product') {
             $response['message'] = trans('orders.product_not_saved');
             $error = FALSE;
             if (empty($order_id = intval(trim(Input::get('order_id'))))) {
                 $error = TRUE;
             }
             if (empty($size = trim(Input::get('size')))) {
                 $error = TRUE;
             }
             if (empty($quantity = intval(trim(Input::get('quantity'))))) {
                 $error = TRUE;
             }
             if (empty($product_id = intval(trim(Input::get('product_id'))))) {
                 $error = TRUE;
             }
             if ($error === FALSE) {
                 $product = Model_Products::getProducts($product_id, ['sizes']);
                 if (!empty($product[$product_id]) && is_array($product[$product_id])) {
                     $product = $product[$product_id];
                 }
                 $data = ['order_id' => $order_id, 'product_id' => $product_id, 'size' => $size, 'quantity' => $quantity, 'original_price' => $product['original_price']];
                 //If size doesn't have price, get products one
                 if (empty($price = trim(Input::get('price')))) {
                     $data['price'] = floatval($product['price']);
                 } else {
                     $data['price'] = floatval($price);
                 }
                 $data['total'] = $data['price'];
                 //Discount
                 $discount_start = strtotime($product['discount_start']);
                 $discount_end = strtotime($product['discount_end']);
                 $now = time();
                 //If currently the product have discount
                 if ($now > $discount_start && $now < $discount_end) {
                     //If size doesn't have discount, get products one
                     if (empty($discount = trim(Input::get('discount')))) {
                         $data['discount'] = floatval($product['discount_price']);
                     } else {
                         $data['discount'] = floatval($discount);
                     }
                     $data['total'] = $data['discount'];
                 }
                 //If quantity more then one
                 if (intval($quantity) > 1) {
                     $data['total'] = intval($quantity) * $data['total'];
                 }
                 if (Model_Orders::insertProduct($data) === TRUE) {
                     //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'] == $size && intval($quantity) > 0) {
                             $product['sizes'][$size_name]['quantity'] = $product_size['quantity'] - $quantity;
                         }
                     }
                     $sizes = json_encode($product['sizes']);
                     if (intval($quantity) > 0) {
                         $product['quantity'] = intval($product['quantity']) - intval($quantity);
                     } else {
                         $product['quantity'] = 0;
                     }
                     Model_Orders::manageQuantities($product['id'], $sizes, $product['quantity']);
                     Model_Products::saveProductToSize($product['id'], $sizes);
                     $response['status'] = 'success';
                     $response['message'] = trans('orders.product_saved');
                 }
             }
         }
     }
     return response()->json($response);
 }