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_Item extends Mage_Core_Model_Abstract
00029 {
00030 protected $_eventPrefix = 'sales_invoice_item';
00031 protected $_eventObject = 'invoice_item';
00032
00033 protected $_invoice = null;
00034 protected $_orderItem = null;
00035
00036
00037
00038
00039 function _construct()
00040 {
00041 $this->_init('sales/order_invoice_item');
00042 }
00043
00044
00045
00046
00047
00048
00049
00050 public function setInvoice(Mage_Sales_Model_Order_Invoice $invoice)
00051 {
00052 $this->_invoice = $invoice;
00053 return $this;
00054 }
00055
00056
00057
00058
00059
00060
00061 public function getInvoice()
00062 {
00063 return $this->_invoice;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 public function setOrderItem(Mage_Sales_Model_Order_Item $item)
00073 {
00074 $this->_orderItem = $item;
00075 $this->setOrderItemId($item->getId());
00076 return $this;
00077 }
00078
00079
00080
00081
00082
00083
00084 public function getOrderItem()
00085 {
00086 if (is_null($this->_orderItem)) {
00087 if ($this->getInvoice()) {
00088 $this->_orderItem = $this->getInvoice()->getOrder()->getItemById($this->getOrderItemId());
00089 }
00090 else {
00091 $this->_orderItem = Mage::getModel('sales/order_item')
00092 ->load($this->getOrderItemId());
00093 }
00094 }
00095 return $this->_orderItem;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public function setQty($qty)
00105 {
00106 if ($this->getOrderItem()->getIsQtyDecimal()) {
00107 $qty = (float) $qty;
00108 }
00109 else {
00110 $qty = (int) $qty;
00111 }
00112 $qty = $qty > 0 ? $qty : 0;
00113
00114
00115
00116
00117 if ($qty <= $this->getOrderItem()->getQtyToInvoice() || $this->getOrderItem()->isDummy()) {
00118 $this->setData('qty', $qty);
00119 }
00120 else {
00121 Mage::throwException(
00122 Mage::helper('sales')->__('Invalid qty to invoice item "%s"', $this->getName())
00123 );
00124 }
00125 return $this;
00126 }
00127
00128
00129
00130
00131
00132
00133 public function register()
00134 {
00135 $orderItem = $this->getOrderItem();
00136 $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()+$this->getQty());
00137
00138 $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()+$this->getTaxAmount());
00139 $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()+$this->getBaseTaxAmount());
00140
00141 $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()+$this->getDiscountAmount());
00142 $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()+$this->getBaseDiscountAmount());
00143
00144 $orderItem->setRowInvoiced($orderItem->getRowInvoiced()+$this->getRowTotal());
00145 $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()+$this->getBaseRowTotal());
00146 return $this;
00147 }
00148
00149
00150
00151
00152
00153
00154 public function cancel()
00155 {
00156 $orderItem = $this->getOrderItem();
00157 $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()-$this->getQty());
00158
00159 $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()-$this->getTaxAmount());
00160 $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()-$this->getBaseTaxAmount());
00161
00162 $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()-$this->getDiscountAmount());
00163 $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()-$this->getBaseDiscountAmount());
00164
00165 $orderItem->setRowInvoiced($orderItem->getRowInvoiced()-$this->getRowTotal());
00166 $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()-$this->getBaseRowTotal());
00167 return $this;
00168 }
00169
00170
00171
00172
00173
00174
00175 public function calcRowTotal()
00176 {
00177 $rowTotal = $this->getOrderItem()->getRowTotal()/$this->getOrderItem()->getQtyOrdered()*$this->getQty();
00178 $baseRowTotal = $this->getOrderItem()->getBaseRowTotal()/$this->getOrderItem()->getQtyOrdered()*$this->getQty();
00179
00180 $this->setRowTotal($this->getInvoice()->getStore()->roundPrice($rowTotal));
00181 $this->setBaseRowTotal($this->getInvoice()->getStore()->roundPrice($baseRowTotal));
00182 return $this;
00183 }
00184
00185
00186
00187
00188
00189
00190 public function isLast()
00191 {
00192 if ($this->getQty() == $this->getOrderItem()->getQtyToInvoice()) {
00193 return true;
00194 }
00195 return false;
00196 }
00197 }