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_Track _getResource()
31: * @method Mage_Sales_Model_Resource_Order_Shipment_Track getResource()
32: * @method int getParentId()
33: * @method Mage_Sales_Model_Order_Shipment_Track setParentId(int $value)
34: * @method float getWeight()
35: * @method Mage_Sales_Model_Order_Shipment_Track setWeight(float $value)
36: * @method float getQty()
37: * @method Mage_Sales_Model_Order_Shipment_Track setQty(float $value)
38: * @method int getOrderId()
39: * @method Mage_Sales_Model_Order_Shipment_Track setOrderId(int $value)
40: * @method string getNumber()
41: * @method Mage_Sales_Model_Order_Shipment_Track setNumber(string $value)
42: * @method string getDescription()
43: * @method Mage_Sales_Model_Order_Shipment_Track setDescription(string $value)
44: * @method string getTitle()
45: * @method Mage_Sales_Model_Order_Shipment_Track setTitle(string $value)
46: * @method string getCarrierCode()
47: * @method Mage_Sales_Model_Order_Shipment_Track setCarrierCode(string $value)
48: * @method string getCreatedAt()
49: * @method Mage_Sales_Model_Order_Shipment_Track setCreatedAt(string $value)
50: * @method string getUpdatedAt()
51: * @method Mage_Sales_Model_Order_Shipment_Track setUpdatedAt(string $value)
52: *
53: * @category Mage
54: * @package Mage_Sales
55: * @author Magento Core Team <core@magentocommerce.com>
56: */
57: class Mage_Sales_Model_Order_Shipment_Track extends Mage_Sales_Model_Abstract
58: {
59: const CUSTOM_CARRIER_CODE = 'custom';
60: protected $_shipment = null;
61:
62: protected $_eventPrefix = 'sales_order_shipment_track';
63: protected $_eventObject = 'track';
64:
65: /**
66: * Initialize resource model
67: */
68: function _construct()
69: {
70: $this->_init('sales/order_shipment_track');
71: }
72:
73: /**
74: * Init mapping array of short fields to
75: * its full names
76: *
77: * @resturn Varien_Object
78: */
79: protected function _initOldFieldsMap()
80: {
81: $this->_oldFieldsMap = array(
82: 'number' => 'track_number'
83: );
84: }
85:
86: /**
87: * Back compatibility with old versions.
88: *
89: * @return string
90: */
91: public function getNumber()
92: {
93: return $this->getData('track_number');
94: }
95:
96: /**
97: * Declare Shipment instance
98: *
99: * @param Mage_Sales_Model_Order_Shipment $shipment
100: * @return Mage_Sales_Model_Order_Shipment_Item
101: */
102: public function setShipment(Mage_Sales_Model_Order_Shipment $shipment)
103: {
104: $this->_shipment = $shipment;
105: return $this;
106: }
107:
108: /**
109: * Retrieve Shipment instance
110: *
111: * @return Mage_Sales_Model_Order_Shipment
112: */
113: public function getShipment()
114: {
115: if (!($this->_shipment instanceof Mage_Sales_Model_Order_Shipment)) {
116: $this->_shipment = Mage::getModel('sales/order_shipment')->load($this->getParentId());
117: }
118:
119: return $this->_shipment;
120: }
121:
122: public function isCustom()
123: {
124: return $this->getCarrierCode() == self::CUSTOM_CARRIER_CODE;
125: }
126:
127: /**
128: * Retrieve hash code of current order
129: *
130: * @return string
131: */
132: public function getProtectCode()
133: {
134: return (string)$this->getShipment()->getProtectCode();
135: }
136:
137: /**
138: * Retrieve detail for shipment track
139: *
140: * @return string
141: */
142: public function getNumberDetail()
143: {
144: $carrierInstance = Mage::getSingleton('shipping/config')->getCarrierInstance($this->getCarrierCode());
145: if (!$carrierInstance) {
146: $custom = array();
147: $custom['title'] = $this->getTitle();
148: $custom['number'] = $this->getTrackNumber();
149: return $custom;
150: } else {
151: $carrierInstance->setStore($this->getStore());
152: }
153:
154: if (!$trackingInfo = $carrierInstance->getTrackingInfo($this->getNumber())) {
155: return Mage::helper('sales')->__('No detail for number "%s"', $this->getNumber());
156: }
157:
158: return $trackingInfo;
159: }
160:
161: /**
162: * Get store object
163: *
164: * @return Mage_Core_Model_Store
165: */
166: public function getStore()
167: {
168: if ($this->getShipment()) {
169: return $this->getShipment()->getStore();
170: }
171: return Mage::app()->getStore();
172: }
173:
174: /**
175: * Before object save
176: *
177: * @return Mage_Sales_Model_Order_Shipment_Track
178: */
179: protected function _beforeSave()
180: {
181: parent::_beforeSave();
182:
183: if (!$this->getParentId() && $this->getShipment()) {
184: $this->setParentId($this->getShipment()->getId());
185: }
186:
187: return $this;
188: }
189: }
190: