Exemple #1
0
 /**
  * Prepare and do request to shipment
  *
  * @param Shipment $orderShipment
  * @return \Magento\Framework\Object
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function requestToShipment(Shipment $orderShipment)
 {
     $admin = $this->_authSession->getUser();
     $order = $orderShipment->getOrder();
     $address = $order->getShippingAddress();
     $shippingMethod = $order->getShippingMethod(true);
     $shipmentStoreId = $orderShipment->getStoreId();
     $shipmentCarrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
     $baseCurrencyCode = $this->_storeManager->getStore($shipmentStoreId)->getBaseCurrencyCode();
     if (!$shipmentCarrier) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Invalid carrier: %1', $shippingMethod->getCarrierCode()));
     }
     $shipperRegionCode = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_REGION_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId);
     if (is_numeric($shipperRegionCode)) {
         $shipperRegionCode = $this->_regionFactory->create()->load($shipperRegionCode)->getCode();
     }
     $recipientRegionCode = $this->_regionFactory->create()->load($address->getRegionId())->getCode();
     $originStreet1 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS1, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId);
     $originStreet2 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS2, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId);
     $storeInfo = new \Magento\Framework\Object((array) $this->_scopeConfig->getValue('general/store_information', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId));
     if (!$admin->getFirstname() || !$admin->getLastname() || !$storeInfo->getName() || !$storeInfo->getPhone() || !$originStreet1 || !$shipperRegionCode || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)) {
         throw new \Magento\Framework\Exception\LocalizedException(__('We don\'t have enough information to create shipping labels. Please make sure your store information and settings are complete.'));
     }
     /** @var $request \Magento\Shipping\Model\Shipment\Request */
     $request = $this->_shipmentRequestFactory->create();
     $request->setOrderShipment($orderShipment);
     $request->setShipperContactPersonName($admin->getName());
     $request->setShipperContactPersonFirstName($admin->getFirstname());
     $request->setShipperContactPersonLastName($admin->getLastname());
     $request->setShipperContactCompanyName($storeInfo->getName());
     $request->setShipperContactPhoneNumber($storeInfo->getPhone());
     $request->setShipperEmail($admin->getEmail());
     $request->setShipperAddressStreet(trim($originStreet1 . ' ' . $originStreet2));
     $request->setShipperAddressStreet1($originStreet1);
     $request->setShipperAddressStreet2($originStreet2);
     $request->setShipperAddressCity($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId));
     $request->setShipperAddressStateOrProvinceCode($shipperRegionCode);
     $request->setShipperAddressPostalCode($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId));
     $request->setShipperAddressCountryCode($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId));
     $request->setRecipientContactPersonName(trim($address->getFirstname() . ' ' . $address->getLastname()));
     $request->setRecipientContactPersonFirstName($address->getFirstname());
     $request->setRecipientContactPersonLastName($address->getLastname());
     $request->setRecipientContactCompanyName($address->getCompany());
     $request->setRecipientContactPhoneNumber($address->getTelephone());
     $request->setRecipientEmail($address->getEmail());
     $request->setRecipientAddressStreet(trim($address->getStreetLine(1) . ' ' . $address->getStreetLine(2)));
     $request->setRecipientAddressStreet1($address->getStreetLine(1));
     $request->setRecipientAddressStreet2($address->getStreetLine(2));
     $request->setRecipientAddressCity($address->getCity());
     $request->setRecipientAddressStateOrProvinceCode($address->getRegionCode());
     $request->setRecipientAddressRegionCode($recipientRegionCode);
     $request->setRecipientAddressPostalCode($address->getPostcode());
     $request->setRecipientAddressCountryCode($address->getCountryId());
     $request->setShippingMethod($shippingMethod->getMethod());
     $request->setPackageWeight($order->getWeight());
     $request->setPackages($orderShipment->getPackages());
     $request->setBaseCurrencyCode($baseCurrencyCode);
     $request->setStoreId($shipmentStoreId);
     return $shipmentCarrier->requestToShipment($request);
 }
Exemple #2
0
 /**
  * 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\Object $request
  * @return string
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _formIntlShipmentRequest(\Magento\Framework\Object $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\Object();
         $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\Object();
         $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;
 }
Exemple #3
0
 /**
  * Form array with appropriate structure for shipment request
  *
  * @param \Magento\Framework\Object $request
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _formShipmentRequest(\Magento\Framework\Object $request)
 {
     if ($request->getReferenceData()) {
         $referenceData = $request->getReferenceData() . $request->getPackageId();
     } else {
         $referenceData = 'Order #' . $request->getOrderShipment()->getOrder()->getIncrementId() . ' P' . $request->getPackageId();
     }
     $packageParams = $request->getPackageParams();
     $customsValue = $packageParams->getCustomsValue();
     $height = $packageParams->getHeight();
     $width = $packageParams->getWidth();
     $length = $packageParams->getLength();
     $weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LB' : 'KG';
     $dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
     $unitPrice = 0;
     $itemsQty = 0;
     $itemsDesc = [];
     $countriesOfManufacture = [];
     $productIds = [];
     $packageItems = $request->getPackageItems();
     foreach ($packageItems as $itemShipment) {
         $item = new \Magento\Framework\Object();
         $item->setData($itemShipment);
         $unitPrice += $item->getPrice();
         $itemsQty += $item->getQty();
         $itemsDesc[] = $item->getName();
         $productIds[] = $item->getProductId();
     }
     // get countries of manufacture
     $productCollection = $this->_productCollectionFactory->create()->addStoreFilter($request->getStoreId())->addFieldToFilter('entity_id', ['in' => $productIds])->addAttributeToSelect('country_of_manufacture');
     foreach ($productCollection as $product) {
         $countriesOfManufacture[] = $product->getCountryOfManufacture();
     }
     $paymentType = $request->getIsReturn() ? 'RECIPIENT' : 'SENDER';
     $requestClient = ['RequestedShipment' => ['ShipTimestamp' => time(), 'DropoffType' => $this->getConfigData('dropoff'), 'PackagingType' => $request->getPackagingType(), 'ServiceType' => $request->getShippingMethod(), 'Shipper' => ['Contact' => ['PersonName' => $request->getShipperContactPersonName(), 'CompanyName' => $request->getShipperContactCompanyName(), 'PhoneNumber' => $request->getShipperContactPhoneNumber()], 'Address' => ['StreetLines' => [$request->getShipperAddressStreet()], 'City' => $request->getShipperAddressCity(), 'StateOrProvinceCode' => $request->getShipperAddressStateOrProvinceCode(), 'PostalCode' => $request->getShipperAddressPostalCode(), 'CountryCode' => $request->getShipperAddressCountryCode()]], 'Recipient' => ['Contact' => ['PersonName' => $request->getRecipientContactPersonName(), 'CompanyName' => $request->getRecipientContactCompanyName(), 'PhoneNumber' => $request->getRecipientContactPhoneNumber()], 'Address' => ['StreetLines' => [$request->getRecipientAddressStreet()], 'City' => $request->getRecipientAddressCity(), 'StateOrProvinceCode' => $request->getRecipientAddressStateOrProvinceCode(), 'PostalCode' => $request->getRecipientAddressPostalCode(), 'CountryCode' => $request->getRecipientAddressCountryCode(), 'Residential' => (bool) $this->getConfigData('residence_delivery')]], 'ShippingChargesPayment' => ['PaymentType' => $paymentType, 'Payor' => ['AccountNumber' => $this->getConfigData('account'), 'CountryCode' => $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())]], 'LabelSpecification' => ['LabelFormatType' => 'COMMON2D', 'ImageType' => 'PNG', 'LabelStockType' => 'PAPER_8.5X11_TOP_HALF_LABEL'], 'RateRequestTypes' => ['ACCOUNT'], 'PackageCount' => 1, 'RequestedPackageLineItems' => ['SequenceNumber' => '1', 'Weight' => ['Units' => $weightUnits, 'Value' => $request->getPackageWeight()], 'CustomerReferences' => ['CustomerReferenceType' => 'CUSTOMER_REFERENCE', 'Value' => $referenceData], 'SpecialServicesRequested' => ['SpecialServiceTypes' => 'SIGNATURE_OPTION', 'SignatureOptionDetail' => ['OptionType' => $packageParams->getDeliveryConfirmation()]]]]];
     // for international shipping
     if ($request->getShipperAddressCountryCode() != $request->getRecipientAddressCountryCode()) {
         $requestClient['RequestedShipment']['CustomsClearanceDetail'] = ['CustomsValue' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $customsValue], 'DutiesPayment' => ['PaymentType' => $paymentType, 'Payor' => ['AccountNumber' => $this->getConfigData('account'), 'CountryCode' => $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())]], 'Commodities' => ['Weight' => ['Units' => $weightUnits, 'Value' => $request->getPackageWeight()], 'NumberOfPieces' => 1, 'CountryOfManufacture' => implode(',', array_unique($countriesOfManufacture)), 'Description' => implode(', ', $itemsDesc), 'Quantity' => ceil($itemsQty), 'QuantityUnits' => 'pcs', 'UnitPrice' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $unitPrice], 'CustomsValue' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $customsValue]]];
     }
     if ($request->getMasterTrackingId()) {
         $requestClient['RequestedShipment']['MasterTrackingId'] = $request->getMasterTrackingId();
     }
     // set dimensions
     if ($length || $width || $height) {
         $requestClient['RequestedShipment']['RequestedPackageLineItems']['Dimensions'] = [];
         $dimenssions =& $requestClient['RequestedShipment']['RequestedPackageLineItems']['Dimensions'];
         $dimenssions['Length'] = $length;
         $dimenssions['Width'] = $width;
         $dimenssions['Height'] = $height;
         $dimenssions['Units'] = $dimensionsUnits;
     }
     return $this->_getAuthDetails() + $requestClient;
 }
Exemple #4
0
 /**
  * Draw packages block
  *
  * @param  \Zend_Pdf_Page $page
  * @return \Magento\Shipping\Model\Order\Pdf\Packaging
  */
 protected function _drawPackageBlock(\Zend_Pdf_Page $page)
 {
     if ($this->getPackageShippingBlock()) {
         $packaging = $this->getPackageShippingBlock();
     } else {
         $packaging = $this->_layout->getBlockSingleton('Magento\\Shipping\\Block\\Adminhtml\\Order\\Packaging');
     }
     $packages = $packaging->getPackages();
     $packageNum = 1;
     foreach ($packages as $package) {
         $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
         $page->drawRectangle(25, $this->y + 15, 190, $this->y - 35);
         $page->drawRectangle(190, $this->y + 15, 350, $this->y - 35);
         $page->drawRectangle(350, $this->y + 15, 570, $this->y - 35);
         $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
         $page->drawRectangle(520, $this->y + 15, 570, $this->y - 5);
         $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
         $packageText = __('Package') . ' ' . $packageNum;
         $page->drawText($packageText, 525, $this->y, 'UTF-8');
         $packageNum++;
         $package = new \Magento\Framework\Object($package);
         $params = new \Magento\Framework\Object($package->getParams());
         $dimensionUnits = $this->_carrierHelper->getMeasureDimensionName($params->getDimensionUnits());
         $typeText = __('Type') . ' : ' . $packaging->getContainerTypeByCode($params->getContainer());
         $page->drawText($typeText, 35, $this->y, 'UTF-8');
         if ($params->getLength() != null) {
             $lengthText = $params->getLength() . ' ' . $dimensionUnits;
         } else {
             $lengthText = '--';
         }
         $lengthText = __('Length') . ' : ' . $lengthText;
         $page->drawText($lengthText, 200, $this->y, 'UTF-8');
         if ($params->getDeliveryConfirmation() != null) {
             $confirmationText = __('Signature Confirmation') . ' : ' . $packaging->getDeliveryConfirmationTypeByCode($params->getDeliveryConfirmation());
             $page->drawText($confirmationText, 355, $this->y, 'UTF-8');
         }
         $this->y = $this->y - 10;
         if ($packaging->displayCustomsValue() != null) {
             $customsValueText = __('Customs Value') . ' : ' . $packaging->displayPrice($params->getCustomsValue());
             $page->drawText($customsValueText, 35, $this->y, 'UTF-8');
         }
         if ($params->getWidth() != null) {
             $widthText = $params->getWidth() . ' ' . $dimensionUnits;
         } else {
             $widthText = '--';
         }
         $widthText = __('Width') . ' : ' . $widthText;
         $page->drawText($widthText, 200, $this->y, 'UTF-8');
         if ($params->getContentType() != null) {
             if ($params->getContentType() == 'OTHER') {
                 $contentsValue = $params->getContentTypeOther();
             } else {
                 $contentsValue = $packaging->getContentTypeByCode($params->getContentType());
             }
             $contentsText = __('Contents') . ' : ' . $contentsValue;
             $page->drawText($contentsText, 355, $this->y, 'UTF-8');
         }
         $this->y = $this->y - 10;
         $weightText = __('Total Weight') . ' : ' . $params->getWeight() . ' ' . $this->_carrierHelper->getMeasureWeightName($params->getWeightUnits());
         $page->drawText($weightText, 35, $this->y, 'UTF-8');
         if ($params->getHeight() != null) {
             $heightText = $params->getHeight() . ' ' . $dimensionUnits;
         } else {
             $heightText = '--';
         }
         $heightText = __('Height') . ' : ' . $heightText;
         $page->drawText($heightText, 200, $this->y, 'UTF-8');
         $this->y = $this->y - 10;
         if ($params->getSize()) {
             $sizeText = __('Size') . ' : ' . ucfirst(strtolower($params->getSize()));
             $page->drawText($sizeText, 35, $this->y, 'UTF-8');
         }
         if ($params->getGirth() != null) {
             $dimensionGirthUnits = $this->_carrierHelper->getMeasureDimensionName($params->getGirthDimensionUnits());
             $girthText = __('Girth') . ' : ' . $params->getGirth() . ' ' . $dimensionGirthUnits;
             $page->drawText($girthText, 200, $this->y, 'UTF-8');
         }
         $this->y = $this->y - 5;
         $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
         $page->drawRectangle(25, $this->y, 570, $this->y - 30 - count($package->getItems()) * 12);
         $this->y = $this->y - 10;
         $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
         $page->drawText(__('Items in the Package'), 30, $this->y, 'UTF-8');
         $txtIndent = 5;
         $itemCollsNumber = $packaging->displayCustomsValue() ? 5 : 4;
         $itemCollsX[0] = 30;
         //  coordinate for Product name
         $itemCollsX[1] = 250;
         // coordinate for Product name
         $itemCollsXEnd = 565;
         $itemCollsXStep = round(($itemCollsXEnd - $itemCollsX[1]) / ($itemCollsNumber - 1));
         // calculate coordinates for all other cells (Weight, Customs Value, Qty Ordered, Qty)
         for ($i = 2; $i <= $itemCollsNumber; $i++) {
             $itemCollsX[$i] = $itemCollsX[$i - 1] + $itemCollsXStep;
         }
         $i = 0;
         $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
         $page->drawRectangle($itemCollsX[$i], $this->y - 5, $itemCollsX[++$i], $this->y - 15);
         $page->drawRectangle($itemCollsX[$i], $this->y - 5, $itemCollsX[++$i], $this->y - 15);
         $page->drawRectangle($itemCollsX[$i], $this->y - 5, $itemCollsX[++$i], $this->y - 15);
         $page->drawRectangle($itemCollsX[$i], $this->y - 5, $itemCollsX[++$i], $this->y - 15);
         $page->drawRectangle($itemCollsX[$i], $this->y - 5, $itemCollsXEnd, $this->y - 15);
         $this->y = $this->y - 12;
         $i = 0;
         $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
         $page->drawText(__('Product'), $itemCollsX[$i] + $txtIndent, $this->y, 'UTF-8');
         $page->drawText(__('Weight'), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
         if ($packaging->displayCustomsValue()) {
             $page->drawText(__('Customs Value'), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
         }
         $page->drawText(__('Qty Ordered'), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
         $page->drawText(__('Qty'), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
         $i = 0;
         foreach ($package->getItems() as $itemId => $item) {
             $item = new \Magento\Framework\Object($item);
             $i = 0;
             $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
             $page->drawRectangle($itemCollsX[$i], $this->y - 3, $itemCollsX[++$i], $this->y - 15);
             $page->drawRectangle($itemCollsX[$i], $this->y - 3, $itemCollsX[++$i], $this->y - 15);
             $page->drawRectangle($itemCollsX[$i], $this->y - 3, $itemCollsX[++$i], $this->y - 15);
             $page->drawRectangle($itemCollsX[$i], $this->y - 3, $itemCollsX[++$i], $this->y - 15);
             $page->drawRectangle($itemCollsX[$i], $this->y - 3, $itemCollsXEnd, $this->y - 15);
             $this->y = $this->y - 12;
             $i = 0;
             $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
             $page->drawText($item->getName(), $itemCollsX[$i] + $txtIndent, $this->y, 'UTF-8');
             $page->drawText($item->getWeight(), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
             if ($packaging->displayCustomsValue()) {
                 $page->drawText($packaging->displayPrice($item->getCustomsValue()), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
             }
             $page->drawText($packaging->getQtyOrderedItem($item->getOrderItemId()), $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
             $page->drawText($item->getQty() * 1, $itemCollsX[++$i] + $txtIndent, $this->y, 'UTF-8');
         }
         $this->y = $this->y - 30;
     }
     return $this;
 }
Exemple #5
0
 /**
  * Form XML for shipment request
  *
  * @param \Magento\Framework\Object $request
  * @return string
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _formShipmentRequest(\Magento\Framework\Object $request)
 {
     $packageParams = $request->getPackageParams();
     $height = $packageParams->getHeight();
     $width = $packageParams->getWidth();
     $length = $packageParams->getLength();
     $weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LBS' : 'KGS';
     $dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
     $itemsDesc = [];
     $itemsShipment = $request->getPackageItems();
     foreach ($itemsShipment as $itemShipment) {
         $item = new \Magento\Framework\Object();
         $item->setData($itemShipment);
         $itemsDesc[] = $item->getName();
     }
     $xmlRequest = $this->_xmlElFactory->create(['data' => '<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/>']);
     $requestPart = $xmlRequest->addChild('Request');
     $requestPart->addChild('RequestAction', 'ShipConfirm');
     $requestPart->addChild('RequestOption', 'nonvalidate');
     $shipmentPart = $xmlRequest->addChild('Shipment');
     if ($request->getIsReturn()) {
         $returnPart = $shipmentPart->addChild('ReturnService');
         // UPS Print Return Label
         $returnPart->addChild('Code', '9');
     }
     $shipmentPart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
     //empirical
     $shipperPart = $shipmentPart->addChild('Shipper');
     if ($request->getIsReturn()) {
         $shipperPart->addChild('Name', $request->getRecipientContactCompanyName());
         $shipperPart->addChild('AttentionName', $request->getRecipientContactPersonName());
         $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
         $shipperPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
         $addressPart = $shipperPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet());
         $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
         $addressPart->addChild('City', $request->getRecipientAddressCity());
         $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
         if ($request->getRecipientAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressStateOrProvinceCode());
         }
     } else {
         $shipperPart->addChild('Name', $request->getShipperContactCompanyName());
         $shipperPart->addChild('AttentionName', $request->getShipperContactPersonName());
         $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
         $shipperPart->addChild('PhoneNumber', $request->getShipperContactPhoneNumber());
         $addressPart = $shipperPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet());
         $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $addressPart->addChild('City', $request->getShipperAddressCity());
         $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
     }
     $shipToPart = $shipmentPart->addChild('ShipTo');
     $shipToPart->addChild('AttentionName', $request->getRecipientContactPersonName());
     $shipToPart->addChild('CompanyName', $request->getRecipientContactCompanyName() ? $request->getRecipientContactCompanyName() : 'N/A');
     $shipToPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
     $addressPart = $shipToPart->addChild('Address');
     $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet1());
     $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
     $addressPart->addChild('City', $request->getRecipientAddressCity());
     $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
     $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
     if ($request->getRecipientAddressStateOrProvinceCode()) {
         $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressRegionCode());
     }
     if ($this->getConfigData('dest_type') == 'RES') {
         $addressPart->addChild('ResidentialAddress');
     }
     if ($request->getIsReturn()) {
         $shipFromPart = $shipmentPart->addChild('ShipFrom');
         $shipFromPart->addChild('AttentionName', $request->getShipperContactPersonName());
         $shipFromPart->addChild('CompanyName', $request->getShipperContactCompanyName() ? $request->getShipperContactCompanyName() : $request->getShipperContactPersonName());
         $shipFromAddress = $shipFromPart->addChild('Address');
         $shipFromAddress->addChild('AddressLine1', $request->getShipperAddressStreet1());
         $shipFromAddress->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $shipFromAddress->addChild('City', $request->getShipperAddressCity());
         $shipFromAddress->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $shipFromAddress->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $shipFromAddress->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
         $addressPart = $shipToPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet1());
         $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $addressPart->addChild('City', $request->getShipperAddressCity());
         $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
         if ($this->getConfigData('dest_type') == 'RES') {
             $addressPart->addChild('ResidentialAddress');
         }
     }
     $servicePart = $shipmentPart->addChild('Service');
     $servicePart->addChild('Code', $request->getShippingMethod());
     $packagePart = $shipmentPart->addChild('Package');
     $packagePart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
     //empirical
     $packagePart->addChild('PackagingType')->addChild('Code', $request->getPackagingType());
     $packageWeight = $packagePart->addChild('PackageWeight');
     $packageWeight->addChild('Weight', $request->getPackageWeight());
     $packageWeight->addChild('UnitOfMeasurement')->addChild('Code', $weightUnits);
     // set dimensions
     if ($length || $width || $height) {
         $packageDimensions = $packagePart->addChild('Dimensions');
         $packageDimensions->addChild('UnitOfMeasurement')->addChild('Code', $dimensionsUnits);
         $packageDimensions->addChild('Length', $length);
         $packageDimensions->addChild('Width', $width);
         $packageDimensions->addChild('Height', $height);
     }
     // ups support reference number only for domestic service
     if ($this->_isUSCountry($request->getRecipientAddressCountryCode()) && $this->_isUSCountry($request->getShipperAddressCountryCode())) {
         if ($request->getReferenceData()) {
             $referenceData = $request->getReferenceData() . $request->getPackageId();
         } else {
             $referenceData = 'Order #' . $request->getOrderShipment()->getOrder()->getIncrementId() . ' P' . $request->getPackageId();
         }
         $referencePart = $packagePart->addChild('ReferenceNumber');
         $referencePart->addChild('Code', '02');
         $referencePart->addChild('Value', $referenceData);
     }
     $deliveryConfirmation = $packageParams->getDeliveryConfirmation();
     if ($deliveryConfirmation) {
         /** @var $serviceOptionsNode Element */
         $serviceOptionsNode = null;
         switch ($this->_getDeliveryConfirmationLevel($request->getRecipientAddressCountryCode())) {
             case self::DELIVERY_CONFIRMATION_PACKAGE:
                 $serviceOptionsNode = $packagePart->addChild('PackageServiceOptions');
                 break;
             case self::DELIVERY_CONFIRMATION_SHIPMENT:
                 $serviceOptionsNode = $shipmentPart->addChild('ShipmentServiceOptions');
                 break;
             default:
                 break;
         }
         if (!is_null($serviceOptionsNode)) {
             $serviceOptionsNode->addChild('DeliveryConfirmation')->addChild('DCISType', $packageParams->getDeliveryConfirmation());
         }
     }
     $shipmentPart->addChild('PaymentInformation')->addChild('Prepaid')->addChild('BillShipper')->addChild('AccountNumber', $this->getConfigData('shipper_number'));
     if ($request->getPackagingType() != $this->configHelper->getCode('container', 'ULE') && $request->getShipperAddressCountryCode() == self::USA_COUNTRY_ID && ($request->getRecipientAddressCountryCode() == 'CA' || $request->getRecipientAddressCountryCode() == 'PR')) {
         $invoiceLineTotalPart = $shipmentPart->addChild('InvoiceLineTotal');
         $invoiceLineTotalPart->addChild('CurrencyCode', $request->getBaseCurrencyCode());
         $invoiceLineTotalPart->addChild('MonetaryValue', ceil($packageParams->getCustomsValue()));
     }
     $labelPart = $xmlRequest->addChild('LabelSpecification');
     $labelPart->addChild('LabelPrintMethod')->addChild('Code', 'GIF');
     $labelPart->addChild('LabelImageFormat')->addChild('Code', 'GIF');
     $this->setXMLAccessRequest();
     $xmlRequest = $this->_xmlAccessRequest . $xmlRequest->asXml();
     return $xmlRequest;
 }