5
5
*/
6
6
namespace Magento \Sales \Model ;
7
7
8
+ use Magento \Framework \App \ObjectManager ;
8
9
use Magento \Framework \App \ResourceConnection ;
9
10
use Magento \Sales \Api \OrderRepositoryInterface ;
10
11
use Magento \Sales \Api \ShipmentRepositoryInterface ;
@@ -75,6 +76,11 @@ class ShipOrder implements ShipOrderInterface
75
76
*/
76
77
private $ orderRegistrar ;
77
78
79
+ /**
80
+ * @var OrderMutexInterface
81
+ */
82
+ private $ orderMutex ;
83
+
78
84
/**
79
85
* @param ResourceConnection $resourceConnection
80
86
* @param OrderRepositoryInterface $orderRepository
@@ -86,6 +92,7 @@ class ShipOrder implements ShipOrderInterface
86
92
* @param NotifierInterface $notifierInterface
87
93
* @param OrderRegistrarInterface $orderRegistrar
88
94
* @param LoggerInterface $logger
95
+ * @param OrderMutex|null $orderMutex
89
96
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
90
97
*/
91
98
public function __construct (
@@ -98,7 +105,8 @@ public function __construct(
98
105
ShipOrderValidator $ shipOrderValidator ,
99
106
NotifierInterface $ notifierInterface ,
100
107
OrderRegistrarInterface $ orderRegistrar ,
101
- LoggerInterface $ logger
108
+ LoggerInterface $ logger ,
109
+ ?OrderMutexInterface $ orderMutex = null
102
110
) {
103
111
$ this ->resourceConnection = $ resourceConnection ;
104
112
$ this ->orderRepository = $ orderRepository ;
@@ -110,6 +118,7 @@ public function __construct(
110
118
$ this ->notifierInterface = $ notifierInterface ;
111
119
$ this ->logger = $ logger ;
112
120
$ this ->orderRegistrar = $ orderRegistrar ;
121
+ $ this ->orderMutex = $ orderMutex ?: ObjectManager::getInstance ()->get (OrderMutexInterface::class);
113
122
}
114
123
115
124
/**
@@ -139,8 +148,53 @@ public function execute(
139
148
array $ tracks = [],
140
149
array $ packages = [],
141
150
\Magento \Sales \Api \Data \ShipmentCreationArgumentsInterface $ arguments = null
151
+ )
152
+ {
153
+ return $ this ->orderMutex ->execute (
154
+ (int )$ orderId ,
155
+ \Closure::fromCallable ([$ this , 'createShipment ' ]),
156
+ [
157
+ $ orderId ,
158
+ $ items ,
159
+ $ notify ,
160
+ $ appendComment ,
161
+ $ comment ,
162
+ $ tracks ,
163
+ $ packages ,
164
+ $ arguments
165
+ ]
166
+ );
167
+ }
168
+
169
+ /**
170
+ * Creates shipment for provided order ID
171
+ *
172
+ * @param int $orderId
173
+ * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items
174
+ * @param bool $notify
175
+ * @param bool $appendComment
176
+ * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment
177
+ * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks
178
+ * @param \Magento\Sales\Api\Data\ShipmentPackageCreationInterface[] $packages
179
+ * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments
180
+ * @return int
181
+ * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
182
+ * @throws \Magento\Sales\Api\Exception\CouldNotShipExceptionInterface
183
+ * @throws \Magento\Framework\Exception\InputException
184
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
185
+ * @throws \DomainException
186
+ * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
187
+ */
188
+ private function createShipment (
189
+ $ orderId ,
190
+ array $ items = [],
191
+ $ notify = false ,
192
+ $ appendComment = false ,
193
+ \Magento \Sales \Api \Data \ShipmentCommentCreationInterface $ comment = null ,
194
+ array $ tracks = [],
195
+ array $ packages = [],
196
+ \Magento \Sales \Api \Data \ShipmentCreationArgumentsInterface $ arguments = null
142
197
) {
143
- $ connection = $ this ->resourceConnection ->getConnection ('sales ' );
144
198
$ order = $ this ->orderRepository ->get ($ orderId );
145
199
$ shipment = $ this ->shipmentDocumentFactory ->create (
146
200
$ order ,
@@ -166,9 +220,20 @@ public function execute(
166
220
__ ("Shipment Document Validation Error(s): \n" . implode ("\n" , $ validationMessages ->getMessages ()))
167
221
);
168
222
}
169
- $ connection ->beginTransaction ();
170
223
try {
171
224
$ this ->orderRegistrar ->register ($ order , $ shipment );
225
+ /*foreach ($shipment->getItems() as $item) {
226
+ if ($item->getQty() > 0) {
227
+ $orderItem = $item->getOrderItem();
228
+ }
229
+ $orderItem = $item->getOrderItem();
230
+ $orderItem->setQtyShipped( $item->getQty() );
231
+ $orderItem->setQtyShipped( $orderItem->getQtyShipped() + $item->getQty() );
232
+ $orderItem->save();
233
+ }
234
+ $order = $shipment->getOrder()->load( $shipment->getOrder()->getId() );
235
+ $order->save();*/
236
+
172
237
$ shipment = $ this ->shipmentRepository ->save ($ shipment );
173
238
if ($ order ->getState () === Order::STATE_NEW ) {
174
239
$ order ->setState (
@@ -177,10 +242,8 @@ public function execute(
177
242
$ order ->setStatus ($ this ->config ->getStateDefaultStatus ($ order ->getState ()));
178
243
}
179
244
$ this ->orderRepository ->save ($ order );
180
- $ connection ->commit ();
181
245
} catch (\Exception $ e ) {
182
246
$ this ->logger ->critical ($ e );
183
- $ connection ->rollBack ();
184
247
throw new \Magento \Sales \Exception \CouldNotShipException (
185
248
__ ('Could not save a shipment, see error log for details ' )
186
249
);
0 commit comments