1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16:
17: abstract class IllApps_Shipsync_Model_Shipping_Carrier_Abstract extends Mage_Usa_Model_Shipping_Carrier_Abstract
18: {
19:
20: const USA_COUNTRY_ID = 'US';
21: const PUERTORICO_COUNTRY_ID = 'PR';
22: const GUAM_COUNTRY_ID = 'GU';
23: const GUAM_REGION_CODE = 'GU';
24:
25: protected static $_quotesCache = array();
26:
27: 28: 29: 30: 31:
32: protected $_activeFlag = 'active';
33:
34: 35: 36: 37: 38: 39:
40: public function setActiveFlag($code = 'active')
41: {
42: $this->_activeFlag = $code;
43: return $this;
44: }
45:
46: 47: 48: 49: 50:
51: public function getCarrierCode()
52: {
53: return isset($this->_code) ? $this->_code : null;
54: }
55:
56: public function getTrackingInfo($tracking)
57: {
58: $info = array();
59:
60: $result = $this->getTracking($tracking);
61:
62: if($result instanceof Mage_Shipping_Model_Tracking_Result){
63: if ($trackings = $result->getAllTrackings()) {
64: return $trackings[0];
65: }
66: }
67: elseif (is_string($result) && !empty($result)) {
68: return $result;
69: }
70:
71: return false;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function isTrackingAvailable()
81: {
82: return true;
83: }
84:
85: 86: 87: 88: 89:
90: public function isCityRequired()
91: {
92: return true;
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function isZipCodeRequired($countryId = null)
102: {
103: if ($countryId != null) {
104: return !Mage::helper('directory')->isZipCodeOptional($countryId);
105: }
106: return true;
107: }
108:
109: 110: 111: 112: 113:
114: public function isShippingLabelsAvailable()
115: {
116: return true;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public function getAllItems(Mage_Shipping_Model_Rate_Request $request)
128: {
129: $items = array();
130: if ($request->getAllItems()) {
131: foreach ($request->getAllItems() as $item) {
132:
133: if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
134:
135: continue;
136: }
137:
138: if ($item->getHasChildren() && $item->isShipSeparately()) {
139: foreach ($item->getChildren() as $child) {
140: if (!$child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
141: $items[] = $child;
142: }
143: }
144: } else {
145:
146: $items[] = $item;
147: }
148: }
149: }
150: return $items;
151: }
152:
153: 154: 155: 156: 157: 158:
159: public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
160: {
161:
162: if(!count($this->getAllItems($request))) {
163: return $this;
164: }
165:
166: $maxAllowedWeight = (float) $this->getConfigData('max_package_weight');
167: $errorMsg = '';
168: $configErrorMsg = $this->getConfigData('specificerrmsg');
169: $defaultErrorMsg = Mage::helper('shipping')->__('The shipping module is not available.');
170: $showMethod = $this->getConfigData('showmethod');
171:
172: foreach ($this->getAllItems($request) as $item) {
173: if ($item->getProduct() && $item->getProduct()->getId()) {
174: $weight = $item->getProduct()->getWeight();
175: $stockItem = $item->getProduct()->getStockItem();
176: $doValidation = true;
177:
178: if ($stockItem->getIsQtyDecimal() && $stockItem->getIsDecimalDivided()) {
179: if ($stockItem->getEnableQtyIncrements() && $stockItem->getQtyIncrements()) {
180: $weight = $weight * $stockItem->getQtyIncrements();
181: } else {
182: $doValidation = false;
183: }
184: } elseif ($stockItem->getIsQtyDecimal() && !$stockItem->getIsDecimalDivided()) {
185: $weight = $weight * $item->getQty();
186: }
187:
188: if ($doValidation && $weight > $maxAllowedWeight) {
189: $errorMsg = ($configErrorMsg) ? $configErrorMsg : $defaultErrorMsg;
190: break;
191: }
192: }
193: }
194:
195: if (!$errorMsg && !$request->getDestPostcode() && $this->isZipCodeRequired($request->getDestCountryId())) {
196: $errorMsg = Mage::helper('shipping')->__('This shipping method is not available, please specify ZIP-code');
197: }
198:
199: if ($errorMsg && $showMethod) {
200: $error = Mage::getModel('shipping/rate_result_error');
201: $error->setCarrier($this->_code);
202: $error->setCarrierTitle($this->getConfigData('title'));
203: $error->setErrorMessage($errorMsg);
204: return $error;
205: } elseif ($errorMsg) {
206: return false;
207: }
208: return $this;
209: }
210:
211: 212: 213: 214: 215: 216:
217: protected function _getQuotesCacheKey($requestParams)
218: {
219: if (is_array($requestParams)) {
220: $requestParams = implode(',', array_merge(
221: array($this->getCarrierCode()),
222: array_keys($requestParams),
223: $requestParams)
224: );
225: }
226: return crc32($requestParams);
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237:
238: protected function _getCachedQuotes($requestParams)
239: {
240: $key = $this->_getQuotesCacheKey($requestParams);
241: return isset(self::$_quotesCache[$key]) ? self::$_quotesCache[$key] : null;
242: }
243:
244: 245: 246: 247: 248: 249: 250:
251: protected function _setCachedQuotes($requestParams, $response)
252: {
253: $key = $this->_getQuotesCacheKey($requestParams);
254: self::$_quotesCache[$key] = $response;
255: return $this;
256: }
257:
258: 259: 260: 261: 262: 263:
264: protected function _prepareServiceName($name)
265: {
266: $name = html_entity_decode((string)$name);
267: $name = strip_tags(preg_replace('#&\w+;#', '', $name));
268: return trim($name);
269: }
270:
271: 272: 273: 274: 275: 276: 277:
278: protected function _prepareShipmentRequest(Varien_Object $request)
279: {
280: $phonePattern = '/[\s\_\-\(\)]+/';
281: $phoneNumber = $request->getShipperContactPhoneNumber();
282: $phoneNumber = preg_replace($phonePattern, '', $phoneNumber);
283: $request->setShipperContactPhoneNumber($phoneNumber);
284: $phoneNumber = $request->getRecipientContactPhoneNumber();
285: $phoneNumber = preg_replace($phonePattern, '', $phoneNumber);
286: $request->setRecipientContactPhoneNumber($phoneNumber);
287: }
288:
289: 290: 291: 292: 293: 294:
295: public function requestToShipment(Mage_Shipping_Model_Shipment_Request $request)
296: {
297: $packages = $request->getPackages();
298:
299: if (!is_array($packages) || !$packages) {
300: Mage::throwException(Mage::helper('usa')->__('No packages for request'));
301: }
302: if ($request->getStoreId() != null) {
303: $this->setStore($request->getStoreId());
304: }
305: $data = array();
306: foreach ($packages as $packageId => $package) {
307: $request->setPackageId($packageId);
308: $request->setPackagingType($package['params']['container']);
309: $request->setPackageWeight($package['params']['weight']);
310: $request->setPackageParams(new Varien_Object($package['params']));
311: $request->setPackageItems($package['items']);
312: $result = $this->_doShipmentRequest($request);
313:
314: if ($result->hasErrors()) {
315: $this->rollBack($data);
316: break;
317: } else {
318: $data[] = array(
319: 'tracking_number' => $result->getTrackingNumber(),
320: 'label_content' => $result->getShippingLabelContent()
321: );
322: }
323: if (!isset($isFirstRequest)) {
324: $request->setMasterTrackingId($result->getTrackingNumber());
325: $isFirstRequest = false;
326: }
327: }
328:
329: $response = new Varien_Object(array(
330: 'info' => $data
331: ));
332: if ($result->getErrors()) {
333: $response->setErrors($result->getErrors());
334: }
335: return $response;
336: }
337:
338: 339: 340: 341: 342: 343:
344: public function returnOfShipment($request)
345: {
346: $request->setIsReturn(true);
347: $packages = $request->getPackages();
348: if (!is_array($packages) || !$packages) {
349: Mage::throwException(Mage::helper('usa')->__('No packages for request'));
350: }
351: if ($request->getStoreId() != null) {
352: $this->setStore($request->getStoreId());
353: }
354: $data = array();
355: foreach ($packages as $packageId => $package) {
356: $request->setPackageId($packageId);
357: $request->setPackagingType($package['params']['container']);
358: $request->setPackageWeight($package['params']['weight']);
359: $request->setPackageParams(new Varien_Object($package['params']));
360: $request->setPackageItems($package['items']);
361: $result = $this->_doShipmentRequest($request);
362:
363: if ($result->hasErrors()) {
364: $this->rollBack($data);
365: break;
366: } else {
367: $data[] = array(
368: 'tracking_number' => $result->getTrackingNumber(),
369: 'label_content' => $result->getShippingLabelContent()
370: );
371: }
372: if (!isset($isFirstRequest)) {
373: $request->setMasterTrackingId($result->getTrackingNumber());
374: $isFirstRequest = false;
375: }
376: }
377:
378: $response = new Varien_Object(array(
379: 'info' => $data
380: ));
381: if ($result->getErrors()) {
382: $response->setErrors($result->getErrors());
383: }
384: return $response;
385: }
386:
387: 388: 389: 390: 391: 392: 393: 394:
395: public function rollBack($data)
396: {
397: return true;
398: }
399:
400: 401: 402: 403: 404: 405:
406: abstract protected function _doShipmentRequest(Varien_Object $request);
407:
408: 409: 410: 411: 412: 413:
414: protected function _isUSCountry($countyId)
415: {
416: switch ($countyId) {
417: case 'AS':
418: case 'GU':
419: case 'MP':
420: case 'PW':
421: case 'PR':
422: case 'VI':
423: case 'US';
424: return true;
425: }
426:
427: return false;
428: }
429:
430: 431: 432: 433: 434: 435:
436: public function isGirthAllowed($countyDest = null) {
437: return false;
438: }
439: }
440: