Skip to content

Commit

Permalink
Merge branch 'master' into product_prices
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslix authored Oct 29, 2020
2 parents 85dfd3c + 4bb58d8 commit c2134ee
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Api/Data/ReservationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function getCheckoutFormId(): string;
*/
public function getSku(): string;

/**
* @return string
*/
public function getCreatedAt(): string;

/**
* @param int $reservationId
* @return void
Expand All @@ -40,6 +45,13 @@ public function setCheckoutFormId(string $checkoutFormId): void;

/**
* @param string $sku
* @return void
*/
public function setSku(string $sku): void;

/**
* @param string $date
* @return void
*/
public function setCreatedAt(string $date) : void;
}
49 changes: 49 additions & 0 deletions Cron/CleanReservations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types = 1);

namespace Macopedia\Allegro\Cron;

use Macopedia\Allegro\Logger\Logger;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Macopedia\Allegro\Model\OrderImporter\AllegroReservation;

/**
* Class responsible for cleaning old reservations
*/
class CleanReservations
{
const RESERVATIONS_CONFIG_KEY = 'allegro/order/reservations_enabled';

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

/** @var ScopeConfigInterface */
private $scopeConfig;

/** @var AllegroReservation */
private $allegroReservation;

/**
* @param Logger $logger
* @param ScopeConfigInterface $scopeConfig
* @param AllegroReservation $allegroReservation
*/
public function __construct(
Logger $logger,
ScopeConfigInterface $scopeConfig,
AllegroReservation $allegroReservation
) {
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
$this->allegroReservation = $allegroReservation;
}

public function execute()
{
if ($this->scopeConfig->getValue(self::RESERVATIONS_CONFIG_KEY)) {
$this->logger->addInfo("Cronjob clean reservations is executed.");
$this->allegroReservation->cleanOldReservations();
}
}
}
47 changes: 46 additions & 1 deletion Model/OrderImporter/AllegroReservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
use Macopedia\Allegro\Api\Data\AllegroReservationsInterface;
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 @@ -64,6 +69,15 @@ class AllegroReservation implements AllegroReservationsInterface
/** @var Configuration */
private $configuration;

/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;

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

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

/**
* AllegroReservation constructor.
* @param ProductRepositoryInterface $productRepository
Expand All @@ -75,6 +89,9 @@ class AllegroReservation implements AllegroReservationsInterface
* @param ReservationRepository $reservationRepository
* @param ResourceConnection $resource
* @param Configuration $configuration
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param CheckoutFormRepository $checkoutFormRepository
* @param Logger $logger
*/
public function __construct(
ProductRepositoryInterface $productRepository,
Expand All @@ -85,7 +102,10 @@ public function __construct(
StoreManagerInterface $storeManager,
ReservationRepository $reservationRepository,
ResourceConnection $resource,
Configuration $configuration
Configuration $configuration,
SearchCriteriaBuilder $searchCriteriaBuilder,
CheckoutFormRepository $checkoutFormRepository,
Logger $logger
) {
$this->productRepository = $productRepository;
$this->itemsToSellFactory = $itemsToSellFactory;
Expand All @@ -96,6 +116,9 @@ public function __construct(
$this->reservationRepository = $reservationRepository;
$this->resource = $resource;
$this->configuration = $configuration;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->checkoutFormRepository = $checkoutFormRepository;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -146,6 +169,28 @@ public function compensateReservation(CheckoutFormInterface $checkoutForm): void
}
}

public function cleanOldReservations()
{
$tenDaysAgo = (new \DateTime())->modify('-10 day');
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('created_at', $tenDaysAgo->format('Y-m-d 12:00:00'), 'lteq')
->create();

$reservations = $this->reservationRepository->getList($searchCriteria);
foreach ($reservations as $reservation) {
$checkoutFormId = $reservation->getCheckoutFormId();
try {
$checkoutForm = $this->checkoutFormRepository->get($checkoutFormId);
$this->compensateReservation($checkoutForm);
} catch (\Exception $e) {
$this->logger->exception(
$e,
"Error occurred when trying to delete reservation for checkout form with id {$checkoutFormId}"
);
}
}
}

/**
* @param string $type
* @param string $checkoutFormId
Expand Down
17 changes: 17 additions & 0 deletions Model/Reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Reservation extends AbstractModel implements ReservationInterface
const RESERVATION_ID_FIELD = 'reservation_id';
const CHECKOUT_FORM_ID_FIELD = 'checkout_form_id';
const SKU_FIELD = 'sku';
const CREATED_AT_FIELD = 'created_at';
/**#@-*/

/**
Expand Down Expand Up @@ -60,6 +61,14 @@ public function getSku(): string
return $this->getData(self::SKU_FIELD);
}

/**
* @inheritDoc
*/
public function getCreatedAt(): string
{
return $this->getData(self::CREATED_AT_FIELD);
}

/**
* @inheritDoc
*/
Expand All @@ -83,4 +92,12 @@ public function setSku(string $sku): void
{
$this->setData(self::SKU_FIELD, $sku);
}

/**
* @inheritDoc
*/
public function setCreatedAt(string $date): void
{
$this->setData(self::CREATED_AT_FIELD, $date);
}
}
10 changes: 9 additions & 1 deletion Preference/Model/ResourceModel/SaveMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Macopedia\Allegro\Logger\Logger;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\InventoryReservationsApi\Model\ReservationInterface;
use Magento\InventoryReservations\Model\ResourceModel\SaveMultiple as MagentoSaveMultiple;
use Macopedia\Allegro\Api\ReservationRepositoryInterface;
Expand Down Expand Up @@ -36,27 +37,33 @@ class SaveMultiple extends MagentoSaveMultiple
/** @var Logger */
private $logger;

/** @var DateTime */
private $date;

/**
* SaveMultiple constructor.
* @param ResourceConnection $resourceConnection
* @param ReservationRepositoryInterface $reservationRepository
* @param ReservationInterfaceFactory $reservationInterfaceFactory
* @param SerializerInterface $serializer
* @param Logger $logger
* @param DateTime $date
*/
public function __construct(
ResourceConnection $resourceConnection,
ReservationRepositoryInterface $reservationRepository,
ReservationInterfaceFactory $reservationInterfaceFactory,
SerializerInterface $serializer,
Logger $logger
Logger $logger,
DateTime $date
) {
parent::__construct($resourceConnection);
$this->resourceConnection = $resourceConnection;
$this->reservationRepository = $reservationRepository;
$this->reservationInterfaceFactory = $reservationInterfaceFactory;
$this->serializer = $serializer;
$this->logger = $logger;
$this->date = $date;
}

/**
Expand Down Expand Up @@ -85,6 +92,7 @@ public function execute(array $reservations)
$allegroReservation->setReservationId($reservationId);
$allegroReservation->setCheckoutFormId($metadata['object_id']);
$allegroReservation->setSku($reservation->getSku());
$allegroReservation->setCreatedAt($this->date->gmtDate());
$this->reservationRepository->save($allegroReservation);
} else {
if ($metadata['event_type'] === self::ALLEGRO_EVENT_RESERVATION_COMPENSATED) {
Expand Down
3 changes: 3 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<job instance="Macopedia\Allegro\Cron\RefreshToken" method="execute" name="macopedia_allegro_refresh_token">
<schedule>0 */6 * * *</schedule>
</job>
<job instance="Macopedia\Allegro\Cron\CleanReservations" method="execute" name="macopedia_allegro_clean_reservations">
<schedule>0 12 * * *</schedule>
</job>
</group>
</config>
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<column xsi:type="int" name="reservation_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Reservation ID"/>
<column xsi:type="varchar" name="checkout_form_id" nullable="false" comment="Checkout form ID"/>
<column xsi:type="varchar" name="sku" nullable="false" comment="Product SKU"/>
<column xsi:type="datetime" name="created_at" nullable="false" comment="Creation date"/>
<constraint xsi:type="foreign" referenceId="FOREIGN" table="allegro_reservations" column="reservation_id" referenceTable="inventory_reservation" referenceColumn="reservation_id" onDelete="CASCADE"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
Expand Down
3 changes: 2 additions & 1 deletion etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"entity_id": true,
"reservation_id": true,
"checkout_form_id": true,
"sku": true
"sku": true,
"created_at": true
},
"constraint": {
"FK_0DF36C9A02B0CEA1AF96F370F2C37537": true,
Expand Down
1 change: 1 addition & 0 deletions i18n/pl_PL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,4 @@
"Prices higher on Allegro by the given percentage","Ceny na Allegro wyższe o dany procent"
"Product attribute for getting price","Atrybut produktu do pobrania ceny"
"Attributes are taken from the default scope","Atrybuty są pobierane z domyślnego widoku sklepu"
"Creation date","Data utworzenia"
10 changes: 10 additions & 0 deletions view/adminhtml/ui_component/allegro_reservations_listing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,15 @@
</item>
</argument>
</column>
<column name="created_at" class="Magento\Ui\Component\Listing\Columns\Date" sortOrder="70">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">dateRange</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
<item name="dataType" xsi:type="string">date</item>
<item name="label" xsi:type="string" translate="true">Creation date</item>
</item>
</argument>
</column>
</columns>
</listing>

0 comments on commit c2134ee

Please sign in to comment.