Example #1
0
 /**
  * get discount for quote when a rule is applied and recalculate real point used
  * 
  * @param Mage_Sales_Model_Quote $quote
  * @param Varien_Object $rule
  * @param int $points
  * @return float
  */
 public function getQuoteRuleDiscount($quote, $rule, &$points)
 {
     $cacheKey = "quote_rule_discount:{$rule->getId()}:{$points}";
     if ($this->hasCache($cacheKey)) {
         return $this->getCache($cacheKey);
     }
     if ($rule->getId() == 'rate') {
         if ($rule->getBaseRate() && $rule->getPointsSpended()) {
             $baseTotal = $this->getQuoteBaseTotal($quote) - $this->getCheckedRuleDiscount();
             $maxPoints = ceil($baseTotal / $rule->getBaseRate()) * $rule->getPointsSpended();
             if ($maxPerOrder = $this->getMaxPointsPerOrder($quote->getStoreId())) {
                 $maxPerOrder -= $this->getPointItemSpent();
                 $maxPerOrder -= $this->getCheckedRulePoint();
                 if ($maxPerOrder > 0) {
                     $maxPoints = min($maxPoints, $maxPerOrder);
                 } else {
                     $maxPoints = 0;
                 }
             }
             $points = min($points, $maxPoints);
             $points = floor($points / $rule->getPointsSpended()) * $rule->getPointsSpended();
             //$this->saveCache($cacheKey, $points * $rule->getBaseRate() / $rule->getPointsSpended());
             //Hai.Tran 13/11/2013 Rate
             $discount = $points * $rule->getBaseRate() / $rule->getPointsSpended();
             if ($rule->getMaxPriceSpendedType() == 'by_price') {
                 $price = $rule->getMaxPriceSpendedValue();
                 if ($price < $discount) {
                     $discount = $price;
                 }
             } elseif ($rule->getMaxPriceSpendedType() == 'by_percent') {
                 $price = $baseTotal * $rule->getMaxPriceSpendedValue() / 100;
                 if ($price < $discount) {
                     $discount = $price;
                 }
             }
             $this->saveCache($cacheKey, $discount);
             //End Hai.Tran
         } else {
             $points = 0;
             $this->saveCache($cacheKey, 0);
         }
     } else {
         $container = new Varien_Object(array('quote_rule_discount' => 0, 'points' => $points));
         Mage::dispatchEvent('rewardpoints_calculation_spending_quote_rule_discount', array('rule' => $rule, 'quote' => $quote, 'container' => $container));
         $points = $container->getPoints();
         $this->saveCache($cacheKey, $container->getQuoteRuleDiscount());
     }
     return $this->getCache($cacheKey);
 }