ldc-boutique-cnc/app/Http/Controllers/Fo/OrderController.php

157 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Fo;
2022-11-30 17:03:43 +01:00
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;
2022-11-30 17:03:43 +01:00
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
class OrderController extends Controller
{
/**
* Récapitualtif du panier
*
* @return \Illuminate\Contracts\View\View
*/
public function basketRecap()
{
2022-11-30 17:03:43 +01:00
$this->assertDoesNotHaveEmptyBasket();
$this->setOrderProcessStep(OrderProcess::reviewBasket());
$activestep = 2;
/** @var \Gloudemans\Shoppingcart\Cart */
2022-11-30 16:36:27 +01:00
$basket = app('basket');
$messages = [];
$basket = $this->filterBasketKeepOnlyOrderableItems($basket, $messages);
if (!empty($messages)) {
Session::flash('warning', \collect($messages)->implode(",\n"));
}
2022-11-28 16:56:39 +01:00
2022-11-29 15:47:46 +01:00
$basketItems = $this->basketToList($basket);
2022-11-29 15:47:46 +01:00
return view('front.order-tunnel.basket-recap', compact(
'activestep',
'basket',
'basketItems'
));
}
/**
* 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]);
}
2022-11-30 17:03:43 +01:00
/**
* 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);
}
}