140 lines
4.4 KiB
PHP
140 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Order;
|
|
use App\Models\OrderLine;
|
|
use App\Models\OrdersProducts;
|
|
use App\Models\Product;
|
|
use Carbon\Carbon;
|
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
|
|
class OrdersController extends Controller
|
|
{
|
|
public function index() {
|
|
$orders = Order::orderBy('date', 'DESC')->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;
|
|
$order_line->unit_price = $cartItem->price;
|
|
$order_line->unit_discount = 0;
|
|
$order_line->tax_rate = $cartItem->taxRate;
|
|
$order_line->unit_tax = ($order_line->unit_price * ($order_line->tax_rate / 100));
|
|
$order_line->total_discount = $order_line->unit_discount * $order_line->quantity;
|
|
$order_line->total_ht = $order_line->unit_price * $order_line->quantity;
|
|
$order_line->total_tax = $order_line->unit_tax * $order_line->quantity;
|
|
$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');
|
|
}
|
|
|
|
//pick your time slot
|
|
public function timeSlotPick()
|
|
{
|
|
$activestep = 3;
|
|
|
|
//get available timeslots
|
|
|
|
return view('sales.pick-slot', compact('activestep'));
|
|
}
|
|
|
|
//save timeslot and validate order
|
|
public function setTimeSlot()
|
|
{
|
|
//save order with timeslot
|
|
//send confirm mail
|
|
//redirect to final confirmation
|
|
return redirect()->route('sales.confirm');
|
|
}
|
|
|
|
//show confirmation
|
|
public function confirmOrder()
|
|
{
|
|
$activestep = 4;
|
|
|
|
return view('sales.confirm', compact('activestep'));
|
|
}
|
|
}
|