Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Labels.php
Go to the documentation of this file.
1 <?php
8 
16 
22 {
26  protected $_authSession;
27 
31  protected $_request;
32 
47  public function __construct(
48  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
49  \Magento\Shipping\Model\Config $shippingConfig,
51  \Magento\Shipping\Model\CarrierFactory $carrierFactory,
52  \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
53  \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory,
54  \Magento\Directory\Model\RegionFactory $regionFactory,
55  \Magento\Framework\Math\Division $mathDivision,
56  \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
57  \Magento\Backend\Model\Auth\Session $authSession,
59  ) {
60  $this->_authSession = $authSession;
61  $this->_request = $request;
62  parent::__construct(
63  $scopeConfig,
64  $shippingConfig,
66  $carrierFactory,
67  $rateResultFactory,
68  $shipmentRequestFactory,
69  $regionFactory,
72  );
73  }
74 
84  public function requestToShipment(Shipment $orderShipment)
85  {
86  $admin = $this->_authSession->getUser();
87  $order = $orderShipment->getOrder();
88 
89  $shippingMethod = $order->getShippingMethod(true);
90  $shipmentStoreId = $orderShipment->getStoreId();
91  $shipmentCarrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
92  $baseCurrencyCode = $this->_storeManager->getStore($shipmentStoreId)->getBaseCurrencyCode();
93  if (!$shipmentCarrier) {
94  throw new LocalizedException(
95  __('The "%1" carrier is invalid. Verify and try again.', $shippingMethod->getCarrierCode())
96  );
97  }
98  $shipperRegionCode = $this->_scopeConfig->getValue(
100  ScopeInterface::SCOPE_STORE,
101  $shipmentStoreId
102  );
103  if (is_numeric($shipperRegionCode)) {
104  $shipperRegionCode = $this->_regionFactory->create()->load($shipperRegionCode)->getCode();
105  }
106 
107  $originStreet1 = $this->_scopeConfig->getValue(
109  ScopeInterface::SCOPE_STORE,
110  $shipmentStoreId
111  );
112  $storeInfo = new DataObject(
113  (array)$this->_scopeConfig->getValue(
114  'general/store_information',
115  ScopeInterface::SCOPE_STORE,
116  $shipmentStoreId
117  )
118  );
119 
120  if (!$admin->getFirstName()
121  || !$admin->getLastName()
122  || !$storeInfo->getName()
123  || !$storeInfo->getPhone()
124  || !$originStreet1
125  || !$this->_scopeConfig->getValue(
127  ScopeInterface::SCOPE_STORE,
128  $shipmentStoreId
129  )
130  || !$this->_scopeConfig->getValue(
132  ScopeInterface::SCOPE_STORE,
133  $shipmentStoreId
134  )
135  || !$this->_scopeConfig->getValue(
137  ScopeInterface::SCOPE_STORE,
138  $shipmentStoreId
139  )
140  ) {
141  throw new LocalizedException(
142  __(
143  "Shipping labels can't be created. "
144  . "Verify that the store information and settings are complete and try again."
145  )
146  );
147  }
148 
150  $request = $this->_shipmentRequestFactory->create();
151  $request->setOrderShipment($orderShipment);
152  $address = $order->getShippingAddress();
153 
154  $this->setShipperDetails($request, $admin, $storeInfo, $shipmentStoreId, $shipperRegionCode, $originStreet1);
156 
157  $request->setShippingMethod($shippingMethod->getMethod());
158  $request->setPackageWeight($order->getWeight());
159  $request->setPackages($orderShipment->getPackages());
160  $request->setBaseCurrencyCode($baseCurrencyCode);
161  $request->setStoreId($shipmentStoreId);
162 
163  return $shipmentCarrier->requestToShipment($request);
164  }
165 
176  protected function setShipperDetails(
178  User $storeAdmin,
180  $shipmentStoreId,
181  $regionCode,
182  $originStreet
183  ) {
184  $originStreet2 = $this->_scopeConfig->getValue(
186  ScopeInterface::SCOPE_STORE,
187  $shipmentStoreId
188  );
189 
190  $request->setShipperContactPersonName($storeAdmin->getName());
191  $request->setShipperContactPersonFirstName($storeAdmin->getFirstName());
192  $request->setShipperContactPersonLastName($storeAdmin->getLastName());
193  $request->setShipperContactCompanyName($store->getName());
194  $request->setShipperContactPhoneNumber($store->getPhone());
195  $request->setShipperEmail($storeAdmin->getEmail());
196  $request->setShipperAddressStreet(trim($originStreet . ' ' . $originStreet2));
197  $request->setShipperAddressStreet1($originStreet);
198  $request->setShipperAddressStreet2($originStreet2);
199  $request->setShipperAddressCity(
200  $this->_scopeConfig->getValue(
202  ScopeInterface::SCOPE_STORE,
203  $shipmentStoreId
204  )
205  );
206  $request->setShipperAddressStateOrProvinceCode($regionCode);
207  $request->setShipperAddressPostalCode(
208  $this->_scopeConfig->getValue(
210  ScopeInterface::SCOPE_STORE,
211  $shipmentStoreId
212  )
213  );
214  $request->setShipperAddressCountryCode(
215  $this->_scopeConfig->getValue(
217  ScopeInterface::SCOPE_STORE,
218  $shipmentStoreId
219  )
220  );
221  }
222 
230  {
231  $request->setRecipientContactPersonName(trim($address->getFirstname() . ' ' . $address->getLastname()));
232  $request->setRecipientContactPersonFirstName($address->getFirstname());
233  $request->setRecipientContactPersonLastName($address->getLastname());
234  $request->setRecipientContactCompanyName($address->getCompany());
235  $request->setRecipientContactPhoneNumber($address->getTelephone());
236  $request->setRecipientEmail($address->getEmail());
237  $request->setRecipientAddressStreet(trim($address->getStreetLine(1) . ' ' . $address->getStreetLine(2)));
238  $request->setRecipientAddressStreet1($address->getStreetLine(1));
239  $request->setRecipientAddressStreet2($address->getStreetLine(2));
240  $request->setRecipientAddressCity($address->getCity());
241  $request->setRecipientAddressStateOrProvinceCode($address->getRegionCode() ?: $address->getRegion());
242  $request->setRecipientAddressRegionCode($address->getRegionCode());
243  $request->setRecipientAddressPostalCode($address->getPostcode());
244  $request->setRecipientAddressCountryCode($address->getCountryId());
245  }
246 }
setShipperDetails(Request $request, User $storeAdmin, DataObject $store, $shipmentStoreId, $regionCode, $originStreet)
Definition: Labels.php:176
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
$address
Definition: customer.php:38
getName($separator=' ')
Definition: User.php:541
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Shipping\Model\Config $shippingConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Shipping\Model\CarrierFactory $carrierFactory, \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory, \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Framework\Math\Division $mathDivision, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Backend\Model\Auth\Session $authSession, Request $request)
Definition: Labels.php:47
$shippingMethod
Definition: popup.phtml:12
setRecipientDetails(Request $request, Address $address)
Definition: Labels.php:229