2020-11-02 14:47:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
2022-11-16 12:47:41 +01:00
|
|
|
namespace App\Http\Controllers\Fo;
|
2020-11-02 14:47:11 +01:00
|
|
|
|
2022-12-02 10:37:01 +01:00
|
|
|
use App\Enums\Product\WeightUnit;
|
2022-11-16 12:47:41 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2023-03-01 11:02:42 +01:00
|
|
|
use App\Models\Merchandise\Product;
|
2022-11-23 11:10:40 +01:00
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart as FacadesCart;
|
2022-11-28 16:56:39 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2020-11-02 14:47:11 +01:00
|
|
|
|
|
|
|
|
class BasketController extends Controller
|
|
|
|
|
{
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
2022-11-23 11:10:40 +01:00
|
|
|
* Get Basket
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
2022-11-23 11:10:40 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-04-14 09:34:23 +02:00
|
|
|
*/
|
2022-11-23 11:10:40 +01:00
|
|
|
public function getBasket()
|
2021-03-24 17:58:27 +01:00
|
|
|
{
|
2022-12-02 14:55:16 +01:00
|
|
|
$this->authorize('useBasketSystem', Product::class);
|
2022-11-23 11:10:40 +01:00
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
|
|
|
$basket = app('basket');
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
return \response()->json($this->basketToList($basket));
|
2021-07-09 13:32:14 +02:00
|
|
|
}
|
2021-03-24 17:58:27 +01:00
|
|
|
|
2021-07-09 13:32:14 +02:00
|
|
|
/**
|
2022-11-23 11:10:40 +01:00
|
|
|
* Ajout d'un produit au panier
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
2022-11-30 16:36:27 +01:00
|
|
|
* ! Assert Model found, status true and orderable true.
|
|
|
|
|
*
|
2023-03-01 11:02:42 +01:00
|
|
|
* @param \App\Models\Merchandise\Product $product
|
2022-11-23 11:10:40 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2022-11-30 16:36:27 +01:00
|
|
|
* @throws \Illuminate\Validation\ValidationException Si le produit n'est pas disponible (orderable).
|
2021-07-09 13:32:14 +02:00
|
|
|
*/
|
2022-11-23 11:10:40 +01:00
|
|
|
public function addToBasket(Product $product)
|
2021-07-09 13:32:14 +02:00
|
|
|
{
|
2022-12-02 14:55:16 +01:00
|
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
|
$this->authorize('addToCart', $product);
|
|
|
|
|
|
2022-11-23 11:10:40 +01:00
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
|
|
|
$basket = app('basket');
|
|
|
|
|
|
2022-11-30 16:36:27 +01:00
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
|
|
|
|
|
|
if (!$product->status or !$product->orderable) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
'error' => \trans('Ce produit n\'est plus disponible')
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 11:10:40 +01:00
|
|
|
// ! 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
|
2022-11-24 15:30:05 +01:00
|
|
|
/** @var \Gloudemans\Shoppingcart\CartItem */
|
|
|
|
|
$cartItem = FacadesCart::add($product, 1, [
|
2022-11-23 11:10:40 +01:00
|
|
|
'slug' => $product->slug,
|
|
|
|
|
'picture' => $product->picture,
|
|
|
|
|
'sell_unit' => $product->sell_unit,
|
2022-11-28 16:56:39 +01:00
|
|
|
'weight_with_unit' => WeightUnit::getDisplayableWeight($product->weight),
|
2022-12-19 16:20:00 +01:00
|
|
|
'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,
|
2022-11-23 11:10:40 +01:00
|
|
|
]);
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
$cartItem->setTaxRate($product->tax_rate->rate / 100);
|
2022-11-24 15:30:05 +01:00
|
|
|
$cartItem->setDiscountRate($product->discount_rate);
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
return \response()->json($this->basketToList($basket));
|
2021-03-25 15:48:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
2022-11-23 11:10:40 +01:00
|
|
|
* Retrait d'un produit du panier (1 quantité d'1 type)
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
2023-03-01 11:02:42 +01:00
|
|
|
* @param \App\Models\Merchandise\Product $product
|
2022-11-23 11:10:40 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-04-14 09:34:23 +02:00
|
|
|
*/
|
2022-11-23 11:10:40 +01:00
|
|
|
public function removeFromBasket(Product $product)
|
2021-03-25 15:48:07 +01:00
|
|
|
{
|
2022-12-02 14:55:16 +01:00
|
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
|
|
2022-11-23 11:10:40 +01:00
|
|
|
/** @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);
|
|
|
|
|
}
|
2022-11-30 16:36:27 +01:00
|
|
|
|
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
return \response()->json($this->basketToList($basket));
|
2021-03-25 15:48:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
2022-11-23 11:10:40 +01:00
|
|
|
* Retrait d'un type produit du panier (toute les quantités d'1 type)
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
2023-03-01 11:02:42 +01:00
|
|
|
* @param \App\Models\Merchandise\Product $product
|
2022-11-23 11:10:40 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-04-14 09:34:23 +02:00
|
|
|
*/
|
2022-11-23 11:10:40 +01:00
|
|
|
public function removeItemTypeFromBasket(Product $product)
|
2022-08-25 12:26:43 +02:00
|
|
|
{
|
2022-12-02 14:55:16 +01:00
|
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
|
|
2022-11-23 11:10:40 +01:00
|
|
|
/** @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);
|
|
|
|
|
}
|
2022-11-30 16:36:27 +01:00
|
|
|
|
|
|
|
|
$basket = $this->filterBasketKeepOnlyOrderableItems($basket);
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
return \response()->json($this->basketToList($basket));
|
2021-03-25 15:48:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
2022-11-23 11:10:40 +01:00
|
|
|
* Retrait d'un type produit du panier (toute les quantités d'1 type)
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
2022-11-23 11:10:40 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-04-14 09:34:23 +02:00
|
|
|
*/
|
2022-11-23 11:10:40 +01:00
|
|
|
public function emptyBasket()
|
2021-07-09 13:32:14 +02:00
|
|
|
{
|
2022-12-02 14:55:16 +01:00
|
|
|
$this->authorize('useBasketSystem', Product::class);
|
|
|
|
|
|
2022-11-23 11:10:40 +01:00
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
|
|
|
$basket = app('basket');
|
|
|
|
|
|
|
|
|
|
$basket->destroy();
|
|
|
|
|
|
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
|
|
|
$basket = app('basket');
|
|
|
|
|
|
2022-11-28 16:56:39 +01:00
|
|
|
return \response()->json($this->basketToList($basket));
|
2021-07-09 13:32:14 +02:00
|
|
|
}
|
2020-11-02 14:47:11 +01:00
|
|
|
}
|