/** * 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); }
/** * 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); }