Esempio n. 1
0
 /**
  * Add comment to shipment history
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('shipment_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new \Magento\Framework\Model\Exception(__("The comment text field cannot be empty."));
         }
         $this->_title->add(__('Shipments'));
         $shipment = $this->shipmentLoader->load($this->_request);
         $shipment->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $shipment->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
         $shipment->save();
         $this->_view->loadLayout(false);
         $response = $this->_view->getLayout()->getBlock('shipment_comments')->toHtml();
     } catch (\Magento\Framework\Model\Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot add new comment.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Esempio n. 2
0
 /**
  * Remove tracking number from shipment
  *
  * @return void
  */
 public function execute()
 {
     $trackId = $this->getRequest()->getParam('track_id');
     $track = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Shipment\\Track')->load($trackId);
     if ($track->getId()) {
         try {
             $this->_title->add(__('Shipments'));
             $shipment = $this->shipmentLoader->load($this->_request);
             if ($shipment) {
                 $track->delete();
                 $this->_view->loadLayout();
                 $response = $this->_view->getLayout()->getBlock('shipment_tracking')->toHtml();
             } else {
                 $response = array('error' => true, 'message' => __('Cannot initialize shipment for delete tracking number.'));
             }
         } catch (\Exception $e) {
             $response = array('error' => true, 'message' => __('Cannot delete tracking number.'));
         }
     } else {
         $response = array('error' => true, 'message' => __('Cannot load track with retrieving identifier.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Esempio n. 3
0
 /**
  * Add new tracking number action
  *
  * @return void
  * @throws \Magento\Framework\Model\Exception
  */
 public function execute()
 {
     try {
         $carrier = $this->getRequest()->getPost('carrier');
         $number = $this->getRequest()->getPost('number');
         $title = $this->getRequest()->getPost('title');
         if (empty($carrier)) {
             throw new \Magento\Framework\Model\Exception(__('Please specify a carrier.'));
         }
         if (empty($number)) {
             throw new \Magento\Framework\Model\Exception(__('Please enter a tracking number.'));
         }
         $this->_title->add(__('Shipments'));
         $shipment = $this->shipmentLoader->load($this->_request);
         if ($shipment) {
             $track = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Shipment\\Track')->setNumber($number)->setCarrierCode($carrier)->setTitle($title);
             $shipment->addTrack($track)->save();
             $this->_view->loadLayout();
             $response = $this->_view->getLayout()->getBlock('shipment_tracking')->toHtml();
         } else {
             $response = array('error' => true, 'message' => __('Cannot initialize shipment for adding tracking number.'));
         }
     } catch (\Magento\Framework\Model\Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot add tracking number.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Esempio n. 4
0
 /**
  * Print label for one specific shipment
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     try {
         $shipment = $this->shipmentLoader->load($this->_request);
         $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, \Magento\Framework\App\Filesystem::VAR_DIR, 'application/pdf');
         }
     } catch (\Magento\Framework\Model\Exception $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
         $this->messageManager->addError(__('An error occurred while creating shipping label.'));
     }
     $this->_redirect('adminhtml/order_shipment/view', array('shipment_id' => $this->getRequest()->getParam('shipment_id')));
 }
Esempio 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()));
     }
 }
Esempio n. 6
0
 /**
  * Create pdf document with information about packages
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     $shipment = $this->shipmentLoader->load($this->_request);
     if ($shipment) {
         $pdf = $this->_objectManager->create('Magento\\Shipping\\Model\\Order\\Pdf\\Packaging')->getPdf($shipment);
         return $this->_fileFactory->create('packingslip' . $this->_objectManager->get('Magento\\Framework\\Stdlib\\DateTime\\DateTime')->date('Y-m-d_H-i-s') . '.pdf', $pdf->render(), \Magento\Framework\App\Filesystem::VAR_DIR, 'application/pdf');
     } else {
         $this->_forward('noroute');
     }
 }
Esempio n. 7
0
 /**
  * Shipment information page
  *
  * @return void
  */
 public function execute()
 {
     $this->_title->add(__('Shipments'));
     $shipment = $this->shipmentLoader->load($this->_request);
     if ($shipment) {
         $this->_title->add("#" . $shipment->getIncrementId());
         $this->_view->loadLayout();
         $this->_view->getLayout()->getBlock('sales_shipment_view')->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
         $this->_setActiveMenu('Magento_Sales::sales_order');
         $this->_view->renderLayout();
     } else {
         $this->_forward('noroute');
     }
 }
Esempio n. 8
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());
 }
Esempio n. 9
0
 public function testExecute()
 {
     $carrier = 'carrier';
     $number = 'number';
     $title = 'title';
     $shipmentId = 1000012;
     $orderId = 10003;
     $tracking = [];
     $shipmentData = ['items' => [], 'send_email' => ''];
     $shipment = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment', ['addTrack', '__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->request->expects($this->any())->method('getPost')->will($this->returnValueMap([['carrier', $carrier], ['number', $number], ['title', $title]]));
     $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));
     $this->title->expects($this->any())->method('add')->with('Shipments')->will($this->returnSelf());
     $track = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment\\Track')->disableOriginalConstructor()->setMethods(['__wakeup', 'setNumber', 'setCarrierCode', 'setTitle'])->getMock();
     $this->objectManager->expects($this->atLeastOnce())->method('create')->with('Magento\\Sales\\Model\\Order\\Shipment\\Track')->will($this->returnValue($track));
     $track->expects($this->once())->method('setNumber')->with($number)->will($this->returnSelf());
     $track->expects($this->once())->method('setCarrierCode')->with($carrier)->will($this->returnSelf());
     $track->expects($this->once())->method('setTitle')->with($title)->will($this->returnSelf());
     $this->view->expects($this->once())->method('loadLayout')->will($this->returnSelf());
     $layout = $this->getMock('Magento\\Framework\\View\\Layout\\Element\\Layout', ['getBlock'], [], '', false);
     $menuBlock = $this->getMock('Magento\\Framework\\View\\Element\\BlockInterface', ['toHtml'], [], '', false);
     $html = 'html string';
     $this->view->expects($this->once())->method('getLayout')->will($this->returnValue($layout));
     $layout->expects($this->once())->method('getBlock')->with('shipment_tracking')->will($this->returnValue($menuBlock));
     $menuBlock->expects($this->once())->method('toHtml')->will($this->returnValue($html));
     $shipment->expects($this->once())->method('addTrack')->with($this->equalTo($track))->will($this->returnSelf());
     $shipment->expects($this->any())->method('save')->will($this->returnSelf());
     $this->response->expects($this->once())->method('setBody')->with($html);
     $this->assertNull($this->controller->execute());
 }
Esempio n. 10
0
 public function testEmail()
 {
     $shipmentId = 1000012;
     $orderId = 10003;
     $tracking = [];
     $shipment = ['items' => []];
     $orderShipment = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment', ['load', 'save', '__wakeup'], [], '', false);
     $shipmentNotifierClassName = 'Magento\\Shipping\\Model\\ShipmentNotifier';
     $shipmentNotifier = $this->getMock($shipmentNotifierClassName, ['notify', '__wakeup'], [], '', false);
     $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipment], ['tracking', null, $tracking]]));
     $this->shipmentLoader->expects($this->once())->method('setShipmentId')->with($shipmentId);
     $this->shipmentLoader->expects($this->once())->method('setOrderId')->with($orderId);
     $this->shipmentLoader->expects($this->once())->method('setShipment')->with($shipment);
     $this->shipmentLoader->expects($this->once())->method('setTracking')->with($tracking);
     $this->shipmentLoader->expects($this->once())->method('load')->will($this->returnValue($orderShipment));
     $orderShipment->expects($this->once())->method('save')->will($this->returnSelf());
     $this->objectManager->expects($this->once())->method('create')->with($shipmentNotifierClassName)->will($this->returnValue($shipmentNotifier));
     $shipmentNotifier->expects($this->once())->method('notify')->with($orderShipment)->will($this->returnValue(true));
     $this->messageManager->expects($this->once())->method('addSuccess')->with('You sent the shipment.');
     $path = '*/*/view';
     $arguments = ['shipment_id' => $shipmentId];
     $this->prepareRedirect($path, $arguments, 0);
     $this->shipmentEmail->execute();
     $this->assertEquals($this->response, $this->shipmentEmail->getResponse());
 }
 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);
     $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));
     $this->session->expects($this->once())->method('getCommentText')->with(true)->will($this->returnValue(''));
     $this->objectManager->expects($this->atLeastOnce())->method('get')->with('Magento\\Backend\\Model\\Session')->will($this->returnValue($this->session));
     $this->view->expects($this->once())->method('loadLayout')->will($this->returnSelf());
     $this->view->expects($this->once())->method('renderLayout')->will($this->returnSelf());
     $this->view->expects($this->any())->method('getPage')->willReturn($this->resultPageMock);
     $this->resultPageMock->expects($this->any())->method('getConfig')->willReturn($this->pageConfigMock);
     $this->pageConfigMock->expects($this->any())->method('getTitle')->willReturn($this->pageTitleMock);
     $layout = $this->getMock('Magento\\Framework\\View\\Layout\\Element\\Layout', ['getBlock'], [], '', false);
     $menuBlock = $this->getMock('Magento\\Framework\\View\\Element\\BlockInterface', ['toHtml', 'setActive', 'getMenuModel'], [], '', false);
     $menuModel = $this->getMockBuilder('Magento\\Backend\\Model\\Menu')->disableOriginalConstructor()->getMock();
     $itemId = 'Magento_Sales::sales_order';
     $parents = [new \Magento\Framework\DataObject(['title' => 'title1']), new \Magento\Framework\DataObject(['title' => 'title2']), new \Magento\Framework\DataObject(['title' => 'title3'])];
     $menuModel->expects($this->once())->method('getParentItems')->with($itemId)->will($this->returnValue($parents));
     $menuBlock->expects($this->once())->method('setActive')->with($itemId);
     $menuBlock->expects($this->once())->method('getMenuModel')->will($this->returnValue($menuModel));
     $this->view->expects($this->once())->method('getLayout')->will($this->returnValue($layout));
     $layout->expects($this->once())->method('getBlock')->with('menu')->will($this->returnValue($menuBlock));
     $this->assertNull($this->newAction->execute());
 }
 /**
  * Run test execute method (fail load shipment model)
  */
 public function testExecuteLoadShipmentFail()
 {
     $this->shipmentLoaderMock->expects($this->once())->method('load')->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message')));
     $this->messageManagerMock->expects($this->once())->method('addError')->will($this->returnSelf());
     $this->redirectSection();
     $this->assertNull($this->controller->execute());
 }
Esempio n. 13
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());
 }
Esempio n. 14
0
 /**
  * Shipment create page
  *
  * @return void
  */
 public function execute()
 {
     $this->_title->add(__('Shipments'));
     $shipment = $this->shipmentLoader->load($this->_request);
     if ($shipment) {
         $this->_title->add(__('New Shipment'));
         $comment = $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true);
         if ($comment) {
             $shipment->setCommentText($comment);
         }
         $this->_view->loadLayout();
         $this->_setActiveMenu('Magento_Sales::sales_order');
         $this->_view->renderLayout();
     } else {
         $this->_redirect('*/order/view', array('order_id' => $this->getRequest()->getParam('order_id')));
     }
 }
Esempio n. 15
0
 /**
  * Run test execute method (fail print)
  */
 public function testExecuteFail()
 {
     $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue(false));
     $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue(false));
     $this->actionFlag->expects($this->once())->method('get')->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)->will($this->returnValue(true));
     $this->sessionMock->expects($this->once())->method('setIsUrlNotice')->with(true);
     $this->assertNull($this->controller->execute());
 }
Esempio n. 16
0
 /**
  * Run test execute method (delete exception)
  */
 public function testExecuteDeleteFail()
 {
     $errors = ['error' => true, 'message' => 'Cannot delete tracking number.'];
     $this->shipmentLoad();
     $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock));
     $this->shipmentTrackMock->expects($this->once())->method('delete')->will($this->throwException(new \Exception()));
     $this->representJson($errors);
     $this->assertNull($this->controller->execute());
 }
Esempio n. 17
0
 protected function loadShipment($orderId, $shipmentId, $shipment, $tracking, $comeFrom, $returnShipment)
 {
     $valueMap = [['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipment], ['tracking', null, $tracking], ['come_from', null, $comeFrom]];
     $this->requestMock->expects($this->any())->method('getParam')->willReturnMap($valueMap);
     $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')->willReturn($returnShipment);
 }
 public function shipmentTrackingSave(\Praxigento\Odoo\Api\Data\SaleOrder\Shipment\Tracking $data)
 {
     $result = false;
     /* replicate all data in one transaction */
     $def = $this->_manTrans->begin();
     try {
         $orderIdMage = $data->getSaleOrderIdMage();
         $trackNumber = $data->getData('shipment/trackingInfo/trackingNumber');
         $shippingMethodCode = $data->getData('shipment/trackingInfo/shippingCode');
         $this->_shipmentLoader->setOrderId($orderIdMage);
         /** @var \Magento\Sales\Model\Order\Shipment $shipment */
         $shipment = $this->_shipmentLoader->load();
         if ($shipment) {
             $carrierCode = $this->_manBusCodes->getMagCodeForCarrier($shippingMethodCode);
             $title = $this->_manBusCodes->getTitleForCarrier($shippingMethodCode);
             $track = $this->_manObj->create(\Magento\Sales\Model\Order\Shipment\Track::class);
             $track->setNumber($trackNumber);
             $track->setCarrierCode($carrierCode);
             $track->setTitle($title);
             $shipment->addTrack($track);
             $shipment->register();
             $shipment->save();
             $order = $shipment->getOrder();
             $invoice = $this->_manInvoice->prepareInvoice($order);
             $invoice->register();
             $invoice->save();
             $order->save();
             $this->_manTrans->commit($def);
             $result = true;
         }
     } catch (\Exception $e) {
         $msg = 'Product replication from Odoo is failed. Error: ' . $e->getMessage();
         $this->_logger->emergency($msg);
         $traceStr = $e->getTraceAsString();
         $this->_logger->emergency($traceStr);
         throw $e;
     } finally {
         // transaction will be rolled back if commit is not done (otherwise - do nothing)
         $this->_manTrans->end($def);
     }
     return $result;
 }
Esempio n. 19
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());
 }
Esempio n. 20
0
 /**
  * Send email with shipment data to customer
  *
  * @return void
  */
 public function execute()
 {
     try {
         $shipment = $this->shipmentLoader->load($this->_request);
         if ($shipment) {
             $shipment->sendEmail(true)->setEmailSent(true)->save();
             $historyItem = $this->_objectManager->create('Magento\\Sales\\Model\\Resource\\Order\\Status\\History\\Collection')->getUnnotifiedForInstance($shipment, \Magento\Sales\Model\Order\Shipment::HISTORY_ENTITY_NAME);
             if ($historyItem) {
                 $historyItem->setIsCustomerNotified(1);
                 $historyItem->save();
             }
             $this->messageManager->addSuccess(__('You sent the shipment.'));
         }
     } catch (\Magento\Framework\Model\Exception $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addError(__('Cannot send shipment information.'));
     }
     $this->_redirect('*/*/view', array('shipment_id' => $this->getRequest()->getParam('shipment_id')));
 }
Esempio n. 21
0
 /**
  * Add comment to shipment history
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('shipment_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new \Magento\Framework\Exception\LocalizedException(__("The comment text field cannot be empty."));
         }
         $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();
         $shipment->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $this->shipmentCommentSender->send($shipment, !empty($data['is_customer_notified']), $data['comment']);
         $shipment->save();
         $resultLayout = $this->resultLayoutFactory->create();
         $resultLayout->addDefaultHandle();
         $response = $resultLayout->getLayout()->getBlock('shipment_comments')->toHtml();
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $response = ['error' => true, 'message' => $e->getMessage()];
     } catch (\Exception $e) {
         $response = ['error' => true, 'message' => __('Cannot add new comment.')];
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Esempio n. 22
0
 /**
  * Remove tracking number from shipment
  *
  * @return void
  */
 public function execute()
 {
     $trackId = $this->getRequest()->getParam('track_id');
     /** @var \Magento\Sales\Model\Order\Shipment\Track $track */
     $track = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Shipment\\Track')->load($trackId);
     if ($track->getId()) {
         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();
             if ($shipment) {
                 $track->delete();
                 $this->_view->loadLayout();
                 $this->_view->getPage()->getConfig()->getTitle()->prepend(__('Shipments'));
                 $response = $this->_view->getLayout()->getBlock('shipment_tracking')->toHtml();
             } else {
                 $response = ['error' => true, 'message' => __('We can\'t initialize shipment for delete tracking number.')];
             }
         } catch (\Exception $e) {
             $response = ['error' => true, 'message' => __('We can\'t delete tracking number.')];
         }
     } else {
         $response = ['error' => true, 'message' => __('We can\'t load track with retrieving identifier right now.')];
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Esempio n. 23
0
    /**
     * Create pdf document with information about packages
     *
     * @return ResponseInterface|void
     */
    public function executeInternal()
    {
        $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();

        if ($shipment) {
            /** @var \Zend_Pdf $pdf */
            $pdf = $this->_objectManager->create('Magento\Shipping\Model\Order\Pdf\Packaging')->getPdf($shipment);
            return $this->_fileFactory->create(
                'packingslip' . $this->_objectManager->get(
                    'Magento\Framework\Stdlib\DateTime\DateTime'
                )->date(
                    'Y-m-d_H-i-s'
                ) . '.pdf',
                $pdf->render(),
                DirectoryList::VAR_DIR,
                'application/pdf'
            );
        } else {
            $this->_forward('noroute');
        }
    }
Esempio n. 24
0
 /**
  * Run test execute method
  */
 public function testExecute()
 {
     $orderId = 1;
     $shipmentId = 1;
     $shipment = [];
     $tracking = [];
     $result = 'result-html';
     $layoutMock = $this->getMock('Magento\\Framework\\View\\Layout', ['createBlock'], [], '', false);
     $gridMock = $this->getMock('Magento\\Shipping\\Block\\Adminhtml\\Order\\Packaging\\Grid', ['setIndex', 'toHtml'], [], '', false);
     $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->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');
     $layoutMock->expects($this->once())->method('createBlock')->with('Magento\\Shipping\\Block\\Adminhtml\\Order\\Packaging\\Grid')->will($this->returnValue($gridMock));
     $this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($layoutMock));
     $this->responseMock->expects($this->once())->method('setBody')->with($result)->will($this->returnSelf());
     $this->requestMock->expects($this->at(4))->method('getParam')->with('index');
     $gridMock->expects($this->once())->method('setIndex')->will($this->returnSelf());
     $gridMock->expects($this->once())->method('toHtml')->will($this->returnValue($result));
     $this->assertNotEmpty('result-html', $this->controller->execute());
 }
 /**
  * Return grid with shipping items for Ajax request
  *
  * @return ResponseInterface
  */
 public function execute()
 {
     $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'));
     $this->shipmentLoader->load();
     return $this->getResponse()->setBody($this->_view->getLayout()->createBlock('Magento\\Shipping\\Block\\Adminhtml\\Order\\Packaging\\Grid')->setIndex($this->getRequest()->getParam('index'))->toHtml());
 }
 public function testLoadOrderId()
 {
     $this->loader->unsetData('shipment_id');
     $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['getForcedShipmentWithInvoice', 'getId', 'load', 'canShip'])->getMock();
     $this->orderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($orderMock));
     $orderMock->expects($this->once())->method('load')->with($this->loader->getOrderId())->will($this->returnSelf());
     $orderMock->expects($this->once())->method('getId')->will($this->returnValue($this->loader->getOrderId()));
     $orderMock->expects($this->any())->method('getForcedShipmentWithInvoice')->will($this->returnValue(false));
     $orderMock->expects($this->once())->method('canShip')->will($this->returnValue(true));
     $orderServiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Service\\Order')->disableOriginalConstructor()->setMethods([])->getMock();
     $shipmentModelMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment')->disableOriginalConstructor()->setMethods([])->getMock();
     $this->orderServiceFactoryMock->expects($this->once())->method('create')->with(['order' => $orderMock])->will($this->returnValue($orderServiceMock));
     $orderServiceMock->expects($this->once())->method('prepareShipment')->with($this->loader->getShipment()['items'])->will($this->returnValue($shipmentModelMock));
     $trackMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment\\Track')->disableOriginalConstructor()->setMethods([])->getMock();
     $this->trackFactoryMock->expects($this->any())->method('create')->will($this->returnValue($trackMock));
     $trackMock->expects($this->any())->method('addData')->will($this->returnValueMap([[$this->loader->getTracking()[0], $trackMock], [$this->loader->getTracking()[1], $trackMock]]));
     $shipmentModelMock->expects($this->any())->method('addTrack')->with($this->equalTo($trackMock))->will($this->returnSelf());
     $this->registryMock->expects($this->once())->method('register')->with('current_shipment', $shipmentModelMock);
     $this->assertEquals($shipmentModelMock, $this->loader->load());
 }
Esempio n. 27
0
 /**
  * @param Shipment $dataObject
  * @return \Magento\Sales\Model\Order\Shipment
  * @throws \Exception
  */
 public function getModel(Shipment $dataObject)
 {
     $this->shipmentLoader->setOrderId($dataObject->getOrderId());
     $this->shipmentLoader->setShipmentId($dataObject->getEntityId());
     $items = [];
     foreach ($dataObject->getItems() as $item) {
         $items[$item->getOrderItemId()] = $item->getQty();
     }
     $shipmentItems = ['items' => $items];
     $this->shipmentLoader->setShipment($shipmentItems);
     $this->shipmentLoader->setTracking($dataObject->getTracks());
     return $this->shipmentLoader->load();
 }
Esempio n. 28
0
 /**
  * Run test execute method (no shipment)
  */
 public function testExecuteNoShipment()
 {
     $orderId = 1;
     $shipmentId = 1;
     $shipment = [];
     $tracking = [];
     $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->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(false));
     $this->actionFlag->expects($this->once())->method('get')->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)->will($this->returnValue(true));
     $this->sessionMock->expects($this->once())->method('setIsUrlNotice')->with(true);
     $this->assertNull($this->controller->execute());
 }
Esempio n. 29
0
 /**
  * Send email with shipment data to customer
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $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();
     if ($shipment) {
         $this->_objectManager->create('Magento\\Shipping\\Model\\ShipmentNotifier')->notify($shipment);
         $shipment->save();
         $this->messageManager->addSuccess(__('You sent the shipment.'));
     }
     return $this->getDefaultResult();
 }
Esempio n. 30
0
    /**
     * Add new tracking number action
     *
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function executeInternal()
    {
        try {
            $carrier = $this->getRequest()->getPost('carrier');
            $number = $this->getRequest()->getPost('number');
            $title = $this->getRequest()->getPost('title');
            if (empty($carrier)) {
                throw new \Magento\Framework\Exception\LocalizedException(__('Please specify a carrier.'));
            }
            if (empty($number)) {
                throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a tracking number.'));
            }
            $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();
            if ($shipment) {
                $track = $this->_objectManager->create(
                    'Magento\Sales\Model\Order\Shipment\Track'
                )->setNumber(
                    $number
                )->setCarrierCode(
                    $carrier
                )->setTitle(
                    $title
                );
                $shipment->addTrack($track)->save();

                $this->_view->loadLayout();
                $this->_view->getPage()->getConfig()->getTitle()->prepend(__('Shipments'));
                $response = $this->_view->getLayout()->getBlock('shipment_tracking')->toHtml();
            } else {
                $response = [
                    'error' => true,
                    'message' => __('We can\'t initialize shipment for adding tracking number.'),
                ];
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $response = ['error' => true, 'message' => $e->getMessage()];
        } catch (\Exception $e) {
            $response = ['error' => true, 'message' => __('Cannot add tracking number.')];
        }
        if (is_array($response)) {
            $response = $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($response);
            $this->getResponse()->representJson($response);
        } else {
            $this->getResponse()->setBody($response);
        }
    }