Exemplo n.º 1
0
 /**
  * Print label for one specific shipment
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     try {
         $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
         $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
         $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
         $shipment = $this->shipmentLoader->load();
         $labelContent = $shipment->getShippingLabel();
         if ($labelContent) {
             $pdfContent = null;
             if (stripos($labelContent, '%PDF-') !== false) {
                 $pdfContent = $labelContent;
             } else {
                 $pdf = new \Zend_Pdf();
                 $page = $this->labelGenerator->createPdfPageFromImageString($labelContent);
                 if (!$page) {
                     $this->messageManager->addError(__('We don\'t recognize or support the file extension in this shipment: %1.', $shipment->getIncrementId()));
                 }
                 $pdf->pages[] = $page;
                 $pdfContent = $pdf->render();
             }
             return $this->_fileFactory->create('ShippingLabel(' . $shipment->getIncrementId() . ').pdf', $pdfContent, DirectoryList::VAR_DIR, 'application/pdf');
         }
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         $this->messageManager->addError(__('An error occurred while creating shipping label.'));
     }
     $this->_redirect('adminhtml/order_shipment/view', ['shipment_id' => $this->getRequest()->getParam('shipment_id')]);
 }
Exemplo n.º 2
0
 public function testExecute()
 {
     $shipmentId = 1000012;
     $orderId = 10003;
     $tracking = [];
     $shipmentData = ['items' => [], 'send_email' => ''];
     $shipment = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment', ['load', 'save', 'register', 'getOrder', 'getOrderId', '__wakeup'], [], '', false);
     $order = $this->getMock('Magento\\Sales\\Model\\Order', ['setCustomerNoteNotify', '__wakeup'], [], '', false);
     $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipmentData], ['tracking', null, $tracking]]));
     $this->shipmentLoader->expects($this->any())->method('setShipmentId')->with($shipmentId);
     $this->shipmentLoader->expects($this->any())->method('setOrderId')->with($orderId);
     $this->shipmentLoader->expects($this->any())->method('setShipment')->with($shipmentData);
     $this->shipmentLoader->expects($this->any())->method('setTracking')->with($tracking);
     $this->shipmentLoader->expects($this->once())->method('load')->will($this->returnValue($shipment));
     $shipment->expects($this->once())->method('register')->will($this->returnSelf());
     $shipment->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $order->expects($this->once())->method('setCustomerNoteNotify')->with(false);
     $this->labelGenerator->expects($this->any())->method('create')->with($shipment, $this->request)->will($this->returnValue(true));
     $saveTransaction = $this->getMockBuilder('Magento\\Framework\\DB\\Transaction')->disableOriginalConstructor()->setMethods([])->getMock();
     $saveTransaction->expects($this->at(0))->method('addObject')->with($shipment)->will($this->returnSelf());
     $saveTransaction->expects($this->at(1))->method('addObject')->with($order)->will($this->returnSelf());
     $saveTransaction->expects($this->at(2))->method('save');
     $this->session->expects($this->once())->method('getCommentText')->with(true);
     $this->objectManager->expects($this->once())->method('create')->with('Magento\\Framework\\DB\\Transaction')->will($this->returnValue($saveTransaction));
     $this->objectManager->expects($this->once())->method('get')->with('Magento\\Backend\\Model\\Session')->will($this->returnValue($this->session));
     $path = 'sales/order/view';
     $arguments = ['order_id' => $orderId];
     $shipment->expects($this->once())->method('getOrderId')->will($this->returnValue($orderId));
     $this->prepareRedirect($path, $arguments);
     $this->saveAction->execute();
     $this->assertEquals($this->response, $this->saveAction->getResponse());
 }
Exemplo n.º 3
0
 /**
  * Run test execute method (fail generate label)
  */
 public function testExecuteLabelGenerateFail()
 {
     $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->throwException(new \Magento\Framework\Model\Exception()));
     $this->responseMock->expects($this->once())->method('representJson');
     $this->assertNull($this->controller->execute());
 }
 /**
  * @covers \Magento\Shipping\Model\Shipping\LabelGenerator
  * @param array $info
  * @dataProvider labelInfoDataProvider
  */
 public function testAddTrackingNumbersToShipment(array $info)
 {
     $order = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock();
     $order->expects(static::once())->method('getShippingMethod')->with(true)->willReturn($this->getShippingMethodMock());
     /**
      * @var $shipmentMock \Magento\Sales\Model\Order\Shipment|\PHPUnit_Framework_MockObject_MockObject
      */
     $shipmentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment')->disableOriginalConstructor()->getMock();
     $shipmentMock->expects(static::once())->method('getOrder')->willReturn($order);
     $this->carrierFactory->expects(static::once())->method('create')->with(self::CARRIER_CODE)->willReturn($this->getCarrierMock());
     $labelsMock = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Shipping\\Labels')->disableOriginalConstructor()->getMock();
     $labelsMock->expects(static::once())->method('requestToShipment')->with($shipmentMock)->willReturn($this->getResponseMock($info));
     $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock);
     $this->filesystem->expects(static::once())->method('getDirectoryWrite')->willReturn($this->getMock('Magento\\Framework\\Filesystem\\Directory\\WriteInterface'));
     $this->scopeConfig->expects(static::once())->method('getValue')->with('carriers/' . self::CARRIER_CODE . '/title', ScopeInterface::SCOPE_STORE, null)->willReturn(self::CARRIER_TITLE);
     $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock);
     $trackMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment\\Track')->setMethods(['setNumber', 'setCarrierCode', 'setTitle'])->disableOriginalConstructor()->getMock();
     $i = 0;
     $trackingNumbers = is_array($info['tracking_number']) ? $info['tracking_number'] : [$info['tracking_number']];
     foreach ($trackingNumbers as $trackingNumber) {
         $trackMock->expects(static::at($i++))->method('setNumber')->with($trackingNumber)->willReturnSelf();
         $trackMock->expects(static::at($i++))->method('setCarrierCode')->with(self::CARRIER_CODE)->willReturnSelf();
         $trackMock->expects(static::at($i++))->method('setTitle')->with(self::CARRIER_TITLE)->willReturnSelf();
     }
     $this->trackFactory->expects(static::any())->method('create')->willReturn($trackMock);
     /**
      * @var $requestMock \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
      */
     $requestMock = $this->getMock('Magento\\Framework\\App\\RequestInterface');
     $this->labelGenerator->create($shipmentMock, $requestMock);
 }
Exemplo n.º 5
0
 /**
  * Save shipment
  * We can save only new shipment. Existing shipments are not editable
  *
  * @return void
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('shipment');
     if (!empty($data['comment_text'])) {
         $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']);
     }
     try {
         $shipment = $this->shipmentLoader->load($this->_request);
         if (!$shipment) {
             $this->_forward('noroute');
             return;
         }
         $shipment->register();
         $comment = '';
         if (!empty($data['comment_text'])) {
             $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front']));
             if (isset($data['comment_customer_notify'])) {
                 $comment = $data['comment_text'];
             }
         }
         if (!empty($data['send_email'])) {
             $shipment->setEmailSent(true);
         }
         $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
         $responseAjax = new \Magento\Framework\Object();
         $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label'];
         if ($isNeedCreateLabel && $this->labelGenerator->create($shipment, $this->_request)) {
             $responseAjax->setOk(true);
         }
         $this->_saveShipment($shipment);
         $shipment->sendEmail(!empty($data['send_email']), $comment);
         $shipmentCreatedMessage = __('The shipment has been created.');
         $labelCreatedMessage = __('You created the shipping label.');
         $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage);
         $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true);
     } catch (\Magento\Framework\Model\Exception $e) {
         if ($isNeedCreateLabel) {
             $responseAjax->setError(true);
             $responseAjax->setMessage($e->getMessage());
         } else {
             $this->messageManager->addError($e->getMessage());
             $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
         }
     } catch (\Exception $e) {
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
         if ($isNeedCreateLabel) {
             $responseAjax->setError(true);
             $responseAjax->setMessage(__('An error occurred while creating shipping label.'));
         } else {
             $this->messageManager->addError(__('Cannot save shipment.'));
             $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
         }
     }
     if ($isNeedCreateLabel) {
         $this->getResponse()->representJson($responseAjax->toJson());
     } else {
         $this->_redirect('sales/order/view', array('order_id' => $shipment->getOrderId()));
     }
 }
 /**
  * Batch print shipping labels for whole shipments.
  * Push pdf document with shipping labels to user browser
  *
  * @param AbstractCollection $collection
  * @return ResponseInterface|ResultInterface
  */
 protected function massAction(AbstractCollection $collection)
 {
     $labelsContent = [];
     if ($collection->getSize()) {
         /** @var \Magento\Sales\Model\Order\Shipment $shipment */
         foreach ($collection as $shipment) {
             $labelContent = $shipment->getShippingLabel();
             if ($labelContent) {
                 $labelsContent[] = $labelContent;
             }
         }
     }
     if (!empty($labelsContent)) {
         $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
         return $this->fileFactory->create('ShippingLabels.pdf', $outputPdf->render(), DirectoryList::VAR_DIR, 'application/pdf');
     }
     $this->messageManager->addError(__('There are no shipping labels related to selected shipments.'));
     return $this->resultRedirectFactory->create()->setPath('sales/shipment/');
 }
Exemplo n.º 7
0
 /**
  * Batch print shipping labels for whole shipments.
  * Push pdf document with shipping labels to user browser
  *
  * @param AbstractCollection $collection
  * @return ResponseInterface|void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function massAction(AbstractCollection $collection)
 {
     $request = $this->getRequest();
     $ids = $collection->getAllIds();
     $createdFromOrders = !empty($ids);
     $shipments = null;
     $labelsContent = [];
     switch ($request->getParam('massaction_prepare_key')) {
         case 'shipment_ids':
             $ids = $request->getParam('shipment_ids');
             array_filter($ids, 'intval');
             if (!empty($ids)) {
                 $shipments = $this->_objectManager->create('Magento\\Sales\\Model\\Resource\\Order\\Shipment\\Collection')->addFieldToFilter('entity_id', ['in' => $ids]);
             }
             break;
         case 'order_ids':
             $ids = $request->getParam('order_ids');
             array_filter($ids, 'intval');
             if (!empty($ids)) {
                 $shipments = $this->_objectManager->create('Magento\\Sales\\Model\\Resource\\Order\\Shipment\\Collection')->setOrderFilter(['in' => $ids]);
             }
             break;
     }
     if ($shipments && $shipments->getSize()) {
         foreach ($shipments as $shipment) {
             $labelContent = $shipment->getShippingLabel();
             if ($labelContent) {
                 $labelsContent[] = $labelContent;
             }
         }
     }
     if (!empty($labelsContent)) {
         $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
         return $this->_fileFactory->create('ShippingLabels.pdf', $outputPdf->render(), DirectoryList::VAR_DIR, 'application/pdf');
     }
     if ($createdFromOrders) {
         $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
         $this->_redirect('sales/order/index');
     } else {
         $this->messageManager->addError(__('There are no shipping labels related to selected shipments.'));
         $this->_redirect('sales/shipment/index');
     }
 }
Exemplo n.º 8
0
 /**
  * Create shipping label action for specific shipment
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object();
     try {
         $shipment = $this->shipmentLoader->load($this->_request);
         if ($this->labelGenerator->create($shipment, $this->_request)) {
             $shipment->save();
             $this->messageManager->addSuccess(__('You created the shipping label.'));
             $response->setOk(true);
         }
     } catch (\Magento\Framework\Model\Exception $e) {
         $response->setError(true);
         $response->setMessage($e->getMessage());
     } catch (\Exception $e) {
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
         $response->setError(true);
         $response->setMessage(__('An error occurred while creating shipping label.'));
     }
     $this->getResponse()->representJson($response->toJson());
 }
Exemplo n.º 9
0
 /**
  * Create shipping label action for specific shipment
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\DataObject();
     try {
         $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
         $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
         $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
         $shipment = $this->shipmentLoader->load();
         $this->labelGenerator->create($shipment, $this->_request);
         $shipment->save();
         $this->messageManager->addSuccess(__('You created the shipping label.'));
         $response->setOk(true);
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $response->setError(true);
         $response->setMessage($e->getMessage());
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         $response->setError(true);
         $response->setMessage(__('An error occurred while creating shipping label.'));
     }
     $this->getResponse()->representJson($response->toJson());
 }
Exemplo n.º 10
0
 /**
  * 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());
 }
Exemplo n.º 11
0
 /**
  * Save shipment
  * We can save only new shipment. Existing shipments are not editable
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
     $isPost = $this->getRequest()->isPost();
     if (!$formKeyIsValid || !$isPost) {
         $this->messageManager->addError(__('We can\'t save the shipment right now.'));
         return $resultRedirect->setPath('sales/order/index');
     }
     $data = $this->getRequest()->getParam('shipment');
     if (!empty($data['comment_text'])) {
         $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']);
     }
     try {
         $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
         $this->shipmentLoader->setShipment($data);
         $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
         $shipment = $this->shipmentLoader->load();
         if (!$shipment) {
             $this->_forward('noroute');
             return;
         }
         if (!empty($data['comment_text'])) {
             $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front']));
             $shipment->setCustomerNote($data['comment_text']);
             $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify']));
         }
         $shipment->register();
         $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
         $responseAjax = new \Magento\Framework\DataObject();
         $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label'];
         if ($isNeedCreateLabel) {
             $this->labelGenerator->create($shipment, $this->_request);
             $responseAjax->setOk(true);
         }
         $this->_saveShipment($shipment);
         if (!empty($data['send_email'])) {
             $this->shipmentSender->send($shipment);
         }
         $shipmentCreatedMessage = __('The shipment has been created.');
         $labelCreatedMessage = __('You created the shipping label.');
         $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage);
         $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true);
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         if ($isNeedCreateLabel) {
             $responseAjax->setError(true);
             $responseAjax->setMessage($e->getMessage());
         } else {
             $this->messageManager->addError($e->getMessage());
             $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
         }
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         if ($isNeedCreateLabel) {
             $responseAjax->setError(true);
             $responseAjax->setMessage(__('An error occurred while creating shipping label.'));
         } else {
             $this->messageManager->addError(__('Cannot save shipment.'));
             $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
         }
     }
     if ($isNeedCreateLabel) {
         $this->getResponse()->representJson($responseAjax->toJson());
     } else {
         $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]);
     }
 }