2022-11-23 13:00:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Order;
|
|
|
|
|
use App\Models\OrderLine;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
|
|
class OrderController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Récapitualtif du panier
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function basketRecap()
|
|
|
|
|
{
|
|
|
|
|
$activestep = 2;
|
2022-11-23 15:48:07 +01:00
|
|
|
|
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
|
|
|
$basket = app('basket');
|
2022-11-23 13:00:38 +01:00
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
$cartInfos = [
|
2022-11-23 15:48:07 +01:00
|
|
|
"totalTTC" => $basket->total(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Total des taxes.
|
2022-11-23 15:48:07 +01:00
|
|
|
'totalTVA' => $basket->tax(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Total sans les taxes.
|
2022-11-23 15:48:07 +01:00
|
|
|
'totalHT' => $basket->subtotal(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Poids total des items.
|
2022-11-23 15:48:07 +01:00
|
|
|
'totalweight' => $basket->weight(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Total avant taxes et remises.
|
2022-11-23 15:48:07 +01:00
|
|
|
'totalHTbefore' => $basket->priceTotal(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Total des remises.
|
2022-11-23 15:48:07 +01:00
|
|
|
'totaldiscount' => $basket->discount(),
|
2022-11-23 13:00:38 +01:00
|
|
|
// Nb de produits dans le cart.
|
2022-11-23 15:48:07 +01:00
|
|
|
'countItems' => $basket->count(),
|
2022-11-28 16:56:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$basket = $this->basketToList($basket);
|
2022-11-23 13:00:38 +01:00
|
|
|
|
2022-11-24 15:30:05 +01:00
|
|
|
return view('front.order-tunnel.basket-recap', compact('activestep', 'basket', 'cartInfos'));
|
2022-11-23 13:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* front
|
|
|
|
|
* Storing order from sales tunnel
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function basketRecapSubmit()
|
|
|
|
|
{
|
|
|
|
|
// Save.
|
|
|
|
|
Cart::instance('panier');
|
|
|
|
|
$order = new Order();
|
|
|
|
|
// Generate order ref.
|
|
|
|
|
$order->reference = $this->generateOrderRef();
|
|
|
|
|
$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::instance('panier');
|
|
|
|
|
$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 = round(
|
|
|
|
|
(($order_line->unit_price - $order_line->unit_discount) * ($order_line->tax_rate / 100)) * 100
|
|
|
|
|
) / 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();
|
|
|
|
|
} //end foreach
|
|
|
|
|
|
|
|
|
|
// Empty cart.
|
|
|
|
|
Cart::destroy();
|
|
|
|
|
// Then redirect.
|
|
|
|
|
return redirect()->route('order-tunnel.pick-slot', ['reference' => $order->reference]);
|
|
|
|
|
}
|
|
|
|
|
}
|