NewAction.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Backend\App\Action\Context;
  7. use Magento\Framework\Controller\ResultFactory;
  8. use Magento\Framework\Controller\ResultInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Sales\Model\Order;
  11. use Magento\Ui\Component\MassAction\Filter;
  12. use Temando\Shipping\Controller\Adminhtml\Activation\AbstractRegisteredAction;
  13. use Temando\Shipping\Model\BatchProviderInterface;
  14. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  15. use Temando\Shipping\Model\ResourceModel\Batch\OrderCollection;
  16. use Temando\Shipping\Model\ResourceModel\Batch\OrderCollectionFactory;
  17. use Temando\Shipping\Model\Shipping\Carrier;
  18. /**
  19. * Temando Add New Batch Action for MassCreate Action
  20. *
  21. * @package Temando\Shipping\Controller
  22. * @author Rhodri Davies <rhodri.davies@temando.com>
  23. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * @link http://www.temando.com/
  26. */
  27. class NewAction extends AbstractRegisteredAction
  28. {
  29. const ADMIN_RESOURCE = 'Temando_Shipping::batches';
  30. /**
  31. * @var OrderCollectionFactory
  32. */
  33. private $collectionFactory;
  34. /**
  35. * @var Filter
  36. */
  37. private $massActionFilter;
  38. /**
  39. * @var ModuleConfigInterface
  40. */
  41. private $config;
  42. /**
  43. * @var BatchProviderInterface
  44. */
  45. private $batchProvider;
  46. /**
  47. * @param Context $context
  48. * @param ModuleConfigInterface $config
  49. * @param BatchProviderInterface $batchProvider
  50. * @param OrderCollectionFactory $collectionFactory
  51. * @param Filter $massActionFilter
  52. */
  53. public function __construct(
  54. Context $context,
  55. ModuleConfigInterface $config,
  56. BatchProviderInterface $batchProvider,
  57. OrderCollectionFactory $collectionFactory,
  58. Filter $massActionFilter
  59. ) {
  60. $this->batchProvider = $batchProvider;
  61. $this->collectionFactory = $collectionFactory;
  62. $this->massActionFilter = $massActionFilter;
  63. $this->config = $config;
  64. parent::__construct($context, $config);
  65. }
  66. /**
  67. * Prepare order collection as indicated via request params. After that it renders the BatchCreate component.
  68. * The orders listing mass action forward here.
  69. * For the batch listing action a different new controller might be needed
  70. *
  71. * - On success, creating Batch and redirecting to the batch grid page.
  72. * - If no orders ready for processing found, redirect back with error message
  73. *
  74. * @return ResultInterface
  75. * @throws LocalizedException
  76. */
  77. public function execute()
  78. {
  79. $redirectRoute = 'sales/order/index';
  80. $collection = $this->collectionFactory->create();
  81. /** @var OrderCollection $orderCollection */
  82. $orderCollection = $this->massActionFilter->getCollection($collection);
  83. $orderCollection->addCarrierFilter(Carrier::CODE);
  84. $orderCollection->addCanShipFilter();
  85. $orders = array_filter($orderCollection->getItems(), function (Order $order) {
  86. return $order->canShip();
  87. });
  88. $hasError = false;
  89. if (empty($orders)) {
  90. $this->messageManager->addErrorMessage('None of the orders you selected are valid for batch processing.');
  91. $hasError = true;
  92. }
  93. if ((!$this->config->isSyncEnabled()) || (!$this->config->isSyncShipmentEnabled())) {
  94. $this->messageManager->addErrorMessage('You must enable Event Sync for shipments to use batch processing.');
  95. $hasError = true;
  96. }
  97. if ($hasError) {
  98. $resultRedirect = $this->resultRedirectFactory->create();
  99. $resultRedirect->setPath($redirectRoute);
  100. return $resultRedirect;
  101. }
  102. $this->batchProvider->setOrders($orders);
  103. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  104. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  105. $resultPage->setActiveMenu('Temando_Shipping::batches');
  106. $resultPage->getConfig()->getTitle()->prepend(__('Batches'));
  107. $resultPage->getConfig()->getTitle()->prepend(__('Create New Batch'));
  108. $resultPage->addBreadcrumb(__('Batches'), __('Batches'), $this->getUrl('temando/batch'));
  109. $resultPage->addBreadcrumb(__('Create New Batch'), __('Create New Batch'));
  110. return $resultPage;
  111. }
  112. }