Ejemplo n.º 1
0
 /**
  * Collect gift card discount amount
  *
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  Mage_SalesRule_Model_Quote_Discount
  */
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     /* Apply standard sales rules */
     parent::collect($address);
     $address->getQuote()->setUseGiftcards(false);
     $address->getQuote()->setGiftcardsDiscount(0);
     if ($this->getGiftcardDiscountEnabled()) {
         /* Append giftcard description */
         /*$descriptions = $address->getDiscountDescriptionArray();
           $descriptions[] = 'Gift Card';
           $address->setDiscountDescriptionArray($descriptions);
           $this->_calculator->prepareDescription($address);*/
         /* Save gift discount params */
         //$quote = $address->getQuote();
         //$address->getQuote()->setUseGiftcards(true);
         //$address->getQuote()->setGiftcardsDiscount($baseGiftcardsDiscount);
     }
     return $this;
 }
Ejemplo n.º 2
0
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     parent::collect($address);
     $quote = $address->getQuote();
     /*session data*/
     $customDiscount = $this->_getWebposSession()->getCustomDiscount();
     $type = $this->_getWebposSession()->getType();
     $discountValue = $this->_getWebposSession()->getDiscountValue();
     $discountName = $this->_getWebposSession()->getDiscountName();
     if ($customDiscount == 'true') {
         // not apply coupon
         if ($type == 'true') {
             $quote->setWebposDiscountAmount(0)->setWebposDiscountPercent($discountValue)->setWebposDiscountDesc($discountName);
         } else {
             $quote->setWebposDiscountAmount($discountValue)->setWebposDiscountPercent(0)->setWebposDiscountDesc($discountName);
         }
     }
     /**/
     if (!$quote->isVirtual() && $address->getAddressType() == 'billing') {
         return $this;
     }
     if ($quote->getWebposDiscountAmount() < 0.0001 && $quote->getWebposDiscountPercent() < 0.0001) {
         return $this;
     }
     $items = $address->getAllItems();
     if (!count($items)) {
         return $this;
     }
     if ($quote->getWebposDiscountAmount() < 0.0001) {
         // Percent discount
         foreach ($items as $item) {
             if ($item->getParentItemId()) {
                 continue;
             }
             if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                 foreach ($item->getChildren() as $child) {
                     $discount = $child->getQty() * $child->getPrice() * $quote->getWebposDiscountPercent() / 100;
                     $discount = min($discount, $child->getQty() * $child->getPrice() - $child->getDiscountAmount());
                     $discount = $quote->getStore()->roundPrice($discount);
                     $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
                     $child->setDiscountAmount($child->getDiscountAmount() + $discount)->setBaseDiscountAmount($child->getBaseDiscountAmount() + $baseDiscount);
                     $this->_addAmount(-$discount);
                     $this->_addBaseAmount(-$baseDiscount);
                 }
             } else {
                 $discount = $item->getQty() * $item->getPrice() * $quote->getWebposDiscountPercent() / 100;
                 $discount = min($discount, $item->getQty() * $item->getPrice() - $item->getDiscountAmount());
                 $discount = $quote->getStore()->roundPrice($discount);
                 $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
                 $item->setDiscountAmount($item->getDiscountAmount() + $discount)->setBaseDiscountAmount($item->getBaseDiscountAmount() + $baseDiscount);
                 $this->_addAmount(-$discount);
                 $this->_addBaseAmount(-$baseDiscount);
             }
         }
         if ($address->getShippingAmount()) {
             $discount = $address->getShippingAmount() * $quote->getWebposDiscountPercent() / 100;
             $discount = min($discount, $address->getShippingAmount() - $address->getShippingDiscountAmount());
             $discount = $quote->getStore()->roundPrice($discount);
             $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
             $address->setShippingDiscountAmount($address->getShippingDiscountAmount() + $discount)->setBaseShippingDiscountAmount($address->getBaseShippingDiscountAmount() + $baseDiscount);
             $this->_addAmount(-$discount);
             $this->_addBaseAmount(-$baseDiscount);
         }
         $this->_addCustomDiscountDescription($address);
         return $this;
     }
     // Calculate items total
     $itemsPrice = 0;
     foreach ($items as $item) {
         if ($item->getParentItemId()) {
             continue;
         }
         if ($item->getHasChildren() && $item->isChildrenCalculated()) {
             foreach ($item->getChildren() as $child) {
                 $itemsPrice += $item->getQty() * ($child->getQty() * $child->getPrice() - $child->getDiscountAmount());
             }
         } else {
             $itemsPrice += $item->getQty() * $item->getPrice() - $item->getDiscountAmount();
         }
     }
     $itemsPrice += $address->getShippingAmount() - $address->getShippingDiscountAmount();
     if ($itemsPrice < 0.0001) {
         return $this;
     }
     // Calculate custom discount for each item
     $rate = $quote->getWebposDiscountAmount() / $itemsPrice;
     if ($rate > 1) {
         $rate = 1;
     }
     foreach ($items as $item) {
         if ($item->getParentItemId()) {
             continue;
         }
         if ($item->getHasChildren() && $item->isChildrenCalculated()) {
             foreach ($item->getChildren() as $child) {
                 $discount = $rate * ($child->getQty() * $child->getPrice() - $child->getDiscountAmount());
                 $discount = $quote->getStore()->roundPrice($discount);
                 $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
                 $child->setDiscountAmount($child->getDiscountAmount() + $discount)->setBaseDiscountAmount($child->getBaseDiscountAmount() + $baseDiscount);
                 $this->_addAmount(-$discount);
                 $this->_addBaseAmount(-$baseDiscount);
             }
         } else {
             $discount = $rate * ($item->getQty() * $item->getPrice() - $item->getDiscountAmount());
             $discount = $quote->getStore()->roundPrice($discount);
             $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
             $item->setDiscountAmount($item->getDiscountAmount() + $discount)->setBaseDiscountAmount($item->getBaseDiscountAmount() + $baseDiscount);
             $this->_addAmount(-$discount);
             $this->_addBaseAmount(-$baseDiscount);
         }
     }
     if ($address->getShippingAmount()) {
         $discount = $rate * ($address->getShippingAmount() - $address->getShippingDiscountAmount());
         $discount = $quote->getStore()->roundPrice($discount);
         $baseDiscount = $discount / $quote->getStore()->convertPrice(1);
         $address->setShippingDiscountAmount($address->getShippingDiscountAmount() + $discount)->setBaseShippingDiscountAmount($address->getBaseShippingDiscountAmount() + $baseDiscount);
         $this->_addAmount(-$discount);
         $this->_addBaseAmount(-$baseDiscount);
     }
     $this->_addCustomDiscountDescription($address);
     return $this;
 }