/**
  * adds an product to the cart or increases its amount
  * If stock managament is activated:
  * -If the product's stock quantity is overbookable there are noc hanges in
  *  behaviour.
  * -If the stock quantity of a product is NOT overbookable and the $quantity
  *  is larger than the stock quantity $quantity will be set to stock quantity.
  * -If the stock quantity of a product is NOT overbookable and the products
  *  stock quantity is less than zero false will be returned.
  *
  * @param int     $cartID    ID of the users shopping cart
  * @param float   $quantity  Amount of products to be added
  * @param boolean $increment Set to true to increment the quantity instead
  *                           of setting it absolutely
  *
  * @return mixed SilvercartShoppingCartPosition|boolean false
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>,
  *         Roland Lehmann <*****@*****.**>
  * @since 16.06.2014
  */
 public function addToCart($cartID, $quantity = 1, $increment = false)
 {
     $addToCartAllowed = true;
     $this->extend('updateAddToCart', $addToCartAllowed);
     if ($this->IsNotBuyable) {
         return false;
     }
     if ($quantity == 0 || $cartID == 0) {
         return false;
     }
     if (!$addToCartAllowed) {
         return false;
     }
     if (!$this->isBuyableDueToStockManagementSettings()) {
         return false;
     }
     $shoppingCartPosition = SilvercartShoppingCartPosition::get()->filter(array('SilvercartProductID' => $this->ID, 'SilvercartShoppingCartID' => $cartID))->first();
     if (!$shoppingCartPosition) {
         $shoppingCartPosition = new SilvercartShoppingCartPosition();
         $shoppingCartPosition->castedUpdate(array('SilvercartShoppingCartID' => $cartID, 'SilvercartProductID' => $this->ID));
         $shoppingCartPosition->write();
         $shoppingCartPosition = SilvercartShoppingCartPosition::get()->filter(array('SilvercartProductID' => $this->ID, 'SilvercartShoppingCartID' => $cartID))->first();
     }
     $positionNotice = null;
     if ($shoppingCartPosition->Quantity < $quantity) {
         $quantityToAdd = $quantity - $shoppingCartPosition->Quantity;
         if ($shoppingCartPosition->isQuantityIncrementableBy($quantityToAdd)) {
             if ($quantity > SilvercartConfig::addToCartMaxQuantity()) {
                 $shoppingCartPosition->Quantity = SilvercartConfig::addToCartMaxQuantity();
                 $positionNotice = 'maxQuantityReached';
             } else {
                 $shoppingCartPosition->Quantity = $quantity;
             }
         } elseif ($this->StockQuantity > 0) {
             if ($shoppingCartPosition->Quantity + $this->StockQuantity > SilvercartConfig::addToCartMaxQuantity()) {
                 $shoppingCartPosition->Quantity = SilvercartConfig::addToCartMaxQuantity();
                 $positionNotice = 'maxQuantityReached';
             } else {
                 $shoppingCartPosition->Quantity = $this->StockQuantity;
                 $positionNotice = 'remaining';
             }
         } else {
             $shoppingCartPosition = false;
         }
     } else {
         if ($increment) {
             $shoppingCartPosition->Quantity += $quantity;
         } else {
             $shoppingCartPosition->Quantity = $quantity;
         }
     }
     if ($shoppingCartPosition instanceof SilvercartShoppingCartPosition) {
         $shoppingCartPosition->write();
         if (!is_null($positionNotice)) {
             SilvercartShoppingCartPositionNotice::setNotice($shoppingCartPosition->ID, $positionNotice);
         }
         SilvercartPlugin::call($this, 'onAfterAddToCart', array($shoppingCartPosition));
     }
     $this->extend('onAfterAddToCart', $shoppingCartPosition);
     return $shoppingCartPosition;
 }
 /**
  * Decrement the positions quantity if it is higher than the stock quantity.
  * If this position has a quantity of 5 but the products stock quantity is
  * only 3 the positions quantity would be set to 3.
  * This happens only if the product is not overbookable.
  * 
  * @return void
  * 
  * @author Roland Lehmann <*****@*****.**>, Sebastian Diel <*****@*****.**>
  * @since 26.11.2012
  */
 public function adjustQuantityToStockQuantity()
 {
     if (!SilvercartTools::isIsolatedEnvironment()) {
         if (SilvercartConfig::EnableStockManagement() && !$this->SilvercartProduct()->isStockQuantityOverbookable()) {
             if ($this->Quantity > $this->SilvercartProduct()->StockQuantity) {
                 $this->Quantity = $this->SilvercartProduct()->StockQuantity;
                 $this->write();
                 SilvercartShoppingCartPositionNotice::setNotice($this->ID, "adjusted");
             }
         }
     }
 }