public function delete_book(Request $request)
 {
     $book = Book::find($id);
     $book->delete();
     Session::flash('flash_message', 'Xóa thành công!');
     return redirect('/admin_books');
 }
Пример #2
0
 public function undo_delete(Request $request)
 {
     if (Auth::user()->role !== 1) {
         return redirect('/');
     }
     $book = Book::find((int) $request->input('book_id'));
     $book->deleted = 0;
     $book->save();
 }
Пример #3
0
 public function store()
 {
     $error = array();
     $error['qtys'] = array();
     $error['deleted'] = array();
     $count = 0;
     if (Auth::guest()) {
         return redirect('auth/login');
     } else {
         if (Cart::total() > 1000) {
             $error['money'] = "<li>Tổng số tiền phải < 1 triệu</li>";
             $count++;
         }
         $containers = array();
         $container = array();
         $cart = Cart::content();
         foreach ($cart as $item) {
             $book = Book::find($item->id);
             if ($book->quantity < $item->qty) {
                 array_push($error['qtys'], "<li>Số lượng hiện tại nhiều hơn số sách '{$book->title}' đang có</li>");
                 $count++;
             }
             if ($book->deleted == 1) {
                 array_push($error['deleted'], "<li>Sách '{$book->title}' đã bị xóa</li>");
                 $count++;
             }
         }
         if ($count > 0) {
             $error['error'] = "true";
             return json_encode($error);
         }
         $order = new Order();
         $order->user_id = Auth::user()->id;
         $order->date = Carbon::now();
         $order->money = Cart::total();
         $order->save();
         foreach ($cart as $item) {
             $orderline = new Orderline();
             $orderline->order_id = $order->id;
             $orderline->quantity = $item->qty;
             $orderline->book_id = $item->id;
             $orderline->price = $item->subtotal;
             $orderline->save();
         }
         $error["error"] = "false";
         $error["order_id"] = $order->id;
         Cart::destroy();
         return json_encode($error);
     }
 }
Пример #4
0
 public function show()
 {
     if (Auth::guest()) {
         return redirect('auth/login');
     } else {
         $containers = array();
         $container = array();
         $cart = Cart::content();
         foreach ($cart as $item) {
             $containers["{$item->id}"] = ['quantity' => $item->qty, 'book' => Book::find($item->id), 'subtotal' => $item->subtotal];
         }
         return Controller::myView('cart.show')->with('containers', $containers);
     }
 }
Пример #5
0
 public function searchResult(Request $request)
 {
     $data_request['search_text'] = $request->input('search_text');
     $data_request['price'] = $request->input('price');
     $data_request['author'] = $request->input('author');
     $data_request['title'] = $request->input('title');
     $data_request['check_author'] = $request->input('check_author');
     $data_request['check_price'] = $request->input('check_price');
     $data_request['check_category'] = $request->input('check_category');
     $data_request['categories'] = $request->input('categories');
     $data['request'] = $data_request;
     $books = Book::where('title', 'like', "%" . $request->input('search_text') . "%");
     if (null !== $request->input('check_author')) {
         $books = $books->where('author', 'like', "%" . $request->input('author') . "%");
         // if($request->input('author')==="1"){
         // 	$books= $books->where('title','like', "%".$request->input('search_text')."%");
         // }else{
         // 	$books= $books->where('author','like', "%".$request->input('search_text')."%");
         // }
     }
     if (null != $request->input('check_category')) {
         $books = $books->join('book_book_cates', 'books.id', '=', 'book_book_cates.book_id')->where('book_book_cates.book_cate_id', '=', (int) $request->input('categories'));
     }
     if (null !== $request->input('check_price')) {
         if ($request->input('price') === "0") {
             $books = $books->where('price', '<', 20);
         } else {
             if ($request->input('price') === "1") {
                 $books = $books->where('price', '>=', 20)->where('price', '<=', 50);
             } else {
                 if ($request->input('price') === "2") {
                     $books = $books->where('price', '>=', 50)->where('price', '<=', 100);
                 } else {
                     if ($request->input('price') === "3") {
                         $books = $books->where('price', '>', 100);
                     }
                 }
             }
         }
     }
     // if(null ===$request->input('check_author')&null ===$request->input('check_price')&null ===$request->input('check_category'))
     // 	$books=Book::where('title', 'like', "%".$request->input('search_text')."%" );
     $check = $books->first();
     if (is_null($check)) {
         return Controller::myView('book.search_result')->with('books', '1');
     }
     $books = $books->paginate(6);
     $data['books'] = $books;
     return Controller::myView('book.search_result')->with($data);
 }
 public function order_one(Request $request)
 {
     $order = new Order();
     $book = Book::find((int) $request->input('book_id'));
     if ($this->checkQuantity(1, $book)) {
         $orderline = new Orderline();
         $order->user_id = Auth::user()->id;
         $order->date = Carbon::now();
         $order->money = $book->price;
         $order->save();
         $orderline->order_id = $order->id;
         $orderline->book_id = $book->id;
         $orderline->quantity = 1;
         $orderline->price = $book->price;
         $orderline->save();
         $book->update(['quantity' => $book->quantity - 1]);
         return redirect("order/{$order->id}");
     } else {
         echo "k du";
     }
 }
Пример #7
0
 public function search($request)
 {
     $books = Book::where('title', 'like', "%" . $request->input('search_text') . "%");
     if (null !== $request->input('check_author')) {
         $books = $books->where('author', 'like', "%" . $request->input('author') . "%");
     }
     if (null != $request->input('check_category')) {
         $books = $books->join('book_book_cates', 'books.id', '=', 'book_book_cates.book_id')->where('book_book_cates.book_cate_id', '=', (int) $request->input('categories'));
     }
     if (null !== $request->input('check_price')) {
         if ($request->input('price') === "0") {
             $books = $books->where('price', '<', 20);
         } else {
             if ($request->input('price') === "1") {
                 $books = $books->where('price', '>=', 20)->where('price', '<=', 50);
             } else {
                 if ($request->input('price') === "2") {
                     $books = $books->where('price', '>=', 50)->where('price', '<=', 100);
                 } else {
                     if ($request->input('price') === "3") {
                         $books = $books->where('price', '>', 100);
                     }
                 }
             }
         }
     }
     if (null !== $request->input('book_status')) {
         if ($request->input('deleted') === "0") {
             $books = $books->where('deleted', '=', 0);
         } else {
             if ($request->input('deleted') === "1") {
                 $books = $books->where('deleted', '=', 1);
             }
         }
     }
     return $books;
 }
Пример #8
0
 private function changeUpdate($book, $request)
 {
     $this->book = Book::where('id', '=', (int) $request->input('book_id'))->first();
     $this->book->title = $request->input('title');
     $this->book->author = $request->input('author');
     $this->book->quantity = (int) $request->input('quantity');
     $this->book->price = (int) $request->input('price');
     $this->book->description = preg_replace("/\r\n|\r/", "<br />", $request->input('description'));
     $this->book->deleted = 0;
     $this->book->save();
     $old = array();
     $old[0] = (int) $request->input('old-categories');
     if ($request->input('old-categories1') !== null) {
         $old[1] = (int) $request->input('old-categories1');
     }
     if ($request->input('old-categories2') !== null) {
         $old[2] = (int) $request->input('old-categories2');
     }
     if ($request->input('old-categories3') !== null) {
         $old[3] = (int) $request->input('old-categories3');
     }
     $new = array();
     if ($request->input('categories') !== 0) {
         $new[0] = (int) $request->input('categories');
     } else {
         $new[0] = 0;
     }
     if ($request->input('categories1') !== 0) {
         $new[1] = (int) $request->input('categories1');
     } else {
         $new[1] = 0;
     }
     if ($request->input('categories2') !== 0) {
         $new[2] = (int) $request->input('categories2');
     } else {
         $new[2] = 0;
     }
     if ($request->input('categories3') !== 0) {
         $new[3] = (int) $request->input('categories3');
     } else {
         $new[3] = 0;
     }
     for ($i = 0; $i < count($new); $i++) {
         if ($new[$i] != 0) {
             if (isset($old[$i])) {
                 $book_book_cate = Book_book_cate::where('book_id', '=', $this->book->id)->where('book_cate_id', '=', $old[$i])->first();
             } else {
                 $book_book_cate = new Book_book_cate();
                 $book_book_cate->book_id = $this->book->id;
             }
             $old[$i] = $new[$i];
             $book_book_cate->book_cate_id = $new[$i];
             $book_book_cate->save();
         }
     }
     for ($i = 0; $i < count($old) - 1; $i++) {
         for ($j = $i + 1; $j < count($old); $j++) {
             if (isset($old[$j])) {
                 if ($old[$i] == $old[$j]) {
                     $book_book_cate = Book_book_cate::where('book_id', '=', $this->book->id)->where('book_cate_id', '=', $old[$i])->first();
                     $book_book_cate->delete();
                 }
             }
         }
     }
 }