private static function _calculatePrice($product, $startingDate, $endingDate, $qty, $customerGroup, $useCurrency = false) { if (is_numeric($product)) { $product = ITwebexperts_Payperrentals_Helper_Data::initProduct($product); } if (!is_object($product)) { return 0; } list($startingDate, $endingDate) = ITwebexperts_Payperrentals_Helper_Date::convertDatepickerToDbFormat($startingDate, $endingDate); $rentalPeriodInSeconds = ITwebexperts_Payperrentals_Helper_Date::getDifferenceInSeconds($startingDate, $endingDate); if ($product->getPayperrentalsPricingtype() == ITwebexperts_Payperrentals_Model_Product_Pricingtype::PRICING_PRORATED) { $useProRated = true; } else { $useProRated = false; } $pricesCollection = self::getPricesCollection($product); // If no prices, return 0 if (count($pricesCollection) == 0) { return 0; } self::reCalculatePeriodFromSettings($rentalPeriodInSeconds, $startingDate, $endingDate, $product); $pricesArray = array(); $hasSpecials = self::getPricesArray($pricesCollection, $pricesArray, $startingDate, $endingDate, $qty, $customerGroup); if ($hasSpecials && $rentalPeriodInSeconds > 0) { $finalPriceAmount = self::getPriceForAllPricesArray($pricesArray, $useProRated, $rentalPeriodInSeconds); } else { $finalPriceAmount = self::getFinalPrice($pricesArray, $useProRated, $rentalPeriodInSeconds); } return $finalPriceAmount; }
/** * Usually used to return list of products within a bundle and the quantity available for each * for certain rental dates. This is useful to give the admin order creator more details * about each individual product in bundle inventory levels. * * @param $product * @param $startDate * @param $endDate * @param null $attributes * @param null $bundleOptions * @param null $bundleOptionsQty1 * @param null $bundleOptionsQty * @param bool $useRequired * * @return array */ public static function getQuantityForAnyProductTypePerProduct($product, $startDate, $endDate, $attributes = null, $bundleOptions = null, $bundleOptionsQty1 = null, $bundleOptionsQty = null, $useRequired = true, $forceReal = false) { if (is_object($product)) { $productObj = $product; $productId = $product->getId(); } else { $productId = $product; } $qtyPerProduct = array(); $typeId = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($productId, 'type_id'); $isBundle = false; if ($typeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_BUNDLE) { $isBundle = true; if (self::isAllowedOverbook($productId) && !$forceReal) { $qtyPerProduct[$productId] = 'is_overbook'; //always return true if overbooking } } if (is_null($attributes) && is_null($bundleOptions)) { $qtyArr = self::getReservationProductsArrayIds($productId, $attributes, $bundleOptions, $useRequired); foreach ($qtyArr as $iProduct) { $qtyPerProduct[$iProduct] = self::getQuantity($iProduct, $startDate, $endDate); } } else { if (!isset($productObj)) { $productObj = ITwebexperts_Payperrentals_Helper_Data::initProduct($productId); } $qtyArr = self::getQuantityArrayForProduct($productObj, 1, $attributes, $bundleOptions, $bundleOptionsQty1, $bundleOptionsQty); $minQty = 1000000; $isAllowOverbooking = true; foreach ($qtyArr as $iProduct => $iQty) { if (self::isAllowedOverbook($iProduct) && !$forceReal) { $qtyPerProduct[$iProduct] = 'is_overbook'; continue; } $isAllowOverbooking = false; $qtyPerProduct[$iProduct] = intval(self::getQuantity($iProduct, $startDate, $endDate)); if ($isBundle) { if ($minQty > intval(floor($qtyPerProduct[$iProduct] / $iQty))) { $minQty = intval(floor($qtyPerProduct[$iProduct] / $iQty)); } } } if ($isBundle) { $qtyPerProduct[$productId] = $minQty; } if ($isBundle && $isAllowOverbooking) { $qtyPerProduct[$productId] = 'is_overbook'; } } return array($qtyPerProduct, $qtyArr); }
/** * 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)); }