/** * Get first available date range for product * * @param Mage_Catalog_Model_Product $product * @param $startingDate * @param int $rangeSeconds * *@return array */ public static function getFirstAvailableDateRange($product, $startingDate = null, $rangeSeconds = false, $asArrayWithDates = false) { if (!is_object($product)) { $product = Mage::getModel('catalog/product')->load((int) $product); } else { $product->load($product->getId()); } if (!is_null($startingDate)) { $currentTimestamp = strtotime($startingDate); } else { $currentTimestamp = (int) Mage::getSingleton('core/date')->timestamp(); } if ($rangeSeconds == false) { $periodInSecond = Mage::helper('payperrentals/config')->getMinimumPeriod($product->getId(), null, true); } else { $periodInSecond = $rangeSeconds; } $disableDatesArray = array(); $useTimes = self::useTimes($product->getId()); $storeTime = Mage::helper('payperrentals/config')->getStoreTime(Mage::app()->getStore()->getId()); $configHelper = Mage::helper('payperrentals/config'); $inventoryHelper = Mage::helper('payperrentals/inventory'); /** * we get the first available date not time, can take a lot if using time */ /*if($useTimes){ $timeIncrement = $configHelper->getTimeIncrement() * 60; $currentTimestamp = strtotime(date('Y-m-d', $currentTimestamp).' '.$storeTime[0]); } else {*/ $timeIncrement = 3600 * 24; $currentTimestamp = strtotime('+0 day', strtotime(date('Y-m-d', $currentTimestamp) . ' 00:00:00')); //} $isAvailable = false; $initialStart = $currentTimestamp; while (!$isAvailable) { $start = $currentTimestamp; $end = $start + $periodInSecond; if (self::isDisabledDates($product->getId(), $start, $end)) { if ($asArrayWithDates) { $disableDatesArray[] = date('Y-m-d', $currentTimestamp) . ' 00:00'; } $currentTimestamp += $timeIncrement; continue; } /*if($useTimes) { if ($end > strtotime(date('Y-m-d', $end).' '.$storeTime[1])) { $start = strtotime('+1 day',strtotime(date('Y-m-d', $start).' '.$storeTime[0])); $end = $start + $periodInSecond; } }*/ $startDate = date('Y-m-d H:i:s', $start); $endDate = date('Y-m-d H:i:s', $end); $isAvailable = $inventoryHelper->isAvailable($product->getId(), $startDate, $endDate, 1); if (!$isAvailable && $asArrayWithDates) { $disableDatesArray[] = date('Y-m-d', $start) . ' 00:00'; } $currentTimestamp += $timeIncrement; if ($start - $initialStart >= 30 * 3 * 24 * 3600 || $product->getTypeId() != ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE) { //3 months in advance break; } } if ($isAvailable) { $turnoverDays = ITwebexperts_Payperrentals_Helper_Data::getProductPaddingDays($product->getId(), strtotime($startDate), 2); if (count($turnoverDays) > 0) { $difference = strtotime($endDate) - strtotime($startDate); $lastTurnoverDay = $turnoverDays[count($turnoverDays) - 1]; if ($asArrayWithDates) { $disableDatesArray = array_merge($disableDatesArray, $turnoverDays); } $startDate = date('Y-m-d H:i:s', strtotime('+1 day', strtotime($lastTurnoverDay))); $endDate = date('Y-m-d H:i:s', strtotime($startDate) + $difference); } if ($asArrayWithDates) { return $disableDatesArray; } return array('start_date' => $startDate, 'end_date' => $endDate); } else { if ($asArrayWithDates) { return array(); } return array('start_date' => 0, 'end_date' => 0); } }
/** * Check padding days * * @param string $_testId * @param int $product_id * @param int $timestamp * @test * @loadFixture * @loadExpectation * @dataProvider dataProvider */ public function checkPaddingDays($_testId, $product_id, $timestamp) { $product = Mage::getModel('catalog/product')->load($product_id); $_expected = $this->expected('testId' . $_testId); $paddingDays = ITwebexperts_Payperrentals_Helper_Data::getProductPaddingDays($product, $timestamp); if (empty($paddingDays)) { $this->assertEquals($_expected->getValues(), ''); } else { $this->assertCount(count($_expected->getValues()), $paddingDays); foreach ($_expected->getValues() as $value) { $this->assertContains('"' . $value . '"', $paddingDays); } } }