174 lines
5.7 KiB
PHP
174 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\Order\OrderProcess;
|
|
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;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
// * --- TUNNEL DE COMMANDE ---
|
|
/**
|
|
* Récapitualtif du panier
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function basketRecap()
|
|
{
|
|
$this->assertDoesNotHaveEmptyBasket();
|
|
$this->setOrderProcessStep(OrderProcess::reviewBasket());
|
|
|
|
$activestep = 2;
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
$messages = [];
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket, $messages);
|
|
|
|
if (!empty($messages)) {
|
|
Session::flash('warning', \collect($messages)->implode(",\n"));
|
|
}
|
|
|
|
$basketItems = $this->basketToList($basket);
|
|
|
|
return view('front.order-tunnel.basket-recap', compact(
|
|
'activestep',
|
|
'basket',
|
|
'basketItems'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Select shipping Mode
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function shippingMode()
|
|
{
|
|
$this->assertDoesNotHaveEmptyBasket();
|
|
$this->setOrderProcessStep(OrderProcess::shippingMethod(), OrderProcess::reviewBasket());
|
|
|
|
$deliveyModes = DeliveryMode::availables()->get();
|
|
|
|
return view('front.order-tunnel.shipping-mode');
|
|
}
|
|
|
|
// * --- FIN TUNNEL DE COMMANDE ---
|
|
|
|
/**
|
|
* 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]);
|
|
}
|
|
|
|
/**
|
|
* Assert user does not have an empty basket
|
|
* or redirect with warning
|
|
*
|
|
* @return void
|
|
*/
|
|
private function assertDoesNotHaveEmptyBasket(): void
|
|
{
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
if (!$basket->count()) {
|
|
\redirect()->route('home')
|
|
->with('warning', trans('Vous n\'avez aucun produit dans votre panier'))
|
|
->throwResponse();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate Process step
|
|
*
|
|
* @param \App\Enums\Order\OrderProcess $step
|
|
* @param \App\Enums\Order\OrderProcess|null $assertDid
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If previous was not done.
|
|
*/
|
|
private function setOrderProcessStep(OrderProcess $step, OrderProcess $assertDid = null): void
|
|
{
|
|
if ($assertDid) {
|
|
if (!Session::get('PreviousOrderStep')) {
|
|
Session::put('PreviousOrderStep', collect());
|
|
}
|
|
/** @var \Illuminate\Support\Collection */
|
|
$previousSteps = Session::get('PreviousOrderStep');
|
|
if (Session::get('OrderStep') < $step) {
|
|
/** @var \App\Enums\OrderProcess */
|
|
$previousStep = Session::get('OrderStep');
|
|
$previousSteps->put($previousStep->value, $previousStep);
|
|
Session::put('PreviousOrderStep', $previousSteps);
|
|
}
|
|
if (!$previousSteps->count()) {
|
|
\redirect()->route('home')
|
|
->with('warning', trans('Vous devez d\'abord valider votre panier'))
|
|
->throwResponse();
|
|
}
|
|
if (!$previousSteps->contains($assertDid)) {
|
|
throw ValidationException::withMessages(['Étape précédente requise']);
|
|
}
|
|
} //end if
|
|
|
|
Session::put('OrderStep', $step);
|
|
}
|
|
}
|