public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     /* hainh add discount calculate with incl or excl tax variable 22-04-2014*/
     $discount_include_tax = false;
     if ((int) Mage::getStoreConfig('tax/calculation/discount_tax', $address->getQuote()->getStore()) == 1) {
         $discount_include_tax = true;
     }
     Mage::helper('affiliateplus/cookie')->getNumberOrdered();
     if ($this->_getConfigHelper()->getDiscountConfig('type_discount') == 'product') {
         return $this;
     }
     if ($this->_getConfigHelper()->getDiscountConfig('allow_discount') == 'system') {
         $appliedRuleIds = array();
         if (is_string($address->getAppliedRuleIds())) {
             $appliedRuleIds = explode(',', $address->getAppliedRuleIds());
             $appliedRuleIds = array_filter($appliedRuleIds);
         }
         if (count($appliedRuleIds)) {
             return $this;
         }
     }
     $items = $address->getAllItems();
     if (!count($items)) {
         return $this;
     }
     $affiliateInfo = Mage::helper('affiliateplus/cookie')->getAffiliateInfo();
     $baseDiscount = 0;
     $discountObj = new Varien_Object(array('affiliate_info' => $affiliateInfo, 'base_discount' => $baseDiscount, 'default_discount' => true, 'discounted_items' => array()));
     Mage::dispatchEvent('affiliateplus_address_collect_total', array('address' => $address, 'discount_obj' => $discountObj));
     $baseDiscount = $discountObj->getBaseDiscount();
     if ($discountObj->getDefaultDiscount()) {
         $account = '';
         foreach ($affiliateInfo as $info) {
             if (isset($info['account']) && $info['account']) {
                 $account = $info['account'];
             }
         }
         if ($account && $account->getId()) {
             $discountType = $this->_getConfigHelper()->getDiscountConfig('discount_type');
             $discountValue = floatval($this->_getConfigHelper()->getDiscountConfig('discount'));
             if (Mage::helper('affiliateplus/cookie')->getNumberOrdered()) {
                 if ($this->_getConfigHelper()->getDiscountConfig('use_secondary')) {
                     $discountType = $this->_getConfigHelper()->getDiscountConfig('secondary_type');
                     $discountValue = floatval($this->_getConfigHelper()->getDiscountConfig('secondary_discount'));
                 }
             }
             $discountedItems = $discountObj->getDiscountedItems();
             if ($discountValue <= 0) {
                 // do nothing when no discount
             } elseif ($discountType == 'cart_fixed') {
                 $baseItemsPrice = 0;
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems)) {
                         continue;
                     }
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             $baseItemsPrice += $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                         }
                     } elseif ($item->getProduct()) {
                         $baseItemsPrice += $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                     }
                 }
                 if ($baseItemsPrice) {
                     $totalBaseDiscount = min($discountValue, $baseItemsPrice);
                     foreach ($items as $item) {
                         if ($item->getParentItemId()) {
                             continue;
                         }
                         if (in_array($item->getId(), $discountedItems)) {
                             continue;
                         }
                         if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                             foreach ($item->getChildren() as $child) {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                                 $childBaseDiscount = $totalBaseDiscount * $price / $baseItemsPrice;
                                 $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount));
                             }
                         } elseif ($item->getProduct()) {
                             $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                             $itemBaseDiscount = $totalBaseDiscount * $price / $baseItemsPrice;
                             $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                         }
                     }
                     $baseDiscount += $totalBaseDiscount;
                 }
             } elseif ($discountType == 'fixed') {
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems)) {
                         continue;
                     }
                     $itemBaseDiscount = 0;
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             $childBaseDiscount = $item->getQty() * $child->getQty() * $discountValue;
                             $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             $childBaseDiscount = $childBaseDiscount < $price ? $childBaseDiscount : $price;
                             $itemBaseDiscount += $childBaseDiscount;
                             $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount));
                         }
                     } elseif ($item->getProduct()) {
                         $itemBaseDiscount = $item->getQty() * $discountValue;
                         $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         $itemBaseDiscount = $itemBaseDiscount < $price ? $itemBaseDiscount : $price;
                         $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                     }
                     $baseDiscount += $itemBaseDiscount;
                 }
             } elseif ($discountType == 'percentage') {
                 if ($discountValue > 100) {
                     $discountValue = 100;
                 }
                 if ($discountValue < 0) {
                     $discountValue = 0;
                 }
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems)) {
                         continue;
                     }
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             /* hainh add this for calculating discount base on incl or excl tax price 22-04-2014 */
                             if (!$discount_include_tax) {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             } else {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount());
                             }
                             $childBaseDiscount = $price * $discountValue / 100;
                             $itemBaseDiscount += $childBaseDiscount;
                             $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount));
                         }
                     } elseif ($item->getProduct()) {
                         /* hainh add this for calculating discount base on incl or excl tax price 22-04-2014 */
                         if (!$discount_include_tax) {
                             $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         } else {
                             $price = $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount();
                         }
                         $itemBaseDiscount = $price * $discountValue / 100;
                         $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                     }
                     $baseDiscount += $itemBaseDiscount;
                 }
             }
         }
     }
     // if ($baseDiscount > $address->getBaseGrandTotal())
     //     $baseDiscount = $address->getBaseGrandTotal();
     if ($baseDiscount) {
         $discount = Mage::app()->getStore()->convertPrice($baseDiscount);
         $address->setBaseAffiliateplusDiscount(-$baseDiscount);
         $address->setAffiliateplusDiscount(-$discount);
         $session = Mage::getSingleton('checkout/session');
         if ($session->getData('affiliate_coupon_code')) {
             $address->setAffiliateplusCoupon($session->getData('affiliate_coupon_code'));
         }
         $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
         $address->setGrandTotal($address->getGrandTotal() - $discount);
     }
     return $this;
 }
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     // Changed By Adam 28/07/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return $this;
     }
     // Changed By Adam 06/11/2014: Fix bug hidden tax
     $quote = $address->getQuote();
     $applyTaxAfterDiscount = (bool) Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT, $quote->getStoreId());
     if ($applyTaxAfterDiscount) {
         $this->_processHiddenTaxes($address);
         return $this;
     }
     /* hainh add discount calculate with incl or excl tax variable 22-04-2014 */
     $discount_include_tax = false;
     if ((int) Mage::getStoreConfig('tax/calculation/discount_tax', $address->getQuote()->getStore()) == 1) {
         $discount_include_tax = true;
     }
     if ($this->_getConfigHelper()->getDiscountConfig('type_discount') == 'product') {
         return $this;
     }
     if ($this->_getConfigHelper()->getDiscountConfig('allow_discount') == 'system') {
         $appliedRuleIds = array();
         if (is_string($address->getAppliedRuleIds())) {
             $appliedRuleIds = explode(',', $address->getAppliedRuleIds());
             $appliedRuleIds = array_filter($appliedRuleIds);
         }
         if (count($appliedRuleIds)) {
             return $this;
         }
     }
     $items = $address->getAllItems();
     if (!count($items)) {
         return $this;
     }
     /* edit by Jack - get all information from order editing */
     $session = Mage::getSingleton('checkout/session');
     $orderId = Mage::getSingleton('adminhtml/session_quote')->getOrder()->getId();
     /* get data processed */
     $dataProcessing = Mage::helper('affiliateplus')->processDataWhenEditOrder();
     if (isset($dataProcessing['current_couponcode'])) {
         $currentCouponCode = $dataProcessing['current_couponcode'];
     }
     if (isset($dataProcessing['base_affiliate_discount'])) {
         $baseAffiliateDiscount = $dataProcessing['base_affiliate_discount'];
     }
     if (isset($dataProcessing['customer_id'])) {
         $customerId = $dataProcessing['customer_id'];
     }
     if (isset($dataProcessing['default_discount'])) {
         $defaultDiscount = $dataProcessing['default_discount'];
     }
     /* */
     $couponCodeBySession = $session->getAffiliateCouponCode();
     $isAllowUseCoupon = Mage::helper('affiliateplus')->isAllowUseCoupon($couponCodeBySession);
     if (!$isAllowUseCoupon || !Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         $session->unsAffiliateCouponCode();
     }
     $isEnableLiftime = Mage::getStoreConfig('affiliateplus/commission/life_time_sales');
     if (Mage::helper('affiliateplus/cookie')->getNumberOrdered() == 1 && !$session->getData('affiliate_coupon_code') && isset($currentCouponCode) && $currentCouponCode != '') {
         return $this;
     } else {
         if ($isEnableLiftime == 0 && Mage::helper('affiliateplus/cookie')->getNumberOrdered() > 1 && !$session->getData('affiliate_coupon_code') && isset($currentCouponCode) && $currentCouponCode != '') {
             return $this;
         }
     }
     $baseDiscount = 0;
     $affiliateInfo = Mage::helper('affiliateplus/cookie')->getAffiliateInfo();
     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
     $discountObj = new Varien_Object(array('affiliate_info' => $affiliateInfo, 'base_discount' => $baseDiscount, 'default_discount' => true, 'discounted_products' => array(), 'discounted_items' => array(), 'program' => '', 'base_discount_excl_tax' => ''));
     if (!isset($customerId)) {
         $customerId = '';
     }
     /* add new event to calculate commission when edit order - Edit By Jack */
     if (Mage::helper('affiliateplus')->isAdmin()) {
         Mage::dispatchEvent('affiliateplus_address_collect_total_edit', array('address' => $address, 'discount_obj' => $discountObj));
     } else {
         Mage::dispatchEvent('affiliateplus_address_collect_total', array('address' => $address, 'discount_obj' => $discountObj));
     }
     $baseDiscount = $discountObj->getBaseDiscount();
     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
     $baseDiscountExclTax = $discountObj->getBaseDiscountExclTax();
     if ($discountObj->getDefaultDiscount()) {
         $account = '';
         foreach ($affiliateInfo as $info) {
             if (isset($info['account']) && $info['account']) {
                 $account = $info['account'];
             }
         }
         if (isset($defaultDiscount) && $defaultDiscount && !$couponCodeBySession && Mage::helper('affiliateplus')->isAdmin() || $discountObj->getProgram() == 'Affiliate Program' || $account && $account->getId() || isset($baseAffiliateDiscount) && $baseAffiliateDiscount) {
             $currentStoreId = Mage::getSingleton('adminhtml/session_quote')->getStoreId();
             if (!$currentStoreId) {
                 $currentStoreId = null;
             }
             $discountType = $this->_getConfigHelper()->getDiscountConfig('discount_type', $currentStoreId);
             $discountValue = floatval($this->_getConfigHelper()->getDiscountConfig('discount', $currentStoreId));
             if ($orderId && Mage::helper('affiliateplus/cookie')->getNumberOrdered() > 1 || !$orderId && Mage::helper('affiliateplus/cookie')->getNumberOrdered()) {
                 if ($this->_getConfigHelper()->getDiscountConfig('use_secondary', $currentStoreId)) {
                     $discountType = $this->_getConfigHelper()->getDiscountConfig('secondary_type', $currentStoreId);
                     $discountValue = floatval($this->_getConfigHelper()->getDiscountConfig('secondary_discount', $currentStoreId));
                 }
             }
             $discountedItems = $discountObj->getDiscountedItems();
             $discountedProducts = $discountObj->getDiscountedProducts();
             if ($discountValue <= 0) {
                 // do nothing when no discount
             } elseif ($discountType == 'cart_fixed') {
                 $baseItemsPrice = 0;
                 /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                 $baseItemsPriceExclTax = 0;
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getProductId(), $discountedProducts) && Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems) && !Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             /* Changed By Adam 08/10/2014 */
                             if (!$discount_include_tax) {
                                 $baseItemsPrice += $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             } else {
                                 $baseItemsPrice += $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount());
                             }
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $baseItemsPriceExclTax += $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                         }
                     } elseif ($item->getProduct()) {
                         /* Changed By Adam 08/10/2014 */
                         if (!$discount_include_tax) {
                             $baseItemsPrice += $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         } else {
                             $baseItemsPrice += $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount();
                         }
                         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                         $baseItemsPriceExclTax += $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                     }
                 }
                 if ($baseItemsPrice) {
                     $totalBaseDiscount = min($discountValue, $baseItemsPrice);
                     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                     $totalBaseDiscountExclTax = min($discountValue, $baseItemsPriceExclTax);
                     foreach ($items as $item) {
                         if ($item->getParentItemId()) {
                             continue;
                         }
                         if (in_array($item->getProductId(), $discountedProducts) && Mage::helper('affiliateplus')->isAdmin()) {
                             continue;
                         }
                         if (in_array($item->getId(), $discountedItems) && !Mage::helper('affiliateplus')->isAdmin()) {
                             continue;
                         }
                         if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                             foreach ($item->getChildren() as $child) {
                                 /* Changed By Adam 08/10/2014 */
                                 if (!$discount_include_tax) {
                                     $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                                 } else {
                                     $price = $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount());
                                 }
                                 $childBaseDiscount = $totalBaseDiscount * $price / $baseItemsPrice;
                                 $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount))->setRowTotal($child->getRowTotal() - $childBaseDiscount)->setBaseRowTotal($child->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($childBaseDiscount));
                             }
                         } elseif ($item->getProduct()) {
                             /* Changed By Adam 08/10/2014 */
                             if (!$discount_include_tax) {
                                 $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                             } else {
                                 $price = $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount();
                             }
                             $itemBaseDiscount = $totalBaseDiscount * $price / $baseItemsPrice;
                             $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount))->setRowTotal($item->getRowTotal() - $itemBaseDiscount)->setBaseRowTotal($item->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                         }
                     }
                     $baseDiscount += $totalBaseDiscount;
                     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                     $baseDiscountExclTax += $totalBaseDiscountExclTax;
                 }
             } elseif ($discountType == 'fixed') {
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getProductId(), $discountedProducts) && Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems) && !Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     $itemBaseDiscount = 0;
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             $childBaseDiscount = $item->getQty() * $child->getQty() * $discountValue;
                             /* Changed By Adam 08/10/2014 */
                             if (!$discount_include_tax) {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             } else {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount());
                             }
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $priceExclTax = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             $childBaseDiscount = $childBaseDiscount < $price ? $childBaseDiscount : $price;
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $childBaseDiscountExclTax = $childBaseDiscount < $priceExclTax ? $childBaseDiscount : $priceExclTax;
                             $itemBaseDiscount += $childBaseDiscount;
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $itemBaseDiscountExclTax += $childBaseDiscountExclTax;
                             $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount))->setRowTotal($child->getRowTotal() - $childBaseDiscount)->setBaseRowTotal($child->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($childBaseDiscount));
                         }
                     } elseif ($item->getProduct()) {
                         $itemBaseDiscount = $item->getQty() * $discountValue;
                         /* Changed By Adam 08/10/2014 */
                         if (!$discount_include_tax) {
                             $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         } else {
                             $price = $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount();
                         }
                         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                         $priceExclTax = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         $itemBaseDiscount = $itemBaseDiscount < $price ? $itemBaseDiscount : $price;
                         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                         $itemBaseDiscountExclTax = $itemBaseDiscount < $priceExclTax ? $itemBaseDiscount : $priceExclTax;
                         $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount))->setRowTotal($item->getRowTotal() - $itemBaseDiscount)->setBaseRowTotal($item->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                     }
                     $baseDiscount += $itemBaseDiscount;
                     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                     $baseDiscountExclTax += $itemBaseDiscountExclTax;
                 }
             } elseif ($discountType == 'percentage') {
                 if ($discountValue > 100) {
                     $discountValue = 100;
                 }
                 if ($discountValue < 0) {
                     $discountValue = 0;
                 }
                 foreach ($items as $item) {
                     if ($item->getParentItemId()) {
                         continue;
                     }
                     if (in_array($item->getProductId(), $discountedProducts) && Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     if (in_array($item->getId(), $discountedItems) && !Mage::helper('affiliateplus')->isAdmin()) {
                         continue;
                     }
                     $itemBaseDiscount = 0;
                     // Changed By Adam to fix the problem of calculate discount
                     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                     $itemBaseDiscountExclTax = 0;
                     if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                         foreach ($item->getChildren() as $child) {
                             /* hainh add this for calculating discount base on incl or excl tax price 22-04-2014 */
                             if (!$discount_include_tax) {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             } else {
                                 $price = $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount());
                             }
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $priceExclTax = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount());
                             $childBaseDiscount = $price * $discountValue / 100;
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $childBaseDiscountExclTax = $priceExclTax * $discountValue / 100;
                             $itemBaseDiscount += $childBaseDiscount;
                             /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                             $itemBaseDiscountExclTax += $childBaseDiscountExclTax;
                             $child->setBaseAffiliateplusAmount($childBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($childBaseDiscount))->setRowTotal($child->getRowTotal() - $childBaseDiscount)->setBaseRowTotal($child->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($childBaseDiscount));
                         }
                     } elseif ($item->getProduct()) {
                         /* hainh add this for calculating discount base on incl or excl tax price 22-04-2014 */
                         if (!$discount_include_tax) {
                             $price = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         } else {
                             $price = $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount();
                         }
                         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                         $priceExclTax = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount();
                         $itemBaseDiscount = $price * $discountValue / 100;
                         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                         $itemBaseDiscountExclTax = $priceExclTax * $discountValue / 100;
                         $item->setBaseAffiliateplusAmount($itemBaseDiscount)->setAffiliateplusAmount(Mage::app()->getStore()->convertPrice($itemBaseDiscount))->setRowTotal($item->getRowTotal() - $itemBaseDiscount)->setBaseRowTotal($item->getBaseRowTotal() - Mage::app()->getStore()->convertPrice($itemBaseDiscount));
                     }
                     $baseDiscount += $itemBaseDiscount;
                     /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
                     $baseDiscountExclTax += $itemBaseDiscountExclTax;
                 }
             }
         }
     }
     if ($baseDiscount) {
         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
         $baseDiscountExclTax = round($baseDiscountExclTax, '2');
         $discountExclTax = Mage::app()->getStore()->convertPrice($baseDiscountExclTax);
         $discount = Mage::app()->getStore()->convertPrice($baseDiscount);
         $address->setBaseAffiliateplusDiscount(-$baseDiscount);
         $address->setAffiliateplusDiscount(-$discount);
         $session = Mage::getSingleton('checkout/session');
         if ($discountObj->getProgram()) {
             $session->setProgramData($discountObj->getProgram());
         }
         if ($session->getData('affiliate_coupon_code')) {
             $address->setAffiliateplusCoupon($session->getData('affiliate_coupon_code'));
         }
         /* Changed By Adam: 26/08/2015: Fix issue calculate grandtotal after using affiliate discount when calculate discount include tax and calculate tax after discount  */
         if ($applyTaxAfterDiscount && $discount_include_tax) {
             $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscountExclTax);
             $address->setGrandTotal($address->getGrandTotal() - $discountExclTax);
         } else {
             $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
             $address->setGrandTotal($address->getGrandTotal() - $discount);
         }
     }
     return $this;
 }
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     if ($this->_getConfigHelper()->getGeneralConfig('type_discount') == 'product') {
         return $this;
     }
     $items = $address->getAllItems();
     if (!count($items)) {
         return $this;
     }
     $affiliateInfo = Mage::helper('affiliateplus/cookie')->getAffiliateInfo();
     $baseDiscount = 0;
     $discountObj = new Varien_Object(array('affiliate_info' => $affiliateInfo, 'base_discount' => $baseDiscount, 'default_discount' => true, 'discounted_items' => array()));
     Mage::dispatchEvent('affiliateplus_address_collect_total', array('address' => $address, 'discount_obj' => $discountObj));
     $baseDiscount = $discountObj->getBaseDiscount();
     if ($discountObj->getDefaultDiscount()) {
         $account = '';
         foreach ($affiliateInfo as $info) {
             if ($info['account']) {
                 $account = $info['account'];
             }
         }
         if ($account && $account->getId()) {
             $discountValue = floatval($this->_getConfigHelper()->getGeneralConfig('discount'));
             $discountedItems = $discountObj->getDiscountedItems();
             if ($this->_getConfigHelper()->getGeneralConfig('discount_type') == 'fixed') {
                 foreach ($items as $item) {
                     if (in_array($item->getId(), $discountedItems) || 0 == $item->getBasePrice()) {
                         continue;
                     }
                     $itemBaseDiscount = 0;
                     if ($item->getProduct()) {
                         $price = $item->getBasePrice();
                         $discountPrice = $discountValue < $price ? $discountValue : $price;
                         $itemBaseDiscount = $item->getQty() * $discountPrice;
                     }
                     $baseDiscount += $itemBaseDiscount;
                 }
             } elseif ($this->_getConfigHelper()->getGeneralConfig('discount_type') == 'percentage') {
                 if ($discountValue > 100) {
                     $discountValue = 100;
                 }
                 if ($discountValue < 0) {
                     $discountValue = 0;
                 }
                 foreach ($items as $item) {
                     if (in_array($item->getId(), $discountedItems) || 0 == $item->getBasePrice()) {
                         continue;
                     }
                     if ($item->getProduct()) {
                         $price = $item->getBasePrice();
                         $baseDiscount += $item->getQty() * $price * $discountValue / 100;
                     }
                 }
             }
         }
     }
     if ($baseDiscount > $address->getBaseGrandTotal()) {
         $baseDiscount = $address->getBaseGrandTotal();
     }
     if ($baseDiscount) {
         $discount = Mage::app()->getStore()->convertPrice($baseDiscount);
         $address->setBaseAffiliateplusDiscount(-$baseDiscount);
         $address->setAffiliateplusDiscount(-$discount);
         $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
         $address->setGrandTotal($address->getGrandTotal() - $discount);
     }
     return $this;
 }