1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Sales
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Enter description here ...
30: *
31: * @method Mage_Sales_Model_Resource_Order_Invoice_Item _getResource()
32: * @method Mage_Sales_Model_Resource_Order_Invoice_Item getResource()
33: * @method int getParentId()
34: * @method Mage_Sales_Model_Order_Invoice_Item setParentId(int $value)
35: * @method float getBasePrice()
36: * @method Mage_Sales_Model_Order_Invoice_Item setBasePrice(float $value)
37: * @method float getBaseWeeeTaxRowDisposition()
38: * @method Mage_Sales_Model_Order_Invoice_Item setBaseWeeeTaxRowDisposition(float $value)
39: * @method float getWeeeTaxAppliedRowAmount()
40: * @method Mage_Sales_Model_Order_Invoice_Item setWeeeTaxAppliedRowAmount(float $value)
41: * @method float getBaseWeeeTaxAppliedAmount()
42: * @method Mage_Sales_Model_Order_Invoice_Item setBaseWeeeTaxAppliedAmount(float $value)
43: * @method float getTaxAmount()
44: * @method Mage_Sales_Model_Order_Invoice_Item setTaxAmount(float $value)
45: * @method float getBaseRowTotal()
46: * @method Mage_Sales_Model_Order_Invoice_Item setBaseRowTotal(float $value)
47: * @method float getDiscountAmount()
48: * @method Mage_Sales_Model_Order_Invoice_Item setDiscountAmount(float $value)
49: * @method float getRowTotal()
50: * @method Mage_Sales_Model_Order_Invoice_Item setRowTotal(float $value)
51: * @method float getWeeeTaxRowDisposition()
52: * @method Mage_Sales_Model_Order_Invoice_Item setWeeeTaxRowDisposition(float $value)
53: * @method float getBaseDiscountAmount()
54: * @method Mage_Sales_Model_Order_Invoice_Item setBaseDiscountAmount(float $value)
55: * @method float getBaseWeeeTaxDisposition()
56: * @method Mage_Sales_Model_Order_Invoice_Item setBaseWeeeTaxDisposition(float $value)
57: * @method float getPriceInclTax()
58: * @method Mage_Sales_Model_Order_Invoice_Item setPriceInclTax(float $value)
59: * @method float getWeeeTaxAppliedAmount()
60: * @method Mage_Sales_Model_Order_Invoice_Item setWeeeTaxAppliedAmount(float $value)
61: * @method float getBaseTaxAmount()
62: * @method Mage_Sales_Model_Order_Invoice_Item setBaseTaxAmount(float $value)
63: * @method float getBasePriceInclTax()
64: * @method Mage_Sales_Model_Order_Invoice_Item setBasePriceInclTax(float $value)
65: * @method float getQty()
66: * @method float getWeeeTaxDisposition()
67: * @method Mage_Sales_Model_Order_Invoice_Item setWeeeTaxDisposition(float $value)
68: * @method float getBaseCost()
69: * @method Mage_Sales_Model_Order_Invoice_Item setBaseCost(float $value)
70: * @method float getBaseWeeeTaxAppliedRowAmount()
71: * @method Mage_Sales_Model_Order_Invoice_Item setBaseWeeeTaxAppliedRowAmount(float $value)
72: * @method float getPrice()
73: * @method Mage_Sales_Model_Order_Invoice_Item setPrice(float $value)
74: * @method float getBaseRowTotalInclTax()
75: * @method Mage_Sales_Model_Order_Invoice_Item setBaseRowTotalInclTax(float $value)
76: * @method float getRowTotalInclTax()
77: * @method Mage_Sales_Model_Order_Invoice_Item setRowTotalInclTax(float $value)
78: * @method int getProductId()
79: * @method Mage_Sales_Model_Order_Invoice_Item setProductId(int $value)
80: * @method int getOrderItemId()
81: * @method Mage_Sales_Model_Order_Invoice_Item setOrderItemId(int $value)
82: * @method string getAdditionalData()
83: * @method Mage_Sales_Model_Order_Invoice_Item setAdditionalData(string $value)
84: * @method string getDescription()
85: * @method Mage_Sales_Model_Order_Invoice_Item setDescription(string $value)
86: * @method string getWeeeTaxApplied()
87: * @method Mage_Sales_Model_Order_Invoice_Item setWeeeTaxApplied(string $value)
88: * @method string getSku()
89: * @method Mage_Sales_Model_Order_Invoice_Item setSku(string $value)
90: * @method string getName()
91: * @method Mage_Sales_Model_Order_Invoice_Item setName(string $value)
92: * @method float getHiddenTaxAmount()
93: * @method Mage_Sales_Model_Order_Invoice_Item setHiddenTaxAmount(float $value)
94: * @method float getBaseHiddenTaxAmount()
95: * @method Mage_Sales_Model_Order_Invoice_Item setBaseHiddenTaxAmount(float $value)
96: *
97: * @category Mage
98: * @package Mage_Sales
99: * @author Magento Core Team <core@magentocommerce.com>
100: */
101: class Mage_Sales_Model_Order_Invoice_Item extends Mage_Core_Model_Abstract
102: {
103: protected $_eventPrefix = 'sales_invoice_item';
104: protected $_eventObject = 'invoice_item';
105:
106: protected $_invoice = null;
107: protected $_orderItem = null;
108:
109: /**
110: * Initialize resource model
111: */
112: function _construct()
113: {
114: $this->_init('sales/order_invoice_item');
115: }
116:
117: /**
118: * Init mapping array of short fields to
119: * its full names
120: *
121: * @return Varien_Object
122: */
123: protected function _initOldFieldsMap()
124: {
125: $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('invoice_item');
126: return $this;
127: }
128: /**
129: * Declare invoice instance
130: *
131: * @param Mage_Sales_Model_Order_Invoice $invoice
132: * @return Mage_Sales_Model_Order_Invoice_Item
133: */
134: public function setInvoice(Mage_Sales_Model_Order_Invoice $invoice)
135: {
136: $this->_invoice = $invoice;
137: return $this;
138: }
139:
140: /**
141: * Retrieve invoice instance
142: *
143: * @return Mage_Sales_Model_Order_Invoice
144: */
145: public function getInvoice()
146: {
147: return $this->_invoice;
148: }
149:
150: /**
151: * Declare order item instance
152: *
153: * @param Mage_Sales_Model_Order_Item $item
154: * @return Mage_Sales_Model_Order_Invoice_Item
155: */
156: public function setOrderItem(Mage_Sales_Model_Order_Item $item)
157: {
158: $this->_orderItem = $item;
159: $this->setOrderItemId($item->getId());
160: return $this;
161: }
162:
163: /**
164: * Retrieve order item instance
165: *
166: * @return Mage_Sales_Model_Order_Item
167: */
168: public function getOrderItem()
169: {
170: if (is_null($this->_orderItem)) {
171: if ($this->getInvoice()) {
172: $this->_orderItem = $this->getInvoice()->getOrder()->getItemById($this->getOrderItemId());
173: }
174: else {
175: $this->_orderItem = Mage::getModel('sales/order_item')
176: ->load($this->getOrderItemId());
177: }
178: }
179: return $this->_orderItem;
180: }
181:
182: /**
183: * Declare qty
184: *
185: * @param float $qty
186: * @return Mage_Sales_Model_Order_Invoice_Item
187: */
188: public function setQty($qty)
189: {
190: if ($this->getOrderItem()->getIsQtyDecimal()) {
191: $qty = (float) $qty;
192: }
193: else {
194: $qty = (int) $qty;
195: }
196: $qty = $qty > 0 ? $qty : 0;
197: /**
198: * Check qty availability
199: */
200: $qtyToInvoice = sprintf("%F", $this->getOrderItem()->getQtyToInvoice());
201: $qty = sprintf("%F", $qty);
202: if ($qty <= $qtyToInvoice || $this->getOrderItem()->isDummy()) {
203: $this->setData('qty', $qty);
204: }
205: else {
206: Mage::throwException(
207: Mage::helper('sales')->__('Invalid qty to invoice item "%s"', $this->getName())
208: );
209: }
210: return $this;
211: }
212:
213: /**
214: * Applying qty to order item
215: *
216: * @return Mage_Sales_Model_Order_Invoice_Item
217: */
218: public function register()
219: {
220: $orderItem = $this->getOrderItem();
221: $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()+$this->getQty());
222:
223: $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()+$this->getTaxAmount());
224: $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()+$this->getBaseTaxAmount());
225: $orderItem->setHiddenTaxInvoiced($orderItem->getHiddenTaxInvoiced()+$this->getHiddenTaxAmount());
226: $orderItem->setBaseHiddenTaxInvoiced($orderItem->getBaseHiddenTaxInvoiced()+$this->getBaseHiddenTaxAmount());
227:
228: $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()+$this->getDiscountAmount());
229: $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()+$this->getBaseDiscountAmount());
230:
231: $orderItem->setRowInvoiced($orderItem->getRowInvoiced()+$this->getRowTotal());
232: $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()+$this->getBaseRowTotal());
233: return $this;
234: }
235:
236: /**
237: * Cancelling invoice item
238: *
239: * @return Mage_Sales_Model_Order_Invoice_Item
240: */
241: public function cancel()
242: {
243: $orderItem = $this->getOrderItem();
244: $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()-$this->getQty());
245:
246: $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()-$this->getTaxAmount());
247: $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()-$this->getBaseTaxAmount());
248: $orderItem->setHiddenTaxInvoiced($orderItem->getHiddenTaxInvoiced()-$this->getHiddenTaxAmount());
249: $orderItem->setBaseHiddenTaxInvoiced($orderItem->getBaseHiddenTaxInvoiced()-$this->getBaseHiddenTaxAmount());
250:
251:
252: $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()-$this->getDiscountAmount());
253: $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()-$this->getBaseDiscountAmount());
254:
255: $orderItem->setRowInvoiced($orderItem->getRowInvoiced()-$this->getRowTotal());
256: $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()-$this->getBaseRowTotal());
257: return $this;
258: }
259:
260: /**
261: * Invoice item row total calculation
262: *
263: * @return Mage_Sales_Model_Order_Invoice_Item
264: */
265: public function calcRowTotal()
266: {
267: $invoice = $this->getInvoice();
268: $orderItem = $this->getOrderItem();
269: $orderItemQty = $orderItem->getQtyOrdered();
270:
271: $rowTotal = $orderItem->getRowTotal() - $orderItem->getRowInvoiced();
272: $baseRowTotal = $orderItem->getBaseRowTotal() - $orderItem->getBaseRowInvoiced();
273: $rowTotalInclTax = $orderItem->getRowTotalInclTax();
274: $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
275:
276: if (!$this->isLast()) {
277: $availableQty = $orderItemQty - $orderItem->getQtyInvoiced();
278: $rowTotal = $invoice->roundPrice($rowTotal / $availableQty * $this->getQty());
279: $baseRowTotal = $invoice->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
280: }
281:
282: $this->setRowTotal($rowTotal);
283: $this->setBaseRowTotal($baseRowTotal);
284:
285: if ($rowTotalInclTax && $baseRowTotalInclTax) {
286: $this->setRowTotalInclTax($invoice->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including'));
287: $this->setBaseRowTotalInclTax($invoice->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base'));
288: }
289: return $this;
290: }
291:
292: /**
293: * Checking if the item is last
294: *
295: * @return bool
296: */
297: public function isLast()
298: {
299: if ((string)(float)$this->getQty() == (string)(float)$this->getOrderItem()->getQtyToInvoice()) {
300: return true;
301: }
302: return false;
303: }
304:
305: /**
306: * Before object save
307: *
308: * @return Mage_Sales_Model_Order_Invoice_Item
309: */
310: protected function _beforeSave()
311: {
312: parent::_beforeSave();
313:
314: if (!$this->getParentId() && $this->getInvoice()) {
315: $this->setParentId($this->getInvoice()->getId());
316: }
317:
318: return $this;
319: }
320:
321: /**
322: * After object save
323: *
324: * @return Mage_Sales_Model_Order_Invoice_Item
325: */
326: protected function _afterSave()
327: {
328: if (null ==! $this->_orderItem) {
329: $this->_orderItem->save();
330: }
331:
332: parent::_afterSave();
333: return $this;
334: }
335: }
336: