get(); return view('bo.orders', compact('orders')); } public function changeStatus($reference) { $order = Order::where('reference', $reference)->first(); $order->status_id = $order->status_id + 1; $order->save(); return redirect()->route('bo.orders')->with('success', "Changement de status enregistré pour la commande " . $order->reference . "."); } public function cancelOrder($reference) { $order = Order::where('reference', $reference)->first(); $order->status_id = 7; $order->save(); return redirect()->route('bo.orders')->with('success', "La commande à été annulée."); } public function addProduct($orderReference, $productId, $quantity) { $product = Product::findOrFail($productId); $order = Order::where('reference', $orderReference)->first(); $order->total = $order->total + (($product->price - $product->price_buy) * $quantity); $orderProduct = new OrdersProducts(); $orderProduct->product_id = $product->id; $orderProduct->order_id = $order->id; $orderProduct->quantity = $quantity; $orderProduct->save(); return redirect()->route('bo.orders')->with('success', "Le produit à été ajouté."); } //front sales tunnel step2 recap public function recapOrder() { $activestep = 2; $cart = Cart::content(); $cartInfos = array( "totalTTC" => Cart::total(), 'totalTVA' => Cart::tax(), //total des taxes 'totalHT' => Cart::subtotal(), //total sans les taxes 'totalweight' => Cart::weight(), //poids total des items 'totalHTbefore' => Cart::priceTotal(), //total avant taxes et remises 'totaldiscount' => Cart::discount(), //total des remises 'countItems' => Cart::count(), //nb de produits dans le cart ); return view('sales.recap', compact('activestep', 'cart', 'cartInfos')); } //save order and go to step 3 /** * Storing order from sales tunnel */ public function store() { //save $order = new Order(); $order->reference = Str::uuid(); $order->date = Carbon::now(); $order->status_id = 1; $order->total = str_replace(',', '.', str_replace(' ', '', Cart::total())); $order->customer_id = Auth::user()->id; $order->save(); //order_id $cart = Cart::content(); foreach ($cart as $cartItem) { $order_line = new OrderLine(); $order_line->order_id = $order->id; $order_line->product_id = $cartItem->model->id; $order_line->quantity = $cartItem->qty; //gross unit price $order_line->unit_price = $cartItem->model->priceHt; //discount per unit $order_line->unit_discount = $cartItem->model->discountAmount; //tax rate for this line $order_line->tax_rate = $cartItem->taxRate; //tax amount par unit $order_line->unit_tax = (($order_line->unit_price - $order_line->unit_discount) * ($order_line->tax_rate / 100)); //discount amount for the line $order_line->total_discount = $order_line->unit_discount * $order_line->quantity; //gross amount for the line $order_line->total_ht = ($order_line->unit_price - $order_line->unit_discount) * $order_line->quantity; //tax amount for the line $order_line->total_tax = $order_line->unit_tax * $order_line->quantity; //total TTC avec remises de la ligne $order_line->total_ttc = $order_line->total_ht + $order_line->total_tax; $order_line->save(); } //empty cart Cart::destroy(); //then redirect return redirect()->route('sales.pick-slot', ['reference' => $order->reference]); } //pick your time slot public function timeSlotPick($reference) { $activestep = 3; //settings à stocker //get available timeslots //days 0 = sunday => 6 = saturday $plage = array( 0 => array(), 1 => array( 'start' =>'09:00', 'end' => '16:50', ), 2 => array( 'start' =>'09:00', 'end' => '17:00', ), 3 => array( 'start' =>'09:00', 'end' => '17:00', ), 4 => array( 'start' =>'09:00', 'end' => '17:00', ), 5 => array( 'start' =>'09:00', 'end' => '17:00', ), 6 => array( 'start' =>'10:00', 'end' => '12:00', ), ); $plage_interval = "0:15"; $plage_days = 14; //génération des plages //start today $today = CarbonImmutable::now()->startOfDay(); $now = CarbonImmutable::now(); $availCreneaux = array(); $interval = CarbonInterval::createFromFormat('h:i',$plage_interval); for ($i = 0; $i < $plage_days; $i++) { /** @var Carbon */ $current = $today->add($i, 'day'); $weekDay = $current->weekday(); if (isset($plage[$weekDay]['start'])) { $currentTime = Carbon::createFromTimeString($plage[$weekDay]['start']); $endTime = Carbon::createFromTimeString($plage[$weekDay]['end']); do { $creneau = $currentTime->format('H:i'); //attention si la date est passée (today mai avant maintenant) : pas de créneau if($current > $today || $currentTime >= $now) { $availCreneaux[$current->format('d/m/Y')][] = $creneau; } $currentTime->add($interval); } while ($currentTime <= $endTime); } } $order = Order::where('reference', '=', $reference)->firstOrFail(); return view('sales.pick-slot', compact('activestep', 'availCreneaux', 'order')); } //save timeslot and validate order public function setTimeSlot(Request $request) { $time_slot = Carbon::createFromFormat('d/m/Y-H:i', $request->selectedcreneau); $tsday = explode('-',$request->selectedcreneau)[0]; $tstime = explode('-',$request->selectedcreneau)[1]; $reference = $request->order_ref; $order = Order::where('reference', '=', $reference)->firstOrFail(); if($order->status_id == 1) { // on ne traite l'enregistrement que pour les commandes en attente venant de l'étape de récap //save timeslot $timeslot = Timeslot::firstOrNew(['order_id' => $order->id]); $timeslot->order_id = $order->id; $timeslot->day = $tsday; $timeslot->time = $tstime; $timeslot->save(); //save order status $order->status_id = 3; $order->save(); //send confirm mail Mail::to($order->customer->email)->send(new OrderConfirmMail($order)); } //redirect to final confirmation return redirect()->route('sales.confirm', ['reference' => $order->reference]); } //show confirmation public function confirmOrder($reference) { $activestep = 4; $idcommande = Session::get('idcommande'); $order = Order::where('reference', '=', $reference)->firstOrFail(); return view('sales.confirm', compact('activestep', 'order')); } }