protected function getTrackingDetails(\Magento\Sales\Model\Order\Shipment $shipment) { $track = $shipment->getTracksCollection()->getLastItem(); $trackingDetails = array(); $number = trim($track->getData('number')); if (!empty($number)) { $carrierCode = trim($track->getData('carrier_code')); if (strtolower($carrierCode) == 'dhlint') { $carrierCode = 'dhl'; } $trackingDetails = array('carrier_title' => trim($track->getData('title')), 'carrier_code' => $carrierCode, 'tracking_number' => (string) $number); } return $trackingDetails; }
/** * 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); }
/** * Save shipment and order in one transaction * * @param \Magento\Sales\Model\Order\Shipment $shipment * @return $this */ protected function _saveShipment($shipment) { $shipment->getOrder()->setIsInProcess(true); $transaction = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction'); $transaction->addObject($shipment)->addObject($shipment->getOrder())->save(); return $this; }
/** * @param int $configValue * @param bool|null $forceSyncMode * @param bool|null $customerNoteNotify * @param bool|null $emailSendingResult * @dataProvider sendDataProvider * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult) { $comment = 'comment_test'; $address = 'address_test'; $configPath = 'sales_email/general/async_sending'; $this->shipmentMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with($configPath)->willReturn($configValue); if (!$configValue || $forceSyncMode) { $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->any())->method('format')->with($addressMock, 'html')->willReturn($address); $this->orderMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock); $this->orderMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock); $this->shipmentMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify); $this->shipmentMock->expects($this->any())->method('getCustomerNote')->willReturn($comment); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'comment' => $customerNoteNotify ? $comment : '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $address, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn($emailSendingResult); if ($emailSendingResult) { $this->senderBuilderFactoryMock->expects($this->once())->method('create')->willReturn($this->senderMock); $this->senderMock->expects($this->once())->method('send'); $this->senderMock->expects($this->once())->method('sendCopyTo'); $this->shipmentMock->expects($this->once())->method('setEmailSent')->with(true); $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, ['send_email', 'email_sent']); $this->assertTrue($this->sender->send($this->shipmentMock)); } else { $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); } } else { $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); } }
/** * Prepare and do request to shipment * * @param Shipment $orderShipment * @return \Magento\Framework\DataObject * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function requestToShipment(Shipment $orderShipment) { $admin = $this->_authSession->getUser(); $order = $orderShipment->getOrder(); $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 LocalizedException(__('Invalid carrier: %1', $shippingMethod->getCarrierCode())); } $shipperRegionCode = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_REGION_ID, ScopeInterface::SCOPE_STORE, $shipmentStoreId); if (is_numeric($shipperRegionCode)) { $shipperRegionCode = $this->_regionFactory->create()->load($shipperRegionCode)->getCode(); } $originStreet1 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS1, ScopeInterface::SCOPE_STORE, $shipmentStoreId); $storeInfo = new DataObject((array) $this->_scopeConfig->getValue('general/store_information', ScopeInterface::SCOPE_STORE, $shipmentStoreId)); if (!$admin->getFirstname() || !$admin->getLastname() || !$storeInfo->getName() || !$storeInfo->getPhone() || !$originStreet1 || !$shipperRegionCode || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, ScopeInterface::SCOPE_STORE, $shipmentStoreId)) { throw new 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); $address = $order->getShippingAddress(); $this->setShipperDetails($request, $admin, $storeInfo, $shipmentStoreId, $shipperRegionCode, $originStreet1); $this->setRecipientDetails($request, $address); $request->setShippingMethod($shippingMethod->getMethod()); $request->setPackageWeight($order->getWeight()); $request->setPackages($orderShipment->getPackages()); $request->setBaseCurrencyCode($baseCurrencyCode); $request->setStoreId($shipmentStoreId); return $shipmentCarrier->requestToShipment($request); }
/** * @param \Magento\Sales\Model\Order\Shipment $shipment * @param RequestInterface $request * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function create(\Magento\Sales\Model\Order\Shipment $shipment, RequestInterface $request) { $order = $shipment->getOrder(); $carrier = $this->carrierFactory->create($order->getShippingMethod(true)->getCarrierCode()); if (!$carrier->isShippingLabelsAvailable()) { throw new \Magento\Framework\Exception\LocalizedException(__('Shipping labels is not available.')); } $shipment->setPackages($request->getParam('packages')); $response = $this->labelFactory->create()->requestToShipment($shipment); if ($response->hasErrors()) { throw new \Magento\Framework\Exception\LocalizedException(__($response->getErrors())); } if (!$response->hasInfo()) { throw new \Magento\Framework\Exception\LocalizedException(__('Response info is not exist.')); } $labelsContent = []; $trackingNumbers = []; $info = $response->getInfo(); foreach ($info as $inf) { if (!empty($inf['tracking_number']) && !empty($inf['label_content'])) { $labelsContent[] = $inf['label_content']; $trackingNumbers[] = $inf['tracking_number']; } } $outputPdf = $this->combineLabelsPdf($labelsContent); $shipment->setShippingLabel($outputPdf->render()); $carrierCode = $carrier->getCarrierCode(); $carrierTitle = $this->scopeConfig->getValue('carriers/' . $carrierCode . '/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipment->getStoreId()); if (!empty($trackingNumbers)) { $this->addTrackingNumbersToShipment($shipment, $trackingNumbers, $carrierCode, $carrierTitle); } }
/** * Retrieve tracking url with params * * @param string $key * @param \Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Shipment|\Magento\Sales\Model\Order\Shipment\Track $model * @param string $method Optional - method of a model to get id * @return string */ protected function _getTrackingUrl($key, $model, $method = 'getId') { $urlPart = "{$key}:{$model->{$method}()}:{$model->getProtectCode()}"; $params = ['_direct' => 'shipping/tracking/popup', '_query' => ['hash' => $this->urlEncoder->encode($urlPart)]]; $storeModel = $this->_storeManager->getStore($model->getStoreId()); return $storeModel->getUrl('', $params); }
/** * @param \Magento\Sales\Model\Order\Shipment $object * @return \Magento\Sales\Service\V1\Data\Shipment */ public function extractDto(\Magento\Sales\Model\Order\Shipment $object) { $this->shipmentBuilder->populateWithArray($object->getData()); $this->shipmentBuilder->setItems($this->getItems($object)); $this->shipmentBuilder->setTracks($this->getTracks($object)); $this->shipmentBuilder->setPackages(serialize($object->getPackages())); return $this->shipmentBuilder->create(); }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $transport = ['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_shipment_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); return $this->checkAndSend($order, $notify); }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $this->templateContainer->setTemplateVars(['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore()]); $result = $this->checkAndSend($order, $notify); if ($result) { $shipment->setEmailSent(true); $this->shipmentResource->saveAttribute($shipment, 'email_sent'); } return $result; }
public function testProcessRelations() { $this->shipmentMock->expects($this->once())->method('getId')->willReturn('shipment-id-value'); $this->shipmentMock->expects($this->exactly(2))->method('getItems')->willReturn([$this->itemMock]); $this->shipmentMock->expects($this->exactly(2))->method('getComments')->willReturn([$this->commentMock]); $this->shipmentMock->expects($this->exactly(2))->method('getTracks')->willReturn([$this->trackMock]); $this->itemMock->expects($this->once())->method('setParentId')->with('shipment-id-value')->willReturnSelf(); $this->itemResourceMock->expects($this->once())->method('save')->with($this->itemMock)->willReturnSelf(); $this->commentResourceMock->expects($this->once())->method('save')->with($this->commentMock)->willReturnSelf(); $this->trackResourceMock->expects($this->once())->method('save')->with($this->trackMock)->willReturnSelf(); $this->relationProcessor->processRelation($this->shipmentMock); }
private function getItemsToShip(\Ess\M2ePro\Model\Order $order, \Magento\Sales\Model\Order\Shipment $shipment) { $productTypesNotAllowedByDefault = array(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE, \Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE); $items = array(); $allowedItems = array(); foreach ($shipment->getAllItems() as $shipmentItem) { /** @var $shipmentItem \Magento\Sales\Model\Order\Shipment\Item */ $orderItem = $shipmentItem->getOrderItem(); $parentOrderItemId = $orderItem->getParentItemId(); if (!is_null($parentOrderItemId)) { !in_array($parentOrderItemId, $allowedItems) && ($allowedItems[] = $parentOrderItemId); continue; } if (!in_array($orderItem->getProductType(), $productTypesNotAllowedByDefault)) { $allowedItems[] = $orderItem->getId(); } $additionalData = $orderItem->getAdditionalData(); $additionalData = is_string($additionalData) ? @unserialize($additionalData) : array(); $itemId = $transactionId = null; $orderItemDataIdentifier = \Ess\M2ePro\Helper\Data::CUSTOM_IDENTIFIER; if (isset($additionalData['ebay_item_id']) && isset($additionalData['ebay_transaction_id'])) { // backward compatibility with versions 5.0.4 or less $itemId = $additionalData['ebay_item_id']; $transactionId = $additionalData['ebay_transaction_id']; } elseif (isset($additionalData[$orderItemDataIdentifier]['items'])) { if (!is_array($additionalData[$orderItemDataIdentifier]['items']) || count($additionalData[$orderItemDataIdentifier]['items']) != 1) { return null; } if (isset($additionalData[$orderItemDataIdentifier]['items'][0]['item_id'])) { $itemId = $additionalData[$orderItemDataIdentifier]['items'][0]['item_id']; } if (isset($additionalData[$orderItemDataIdentifier]['items'][0]['transaction_id'])) { $transactionId = $additionalData[$orderItemDataIdentifier]['items'][0]['transaction_id']; } } if (is_null($itemId) || is_null($transactionId)) { continue; } $item = $this->ebayFactory->getObject('Order\\Item')->getCollection()->addFieldToFilter('order_id', $order->getId())->addFieldToFilter('item_id', $itemId)->addFieldToFilter('transaction_id', $transactionId)->getFirstItem(); if (!$item->getId()) { continue; } $items[$orderItem->getId()] = $item; } $resultItems = array(); foreach ($items as $orderItemId => $item) { if (!in_array($orderItemId, $allowedItems)) { continue; } $resultItems[] = $item; } return $resultItems; }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); if ($order->getShippingAddress()) { $formattedShippingAddress = $this->addressRenderer->format($order->getShippingAddress(), 'html'); } else { $formattedShippingAddress = ''; } $formattedBillingAddress = $this->addressRenderer->format($order->getBillingAddress(), 'html'); $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]); $this->eventManager->dispatch('email_shipment_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); return $this->checkAndSend($order, $notify); }
/** * Run shipment mapper test * * @return void */ public function testInvoke() { $this->shipmentMock->expects($this->once())->method('getData')->will($this->returnValue(['field-1' => 'value-1'])); $this->shipmentBuilderMock->expects($this->once())->method('populateWithArray')->with($this->equalTo(['field-1' => 'value-1']))->will($this->returnSelf()); $this->shipmentMock->expects($this->once())->method('getPackages')->will($this->returnValue([[], []])); $this->shipmentBuilderMock->expects($this->once())->method('setPackages')->with($this->equalTo(serialize([[], []])))->will($this->returnSelf()); $this->shipmentMock->expects($this->once())->method('getItemsCollection')->will($this->returnValue([$this->shipmentItemMock])); $this->shipmentItemMapperMock->expects($this->once())->method('extractDto')->with($this->equalTo($this->shipmentItemMock))->will($this->returnValue('item-1')); $this->shipmentBuilderMock->expects($this->once())->method('setItems')->with($this->equalTo(['item-1']))->will($this->returnSelf()); $this->shipmentMock->expects($this->once())->method('getTracksCollection')->will($this->returnValue([$this->shipmentTrackMock])); $this->shipmentTrackMapperMock->expects($this->once())->method('extractDto')->with($this->equalTo($this->shipmentTrackMock))->will($this->returnValue('track-1')); $this->shipmentBuilderMock->expects($this->once())->method('setTracks')->with($this->equalTo(['track-1']))->will($this->returnSelf()); $this->shipmentBuilderMock->expects($this->once())->method('create')->will($this->returnValue('data-object-with-shipment')); $this->assertEquals('data-object-with-shipment', $this->shipmentMapper->extractDto($this->shipmentMock)); }
/** * Run test execute method */ public function testExecute() { $orderId = 1; $shipmentId = 1; $shipment = []; $tracking = []; $incrementId = '10000001'; $comeFrom = true; $this->requestMock->expects($this->at(0))->method('getParam')->with('order_id')->will($this->returnValue($orderId)); $this->requestMock->expects($this->at(1))->method('getParam')->with('shipment_id')->will($this->returnValue($shipmentId)); $this->requestMock->expects($this->at(2))->method('getParam')->with('shipment')->will($this->returnValue($shipment)); $this->requestMock->expects($this->at(3))->method('getParam')->with('tracking')->will($this->returnValue($tracking)); $this->requestMock->expects($this->at(4))->method('getParam')->with('come_from')->will($this->returnValue($comeFrom)); $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId); $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId); $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment); $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock)); $this->shipmentMock->expects($this->once())->method('getIncrementId')->will($this->returnValue($incrementId)); $menuBlockMock = $this->getMock('Magento\\Backend\\Block\\Menu', ['getParentItems', 'getMenuModel'], [], '', false); $menuBlockMock->expects($this->any())->method('getMenuModel')->will($this->returnSelf()); $menuBlockMock->expects($this->any())->method('getParentItems')->with('Magento_Sales::sales_order')->will($this->returnValue([])); $shipmentBlockMock = $this->getMock('Magento\\Shipping\\Block\\Adminhtml\\View', ['updateBackButtonUrl'], [], '', false); $layoutMock = $this->getMock('Magento\\Framework\\View\\Layout', ['getBlock'], [], '', false); $shipmentBlockMock->expects($this->once())->method('updateBackButtonUrl')->with($comeFrom)->will($this->returnSelf()); $layoutMock->expects($this->at(0))->method('getBlock')->with('sales_shipment_view')->will($this->returnValue($shipmentBlockMock)); $layoutMock->expects($this->at(1))->method('getBlock')->with('menu')->will($this->returnValue($menuBlockMock)); $this->viewMock->expects($this->once())->method('loadLayout')->will($this->returnSelf()); $this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($layoutMock)); $this->viewMock->expects($this->once())->method('renderLayout')->will($this->returnSelf()); $this->assertNull($this->controller->execute()); }
/** * Sends order shipment email to the customer. * * Email will be sent immediately in two cases: * * - if asynchronous email sending is disabled in global settings * - if $forceSyncMode parameter is set to TRUE * * Otherwise, email will be sent later during running of * corresponding cron job. * * @param Shipment $shipment * @param bool $forceSyncMode * @return bool */ public function send(Shipment $shipment, $forceSyncMode = false) { $shipment->setSendEmail(true); if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) { $order = $shipment->getOrder(); $transport = ['order' => $order, 'shipment' => $shipment, 'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '', 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_shipment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); if ($this->checkAndSend($order)) { $shipment->setEmailSent(true); $this->shipmentResource->saveAttribute($shipment, ['send_email', 'email_sent']); return true; } } $this->shipmentResource->saveAttribute($shipment, 'send_email'); return false; }
/** * Run test execute method (exception save shipment) */ public function testExecuteSaveException() { $logerMock = $this->getMock('Magento\\Framework\\Logger', ['logException'], [], '', false); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock)); $this->labelGenerator->expects($this->once())->method('create')->with($this->shipmentMock, $this->requestMock)->will($this->returnValue(true)); $this->shipmentMock->expects($this->once())->method('save')->will($this->throwException(new \Exception())); $logerMock->expects($this->once())->method('logException'); $this->objectManagerMock->expects($this->once())->method('get')->with('Magento\\Framework\\Logger')->will($this->returnValue($logerMock)); $this->responseMock->expects($this->once())->method('representJson'); $this->assertNull($this->controller->execute()); }
/** * @param $shipmentId * * @return string */ public function getTrackingPrintUrl($shipmentId) { if ($shipmentId) { if ($shipment = $this->_shipment->load($shipmentId)) { if ($shipment->getShippingLabel()) { $params = ['shipment_ids' => $shipment->getShippingLabel(), 'response_type' => $this->scopeConfig->getValue('carriers/mercadoenvios/shipping_label'), 'access_token' => $this->_mpHelper->getAccessToken()]; return self::ME_SHIPMENT_LABEL_URL . '?' . http_build_query($params); } } } return ''; }
/** * @param \Magento\Framework\Event\Observer $observer * * @throws \Exception * @throws bool */ public function execute(\Magento\Framework\Event\Observer $observer) { $merchantOrder = $observer->getMerchantOrder(); if (!count($merchantOrder['shipments']) > 0) { return; } $data = $observer->getPayment(); $order = $this->_coreModel->_getOrder($data["external_reference"]); //if order has shipments, status is updated. If it doesn't the shipment is created. if ($merchantOrder['shipments'][0]['status'] == 'ready_to_ship') { if ($order->hasShipments()) { $shipment = $this->_shipment->load($order->getId(), 'order_id'); } else { $shipment = $this->_shipmentFactory->create($order); $order->setIsInProcess(true); } $shipment->setShippingLabel($merchantOrder['shipments'][0]['id']); $shipmentInfo = $this->_shipmentHelper->getShipmentInfo($merchantOrder['shipments'][0]['id']); $this->_coreHelper->log("Shipment Info", 'mercadopago-notification.log', $shipmentInfo); $serviceInfo = $this->_shipmentHelper->getServiceInfo($merchantOrder['shipments'][0]['service_id'], $merchantOrder['site_id']); $this->_coreHelper->log("Service Info by service id", 'mercadopago-notification.log', $serviceInfo); if ($shipmentInfo && isset($shipmentInfo->tracking_number)) { $tracking['number'] = $shipmentInfo->tracking_number; $tracking['description'] = str_replace('#{trackingNumber}', $shipmentInfo->tracking_number, $serviceInfo->tracking_url); $tracking['title'] = self::CODE; $existingTracking = $this->_trackFactory->create()->load($shipment->getOrderId(), 'order_id'); if ($existingTracking->getId()) { $track = $shipment->getTrackById($existingTracking->getId()); $track->setNumber($tracking['number'])->setDescription($tracking['description'])->setTitle($tracking['title'])->save(); } else { $track = $this->_trackFactory->create()->addData($tracking); $track->setCarrierCode(\MercadoPago\MercadoEnvios\Model\Carrier\MercadoEnvios::CODE); $shipment->addTrack($track); $shipment->save(); } $this->_coreHelper->log("Track added", 'mercadopago-notification.log', $track); } $this->_transaction->addObject($order)->save(); } }
/** * @param bool $isVirtualOrder * @param int $formatCallCount * @param string|null $expectedShippingAddress * @dataProvider sendVirtualOrderDataProvider */ public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress) { $address = 'address_test'; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->shipmentMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with('sales_email/general/async_sending')->willReturn(false); $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->exactly($formatCallCount))->method('format')->with($addressMock, 'html')->willReturn($address); $this->stepAddressFormat($addressMock, $isVirtualOrder); $this->shipmentMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(false); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false); $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); }
/** * @param \Ess\M2ePro\Model\Order $order * @param \Magento\Sales\Model\Order\Shipment $shipment * * @throws \LogicException * * @return array */ private function getItemsToShip(\Ess\M2ePro\Model\Order $order, \Magento\Sales\Model\Order\Shipment $shipment) { $shipmentItems = $shipment->getAllItems(); $orderItemDataIdentifier = \Ess\M2ePro\Helper\Data::CUSTOM_IDENTIFIER; $items = array(); foreach ($shipmentItems as $shipmentItem) { $additionalData = $shipmentItem->getOrderItem()->getAdditionalData(); $additionalData = is_string($additionalData) ? @unserialize($additionalData) : array(); if (!isset($additionalData[$orderItemDataIdentifier]['items'])) { continue; } if (!is_array($additionalData[$orderItemDataIdentifier]['items'])) { continue; } $qtyAvailable = (int) $shipmentItem->getQty(); foreach ($additionalData[$orderItemDataIdentifier]['items'] as $data) { if ($qtyAvailable <= 0) { continue; } if (!isset($data['order_item_id'])) { continue; } $item = $order->getItemsCollection()->getItemByColumnValue('amazon_order_item_id', $data['order_item_id']); if (is_null($item)) { continue; } $qty = $item->getChildObject()->getQtyPurchased(); if ($qty > $qtyAvailable) { $qty = $qtyAvailable; } $items[] = array('qty' => $qty, 'amazon_order_item_id' => $data['order_item_id']); $qtyAvailable -= $qty; } } return $items; }
/** * Run test execute method */ public function testExecute() { $orderId = 1; $shipmentId = 1; $shipment = []; $tracking = []; $incrementId = '10000001'; $comeFrom = true; $this->loadShipment($orderId, $shipmentId, $shipment, $tracking, $comeFrom, $this->shipmentMock); $this->shipmentMock->expects($this->once())->method('getIncrementId')->willReturn($incrementId); $this->resultPageFactoryMock->expects($this->once()) ->method('create') ->willReturn($this->resultPageMock); $layoutMock = $this->getMock('Magento\Framework\View\Layout', ['getBlock', '__wakeup'], [], '', false); $this->resultPageMock->expects($this->once()) ->method('getLayout') ->willReturn($layoutMock); $layoutMock->expects($this->once()) ->method('getBlock') ->with('sales_shipment_view') ->willReturn($this->blockMock); $this->blockMock->expects($this->once()) ->method('updateBackButtonUrl') ->with($comeFrom) ->willReturnSelf(); $this->resultPageMock->expects($this->once()) ->method('setActiveMenu') ->with('Magento_Sales::sales_shipment') ->willReturnSelf(); $this->resultPageMock->expects($this->atLeastOnce()) ->method('getConfig') ->willReturn($this->pageConfigMock); $this->pageConfigMock->expects($this->atLeastOnce()) ->method('getTitle') ->willReturn($this->pageTitleMock); $this->pageTitleMock->expects($this->exactly(2)) ->method('prepend') ->withConsecutive( ['Shipments'], ["#" . $incrementId] ) ->willReturnSelf(); $this->assertEquals($this->resultPageMock, $this->controller->executeInternal()); }
/** * Run test execute method (fail load page from image string) */ public function testExecuteImageStringFail() { $labelContent = 'Label-content'; $incrementId = '1000001'; $loggerMock = $this->getMock('Psr\\Log\\LoggerInterface'); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock)); $this->shipmentMock->expects($this->once())->method('getShippingLabel')->will($this->returnValue($labelContent)); $this->shipmentMock->expects($this->once())->method('getIncrementId')->will($this->returnValue($incrementId)); $this->labelGenerator->expects($this->once())->method('createPdfPageFromImageString')->with($labelContent)->will($this->returnValue(false)); $this->messageManagerMock->expects($this->at(0))->method('addError')->with(sprintf('We don\'t recognize or support the file extension in this shipment: %s.', $incrementId))->will($this->throwException(new \Exception())); $this->messageManagerMock->expects($this->at(1))->method('addError')->with('An error occurred while creating shipping label.')->will($this->returnSelf()); $this->objectManagerMock->expects($this->once())->method('get')->with('Psr\\Log\\LoggerInterface')->will($this->returnValue($loggerMock)); $loggerMock->expects($this->once())->method('critical'); $this->requestMock->expects($this->at(4))->method('getParam')->with('shipment_id')->will($this->returnValue(1)); $this->redirectSection(); $this->assertNull($this->controller->execute()); }
/** * Run test execute method (exception save shipment) */ public function testExecuteSaveException() { $logerMock = $this->getMock('Psr\Log\LoggerInterface'); $this->shipmentLoaderMock->expects($this->once()) ->method('load') ->will($this->returnValue($this->shipmentMock)); $this->labelGenerator->expects($this->once()) ->method('create') ->with($this->shipmentMock, $this->requestMock) ->will($this->returnValue(true)); $this->shipmentMock->expects($this->once())->method('save')->will($this->throwException(new \Exception())); $logerMock->expects($this->once())->method('critical'); $this->objectManagerMock->expects($this->once()) ->method('get') ->with('Psr\Log\LoggerInterface') ->will($this->returnValue($logerMock)); $this->responseMock->expects($this->once())->method('representJson'); $this->assertNull($this->controller->executeInternal()); }
/** * Run test execute method (save exception) */ public function testExecuteExceptionSave() { $data = ['comment' => 'comment']; $orderId = 1; $shipmentId = 1; $shipment = []; $tracking = []; $this->requestMock->expects($this->once())->method('setParam')->with('shipment_id', $shipmentId); $this->requestMock->expects($this->once())->method('getPost')->with('comment')->will($this->returnValue($data)); $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([['id', null, $shipmentId], ['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipment], ['tracking', null, $tracking]])); $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId); $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId); $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment); $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock)); $this->shipmentMock->expects($this->once())->method('addComment'); $this->shipmentCommentSenderMock->expects($this->once())->method('send'); $this->shipmentMock->expects($this->once())->method('save')->will($this->throwException(new \Exception())); $this->exceptionResponse(); $this->assertNull($this->controller->execute()); }
public function testGetEntityType() { $this->assertEquals('shipment', $this->shipment->getEntityType()); }
/** * Getter for billing address of order by format * * @param \Magento\Sales\Model\Order\Shipment $shipment * @return array */ public function getShipmentItems($shipment) { $res = array(); foreach ($shipment->getItemsCollection() as $item) { if (!$item->getOrderItem()->getParentItem()) { $res[] = $item; } } return $res; }
/** * @param Shipment $shipment * @return \Magento\Shipping\Model\ResourceModel\Order\Track\Collection */ protected function _getTracksCollection(Shipment $shipment) { $tracks = $this->_trackCollectionFactory->create()->setShipmentFilter($shipment->getId()); if ($shipment->getId()) { foreach ($tracks as $track) { $track->setShipment($shipment); } } return $tracks; }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $this->templateContainer->setTemplateVars(['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore()]); return $this->checkAndSend($order, $notify); }
/** * test shipment label get service */ public function testInvoke() { $this->shipmentRepositoryMock->expects($this->once())->method('get')->with($this->equalTo(1))->will($this->returnValue($this->shipmentMock)); $this->shipmentMock->expects($this->once())->method('getShippingLabel')->will($this->returnValue('shipping_label')); $this->assertEquals('shipping_label', $this->shipmentLabelGet->invoke(1)); }