/**
  * @throws OnlineShop_Framework_Exception_UnsupportedException
  */
 public function calculate()
 {
     //sum up all item prices
     $subTotal = 0;
     $currency = null;
     foreach ($this->cart->getItems() as $item) {
         if (is_object($item->getPrice())) {
             if (!$currency) {
                 $currency = $item->getPrice()->getCurrency();
             }
             if ($currency->compare($item->getPrice()->getCurrency()) != 0) {
                 throw new OnlineShop_Framework_Exception_UnsupportedException("Different currencies within one cart are not supported");
             }
             $subTotal += $item->getTotalPrice()->getAmount();
         }
     }
     //by default currency is retrieved from item prices. if there are no items, its loaded from the default locale defined in the environment
     if (!$currency) {
         $currency = $this->getDefaultCurrency();
     }
     $this->subTotal = $this->getDefaultPriceObject($subTotal, $currency);
     //consider all price modificators
     $currentSubTotal = $this->getDefaultPriceObject($subTotal, $currency);
     $this->modifications = array();
     foreach ($this->modificators as $modificator) {
         /* @var OnlineShop_Framework_ICartPriceModificator $modificator */
         $modification = $modificator->modify($currentSubTotal, $this->cart);
         if ($modification !== null) {
             $this->modifications[$modificator->getName()] = $modification;
             $currentSubTotal->setAmount($currentSubTotal->getAmount() + $modification->getAmount());
         }
     }
     $this->gradTotal = $currentSubTotal;
     $this->isCalculated = true;
 }
Beispiel #2
0
 public function updateOfferFromCart(OnlineShop_OfferTool_AbstractOffer $offer, OnlineShop_Framework_ICart $cart, array $excludeItems = array(), $save = true)
 {
     $excludedItemKeys = $this->getExcludedItemKeys($excludeItems);
     if ($cart->getId() != $offer->getCartId()) {
         throw new Exception("Cart does not match to the offer given, update is not possible");
     }
     //Update existing offer items
     $offerItems = $offer->getItems();
     $newOfferItems = array();
     foreach ($offerItems as $offerItem) {
         $cartItem = $cart->getItem($offerItem->getCartItemKey());
         if ($cartItem && !$excludedItemKeys[$offerItem->getCartItemKey()]) {
             $newOfferItems[$offerItem->getCartItemKey()] = $this->updateOfferItem($cartItem, $offerItem);
         }
     }
     //Add non existing cart items to offer
     $cartItems = $cart->getItems();
     foreach ($cartItems as $cartItem) {
         if (!$newOfferItems[$cartItem->getItemKey()] && !$excludedItemKeys[$cartItem->getItemKey()]) {
             $offerItem = $this->createOfferItem($cartItem, $offer);
             $newOfferItems[$offerItem->getCartItemKey()] = $offerItem;
         }
     }
     //Delete offer items which are not needed any more
     foreach ($offerItems as $offerItem) {
         if (!$newOfferItems[$offerItem->getCartItemKey()]) {
             $offerItem->delete();
         }
     }
     $offer->setItems($newOfferItems);
     //Update total price
     $offer = $this->updateTotalPriceOfOffer($offer);
     if ($save) {
         $offer->save();
     }
     return $offer;
 }