Skip to content

Commit bd16c94

Browse files
committed
Initial commit
0 parents  commit bd16c94

File tree

15 files changed

+615
-0
lines changed

15 files changed

+615
-0
lines changed

Block/Adminhtml/Confirm.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Block\Adminhtml;
10+
11+
use Magento\Backend\Block\Widget\Form\Container;
12+
13+
class Confirm extends Container
14+
{
15+
/**
16+
* Constructor
17+
*
18+
* @return void
19+
*/
20+
protected function _construct()
21+
{
22+
$this->_controller = 'adminhtml';
23+
$this->_mode = 'confirm';
24+
$this->_blockGroup = 'Netresearch_InteractiveBatchProcessing';
25+
26+
parent::_construct();
27+
}
28+
29+
public function getBackUrl()
30+
{
31+
return $this->getUrl('sales/order/index');
32+
}
33+
}

Block/Adminhtml/Confirm/Form.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Block\Adminhtml\Confirm;
10+
11+
use Magento\Backend\Block\Template\Context;
12+
use Magento\Backend\Block\Widget\Form\Generic;
13+
use Magento\Framework\Data\FormFactory;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Registry;
16+
use Magento\Sales\Api\Data\OrderItemInterface;
17+
use Magento\Sales\Model\Order\Address\Renderer as AddressRenderer;
18+
use Netresearch\InteractiveBatchProcessing\Model\OrderProvider;
19+
use Netresearch\ShippingCore\Api\BulkShipment\Interactive\InputsProviderInterface;
20+
use Netresearch\ShippingCore\Api\Data\ShippingSettings\ShippingOption\OptionInterface;
21+
use Netresearch\ShippingCore\Model\ShippingSettings\ShippingOption\Codes;
22+
23+
class Form extends Generic
24+
{
25+
/**
26+
* @var OrderProvider
27+
*/
28+
private $orderProvider;
29+
30+
/**
31+
* @var AddressRenderer
32+
*/
33+
private $addressRenderer;
34+
35+
/**
36+
* @var InputsProviderInterface
37+
*/
38+
private $inputsProvider;
39+
40+
public function __construct(
41+
Context $context,
42+
Registry $registry,
43+
FormFactory $formFactory,
44+
OrderProvider $orderProvider,
45+
AddressRenderer $addressRenderer,
46+
InputsProviderInterface $inputsProvider,
47+
array $data = []
48+
) {
49+
$this->orderProvider = $orderProvider;
50+
$this->addressRenderer = $addressRenderer;
51+
$this->inputsProvider = $inputsProvider;
52+
53+
parent::__construct($context, $registry, $formFactory, $data);
54+
}
55+
56+
/**
57+
* @throws LocalizedException
58+
*/
59+
protected function _prepareForm(): self
60+
{
61+
$orders = $this->orderProvider->getOrders();
62+
$form = $this->_formFactory->create(['data' => ['id' => 'edit_form', 'method' => 'post']]);
63+
64+
foreach ($orders as $orderId => $order) {
65+
if ($order->getIsVirtual()) {
66+
continue;
67+
}
68+
69+
$shippingAddress = $order->getShippingAddress();
70+
$form->addField(
71+
"order-$orderId-address",
72+
'note',
73+
[
74+
'name' => "receiver[$orderId][address]",
75+
'label' => __('Destination Address'),
76+
'text' => $this->addressRenderer->format($shippingAddress, 'html'),
77+
]
78+
);
79+
80+
// todo(nr): add item renderer: collect shippable items, display summary (name, sku, weight?)
81+
$skus = array_map(
82+
function (OrderItemInterface $orderItem) {
83+
return sprintf('%s (%s)', $orderItem->getName(), $orderItem->getSku());
84+
},
85+
$order->getItems()
86+
);
87+
$form->addField(
88+
"order-$orderId-items",
89+
'note',
90+
[
91+
'name' => "order[$orderId][items]",
92+
'label' => __('Items to Ship'),
93+
'text' => implode(', ', $skus),
94+
]
95+
);
96+
97+
$optionCode = Codes::PACKAGE_OPTION_DETAILS;
98+
99+
$input = $this->inputsProvider->getInput($order, $optionCode, Codes::PACKAGE_INPUT_PRODUCT_CODE);
100+
$productOptions = array_combine(
101+
array_map(function (OptionInterface $option) { return $option->getValue(); }, $input->getOptions()),
102+
array_map(function (OptionInterface $option) { return $option->getLabel(); }, $input->getOptions())
103+
);
104+
$form->addField(
105+
sprintf('order-%d-%s', $orderId, $input->getCode()),
106+
'select',
107+
[
108+
'name' => "inputs[{$order->getEntityId()}][$optionCode.{$input->getCode()}]",
109+
'label' => $input->getLabel(),
110+
'options' => $productOptions,
111+
'value' => $input->getDefaultValue(),
112+
]
113+
);
114+
115+
$input = $this->inputsProvider->getInput($order, $optionCode, Codes::PACKAGE_INPUT_PACKAGING_ID);
116+
if ($input) {
117+
$packagingOptions = array_combine(
118+
array_map(function (OptionInterface $option) { return $option->getValue(); }, $input->getOptions()),
119+
array_map(function (OptionInterface $option) { return $option->getLabel(); }, $input->getOptions())
120+
);
121+
$form->addField(
122+
sprintf('order-%d-%s', $orderId, $input->getCode()),
123+
'select',
124+
[
125+
'name' => "inputs[{$order->getEntityId()}][$optionCode.{$input->getCode()}]",
126+
'label' => $input->getLabel(),
127+
'options' => $packagingOptions,
128+
'value' => $input->getDefaultValue(),
129+
]
130+
);
131+
}
132+
}
133+
134+
$form->setAction($this->getUrl('nrshipping/shipment/submit'));
135+
$form->setUseContainer(true);
136+
$this->setForm($form);
137+
138+
// todo(nr): render a table row for each order. use another form template, or override \Magento\Framework\Data\Form::toHtml
139+
return parent::_prepareForm();
140+
}
141+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Controller\Adminhtml\Shipment;
10+
11+
use Magento\Backend\App\Action;
12+
use Magento\Backend\App\Action\Context;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\Request\Http;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
17+
use Magento\Ui\Component\MassAction\Filter;
18+
use Netresearch\InteractiveBatchProcessing\Model\OrderProvider;
19+
20+
/**
21+
* @method Http getRequest()
22+
*/
23+
class Interactive extends Action implements HttpPostActionInterface
24+
{
25+
/**
26+
* @var CollectionFactory
27+
*/
28+
private $collectionFactory;
29+
30+
/**
31+
* @var Filter
32+
*/
33+
private $filter;
34+
35+
/**
36+
* @var OrderProvider
37+
*/
38+
private $orderProvider;
39+
40+
public function __construct(
41+
Context $context,
42+
CollectionFactory $collectionFactory,
43+
Filter $filter,
44+
OrderProvider $orderProvider
45+
) {
46+
$this->collectionFactory = $collectionFactory;
47+
$this->filter = $filter;
48+
$this->orderProvider = $orderProvider;
49+
50+
parent::__construct($context);
51+
}
52+
53+
/**
54+
* @throws LocalizedException
55+
*/
56+
public function execute()
57+
{
58+
$this->_view->loadLayout();
59+
$this->_setActiveMenu('Magento_Sales::sales_order');
60+
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Bulk Shipment'));
61+
62+
// todo(nr): filter applicable orders (label status, carrier code)
63+
// @see \Netresearch\ShippingCore\Model\BulkShipment\BulkShipmentManagement::createShipments
64+
$orderCollection = $this->filter->getCollection($this->collectionFactory->create());
65+
66+
// the "Create New Order" UI button gets added by using the UI filter above…
67+
$this->_view->getLayout()->unsetElement('container-sales_order_grid-add');
68+
69+
// todo(nr): if order collection is empty, redirect to grid.
70+
$this->orderProvider->setOrders($orderCollection->getItems());
71+
72+
return $this->_view->getPage();
73+
}
74+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Controller\Adminhtml\Shipment;
10+
11+
use Magento\Backend\App\Action;
12+
use Magento\Backend\App\Action\Context;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\Request\Http;
15+
use Netresearch\InteractiveBatchProcessing\Model\InputValuesProvider;
16+
17+
/**
18+
* @method Http getRequest()
19+
*/
20+
class Submit extends Action implements HttpPostActionInterface
21+
{
22+
/**
23+
* @var InputValuesProvider
24+
*/
25+
private $inputValuesProvider;
26+
27+
public function __construct(Context $context, InputValuesProvider $inputValuesProvider)
28+
{
29+
$this->inputValuesProvider = $inputValuesProvider;
30+
31+
parent::__construct($context);
32+
}
33+
34+
public function execute()
35+
{
36+
$inputValues = $this->getRequest()->getParam('inputs', []);
37+
foreach ($inputValues as $orderId => $orderInputValues) {
38+
$this->inputValuesProvider->setInputValues((int) $orderId, $orderInputValues);
39+
}
40+
41+
$this->_forward(
42+
'autocreate',
43+
'shipment',
44+
'nrshipping',
45+
[
46+
'selected' => array_keys($inputValues),
47+
'namespace' => 'sales_order_grid',
48+
]
49+
);
50+
}
51+
}

Model/InputValuesProvider.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Model;
10+
11+
// fixme(nr): find a better name
12+
class InputValuesProvider
13+
{
14+
/**
15+
* @var string[][]
16+
*/
17+
private $inputValues = [];
18+
19+
/**
20+
* @return string[]
21+
*/
22+
public function getInputValues(int $orderId): array
23+
{
24+
return $this->inputValues[$orderId] ?? [];
25+
}
26+
27+
/**
28+
* @param int $orderId
29+
* @param string[] $inputValues
30+
*/
31+
public function setInputValues(int $orderId, array $inputValues): void
32+
{
33+
$this->inputValues[$orderId] = $inputValues;
34+
}
35+
}

Model/OrderProvider.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* See LICENSE.md for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Netresearch\InteractiveBatchProcessing\Model;
10+
11+
use Magento\Sales\Api\Data\OrderInterface;
12+
13+
class OrderProvider
14+
{
15+
/**
16+
* @var OrderInterface[]
17+
*/
18+
private $orders = [];
19+
20+
/**
21+
* @return OrderInterface[]
22+
*/
23+
public function getOrders(): array
24+
{
25+
return $this->orders;
26+
}
27+
28+
/**
29+
* @param OrderInterface[] $orders
30+
*/
31+
public function setOrders(array $orders): void
32+
{
33+
$this->orders = $orders;
34+
}
35+
}

0 commit comments

Comments
 (0)