public static function isDisabledDates($productId, $startTimestamp, $endTimestamp) { $blockedDates = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates($productId); $disabledDays = ITwebexperts_Payperrentals_Helper_Data::getDisabledDays($productId, true); $disabledDaysStart = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysStart(true); $disabledDaysEnd = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysEnd(true); $currentTimeStamp = Mage::getSingleton('core/date')->timestamp(time()); $paddingDays = ITwebexperts_Payperrentals_Helper_Data::getProductPaddingDays($productId, $currentTimeStamp, 1); $dayOfWeekStart = date('w', $startTimestamp); $dayOfWeekEnd = date('w', $endTimestamp); $formattedDateStart = date('Y-m-d H:i', $startTimestamp); $formattedDateEnd = date('Y-m-d H:i', $endTimestamp); return in_array($dayOfWeekStart, $disabledDaysStart) || in_array($dayOfWeekEnd, $disabledDaysEnd) || in_array($dayOfWeekStart, $disabledDays) || in_array($dayOfWeekEnd, $disabledDays) || in_array($formattedDateStart, $blockedDates) || in_array($formattedDateEnd, $blockedDates) || in_array($formattedDateStart, $paddingDays) || in_array($formattedDateEnd, $paddingDays); }
/** * Function which completes the booked array with the disabled and excluded days, so they * can't be selected in the calendar. The report will ignore them * * @param array $productIds * @param array $booked * @param int $currentTimestamp * @param datetime $stDate * @param datetime $enDate * * @return array */ public static function isExcludedDay($productIds, $stDate, $enDate) { if (!is_array($productIds)) { $productIds = array($productIds); } $currentTimestamp = (int) Mage::getSingleton('core/date')->timestamp(time()); foreach ($productIds as $id) { $disabledDays = ITwebexperts_Payperrentals_Helper_Data::getDisabledDays($id); $disabledDaysStart = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysStart(); $disabledDaysEnd = ITwebexperts_Payperrentals_Helper_Data::getDisabledDaysEnd(); //$paddingDays = ITwebexperts_Payperrentals_Helper_Data::getProductPaddingDays($id, $currentTimestamp); $paddingDays = ITwebexperts_Payperrentals_Helper_Data::getFirstAvailableDateRange($id, null, false, true); if (!$paddingDays) { return true; } $blockedDates = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates($id); foreach ($blockedDates as $dateFormatted) { if (date('Y-m-d', strtotime($dateFormatted)) == date('Y-m-d', strtotime($stDate)) || date('Y-m-d', strtotime($dateFormatted)) == date('Y-m-d', strtotime($enDate))) { return true; } } foreach ($paddingDays as $dateFormatted) { if (strtotime($dateFormatted) >= strtotime($stDate) && strtotime($dateFormatted) <= strtotime($enDate)) { return true; } } if (count($disabledDays) > 0) { $startTimePadding = strtotime(date('Y-m-d', strtotime($stDate))); $endTimePadding = strtotime(date('Y-m-d', strtotime($enDate))); $dayofWeek = date('D', $startTimePadding); if (in_array($dayofWeek, $disabledDays)) { return true; } $dayofWeek = date('D', $startTimePadding); if (in_array($dayofWeek, $disabledDaysStart)) { return true; } $dayofWeek = date('D', $endTimePadding); if (in_array($dayofWeek, $disabledDays)) { return true; } $dayofWeek = date('D', $endTimePadding); if (in_array($dayofWeek, $disabledDaysEnd)) { return true; } } } return false; }
/** * Function to remove disabled days and dates from the start-end interval * @param $rentalPeriodInSeconds * @param $startingDate * @param $endingDate * @param $start_date * @param $end_date * @param $product */ private static function reCalculatePeriodFromSettings(&$rentalPeriodInSeconds, $startingDate, $endingDate, $product) { // Add extra time from admin settings field if (Mage::getStoreConfig(ITwebexperts_Payperrentals_Helper_Config::XML_PATH_ADDTIME_NUMBER) != '' && Mage::getStoreConfig(ITwebexperts_Payperrentals_Helper_Config::XML_PATH_ADDTIME_NUMBER) > 0 && !(date('Y-m-d', strtotime($startingDate)) == date('Y-m-d', strtotime($endingDate)) && date('H:i:s', strtotime($endingDate)) == '23:59:59')) { $secondsToAdd = ITwebexperts_Payperrentals_Helper_Data::getPeriodInSeconds(Mage::getStoreConfig(ITwebexperts_Payperrentals_Helper_Config::XML_PATH_ADDTIME_NUMBER), Mage::getStoreConfig(ITwebexperts_Payperrentals_Helper_Config::XML_PATH_ADDTIME_TYPE)); $rentalPeriodInSeconds += $secondsToAdd; } $disabledDays = ITwebexperts_Payperrentals_Helper_Data::getDisabledDays($product->getId(), false, true); $blockedDates = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates($product->getId(), true); foreach ($blockedDates as $dateFormattedString) { if (strtotime($dateFormattedString) >= strtotime($startingDate) && strtotime($dateFormattedString) <= strtotime($endingDate)) { $secondsToRemove = 60 * 60 * 24; $rentalPeriodInSeconds -= $secondsToRemove; } } if (count($disabledDays) > 0) { $startTimePadding = strtotime(date('Y-m-d', strtotime($startingDate))); $endTimePadding = strtotime(date('Y-m-d', strtotime($endingDate))); while ($startTimePadding <= $endTimePadding) { $dayofWeek = date('D', $startTimePadding); if (in_array($dayofWeek, $disabledDays)) { $secondsToRemove = 60 * 60 * 24; $rentalPeriodInSeconds -= $secondsToRemove; } $startTimePadding += 60 * 60 * 24; } } }
/** * 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)); }
/** * Check closed times * * @param string $_testId * @param int $product_id * @test * @loadFixture * @loadExpectation * @dataProvider dataProvider */ public function checkClosedTimes($_testId, $product_id) { $product = Mage::getModel('catalog/product')->load($product_id); $_expected = $this->expected('testId' . $_testId); $closedTimes = ITwebexperts_Payperrentals_Helper_Data::getDisabledDates($product); if (empty($closedTimes)) { $this->assertEquals($_expected->getValues(), ''); } elseif (count($closedTimes) > 10) { foreach ($_expected->getValues() as $value) { $this->assertContains('"' . $value . '"', $closedTimes); } } else { $this->assertCount(count($_expected->getValues()), $closedTimes); foreach ($_expected->getValues() as $value) { $this->assertContains('"' . $value . '"', $closedTimes); } } }