Overview

Packages

  • IllApps
    • Shipsync
  • PHP

Classes

  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_Abstract
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_Packages_View
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_Packages_View_Form
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_Packages_View_Items
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_Packages_View_Package
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_Shipment_View
  • IllApps_Shipsync_Block_Adminhtml_Sales_Order_View
  • IllApps_Shipsync_Block_Adminhtml_Shipsync
  • IllApps_Shipsync_Block_Adminhtml_Shipsync_Option
  • IllApps_Shipsync_Helper_Data
  • IllApps_Shipsync_IndexController
  • IllApps_Shipsync_Model_Mysql4_Setup
  • IllApps_Shipsync_Model_Mysql4_Shipment_Package
  • IllApps_Shipsync_Model_Mysql4_Shipment_Package_Collection
  • IllApps_Shipsync_Model_Sales_Order_Shipment_Package
  • IllApps_Shipsync_Model_Sales_Order_Shipment_Package_Item
  • IllApps_Shipsync_Model_Sales_Quote_Address
  • IllApps_Shipsync_Model_Shipment_Abstract
  • IllApps_Shipsync_Model_Shipment_Package
  • IllApps_Shipsync_Model_Shipment_Request
  • IllApps_Shipsync_Model_Shipment_Response
  • IllApps_Shipsync_Model_Shipment_Result
  • IllApps_Shipsync_Model_Shipping_Carrier_Abstract
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Address
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Package
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Rate
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Ship
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_B13afilingoption
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Freemethod
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Label_Image
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Label_Orientation
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Label_Stock
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Method
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Packaging
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Rate
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Ratediscount
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Residencedelivery
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Signature
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Smartpost_Endorsement
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Smartpost_Indicia
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Source_Unitofmeasure
  • IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Track
  • IllApps_Shipsync_Model_Shipping_Package
  • IllApps_Shipsync_Model_Shipping_Package_Item
  • IllApps_Shipsync_Model_Shipping_Package_Origins
  • IllApps_Shipsync_Model_Shipping_Package_Sort
  • Overview
  • Package
  • Class
  • Tree
  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:  * Abstract USA shipping carrier model
 14:  *
 15:  * @author      Magento Core Team <core@magentocommerce.com>
 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:      * Flag for check carriers for activity
 29:      *
 30:      * @var string
 31:      */
 32:     protected $_activeFlag = 'active';
 33: 
 34:     /**
 35:      * Set flag for check carriers for activity
 36:      *
 37:      * @param string $code
 38:      * @return Mage_Usa_Model_Shipping_Carrier_Abstract
 39:      */
 40:     public function setActiveFlag($code = 'active')
 41:     {
 42:         $this->_activeFlag = $code;
 43:         return $this;
 44:     }
 45: 
 46:     /**
 47:      * Return code of carrier
 48:      *
 49:      * @return string
 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:      * Check if carrier has shipping tracking option available
 76:      * All Mage_Usa carriers have shipping tracking option available
 77:      *
 78:      * @return boolean
 79:      */
 80:     public function isTrackingAvailable()
 81:     {
 82:         return true;
 83:     }
 84: 
 85:     /**
 86:      * Check if city option required
 87:      *
 88:      * @return boolean
 89:      */
 90:     public function isCityRequired()
 91:     {
 92:         return true;
 93:     }
 94: 
 95:     /**
 96:      * Determine whether zip-code is required for the country of destination
 97:      *
 98:      * @param string|null $countryId
 99:      * @return bool
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:      * Check if carrier has shipping label option available
111:      *
112:      * @return boolean
113:      */
114:     public function isShippingLabelsAvailable()
115:     {
116:         return true;
117:     }
118: 
119:     /**
120:      * Return items for further shipment rate evaluation. We need to pass children of a bundle instead passing the
121:      * bundle itself, otherwise we may not get a rate at all (e.g. when total weight of a bundle exceeds max weight
122:      * despite each item by itself is not)
123:      *
124:      * @param Mage_Shipping_Model_Rate_Request $request
125:      * @return array
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:                 /* @var $item Mage_Sales_Model_Quote_Item */
133:                 if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
134:                     // Don't process children here - we will process (or already have processed) them below
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:                     // Ship together - count compound item as one solid
146:                     $items[] = $item;
147:                 }
148:             }
149:         }
150:         return $items;
151:     }
152: 
153:     /**
154:      * Processing additional validation to check if carrier applicable.
155:      *
156:      * @param Mage_Shipping_Model_Rate_Request $request
157:      * @return Mage_Shipping_Model_Carrier_Abstract|Mage_Shipping_Model_Rate_Result_Error|boolean
158:      */
159:     public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
160:     {
161:         //Skip by item validation if there is no items in request
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:      * Returns cache key for some request to carrier quotes service
213:      *
214:      * @param string|array $requestParams
215:      * @return string
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:      * Checks whether some request to rates have already been done, so we have cache for it
231:      * Used to reduce number of same requests done to carrier service during one session
232:      *
233:      * Returns cached response or null
234:      *
235:      * @param string|array $requestParams
236:      * @return null|string
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:      * Sets received carrier quotes to cache
246:      *
247:      * @param string|array $requestParams
248:      * @param string $response
249:      * @return Mage_Usa_Model_Shipping_Carrier_Abstract
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:      * Prepare service name. Strip tags and entities from name
260:      *
261:      * @param string|object $name  service name or object with implemented __toString() method
262:      * @return string              prepared service name
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:      * Prepare shipment request.
273:      * Validate and correct request information
274:      *
275:      * @param Varien_Object $request
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:      * Do request to shipment
291:      *
292:      * @param Mage_Shipping_Model_Shipment_Request $request
293:      * @return array
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:      * Do request to RMA shipment
340:      *
341:      * @param $request
342:      * @return array
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:      * For multi package shipments. Delete requested shipments if the current shipment
389:      * request is failed
390:      *
391:      * @todo implement rollback logic
392:      * @param array $data
393:      * @return bool
394:      */
395:     public function rollBack($data)
396:     {
397:         return true;
398:     }
399: 
400:     /**
401:      * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
402:      *
403:      * @param Varien_Object $request
404:      * @return Varien_Object
405:      */
406:     abstract protected function _doShipmentRequest(Varien_Object $request);
407: 
408:     /**
409:      * Check is Country U.S. Possessions and Trust Territories
410:      *
411:      * @param string $countyId
412:      * @return boolean
413:      */
414:     protected function _isUSCountry($countyId)
415:     {
416:         switch ($countyId) {
417:             case 'AS': // Samoa American
418:             case 'GU': // Guam
419:             case 'MP': // Northern Mariana Islands
420:             case 'PW': // Palau
421:             case 'PR': // Puerto Rico
422:             case 'VI': // Virgin Islands US
423:             case 'US'; // United States
424:                 return true;
425:         }
426: 
427:         return false;
428:     }
429: 
430:     /**
431:      * Check whether girth is allowed for the carrier
432:      *
433:      * @param null|string $countyDest
434:      * @return bool
435:      */
436:     public function isGirthAllowed($countyDest = null) {
437:         return false;
438:     }
439: }
440: 
shipsync-community API documentation generated by ApiGen 2.8.0