Skip to content

Commit

Permalink
Fix problem with deleting reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrzyzewski authored and cieslix committed Nov 26, 2020
1 parent 8018b2b commit fee17d2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Api/Data/AllegroReservationsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ interface AllegroReservationsInterface
public function placeReservation(CheckoutFormInterface $checkoutForm): void;

/**
* @param CheckoutFormInterface $checkoutForm
* @param string $checkoutFormId
* @return void
* @throws \Exception
*/
public function compensateReservation(CheckoutFormInterface $checkoutForm): void;
public function compensateReservation(string $checkoutFormId): void;
}
12 changes: 2 additions & 10 deletions Controller/Adminhtml/Reservations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Magento\Ui\Component\MassAction\Filter;
use Macopedia\Allegro\Model\ResourceModel\Reservation\CollectionFactory;
use Macopedia\Allegro\Model\OrderImporter\AllegroReservation;
use Macopedia\Allegro\Model\CheckoutFormRepository;

/**
* Delete controller class
Expand All @@ -35,32 +34,26 @@ class Delete extends Action
/** @var AllegroReservation */
private $allegroReservation;

/** @var CheckoutFormRepository */
private $checkoutFormRepository;

/**
* Delete constructor.
* @param Action\Context $context
* @param Logger $logger
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param AllegroReservation $allegroReservation
* @param CheckoutFormRepository $checkoutFormRepository
*/
public function __construct(
Action\Context $context,
Logger $logger,
Filter $filter,
CollectionFactory $collectionFactory,
AllegroReservation $allegroReservation,
CheckoutFormRepository $checkoutFormRepository
AllegroReservation $allegroReservation
) {
parent::__construct($context);
$this->logger = $logger;
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->allegroReservation = $allegroReservation;
$this->checkoutFormRepository = $checkoutFormRepository;
}

/**
Expand All @@ -83,8 +76,7 @@ public function execute()
$checkoutFormId = $item->getCheckoutFormId();
$reservationId = $item->getReservationId();
try {
$checkoutForm = $this->checkoutFormRepository->get($checkoutFormId);
$this->allegroReservation->compensateReservation($checkoutForm);
$this->allegroReservation->compensateReservation($checkoutFormId);
$this->logger->info(__("Reservation with ID: %1 has been successfully deleted", $reservationId));
$this->messageManager->addSuccessMessage(__(
"Reservation with ID: %1 has been successfully deleted",
Expand Down
5 changes: 2 additions & 3 deletions Model/AllegroPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Macopedia\Allegro\Model;


use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Config;
Expand Down Expand Up @@ -73,7 +72,7 @@ public function getByProductId(string $productId, int $storeId = 0)
];

$price = $connection->fetchOne($select, $bind);
if (is_null($price)) {
if (!$price) {
throw new AllegroPriceGettingException(
"Error while trying to get Allegro price for product with id {$productId}",
1603885321
Expand All @@ -88,7 +87,7 @@ public function getByProductId(string $productId, int $storeId = 0)
*/
protected function getPriceAttributeCode()
{
return $this->config->getPriceAttributeCode();;
return $this->config->getPriceAttributeCode();
}

/**
Expand Down
1 change: 0 additions & 1 deletion Model/AllegroPriceGettingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Macopedia\Allegro\Model;


class AllegroPriceGettingException extends \Exception
{

Expand Down
81 changes: 54 additions & 27 deletions Model/OrderImporter/AllegroReservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
use Macopedia\Allegro\Api\Data\CheckoutForm\LineItemInterface;
use Macopedia\Allegro\Api\Data\CheckoutFormInterface;
use Macopedia\Allegro\Logger\Logger;
use Macopedia\Allegro\Model\Api\ClientException;
use Macopedia\Allegro\Model\CheckoutFormRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\SalesEventInterface;
Expand Down Expand Up @@ -72,9 +69,6 @@ class AllegroReservation implements AllegroReservationsInterface
/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;

/** @var CheckoutFormRepository */
private $checkoutFormRepository;

/** @var Logger */
private $logger;

Expand All @@ -90,7 +84,6 @@ class AllegroReservation implements AllegroReservationsInterface
* @param ResourceConnection $resource
* @param Configuration $configuration
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param CheckoutFormRepository $checkoutFormRepository
* @param Logger $logger
*/
public function __construct(
Expand All @@ -104,7 +97,6 @@ public function __construct(
ResourceConnection $resource,
Configuration $configuration,
SearchCriteriaBuilder $searchCriteriaBuilder,
CheckoutFormRepository $checkoutFormRepository,
Logger $logger
) {
$this->productRepository = $productRepository;
Expand All @@ -117,7 +109,6 @@ public function __construct(
$this->resource = $resource;
$this->configuration = $configuration;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->checkoutFormRepository = $checkoutFormRepository;
$this->logger = $logger;
}

Expand All @@ -132,7 +123,7 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
return;
}
$this->placeReservationsForSalesEvent->execute(
$this->getItemsToSell($checkoutForm, false),
$this->getItemsToReserve($checkoutForm),
$this->getSalesChannel(),
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_PLACED, $checkoutForm->getId())
);
Expand All @@ -146,23 +137,23 @@ public function placeReservation(CheckoutFormInterface $checkoutForm): void
}

/**
* @param CheckoutFormInterface $checkoutForm
* @throws \Exception
* @param string $checkoutFormId
* @throws ReservationPlacingException
*/
public function compensateReservation(CheckoutFormInterface $checkoutForm): void
public function compensateReservation(string $checkoutFormId): void
{
try {
if (!$this->configuration->areReservationsEnabled()) {
return;
}
$this->placeReservationsForSalesEvent->execute(
$this->getItemsToSell($checkoutForm, true),
$this->getItemsToCompensate($checkoutFormId),
$this->getSalesChannel(),
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_COMPENSATED, $checkoutForm->getId())
$this->getSalesEvent(self::ALLEGRO_EVENT_RESERVATION_COMPENSATED, $checkoutFormId)
);
} catch (\Exception $e) {
throw new ReservationPlacingException(
"Error while compensating reservation for order with id [{$checkoutForm->getId()}]",
"Error while compensating reservation for order with id [{$checkoutFormId}]",
1589540303,
$e
);
Expand All @@ -180,8 +171,7 @@ public function cleanOldReservations()
foreach ($reservations as $reservation) {
$checkoutFormId = $reservation->getCheckoutFormId();
try {
$checkoutForm = $this->checkoutFormRepository->get($checkoutFormId);
$this->compensateReservation($checkoutForm);
$this->compensateReservation($checkoutFormId);
} catch (\Exception $e) {
$this->logger->exception(
$e,
Expand Down Expand Up @@ -235,6 +225,23 @@ private function getSalesChannel(): SalesChannelInterface
* @throws NoSuchEntityException
*/
private function getProductsSku(string $checkoutFormId): array
{
$reservations = $this->reservationRepository->getByCheckoutFormId($checkoutFormId);
$productsSku = [];

foreach ($reservations as $reservation) {
array_push($productsSku, $reservation->getSku());
}

return $productsSku;
}

/**
* @param string $checkoutFormId
* @return array
* @throws NoSuchEntityException
*/
private function getProductsData(string $checkoutFormId): array
{
$reservations = $this->reservationRepository->getByCheckoutFormId($checkoutFormId);

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

$query = $connection
->select()
->from($originalReservations, ['sku'])
->from($originalReservations)
->columns(['sku', 'quantity'])
->where('reservation_id IN (?)', $reservationsIds);

return $connection->fetchCol($query);
return $connection->fetchAll($query);
}

/**
* @param CheckoutFormInterface $checkoutForm
* @param bool $compensate
* @return array
* @throws CreatorItemsException
* @throws NoSuchEntityException
*/
private function getItemsToSell(CheckoutFormInterface $checkoutForm, bool $compensate = false): array
private function getItemsToReserve(CheckoutFormInterface $checkoutForm): array
{
$itemsToSell = [];
$itemsToReserve = [];
$productsSku = $this->getProductsSku($checkoutForm->getId());

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

if (in_array($sku, $productsSku) === $compensate) {
$itemsToSell[] = $this->itemsToSellFactory->create([
if (!in_array($sku, $productsSku)) {
$itemsToReserve[] = $this->itemsToSellFactory->create([
'sku' => $sku,
'qty' => $compensate ? (float)$lineItem->getQty() : -(float)$lineItem->getQty()
'qty' => -(float)$lineItem->getQty()
]);
}
}

return $itemsToSell;
return $itemsToReserve;
}

/**
* @param string $checkoutFormId
* @return array
* @throws NoSuchEntityException
*/
private function getItemsToCompensate(string $checkoutFormId)
{
$products = $this->getProductsData($checkoutFormId);
$itemsToCompensate = [];

foreach ($products as $product) {
$itemsToCompensate[] = $this->itemsToSellFactory->create([
'sku' => $product['sku'],
'qty' => -(float)$product['quantity']
]);
}

return $itemsToCompensate;
}
}
4 changes: 2 additions & 2 deletions Model/OrderImporter/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public function processOrder(CheckoutFormInterface $checkoutForm): void

if ($checkoutForm->getStatus() === Status::ALLEGRO_READY_FOR_PROCESSING) {
if (!$this->tryToGetOrder($checkoutForm->getId())) {
$this->allegroReservation->compensateReservation($checkoutForm);
$this->allegroReservation->compensateReservation($checkoutForm->getId());
$this->tryCreateOrder($checkoutForm);
}
} elseif ($checkoutForm->getStatus() === Status::ALLEGRO_CANCELLED) {
$this->allegroReservation->compensateReservation($checkoutForm);
$this->allegroReservation->compensateReservation($checkoutForm->getId());
} else {
$this->allegroReservation->placeReservation($checkoutForm);
}
Expand Down

0 comments on commit fee17d2

Please sign in to comment.