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:  * IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Rate
 14:  */
 15: class IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Rate extends IllApps_Shipsync_Model_Shipping_Carrier_Fedex
 16: {
 17:     
 18:     
 19:     protected $_rateRequest;
 20:     protected $_rateResult;
 21:     protected $_rateResultCollection;
 22:     protected $_rateResultError;
 23:     protected $_rateServiceClient;
 24:     protected $_rateServiceVersion = '14';
 25:     protected $_rateServiceWsdlPath = 'RateService_v14.wsdl';
 26:     protected $_saturdayDelivery;
 27:     
 28:     
 29:     
 30:     /**
 31:      * collectRates
 32:      *
 33:      * @param Mage_Shipping_Model_Rate_Request $request
 34:      * @return object
 35:      */
 36:     public function collectRates(Mage_Shipping_Model_Rate_Request $request)
 37:     {
 38:         
 39:         // Set client
 40:         $this->_rateServiceClient = $this->_initWebServices($this->_rateServiceWsdlPath);
 41:         
 42:         // Init request object
 43:         $this->_rateRequest = new Varien_Object();
 44:         
 45:         // Set rate request
 46:         $this->setRateRequest($request);
 47:         
 48:         // Rate result
 49:         $this->_rateResult = $this->_getQuotes();
 50:         
 51:         if ($this->_rateResult->getError()) {
 52:                 $error = Mage::getModel('shipping/rate_result_error');
 53:                 $error->setCarrier('fedex');
 54:                 $error->setCarrierTitle(Mage::getStoreConfig('carriers/fedex/title'));
 55:                 $error->setErrorMessage($this->_rateResultError);
 56:                 $this->_rateResult->append($error);
 57:         }
 58:         
 59:         // Return rate result
 60:         return $this->getRateResult();
 61:         
 62:     }
 63:     
 64:     
 65:     /**
 66:      * Get result
 67:      *
 68:      * @return object
 69:      */
 70:     public function getRateResult() { return $this->_rateResult; }
 71:     
 72:     
 73:     
 74:     /**
 75:      * setRateRequest
 76:      *
 77:      * @param Mage_Shipping_Model_Rate_Request $request
 78:      * @return IllApps_Shipsync_Model_Shipping_Carrier_Fedex_Rate
 79:      */
 80:     public function setRateRequest(Mage_Shipping_Model_Rate_Request $request)
 81:     {
 82:         $rateRequest = $this->_rateRequest;
 83:         
 84:         $rateRequest->setShippingPackage(Mage::getModel('shipsync/shipping_package'));
 85:         
 86:         $rateRequest->setOrder($request->getOrder());
 87:         
 88:         if ($request->getLimitMethod()) {
 89:             $rateRequest->setService($request->getLimitMethod());
 90:         }
 91:         
 92:         $rateRequest->setItems(Mage::getModel('shipsync/shipping_package')->getParsedItems($request->getAllItems()));       
 93:         
 94:         $rateRequest->setDefaultPackages(Mage::getModel('shipsync/shipping_package')->getDefaultPackages(array(
 95:             'fedex'
 96:         )));
 97:         
 98:         if ($request->getOrigCountry()) {
 99:             $origCountry = $request->getOrigCountry();
100:         } else {
101:             $origCountry = Mage::getStoreConfig('shipping/origin/country_id', $this->getStore());
102:             $rateRequest->setOrigCountry(Mage::getModel('directory/country')->load($origCountry)->getIso2Code());
103:         }
104:         
105:         // Shipper region id
106:         $shipperRegionId = Mage::getStoreConfig('shipping/origin/region_id');               
107:         
108:         // Shipper region code
109:         if (is_numeric($shipperRegionId)) {
110:             $rateRequest->setOrigRegionCode(Mage::getModel('directory/region')->load($shipperRegionId)->getCode());
111:         }               
112:         
113:         if ($request->getInsureShipment()) {
114:             $rateRequest->setInsureShipment(true)->setInsureAmount($request->getInsureAmount());
115:         }
116:         
117:         if ($request->getOrigPostcode()) {
118:             $rateRequest->setOrigPostcode($request->getOrigPostcode());
119:         } else {
120:             $rateRequest->setOrigPostcode(Mage::getStoreConfig('shipping/origin/postcode', $this->getStore()));
121:         }
122:         
123:         if ($request->getOrigCity()) {
124:             $rateRequest->setOrigCity($request->getOrigCity());
125:         } else {
126:             $rateRequest->setOrigCity(Mage::getStoreConfig('shipping/origin/city', $this->getStore()));
127:         }
128:         
129:         if (!$request->getOrigStreet()) {
130:             $shipperstreetlines = array(
131:                 Mage::getStoreConfig('shipping/origin/street_line1')
132:             );
133:             if (Mage::getStoreConfig('shipping/origin/street_line2') != '') {
134:                 $shipperstreetlines[] = Mage::getStoreConfig('shipping/origin/street_line2');
135:             }
136:             if (Mage::getStoreConfig('shipping/origin/street_line3') != '') {
137:                 $shipperstreetlines[] = Mage::getStoreConfig('shipping/origin/street_line3');
138:             }
139:             
140:             $rateRequest->setOrigStreet($shipperstreetlines);
141:         } else {
142:             $rateRequest->setOrigStreet($request->getOrigStreet());
143:         }
144:         
145:         if ($request->getDestStreet()) {
146:             $rateRequest->setDestStreet($request->getDestStreet());
147:         }
148:         
149:         if ($request->getDestCity()) {
150:             $rateRequest->setDestCity($request->getDestCity());
151:         }
152:         
153:         if ($request->getDestRegionCode() && strlen($request->getDestRegionCode()) === 2) {
154:             $rateRequest->setDestRegionCode($request->getDestRegionCode());
155:         }
156:         
157:         if ($request->getDestPostcode()) {
158:             $rateRequest->setDestPostcode($request->getDestPostcode());
159:         }
160:         
161:         if ($request->getDestCountryId()) {
162:             $rateRequest->setDestCountry(Mage::getModel('directory/country')->load($request->getDestCountryId())->getIso2Code());
163:         } else {
164:             $rateRequest->setDestCountry(Mage::getModel('directory/country')->load(self::USA_COUNTRY_ID)->getIso2Code());
165:         }
166:         
167:         
168:         $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
169:         $rateRequest->setWeight($weight);
170:         if ($request->getFreeMethodWeight()!= $request->getPackageWeight()) {
171:             $rateRequest->setFreeMethodWeight($request->getFreeMethodWeight());
172:         }
173: 
174:         if (($request->getAddressValidation() == 'ENABLED') && ($request->getResidenceDelivery() == 'VALIDATE')) {
175:             $rateRequest->setResidential($this->getResidential($rateRequest->getDestStreet(), $rateRequest->getDestPostcode()));
176:         }
177:         else if ($request->getResidenceDelivery() == 'ENABLED') {
178:             $rateRequest->setResidential(true);
179:         }
180:         else if ($request->getResidenceDelivery() == 'DISABLED') {
181:             $rateRequest->setResidential(false);
182:         }
183:         else {
184:             if (Mage::getStoreConfig('carriers/fedex/rate_address_validation')
185:                 && (Mage::getStoreConfig('carriers/fedex/residence_delivery') == 'VALIDATE')
186:                 && $rateRequest->getDestCountry() == 'US'
187:                 && $rateRequest->getDestStreet()
188:                 && $rateRequest->getDestPostcode())
189:             {
190:                 $rateRequest->setResidential($this->getResidential($rateRequest->getDestStreet(), $rateRequest->getDestPostcode()));
191:             }
192:             else if (Mage::getStoreConfig('carriers/fedex/residence_delivery') == 'DISABLED') {
193:                 $rateRequest->setResidential(false);
194:             }
195:             else {
196:                 $rateRequest->setResidential(true);
197:             }
198:         }
199: 
200:         $rateRequest->setValue($request->getPackagePhysicalValue());
201:         $rateRequest->setValueWithDiscount($request->getPackageValueWithDiscount());
202:         
203:         $rateRequest->setDropoff($this->getUnderscoreCodeFromCode(Mage::getStoreConfig('carriers/fedex/dropoff')));
204:         $rateRequest->setRateType(Mage::getStoreConfig('carriers/fedex/rate_type'));
205:         $rateRequest->setShipTimestamp(date('c'));
206:         $rateRequest->setSaturdayDelivery($this->_saturdayDelivery);
207:         
208:         if (Mage::getStoreConfig('carriers/fedex/enable_smartpost')) {
209:             $rateRequest->setEnableSmartPost(true);
210:             $rateRequest->setSmartPostIndiciaType(Mage::getStoreConfig('carriers/fedex/smartpost_indicia'));
211:             $rateRequest->setSmartPostAncillaryEndorsement(Mage::getStoreConfig('carriers/fedex/smartpost_endorsement'));
212:             $rateRequest->setSmartPostHubId(Mage::getStoreConfig('carriers/fedex/smartpost_hub_id'));
213:             $rateRequest->setSmartPostSpecialServices('USPS_DELIVERY_CONFIRMATION');
214:             
215:             if (Mage::getStoreConfig('carriers/fedex/smartpost_customer_manifest_id')) {
216:                 $rateRequest->setSmartPostCustomerManifestId(Mage::getStoreConfig('carriers/fedex/smartpost_customer_manifest_id'));
217:             }
218:         }
219:         
220:         if (Mage::getStoreConfig('carriers/fedex/default_commodity')) {
221:             $rateRequest->setDefaultCommodity(Mage::getStoreConfig('carriers/fedex/default_commodity'));
222:         }
223:         
224:         $rateRequest->setIsReturn($request->getIsReturn());
225: 
226:         $rateRequest->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax());
227:                                    
228:         $this->_rateRequest = $rateRequest;
229:         
230:         return $this;
231:     }
232:     
233:     
234:     
235:     /**
236:      * _getQuotes
237:      * 
238:      * @return object
239:      */
240:     protected function _getQuotes()
241:     {
242:         return $this->getWsdlQuotes();
243:     }
244:     
245:     
246:     
247:     /**
248:      * getWsdlQuotes
249:      * 
250:      * @return object
251:      */
252:     public function getWsdlQuotes()
253:     {
254:         $rateRequest        = $this->_rateRequest;
255:         $rateServiceClient  = $this->_rateServiceClient;
256:         $rateServiceVersion = $this->_rateServiceVersion;
257:         
258:         $rateResult = Mage::getModel('shipping/rate_result');
259:         
260:         $request['WebAuthenticationDetail'] = array(
261:             'UserCredential' => array(
262:                 'Key' => $this->getFedexKey(),
263:                 'Password' => $this->getFedexPassword()
264:             )
265:         );
266:         
267:         $request['ClientDetail'] = array(
268:             'AccountNumber' => $this->getFedexAccount(),
269:             'MeterNumber' => $this->getFedexMeter()
270:         );
271:         
272:         $request['TransactionDetail']['CustomerTransactionId'] = '*** Rate Request v' . $rateServiceVersion . ' Using PHP ***';
273:         
274:         $request['Version'] = array(
275:             'ServiceId' => 'crs',
276:             'Major' => $rateServiceVersion,
277:             'Intermediate' => '0',
278:             'Minor' => '0'
279:         );
280:         
281:         $request['ReturnTransitAndCommit'] = true;
282:         
283:         $request['RequestedShipment']['DropoffType']      = $rateRequest->getDropoff();
284:         $request['RequestedShipment']['ShipTimestamp']    = $rateRequest->getShipTimestamp();
285:         $request['RequestedShipment']['RateRequestTypes'] = $rateRequest->getRateType();
286:         $request['RequestedShipment']['PreferredCurrency'] = 'USD';
287:         
288:         if ($rateRequest->getSaturdayDelivery()) {
289:             $request['RequestedShipment']['SpecialServicesRequested']['SpecialServiceTypes'] = array(
290:                 'SATURDAY_DELIVERY'
291:             );
292:         }
293:         
294:         if ($rateRequest->getEnableSmartPost()) {
295:             $request['RequestedShipment']['SmartPostDetail'] = array(
296:                 'Indicia' => $rateRequest->getSmartPostIndiciaType(),
297:                 'AncillaryEndorsement' => $rateRequest->getSmartPostAncillaryEndorsement(),
298:                 'HubId' => $rateRequest->getSmartPostHubId(),
299:                 'SpecialServices' => $rateRequest->getSmartPostSpecialServices()
300:             );
301:             
302:             if ($rateRequest->getSmartPostCustomerManifestId()) {
303:                 $request['RequestedShipment']['SmartPostDetail']['CustomerManifestId'] = $rateRequest->getSmartPostCustomerManifestId();
304:             }
305:         }
306:         
307:         if ($rateRequest->getThirdParty()) {
308:             $request['RequestedShipment']['ShippingChargesPayment'] = array(
309:                 'PaymentType' => 'THIRD_PARTY',
310:                 'Payor' => array(
311:                     'ResponsibleParty' => array(
312:                         'AccountNumber' => $rateRequest->getThirdPartyFedexAccount(),
313:                         'CountryCode' => $rateRequest->getThirdPartyFedexAccountCountry()
314:                     )
315:                 )
316:             );
317:         } else {
318:             $request['RequestedShipment']['ShippingChargesPayment'] = array(
319:                 'PaymentType' => 'SENDER',
320:                 'Payor' => array(
321:                     'ResponsibleParty' => array(
322:                         'AccountNumber' => $this->getFedexAccount(),
323:                         'CountryCode' => $this->getFedexAccountCountry()
324:                     )
325:                 )
326:             );
327:         }
328:         
329:         $request['RequestedShipment']['Shipper']['Address']['StreetLines'] = $rateRequest->getOrigStreet();
330:         $request['RequestedShipment']['Shipper']['Address']['City'] = $rateRequest->getOrigCity();
331:         $request['RequestedShipment']['Shipper']['Address']['PostalCode'] = $rateRequest->getOrigPostcode();
332:         $request['RequestedShipment']['Shipper']['Address']['CountryCode'] = $rateRequest->getOrigCountry();
333:             
334:         if ($rateRequest->getOrigRegionCode()) {
335:             $request['RequestedShipment']['Shipper']['Address']['StateOrProvinceCode'] = $rateRequest->getOrigRegionCode();
336:         }
337:         
338:         if ($rateRequest->getDestStreet()) {
339:             $request['RequestedShipment']['Recipient']['Address']['StreetLines'] = $rateRequest->getDestStreet();
340:         }
341:         
342:         if ($rateRequest->getDestCity()) {
343:             $request['RequestedShipment']['Recipient']['Address']['City'] = $rateRequest->getDestCity();
344:         }
345:         
346:         if ($rateRequest->getDestPostcode()) {
347:             $request['RequestedShipment']['Recipient']['Address']['PostalCode'] = $rateRequest->getDestPostcode();
348:         }
349:         
350:         if ($rateRequest->getDestRegionCode()) {
351:             $request['RequestedShipment']['Recipient']['Address']['StateOrProvinceCode'] = $rateRequest->getDestRegionCode();
352:         }
353:         
354:         if ($rateRequest->getDestCountry() == 'US') {
355:             $request['RequestedShipment']['Recipient']['Address']['Residential'] = $rateRequest->getResidential();
356:         }
357:         
358:         $request['RequestedShipment']['Recipient']['Address']['CountryCode'] = $rateRequest->getDestCountry();
359:       
360:         if ($rateRequest->getOrigCountry() != $rateRequest->getDestCountry()) {
361:             $request['RequestedShipment']['CustomsClearanceDetail'] = array(
362:                 'DutiesPayment' => array(
363:                     'PaymentType' => 'SENDER',
364:                     'Payor' => array(
365:                         'ResponsibleParty' => array(
366:                             'AccountNumber' => $this->getFedexAccount(),
367:                             'CountryCode' => $rateRequest->getOrigCountry()
368:                         )
369:                     )
370:                 ),
371:                 'DocumentContent' => 'NON_DOCUMENTS',
372:                 'CustomsValue' => array(
373:                     'Currency' => $this->getCurrencyCode(),
374:                     'Amount' => sprintf('%01.2f', $rateRequest->getValue())
375:                 ),
376:                 /*'CommercialInvoice' => array(
377:                     'TermsOfSale' => 'FOB_OR_FCA' // 'CFR_OR_CPT', etc...
378:                 )*/
379:             );
380:         }
381:         if ($rateRequest->getItems()) {
382:             if ($rateRequest->getPackaging()) {
383:                 $packages = $rateRequest->getPackaging();
384:             } else {
385:                 $packages = $rateRequest->getShippingPackage()->estimatePackages($rateRequest->getItems(), $rateRequest->getDefaultPackages());
386:             }
387:             
388:             if ($rateRequest->getShippingPackage()->getPackageError()) {
389:                 $this->_rateResultError = $rateRequest->getShippingPackage()->getPackageError();
390:                 return $rateResult;
391:             }
392:             
393:             if (isset($packages) && is_array($packages)) {
394:                 
395:                 $i = 0;
396:                 
397:                 foreach ($packages as $package) {                   
398:                     
399:                     $weightCoef = 1.0;
400:                     
401:                     $weightUnit = Mage::getModel('shipsync/shipping_carrier_fedex')->getWeightUnits();
402: 
403:                     if ($weightUnit == 'G') {
404:                         $package['weight'] = $package['weight'] * 0.001; 
405:                         $weightUnit = 'KG';
406:                         $weightCoef = 0.001;
407:                     }              
408:                     
409:                     $weight = (isset($package['weight']) && round($package['weight'], 1) > 0) ? round($package['weight'], 1) : 0.1;
410:                     $length = (isset($package['length']) && round($package['length']) > 0)    ? round($package['length'])    : 1;
411:                     $width  = (isset($package['width'])  && round($package['width']) > 0)     ? round($package['width'])     : 1;
412:                     $height = (isset($package['height']) && round($package['height']) > 0)    ? round($package['height'])    : 1;
413: 
414:                     if ($this->getEnableSmartPost() && ($weight < 1)) { $weight = 1; }
415: 
416:                     $request['RequestedShipment']['RequestedPackageLineItems'][$i]['SequenceNumber'] = $i + 1;
417:                     $request['RequestedShipment']['RequestedPackageLineItems'][$i]['GroupPackageCount'] = $i + 1;                    
418:                     $request['RequestedShipment']['RequestedPackageLineItems'][$i]['Weight'] = array(
419:                         'Value' => $weight,
420:                         'Units' => $weightUnit
421:                     );
422:                     
423:                     if ($rateRequest->getOrigCountry() != $rateRequest->getDestCountry()) {
424:                         
425:                         $itemsById = $this->getItemsById($package['items']);
426:                         
427:                         foreach ($itemsById as $qty => $item) {
428:                             
429:                             $itemValue = isset($package['package_value']) && $package['package_value'] < $item['value'] ? $package['package_value'] : $item['value'];
430: 
431:                             if ($rateRequest->getDefaultCommodity() != "") {
432:                                 $itemName = $rateRequest->getDefaultCommodity();
433:                             } else {                                
434:                                 $itemName = preg_replace('/[^\w\d_ -]/si', '', $item['name']);
435:                             }
436:                             
437:                             $itemWeight = (isset($item['weight']) 
438:                                            && round($item['weight'] * $weightCoef, 1) > 0) 
439:                                             ? round($item['weight'] * $weightCoef, 1) : 0.1;
440:                             
441:                             $commodities[] = array(
442:                                 'NumberOfPieces' => 1,
443:                                 'Description' => $itemName,
444:                                 'CountryOfManufacture' => $rateRequest->getOrigCountry(),
445:                                 'Weight' => array(
446:                                     'Units' => $weightUnit,
447:                                     'Value' => $itemWeight
448:                                 ),
449:                                 'Quantity' => $item['qty_to_ship'],
450:                                 'QuantityUnits' => 'EA',
451:                                 'UnitPrice' => array(
452:                                     'Currency' => $this->getCurrencyCode(),
453:                                     'Amount' => sprintf('%01.2f', $item['value'])
454:                                 ),
455:                                 'CustomsValue' => array(
456:                                     'Currency' => $this->getCurrencyCode(),
457:                                     'Amount' => sprintf('%01.2f', $item['value'] * $item['qty_to_ship'])
458:                                 )
459:                             );
460:                         }
461:                     }
462:                     
463:                     if (isset($commodities)) {
464:                         $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] = $commodities;
465:                     }
466:                     
467:                     if ($rateRequest->getInsureShipment()) {
468:                         
469:                         $request['RequestedShipment']['RequestedPackageLineItems'][$i]['InsuredValue']['Amount']   = $rateRequest->getInsureAmount();
470:                         $request['RequestedShipment']['RequestedPackageLineItems'][$i]['InsuredValue']['Currency'] = $this->getCurrencyCode();
471:                         
472:                     } else if (Mage::getStoreConfig('carriers/fedex/rating_insured_value')) {
473:                         
474:                         if (isset($package['package_value'])) {
475:                             $package_value = $package['package_value'];
476:                         } else {
477:                             $package_value = 0.0;
478:                             foreach ($package['items'] as $item) {
479:                                 $package_value += $item['value'];
480:                             }
481:                         }
482:                         $request['RequestedShipment']['RequestedPackageLineItems'][$i]['InsuredValue']['Amount']   = round($package_value, 1);
483:                         $request['RequestedShipment']['RequestedPackageLineItems'][$i]['InsuredValue']['Currency'] = $this->getCurrencyCode();
484:                     }
485:                     
486:                     if (Mage::getStoreConfig('carriers/fedex/enable_dimensions')) {
487:                         $request['RequestedShipment']['RequestedPackageLineItems'][$i]['Dimensions'] = array(
488:                             'Length' => $length,
489:                             'Width' => $width,
490:                             'Height' => $height,
491:                             'Units' => $this->getDimensionUnits()
492:                         );
493:                     }
494:                     
495:                     $i++;
496:                 }
497:                 
498:                 $request['RequestedShipment']['PackageCount']  = $i;
499:                 $request['RequestedShipment']['PackageDetail'] = 'INDIVIDUAL_PACKAGES';
500:                 
501:             } else {
502:                 $this->_rateResultError = 'Unable to estimate packages';
503:                 return $rateResult;
504:             }
505:         } else {
506:             $this->_rateResultError = 'No items found to ship';
507:             return $rateResult;
508:         }
509:         
510:         $this->setPackageCount($request['RequestedShipment']['PackageCount']);
511:         
512:         try {
513:             Mage::Helper('shipsync')->mageLog($request, 'rate');            
514:             $response = $rateServiceClient->getRates($request);            
515:             Mage::Helper('shipsync')->mageLog($response, 'rate');
516:         }
517:         catch (SoapFault $ex) {
518:             $this->_rateResultError = $ex->getMessage();
519:             return $rateResult;
520:         }
521:         
522:         $this->_rateResult = $rateResult;
523:         
524:         return $this->_parseWsdlResponse($response);
525:     }
526:     
527:     public function getItemsById($items)
528:     {
529:         $itemsById = array();
530:         
531:         foreach ($items as $item) {
532:             $id                  = $item['product_id'];
533:             $count               = isset($itemsById[$id]['qty_to_ship']) ? $itemsById[$id]['qty_to_ship'] : 0;
534:             $item['qty_to_ship'] = 1 + $count;
535:             $itemsById[$id]      = $item;
536:         }
537:         
538:         return $itemsById;
539:     }
540:     
541:     
542:     /**
543:      * _parseWsdlResponse
544:      * 
545:      * @param object $response
546:      * @return object
547:      */
548:     protected function _parseWsdlResponse($response)
549:     {
550:         $rateRequest = $this->_rateRequest;
551:         $rateResult  = $this->_rateResult;
552:         
553:         if ($response->HighestSeverity == 'ERROR' || $response->HighestSeverity == 'FAILURE' || ($response->HighestSeverity == 'WARNING' && !isset($response->RateReplyDetails))) {
554:             $errorMsg = '';
555:             
556:             if (is_array($response->Notifications)) {
557:                 foreach ($response->Notifications as $notification) {
558:                     $errorMsg .= $notification->Severity . ': ' . $notification->Message . '<br />';
559:                 }
560:             } elseif (($response->Notifications->Message == 'General Error') && Mage::getStoreConfig('carriers/fedex/test_mode')) {
561:                 $errorMsg .= 'ERROR: FedEx Testing servers are temporarily unavailable. Please try again in a few moments.<br />';
562:             } else {
563:                 $errorMsg .= $response->Notifications->Severity . ': ' . $response->Notifications->Message . '<br />';
564:             }
565:             
566:             $this->_rateResultError = $errorMsg;
567:             return $rateResult;
568:         } elseif (!isset($response->RateReplyDetails)) {
569:             $this->_rateResultError = 'Error: Empty rate result.  Please contact your system administrator.';
570:             return $rateResult;
571:         }
572:         
573:         if (!is_array($response->RateReplyDetails)) {
574:             $rateReplyDetails = array(
575:                 $response->RateReplyDetails
576:             );
577:         } else {
578:             $rateReplyDetails = $response->RateReplyDetails;
579:         }
580:         
581:         $rateType = Mage::getStoreConfig('carriers/fedex/rate_type');
582:         
583:         $i = 0;
584:         
585:         foreach ($rateReplyDetails as $rateReply) {
586:             if (isset($rateReply->ServiceType) && in_array($rateReply->ServiceType, $this->getParsedAllowedRatingMethods())) {
587:                 $rateResultMethod = Mage::getModel('shipping/rate_result_method');
588:                 
589:                 $rateResultMethod->setCarrier('fedex');
590:                 $rateResultMethod->setCarrierTitle(Mage::getStoreConfig('carriers/fedex/title'));
591:                 $rateResultMethod->setMethod(str_replace('_', '', $rateReply->ServiceType));
592:                 $rateResultMethod->setMethodTitle($this->getCode('method', $rateReply->ServiceType, true));
593:                 
594:                 if (isset($rateReply->DeliveryDayOfWeek) && $rateReply->DeliveryDayOfWeek == 'SAT') {
595:                     $rateResultMethod->setMethodTitle($rateResultMethod->getMethodTitle() . ' - Saturday Delivery');
596:                 }
597:                 
598:                 if (Mage::getStoreConfig('carriers/fedex/show_timestamp')) {
599:                     if (isset($rateReply->DeliveryTimestamp)) {
600:                         $rateResultMethod->setMethodTitle($rateResultMethod->getMethodTitle() . ' (' . date("m/d g:ia", strtotime($rateReply->DeliveryTimestamp)) . ')');
601:                     } elseif (isset($rateReply->CommitDetails->TransitTime)) {
602:                         if ($rateReply->ServiceType == 'SMART_POST') {
603:                             $transitTimeInt = (isset($transitTimeInt)) ? $transitTimeInt + 1 : 2;
604:                             $transitTime    = $transitTimeInt . '-8 Days';
605:                             $rateResultMethod->setMethodTitle($rateResultMethod->getMethodTitle() . ' (' . $transitTime . ')');
606:                             $rateResultMethod->setTransitTime($transitTimeInt);
607:                         } else {
608:                             $transitTime    = strtolower($rateReply->CommitDetails->TransitTime);
609:                             $tmp            = explode('_', $transitTime);
610:                             $transitTimeInt = Mage::helper('shipsync')->getNumberAsInt($tmp[0]);
611:                             $transitTime    = $transitTimeInt . ' ' . ucwords($tmp[1]);
612:                             $rateResultMethod->setMethodTitle($rateResultMethod->getMethodTitle() . ' (' . $transitTime . ')');
613:                             $rateResultMethod->setTransitTime($transitTimeInt);
614:                         }
615:                     }
616:                 }
617:                 
618:                 if (!Mage::getStoreConfig('carriers/fedex/test_mode') && ($rateType == 'PREFERRED')) {
619:                     if (is_array($rateReply->RatedShipmentDetails)) {
620:                         
621:                         foreach ($rateReply->RatedShipmentDetails as $ratedShipmentDetail) {
622:                             
623:                             if ($ratedShipmentDetail->ShipmentRateDetail->RateType == 'PREFERRED_ACCOUNT_SHIPMENT') {                            
624:                                     $shipmentRateDetail = $ratedShipmentDetail->ShipmentRateDetail;                       
625:                             }
626:                         }
627:                     }
628:                 }
629:                 else {
630:                     if (is_array($rateReply->RatedShipmentDetails))
631:                     {
632:                         foreach ($rateReply->RatedShipmentDetails as $ratedShipmentDetail)
633:                         {
634:                             $_rateType = $ratedShipmentDetail->ShipmentRateDetail->RateType;
635:                 
636:                             if (($_rateType == 'PAYOR_' . $rateType . '_SHIPMENT') || ($_rateType == 'RATED_' . $rateType . '_SHIPMENT') ||
637:                                 ($_rateType == 'PAYOR_' . $rateType . '_PACKAGE')  || ($_rateType == 'RATED_' . $rateType . '_PACKAGE'))
638:                             {
639:                                 $shipmentRateDetail = $ratedShipmentDetail->ShipmentRateDetail; break;
640:                             }
641:                         }
642:                     }
643:                     elseif (isset($rateReply->RatedShipmentDetails->ShipmentRateDetail))
644:                     {
645:                         $shipmentRateDetail = $rateReply->RatedShipmentDetails->ShipmentRateDetail;
646:                     }
647:                 }
648:                     
649:                 if (isset($shipmentRateDetail)) {
650:                     $rate = $shipmentRateDetail->TotalNetCharge->Amount;
651:                 } else {
652:                     $rate = 0;
653:                 }
654:                 
655:                 if (Mage::getStoreConfig('carriers/fedex/subtract_vat') > 0) {
656:                     $rate = $rate / (1 + (Mage::getStoreConfig('carriers/fedex/subtract_vat') / 100));
657:                 }
658:                 
659:                 if (Mage::getStoreConfig('carriers/fedex/handling_fee') > 0) {
660:                     $handling_fee = Mage::getStoreConfig('carriers/fedex/handling_fee');
661:                     
662:                     if (Mage::getStoreConfig('carriers/fedex/handling_action') == 'P') {
663:                         $handling_fee = $handling_fee * $this->getPackageCount();
664:                     }
665:                     
666:                     if (Mage::getStoreConfig('carriers/fedex/handling_type') == 'F') {
667:                         $rate = $rate + $handling_fee;
668:                     } else {
669:                         if (Mage::getStoreConfig('carriers/fedex/handling_shelf') && $rate >= 10) {
670:                             $handling_fee = $handling_fee / log($rate);
671:                             $rate         = $rate + ($rate * ($handling_fee / 100));
672:                         } else {
673:                             $rate = $rate + ($rate * ($handling_fee / 100));
674:                         }
675:                     }
676:                 }
677:                 
678:                 $rateResultMethod->setCost($rate);
679:                 $rateResultMethod->setPrice($rate);
680:                 
681:                 //Test for Continental US destination
682:                 $freeMethodLimit = Mage::getStoreConfig('carriers/fedex/free_shipping_subtotal');
683:                 $destCode        = $this->_rateRequest->getDestRegionCode();
684:                 $destContinental = ($destCode == 'AK' || $destCode == 'HI') ? false : true;
685:                 $continentalTest = (($freeMethodLimit && $destContinental) || !$freeMethodLimit) ? true : false;
686:                 
687:                 if (Mage::getStoreConfig('carriers/fedex/free_shipping_enable') && $continentalTest) {
688:                     if (Mage::getStoreConfig('carriers/fedex/free_shipping_discounts')) {
689:                         $value = $this->_rateRequest->getValue();
690:                     } else {
691:                         $value = $this->_rateRequest->getValueWithDiscount();
692:                     }
693:                     
694:                     $freeMethods = array(
695:                         Mage::getStoreConfig('carriers/fedex/free_method')
696:                     );
697:                     
698:                     $freeMethods = explode(",", $freeMethods[0]);
699:                     
700:                     if (in_array($rateResultMethod->getMethod(), $freeMethods) && $value > Mage::getStoreConfig('carriers/fedex/free_shipping_subtotal')) {
701:                         if (Mage::getStoreConfig('carriers/fedex/free_shipping_enable_all')) {
702:                             $rateResultMethod->setCost($rate);
703:                             $rateResultMethod->setPrice('0');
704:                             $rateResultMethod->setMethodTitle('Free Shipping (' . $rateResultMethod->getMethodTitle() . ')');
705:                             $rateResultMethod->setMethodDescription($rateResultMethod->getMethodTitle());
706:                         } elseif ($rateRequest->getFreeMethodWeight() == 0) {
707:                             $rateResultMethod->setCost($rate);
708:                             $rateResultMethod->setPrice('0');
709:                             $rateResultMethod->setMethodTitle('Free Shipping (' . $rateResultMethod->getMethodTitle() . ')');
710:                             $rateResultMethod->setMethodDescription($rateResultMethod->getMethodTitle());
711:                         } elseif ($rateRequest->getFreeMethodWeight() != $rateRequest->getPackageWeight()) {
712:                             $discountPercent = $rateRequest->getFreeMethodWeight() / $rateRequest->getPackageWeight();
713:                             $rateResultMethod->setCost($rate);
714:                             $rateResultMethod->setPrice($rate * $discountPercent);
715:                             $rateResultMethod->setMethodTitle('Discounted (' . $rateResultMethod->getMethodTitle() . ')');
716:                             $rateResultMethod->setMethodDescription($rateResultMethod->getMethodTitle());
717:                         }
718:                     }
719:                 } else {
720:                     $rateResultMethod->setCost($rate);
721:                     $rateResultMethod->setPrice($rate);
722:                     $rateResultMethod->setMethodTitle($rateResultMethod->getMethodTitle());
723:                     $rateResultMethod->setMethodDescription($rateResultMethod->getMethodTitle());
724:                 }
725:                 $rateResult->append($rateResultMethod);
726:             }
727:         }
728:         if (!isset($rateResult)) {
729:             $this->_rateResultError('No applicable rates available');
730:             return $rateResult;
731:         }
732:         
733:         return $rateResult;
734:     }
735: }
736: 
shipsync-community API documentation generated by ApiGen 2.8.0