1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © Magento, Inc. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
6
+
7
7
namespace Magento \Shipping \Controller \Adminhtml \Order \Shipment ;
8
8
9
- use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
10
- use Magento \Backend \ App \ Action ;
9
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
10
+ use Magento \Framework \ Controller \ ResultFactory ;
11
11
use Magento \Sales \Model \Order \Shipment \Validation \QuantityValidator ;
12
12
13
13
/**
@@ -48,17 +48,22 @@ class Save extends \Magento\Backend\App\Action implements HttpPostActionInterfac
48
48
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
49
49
* @param \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator
50
50
* @param \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
51
+ * @param \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface|null $shipmentValidator
51
52
*/
52
53
public function __construct (
53
54
\Magento \Backend \App \Action \Context $ context ,
54
55
\Magento \Shipping \Controller \Adminhtml \Order \ShipmentLoader $ shipmentLoader ,
55
56
\Magento \Shipping \Model \Shipping \LabelGenerator $ labelGenerator ,
56
- \Magento \Sales \Model \Order \Email \Sender \ShipmentSender $ shipmentSender
57
+ \Magento \Sales \Model \Order \Email \Sender \ShipmentSender $ shipmentSender ,
58
+ \Magento \Sales \Model \Order \Shipment \ShipmentValidatorInterface $ shipmentValidator = null
57
59
) {
60
+ parent ::__construct ($ context );
61
+
58
62
$ this ->shipmentLoader = $ shipmentLoader ;
59
63
$ this ->labelGenerator = $ labelGenerator ;
60
64
$ this ->shipmentSender = $ shipmentSender ;
61
- parent ::__construct ($ context );
65
+ $ this ->shipmentValidator = $ shipmentValidator ?: \Magento \Framework \App \ObjectManager::getInstance ()
66
+ ->get (\Magento \Sales \Model \Order \Shipment \ShipmentValidatorInterface::class);
62
67
}
63
68
64
69
/**
@@ -84,9 +89,10 @@ protected function _saveShipment($shipment)
84
89
85
90
/**
86
91
* Save shipment
92
+ *
87
93
* We can save only new shipment. Existing shipments are not editable
88
94
*
89
- * @return void
95
+ * @return \Magento\Framework\Controller\ResultInterface
90
96
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
91
97
* @SuppressWarnings(PHPMD.NPathComplexity)
92
98
*/
@@ -98,7 +104,7 @@ public function execute()
98
104
$ formKeyIsValid = $ this ->_formKeyValidator ->validate ($ this ->getRequest ());
99
105
$ isPost = $ this ->getRequest ()->isPost ();
100
106
if (!$ formKeyIsValid || !$ isPost ) {
101
- $ this ->messageManager ->addError (__ ('We can \'t save the shipment right now. ' ));
107
+ $ this ->messageManager ->addErrorMessage (__ ('We can \'t save the shipment right now. ' ));
102
108
return $ resultRedirect ->setPath ('sales/order/index ' );
103
109
}
104
110
@@ -118,8 +124,7 @@ public function execute()
118
124
$ this ->shipmentLoader ->setTracking ($ this ->getRequest ()->getParam ('tracking ' ));
119
125
$ shipment = $ this ->shipmentLoader ->load ();
120
126
if (!$ shipment ) {
121
- $ this ->_forward ('noroute ' );
122
- return ;
127
+ return $ this ->resultFactory ->create (ResultFactory::TYPE_FORWARD )->forward ('noroute ' );
123
128
}
124
129
125
130
if (!empty ($ data ['comment_text ' ])) {
@@ -132,15 +137,13 @@ public function execute()
132
137
$ shipment ->setCustomerNote ($ data ['comment_text ' ]);
133
138
$ shipment ->setCustomerNoteNotify (isset ($ data ['comment_customer_notify ' ]));
134
139
}
135
- $ validationResult = $ this ->getShipmentValidator ()
136
- ->validate ($ shipment , [QuantityValidator::class]);
140
+ $ validationResult = $ this ->shipmentValidator ->validate ($ shipment , [QuantityValidator::class]);
137
141
138
142
if ($ validationResult ->hasMessages ()) {
139
- $ this ->messageManager ->addError (
143
+ $ this ->messageManager ->addErrorMessage (
140
144
__ ("Shipment Document Validation Error(s): \n" . implode ("\n" , $ validationResult ->getMessages ()))
141
145
);
142
- $ this ->_redirect ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
143
- return ;
146
+ return $ resultRedirect ->setPath ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
144
147
}
145
148
$ shipment ->register ();
146
149
@@ -160,7 +163,7 @@ public function execute()
160
163
$ shipmentCreatedMessage = __ ('The shipment has been created. ' );
161
164
$ labelCreatedMessage = __ ('You created the shipping label. ' );
162
165
163
- $ this ->messageManager ->addSuccess (
166
+ $ this ->messageManager ->addSuccessMessage (
164
167
$ isNeedCreateLabel ? $ shipmentCreatedMessage . ' ' . $ labelCreatedMessage : $ shipmentCreatedMessage
165
168
);
166
169
$ this ->_objectManager ->get (\Magento \Backend \Model \Session::class)->getCommentText (true );
@@ -169,38 +172,23 @@ public function execute()
169
172
$ responseAjax ->setError (true );
170
173
$ responseAjax ->setMessage ($ e ->getMessage ());
171
174
} else {
172
- $ this ->messageManager ->addError ($ e ->getMessage ());
173
- $ this -> _redirect ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
175
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
176
+ return $ resultRedirect -> setPath ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
174
177
}
175
178
} catch (\Exception $ e ) {
176
179
$ this ->_objectManager ->get (\Psr \Log \LoggerInterface::class)->critical ($ e );
177
180
if ($ isNeedCreateLabel ) {
178
181
$ responseAjax ->setError (true );
179
182
$ responseAjax ->setMessage (__ ('An error occurred while creating shipping label. ' ));
180
183
} else {
181
- $ this ->messageManager ->addError (__ ('Cannot save shipment. ' ));
182
- $ this -> _redirect ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
184
+ $ this ->messageManager ->addErrorMessage (__ ('Cannot save shipment. ' ));
185
+ return $ resultRedirect -> setPath ('*/*/new ' , ['order_id ' => $ this ->getRequest ()->getParam ('order_id ' )]);
183
186
}
184
187
}
185
188
if ($ isNeedCreateLabel ) {
186
- $ this ->getResponse ()->representJson ($ responseAjax ->toJson ());
187
- } else {
188
- $ this ->_redirect ('sales/order/view ' , ['order_id ' => $ shipment ->getOrderId ()]);
189
- }
190
- }
191
-
192
- /**
193
- * @return \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface
194
- * @deprecated 100.1.1
195
- */
196
- private function getShipmentValidator ()
197
- {
198
- if ($ this ->shipmentValidator === null ) {
199
- $ this ->shipmentValidator = $ this ->_objectManager ->get (
200
- \Magento \Sales \Model \Order \Shipment \ShipmentValidatorInterface::class
201
- );
189
+ return $ this ->resultFactory ->create (ResultFactory::TYPE_JSON )->setJsonData ($ responseAjax ->toJson ());
202
190
}
203
191
204
- return $ this -> shipmentValidator ;
192
+ return $ resultRedirect -> setPath ( ' sales/order/view ' , [ ' order_id ' => $ shipment -> getOrderId ()]) ;
205
193
}
206
194
}
0 commit comments