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

204 lines
6.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Fo;
2022-10-03 17:13:44 +02:00
use App\Enums\GeneralConfigType;
2022-11-23 11:10:40 +01:00
use App\Enums\WeightUnit;
use App\Http\Controllers\Controller;
2021-03-24 17:58:27 +01:00
use App\Models\Product;
2022-11-23 11:10:40 +01:00
use Gloudemans\Shoppingcart\Cart;
2022-11-28 16:56:39 +01:00
use Gloudemans\Shoppingcart\CartItem;
2022-11-23 11:10:40 +01:00
use Gloudemans\Shoppingcart\CartItemOptions;
use Gloudemans\Shoppingcart\Facades\Cart as FacadesCart;
use Illuminate\Http\Request;
2021-03-25 15:48:07 +01:00
use Illuminate\Support\Collection;
2021-11-03 17:23:25 +01:00
use Illuminate\Support\Facades\Auth;
2022-10-03 17:13:44 +02:00
use Illuminate\Support\Facades\Cache;
2022-11-28 16:56:39 +01:00
use Illuminate\Validation\ValidationException;
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-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-03-24 17:58:27 +01: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-23 11:10:40 +01:00
* @param \App\Models\Product $product
* @return \Illuminate\Http\JsonResponse
*/
2022-11-23 11:10:40 +01:00
public function addToBasket(Product $product)
{
2022-11-23 11:10:40 +01:00
/** @var \Gloudemans\Shoppingcart\Cart */
$basket = app('basket');
// ! 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),
'discount_type' => \optional(\optional($product->discount)->type)->value
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
*
2022-11-23 11:10:40 +01:00
* @param \App\Models\Product $product
* @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-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-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
* @param \App\Models\Product $product
* @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-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-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()
{
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-03-24 17:58:27 +01:00
2022-11-23 11:10:40 +01:00
2022-09-03 16:56:16 +02:00
/**
* return cart data array
*
* @param string $instance
* @return array
*/
public function getCart(string $instance = "panier")
{
Cart::instance($instance);
2021-03-25 15:48:07 +01:00
$dbpanier = new Collection();
2021-03-24 17:58:27 +01:00
foreach (Cart::content() as $row) {
$product = $row->model;
2022-09-03 16:56:16 +02:00
$product->calculatedPrices = $product->getCalcPrices();
$row->setTaxRate($product->tax->rate);
2022-11-24 15:30:05 +01:00
$row->setDiscountRate($product->discount_rate);
2021-03-25 15:48:07 +01:00
2021-03-24 17:58:27 +01:00
$cartextendedItem = array(
'cartItem' => $row,
'product' => $product
2021-03-24 17:58:27 +01:00
);
$dbpanier->add($cartextendedItem);
}
2021-03-25 15:48:07 +01:00
$cartInfos = array(
"totalTTC" => Cart::total(),
2022-09-03 16:56:16 +02:00
// Total des taxes.
2022-08-25 12:26:43 +02:00
'totalTVA' => Cart::tax(),
2022-09-03 16:56:16 +02:00
// Total sans les taxes.
2022-08-25 12:26:43 +02:00
'totalHT' => Cart::subtotal(),
2022-09-03 16:56:16 +02:00
// Poids total des items.
2022-08-25 12:26:43 +02:00
'totalweight' => Cart::weight(),
2022-09-03 16:56:16 +02:00
// Total avant taxes et remises.
2022-08-25 12:26:43 +02:00
'totalHTbefore' => Cart::priceTotal(),
2022-09-03 16:56:16 +02:00
// Total des remises.
2022-08-25 12:26:43 +02:00
'totaldiscount' => Cart::discount(),
2022-09-03 16:56:16 +02:00
// Nb de produits dans le cart.
2022-08-25 12:26:43 +02:00
'countItems' => Cart::count(),
2021-03-25 15:48:07 +01:00
);
2021-11-03 17:23:25 +01:00
$cartInfosNoPrice = array(
"totalTTC" => "-.--",
2022-09-03 16:56:16 +02:00
// Total des taxes.
2022-08-25 12:26:43 +02:00
'totalTVA' => "-.--",
2022-09-03 16:56:16 +02:00
// Total sans les taxes.
2022-08-25 12:26:43 +02:00
'totalHT' => "-.--",
2022-09-03 16:56:16 +02:00
// Poids total des items.
2022-08-25 12:26:43 +02:00
'totalweight' => Cart::weight(),
2022-09-03 16:56:16 +02:00
// Total avant taxes et remises.
2022-08-25 12:26:43 +02:00
'totalHTbefore' => "-.--",
2022-09-03 16:56:16 +02:00
// Total des remises.
2022-08-25 12:26:43 +02:00
'totaldiscount' => "-.--",
2022-09-03 16:56:16 +02:00
// Nb de produits dans le cart.
2022-08-25 12:26:43 +02:00
'countItems' => Cart::count(),
2021-11-03 17:23:25 +01:00
);
$cartReturn = array($dbpanier, $cartInfosNoPrice);
2022-10-03 17:13:44 +02:00
/** @var \App\Models\GeneralConfig */
$config = Cache::get('GeneralConfigGlobal')->where('type', GeneralConfigType::global())->first();
// ? Don't show prices only for auth customers ?
if (!$config->data['orders_display_prices_for_auth']['value']) {
return $cartReturn;
2021-11-03 17:23:25 +01:00
}
// ? Customer is auth ?
if (Auth::guard('frontend')->check()) {
$cartReturn = array($dbpanier, $cartInfos);
2021-11-03 17:23:25 +01:00
}
2021-03-25 15:48:07 +01:00
return $cartReturn;
2021-03-24 17:58:27 +01:00
}
}