/** * Define params and variables * * @return void */ public function _construct() { parent::_construct(); $this->setInch($this->carrierDhl->getCode('unit_of_dimension_cut', 'I')); $this->setCm($this->carrierDhl->getCode('unit_of_dimension_cut', 'C')); $this->setHeight($this->carrierDhl->getCode('dimensions', 'height')); $this->setDepth($this->carrierDhl->getCode('dimensions', 'depth')); $this->setWidth($this->carrierDhl->getCode('dimensions', 'width')); $kgWeight = 70; $this->setDivideOrderWeightNoteKg(__('Select this to allow DHL to optimize shipping charges by splitting the order if it exceeds %1 %2.', $kgWeight, 'kg')); $weight = round($this->carrierHelper->convertMeasureWeight($kgWeight, \Zend_Measure_Weight::KILOGRAM, \Zend_Measure_Weight::POUND), 3); $this->setDivideOrderWeightNoteLbp(__('Select this to allow DHL to optimize shipping charges by splitting the order if it exceeds %1 %2.', $weight, 'pounds')); $this->setTemplate('unitofmeasure.phtml'); }
/** * Define params and variables * * @return void */ public function _construct() { parent::_construct(); $carrierModel = $this->carrierDhl; $this->setInch($this->escapeJsQuote($carrierModel->getCode('unit_of_dimension_cut', 'I'))); $this->setCm($this->escapeJsQuote($carrierModel->getCode('unit_of_dimension_cut', 'C'))); $this->setHeight($this->escapeJsQuote($carrierModel->getCode('dimensions', 'height'))); $this->setDepth($this->escapeJsQuote($carrierModel->getCode('dimensions', 'depth'))); $this->setWidth($this->escapeJsQuote($carrierModel->getCode('dimensions', 'width'))); $kgWeight = 70; $this->setDivideOrderWeightNoteKg($this->escapeJsQuote(__('This allows breaking total order weight into smaller pieces if it exceeds %1 %2 to ensure accurate calculation of shipping charges.', $kgWeight, 'kg'))); $weight = round($this->_carrierHelper->convertMeasureWeight($kgWeight, \Zend_Measure_Weight::KILOGRAM, \Zend_Measure_Weight::POUND), 3); $this->setDivideOrderWeightNoteLbp($this->escapeJsQuote(__('This allows breaking total order weight into smaller pieces if it exceeds %1 %2 to ensure accurate calculation of shipping charges.', $weight, 'pounds'))); $this->setTemplate('unitofmeasure.phtml'); }
/** * Form XML for international shipment request * As integration guide it is important to follow appropriate sequence for tags e.g.: <FromLastName /> must be * after <FromFirstName /> * * @param \Magento\Framework\DataObject $request * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _formIntlShipmentRequest(\Magento\Framework\DataObject $request) { $packageParams = $request->getPackageParams(); $height = $packageParams->getHeight(); $width = $packageParams->getWidth(); $length = $packageParams->getLength(); $girth = $packageParams->getGirth(); $packageWeight = $request->getPackageWeight(); if ($packageParams->getWeightUnits() != \Zend_Measure_Weight::POUND) { $packageWeight = $this->_carrierHelper->convertMeasureWeight($request->getPackageWeight(), $packageParams->getWeightUnits(), \Zend_Measure_Weight::POUND); } if ($packageParams->getDimensionUnits() != \Zend_Measure_Length::INCH) { $length = round($this->_carrierHelper->convertMeasureDimension($packageParams->getLength(), $packageParams->getDimensionUnits(), \Zend_Measure_Length::INCH)); $width = round($this->_carrierHelper->convertMeasureDimension($packageParams->getWidth(), $packageParams->getDimensionUnits(), \Zend_Measure_Length::INCH)); $height = round($this->_carrierHelper->convertMeasureDimension($packageParams->getHeight(), $packageParams->getDimensionUnits(), \Zend_Measure_Length::INCH)); } if ($packageParams->getGirthDimensionUnits() != \Zend_Measure_Length::INCH) { $girth = round($this->_carrierHelper->convertMeasureDimension($packageParams->getGirth(), $packageParams->getGirthDimensionUnits(), \Zend_Measure_Length::INCH)); } $container = $request->getPackagingType(); switch ($container) { case 'VARIABLE': $container = 'VARIABLE'; break; case 'FLAT RATE ENVELOPE': $container = 'FLATRATEENV'; break; case 'FLAT RATE BOX': $container = 'FLATRATEBOX'; break; case 'RECTANGULAR': $container = 'RECTANGULAR'; break; case 'NONRECTANGULAR': $container = 'NONRECTANGULAR'; break; default: $container = 'VARIABLE'; } $shippingMethod = $request->getShippingMethod(); list($fromZip5, $fromZip4) = $this->_parseZip($request->getShipperAddressPostalCode()); // the wrap node needs for remove xml declaration above $xmlWrap = $this->_xmlElFactory->create(['data' => '<?xml version = "1.0" encoding = "UTF-8"?><wrap/>']); $method = ''; $service = $this->getCode('service_to_code', $shippingMethod); if ($service == 'Priority') { $method = 'Priority'; $rootNode = 'PriorityMailIntlRequest'; $xml = $xmlWrap->addChild($rootNode); } else { if ($service == 'First Class') { $method = 'FirstClass'; $rootNode = 'FirstClassMailIntlRequest'; $xml = $xmlWrap->addChild($rootNode); } else { $method = 'Express'; $rootNode = 'ExpressMailIntlRequest'; $xml = $xmlWrap->addChild($rootNode); } } $xml->addAttribute('USERID', $this->getConfigData('userid')); $xml->addAttribute('PASSWORD', $this->getConfigData('password')); $xml->addChild('Option'); $xml->addChild('Revision', self::DEFAULT_REVISION); $xml->addChild('ImageParameters'); $xml->addChild('FromFirstName', $request->getShipperContactPersonFirstName()); $xml->addChild('FromLastName', $request->getShipperContactPersonLastName()); $xml->addChild('FromFirm', $request->getShipperContactCompanyName()); $xml->addChild('FromAddress1', $request->getShipperAddressStreet2()); $xml->addChild('FromAddress2', $request->getShipperAddressStreet1()); $xml->addChild('FromCity', $request->getShipperAddressCity()); $xml->addChild('FromState', $request->getShipperAddressStateOrProvinceCode()); $xml->addChild('FromZip5', $fromZip5); $xml->addChild('FromZip4', $fromZip4); $xml->addChild('FromPhone', $request->getShipperContactPhoneNumber()); if ($method != 'FirstClass') { if ($request->getReferenceData()) { $referenceData = $request->getReferenceData() . ' P' . $request->getPackageId(); } else { $referenceData = $request->getOrderShipment()->getOrder()->getIncrementId() . ' P' . $request->getPackageId(); } $xml->addChild('FromCustomsReference', 'Order #' . $referenceData); } $xml->addChild('ToFirstName', $request->getRecipientContactPersonFirstName()); $xml->addChild('ToLastName', $request->getRecipientContactPersonLastName()); $xml->addChild('ToFirm', $request->getRecipientContactCompanyName()); $xml->addChild('ToAddress1', $request->getRecipientAddressStreet1()); $xml->addChild('ToAddress2', $request->getRecipientAddressStreet2()); $xml->addChild('ToCity', $request->getRecipientAddressCity()); $xml->addChild('ToProvince', $request->getRecipientAddressStateOrProvinceCode()); $xml->addChild('ToCountry', $this->_getCountryName($request->getRecipientAddressCountryCode())); $xml->addChild('ToPostalCode', $request->getRecipientAddressPostalCode()); $xml->addChild('ToPOBoxFlag', 'N'); $xml->addChild('ToPhone', $request->getRecipientContactPhoneNumber()); $xml->addChild('ToFax'); $xml->addChild('ToEmail'); if ($method != 'FirstClass') { $xml->addChild('NonDeliveryOption', 'Return'); } if ($method == 'FirstClass') { if (stripos($shippingMethod, 'Letter') !== false) { $xml->addChild('FirstClassMailType', 'LETTER'); } else { if (stripos($shippingMethod, 'Flat') !== false) { $xml->addChild('FirstClassMailType', 'FLAT'); } else { $xml->addChild('FirstClassMailType', 'PARCEL'); } } } if ($method != 'FirstClass') { $xml->addChild('Container', $container); } $shippingContents = $xml->addChild('ShippingContents'); $packageItems = $request->getPackageItems(); // get countries of manufacture $countriesOfManufacture = []; $productIds = []; foreach ($packageItems as $itemShipment) { $item = new \Magento\Framework\DataObject(); $item->setData($itemShipment); $productIds[] = $item->getProductId(); } $productCollection = $this->_productCollectionFactory->create()->addStoreFilter($request->getStoreId())->addFieldToFilter('entity_id', ['in' => $productIds])->addAttributeToSelect('country_of_manufacture'); foreach ($productCollection as $product) { $countriesOfManufacture[$product->getId()] = $product->getCountryOfManufacture(); } $packagePoundsWeight = $packageOuncesWeight = 0; // for ItemDetail foreach ($packageItems as $itemShipment) { $item = new \Magento\Framework\DataObject(); $item->setData($itemShipment); $itemWeight = $item->getWeight() * $item->getQty(); if ($packageParams->getWeightUnits() != \Zend_Measure_Weight::POUND) { $itemWeight = $this->_carrierHelper->convertMeasureWeight($itemWeight, $packageParams->getWeightUnits(), \Zend_Measure_Weight::POUND); } if (!empty($countriesOfManufacture[$item->getProductId()])) { $countryOfManufacture = $this->_getCountryName($countriesOfManufacture[$item->getProductId()]); } else { $countryOfManufacture = ''; } $itemDetail = $shippingContents->addChild('ItemDetail'); $itemDetail->addChild('Description', $item->getName()); $ceiledQty = ceil($item->getQty()); if ($ceiledQty < 1) { $ceiledQty = 1; } $individualItemWeight = $itemWeight / $ceiledQty; $itemDetail->addChild('Quantity', $ceiledQty); $itemDetail->addChild('Value', $item->getCustomsValue() * $item->getQty()); list($individualPoundsWeight, $individualOuncesWeight) = $this->_convertPoundOunces($individualItemWeight); $itemDetail->addChild('NetPounds', $individualPoundsWeight); $itemDetail->addChild('NetOunces', $individualOuncesWeight); $itemDetail->addChild('HSTariffNumber', 0); $itemDetail->addChild('CountryOfOrigin', $countryOfManufacture); list($itemPoundsWeight, $itemOuncesWeight) = $this->_convertPoundOunces($itemWeight); $packagePoundsWeight += $itemPoundsWeight; $packageOuncesWeight += $itemOuncesWeight; } $additionalPackagePoundsWeight = floor($packageOuncesWeight / self::OUNCES_POUND); $packagePoundsWeight += $additionalPackagePoundsWeight; $packageOuncesWeight -= $additionalPackagePoundsWeight * self::OUNCES_POUND; if ($packagePoundsWeight + $packageOuncesWeight / self::OUNCES_POUND < $packageWeight) { list($packagePoundsWeight, $packageOuncesWeight) = $this->_convertPoundOunces($packageWeight); } $xml->addChild('GrossPounds', $packagePoundsWeight); $xml->addChild('GrossOunces', $packageOuncesWeight); if ($packageParams->getContentType() == 'OTHER' && $packageParams->getContentTypeOther() != null) { $xml->addChild('ContentType', $packageParams->getContentType()); $xml->addChild('ContentTypeOther ', $packageParams->getContentTypeOther()); } else { $xml->addChild('ContentType', $packageParams->getContentType()); } $xml->addChild('Agreement', 'y'); $xml->addChild('ImageType', 'PDF'); $xml->addChild('ImageLayout', 'ALLINONEFILE'); if ($method == 'FirstClass') { $xml->addChild('Container', $container); } // set size if ($packageParams->getSize()) { $xml->addChild('Size', $packageParams->getSize()); } // set dimensions $xml->addChild('Length', $length); $xml->addChild('Width', $width); $xml->addChild('Height', $height); if ($girth) { $xml->addChild('Girth', $girth); } $xml = $xmlWrap->{$rootNode}->asXML(); return $xml; }
/** * Convert item weight to needed weight based on config weight unit dimensions * * @param float $weight * @param bool $maxWeight * @param string|bool $configWeightUnit * @return float */ protected function _getWeight($weight, $maxWeight = false, $configWeightUnit = false) { if ($maxWeight) { $configWeightUnit = \Zend_Measure_Weight::KILOGRAM; } elseif ($configWeightUnit) { $configWeightUnit = $this->getCode('dimensions_variables', $configWeightUnit); } else { $configWeightUnit = $this->getCode('dimensions_variables', (string) $this->getConfigData('unit_of_measure')); } $countryWeightUnit = $this->getCode('dimensions_variables', $this->_getWeightUnit()); if ($configWeightUnit != $countryWeightUnit) { $weight = $this->_carrierHelper->convertMeasureWeight(round($weight, 3), $configWeightUnit, $countryWeightUnit); } return round($weight, 3); }