public function index()
 {
     $member_id = \Auth::user()->id;
     $books_in_cart = \App\Cart::with('books')->where('member_id', '=', $member_id)->get();
     $cart_total = \App\Cart::with('books')->where('member_id', '=', $member_id)->sum('total');
     return view('cart', compact('books_in_cart', 'cart_total'));
 }
 public function postOrder()
 {
     $member_id = \Auth::user()->id;
     $address = \Request::get('address');
     $books_in_cart = \App\Cart::with('books')->where('member_id', '=', $member_id)->get();
     $cart_total = \App\Cart::with('books')->where('member_id', '=', $member_id)->sum('total');
     $order = \App\Order::create(['member_id' => $member_id, 'address' => $address, 'total' => $cart_total, 'created_at' => \Carbon\Carbon::now()]);
     $message = [];
     foreach ($books_in_cart as $cart_item) {
         // dobavlenie v pivot
         $order->orderItems()->attach($cart_item->book_id, ['amount' => $cart_item->amount, 'price' => $cart_item->books->price, 'total' => $cart_item->books->price * $cart_item->amount]);
         array_push($message, $cart_item);
     }
     // dd($books_in_cart[0]->books->title);
     // $to = '*****@*****.**';
     // $subject = 'Tema';
     // mail($to, $subject, implode($message));
     // dd($message);
     \Mail::send('emails.order', ['cart' => $message, 'order' => $order], function ($msg) {
         //  and a copy to users
         $msg->to(['*****@*****.**', \Auth::user()->email])->subject('Order');
     });
     //Udalyaem iz korziny vse Posle uspeshnogo dobavleniya v book_order
     //To est sozdaniya zakaza i dobavleniya ego v PIVOT
     \App\Cart::where('member_id', '=', $member_id)->delete();
     return \Redirect::to('index')->with('message', 'Your order successfully added');
 }
示例#3
0
 public function getEmpty($user_id)
 {
     $carts = Cart::with('Items')->where('user_id', $user_id)->get();
     Cart::with('Items')->where('user_id', $user_id)->delete();
     foreach ($carts as $item) {
         $item->Items->increment('stock');
     }
     User::where('id', $user_id)->update(['last_cart_timestamp' => NULL]);
     return redirect('/');
 }
示例#4
0
 /**
  * Store a newly created resource in storage.
  *
  * @param CreateCartItemRequest|Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(CreateCartItemRequest $request)
 {
     $cart = Cart::with('cart_items.product.stocks')->first();
     foreach ($cart->cart_items as $cartItem) {
         if ($cartItem->product_id == $request->product_id) {
             $cartItem->amount++;
             $cartItem->save();
             return redirect()->route('checkout.index');
         }
     }
     $cart->cart_items()->create(['product_id' => $request->product_id, 'amount' => 1]);
     return redirect()->route('checkout.index');
 }
示例#5
0
 public function postOrder(Request $request)
 {
     $user = Auth::user();
     if ($user->firstname == NULL || $user->lastname == NULL) {
         return back()->withErrors('Merci d\'enregistrer vos nom et prénom sur la page Mon Compte avant de passer commande.');
     }
     $this->validate($request, ['address' => 'required']);
     if ($request->input('address') == 'saved') {
         $address = serialize(collect($user->Address)->only(['street', 'post_code', 'city', 'country']));
     } else {
         $this->validate($request, ['street' => 'required', 'postcode' => 'required|numeric', 'city' => 'required', 'country' => 'required']);
         $address = serialize(['street' => $request->input('street'), 'post_code' => $request->input('postcode'), 'city' => $request->input('city'), 'country' => $request->input('country')]);
         if ($request->input('remember') == 'remember') {
             if (!$user->Address()->exists()) {
                 $new_address = new Address();
             } else {
                 $new_address = $user->Address;
             }
             $new_address->street = $request->input('street');
             $new_address->post_code = $request->input('postcode');
             $new_address->city = $request->input('city');
             $new_address->country = $request->input('country');
             $user->Address()->save($new_address);
         }
     }
     $cart_items = Cart::with('Items')->where('user_id', $user->id)->get();
     $cart_total = Cart::with('Items')->where('user_id', $user->id)->sum('total');
     if ($cart_items->count() == 0) {
         return back()->withErrors('Une erreur est parvenue, vueillez recommencer l\'opération');
     }
     $order = new Order();
     $order->user_id = $user->id;
     $order->address = $address;
     $order->total = $cart_total;
     $order->fullname = serialize(['firstname' => $user->firstname, 'lastname' => $user->lastname]);
     $order->status = 1;
     $order->save();
     foreach ($cart_items as $order_item) {
         $order->orderItems()->attach($order_item->item_id, ['amount' => $order_item->amount, 'price' => $order_item->Items->price, 'total' => $order_item->Items->price * $order_item->amount]);
     }
     Cart::where('user_id', $user->id)->delete();
     Mail::send([], ['user' => $user], function ($mail) use($user) {
         $mail->to($user->email, $user->username)->subject('Commande enregistrée !')->setBody('Votre commande à été enregistrée');
     });
     return redirect('/')->with('status', 'Votre commande a été enregistrée.');
 }
示例#6
0
 /**
  * Define the application's command schedule.
  *
  * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
  * @return void
  */
 protected function schedule(Schedule $schedule)
 {
     $schedule->call(function () {
         $cd = DB::table('master')->first()->carts_cd - 1;
         $olderThan = \Carbon\Carbon::now()->subMinutes($cd);
         $users = User::where('last_cart_timestamp', '<', $olderThan->format('Y-m-d H:i:s'))->get();
         foreach ($users as $user) {
             $carts = Cart::with('Items')->where('user_id', $user->id)->get();
             foreach ($carts as $item) {
                 $item->Items->increment('stock');
             }
             Cart::with('Items')->where('user_id', $user->id)->delete();
             $user->last_cart_timestamp = NULL;
             $user->save();
         }
     })->everyMinute();
 }