152 lines
4.7 KiB
PHP
152 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\Product\WeightUnit;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Product;
|
|
use Gloudemans\Shoppingcart\Facades\Cart as FacadesCart;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class BasketController extends Controller
|
|
{
|
|
/**
|
|
* Get Basket
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getBasket()
|
|
{
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
/**
|
|
* Ajout d'un produit au panier
|
|
*
|
|
* ! Assert Model found, status true and orderable true.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\JsonResponse
|
|
* @throws \Illuminate\Validation\ValidationException Si le produit n'est pas disponible (orderable).
|
|
*/
|
|
public function addToBasket(Product $product)
|
|
{
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
$this->authorize('addToCart', $product);
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
if (!$product->status or !$product->orderable) {
|
|
throw ValidationException::withMessages([
|
|
'error' => \trans('Ce produit n\'est plus disponible')
|
|
]);
|
|
}
|
|
|
|
// ! Use cart facade here because of lib bug
|
|
// ! https://github.com/bumbummen99/LaravelShoppingcart/pull/181
|
|
// * Note Lib is is actually refactoring for laravel 9 php8 only
|
|
// * https://github.com/bumbummen99/LaravelShoppingcart/pull/145
|
|
/** @var \Gloudemans\Shoppingcart\CartItem */
|
|
$cartItem = FacadesCart::add($product, 1, [
|
|
'slug' => $product->slug,
|
|
'picture' => $product->picture,
|
|
'sell_unit' => $product->sell_unit,
|
|
'weight_with_unit' => WeightUnit::getDisplayableWeight($product->weight),
|
|
'discount_type' => \optional(\optional($product->discount)->type)->value,
|
|
// * Prix ttc du produit
|
|
'price_ttc' => $product->price_ttc,
|
|
// * Prix ttc du produit remisé
|
|
'undiscounted_price_ttc' => $product->undiscounted_price_ttc,
|
|
]);
|
|
|
|
$cartItem->setTaxRate($product->tax_rate->rate / 100);
|
|
$cartItem->setDiscountRate($product->discount_rate);
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
/**
|
|
* Retrait d'un produit du panier (1 quantité d'1 type)
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function removeFromBasket(Product $product)
|
|
{
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
/** @var \Gloudemans\Shoppingcart\CartItem|null */
|
|
$basketItem = $basket->content()->where('id', $product->id)->first();
|
|
|
|
/** @var string|null */
|
|
$rowId = \optional($basketItem)->rowId;
|
|
/** @var integer|null */
|
|
$qty = \optional($basketItem)->qty;
|
|
|
|
if ($rowId and !\is_null($qty)) {
|
|
$basket->update($rowId, $qty - 1);
|
|
}
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
/**
|
|
* Retrait d'un type produit du panier (toute les quantités d'1 type)
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function removeItemTypeFromBasket(Product $product)
|
|
{
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
/** @var \Gloudemans\Shoppingcart\CartItem|null */
|
|
$basketItem = $basket->content()->where('id', $product->id)->first();
|
|
|
|
/** @var string|null */
|
|
$rowId = \optional($basketItem)->rowId;
|
|
|
|
if ($rowId) {
|
|
$basket->remove($rowId);
|
|
}
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
/**
|
|
* Retrait d'un type produit du panier (toute les quantités d'1 type)
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function emptyBasket()
|
|
{
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
$basket->destroy();
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
}
|