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_Shipping
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: class Mage_Shipping_Model_Info extends Varien_Object
29: {
30: /**
31: * Tracking info
32: *
33: * @var array
34: */
35: protected $_trackingInfo = array();
36:
37: /**
38: * Generating tracking info
39: *
40: * @param array $hash
41: * @return Mage_Shipping_Model_Info
42: */
43: public function loadByHash($hash)
44: {
45: /* @var $helper Mage_Shipping_Helper_Data */
46: $helper = Mage::helper('shipping');
47: $data = $helper->decodeTrackingHash($hash);
48: if (!empty($data)) {
49: $this->setData($data['key'], $data['id']);
50: $this->setProtectCode($data['hash']);
51:
52: if ($this->getOrderId() > 0) {
53: $this->getTrackingInfoByOrder();
54: } elseif($this->getShipId() > 0) {
55: $this->getTrackingInfoByShip();
56: } else {
57: $this->getTrackingInfoByTrackId();
58: }
59: }
60: return $this;
61: }
62:
63: /**
64: * Retrieve tracking info
65: *
66: * @return array
67: */
68: public function getTrackingInfo()
69: {
70: return $this->_trackingInfo;
71: }
72:
73: /**
74: * Instantiate order model
75: *
76: * @return Mage_Sales_Model_Order|bool
77: */
78: protected function _initOrder()
79: {
80: $order = Mage::getModel('sales/order')->load($this->getOrderId());
81:
82: if (!$order->getId() || $this->getProtectCode() != $order->getProtectCode()) {
83: return false;
84: }
85:
86: return $order;
87: }
88:
89: /**
90: * Instantiate ship model
91: *
92: * @return Mage_Sales_Model_Order_Shipment|bool
93: */
94: protected function _initShipment()
95: {
96: /* @var $model Mage_Sales_Model_Order_Shipment */
97: $model = Mage::getModel('sales/order_shipment');
98: $ship = $model->load($this->getShipId());
99: if (!$ship->getEntityId() || $this->getProtectCode() != $ship->getProtectCode()) {
100: return false;
101: }
102:
103: return $ship;
104: }
105:
106: /**
107: * Retrieve all tracking by order id
108: *
109: * @return array
110: */
111: public function getTrackingInfoByOrder()
112: {
113: $shipTrack = array();
114: $order = $this->_initOrder();
115: if ($order) {
116: $shipments = $order->getShipmentsCollection();
117: foreach ($shipments as $shipment){
118: $increment_id = $shipment->getIncrementId();
119: $tracks = $shipment->getTracksCollection();
120:
121: $trackingInfos=array();
122: foreach ($tracks as $track){
123: $trackingInfos[] = $track->getNumberDetail();
124: }
125: $shipTrack[$increment_id] = $trackingInfos;
126: }
127: }
128: $this->_trackingInfo = $shipTrack;
129: return $this->_trackingInfo;
130: }
131:
132: /**
133: * Retrieve all tracking by ship id
134: *
135: * @return array
136: */
137: public function getTrackingInfoByShip()
138: {
139: $shipTrack = array();
140: $shipment = $this->_initShipment();
141: if ($shipment) {
142: $increment_id = $shipment->getIncrementId();
143: $tracks = $shipment->getTracksCollection();
144:
145: $trackingInfos=array();
146: foreach ($tracks as $track){
147: $trackingInfos[] = $track->getNumberDetail();
148: }
149: $shipTrack[$increment_id] = $trackingInfos;
150:
151: }
152: $this->_trackingInfo = $shipTrack;
153: return $this->_trackingInfo;
154: }
155:
156: /**
157: * Retrieve tracking by tracking entity id
158: *
159: * @return array
160: */
161: public function getTrackingInfoByTrackId()
162: {
163: $track = Mage::getModel('sales/order_shipment_track')->load($this->getTrackId());
164: if ($track->getId() && $this->getProtectCode() == $track->getProtectCode()) {
165: $this->_trackingInfo = array(array($track->getNumberDetail()));
166: }
167: return $this->_trackingInfo;
168: }
169: }
170: