00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Sales_Model_Quote_Address_Total_Discount extends Mage_Sales_Model_Quote_Address_Total_Abstract
00029 {
00030 public function collect(Mage_Sales_Model_Quote_Address $address)
00031 {
00032 $quote = $address->getQuote();
00033 $eventArgs = array(
00034 'website_id'=>Mage::app()->getStore($quote->getStoreId())->getWebsiteId(),
00035 'customer_group_id'=>$quote->getCustomerGroupId(),
00036 'coupon_code'=>$quote->getCouponCode(),
00037 );
00038
00039 $address->setFreeShipping(0);
00040 $totalDiscountAmount = 0;
00041 $subtotalWithDiscount= 0;
00042 $baseTotalDiscountAmount = 0;
00043 $baseSubtotalWithDiscount= 0;
00044
00045 $items = $address->getAllItems();
00046 if (!count($items)) {
00047 $address->setDiscountAmount($totalDiscountAmount);
00048 $address->setSubtotalWithDiscount($subtotalWithDiscount);
00049 $address->setBaseDiscountAmount($baseTotalDiscountAmount);
00050 $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
00051 return $this;
00052 }
00053
00054 $hasDiscount = false;
00055 foreach ($items as $item) {
00056 if ($item->getNoDiscount()) {
00057 $item->setDiscountAmount(0);
00058 $item->setBaseDiscountAmount(0);
00059 $item->setRowTotalWithDiscount($item->getRowTotal());
00060 $item->setBaseRowTotalWithDiscount($item->getRowTotal());
00061
00062 $subtotalWithDiscount+=$item->getRowTotal();
00063 $baseSubtotalWithDiscount+=$item->getBaseRowTotal();
00064 }
00065 else {
00066
00067
00068
00069 if ($item->getParentItemId()) {
00070 continue;
00071 }
00072
00073
00074
00075
00076
00077 if ($item->getHasChildren() && $item->isChildrenCalculated()) {
00078 $eventArgs['item'] = $item;
00079 Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
00080 if ($item->getDiscountAmount() || $item->getFreeShipping()) {
00081 $hasDiscount = true;
00082 }
00083 foreach ($item->getChildren() as $child) {
00084 $eventArgs['item'] = $child;
00085 Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
00086
00087 if ($child->getDiscountAmount() || $child->getFreeShipping()) {
00088 $hasDiscount = true;
00089 }
00090
00091
00092
00093
00094 if ($item->getFreeShipping()) {
00095 $child->setFreeShipping($item->getFreeShipping());
00096 }
00097
00098
00099
00100
00101 if (!$child->getDiscountAmount() && $item->getDiscountPercent()) {
00102
00103 }
00104 $totalDiscountAmount += $child->getDiscountAmount();
00105 $baseTotalDiscountAmount += $child->getBaseDiscountAmount();
00106
00107 $child->setRowTotalWithDiscount($child->getRowTotal()-$child->getDiscountAmount());
00108 $child->setBaseRowTotalWithDiscount($child->getBaseRowTotal()-$child->getBaseDiscountAmount());
00109
00110 $subtotalWithDiscount+=$child->getRowTotalWithDiscount();
00111 $baseSubtotalWithDiscount+=$child->getBaseRowTotalWithDiscount();
00112 }
00113 }
00114 else {
00115 $eventArgs['item'] = $item;
00116 Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
00117
00118 if ($item->getDiscountAmount() || $item->getFreeShipping()) {
00119 $hasDiscount = true;
00120 }
00121 $totalDiscountAmount += $item->getDiscountAmount();
00122 $baseTotalDiscountAmount += $item->getBaseDiscountAmount();
00123
00124 $item->setRowTotalWithDiscount($item->getRowTotal()-$item->getDiscountAmount());
00125 $item->setBaseRowTotalWithDiscount($item->getBaseRowTotal()-$item->getBaseDiscountAmount());
00126
00127 $subtotalWithDiscount+=$item->getRowTotalWithDiscount();
00128 $baseSubtotalWithDiscount+=$item->getBaseRowTotalWithDiscount();
00129 }
00130 }
00131 }
00132 $address->setDiscountAmount($totalDiscountAmount);
00133 $address->setSubtotalWithDiscount($subtotalWithDiscount);
00134 $address->setBaseDiscountAmount($baseTotalDiscountAmount);
00135 $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
00136
00137 $address->setGrandTotal($address->getGrandTotal() - $address->getDiscountAmount());
00138 $address->setBaseGrandTotal($address->getBaseGrandTotal()-$address->getBaseDiscountAmount());
00139 return $this;
00140 }
00141
00142 public function fetch(Mage_Sales_Model_Quote_Address $address)
00143 {
00144 $amount = $address->getDiscountAmount();
00145 if ($amount!=0) {
00146 $title = Mage::helper('sales')->__('Discount');
00147 if ($code = $address->getCouponCode()) {
00148 $title = Mage::helper('sales')->__('Discount (%s)', $code);
00149 }
00150 $address->addTotal(array(
00151 'code'=>$this->getCode(),
00152 'title'=>$title,
00153 'value'=>-$amount
00154 ));
00155 }
00156 return $this;
00157 }
00158
00159 }