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_Order_Invoice_Total_Discount extends Mage_Sales_Model_Order_Invoice_Total_Abstract
00029 {
00030 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
00031 {
00032 $invoice->setDiscountAmount(0);
00033 $invoice->setBaseDiscountAmount(0);
00034
00035 $totalDiscountAmount = 0;
00036 $baseTotalDiscountAmount = 0;
00037 foreach ($invoice->getAllItems() as $item) {
00038 if ($item->getOrderItem()->isDummy()) {
00039 continue;
00040 }
00041 $orderItem = $item->getOrderItem();
00042 $orderItemDiscount = (float) $orderItem->getDiscountAmount();
00043 $baseOrderItemDiscount = (float) $orderItem->getBaseDiscountAmount();
00044 $orderItemQty = $orderItem->getQtyOrdered();
00045
00046 if ($orderItemDiscount && $orderItemQty) {
00047
00048
00049
00050 if ($item->isLast()) {
00051 $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
00052 $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
00053 }
00054 else {
00055 $discount = $orderItemDiscount*$item->getQty()/$orderItemQty;
00056 $baseDiscount = $baseOrderItemDiscount*$item->getQty()/$orderItemQty;
00057
00058 $discount = $invoice->getStore()->roundPrice($discount);
00059 $baseDiscount = $invoice->getStore()->roundPrice($baseDiscount);
00060 }
00061
00062 $item->setDiscountAmount($discount);
00063 $item->setBaseDiscountAmount($baseDiscount);
00064
00065 $totalDiscountAmount += $discount;
00066 $baseTotalDiscountAmount += $baseDiscount;
00067 }
00068 }
00069
00070 $invoice->setDiscountAmount($totalDiscountAmount);
00071 $invoice->setBaseDiscountAmount($baseTotalDiscountAmount);
00072
00073 $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
00074 $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
00075 return $this;
00076 }
00077 }