Skip to content

Commit fee17d2

Browse files
mstrzyzewskicieslix
authored andcommitted
Fix problem with deleting reservations
1 parent 8018b2b commit fee17d2

File tree

6 files changed

+62
-45
lines changed

6 files changed

+62
-45
lines changed

Api/Data/AllegroReservationsInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ interface AllegroReservationsInterface
1212
public function placeReservation(CheckoutFormInterface $checkoutForm): void;
1313

1414
/**
15-
* @param CheckoutFormInterface $checkoutForm
15+
* @param string $checkoutFormId
1616
* @return void
1717
* @throws \Exception
1818
*/
19-
public function compensateReservation(CheckoutFormInterface $checkoutForm): void;
19+
public function compensateReservation(string $checkoutFormId): void;
2020
}

Controller/Adminhtml/Reservations/Delete.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Magento\Ui\Component\MassAction\Filter;
1717
use Macopedia\Allegro\Model\ResourceModel\Reservation\CollectionFactory;
1818
use Macopedia\Allegro\Model\OrderImporter\AllegroReservation;
19-
use Macopedia\Allegro\Model\CheckoutFormRepository;
2019

2120
/**
2221
* Delete controller class
@@ -35,32 +34,26 @@ class Delete extends Action
3534
/** @var AllegroReservation */
3635
private $allegroReservation;
3736

38-
/** @var CheckoutFormRepository */
39-
private $checkoutFormRepository;
40-
4137
/**
4238
* Delete constructor.
4339
* @param Action\Context $context
4440
* @param Logger $logger
4541
* @param Filter $filter
4642
* @param CollectionFactory $collectionFactory
4743
* @param AllegroReservation $allegroReservation
48-
* @param CheckoutFormRepository $checkoutFormRepository
4944
*/
5045
public function __construct(
5146
Action\Context $context,
5247
Logger $logger,
5348
Filter $filter,
5449
CollectionFactory $collectionFactory,
55-
AllegroReservation $allegroReservation,
56-
CheckoutFormRepository $checkoutFormRepository
50+
AllegroReservation $allegroReservation
5751
) {
5852
parent::__construct($context);
5953
$this->logger = $logger;
6054
$this->filter = $filter;
6155
$this->collectionFactory = $collectionFactory;
6256
$this->allegroReservation = $allegroReservation;
63-
$this->checkoutFormRepository = $checkoutFormRepository;
6457
}
6558

6659
/**
@@ -83,8 +76,7 @@ public function execute()
8376
$checkoutFormId = $item->getCheckoutFormId();
8477
$reservationId = $item->getReservationId();
8578
try {
86-
$checkoutForm = $this->checkoutFormRepository->get($checkoutFormId);
87-
$this->allegroReservation->compensateReservation($checkoutForm);
79+
$this->allegroReservation->compensateReservation($checkoutFormId);
8880
$this->logger->info(__("Reservation with ID: %1 has been successfully deleted", $reservationId));
8981
$this->messageManager->addSuccessMessage(__(
9082
"Reservation with ID: %1 has been successfully deleted",

Model/AllegroPrice.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Macopedia\Allegro\Model;
55

6-
76
use Magento\Catalog\Api\Data\ProductInterface;
87
use Magento\Catalog\Model\Product;
98
use Magento\Eav\Model\Config;
@@ -73,7 +72,7 @@ public function getByProductId(string $productId, int $storeId = 0)
7372
];
7473

7574
$price = $connection->fetchOne($select, $bind);
76-
if (is_null($price)) {
75+
if (!$price) {
7776
throw new AllegroPriceGettingException(
7877
"Error while trying to get Allegro price for product with id {$productId}",
7978
1603885321
@@ -88,7 +87,7 @@ public function getByProductId(string $productId, int $storeId = 0)
8887
*/
8988
protected function getPriceAttributeCode()
9089
{
91-
return $this->config->getPriceAttributeCode();;
90+
return $this->config->getPriceAttributeCode();
9291
}
9392

9493
/**

Model/AllegroPriceGettingException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Macopedia\Allegro\Model;
55

6-
76
class AllegroPriceGettingException extends \Exception
87
{
98

Model/OrderImporter/AllegroReservation.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
use Macopedia\Allegro\Api\Data\CheckoutForm\LineItemInterface;
99
use Macopedia\Allegro\Api\Data\CheckoutFormInterface;
1010
use Macopedia\Allegro\Logger\Logger;
11-
use Macopedia\Allegro\Model\Api\ClientException;
12-
use Macopedia\Allegro\Model\CheckoutFormRepository;
1311
use Magento\Framework\Api\SearchCriteriaBuilder;
1412
use Magento\Framework\Exception\LocalizedException;
1513
use Magento\Framework\Exception\NoSuchEntityException;
16-
use Magento\Framework\Stdlib\DateTime\DateTime;
1714
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
1815
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
1916
use Magento\InventorySalesApi\Api\Data\SalesEventInterface;
@@ -72,9 +69,6 @@ class AllegroReservation implements AllegroReservationsInterface
7269
/** @var SearchCriteriaBuilder */
7370
private $searchCriteriaBuilder;
7471

75-
/** @var CheckoutFormRepository */
76-
private $checkoutFormRepository;
77-
7872
/** @var Logger */
7973
private $logger;
8074

@@ -90,7 +84,6 @@ class AllegroReservation implements AllegroReservationsInterface
9084
* @param ResourceConnection $resource
9185
* @param Configuration $configuration
9286
* @param SearchCriteriaBuilder $searchCriteriaBuilder
93-
* @param CheckoutFormRepository $checkoutFormRepository
9487
* @param Logger $logger
9588
*/
9689
public function __construct(
@@ -104,7 +97,6 @@ public function __construct(
10497
ResourceConnection $resource,
10598
Configuration $configuration,
10699
SearchCriteriaBuilder $searchCriteriaBuilder,
107-
CheckoutFormRepository $checkoutFormRepository,
108100
Logger $logger
109101
) {
110102
$this->productRepository = $productRepository;
@@ -117,7 +109,6 @@ public function __construct(
117109
$this->resource = $resource;
118110
$this->configuration = $configuration;
119111
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
120-
$this->checkoutFormRepository = $checkoutFormRepository;
121112
$this->logger = $logger;
122113
}
123114

@@ -132,7 +123,7 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
132123
return;
133124
}
134125
$this->placeReservationsForSalesEvent->execute(
135-
$this->getItemsToSell($checkoutForm, false),
126+
$this->getItemsToReserve($checkoutForm),
136127
$this->getSalesChannel(),
137128
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_PLACED, $checkoutForm->getId())
138129
);
@@ -146,23 +137,23 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
146137
}
147138

148139
/**
149-
* @param CheckoutFormInterface $checkoutForm
150-
* @throws \Exception
140+
* @param string $checkoutFormId
141+
* @throws ReservationPlacingException
151142
*/
152-
public function compensateReservation(CheckoutFormInterface $checkoutForm): void
143+
public function compensateReservation(string $checkoutFormId): void
153144
{
154145
try {
155146
if (!$this->configuration->areReservationsEnabled()) {
156147
return;
157148
}
158149
$this->placeReservationsForSalesEvent->execute(
159-
$this->getItemsToSell($checkoutForm, true),
150+
$this->getItemsToCompensate($checkoutFormId),
160151
$this->getSalesChannel(),
161-
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_COMPENSATED, $checkoutForm->getId())
152+
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_COMPENSATED, $checkoutFormId)
162153
);
163154
} catch (\Exception $e) {
164155
throw new ReservationPlacingException(
165-
"Error while compensating reservation for order with id [{$checkoutForm->getId()}]",
156+
"Error while compensating reservation for order with id [{$checkoutFormId}]",
166157
1589540303,
167158
$e
168159
);
@@ -180,8 +171,7 @@ public function cleanOldReservations()
180171
foreach ($reservations as $reservation) {
181172
$checkoutFormId = $reservation->getCheckoutFormId();
182173
try {
183-
$checkoutForm = $this->checkoutFormRepository->get($checkoutFormId);
184-
$this->compensateReservation($checkoutForm);
174+
$this->compensateReservation($checkoutFormId);
185175
} catch (\Exception $e) {
186176
$this->logger->exception(
187177
$e,
@@ -235,6 +225,23 @@ private function getSalesChannel(): SalesChannelInterface
235225
* @throws NoSuchEntityException
236226
*/
237227
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
238245
{
239246
$reservations = $this->reservationRepository->getByCheckoutFormId($checkoutFormId);
240247

@@ -250,22 +257,22 @@ private function getProductsSku(string $checkoutFormId): array
250257

251258
$query = $connection
252259
->select()
253-
->from($originalReservations, ['sku'])
260+
->from($originalReservations)
261+
->columns(['sku', 'quantity'])
254262
->where('reservation_id IN (?)', $reservationsIds);
255263

256-
return $connection->fetchCol($query);
264+
return $connection->fetchAll($query);
257265
}
258266

259267
/**
260268
* @param CheckoutFormInterface $checkoutForm
261-
* @param bool $compensate
262269
* @return array
263270
* @throws CreatorItemsException
264271
* @throws NoSuchEntityException
265272
*/
266-
private function getItemsToSell(CheckoutFormInterface $checkoutForm, bool $compensate = false): array
273+
private function getItemsToReserve(CheckoutFormInterface $checkoutForm): array
267274
{
268-
$itemsToSell = [];
275+
$itemsToReserve = [];
269276
$productsSku = $this->getProductsSku($checkoutForm->getId());
270277

271278
/** @var LineItemInterface $lineItem */
@@ -278,14 +285,34 @@ private function getItemsToSell(CheckoutFormInterface $checkoutForm, bool $compe
278285
throw new CreatorItemsException("Product for requested offer id {$offerId} does not exist");
279286
}
280287

281-
if (in_array($sku, $productsSku) === $compensate) {
282-
$itemsToSell[] = $this->itemsToSellFactory->create([
288+
if (!in_array($sku, $productsSku)) {
289+
$itemsToReserve[] = $this->itemsToSellFactory->create([
283290
'sku' => $sku,
284-
'qty' => $compensate ? (float)$lineItem->getQty() : -(float)$lineItem->getQty()
291+
'qty' => -(float)$lineItem->getQty()
285292
]);
286293
}
287294
}
288295

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;
290317
}
291318
}

Model/OrderImporter/Processor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public function processOrder(CheckoutFormInterface $checkoutForm): void
9797

9898
if ($checkoutForm->getStatus() === Status::ALLEGRO_READY_FOR_PROCESSING) {
9999
if (!$this->tryToGetOrder($checkoutForm->getId())) {
100-
$this->allegroReservation->compensateReservation($checkoutForm);
100+
$this->allegroReservation->compensateReservation($checkoutForm->getId());
101101
$this->tryCreateOrder($checkoutForm);
102102
}
103103
} elseif ($checkoutForm->getStatus() === Status::ALLEGRO_CANCELLED) {
104-
$this->allegroReservation->compensateReservation($checkoutForm);
104+
$this->allegroReservation->compensateReservation($checkoutForm->getId());
105105
} else {
106106
$this->allegroReservation->placeReservation($checkoutForm);
107107
}

0 commit comments

Comments
 (0)