/** * 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)); }
public function updateBookedHtmlForShipping(Varien_Event_Observer &$observer) { $requestParams = $observer->getEvent()->getRequestParams(); $bookedHtml = $observer->getEvent()->getBookedHtml(); $product = $observer->getEvent()->getProduct(); if (array_key_exists('shipping_method', $requestParams)) { if ($requestParams['shipping_method'] != '') { $shippingMethodForLoad = $requestParams['shipping_method']; } elseif (array_key_exists('shipping_method_select_box', $requestParams) && $requestParams['shipping_method_select_box'] != 'null') { $shippingMethodForLoad = $requestParams['shipping_method_select_box']; } elseif (array_key_exists('shipping_method_select_box_additional', $requestParams)) { $shippingMethodForLoad = $requestParams['shipping_method_select_box_additional']; } else { $shippingMethodForLoad = ''; } $shippingMethod = Mage::getModel('rshipping/rshipping')->load($shippingMethodForLoad, 'shipping_method'); $bookedHtml['shippingMinRentalPeriod'] = ITwebexperts_Payperrentals_Helper_Data::getPeriodInSeconds($shippingMethod->getMinRentalPeriod(), $shippingMethod->getMinRentalType()); $bookedHtml['shippingMinRentalMessage'] = Mage::helper('payperrentals')->__('Minimum rental period is "%s". Please make sure you select at least that number', ITwebexperts_Payperrentals_Helper_Html::getTextForType($shippingMethod->getMinRentalPeriod(), $shippingMethod->getMinRentalType())); $upsTransitTime = 0; if (stripos($shippingMethod->getShippingMethod(), 'ups') !== false && Mage::getStoreConfig('payperrentals/rshipping_ups/enabled') && $shippingMethod->getUseLiveUpsApi()) { $zip = $requestParams['zip_code']; $upsTransitTime = Mage::helper('rshipping')->getTimeInTransit($zip, $product, $shippingMethod->getShippingMethod()); } $paddingDays = array(); /** * Day cutoff time. If current time > cutoff time => disable today and start from tomorrow * */ $currentTime = (int) Mage::getModel('core/date')->timestamp(time()); if ($shippingMethod->getShippingCutoffTime() != '') { $shipCutoffTime = strtotime(date('Y-m-d', $currentTime) . ' ' . $shippingMethod->getShippingCutoffTime()); if ($currentTime > $shipCutoffTime) { //$bookedHtml['disableToday'] = true; $dateFormatted = date('Y-m-d', $currentTime); $paddingDays[] = $dateFormatted; } } if ($shippingMethod->getStartDisabledDays()) { $bookedHtml['disabledForStartRange'] = ITwebexperts_Payperrentals_Helper_Data::getWeekdayForJs(explode(',', $shippingMethod->getStartDisabledDays())); } else { $bookedHtml['disabledForStartRange'] = ''; } if ($shippingMethod->getEndDisabledDays()) { $bookedHtml['disabledForEndRange'] = ITwebexperts_Payperrentals_Helper_Data::getWeekdayForJs(explode(',', $shippingMethod->getEndDisabledDays())); } else { $bookedHtml['disabledForEndRange'] = ''; } if (stripos($shippingMethod->getShippingMethod(), 'ups') !== false && $shippingMethod->getIgnoreTurnoverDay()) { $bookedHtml['ignoreTurnoverDay'] = ITwebexperts_Payperrentals_Helper_Data::getWeekdayForJs(explode(',', $shippingMethod->getIgnoreTurnoverDay())); } $turnoverBeforePeriod = (int) $shippingMethod->getTurnoverBeforePeriod() + $upsTransitTime; $turnoverAfterPeriod = (int) $shippingMethod->getTurnoverAfterPeriod(); $bookedHtml['turnoverTimeBefore'] = $turnoverBeforePeriod; $bookedHtml['turnoverTimeAfter'] = $turnoverAfterPeriod; if ($turnoverBeforePeriod > 0) { $startTimePadding = Mage::getSingleton('core/date')->timestamp(time()); while ($turnoverBeforePeriod > 0) { $dateFormatted = date('Y-m-d', $startTimePadding) . ' 00:00'; if (!in_array($dateFormatted, $paddingDays)) { $paddingDays[] = $dateFormatted; } $startTimePadding = strtotime('+1 day', $startTimePadding); $turnoverBeforePeriod--; } } if (count($paddingDays) > 0) { $bookedHtml['disabledDatesPadding'] = ITwebexperts_Payperrentals_Helper_Data::toFormattedDateArray($paddingDays, false); } $observer->getEvent()->setBookedHtml($bookedHtml); } }