public function storeCommand() { $user = Auth::user(); $token = Session('_token'); $command = Cart::where('_token', $token)->get(); foreach ($command as $item) { $product_id = $item->product_id; $price = $item->price; $quantity = $item->quantity; $customer = $user->customer; $history = History::create(['product_id' => $product_id, 'quantity' => $quantity, 'price' => $price, 'customer_id' => $customer->id, 'status' => 'finalized']); $item->delete(); } return redirect('/'); }
public function finalize(Request $request) { $panier = session('panier'); $user = Auth::user(); // user authentifié par le login $user_id = $user->id; // dd($user_id); try { $customer = $user->customer()->firstOrFail(); // dd($customer); // $customer = $data[0]; // $data est une collection !? if (!is_null($panier)) { foreach ($panier as $product_id => $quantity) { $product = Product::find($product_id); Event::fire(new ProductWasCommanded($product, $quantity)); $price = $product->price; $quantities[] = $quantity; History::create(['product_id' => $product_id, 'customer_id' => $customer->id, 'price' => $price, 'quantity' => $quantity, 'command_at' => Carbon::now(), 'status' => 'finalized']); } $number_command = array_sum($quantities); $customer->update(['number_command' => $number_command]); session(['panier' => NULL]); session(['stock' => NULL]); // dd($panier); return redirect('/home')->with(['message' => 'succes add', 'alert' => 'Commande effectuée']); // return view('front.cart', compact('products', 'quantities', 'total')); } else { return back()->with(['message' => 'Votre panier est vide']); } } catch (\Exception $e) { // dd('mauvaise route'); $request->session()->flash('status', 'fail'); return back()->with(['message' => 'We need some information', 'alert' => 'fail']); // dd('coucou'); // var_dump customisé + die } }
public function confirmatePanier() { $panier = Session::get('panier'); $date = Carbon::now(); $idUser = Auth::user()->id; $idCustomer = User::find($idUser)->customer->id; $orderNum = rand(1000, 3999); foreach ($panier as $idProd => $quantity) { if ($quantity > 0) { $price = Product::find($idProd)->price; Event::fire(new ScoreProduct($idProd)); History::create(array('product_id' => $idProd, 'customer_id' => $idCustomer, 'order_number' => $orderNum, 'price' => $price, 'quantity' => $quantity, 'command_at' => $date)); } } Session::forget('panier'); return redirect('/')->with(['message' => 'succes']); }
function hist($action, $content, $id) { \App\History::create(['action' => $action, 'content' => $content, 'user_id' => $id]); }
public function validCommand() { $user_id = Auth::user()->id; $customer_id = Customer::where('user_id', '=', $user_id)->value('id'); $token = session('_token'); if (empty($customer_id)) { return view('front.form_customer'); } else { $history = History::create(['customer_id' => $customer_id, 'token' => $token]); $history_id = $history->id; $carts = Cart::where('user_id', '=', $user_id)->get(); foreach ($carts as $cart) { History_detail::create(['history_id' => $history_id, 'product_id' => $cart->product_id, 'quantity' => $cart->quantity]); $number_products_commanded = DB::table('customers')->where('id', '=', $customer_id)->value('number_products_commanded'); $number_products_commanded += $cart->quantity; Customer::where('id', '=', $customer_id)->update(['number_products_commanded' => $number_products_commanded]); $cart->command_unf->delete(); $cart->delete(); } $total_price = 0; $history_details = History_detail::where('history_id', '=', $history_id)->get(); foreach ($history_details as $history_detail) { $quantity = $history_detail->quantity; $price = $history_detail->product->price; $total_price += $quantity * $price; } DB::table('histories')->where('id', '=', $history_id)->update(['total_price' => $total_price]); return redirect('/')->with(['validcommand' => "Votre commande a bien été enregistrée ! Nous vous en remercions.", 'alert' => 'success']); } }
/** * Save the orders in storage */ public function confirmCart() { $user_id = Auth::user()->id; $user = User::find($user_id); $customer = $user->customer; if ($customer == null) { return redirect('customer'); } else { $command_id = History::all()->max('command_id'); $command_id++; if (Session::has('cart')) { $cart = Session::get('cart'); foreach ($cart as $id => $quantity) { $product = Product::find($id); $command = ['command_id' => $command_id, 'product_id' => $id, 'customer_id' => $customer->id, 'price' => $product->price * $quantity, 'quantity' => (int) $quantity, 'command_at' => date('Y-m-d H:i:s'), 'status' => 'unfinalized']; History::create($command); $product->score++; $product->quantity -= $quantity; $product->save(); } } Session::forget('cart'); $customer->increment('number_command'); return redirect('/')->with(['message' => trans('app.finishSuccess'), 'alert' => 'success']); } }