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: * Enter description here ...
29: *
30: * @method Mage_Sales_Model_Resource_Order_Creditmemo_Item _getResource()
31: * @method Mage_Sales_Model_Resource_Order_Creditmemo_Item getResource()
32: * @method int getParentId()
33: * @method Mage_Sales_Model_Order_Creditmemo_Item setParentId(int $value)
34: * @method float getWeeeTaxAppliedRowAmount()
35: * @method Mage_Sales_Model_Order_Creditmemo_Item setWeeeTaxAppliedRowAmount(float $value)
36: * @method float getBasePrice()
37: * @method Mage_Sales_Model_Order_Creditmemo_Item setBasePrice(float $value)
38: * @method float getBaseWeeeTaxRowDisposition()
39: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseWeeeTaxRowDisposition(float $value)
40: * @method float getTaxAmount()
41: * @method Mage_Sales_Model_Order_Creditmemo_Item setTaxAmount(float $value)
42: * @method float getBaseWeeeTaxAppliedAmount()
43: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseWeeeTaxAppliedAmount(float $value)
44: * @method float getWeeeTaxRowDisposition()
45: * @method Mage_Sales_Model_Order_Creditmemo_Item setWeeeTaxRowDisposition(float $value)
46: * @method float getBaseRowTotal()
47: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseRowTotal(float $value)
48: * @method float getDiscountAmount()
49: * @method Mage_Sales_Model_Order_Creditmemo_Item setDiscountAmount(float $value)
50: * @method float getRowTotal()
51: * @method Mage_Sales_Model_Order_Creditmemo_Item setRowTotal(float $value)
52: * @method float getWeeeTaxAppliedAmount()
53: * @method Mage_Sales_Model_Order_Creditmemo_Item setWeeeTaxAppliedAmount(float $value)
54: * @method float getBaseDiscountAmount()
55: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseDiscountAmount(float $value)
56: * @method float getBaseWeeeTaxDisposition()
57: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseWeeeTaxDisposition(float $value)
58: * @method float getPriceInclTax()
59: * @method Mage_Sales_Model_Order_Creditmemo_Item setPriceInclTax(float $value)
60: * @method float getBaseTaxAmount()
61: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseTaxAmount(float $value)
62: * @method float getWeeeTaxDisposition()
63: * @method Mage_Sales_Model_Order_Creditmemo_Item setWeeeTaxDisposition(float $value)
64: * @method float getBasePriceInclTax()
65: * @method Mage_Sales_Model_Order_Creditmemo_Item setBasePriceInclTax(float $value)
66: * @method float getQty()
67: * @method float getBaseCost()
68: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseCost(float $value)
69: * @method float getBaseWeeeTaxAppliedRowAmount()
70: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseWeeeTaxAppliedRowAmount(float $value)
71: * @method float getPrice()
72: * @method Mage_Sales_Model_Order_Creditmemo_Item setPrice(float $value)
73: * @method float getBaseRowTotalInclTax()
74: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseRowTotalInclTax(float $value)
75: * @method float getRowTotalInclTax()
76: * @method Mage_Sales_Model_Order_Creditmemo_Item setRowTotalInclTax(float $value)
77: * @method int getProductId()
78: * @method Mage_Sales_Model_Order_Creditmemo_Item setProductId(int $value)
79: * @method int getOrderItemId()
80: * @method Mage_Sales_Model_Order_Creditmemo_Item setOrderItemId(int $value)
81: * @method string getAdditionalData()
82: * @method Mage_Sales_Model_Order_Creditmemo_Item setAdditionalData(string $value)
83: * @method string getDescription()
84: * @method Mage_Sales_Model_Order_Creditmemo_Item setDescription(string $value)
85: * @method string getWeeeTaxApplied()
86: * @method Mage_Sales_Model_Order_Creditmemo_Item setWeeeTaxApplied(string $value)
87: * @method string getSku()
88: * @method Mage_Sales_Model_Order_Creditmemo_Item setSku(string $value)
89: * @method string getName()
90: * @method Mage_Sales_Model_Order_Creditmemo_Item setName(string $value)
91: * @method float getHiddenTaxAmount()
92: * @method Mage_Sales_Model_Order_Creditmemo_Item setHiddenTaxAmount(float $value)
93: * @method float getBaseHiddenTaxAmount()
94: * @method Mage_Sales_Model_Order_Creditmemo_Item setBaseHiddenTaxAmount(float $value)
95: *
96: * @category Mage
97: * @package Mage_Sales
98: * @author Magento Core Team <core@magentocommerce.com>
99: */
100: class Mage_Sales_Model_Order_Creditmemo_Item extends Mage_Core_Model_Abstract
101: {
102: protected $_eventPrefix = 'sales_creditmemo_item';
103: protected $_eventObject = 'creditmemo_item';
104: protected $_creditmemo = null;
105: protected $_orderItem = null;
106:
107: /**
108: * Initialize resource model
109: */
110: function _construct()
111: {
112: $this->_init('sales/order_creditmemo_item');
113: }
114:
115: /**
116: * Declare creditmemo instance
117: *
118: * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
119: * @return Mage_Sales_Model_Order_Creditmemo_Item
120: */
121: public function setCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
122: {
123: $this->_creditmemo = $creditmemo;
124: return $this;
125: }
126:
127: /**
128: * Init mapping array of short fields to
129: * its full names
130: *
131: * @return Varien_Object
132: */
133: protected function _initOldFieldsMap()
134: {
135: $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('creditmemo_item');
136: return $this;
137: }
138:
139: /**
140: * Retrieve creditmemo instance
141: *
142: * @return Mage_Sales_Model_Order_Creditmemo
143: */
144: public function getCreditmemo()
145: {
146: return $this->_creditmemo;
147: }
148:
149: /**
150: * Declare order item instance
151: *
152: * @param Mage_Sales_Model_Order_Item $item
153: * @return Mage_Sales_Model_Order_Creditmemo_Item
154: */
155: public function setOrderItem(Mage_Sales_Model_Order_Item $item)
156: {
157: $this->_orderItem = $item;
158: $this->setOrderItemId($item->getId());
159: return $this;
160: }
161:
162: /**
163: * Retrieve order item instance
164: *
165: * @return Mage_Sales_Model_Order_Item
166: */
167: public function getOrderItem()
168: {
169: if (is_null($this->_orderItem)) {
170: if ($this->getCreditmemo()) {
171: $this->_orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
172: }
173: else {
174: $this->_orderItem = Mage::getModel('sales/order_item')
175: ->load($this->getOrderItemId());
176: }
177: }
178: return $this->_orderItem;
179: }
180:
181: /**
182: * Declare qty
183: *
184: * @param float $qty
185: * @return Mage_Sales_Model_Order_Creditmemo_Item
186: */
187: public function setQty($qty)
188: {
189: if ($this->getOrderItem()->getIsQtyDecimal()) {
190: $qty = (float) $qty;
191: }
192: else {
193: $qty = (int) $qty;
194: }
195: $qty = $qty > 0 ? $qty : 0;
196: /**
197: * Check qty availability
198: */
199: if ($qty <= $this->getOrderItem()->getQtyToRefund() || $this->getOrderItem()->isDummy()) {
200: $this->setData('qty', $qty);
201: }
202: else {
203: Mage::throwException(
204: Mage::helper('sales')->__('Invalid qty to refund item "%s"', $this->getName())
205: );
206: }
207: return $this;
208: }
209:
210: /**
211: * Applying qty to order item
212: *
213: * @return Mage_Sales_Model_Order_Shipment_Item
214: */
215: public function register()
216: {
217: $orderItem = $this->getOrderItem();
218:
219: $orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $this->getQty());
220: $orderItem->setTaxRefunded($orderItem->getTaxRefunded() + $this->getTaxAmount());
221: $orderItem->setBaseTaxRefunded($orderItem->getBaseTaxRefunded() + $this->getBaseTaxAmount());
222: $orderItem->setHiddenTaxRefunded($orderItem->getHiddenTaxRefunded() + $this->getHiddenTaxAmount());
223: $orderItem->setBaseHiddenTaxRefunded($orderItem->getBaseHiddenTaxRefunded() + $this->getBaseHiddenTaxAmount());
224: $orderItem->setAmountRefunded($orderItem->getAmountRefunded() + $this->getRowTotal());
225: $orderItem->setBaseAmountRefunded($orderItem->getBaseAmountRefunded() + $this->getBaseRowTotal());
226: $orderItem->setDiscountRefunded($orderItem->getDiscountRefunded() + $this->getDiscountAmount());
227: $orderItem->setBaseDiscountRefunded($orderItem->getBaseDiscountRefunded() + $this->getBaseDiscountAmount());
228:
229: return $this;
230: }
231:
232: public function cancel()
233: {
234: $this->getOrderItem()->setQtyRefunded(
235: $this->getOrderItem()->getQtyRefunded()-$this->getQty()
236: );
237: $this->getOrderItem()->setTaxRefunded(
238: $this->getOrderItem()->getTaxRefunded()
239: - $this->getOrderItem()->getBaseTaxAmount() * $this->getQty() / $this->getOrderItem()->getQtyOrdered()
240: );
241: $this->getOrderItem()->setHiddenTaxRefunded(
242: $this->getOrderItem()->getHiddenTaxRefunded()
243: - $this->getOrderItem()->getHiddenTaxAmount() * $this->getQty() / $this->getOrderItem()->getQtyOrdered()
244: );
245: return $this;
246: }
247:
248: /**
249: * Invoice item row total calculation
250: *
251: * @return Mage_Sales_Model_Order_Invoice_Item
252: */
253: public function calcRowTotal()
254: {
255: $creditmemo = $this->getCreditmemo();
256: $orderItem = $this->getOrderItem();
257: $orderItemQtyInvoiced = $orderItem->getQtyInvoiced();
258:
259: $rowTotal = $orderItem->getRowInvoiced() - $orderItem->getAmountRefunded();
260: $baseRowTotal = $orderItem->getBaseRowInvoiced() - $orderItem->getBaseAmountRefunded();
261: $rowTotalInclTax = $orderItem->getRowTotalInclTax();
262: $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
263:
264: if (!$this->isLast()) {
265: $availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
266: $rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());
267: $baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
268: }
269: $this->setRowTotal($rowTotal);
270: $this->setBaseRowTotal($baseRowTotal);
271:
272: if ($rowTotalInclTax && $baseRowTotalInclTax) {
273: $orderItemQty = $orderItem->getQtyOrdered();
274: $this->setRowTotalInclTax(
275: $creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
276: );
277: $this->setBaseRowTotalInclTax(
278: $creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
279: );
280: }
281: return $this;
282: }
283:
284: /**
285: * Checking if the item is last
286: *
287: * @return bool
288: */
289: public function isLast()
290: {
291: $orderItem = $this->getOrderItem();
292: if ((string)(float)$this->getQty() == (string)(float)$orderItem->getQtyToRefund()
293: && !$orderItem->getQtyToInvoice()) {
294: return true;
295: }
296: return false;
297: }
298:
299: /**
300: * Before object save
301: *
302: * @return Mage_Sales_Model_Order_Creditmemo_Item
303: */
304: protected function _beforeSave()
305: {
306: parent::_beforeSave();
307:
308: if (!$this->getParentId() && $this->getCreditmemo()) {
309: $this->setParentId($this->getCreditmemo()->getId());
310: }
311:
312: return $this;
313: }
314: }
315: