Inital commit

This commit is contained in:
2020-11-19 15:36:28 +01:00
parent 71f32f83d3
commit 66ce4ee218
18077 changed files with 2166122 additions and 35184 deletions

View File

@@ -3,23 +3,37 @@
namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Event\Cart\CartItemDuplicationItem;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Cart as BaseCart;
use Thelia\Model\Country;
use Thelia\Model\State;
class Cart extends BaseCart
{
/**
* Duplicate the current existing cart. Only the token is changed
*
* @param $token
* @param Customer $customer
* @param string $token
* @param Customer $customer
* @param Currency $currency
* @param EventDispatcherInterface $dispatcher
* @return Cart
* @throws \Exception
* @throws \Propel\Runtime\Exception\PropelException
*/
public function duplicate($token, Customer $customer = null, Currency $currency = null, EventDispatcherInterface $dispatcher)
{
public function duplicate(
$token,
Customer $customer = null,
Currency $currency = null,
EventDispatcherInterface $dispatcher = null
) {
if (!$dispatcher) {
return false;
}
$cartItems = $this->getCartItems();
$cart = new Cart();
@@ -45,15 +59,12 @@ class Cart extends BaseCart
$cart->save();
foreach ($cartItems as $cartItem) {
$product = $cartItem->getProduct();
$productSaleElements = $cartItem->getProductSaleElements();
if ($product &&
$productSaleElements &&
$product->getVisible() == 1 &&
($productSaleElements->getQuantity() > $cartItem->getQuantity() || ! ConfigQuery::checkAvailableStock()))
{
($productSaleElements->getQuantity() >= $cartItem->getQuantity() || $product->getVirtual() === 1 || ! ConfigQuery::checkAvailableStock())) {
$item = new CartItem();
$item->setCart($cart);
$item->setProductId($cartItem->getProductId());
@@ -68,9 +79,13 @@ class Cart extends BaseCart
$item->save();
$dispatcher->dispatch(TheliaEvents::CART_ITEM_DUPLICATE, new CartItemDuplicationItem($item, $cartItem));
}
}
$this->delete();
try {
$this->delete();
} catch (\Exception $e) {
// just fail silently in some cases
}
return $cart;
}
@@ -97,20 +112,25 @@ class Cart extends BaseCart
*
* /!\ The postage amount is not available so it's the total with or without discount an without postage
*
* @param Country $country
* @param bool $discount
* @param Country $country
* @param bool $discount
* @param State|null $state
* @return float|int
*/
public function getTaxedAmount(Country $country, $discount = true)
public function getTaxedAmount(Country $country, $discount = true, State $state = null)
{
$total = 0;
foreach ($this->getCartItems() as $cartItem) {
$total += $cartItem->getRealTaxedPrice($country) * $cartItem->getQuantity();
$total += $cartItem->getTotalRealTaxedPrice($country, $state);
}
if ($discount) {
$total -= $this->getDiscount();
if ($total < 0) {
$total = 0;
}
}
return $total;
@@ -119,7 +139,7 @@ class Cart extends BaseCart
/**
*
* @see getTaxedAmount same as this method but the amount is without taxes
* @param bool $discount
* @param bool $discount
* @return float|int
*/
public function getTotalAmount($discount = true)
@@ -128,18 +148,33 @@ class Cart extends BaseCart
foreach ($this->getCartItems() as $cartItem) {
$subtotal = $cartItem->getRealPrice();
$subtotal *= $cartItem->getQuantity();
$total += $subtotal;
}
if ($discount) {
// discount value is taxed see ISSUE #1476
$total -= $this->getDiscount();
if ($total < 0) {
$total = 0;
}
}
return $total;
}
/**
* Return the VAT of all items
* @return float|int
*/
public function getTotalVAT($taxCountry, $taxState = null)
{
return ($this->getTaxedAmount($taxCountry, true, $taxState) - $this->getTotalAmount(true));
}
/**
* Retrieve the total weight for all products in cart
*
@@ -158,4 +193,25 @@ class Cart extends BaseCart
return $weight;
}
/**
* Tell if the cart contains only virtual products
*
* @return bool
*/
public function isVirtual()
{
foreach ($this->getCartItems() as $cartItem) {
if (0 < $cartItem->getProductSaleElements()->getWeight()) {
return false;
}
$product = $cartItem->getProductSaleElements()->getProduct();
if (!$product->getVirtual()) {
return false;
}
}
return $this->getCartItems()->count() > 0;
}
}