Example #1
0
 /**
  * Function to get maximum quantity available for specific start and end dates.
  * This function first gets max available inventory for a product, then reduces it
  * via the number reserved in the inventory_serialized EAV field for that time period.
  * It then returns the inventory for when the least amount of inventory is available.
  *
  * @param Mage_Catalog_Product|int $product
  * @param                          $startDate
  * @param                          $endDate
  *
  * @return int
  */
 public static function getQuantity($product, $startDate = null, $endDate = null, $options = null, $forceReal = false)
 {
     $productId = $product;
     if (is_object($product)) {
         $productId = $product->getId();
     }
     if (!is_object($product)) {
         $product = ITwebexperts_Payperrentals_Helper_Data::initProduct($product);
     }
     $isReservation = ITwebexperts_Payperrentals_Helper_Data::isReservationType($productId);
     $maxQty = 0;
     if (!$isReservation) {
         if (!is_object($product)) {
             $product = ITwebexperts_Payperrentals_Helper_Data::initProduct($product);
         }
         $maxQty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
     }
     if ($isReservation && ITwebexperts_Payperrentals_Helper_Data::isReservationOnly($productId)) {
         $payperrentalsUseSerials = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($productId, 'payperrentals_use_serials');
         $payperrentalsQuantity = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($productId, 'payperrentals_quantity');
         if (!$payperrentalsQuantity) {
             $payperrentalsQuantity = 0;
         }
         if ($payperrentalsUseSerials == ITwebexperts_Payperrentals_Model_Product_Useserials::STATUS_ENABLED) {
             $serialCollection = Mage::getModel('payperrentals/serialnumbers')->getCollection()->addEntityIdFilter($productId)->addSelectFilter("status <> 'B' AND status <> 'M'");
             $maxQty = $serialCollection->getSize();
         } else {
             $maxQty = $payperrentalsQuantity;
         }
         if (!is_null($startDate) && !is_null($endDate)) {
             //should add turnover times to start end date
             $bookedArray = self::getBookedQtyForProducts($productId, $startDate, $endDate, false, isset($options['excluded_qty']) ? $options['excluded_qty'] : null);
             $maxQtyReserved = 1000000;
             if (isset($bookedArray[$productId])) {
                 foreach ($bookedArray[$productId] as $dateFormatted => $vObject) {
                     if (date('H:i', strtotime($dateFormatted)) == '00:00') {
                         $timeStart = strtotime($dateFormatted);
                         $timeEnd = strtotime($dateFormatted) + 86340;
                     } else {
                         $timeStart = strtotime($dateFormatted);
                         $timeEnd = strtotime($dateFormatted) + 3600;
                     }
                     if ($timeStart <= strtotime($endDate) && $timeEnd >= strtotime($startDate)) {
                         if ($maxQtyReserved > $maxQty - $vObject->getQty()) {
                             $maxQtyReserved = $maxQty - $vObject->getQty();
                         }
                     }
                 }
                 if ($maxQtyReserved == 1000000) {
                     $maxQtyReserved = $maxQty;
                 }
                 $maxQty = $maxQtyReserved;
             }
         }
     } elseif (!is_null($options)) {
         $maxQty = self::getQuantityForAnyProductTypeFromOptions($productId, $startDate, $endDate, $options, $forceReal);
     }
     $resultObject = new Varien_Object();
     $resultObject->setRetQty($maxQty);
     Mage::dispatchEvent('after_getquantity', array('product' => $productId, 'result' => $resultObject, 'start_date' => $startDate, 'end_date' => $endDate));
     return intval($resultObject->getRetQty());
 }