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