MassPrint.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Pickup;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\App\Response\Http\FileFactory;
  10. use Magento\Framework\App\ResponseInterface;
  11. use Magento\Framework\Controller\Result\Redirect;
  12. use Magento\Framework\Stdlib\DateTime\DateTime;
  13. use Temando\Shipping\Model\Pickup;
  14. use Temando\Shipping\Model\Pickup\Pdf\PickupPdfFactory;
  15. use Temando\Shipping\Model\Pickup\PickupLoader;
  16. use Temando\Shipping\Model\PickupInterface;
  17. use Temando\Shipping\Model\PickupProviderInterface;
  18. use Temando\Shipping\Model\ResourceModel\Pickup\Grid\Collection;
  19. use Temando\Shipping\Model\ResourceModel\Pickup\Grid\CollectionFactory;
  20. use Temando\Shipping\Ui\Component\MassAction\Filter;
  21. /**
  22. * Temando Mass Print Action
  23. *
  24. * @package Temando\Shipping\Controller
  25. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  26. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. * @link https://www.temando.com/
  28. */
  29. class MassPrint extends Action
  30. {
  31. /**
  32. * Authorization level
  33. */
  34. const ADMIN_RESOURCE = 'Temando_Shipping::pickups';
  35. /**
  36. * @var CollectionFactory
  37. */
  38. private $collectionFactory;
  39. /**
  40. * @var Filter
  41. */
  42. private $filter;
  43. /**
  44. * @var PickupLoader
  45. */
  46. private $pickupLoader;
  47. /**
  48. * @var PickupProviderInterface
  49. */
  50. private $pickupProvider;
  51. /**
  52. * @var FileFactory
  53. */
  54. private $fileFactory;
  55. /**
  56. * @var DateTime
  57. */
  58. private $dateTime;
  59. /**
  60. * @var PickupPdfFactory
  61. */
  62. private $pickupPdfFactory;
  63. /**
  64. * @param Context $context
  65. * @param CollectionFactory $collectionFactory
  66. * @param Filter $filter
  67. * @param PickupLoader $pickupLoader
  68. * @param PickupProviderInterface $pickupProvider
  69. * @param FileFactory $fileFactory
  70. * @param DateTime $dateTime
  71. * @param PickupPdfFactory $pickupPdfFactory
  72. */
  73. public function __construct(
  74. Context $context,
  75. CollectionFactory $collectionFactory,
  76. Filter $filter,
  77. PickupLoader $pickupLoader,
  78. PickupProviderInterface $pickupProvider,
  79. FileFactory $fileFactory,
  80. DateTime $dateTime,
  81. PickupPdfFactory $pickupPdfFactory
  82. ) {
  83. $this->collectionFactory = $collectionFactory;
  84. $this->filter = $filter;
  85. $this->pickupLoader = $pickupLoader;
  86. $this->pickupProvider = $pickupProvider;
  87. $this->fileFactory = $fileFactory;
  88. $this->dateTime = $dateTime;
  89. $this->pickupPdfFactory = $pickupPdfFactory;
  90. parent::__construct($context);
  91. }
  92. /**
  93. * @return ResponseInterface|Redirect
  94. */
  95. public function execute()
  96. {
  97. $selected = $this->getRequest()->getParam(\Magento\Ui\Component\MassAction\Filter::SELECTED_PARAM, []);
  98. $excluded = $this->getRequest()->getParam(\Magento\Ui\Component\MassAction\Filter::EXCLUDED_PARAM, []);
  99. if ($excluded === 'false') {
  100. $excluded = [];
  101. }
  102. $pickupCollection = $this->collectionFactory->create();
  103. $pickupCollection->setItemObjectClass(Pickup::class);
  104. $pickupIds = $this->filter->getPickupIds($pickupCollection, $selected, $excluded);
  105. try {
  106. $downloadResponse = $this->createPackagingSlips($pickupCollection, $pickupIds);
  107. return $downloadResponse;
  108. } catch (\InvalidArgumentException $e) {
  109. $this->messageManager->addErrorMessage(__('There are no packing slips related to selected pickups.'));
  110. } catch (\Exception $e) {
  111. $this->messageManager->addErrorMessage(__('There was an error creating package slip pdf.'));
  112. }
  113. $redirect = $this->resultRedirectFactory->create();
  114. $redirect->setPath($this->_redirect->getRefererUrl());
  115. return $redirect;
  116. }
  117. /**
  118. * @param Collection $pickupCollection
  119. * @param string[] $pickupIds
  120. *
  121. * @return ResponseInterface|Redirect
  122. * @throws \InvalidArgumentException
  123. * @throws \Exception
  124. */
  125. private function createPackagingSlips($pickupCollection, $pickupIds)
  126. {
  127. $pickups = $pickupCollection->getItems();
  128. $labelsContent = array_reduce($pickups, function (array $carry, PickupInterface $pickup) use ($pickupIds) {
  129. if (!in_array($pickup->getPickupId(), $pickupIds)) {
  130. return $carry;
  131. }
  132. $this->pickupLoader->register(
  133. [$pickup->getPickupId() => $pickup],
  134. (int)$pickup->getSalesOrderId(),
  135. (string)$pickup->getPickupId()
  136. );
  137. $pickup = $this->pickupProvider->getPickup();
  138. $order = $this->pickupProvider->getOrder();
  139. $pickupPdf = $this->pickupPdfFactory->create(
  140. ['data' => ['order' => $order, 'pickup' => $pickup, 'pickups' => [$pickup]]]
  141. );
  142. try {
  143. $carry[] = $pickupPdf->getPdf();
  144. return $carry;
  145. } catch (\Zend_Pdf_Exception $exception) {
  146. return $carry;
  147. }
  148. }, []);
  149. if (empty($labelsContent)) {
  150. throw new \InvalidArgumentException('Please select pickup IDs.');
  151. }
  152. $outputPdf = new \Zend_Pdf();
  153. $pages = array_reduce($labelsContent, function ($carry, \Zend_Pdf $pdf) {
  154. foreach ($pdf->pages as $page) {
  155. $carry[] = $page;
  156. }
  157. return $carry;
  158. }, []);
  159. $outputPdf->pages = $pages;
  160. return $this->fileFactory->create(
  161. sprintf('packingslips-%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
  162. $outputPdf->render(),
  163. DirectoryList::VAR_DIR,
  164. 'application/pdf'
  165. );
  166. }
  167. }