203 lines
6.2 KiB
PHP
203 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\GeneralConfigType;
|
|
use App\Enums\WeightUnit;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Product;
|
|
use Gloudemans\Shoppingcart\Cart;
|
|
use Gloudemans\Shoppingcart\CartItem;
|
|
use Gloudemans\Shoppingcart\CartItemOptions;
|
|
use Gloudemans\Shoppingcart\Facades\Cart as FacadesCart;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class BasketController extends Controller
|
|
{
|
|
/**
|
|
* Get Basket
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getBasket()
|
|
{
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
/**
|
|
* Ajout d'un produit au panier
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function addToBasket(Product $product)
|
|
{
|
|
/** @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
|
|
/** @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
|
|
]);
|
|
|
|
$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)
|
|
{
|
|
/** @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);
|
|
}
|
|
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)
|
|
{
|
|
/** @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);
|
|
}
|
|
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()
|
|
{
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
$basket->destroy();
|
|
|
|
/** @var \Gloudemans\Shoppingcart\Cart */
|
|
$basket = app('basket');
|
|
|
|
return \response()->json($this->basketToList($basket));
|
|
}
|
|
|
|
|
|
/**
|
|
* return cart data array
|
|
*
|
|
* @param string $instance
|
|
* @return array
|
|
*/
|
|
public function getCart(string $instance = "panier")
|
|
{
|
|
Cart::instance($instance);
|
|
$dbpanier = new Collection();
|
|
foreach (Cart::content() as $row) {
|
|
$product = $row->model;
|
|
|
|
$product->calculatedPrices = $product->getCalcPrices();
|
|
|
|
$row->setTaxRate($product->tax->rate);
|
|
$row->setDiscountRate($product->discount_rate);
|
|
|
|
$cartextendedItem = array(
|
|
'cartItem' => $row,
|
|
'product' => $product
|
|
);
|
|
$dbpanier->add($cartextendedItem);
|
|
}
|
|
|
|
$cartInfos = array(
|
|
"totalTTC" => Cart::total(),
|
|
// Total des taxes.
|
|
'totalTVA' => Cart::tax(),
|
|
// Total sans les taxes.
|
|
'totalHT' => Cart::subtotal(),
|
|
// Poids total des items.
|
|
'totalweight' => Cart::weight(),
|
|
// Total avant taxes et remises.
|
|
'totalHTbefore' => Cart::priceTotal(),
|
|
// Total des remises.
|
|
'totaldiscount' => Cart::discount(),
|
|
// Nb de produits dans le cart.
|
|
'countItems' => Cart::count(),
|
|
);
|
|
|
|
$cartInfosNoPrice = array(
|
|
"totalTTC" => "-.--",
|
|
// Total des taxes.
|
|
'totalTVA' => "-.--",
|
|
// Total sans les taxes.
|
|
'totalHT' => "-.--",
|
|
// Poids total des items.
|
|
'totalweight' => Cart::weight(),
|
|
// Total avant taxes et remises.
|
|
'totalHTbefore' => "-.--",
|
|
// Total des remises.
|
|
'totaldiscount' => "-.--",
|
|
// Nb de produits dans le cart.
|
|
'countItems' => Cart::count(),
|
|
);
|
|
|
|
$cartReturn = array($dbpanier, $cartInfosNoPrice);
|
|
|
|
/** @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;
|
|
}
|
|
// ? Customer is auth ?
|
|
if (Auth::guard('frontend')->check()) {
|
|
$cartReturn = array($dbpanier, $cartInfos);
|
|
}
|
|
return $cartReturn;
|
|
}
|
|
}
|