|
| 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 | +} |
0 commit comments