예제 #1
0
 /**
  * override for changing the current stock qty based on the quote_item
  * (this is a bad design from Magento, checkQuoteItemQty should receive the quoteItem in the original code too)
  *
  * @param mixed $qty
  * @param mixed $summaryQty
  * @param int $origQty
  * @param Mage_Sales_Model_Quote_Item $quoteItem
  *
  * @return Varien_Object
  */
 public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0, $quoteItem = null, $product = null)
 {
     if ($quoteItem && ITwebexperts_Payperrentals_Helper_Data::isReservationOnly($product) && (!$quoteItem->getChildren() || Mage::app()->getStore()->isAdmin())) {
         $qtyOption = 1;
         if ($quoteItem->getParentProductQty()) {
             $qty = $quoteItem->getParentProductQty();
             $qtyOption = $quoteItem->getParentProductQtyOption();
         }
         $quoteItemId = $quoteItem->getId();
         if ($quoteItem->getParentItem() && $quoteItem->getParentItem()->getProduct()) {
             if ($quoteItem->getParentItem()->getProduct()->getTypeId() == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_BUNDLE) {
                 $quoteItemId = $quoteItem->getParentItem()->getItemId();
             }
         }
         $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($product, $quoteItem);
         list($startDate, $endDate) = array($turnoverArr['before'], $turnoverArr['after']);
         $result = new Varien_Object();
         $result->setHasError(false);
         if (!is_numeric($qty)) {
             $qty = Mage::app()->getLocale()->getNumber($qty);
         }
         /**
          * Check quantity type
          */
         $result->setItemIsQtyDecimal($this->getIsQtyDecimal());
         if (!$this->getIsQtyDecimal()) {
             $result->setHasQtyOptionUpdate(true);
             $qty = intval($qty);
             /**
              * Adding stock data to quote item
              */
             $result->setItemQty($qty);
             if (!is_numeric($qty)) {
                 $qty = Mage::app()->getLocale()->getNumber($qty);
             }
             $origQty = intval($origQty);
             $result->setOrigQty($origQty);
         }
         if ($qty < ITwebexperts_Payperrentals_Helper_Inventory::getMinSaleQuantity($product)) {
             $result->setHasError(true)->setMessage(Mage::helper('cataloginventory')->__('The minimum quantity allowed for purchase is %s.', ITwebexperts_Payperrentals_Helper_Inventory::getMinSaleQuantity($product)))->setErrorCode('qty_min')->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products cannot be ordered in requested quantity.'))->setQuoteMessageIndex('qty');
             return $result;
         }
         if ($qty > ITwebexperts_Payperrentals_Helper_Inventory::getMaxSaleQuantity($product)) {
             $result->setHasError(true)->setMessage(Mage::helper('cataloginventory')->__('The maximum quantity allowed for purchase is %s.', ITwebexperts_Payperrentals_Helper_Inventory::getMaxSaleQuantity($product)))->setErrorCode('qty_max')->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products cannot be ordered in requested quantity.'))->setQuoteMessageIndex('qty');
             return $result;
         }
         $result->addData($this->checkQtyIncrements($qty)->getData());
         if ($result->getHasError()) {
             return $result;
         }
         $option = $quoteItem->getOptionByCode('info_buyRequest');
         $buyRequest = $option ? unserialize($option->getValue()) : null;
         if (Mage::app()->getStore()->isAdmin() && Mage::getSingleton('adminhtml/session_quote')->getOrderId()) {
             $editedOrderId = Mage::getSingleton('adminhtml/session_quote')->getOrderId();
             $order = Mage::getModel('sales/order')->load($editedOrderId);
             $buyRequestArray = (array) $buyRequest;
             foreach ($order->getAllItems() as $oItem) {
                 if ($oItem->getProduct()->getId() != $product->getId()) {
                     continue;
                 }
                 if (is_object($oItem->getOrderItem())) {
                     $item = $oItem->getOrderItem();
                 } else {
                     $item = $oItem;
                 }
                 if ($item->getParentItem()) {
                     continue;
                 }
                 //check for bundles
                 if (count($item->getChildrenItems()) > 0) {
                     foreach ($item->getChildrenItems() as $child) {
                         $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($child->getProductId(), $child);
                         $buyRequestArray['excluded_qty'][] = array('product_id' => $child->getProductId(), 'start_date' => $turnoverArr['before'], 'end_date' => $turnoverArr['after'], 'qty' => $item->getQtyOrdered());
                     }
                 } else {
                     $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($item->getProductId(), $item);
                     $buyRequestArray['excluded_qty'][] = array('product_id' => $item->getProductId(), 'start_date' => $turnoverArr['before'], 'end_date' => $turnoverArr['after'], 'qty' => $item->getQtyOrdered());
                 }
             }
             $buyRequest = new Varien_Object($buyRequestArray);
         }
         if (isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) {
             if (strtotime($buyRequest['start_date']) > strtotime($buyRequest['end_date'])) {
                 $message = Mage::helper('payperrentals')->__('Start Date is bigger then End Date. Please change!');
                 $result->setHasError(true)->setMessage($message)->setQuoteMessage($message);
                 return $result;
             }
         }
         $isAvailable = false;
         if (ITwebexperts_Payperrentals_Helper_Inventory::isAllowedOverbook($product->getId())) {
             $isAvailable = true;
         }
         if (ITwebexperts_Payperrentals_Helper_Data::isBuyout($buyRequest)) {
             if (!$isAvailable) {
                 Mage::register('quote_item_id', $quoteItemId);
                 $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, null, null, $buyRequest);
                 Mage::unregister('quote_item_id');
                 if ($maxQty >= $qty) {
                     $isAvailable = true;
                 }
             }
             if ($isAvailable) {
                 return $result;
             } else {
                 $message = Mage::helper('payperrentals')->__('There is not enough quantity for buy this product');
                 $result->setHasError(true)->setMessage($message)->setQuoteMessage($message);
                 return $result;
             }
         }
         if (!Mage::app()->getStore()->isAdmin() && !Mage::getSingleton('checkout/session')->getIsExtendedQuote() && isset($buyRequest['start_date']) && isset($buyRequest['end_date']) && ITwebexperts_Payperrentals_Helper_Inventory::isExcludedDay($product->getId(), $buyRequest['start_date'], $buyRequest['end_date'])) {
             $message = Mage::helper('payperrentals')->__('There are blocked dates or excluded days on your selected dates!');
             $result->setHasError(true)->setMessage($message)->setQuoteMessage($message);
             return $result;
         } else {
             if (!$isAvailable && isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) {
                 Mage::register('quote_item_id', $quoteItemId);
                 $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest);
                 Mage::unregister('quote_item_id');
                 if ($maxQty >= $qty) {
                     $isAvailable = true;
                 }
             }
         }
         if (ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart($product) && !$buyRequest['start_date']) {
             $message = Mage::helper('payperrentals')->__('Please select Global Dates!');
             $result->setHasError(true)->setMessage($message)->setQuoteMessage($message);
             return $result;
         }
         if (!Mage::app()->getStore()->isAdmin() && !ITwebexperts_Payperrentals_Helper_Data::isBuyout($buyRequest) && (isset($buyRequest['start_date']) && strtotime($buyRequest['start_date']) < strtotime(date('Y-m-d')) || isset($buyRequest['end_date']) && strtotime($buyRequest['end_date']) < strtotime(date('Y-m-d')))) {
             $message = Mage::helper('payperrentals')->__('The selected Dates are in the past. Please select new dates!');
             $result->setHasError(true)->setMessage($message)->setQuoteMessage($message);
             return $result;
         }
         if (Mage::app()->getStore()->isAdmin() && ITwebexperts_Payperrentals_Helper_Inventory::isAllowedOverbook($product->getId()) && ITwebexperts_Payperrentals_Helper_Inventory::showAdminOverbookWarning()) {
             Mage::register('quote_item_id', $quoteItemId);
             $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest, true);
             Mage::unregister('quote_item_id');
             if ($maxQty < $qty) {
                 $message = Mage::helper('cataloginventory')->__('Product is not available for the selected dates');
                 $result->setHasError(false)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qtyppr');
                 return $result;
             }
         }
         Mage::dispatchEvent('before_stock_check', array('buyrequest' => $buyRequest, 'isavailable' => &$isAvailable));
         if (!$isAvailable) {
             if (isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) {
                 //Mage::register('no_quote', 1);
                 Mage::register('quote_item_id', $quoteItemId);
                 $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest);
                 Mage::unregister('quote_item_id');
                 $maxQty = intval($maxQty / $qtyOption);
                 //Mage::unregister('no_quote', 1);
                 if ($maxQty > 0) {
                     $message = Mage::helper('payperrentals')->__('Max quantity available for these dates is: ' . $maxQty . ', your quantity has been adjusted.');
                     // Mage::getSingleton('checkout/session')->addError($message);
                     $result->setHasQtyOptionUpdate(true);
                     //$result->setOrigQty($maxQty);
                     $result->setQty($maxQty);
                     $result->setItemQty($maxQty);
                     $result->setMessage($message)->setQuoteMessage($message);
                     return $result;
                 }
                 $message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available.', $this->getProductName());
             } else {
                 $message = Mage::helper('cataloginventory')->__('Please select Start and End Dates');
             }
             $result->setHasError(true)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qtyppr');
             return $result;
         }
         return $result;
     }
     $return = parent::checkQuoteItemQty($qty, $summaryQty, $origQty);
     return $return;
 }
예제 #2
0
 /**
  * Returns the array of booked days, for the quantity and selected dates
  *
  * @return array
  */
 public function updateBookedForProductAction()
 {
     if (!$this->getRequest()->getParam('product_id')) {
         $bookedHtml = array('bookedDates' => '', 'fixedRentalDates' => '', 'isDisabled' => true, 'isShoppingCartDates' => ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart() ? true : false, 'partiallyBooked' => '');
         $this->getResponse()->setBody(Zend_Json::encode($bookedHtml));
         return;
     }
     $product = ITwebexperts_Payperrentals_Helper_Data::initProduct($this->getRequest()->getParam('product_id'));
     //todo might not be necessary
     $qty = $this->getRequest()->getParam('qty') ? $this->getRequest()->getParam('qty') : 1;
     $normalPrice = '';
     $needsConfigure = false;
     $isConfigurable = false;
     $isDisabled = false;
     $attributes = $this->getRequest()->getParam('super_attribute') ? $this->getRequest()->getParam('super_attribute') : null;
     $bundleOptions = $this->getRequest()->getParam('bundle_option') ? $this->getRequest()->getParam('bundle_option') : null;
     $bundleOptionsQty1 = $this->getRequest()->getParam('bundle_option_qty1') ? $this->getRequest()->getParam('bundle_option_qty1') : null;
     $bundleOptionsQty = $this->getRequest()->getParam('bundle_option_qty') ? $this->getRequest()->getParam('bundle_option_qty') : null;
     $qtyArr = ITwebexperts_Payperrentals_Helper_Inventory::getQuantityArrayForProduct($product, 1, $attributes, $bundleOptions, $bundleOptionsQty1, $bundleOptionsQty, true);
     $maxQtyArr = array();
     $bookedDates = array();
     $partiallyBookedDates = array();
     $turnoverTimeBefore = 0;
     $turnoverTimeAfter = 0;
     $maxQtyForParentProduct = 10000;
     /**
      * We get the maximum qty available at this moment for every associated product
      */
     foreach ($qtyArr as $iProduct => $iQty) {
         if (ITwebexperts_Payperrentals_Helper_Inventory::isAllowedOverbook($iProduct)) {
             $maxQtyForChildProduct = 100000;
         } else {
             $maxQtyForChildProduct = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($iProduct);
         }
         $qtyForParentProduct = intval($maxQtyForChildProduct / $iQty);
         $maxQtyArr[$iProduct] = $maxQtyForChildProduct;
         if ($maxQtyForParentProduct > $qtyForParentProduct) {
             $maxQtyForParentProduct = $qtyForParentProduct;
         }
         if ($maxQtyForChildProduct < $iQty * $qty) {
             $isDisabled = true;
             break;
         }
     }
     if (!$isDisabled) {
         $resProductArrIds = ITwebexperts_Payperrentals_Helper_Inventory::getReservationProductsArrayIds($product->getId(), $attributes, $bundleOptions);
         $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($product->getId(), null, null, null, 'days');
         $turnoverTimeBefore = $turnoverArr['before'];
         $turnoverTimeAfter = $turnoverArr['after'];
         foreach ($resProductArrIds as $resProductId) {
             $booked = ITwebexperts_Payperrentals_Helper_Inventory::getBookedQtyForProducts($resProductId);
             //Normally if shipping method with turnovers is used global turnovers should be null
             if ($turnoverTimeAfter == 0 && $turnoverTimeBefore == 0) {
                 $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($resProductId, null, null, null, 'days');
                 $turnoverTimeBefore = $turnoverArr['before'];
                 $turnoverTimeAfter = $turnoverArr['after'];
             }
             /**
              * We go product by product(in the associated products) and date by date and subtract from
              * this moment qty the booked qty for that date This is how calendar is created.
              */
             if (isset($booked[$resProductId])) {
                 foreach ($booked[$resProductId] as $dateFormatted => $vObject) {
                     $qtyAvailForProduct = intval(($maxQtyArr[$resProductId] - $vObject->getQty()) / $qtyArr[$resProductId]);
                     $isPartial = false;
                     if (date('H:i', strtotime($dateFormatted)) != '00:00') {
                         if (!isset($partiallyBookedDates[$dateFormatted . ':00'])) {
                             $partiallyBookedDates[$dateFormatted . ':00'] = $qtyAvailForProduct;
                         } else {
                             $partiallyBookedDates[$dateFormatted . ':00'] = min($qtyAvailForProduct, $partiallyBookedDates[$dateFormatted . ':00']);
                         }
                         $isPartial = true;
                     }
                     if (!$isPartial) {
                         if (!isset($bookedDates[$dateFormatted])) {
                             $bookedDates[$dateFormatted] = $qtyAvailForProduct;
                         } else {
                             $bookedDates[$dateFormatted] = min($bookedDates[$dateFormatted], $qtyAvailForProduct);
                         }
                     }
                 }
             }
         }
         $bookedDates = ITwebexperts_Payperrentals_Helper_Data::toFormattedBookedArray($bookedDates, false);
     }
     $typeId = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($product->getId(), 'type_id');
     if ($typeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_CONFIGURABLE) {
         reset($qtyArr);
         $normalPrice = ITwebexperts_Payperrentals_Helper_Price::getPriceListHtml(key($qtyArr));
         $isConfigurable = true;
         $blockedDates = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates(key($qtyArr));
         $fixedRentalDates = ITwebexperts_Payperrentals_Helper_Data::getFixedRentalDates(key($qtyArr));
     } else {
         $blockedDates = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates($product->getId());
         $fixedRentalDates = ITwebexperts_Payperrentals_Helper_Data::getFixedRentalDates($product->getId());
     }
     $blockedDates = ITwebexperts_Payperrentals_Helper_Data::toFormattedDateArray($blockedDates, false);
     $fixedRentalDates = ITwebexperts_Payperrentals_Helper_Data::toFormattedArraysOfDatesArray($fixedRentalDates, false);
     $bookedHtml = array('bookedDates' => $bookedDates, 'fixedRentalDates' => $fixedRentalDates, 'isDisabled' => $isDisabled, 'blockedDates' => implode(',', $blockedDates), 'isShoppingCartDates' => ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart() ? true : false, 'isConfigurable' => $isConfigurable, 'needsConfigure' => $needsConfigure, 'normalPrice' => $normalPrice, 'partiallyBooked' => $partiallyBookedDates);
     $bookedHtml['turnoverTimeBefore'] = $turnoverTimeBefore;
     $bookedHtml['turnoverTimeAfter'] = $turnoverTimeAfter;
     $bookedHtml['maxQty'] = $maxQtyForParentProduct;
     $disabledDaysStart = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysStart();
     $disabledDaysEnd = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysEnd();
     if (count($disabledDaysStart) > 0) {
         $bookedHtml['disabledForStartRange'] = $disabledDaysStart;
     }
     if (count($disabledDaysEnd) > 0) {
         $bookedHtml['disabledForEndRange'] = $disabledDaysEnd;
     }
     if (Mage::helper('payperrentals/config')->getFutureReservationLimit($product) > 0) {
         $bookedHtml['futureDate'] = date('Y-m-d', time() + Mage::helper('payperrentals/config')->getFutureReservationLimit($product) * 3600 * 24);
     }
     Mage::dispatchEvent('ppr_get_booked_html_for_products', array('request_params' => $this->getRequest()->getParams(), 'booked_html' => &$bookedHtml, 'product' => $product));
     $this->getResponse()->setBody(Zend_Json::encode($bookedHtml));
 }
예제 #3
0
 /**
  * Checks if newly added product to cart uses same start and end dates as the other products
  *
  * @param Varien_Event_Observer $_observer
  */
 public function sameDatesAllProducts(Varien_Event_Observer $observer)
 {
     $helper = Mage::helper('payperrentals');
     if (!ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart() && Mage::getStoreConfig('payperrentals/calendar_options/enforce_same_dates') == 1) {
         if ($observer->getEvent()->getQuote()) {
             $quote = $observer->getEvent()->getQuote();
         } else {
             $quote = Mage::getSingleton('checkout/session')->getQuote();
         }
         $quoteItems = $quote->getAllItems();
         $firstQuoteItem = false;
         foreach ($quoteItems as $quoteItem) {
             if ($quoteItem->getParentItem()) {
                 continue;
             }
             $options = $quoteItem->getOptionsByCode();
             $buyRequest = $options['info_buyRequest'];
             if (Mage::getSingleton('checkout/session')->getIsExtendedQuote() && !isset($buyRequest['is_extended'])) {
                 Mage::getSingleton('checkout/session')->addError($helper->__('This is an extended order you cannot add other items!'));
                 Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
                 Mage::app()->getResponse()->sendResponse();
                 exit;
             }
             $buyRequest = $quoteItem->getBuyRequest();
             if ($buyRequest->getStartDate() && $buyRequest->getEndDate()) {
                 if (!$firstQuoteItem) {
                     $startdatetime = new DateTime($buyRequest->getStartDate());
                     $startFormatted = $startdatetime->format('Y-m-d H:i:s');
                     $enddatetime = new DateTime($buyRequest->getEndDate());
                     $endFormatted = $enddatetime->format('Y-m-d H:i:s');
                     $counter = 0;
                     foreach ($quoteItems as $quoteItemNew) {
                         if ($quoteItemNew->getParentItem()) {
                             continue;
                         }
                         $buyRequestNew = $quoteItemNew->getBuyRequest();
                         if ($buyRequestNew->getStartDate() && $buyRequestNew->getEndDate()) {
                             if ($counter > 0) {
                                 $startdatetimeNew = new DateTime($buyRequestNew->getStartDate());
                                 $startFormattedNew = $startdatetimeNew->format('Y-m-d H:i:s');
                                 $enddatetimeNew = new DateTime($buyRequestNew->getEndDate());
                                 $endFormattedNew = $enddatetimeNew->format('Y-m-d H:i:s');
                                 if ($startFormatted != $startFormattedNew || $endFormatted != $endFormattedNew) {
                                     Mage::getSingleton('checkout/session')->addError($helper->__('All products must use the same start and end dates'));
                                     Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
                                     Mage::app()->getResponse()->sendResponse();
                                     exit;
                                 }
                             }
                             $counter++;
                         }
                     }
                 }
             }
             break;
         }
     }
 }
예제 #4
0
 /**
  * Check product inventory data when quote item quantity declaring
  *
  * @param  Varien_Event_Observer $observer
  * @return Mage_CatalogInventory_Model_Observer
  */
 public function checkQuoteItemQty($observer)
 {
     $quoteItem = $observer->getEvent()->getItem();
     /* @var $quoteItem Mage_Sales_Model_Quote_Item */
     if (!$quoteItem || !$quoteItem->getProductId() || !$quoteItem->getQuote() || $quoteItem->getQuote()->getIsSuperMode()) {
         return $this;
     }
     /**
      * Get Qty
      */
     $qty = $quoteItem->getQty();
     /**
      * Check if product in stock. For composite products check base (parent) item stosk status
      */
     $stockItem = $quoteItem->getProduct()->getStockItem();
     $parentStockItem = false;
     if ($quoteItem->getParentItem()) {
         $parentStockItem = $quoteItem->getParentItem()->getProduct()->getStockItem();
     }
     if ($stockItem) {
         if (!$stockItem->getIsInStock() || $parentStockItem && !$parentStockItem->getIsInStock()) {
             $quoteItem->addErrorInfo('cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, Mage::helper('cataloginventory')->__('This product is currently out of stock.'));
             $quoteItem->getQuote()->addErrorInfo('stock', 'cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, Mage::helper('cataloginventory')->__('Some of the products are currently out of stock.'));
             return $this;
         } else {
             // Delete error from item and its quote, if it was set due to item out of stock
             $this->_removeErrorsFromQuoteAndItem($quoteItem, Mage_CatalogInventory_Helper_Data::ERROR_QTY);
         }
     }
     /**
      * Check item for options
      */
     $options = $quoteItem->getQtyOptions();
     if ($options && $qty > 0) {
         $qty = $quoteItem->getProduct()->getTypeInstance(true)->prepareQuoteItemQty($qty, $quoteItem->getProduct());
         $quoteItem->setData('qty', $qty);
         if ($stockItem) {
             $result = $stockItem->checkQtyIncrements($qty);
             if ($result->getHasError()) {
                 $quoteItem->addErrorInfo('cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY_INCREMENTS, $result->getMessage());
                 $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY_INCREMENTS, $result->getQuoteMessage());
             } else {
                 // Delete error from item and its quote, if it was set due to qty problems
                 $this->_removeErrorsFromQuoteAndItem($quoteItem, Mage_CatalogInventory_Helper_Data::ERROR_QTY_INCREMENTS);
             }
         }
         $quoteItemHasErrors = false;
         foreach ($options as $option) {
             $optionValue = $option->getValue();
             /* @var $option Mage_Sales_Model_Quote_Item_Option */
             $optionQty = $qty * $optionValue;
             $increaseOptionQty = ($quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty) * $optionValue;
             $stockItem = $option->getProduct()->getStockItem();
             if ($quoteItem->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
                 $stockItem->setProductName($quoteItem->getName());
             }
             /* @var $stockItem Mage_CatalogInventory_Model_Stock_Item */
             if (!$stockItem instanceof Mage_CatalogInventory_Model_Stock_Item) {
                 Mage::throwException(Mage::helper('cataloginventory')->__('The stock item for Product in option is not valid.'));
             }
             /**
              * define that stock item is child for composite product
              */
             $stockItem->setIsChildItem(true);
             /**
              * don't check qty increments value for option product
              */
             $stockItem->setSuppressCheckQtyIncrements(true);
             $qtyForCheck = $this->_getQuoteItemQtyForCheck($option->getProduct()->getId(), $quoteItem->getId(), $increaseOptionQty);
             $result = $stockItem->checkQuoteItemQty($optionQty, $qtyForCheck, $optionValue, $quoteItem, $option->getProduct(), true);
             foreach ($quoteItem->getChildren() as $children) {
                 $children->setParentProductQty($qtyForCheck);
                 $children->setParentProductQtyOption(intval($qtyForCheck / $qty));
             }
             if (!is_null($result->getItemIsQtyDecimal())) {
                 $option->setIsQtyDecimal($result->getItemIsQtyDecimal());
             }
             if ($result->getHasQtyOptionUpdate()) {
                 $option->setHasQtyOptionUpdate(true);
                 $quoteItem->updateQtyOption($option, $result->getOrigQty());
                 $option->setValue($result->getOrigQty());
                 if ($result->getQty()) {
                     $qty = $result->getQty();
                 }
                 /**
                  * if option's qty was updates we also need to update quote item qty
                  */
                 $quoteItem->setData('qty', intval($qty));
             }
             if (!is_null($result->getMessage())) {
                 $option->setMessage($result->getMessage());
                 $quoteItem->setMessage($result->getMessage());
             }
             if (!is_null($result->getItemBackorders())) {
                 $option->setBackorders($result->getItemBackorders());
             }
             if ($result->getHasError()) {
                 $option->setHasError(true);
                 $quoteItemHasErrors = true;
                 $quoteItem->addErrorInfo('cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, $result->getMessage());
                 $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, $result->getQuoteMessage());
             } elseif (!$quoteItemHasErrors) {
                 // Delete error from item and its quote, if it was set due to qty lack
                 $this->_removeErrorsFromQuoteAndItem($quoteItem, Mage_CatalogInventory_Helper_Data::ERROR_QTY);
             }
             $stockItem->unsIsChildItem();
         }
     } else {
         /* @var $stockItem Mage_CatalogInventory_Model_Stock_Item */
         if (!$stockItem instanceof Mage_CatalogInventory_Model_Stock_Item) {
             Mage::throwException(Mage::helper('cataloginventory')->__('The stock item for Product is not valid.'));
         }
         /**
          * When we work with subitem (as subproduct of bundle or configurable product)
          */
         if ($quoteItem->getParentItem()) {
             $rowQty = $quoteItem->getParentItem()->getQty() * $qty;
             /**
              * we are using 0 because original qty was processed
              */
             $qtyForCheck = $this->_getQuoteItemQtyForCheck($quoteItem->getProduct()->getId(), $quoteItem->getId(), 0);
         } else {
             $increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty;
             $rowQty = $qty;
             $qtyForCheck = $this->_getQuoteItemQtyForCheck($quoteItem->getProduct()->getId(), $quoteItem->getId(), $increaseQty);
         }
         $productTypeCustomOption = $quoteItem->getProduct()->getCustomOption('product_type');
         if (!is_null($productTypeCustomOption)) {
             // Check if product related to current item is a part of grouped product
             if ($productTypeCustomOption->getValue() == Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
                 $stockItem->setProductName($quoteItem->getProduct()->getName());
                 $stockItem->setIsChildItem(true);
             }
         }
         $result = $stockItem->checkQuoteItemQty($rowQty, $qtyForCheck, $qty, $quoteItem, $quoteItem->getProduct());
         if ($stockItem->hasIsChildItem()) {
             $stockItem->unsIsChildItem();
         }
         if (!is_null($result->getItemIsQtyDecimal())) {
             $quoteItem->setIsQtyDecimal($result->getItemIsQtyDecimal());
             if ($quoteItem->getParentItem()) {
                 $quoteItem->getParentItem()->setIsQtyDecimal($result->getItemIsQtyDecimal());
             }
         }
         /**
          * Just base (parent) item qty can be changed
          * qty of child products are declared just during add process
          * exception for updating also managed by product type
          */
         if ($result->getHasQtyOptionUpdate() && (!$quoteItem->getParentItem() || $quoteItem->getParentItem()->getProduct()->getTypeInstance(true)->getForceChildItemQtyChanges($quoteItem->getParentItem()->getProduct()))) {
             $quoteItem->setData('qty', $result->getOrigQty());
         }
         if ($result->getQty()) {
             $qty = $result->getQty();
             if ($quoteItem->getParentItem()) {
                 $quoteItem->getParentItem()->setData('qty', intval($qty));
                 $quoteItem->getParentItem()->addErrorInfo('cataloginventory', 3, $result->getMessage());
                 Mage::getSingleton('checkout/session')->addNotice(Mage::helper('payperrentals')->__('For some products in cart the quantities has been adjusted'));
             } else {
                 $quoteItem->setData('qty', intval($qty));
                 $quoteItem->addErrorInfo('cataloginventory', 3, $result->getMessage());
                 Mage::getSingleton('checkout/session')->addNotice(Mage::helper('payperrentals')->__('For some products in cart the quantities has been adjusted'));
             }
         }
         if (!is_null($result->getItemUseOldQty())) {
             $quoteItem->setUseOldQty($result->getItemUseOldQty());
         }
         if (!is_null($result->getMessage())) {
             $quoteItem->setMessage($result->getMessage());
         }
         if (!is_null($result->getItemBackorders())) {
             $quoteItem->setBackorders($result->getItemBackorders());
         }
         if ($result->getHasError()) {
             if (!ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart()) {
                 $quoteItem->addErrorInfo('cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, $result->getMessage());
             }
             $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', Mage_CatalogInventory_Helper_Data::ERROR_QTY, $result->getQuoteMessage());
         } else {
             // Delete error from item and its quote, if it was set due to qty lack
             $this->_removeErrorsFromQuoteAndItem($quoteItem, Mage_CatalogInventory_Helper_Data::ERROR_QTY);
         }
     }
     return $this;
 }
예제 #5
0
 private function _completeBuyRequestObject(&$buyRequest, $product, $productType)
 {
     $msg = '';
     $useNonSequential = Mage::helper('payperrentals/config')->isNonSequentialSelect(Mage::app()->getStore()->getId());
     if (!$buyRequest->getStartDate()) {
         //if (ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDates($product)) {
         if (Mage::getSingleton('core/session')->getData('startDateInitial')) {
             $buyRequest->setStartDate(Mage::getSingleton('core/session')->getData('startDateInitial'));
             if (Mage::getSingleton('core/session')->getData('endDateInitial')) {
                 $buyRequest->setEndDate(Mage::getSingleton('core/session')->getData('endDateInitial'));
             }
             $resultObject = new Varien_Object();
             Mage::dispatchEvent('init_globals_prepare_advanced', array('buy_request' => $buyRequest, 'productType' => $productType, 'product' => $product, 'result' => $resultObject));
             if ($resultObject->getResult() != '') {
                 $msg = $resultObject->getResult();
             }
         } else {
             if (!ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart($product)) {
                 $msg = Mage::helper('payperrentals')->__('Please specify reservation information');
             } else {
                 $msg = 'call_parent';
             }
         }
         //}
     } else {
         $this->_setNonSequentialInBuyRequest($buyRequest, $useNonSequential);
         if (!$buyRequest->getEndDate()) {
             $buyRequest->setEndDate($buyRequest->getStartDate());
         }
         list($startDate, $endDate) = ITwebexperts_Payperrentals_Helper_Date::convertDatepickerToDbFormat($buyRequest->getStartDate(), $buyRequest->getEndDate());
         $buyRequest->setStartDate($startDate);
         $buyRequest->setEndDate($endDate);
         if ($buyRequest->getStartDate() == $buyRequest->getEndDate()) {
             if (date('H:i:s', strtotime($buyRequest->getStartDate())) == '00:00:00') {
                 $buyRequest->setEndDate(date('Y-m-d', strtotime($buyRequest->getStartDate())) . ' 23:59:59');
             }
         }
     }
     if (!$buyRequest->getStartDate()) {
         if (Mage::helper('payperrentals/config')->NoCalendarUseTodayAsStartDate()) {
             if (Mage::helper('payperrentals/config')->isNextHourSelection()) {
                 $buyRequest->setStartDate(date('Y-m-d', strtotime('+1 day', time())) . ' 00:00:00');
             } else {
                 $buyRequest->setStartDate(date('Y-m-d', strtotime('+0 day', time())) . ' 00:00:00');
             }
         }
     }
     if ($buyRequest->getSelectedDays()) {
         $buyRequest->setEndDate(date('Y-m-d H:i:s', strtotime('+ ' . $buyRequest->getSelectedDays() . ' DAY', strtotime($buyRequest->getStartDate()))));
     }
     if ($buyRequest->getFixedDateId()) {
         $fixedRentalDatesCollection = Mage::getModel('payperrentals/fixedrentaldates')->getCollection()->addFieldToFilter('id', $buyRequest->getFixedDateId())->getFirstItem();
         $fixedStartDate = $fixedRentalDatesCollection->getStartDate();
         $fixedEndDate = $fixedRentalDatesCollection->getEndDate();
         $difference = strtotime($fixedEndDate) - strtotime($fixedStartDate);
         $start_date = date('Y-m-d', strtotime($buyRequest->getStartDate())) . ' ' . date('H:i:s', strtotime($fixedStartDate));
         $end_date = date('Y-m-d H:i:s', strtotime($start_date) + $difference);
         $buyRequest->setStartDate($start_date);
         $buyRequest->setEndDate($end_date);
     }
     return $msg;
 }