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_Shipment_Item _getResource()
31: * @method Mage_Sales_Model_Resource_Order_Shipment_Item getResource()
32: * @method int getParentId()
33: * @method Mage_Sales_Model_Order_Shipment_Item setParentId(int $value)
34: * @method float getRowTotal()
35: * @method Mage_Sales_Model_Order_Shipment_Item setRowTotal(float $value)
36: * @method float getPrice()
37: * @method Mage_Sales_Model_Order_Shipment_Item setPrice(float $value)
38: * @method float getWeight()
39: * @method Mage_Sales_Model_Order_Shipment_Item setWeight(float $value)
40: * @method float getQty()
41: * @method int getProductId()
42: * @method Mage_Sales_Model_Order_Shipment_Item setProductId(int $value)
43: * @method int getOrderItemId()
44: * @method Mage_Sales_Model_Order_Shipment_Item setOrderItemId(int $value)
45: * @method string getAdditionalData()
46: * @method Mage_Sales_Model_Order_Shipment_Item setAdditionalData(string $value)
47: * @method string getDescription()
48: * @method Mage_Sales_Model_Order_Shipment_Item setDescription(string $value)
49: * @method string getName()
50: * @method Mage_Sales_Model_Order_Shipment_Item setName(string $value)
51: * @method string getSku()
52: * @method Mage_Sales_Model_Order_Shipment_Item setSku(string $value)
53: *
54: * @category Mage
55: * @package Mage_Sales
56: * @author Magento Core Team <core@magentocommerce.com>
57: */
58: class Mage_Sales_Model_Order_Shipment_Item extends Mage_Core_Model_Abstract
59: {
60: protected $_eventPrefix = 'sales_shipment_item';
61: protected $_eventObject = 'shipment_item';
62:
63: protected $_shipment = null;
64: protected $_orderItem = null;
65:
66: /**
67: * Initialize resource model
68: */
69: function _construct()
70: {
71: $this->_init('sales/order_shipment_item');
72: }
73:
74: /**
75: * Declare Shipment instance
76: *
77: * @param Mage_Sales_Model_Order_Shipment $shipment
78: * @return Mage_Sales_Model_Order_Shipment_Item
79: */
80: public function setShipment(Mage_Sales_Model_Order_Shipment $shipment)
81: {
82: $this->_shipment = $shipment;
83: return $this;
84: }
85:
86: /**
87: * Retrieve Shipment instance
88: *
89: * @return Mage_Sales_Model_Order_Shipment
90: */
91: public function getShipment()
92: {
93: return $this->_shipment;
94: }
95:
96: /**
97: * Declare order item instance
98: *
99: * @param Mage_Sales_Model_Order_Item $item
100: * @return Mage_Sales_Model_Order_Shipment_Item
101: */
102: public function setOrderItem(Mage_Sales_Model_Order_Item $item)
103: {
104: $this->_orderItem = $item;
105: $this->setOrderItemId($item->getId());
106: return $this;
107: }
108:
109: /**
110: * Retrieve order item instance
111: *
112: * @return Mage_Sales_Model_Order_Item
113: */
114: public function getOrderItem()
115: {
116: if (is_null($this->_orderItem)) {
117: if ($this->getShipment()) {
118: $this->_orderItem = $this->getShipment()->getOrder()->getItemById($this->getOrderItemId());
119: }
120: else {
121: $this->_orderItem = Mage::getModel('sales/order_item')
122: ->load($this->getOrderItemId());
123: }
124: }
125: return $this->_orderItem;
126: }
127:
128: /**
129: * Declare qty
130: *
131: * @param float $qty
132: * @return Mage_Sales_Model_Order_Invoice_Item
133: */
134: public function setQty($qty)
135: {
136: if ($this->getOrderItem()->getIsQtyDecimal()) {
137: $qty = (float) $qty;
138: }
139: else {
140: $qty = (int) $qty;
141: }
142: $qty = $qty > 0 ? $qty : 0;
143: /**
144: * Check qty availability
145: */
146: if ($qty <= $this->getOrderItem()->getQtyToShip() || $this->getOrderItem()->isDummy(true)) {
147: $this->setData('qty', $qty);
148: }
149: else {
150: Mage::throwException(
151: Mage::helper('sales')->__('Invalid qty to ship for item "%s"', $this->getName())
152: );
153: }
154: return $this;
155: }
156:
157: /**
158: * Applying qty to order item
159: *
160: * @return Mage_Sales_Model_Order_Shipment_Item
161: */
162: public function register()
163: {
164: $this->getOrderItem()->setQtyShipped(
165: $this->getOrderItem()->getQtyShipped()+$this->getQty()
166: );
167: return $this;
168: }
169:
170: /**
171: * Before object save
172: *
173: * @return Mage_Sales_Model_Order_Shipment_Item
174: */
175: protected function _beforeSave()
176: {
177: parent::_beforeSave();
178:
179: if (!$this->getParentId() && $this->getShipment()) {
180: $this->setParentId($this->getShipment()->getId());
181: }
182:
183: return $this;
184: }
185:
186: }
187: