PrintPackageSlips.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Batch;
  6. use Magento\Framework\App\Filesystem\DirectoryList;
  7. use Magento\Framework\App\ResponseInterface;
  8. use Magento\Framework\Controller\Result\Redirect;
  9. use Magento\Sales\Controller\Adminhtml\Order\Pdfshipments;
  10. /**
  11. * Temando Batch PrintPackageSlips Action
  12. *
  13. * @package Temando\Shipping\Controller
  14. * @author Sebastian Ertner <sebastian.ertner@temando.com>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class PrintPackageSlips extends Pdfshipments
  19. {
  20. /**
  21. * Authorization level
  22. */
  23. const ADMIN_RESOURCE = 'Temando_Shipping::batches';
  24. /**
  25. * Check ACL.
  26. *
  27. * @return bool
  28. */
  29. protected function _isAllowed()
  30. {
  31. return (
  32. $this->_authorization->isAllowed(static::ADMIN_RESOURCE) &&
  33. $this->_authorization->isAllowed('Magento_Sales::ship') &&
  34. $this->_authorization->isAllowed('Magento_Sales::shipment')
  35. );
  36. }
  37. /**
  38. * Execute action.
  39. *
  40. * @return ResponseInterface|Redirect
  41. */
  42. public function execute()
  43. {
  44. $orderIds = $this->getRequest()->getParam('order_ids');
  45. $orderIds = explode(',', $orderIds);
  46. return $this->packageSlipsMassAction($orderIds);
  47. }
  48. /**
  49. * Prepare download response.
  50. *
  51. * @param int[] $orderIds
  52. * @return ResponseInterface|Redirect
  53. */
  54. private function packageSlipsMassAction(array $orderIds)
  55. {
  56. $shipmentsCollection = $this->shipmentCollectionFactory
  57. ->create()
  58. ->setOrderFilter(['in' => $orderIds]);
  59. if (!$shipmentsCollection->getSize()) {
  60. $this->messageManager->addErrorMessage(__('There are no printable documents related to selected orders.'));
  61. return $this->resultRedirectFactory->create()->setPath($this->_redirect->getRefererUrl());
  62. }
  63. try {
  64. $fileName = sprintf(
  65. 'packingslip-%s-%s.pdf',
  66. $this->getRequest()->getParam('batch_id'),
  67. $this->dateTime->date('Y-m-d_H-i-s')
  68. );
  69. $response = $this->fileFactory->create(
  70. $fileName,
  71. $this->pdfShipment->getPdf($shipmentsCollection->getItems())->render(),
  72. DirectoryList::VAR_DIR,
  73. 'application/pdf'
  74. );
  75. } catch (\Exception $e) {
  76. $this->messageManager->addErrorMessage(__('There was a error creating package slip pdf.'));
  77. return $this->resultRedirectFactory->create()->setPath($this->_redirect->getRefererUrl());
  78. }
  79. return $response;
  80. }
  81. }