Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Usa_Block_Adminhtml_Dhl_Unitofmeasure
  • Mage_Usa_Helper_Data
  • Mage_Usa_Model_Shipping_Carrier_Abstract
  • Mage_Usa_Model_Shipping_Carrier_Abstract_Source_Mode
  • Mage_Usa_Model_Shipping_Carrier_Abstract_Source_Requesttype
  • Mage_Usa_Model_Shipping_Carrier_Dhl
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Contenttype
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Abstract
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Doc
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Freedoc
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Freenondoc
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Nondoc
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Size
  • Mage_Usa_Model_Shipping_Carrier_Dhl_International_Source_Method_Unitofmeasure
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_PageBuilder
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Dutypaymenttype
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Freemethod
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Method
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Protection_Rounding
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Protection_Value
  • Mage_Usa_Model_Shipping_Carrier_Dhl_Source_Shipmenttype
  • Mage_Usa_Model_Shipping_Carrier_Fedex
  • Mage_Usa_Model_Shipping_Carrier_Fedex_Source_Dropoff
  • Mage_Usa_Model_Shipping_Carrier_Fedex_Source_Freemethod
  • Mage_Usa_Model_Shipping_Carrier_Fedex_Source_Method
  • Mage_Usa_Model_Shipping_Carrier_Fedex_Source_Packaging
  • Mage_Usa_Model_Shipping_Carrier_Ups
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Container
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_DestType
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Freemethod
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Method
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_OriginShipment
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Pickup
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Type
  • Mage_Usa_Model_Shipping_Carrier_Ups_Source_Unitofmeasure
  • Mage_Usa_Model_Shipping_Carrier_Usps
  • Mage_Usa_Model_Shipping_Carrier_Usps_Source_Container
  • Mage_Usa_Model_Shipping_Carrier_Usps_Source_Freemethod
  • Mage_Usa_Model_Shipping_Carrier_Usps_Source_Machinable
  • Mage_Usa_Model_Shipping_Carrier_Usps_Source_Method
  • Mage_Usa_Model_Shipping_Carrier_Usps_Source_Size
  • Overview
  • Package
  • Class
  • Tree
  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_Usa
 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:  * Abstract USA shipping carrier model
 29:  *
 30:  * @author      Magento Core Team <core@magentocommerce.com>
 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:      * Flag for check carriers for activity
 44:      *
 45:      * @var string
 46:      */
 47:     protected $_activeFlag = 'active';
 48: 
 49:     /**
 50:      * Set flag for check carriers for activity
 51:      *
 52:      * @param string $code
 53:      * @return Mage_Usa_Model_Shipping_Carrier_Abstract
 54:      */
 55:     public function setActiveFlag($code = 'active')
 56:     {
 57:         $this->_activeFlag = $code;
 58:         return $this;
 59:     }
 60: 
 61:     /**
 62:      * Return code of carrier
 63:      *
 64:      * @return string
 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:      * Check if carrier has shipping tracking option available
 91:      * All Mage_Usa carriers have shipping tracking option available
 92:      *
 93:      * @return boolean
 94:      */
 95:     public function isTrackingAvailable()
 96:     {
 97:         return true;
 98:     }
 99: 
100:     /**
101:      * Check if city option required
102:      *
103:      * @return boolean
104:      */
105:     public function isCityRequired()
106:     {
107:         return true;
108:     }
109: 
110:     /**
111:      * Determine whether zip-code is required for the country of destination
112:      *
113:      * @param string|null $countryId
114:      * @return bool
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:      * Check if carrier has shipping label option available
126:      *
127:      * @return boolean
128:      */
129:     public function isShippingLabelsAvailable()
130:     {
131:         return true;
132:     }
133: 
134:     /**
135:      * Return items for further shipment rate evaluation. We need to pass children of a bundle instead passing the
136:      * bundle itself, otherwise we may not get a rate at all (e.g. when total weight of a bundle exceeds max weight
137:      * despite each item by itself is not)
138:      *
139:      * @param Mage_Shipping_Model_Rate_Request $request
140:      * @return array
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:                 /* @var $item Mage_Sales_Model_Quote_Item */
148:                 if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
149:                     // Don't process children here - we will process (or already have processed) them below
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:                     // Ship together - count compound item as one solid
161:                     $items[] = $item;
162:                 }
163:             }
164:         }
165:         return $items;
166:     }
167: 
168:     /**
169:      * Processing additional validation to check if carrier applicable.
170:      *
171:      * @param Mage_Shipping_Model_Rate_Request $request
172:      * @return Mage_Shipping_Model_Carrier_Abstract|Mage_Shipping_Model_Rate_Result_Error|boolean
173:      */
174:     public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
175:     {
176:         //Skip by item validation if there is no items in request
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:      * Returns cache key for some request to carrier quotes service
228:      *
229:      * @param string|array $requestParams
230:      * @return string
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:      * Checks whether some request to rates have already been done, so we have cache for it
246:      * Used to reduce number of same requests done to carrier service during one session
247:      *
248:      * Returns cached response or null
249:      *
250:      * @param string|array $requestParams
251:      * @return null|string
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:      * Sets received carrier quotes to cache
261:      *
262:      * @param string|array $requestParams
263:      * @param string $response
264:      * @return Mage_Usa_Model_Shipping_Carrier_Abstract
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:      * Prepare service name. Strip tags and entities from name
275:      *
276:      * @param string|object $name  service name or object with implemented __toString() method
277:      * @return string              prepared service name
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:      * Prepare shipment request.
288:      * Validate and correct request information
289:      *
290:      * @param Varien_Object $request
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:      * Do request to shipment
306:      *
307:      * @param Mage_Shipping_Model_Shipment_Request $request
308:      * @return array
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:      * Do request to RMA shipment
354:      *
355:      * @param $request
356:      * @return array
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:      * For multi package shipments. Delete requested shipments if the current shipment
403:      * request is failed
404:      *
405:      * @todo implement rollback logic
406:      * @param array $data
407:      * @return bool
408:      */
409:     public function rollBack($data)
410:     {
411:         return true;
412:     }
413: 
414:     /**
415:      * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
416:      *
417:      * @param Varien_Object $request
418:      * @return Varien_Object
419:      */
420:     abstract protected function _doShipmentRequest(Varien_Object $request);
421: 
422:     /**
423:      * Check is Country U.S. Possessions and Trust Territories
424:      *
425:      * @param string $countyId
426:      * @return boolean
427:      */
428:     protected function _isUSCountry($countyId)
429:     {
430:         switch ($countyId) {
431:             case 'AS': // Samoa American
432:             case 'GU': // Guam
433:             case 'MP': // Northern Mariana Islands
434:             case 'PW': // Palau
435:             case 'PR': // Puerto Rico
436:             case 'VI': // Virgin Islands US
437:             case 'US'; // United States
438:                 return true;
439:         }
440: 
441:         return false;
442:     }
443: 
444:     /**
445:      * Check whether girth is allowed for the carrier
446:      *
447:      * @param null|string $countyDest
448:      * @return bool
449:      */
450:     public function isGirthAllowed($countyDest = null) {
451:         return false;
452:     }
453: }
454: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0