8
8
use Macopedia \Allegro \Api \Data \CheckoutForm \LineItemInterface ;
9
9
use Macopedia \Allegro \Api \Data \CheckoutFormInterface ;
10
10
use Macopedia \Allegro \Logger \Logger ;
11
- use Macopedia \Allegro \Model \Api \ClientException ;
12
- use Macopedia \Allegro \Model \CheckoutFormRepository ;
13
11
use Magento \Framework \Api \SearchCriteriaBuilder ;
14
12
use Magento \Framework \Exception \LocalizedException ;
15
13
use Magento \Framework \Exception \NoSuchEntityException ;
16
- use Magento \Framework \Stdlib \DateTime \DateTime ;
17
14
use Magento \InventorySalesApi \Api \Data \SalesChannelInterface ;
18
15
use Magento \InventorySalesApi \Api \Data \SalesChannelInterfaceFactory ;
19
16
use Magento \InventorySalesApi \Api \Data \SalesEventInterface ;
@@ -72,9 +69,6 @@ class AllegroReservation implements AllegroReservationsInterface
72
69
/** @var SearchCriteriaBuilder */
73
70
private $ searchCriteriaBuilder ;
74
71
75
- /** @var CheckoutFormRepository */
76
- private $ checkoutFormRepository ;
77
-
78
72
/** @var Logger */
79
73
private $ logger ;
80
74
@@ -90,7 +84,6 @@ class AllegroReservation implements AllegroReservationsInterface
90
84
* @param ResourceConnection $resource
91
85
* @param Configuration $configuration
92
86
* @param SearchCriteriaBuilder $searchCriteriaBuilder
93
- * @param CheckoutFormRepository $checkoutFormRepository
94
87
* @param Logger $logger
95
88
*/
96
89
public function __construct (
@@ -104,7 +97,6 @@ public function __construct(
104
97
ResourceConnection $ resource ,
105
98
Configuration $ configuration ,
106
99
SearchCriteriaBuilder $ searchCriteriaBuilder ,
107
- CheckoutFormRepository $ checkoutFormRepository ,
108
100
Logger $ logger
109
101
) {
110
102
$ this ->productRepository = $ productRepository ;
@@ -117,7 +109,6 @@ public function __construct(
117
109
$ this ->resource = $ resource ;
118
110
$ this ->configuration = $ configuration ;
119
111
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
120
- $ this ->checkoutFormRepository = $ checkoutFormRepository ;
121
112
$ this ->logger = $ logger ;
122
113
}
123
114
@@ -132,7 +123,7 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
132
123
return ;
133
124
}
134
125
$ this ->placeReservationsForSalesEvent ->execute (
135
- $ this ->getItemsToSell ($ checkoutForm, false ),
126
+ $ this ->getItemsToReserve ($ checkoutForm ),
136
127
$ this ->getSalesChannel (),
137
128
$ this ->getSalesEvent (self ::ALLEGRO_EVENT_RESERVATION_PLACED , $ checkoutForm ->getId ())
138
129
);
@@ -146,23 +137,23 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
146
137
}
147
138
148
139
/**
149
- * @param CheckoutFormInterface $checkoutForm
150
- * @throws \Exception
140
+ * @param string $checkoutFormId
141
+ * @throws ReservationPlacingException
151
142
*/
152
- public function compensateReservation (CheckoutFormInterface $ checkoutForm ): void
143
+ public function compensateReservation (string $ checkoutFormId ): void
153
144
{
154
145
try {
155
146
if (!$ this ->configuration ->areReservationsEnabled ()) {
156
147
return ;
157
148
}
158
149
$ this ->placeReservationsForSalesEvent ->execute (
159
- $ this ->getItemsToSell ( $ checkoutForm , true ),
150
+ $ this ->getItemsToCompensate ( $ checkoutFormId ),
160
151
$ this ->getSalesChannel (),
161
- $ this ->getSalesEvent (self ::ALLEGRO_EVENT_RESERVATION_COMPENSATED , $ checkoutForm -> getId () )
152
+ $ this ->getSalesEvent (self ::ALLEGRO_EVENT_RESERVATION_COMPENSATED , $ checkoutFormId )
162
153
);
163
154
} catch (\Exception $ e ) {
164
155
throw new ReservationPlacingException (
165
- "Error while compensating reservation for order with id [ {$ checkoutForm -> getId () }] " ,
156
+ "Error while compensating reservation for order with id [ {$ checkoutFormId }] " ,
166
157
1589540303 ,
167
158
$ e
168
159
);
@@ -180,8 +171,7 @@ public function cleanOldReservations()
180
171
foreach ($ reservations as $ reservation ) {
181
172
$ checkoutFormId = $ reservation ->getCheckoutFormId ();
182
173
try {
183
- $ checkoutForm = $ this ->checkoutFormRepository ->get ($ checkoutFormId );
184
- $ this ->compensateReservation ($ checkoutForm );
174
+ $ this ->compensateReservation ($ checkoutFormId );
185
175
} catch (\Exception $ e ) {
186
176
$ this ->logger ->exception (
187
177
$ e ,
@@ -235,6 +225,23 @@ private function getSalesChannel(): SalesChannelInterface
235
225
* @throws NoSuchEntityException
236
226
*/
237
227
private function getProductsSku (string $ checkoutFormId ): array
228
+ {
229
+ $ reservations = $ this ->reservationRepository ->getByCheckoutFormId ($ checkoutFormId );
230
+ $ productsSku = [];
231
+
232
+ foreach ($ reservations as $ reservation ) {
233
+ array_push ($ productsSku , $ reservation ->getSku ());
234
+ }
235
+
236
+ return $ productsSku ;
237
+ }
238
+
239
+ /**
240
+ * @param string $checkoutFormId
241
+ * @return array
242
+ * @throws NoSuchEntityException
243
+ */
244
+ private function getProductsData (string $ checkoutFormId ): array
238
245
{
239
246
$ reservations = $ this ->reservationRepository ->getByCheckoutFormId ($ checkoutFormId );
240
247
@@ -250,22 +257,22 @@ private function getProductsSku(string $checkoutFormId): array
250
257
251
258
$ query = $ connection
252
259
->select ()
253
- ->from ($ originalReservations , ['sku ' ])
260
+ ->from ($ originalReservations )
261
+ ->columns (['sku ' , 'quantity ' ])
254
262
->where ('reservation_id IN (?) ' , $ reservationsIds );
255
263
256
- return $ connection ->fetchCol ($ query );
264
+ return $ connection ->fetchAll ($ query );
257
265
}
258
266
259
267
/**
260
268
* @param CheckoutFormInterface $checkoutForm
261
- * @param bool $compensate
262
269
* @return array
263
270
* @throws CreatorItemsException
264
271
* @throws NoSuchEntityException
265
272
*/
266
- private function getItemsToSell (CheckoutFormInterface $ checkoutForm, bool $ compensate = false ): array
273
+ private function getItemsToReserve (CheckoutFormInterface $ checkoutForm ): array
267
274
{
268
- $ itemsToSell = [];
275
+ $ itemsToReserve = [];
269
276
$ productsSku = $ this ->getProductsSku ($ checkoutForm ->getId ());
270
277
271
278
/** @var LineItemInterface $lineItem */
@@ -278,14 +285,34 @@ private function getItemsToSell(CheckoutFormInterface $checkoutForm, bool $compe
278
285
throw new CreatorItemsException ("Product for requested offer id {$ offerId } does not exist " );
279
286
}
280
287
281
- if (in_array ($ sku , $ productsSku ) === $ compensate ) {
282
- $ itemsToSell [] = $ this ->itemsToSellFactory ->create ([
288
+ if (! in_array ($ sku , $ productsSku )) {
289
+ $ itemsToReserve [] = $ this ->itemsToSellFactory ->create ([
283
290
'sku ' => $ sku ,
284
- 'qty ' => $ compensate ? ( float ) $ lineItem -> getQty () : -(float )$ lineItem ->getQty ()
291
+ 'qty ' => -(float )$ lineItem ->getQty ()
285
292
]);
286
293
}
287
294
}
288
295
289
- return $ itemsToSell ;
296
+ return $ itemsToReserve ;
297
+ }
298
+
299
+ /**
300
+ * @param string $checkoutFormId
301
+ * @return array
302
+ * @throws NoSuchEntityException
303
+ */
304
+ private function getItemsToCompensate (string $ checkoutFormId )
305
+ {
306
+ $ products = $ this ->getProductsData ($ checkoutFormId );
307
+ $ itemsToCompensate = [];
308
+
309
+ foreach ($ products as $ product ) {
310
+ $ itemsToCompensate [] = $ this ->itemsToSellFactory ->create ([
311
+ 'sku ' => $ product ['sku ' ],
312
+ 'qty ' => -(float )$ product ['quantity ' ]
313
+ ]);
314
+ }
315
+
316
+ return $ itemsToCompensate ;
290
317
}
291
318
}
0 commit comments