99 lines
3.3 KiB
PHP
99 lines
3.3 KiB
PHP
<?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;
|
|
Cart::instance('panier');
|
|
$cart = Cart::content();
|
|
|
|
$cartInfos = array(
|
|
"totalTTC" => Cart::total(),
|
|
// Total des taxes.
|
|
'totalTVA' => Cart::tax(),
|
|
// Total sans les taxes.
|
|
'totalHT' => Cart::subtotal(),
|
|
// Poids total des items.
|
|
'totalweight' => Cart::weight(),
|
|
// Total avant taxes et remises.
|
|
'totalHTbefore' => Cart::priceTotal(),
|
|
// Total des remises.
|
|
'totaldiscount' => Cart::discount(),
|
|
// Nb de produits dans le cart.
|
|
'countItems' => Cart::count(),
|
|
);
|
|
|
|
return view('sales.recap', compact('activestep', 'cart', 'cartInfos'));
|
|
}
|
|
|
|
/**
|
|
* 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]);
|
|
}
|
|
}
|