1: <?php
2:
3: /**
4: * ShipSync
5: *
6: * @category IllApps
7: * @package IllApps_Shipsync
8: * @copyright Copyright (c) 2014 EcoMATICS, Inc. DBA IllApps (http://www.illapps.com)
9: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10: */
11:
12: /**
13: * IllApps_Shipsync_Model_Shipment_Response
14: */
15: class IllApps_Shipsync_Model_Shipment_Response extends Varien_Object
16: {
17: protected $_response;
18: protected $_errors = '';
19:
20: const INCOMPLETE_API = 'Error: Incomplete API response. Please check your request and try again.';
21:
22: /*
23: * Set response
24: *
25: * @return IllApps_Shipsync_Model_Shipment_Response
26: */
27: public function setResponse($response)
28: {
29: $this->_response = $response;
30: return $this;
31: }
32:
33: /*
34: * Return response
35: *
36: * @return stdClass Object
37: */
38: public function getResponse()
39: {
40: return $this->_response;
41: }
42:
43: /*
44: * Set notifications errors
45: *
46: * @return bool
47: */
48: public function setNotificationsErrors()
49: {
50: $this->setHighestSeverity($this->_response->HighestSeverity);
51:
52: if ($this->_response->HighestSeverity == 'FAILURE' || $this->_response->HighestSeverity == 'ERROR')
53: {
54: $this->collectErrors($this->_response->Notifications);
55: $this->setErrors($this->_errors);
56: return true;
57: }
58: return false;
59: }
60:
61: /*
62: * Collect response errors, recursively (you know, for style points and all)
63: *
64: * @param stdClass Object
65: */
66: public function collectErrors($notification)
67: {
68: if(!is_array($notification))
69: {
70: $this->_errors .= $notification->Severity . ': ' . $notification->Message . '<br />';
71: }
72: else
73: {
74: foreach ($notification as $notice) { $this->collectErrors($notice); }
75: }
76: }
77:
78: /*
79: * Is (response highest severity a) warning?
80: *
81: * @return bool
82: */
83: public function isWarning()
84: {
85: return $this->getHighestSeverity() == 'WARNING' ? true : false;
86: }
87:
88: /*
89: * Incomplete Api
90: *
91: * @return String
92: */
93: public function incompleteApi()
94: {
95: return Mage::helper('usa')->__(self::INCOMPLETE_API);
96: }
97:
98: /*
99: * Finds structure of response for the key given. Valid args
100: *
101: * @param String
102: * @return String
103: */
104: public function findStructure($var)
105: {
106: if ($obj = $this->getPackageRateDetails())
107: {
108: return isset($obj->NetCharge->$var) ? $obj->NetCharge->$var : $obj->BillingWeight->$var;
109: }
110: elseif ($obj = $this->getShipmentRateDetails())
111: {
112: return isset($obj->NetCharge->$var) ? $obj->TotalNetCharge->$var : $obj->TotalBillingWeight->$var;
113: }
114:
115: }
116:
117: /*
118: * Tests if path is available in response.
119: *
120: * @return stdClass Object || false
121: */
122: public function getPackageRateDetails()
123: {
124: if (isset($this->_response->CompletedShipmentDetail->CompletedPackageDetails->PackageRating->PackageRateDetails))
125: {
126: $var = $this->_response->CompletedShipmentDetail->CompletedPackageDetails->PackageRating->PackageRateDetails;
127: return is_array($var) ? $this->_response->CompletedShipmentDetail->CompletedPackageDetails->PackageRating->PackageRateDetails[0] : $var;
128: }
129: return false;
130: }
131:
132: /*
133: * Tests if path is available in response.
134: *
135: * @return stdClass Object || false
136: */
137: public function getShipmentRateDetails()
138: {
139: if (isset($this->_response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails))
140: {
141: $var = $this->_response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails;
142: return is_array($var) ? $this->_response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails[0] : $var;
143: }
144: return false;
145: }
146:
147: /*
148: *
149: */
150: public function getTrackingNumber()
151: {
152: return !is_array($this->_response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds) ?
153: $this->_response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber :
154: $this->_response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds[1]->TrackingNumber;
155: }
156:
157: /*
158: *
159: */
160: public function getMasterTrackingId()
161: {
162: return isset($this->_response->CompletedShipmentDetail->MasterTrackingId) ?
163: $this->_response->CompletedShipmentDetail->MasterTrackingId : false;
164: }
165:
166: /*
167: *
168: */
169: public function getCodLabelImage()
170: {
171: return isset($this->_response->CompletedShipmentDetail->CodReturnDetail->Label->Parts->Image) ?
172: base64_encode($this->_response->CompletedShipmentDetail->CodReturnDetail->Label->Parts->Image) : null;
173: }
174:
175: /*
176: *
177: */
178: public function getSequenceNumber()
179: {
180: return isset($this->_response->CompletedShipmentDetail->CompletedPackageDetails->SequenceNumber) ?
181: $this->_response->CompletedShipmentDetail->CompletedPackageDetails->SequenceNumber : 1;
182: }
183: }
184: