00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Usa_Model_Shipping_Carrier_Dhl
00036 extends Mage_Usa_Model_Shipping_Carrier_Abstract
00037 implements Mage_Shipping_Model_Carrier_Interface
00038 {
00039
00040 protected $_code = 'dhl';
00041
00042 protected $_request = null;
00043
00044 protected $_result = null;
00045
00046 protected $_errors = array();
00047
00048 protected $_dhlRates = array();
00049
00050 protected $_defaultGatewayUrl = 'https://eCommerce.airborne.com/ApiLandingTest.asp';
00051
00052 const SUCCESS_CODE = 203;
00053
00054 const ADDITIONAL_PROTECTION_ASSET = 'AP';
00055 const ADDITIONAL_PROTECTION_NOT_REQUIRED = 'NR';
00056
00057 const ADDITIONAL_PROTECTION_VALUE_CONFIG = 0;
00058 const ADDITIONAL_PROTECTION_VALUE_SUBTOTAL = 1;
00059 const ADDITIONAL_PROTECTION_VALUE_SUBTOTAL_WITH_DISCOUNT = 2;
00060
00061 const ADDITIONAL_PROTECTION_ROUNDING_FLOOR = 0;
00062 const ADDITIONAL_PROTECTION_ROUNDING_CEIL = 1;
00063 const ADDITIONAL_PROTECTION_ROUNDING_ROUND = 2;
00064
00065
00066 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
00067 {
00068 if (!$this->getConfigFlag('active')) {
00069 return false;
00070 }
00071
00072 $this->setRequest($request);
00073
00074 $this->_result = $this->_getQuotes();
00075
00076 $this->_updateFreeMethodQuote($request);
00077
00078 return $this->getResult();
00079 }
00080
00081 public function setRequest(Mage_Shipping_Model_Rate_Request $request)
00082 {
00083 $this->_request = $request;
00084
00085 $r = new Varien_Object();
00086
00087 $r->setStoreId($request->getStoreId());
00088
00089 if ($request->getLimitMethod()) {
00090 $r->setService($request->getLimitMethod());
00091 }
00092
00093 if ($request->getDhlId()) {
00094 $id = $request->getDhlId();
00095 } else {
00096 $id = $this->getConfigData('id');
00097 }
00098 $r->setId($id);
00099
00100 if ($request->getDhlPassword()) {
00101 $password = $request->getDhlPassword();
00102 } else {
00103 $password = $this->getConfigData('password');
00104 }
00105 $r->setPassword($password);
00106
00107 if ($request->getDhlAccount()) {
00108 $accountNbr = $request->getDhlAccount();
00109 } else {
00110 $accountNbr = $this->getConfigData('account');
00111 }
00112 $r->setAccountNbr($accountNbr);
00113
00114 if ($request->getDhlShippingKey()) {
00115 $shippingKey = $request->getDhlShippingKey();
00116 } else {
00117 $shippingKey = $this->getConfigData('shipping_key');
00118 }
00119 $r->setShippingKey($shippingKey);
00120
00121 if ($request->getDhlShippingIntlKey()) {
00122 $shippingKey = $request->getDhlShippingIntlKey();
00123 } else {
00124 $shippingKey = $this->getConfigData('shipping_intlkey');
00125 }
00126 $r->setShippingIntlKey($shippingKey);
00127
00128 if ($request->getDhlShipmentType()) {
00129 $shipmentType = $request->getDhlShipmentType();
00130 } else {
00131 $shipmentType = $this->getConfigData('shipment_type');
00132 }
00133 $r->setShipmentType($shipmentType);
00134
00135 if($request->getDhlDutiable()){
00136 $shipmentDutible = $request->getDhlDutiable();
00137 }else{
00138 $shipmentDutible = $this->getConfigData('dutiable');
00139 }
00140 $r->setDutiable($shipmentDutible);
00141
00142 if($request->getDhlDutyPaymentType()){
00143 $dutypaytype = $request->getDhlDutyPaymentType();
00144 }else{
00145 $dutypaytype = $this->getConfigData('dutypaymenttype');
00146 }
00147 $r->setDutyPaymentType($dutypaytype);
00148
00149 if($request->getDhlContentDesc()){
00150 $contentdesc = $request->getDhlContentDesc();
00151 }else{
00152 $contentdesc = $this->getConfigData('contentdesc');
00153 }
00154 $r->setContentDesc($contentdesc);
00155
00156 if ($request->getDestPostcode()) {
00157 $r->setDestPostal($request->getDestPostcode());
00158 }
00159
00160 if ($request->getOrigCountry()) {
00161 $origCountry = $request->getOrigCountry();
00162 } else {
00163 $origCountry = Mage::getStoreConfig('shipping/origin/country_id', $this->getStore());
00164 }
00165 $r->setOrigCountry($origCountry);
00166
00167
00168
00169
00170 $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
00171 $shippingWeight = round(max(1, $weight),0);
00172
00173 $r->setValue(round($request->getPackageValue(),2));
00174 $r->setValueWithDiscount($request->getPackageValueWithDiscount());
00175 $r->setDestStreet(Mage::helper('core/string')->substr($request->getDestStreet(), 0, 35));
00176 $r->setDestCity($request->getDestCity());
00177
00178 if ($request->getDestCountryId()) {
00179 $destCountry = $request->getDestCountryId();
00180 } else {
00181 $destCountry = self::USA_COUNTRY_ID;
00182 }
00183
00184
00185
00186 if ($destCountry==self::USA_COUNTRY_ID && ($request->getDestPostcode()=='00912' || $request->getDestRegionCode()==self::PUERTORICO_COUNTRY_ID)) {
00187 $destCountry = self::PUERTORICO_COUNTRY_ID;
00188 }
00189
00190 $r->setDestCountryId($destCountry);
00191 $r->setDestState( $request->getDestRegionCode());
00192
00193 $r->setWeight($shippingWeight);
00194 $r->setFreeMethodWeight($request->getFreeMethodWeight());
00195 $this->_rawRequest = $r;
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 return $this;
00230 }
00231
00232 public function getResult()
00233 {
00234 return $this->_result;
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 }
00258
00259 protected function _getQuotes()
00260 {
00261 return $this->_getXmlQuotes();
00262 }
00263
00264 protected function _setFreeMethodRequest($freeMethod)
00265 {
00266 $r = $this->_rawRequest;
00267
00268 $r->setFreeMethodRequest(true);
00269 $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
00270 $freeWeight = round(max(1, $weight),0);
00271 $r->setWeight($freeWeight);
00272 $r->setService($freeMethod);
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 protected function _getShipDate($domestic=true)
00290 {
00291 if ($domestic) {
00292 $days = explode(',', $this->getConfigData('shipment_days'));
00293 } else {
00294 $days = explode(',', $this->getConfigData('intl_shipment_days'));
00295 }
00296
00297 if (!$days) {
00298 return date('Y-m-d');
00299 }
00300
00301 $i=0;
00302 $weekday = date('w');
00303 while(!in_array($weekday, $days) && $i < 10) {
00304 $i++;
00305 $weekday = date('w', strtotime("+$i day"));
00306 }
00307
00308 return date('Y-m-d', strtotime("+$i day"));
00309 }
00310
00311 protected function _getXmlQuotes()
00312 {
00313 $r = $this->_rawRequest;
00314
00315 $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><eCommerce/>');
00316 $xml->addAttribute('action', 'Request');
00317 $xml->addAttribute('version', '1.1');
00318
00319 $requestor = $xml->addChild('Requestor');
00320 $requestor->addChild('ID', $r->getId());
00321 $requestor->addChild('Password', $r->getPassword());
00322
00323 $methods = explode(',', $this->getConfigData('allowed_methods'));
00324 $internationcode = $this->getCode('international_searvice');
00325 $hasShipCode = false;
00326
00327 $shipDate = $this->_getShipDate();
00328
00329 if ($r->hasService() && $r->getFreeMethodRequest()) {
00330 if ($r->getDestCountryId() == self::USA_COUNTRY_ID) {
00331 $shipment = $xml->addChild('Shipment');
00332 $shipKey=$r->getShippingKey();
00333 $r->setShipDate($shipDate);
00334 } else {
00335 $shipment = $xml->addChild('IntlShipment');
00336 $shipKey=$r->getShippingIntlKey();
00337 $r->setShipDate($this->_getShipDate(false));
00338
00339
00340
00341 $shippingDuty = $shipment->addChild('Dutiable');
00342 $shippingDuty->addChild('DutiableFlag',($r->getDutiable()?'Y':'N'));
00343 $shippingDuty->addChild('CustomsValue',$r->getValue());
00344 }
00345 $hasShipCode = true;
00346 $this->_createShipmentXml($shipment,$shipKey);
00347 } else {
00348 foreach ($methods as $method) {
00349 $shipment = false;
00350 if (in_array($method, array_keys($this->getCode('special_express')))) {
00351 $r->setService('E');
00352 $r->setExtendedService($this->getCode('special_express', $method));
00353 } else {
00354 $r->setService($method);
00355 $r->setExtendedService(null);
00356 }
00357 if ($r->getDestCountryId() == self::USA_COUNTRY_ID && $method!=$internationcode) {
00358 $shipment = $xml->addChild('Shipment');
00359 $shipKey=$r->getShippingKey();
00360 $r->setShipDate($shipDate);
00361 }elseif($r->getDestCountryId() != self::USA_COUNTRY_ID && $method==$internationcode){
00362 $shipment = $xml->addChild('IntlShipment');
00363 $shipKey=$r->getShippingIntlKey();
00364 $r->setShipDate($this->_getShipDate(false));
00365
00366
00367
00368
00369 $shippingDuty = $shipment->addChild('Dutiable');
00370 $shippingDuty->addChild('DutiableFlag',($r->getDutiable()?'Y':'N'));
00371 $shippingDuty->addChild('CustomsValue',$r->getValue());
00372 }
00373 if ($shipment!==false) {
00374 $hasShipCode = true;
00375 $this->_createShipmentXml($shipment,$shipKey);
00376 }
00377 }
00378 }
00379
00380 if (!$hasShipCode) {
00381 $this->_errors[] = Mage::helper('usa')->__('There is no available method for selected shipping address.');
00382 return;
00383 }
00384
00385 $request = $xml->asXML();
00386
00387 try {
00388 $url = $this->getConfigData('gateway_url');
00389 if (!$url) {
00390 $url = $this->_defaultGatewayUrl;
00391 }
00392 $ch = curl_init();
00393 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00394 curl_setopt($ch, CURLOPT_URL, $url);
00395 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
00396 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
00397 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
00398 $responseBody = curl_exec($ch);
00399 curl_close ($ch);
00400 } catch (Exception $e) {
00401 $responseBody = '';
00402 }
00403 $res = $this->_parseXmlResponse($responseBody);
00404
00405 return $res;
00406 }
00407
00408 protected function _createShipmentXml($shipment,$shipKey)
00409 {
00410 $r = $this->_rawRequest;
00411
00412 $store = Mage::app()->getStore($r->getStoreId());
00413
00414 $_haz = $this->getConfigFlag('hazardous_materials');
00415
00416 $_subtotal = $r->getValue();
00417 $_subtotalWithDiscount = $r->getValueWithDiscount();
00418
00419 $_width = max(0, (double) $this->getConfigData('default_width'));
00420 $_height = max(0, (double) $this->getConfigData('default_height'));
00421 $_length = max(0, (double) $this->getConfigData('default_length'));
00422
00423 $_apEnabled = $this->getConfigFlag('additional_protection_enabled');
00424 $_apUseSubtotal = $this->getConfigData('additional_protection_use_subtotal');
00425 $_apConfigValue = max(0, (double) $this->getConfigData('additional_protection_value'));
00426 $_apMinValue = max(0, (double) $this->getConfigData('additional_protection_min_value'));
00427 $_apValueRounding = $this->getConfigData('additional_protection_rounding');
00428
00429 $apValue = 0;
00430 $apCode = self::ADDITIONAL_PROTECTION_NOT_REQUIRED;
00431 if ($_apEnabled) {
00432 if ($_apMinValue <= $_subtotal) {
00433 switch ($_apUseSubtotal) {
00434 case self::ADDITIONAL_PROTECTION_VALUE_SUBTOTAL:
00435 $apValue = $_subtotal;
00436 break;
00437 case self::ADDITIONAL_PROTECTION_VALUE_SUBTOTAL_WITH_DISCOUNT:
00438 $apValue = $_subtotalWithDiscount;
00439 break;
00440 default:
00441 case self::ADDITIONAL_PROTECTION_VALUE_CONFIG:
00442 $apValue = $_apConfigValue;
00443 break;
00444 }
00445
00446 if ($apValue) {
00447 $apCode = self::ADDITIONAL_PROTECTION_ASSET;
00448
00449
00450 switch ($_apValueRounding) {
00451 case self::ADDITIONAL_PROTECTION_ROUNDING_CEIL:
00452 $apValue = ceil($apValue);
00453 break;
00454 case self::ADDITIONAL_PROTECTION_ROUNDING_ROUND:
00455 $apValue = round($apValue);
00456 break;
00457 default:
00458 case self::ADDITIONAL_PROTECTION_ROUNDING_FLOOR:
00459 $apValue = floor($apValue);
00460 break;
00461 }
00462 }
00463 }
00464 }
00465
00466 $shipment->addAttribute('action', 'RateEstimate');
00467 $shipment->addAttribute('version', '1.0');
00468
00469 $shippingCredentials = $shipment->addChild('ShippingCredentials');
00470 $shippingCredentials->addChild('ShippingKey',$shipKey);
00471 $shippingCredentials->addChild('AccountNbr', $r->getAccountNbr());
00472
00473 $shipmentDetail = $shipment->addChild('ShipmentDetail');
00474 $shipmentDetail->addChild('ShipDate', $r->getShipDate());
00475 $shipmentDetail->addChild('Service')->addChild('Code', $r->getService());
00476 $shipmentDetail->addChild('ShipmentType')->addChild('Code', $r->getShipmentType());
00477 $shipmentDetail->addChild('Weight', $r->getWeight());
00478 $shipmentDetail->addChild('ContentDesc', $r->getContentDesc());
00479 $additionalProtection = $shipmentDetail->addChild('AdditionalProtection');
00480 $additionalProtection->addChild('Code', $apCode);
00481 $additionalProtection->addChild('Value', floor($apValue));
00482
00483 if ($_width && $_height && $_length) {
00484 $dimensions = $shipmentDetail->addChild('Dimensions');
00485 $dimensions->addChild('Length', $_length);
00486 $dimensions->addChild('Width', $_width);
00487 $dimensions->addChild('Height', $_height);
00488 }
00489
00490 if ($_haz || ($r->getExtendedService())) {
00491 $specialServices = $shipmentDetail->addChild('SpecialServices');
00492 }
00493
00494 if ($_haz) {
00495 $hazardousMaterials = $specialServices->addChild('SpecialService');
00496 $hazardousMaterials->addChild('Code', 'HAZ');
00497 }
00498
00499 if ($r->getExtendedService()) {
00500 $extendedService = $specialServices->addChild('SpecialService');
00501 $extendedService->addChild('Code', $r->getExtendedService());
00502 }
00503
00504
00505
00506
00507
00508
00509
00510 $billing = $shipment->addChild('Billing');
00511 $billing->addChild('Party')->addChild('Code', 'S');
00512 $billing->addChild('DutyPaymentType',$r->getDutyPaymentType());
00513
00514
00515
00516
00517
00518
00519
00520
00521 $receiverAddress = $shipment->addChild('Receiver')->addChild('Address');
00522 $receiverAddress->addChild('Street', htmlspecialchars($r->getDestStreet()?$r->getDestStreet():'NA'));
00523 $receiverAddress->addChild('City', htmlspecialchars($r->getDestCity()));
00524 $receiverAddress->addChild('State', htmlspecialchars($r->getDestState()));
00525
00526
00527
00528 $receiverAddress->addChild('Country', ($r->getDestCountryId()=='GB'?'UK':$r->getDestCountryId()));
00529 $receiverAddress->addChild('PostalCode', $r->getDestPostal());
00530
00531
00532
00533
00534
00535
00536
00537 }
00538
00539 protected function _parseXmlResponse($response)
00540 {
00541 $r = $this->_rawRequest;
00542 $costArr = array();
00543 $priceArr = array();
00544 $errorTitle = 'Unable to retrieve quotes';
00545
00546 $tr = get_html_translation_table(HTML_ENTITIES);
00547 unset($tr['<'], $tr['>'], $tr['"']);
00548 $response = str_replace(array_keys($tr), array_values($tr), $response);
00549
00550 if (strlen(trim($response))>0) {
00551 if (strpos(trim($response), '<?xml')===0) {
00552 $xml = simplexml_load_string($response);
00553 if (is_object($xml)) {
00554 if (
00555 is_object($xml->Faults)
00556 && is_object($xml->Faults->Fault)
00557 && is_object($xml->Faults->Fault->Code)
00558 && is_object($xml->Faults->Fault->Description)
00559 && is_object($xml->Faults->Fault->Context)
00560 ) {
00561 $code = (string)$xml->Faults->Fault->Code;
00562 $description = $xml->Faults->Fault->Description;
00563 $context = $xml->Faults->Fault->Context;
00564 $this->_errors[$code] = Mage::helper('usa')->__('Error #%s : %s (%s)', $code, $description, $context);
00565 } else {
00566 if ($r->getDestCountryId() == self::USA_COUNTRY_ID) {
00567 if ($xml->Shipment) {
00568 foreach($xml->Shipment as $shipXml){
00569 $this->_parseXmlObject($shipXml);
00570 }
00571 } else {
00572 $this->_errors[] = Mage::helper('usa')->__('Shipment is not available.');
00573 }
00574 } else {
00575 $shipXml = $xml->IntlShipment;
00576 $this->_parseXmlObject($shipXml);
00577 }
00578 $shipXml=(($r->getDestCountryId() == self::USA_COUNTRY_ID)?$xml->Shipment:$xml->IntlShipment);
00579 }
00580 }
00581 } else {
00582 $this->_errors[] = Mage::helper('usa')->__('Response is in the wrong format');
00583 }
00584 }
00585
00586 $result = Mage::getModel('shipping/rate_result');
00587
00588 foreach ($this->_errors as $errorText) {
00589 $error = Mage::getModel('shipping/rate_result_error');
00590 $error->setCarrier('dhl');
00591 $error->setCarrierTitle($this->getConfigData('title'));
00592
00593 $error->setErrorMessage($this->getConfigData('specificerrmsg'));
00594 $result->append($error);
00595 }
00596
00597 foreach($this->_dhlRates as $rate) {
00598 $method = $rate['service'];
00599 $data = $rate['data'];
00600 $rate = Mage::getModel('shipping/rate_result_method');
00601 $rate->setCarrier('dhl');
00602 $rate->setCarrierTitle($this->getConfigData('title'));
00603 $rate->setMethod($method);
00604 $rate->setMethodTitle($data['term']);
00605 $rate->setCost($data['price_total']);
00606 $rate->setPrice($data['price_total']);
00607 $result->append($rate);
00608 }
00609 return $result;
00610
00611 }
00612
00613 protected function _parseXmlObject($shipXml)
00614 {
00615 if(
00616 is_object($shipXml->Faults)
00617 && is_object($shipXml->Faults->Fault)
00618 && is_object($shipXml->Faults->Fault->Desc)
00619 && intval($shipXml->Faults->Fault->Code) != self::SUCCESS_CODE
00620 ) {
00621 $code = (string)$shipXml->Faults->Fault->Code;
00622 $description = $shipXml->Faults->Fault->Desc;
00623 $this->_errors[$code] = Mage::helper('usa')->__('Error #%s: %s', $code, $description);
00624 } elseif(
00625 is_object($shipXml->Faults)
00626 && is_object($shipXml->Result->Code)
00627 && is_object($shipXml->Result->Desc)
00628 && intval($shipXml->Result->Code) != self::SUCCESS_CODE
00629 ) {
00630 $code = (string)$shipXml->Result->Code;
00631 $description = $shipXml->Result->Desc;
00632 $this->_errors[$code] = Mage::helper('usa')->__('Error #%s: %s', $code, $description);
00633 }else {
00634 $this->_addRate($shipXml);
00635 }
00636 return $this;
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 public function getCode($type, $code='')
00790 {
00791 static $codes;
00792 $codes = array(
00793 'service'=>array(
00794 'IE' => Mage::helper('usa')->__('International Express'),
00795 'E SAT' => Mage::helper('usa')->__('Express Saturday'),
00796 'E 10:30AM' => Mage::helper('usa')->__('Express 10:30 AM'),
00797 'E' => Mage::helper('usa')->__('Express'),
00798 'N' => Mage::helper('usa')->__('Next Afternoon'),
00799 'S' => Mage::helper('usa')->__('Second Day Service'),
00800 'G' => Mage::helper('usa')->__('Ground'),
00801 ),
00802 'shipment_type'=>array(
00803 'L' => Mage::helper('usa')->__('Letter'),
00804 'P' => Mage::helper('usa')->__('Package'),
00805 ),
00806 'international_searvice'=>'IE',
00807
00808
00809
00810
00811
00812
00813 'dutypayment_type'=>array(
00814 'S' => Mage::helper('usa')->__('Sender'),
00815 'R' => Mage::helper('usa')->__('Receiver'),
00816 '3' => Mage::helper('usa')->__('Third Party'),
00817 ),
00818
00819 'special_express'=>array(
00820 'E SAT'=>'SAT',
00821 'E 10:30AM'=>'1030',
00822 ),
00823
00824 'descr_to_service'=>array(
00825 'E SAT'=>'Saturday',
00826 'E 10:30AM'=>'10:30 A.M',
00827 ),
00828
00829 );
00830
00831
00832
00833 if (!isset($codes[$type])) {
00834
00835 return false;
00836 } elseif (''===$code) {
00837 return $codes[$type];
00838 }
00839
00840 if (!isset($codes[$type][$code])) {
00841
00842 return false;
00843 } else {
00844 return $codes[$type][$code];
00845 }
00846 }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867 protected function _addRate($shipXml)
00868 {
00869 $r = $this->_rawRequest;
00870 $services = $this->getCode('service');
00871 $regexps = $this->getCode('descr_to_service');
00872 $desc=(string)$shipXml->EstimateDetail->ServiceLevelCommitment->Desc;
00873 $totalEstimate=(string)$shipXml->EstimateDetail->RateEstimate->TotalChargeEstimate;
00874
00875
00876
00877
00878 if($desc && $totalEstimate){
00879 $service = (string)$shipXml->EstimateDetail->Service->Code;
00880 $description = (string)$shipXml->EstimateDetail->ServiceLevelCommitment->Desc;
00881 if ($service == 'E') {
00882 foreach ($regexps as $expService=>$exp) {
00883 if (preg_match('/'.preg_quote($exp, '/').'/', $description)) {
00884 $service = $expService;
00885 }
00886 }
00887 }
00888
00889 $data['term'] = (isset($services[$service])?$services[$service]:$desc);
00890 $data['price_total'] = $this->getMethodPrice($totalEstimate, $service);
00891 $this->_dhlRates[] = array('service'=>$service, 'data'=>$data);
00892 }
00893 }
00894
00895 public function getTracking($trackings)
00896 {
00897 $this->setTrackingReqeust();
00898
00899 if (!is_array($trackings)) {
00900 $trackings=array($trackings);
00901 }
00902 $this->_getXMLTracking($trackings);
00903
00904 return $this->_result;
00905 }
00906
00907 protected function setTrackingReqeust()
00908 {
00909 $r = new Varien_Object();
00910
00911 $id = $this->getConfigData('id');
00912 $r->setId($id);
00913
00914 $password = $this->getConfigData('password');
00915 $r->setPassword($password);
00916
00917 $this->_rawTrackRequest = $r;
00918 }
00919
00920 protected function _getXMLTracking($trackings)
00921 {
00922 $r = $this->_rawTrackRequest;
00923
00924 $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><eCommerce/>');
00925 $xml->addAttribute('action', 'Request');
00926 $xml->addAttribute('version', '1.1');
00927
00928 $requestor = $xml->addChild('Requestor');
00929 $requestor->addChild('ID', $r->getId());
00930 $requestor->addChild('Password', $r->getPassword());
00931
00932 $track=$xml->addChild('Track');
00933 $track->addAttribute('action', 'Get');
00934 $track->addAttribute('version', '1.0');
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947 foreach($trackings as $tracking){
00948 $track->addChild('Shipment')->addChild('TrackingNbr',$tracking);
00949 }
00950 $request = $xml->asXML();
00951
00952
00953
00954
00955 try {
00956 $url = $this->getConfigData('gateway_url');
00957 if (!$url) {
00958 $url = $this->_defaultGatewayUrl;
00959 }
00960 $ch = curl_init();
00961 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00962 curl_setopt($ch, CURLOPT_URL, $url);
00963 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
00964 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
00965 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
00966 $responseBody = curl_exec($ch);
00967 curl_close ($ch);
00968 } catch (Exception $e) {
00969 $responseBody = '';
00970 }
00971 #echo "<xmp>".$responseBody."</xmp>";
00972 $this->_parseXmlTrackingResponse($trackings, $responseBody);
00973 }
00974
00975 protected function _parseXmlTrackingResponse($trackings, $response)
00976 {
00977 $errorTitle = 'Unable to retrieve tracking';
00978 $resultArr=array();
00979 $errorArr=array();
00980 $trackingserror=array();
00981 $tracknum='';
00982 if (strlen(trim($response))>0) {
00983 if (strpos(trim($response), '<?xml')===0) {
00984 $xml = simplexml_load_string($response);
00985 if (is_object($xml)) {
00986 $trackxml=$xml->Track;
00987 if (
00988 is_object($xml->Faults)
00989 && is_object($xml->Faults->Fault)
00990 && is_object($xml->Faults->Fault->Code)
00991 && is_object($xml->Faults->Fault->Description)
00992 && is_object($xml->Faults->Fault->Context)
00993 ) {
00994 $code = (string)$xml->Faults->Fault->Code;
00995 $description = $xml->Faults->Fault->Description;
00996 $context = $xml->Faults->Fault->Context;
00997 $errorTitle = Mage::helper('usa')->__('Error #%s : %s (%s)', $code, $description, $context);
00998 }elseif(is_object($trackxml) && is_object($trackxml->Shipment)){
00999 foreach($trackxml->Shipment as $txml){
01000 $rArr=array();
01001
01002 if(is_object($txml)){
01003 $tracknum=(string)$txml->TrackingNbr;
01004 if($txml->Fault){
01005 $code = (string)$txml->Fault->Code;
01006 $description = $txml->Fault->Description;
01007 $errorArr[$tracknum] = Mage::helper('usa')->__('Error #%s: %s', $code, $description);
01008 }elseif($txml->Result){
01009 $code = (int)$txml->Result->Code;
01010 if($code===0){
01011
01012
01013
01014 $rArr['service']=(string)$txml->Service->Desc;
01015 if(isset($txml->Weight))
01016 $rArr['weight']=(string)$txml->Weight." lbs";
01017 if (isset($txml->Delivery)) {
01018 $rArr['deliverydate'] = (string)$txml->Delivery->Date;
01019 $rArr['deliverytime'] = (string)$txml->Delivery->Time.':00';
01020 $rArr['status'] = Mage::helper('usa')->__('Delivered');
01021 if (isset($txml->Delivery->Location->Desc)) {
01022 $rArr['deliverylocation'] = (string)$txml->Delivery->Location->Desc;
01023 }
01024 } elseif (isset($txml->Pickup)) {
01025 $rArr['deliverydate'] = (string)$txml->Pickup->Date;
01026 $rArr['deliverytime'] = (string)$txml->Pickup->Time.':00';
01027 $rArr['status'] = Mage::helper('usa')->__('Shipment picked up');
01028 } else {
01029 $rArr['status']=(string)$txml->ShipmentType->Desc.Mage::helper('usa')->__(' was not delivered nor scanned');
01030 }
01031
01032 $packageProgress = array();
01033 if (isset($txml->TrackingHistory) && isset($txml->TrackingHistory->Status)) {
01034
01035 foreach ($txml->TrackingHistory->Status as $thistory) {
01036 $tempArr=array();
01037 $tempArr['activity'] = (string)$thistory->StatusDesc;
01038 $tempArr['deliverydate'] = (string)$thistory->Date;
01039 $tempArr['deliverytime'] = (string)$thistory->Time.':00';
01040 $addArr=array();
01041 if (isset($thistory->Location->City)) {
01042 $addArr[] = (string)$thistory->Location->City;
01043 }
01044 if (isset($thistory->Location->State)) {
01045 $addArr[] = (string)$thistory->Location->State;
01046 }
01047 if (isset($thistory->Location->CountryCode)) {
01048 $addArr[] = (string)$thistory->Location->Country;
01049 }
01050 if ($addArr) {
01051 $tempArr['deliverylocation']=implode(', ',$addArr);
01052 }elseif(isset($thistory['final_delivery']) && (string)$thistory['final_delivery']==='true'){
01053
01054
01055
01056 $addArr=array();
01057 if (isset($txml->Receiver->City)) {
01058 $addArr[] = (string)$txml->Receiver->City;
01059 }
01060 if (isset($thistory->Receiver->State)) {
01061 $addArr[] = (string)$txml->Receiver->State;
01062 }
01063 if (isset($thistory->Receiver->CountryCode)) {
01064 $addArr[] = (string)$txml->Receiver->Country;
01065 }
01066 $tempArr['deliverylocation']=implode(', ',$addArr);
01067 }
01068 $packageProgress[] = $tempArr;
01069 }
01070 $rArr['progressdetail'] = $packageProgress;
01071
01072 }
01073 $resultArr[$tracknum]=$rArr;
01074 }else{
01075 $description =(string)$txml->Result->Desc;
01076 if($description)
01077 $errorArr[$tracknum]=Mage::helper('usa')->__('Error #%s: %s', $code, $description);
01078 else
01079 $errorArr[$tracknum]=Mage::helper('usa')->__('Unable to retrieve tracking');
01080 }
01081 }else{
01082 $errorArr[$tracknum]=Mage::helper('usa')->__('Unable to retrieve tracking');
01083 }
01084
01085 }
01086 }
01087
01088 }
01089 }
01090 } else {
01091 $errorTitle = Mage::helper('usa')->__('Response is in the wrong format');
01092 }
01093 }
01094
01095 $result = Mage::getModel('shipping/tracking_result');
01096 if($errorArr || $resultArr){
01097 foreach ($errorArr as $t=>$r) {
01098 $error = Mage::getModel('shipping/tracking_result_error');
01099 $error->setCarrier('dhl');
01100 $error->setCarrierTitle($this->getConfigData('title'));
01101 $error->setTracking($t);
01102 $error->setErrorMessage($r);
01103 $result->append($error);
01104 }
01105
01106 foreach($resultArr as $t => $data) {
01107 $tracking = Mage::getModel('shipping/tracking_result_status');
01108 $tracking->setCarrier('dhl');
01109 $tracking->setCarrierTitle($this->getConfigData('title'));
01110 $tracking->setTracking($t);
01111 $tracking->addData($data);
01112
01113
01114
01115
01116
01117
01118 $result->append($tracking);
01119 }
01120 }else{
01121 foreach($trackings as $t){
01122 $error = Mage::getModel('shipping/tracking_result_error');
01123 $error->setCarrier('dhl');
01124 $error->setCarrierTitle($this->getConfigData('title'));
01125 $error->setTracking($t);
01126 $error->setErrorMessage($errorTitle);
01127 $result->append($error);
01128
01129 }
01130 }
01131 $this->_result = $result;
01132
01133
01134 }
01135
01136 public function getResponse()
01137 {
01138 $statuses = '';
01139 if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result){
01140 if ($trackings = $this->_result->getAllTrackings()) {
01141 foreach ($trackings as $tracking){
01142 if($data = $tracking->getAllData()){
01143 if (isset($data['status'])) {
01144 $statuses .= Mage::helper('usa')->__($data['status'])."\n<br/>";
01145 } else {
01146 $statuses .= Mage::helper('usa')->__($data['error_message'])."\n<br/>";
01147 }
01148 }
01149 }
01150 }
01151 }
01152 if (empty($statuses)) {
01153 $statuses = Mage::helper('usa')->__('Empty response');
01154 }
01155 return $statuses;
01156 }
01157
01158
01159
01160
01161
01162
01163 public function getAllowedMethods()
01164 {
01165 $allowed = explode(',', $this->getConfigData('allowed_methods'));
01166 $arr = array();
01167 foreach ($allowed as $k) {
01168 $arr[$k] = $this->getCode('service', $k);
01169 }
01170 return $arr;
01171 }
01172
01173 public function isStateProvinceRequired()
01174 {
01175 return true;
01176 }
01177
01178 public function getAdditionalProtectionValueTypes()
01179 {
01180 return array(
01181 self::ADDITIONAL_PROTECTION_VALUE_CONFIG=>Mage::helper('usa')->__('Configuration'),
01182 self::ADDITIONAL_PROTECTION_VALUE_SUBTOTAL=>Mage::helper('usa')->__('Subtotal'),
01183 self::ADDITIONAL_PROTECTION_VALUE_SUBTOTAL_WITH_DISCOUNT=>Mage::helper('usa')->__('Subtotal With Discount'),
01184 );
01185 }
01186
01187 public function getAdditionalProtectionRoundingTypes()
01188 {
01189 return array(
01190 self::ADDITIONAL_PROTECTION_ROUNDING_FLOOR => Mage::helper('usa')->__('To lower'),
01191 self::ADDITIONAL_PROTECTION_ROUNDING_CEIL => Mage::helper('usa')->__('To upper'),
01192 self::ADDITIONAL_PROTECTION_ROUNDING_ROUND => Mage::helper('usa')->__('Round'),
01193 );
01194 }
01195 }