From 422c98b712a037efa0b94e6b9a5e43da576ecdb3 Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Mon, 29 Mar 2021 16:05:13 +0100 Subject: [PATCH] Created Bulk ship actions --- Controller/Adminhtml/Order/MassOrderShip.php | 112 +++++++++++++++++- Ui/Component/Action/AddParams.php | 33 ++++++ etc/adminhtml/routes.xml | 8 ++ .../ui_component/sales_order_grid.xml | 16 +-- 4 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 Ui/Component/Action/AddParams.php create mode 100644 etc/adminhtml/routes.xml diff --git a/Controller/Adminhtml/Order/MassOrderShip.php b/Controller/Adminhtml/Order/MassOrderShip.php index 30d74d2..2dabf97 100644 --- a/Controller/Adminhtml/Order/MassOrderShip.php +++ b/Controller/Adminhtml/Order/MassOrderShip.php @@ -1 +1,111 @@ -test \ No newline at end of file +messageManager = $messageManager; + $this->orderModel = $orderModel; + $this->convertOrder = $convertOrder; + $this->shipmentNotifier = $shipmentNotifier; + } + + public function execute() + { + $resultRedirect = $this->resultRedirectFactory->create(); + + $ordersId = $this->getRequest()->getParam('selected'); + $notify = $this->getRequest()->getParam('notify'); + + if (!empty($ordersId)) { + // Ship Order + foreach ($ordersId as $orderId) { + + $order = $this->orderModel->loadByAttribute('entity_id', $orderId); + if ($order->canShip()) { + + $convertOrder = $this->convertOrder; + $shipment = $convertOrder->toShipment($order); + + foreach ($order->getAllItems() AS $orderItem) { + // Check if order item has qty to ship or is virtual + if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) { + continue; + } + $qtyShipped = $orderItem->getQtyToShip(); + // Create shipment item with qty + $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped); + // Add shipment item to shipment + $shipment->addItem($shipmentItem); + } + + // Register shipment + $shipment->register(); + $shipment->getOrder()->setIsInProcess(true); + + try { + // Save created shipment and order + $shipment->save(); + $shipment->getOrder()->save(); + + // Send email + if ($notify) { + $this->shipmentNotifier->notify($shipment); + $shipment->save(); + } + $this->messageManager->addSuccess(__("Shipment Succesfully Generated for order: #".$order->getIncrementId())); + } catch (\Exception $e) { + $this->messageManager->addError(__('Cannot ship order'. $e->getMessage())); + } + + } else { + $this->messageManager->addError(__("Cannot ship order, becuase It's already created or something went wrong")); + } + } + } + return $resultRedirect->setPath('sales/order/index', [], ['error' => true]); + } +} diff --git a/Ui/Component/Action/AddParams.php b/Ui/Component/Action/AddParams.php new file mode 100644 index 0000000..f7003d8 --- /dev/null +++ b/Ui/Component/Action/AddParams.php @@ -0,0 +1,33 @@ +urlBuilder = $urlBuilder; + } + + public function prepare() + { + parent::prepare(); + + $config = $this->getConfiguration(); + + $params = array('notify' => '1'); + + $config['url'] = $this->urlBuilder->getUrl('shipbulkactions/order/massordership/', $params); + + $this->setData('config', $config); + } +} diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..65cc94b --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/view/adminhtml/ui_component/sales_order_grid.xml b/view/adminhtml/ui_component/sales_order_grid.xml index 182b03f..fc8382f 100644 --- a/view/adminhtml/ui_component/sales_order_grid.xml +++ b/view/adminhtml/ui_component/sales_order_grid.xml @@ -2,18 +2,18 @@ - + - - set_order_to_in_production - + + ship_and_notify + - + - - send_shipment_email_and_complete_order - + + ship_and_dont_notify +