Skip to content

Commit

Permalink
Add price policy configuration
Browse files Browse the repository at this point in the history
- update Allegro offer price after Magento product save
- allow increasing prices on Allegro by given percent
  • Loading branch information
mstrzyzewski committed Oct 29, 2020
1 parent f3aaa2e commit 85dfd3c
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 12 deletions.
13 changes: 13 additions & 0 deletions Api/PriceCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Macopedia\Allegro\Api;

interface PriceCommandInterface
{
/**
* @param string $offerId
* @param float $price
* @return array
*/
public function change(string $offerId, float $price);
}
107 changes: 107 additions & 0 deletions Model/AllegroPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php


namespace Macopedia\Allegro\Model;


use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;

class AllegroPrice
{
/** @var Configuration */
protected $config;

/** @var Config */
protected $eavConfig;

/** @var ResourceConnection */
protected $resource;

/**
* AllegroPrice constructor.
* @param Configuration $config
* @param Config $eavConfig
* @param ResourceConnection $resource
*/
public function __construct(
Configuration $config,
Config $eavConfig,
ResourceConnection $resource
) {
$this->config = $config;
$this->eavConfig = $eavConfig;
$this->resource = $resource;
}

/**
* @param ProductInterface $product
* @return float|int|mixed|null
*/
public function get(ProductInterface $product)
{
$price = (float)$product->getData($this->getPriceAttributeCode());
return $this->calculateNewPrice($price);
}

/**
* @param string $productId
* @param int $storeId
* @return float|int
* @throws AllegroPriceGettingException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getByProductId(string $productId, int $storeId = 0)
{
$priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, $this->getPriceAttributeCode());

$connection = $this->resource->getConnection();

$select = $connection->select()
->from($priceAttribute->getBackendTable(), ['value'])
->where('entity_id = :productId')
->where('store_id = :storeId')
->where('attribute_id = :attributeId')
->where('value IS NOT NULL');

$bind = [
':productId' => (int)$productId,
':storeId' => (int)$storeId,
':attributeId' => (int)$priceAttribute->getId()
];

$price = $connection->fetchOne($select, $bind);
if (is_null($price)) {
throw new AllegroPriceGettingException(
"Error while trying to get Allegro price for product with id {$productId}",
1603885321
);
}

return $this->calculateNewPrice((float)$price);
}

/**
* @return string|null
*/
protected function getPriceAttributeCode()
{
return $this->config->getPriceAttributeCode();;
}

/**
* @param float $price
* @return float|int
*/
protected function calculateNewPrice(float $price)
{
if ($this->config->isPricePolicyEnabled()) {
$percentIncrease = $this->config->getPricePercentIncrease();
$price = $price + $price * $percentIncrease / 100;
}

return $price;
}
}
10 changes: 10 additions & 0 deletions Model/AllegroPriceGettingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Macopedia\Allegro\Model;


class AllegroPriceGettingException extends \Exception
{

}
39 changes: 39 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class Configuration
const DEBUG_MODE_ENABLED_CONFIG_PATH = 'allegro/debug_mode/debug_mode_enabled';
const EAN_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/ean_attribute';
const DESCRIPTION_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/description_attribute';
const PRICE_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/price_attribute';
const STORE_ID_CONFIG_PATH = 'allegro/order/store';
const RESERVATIONS_ENABLED_CONFIG_PATH = 'allegro/order/reservations_enabled';
const PRICE_POLICY_ENABLED_CONFIG_PATH = 'allegro/price_policy/price_policy_enabled';
const PERCENT_INCREASE_CONFIG_PATH = 'allegro/price_policy/percent_increase';
const LAST_EVENT_ID_FLAG_NAME = 'allegro_order_last_event_id';
const LAST_USER_ID_FLAG_NAME = 'allegro_credentials_last_user_id';
const INITIALIZATION_TIME_FLAG_NAME = 'allegro_initialization_time';
Expand Down Expand Up @@ -87,6 +90,30 @@ public function areReservationsEnabled(
return $this->scopeConfig->isSetFlag(self::RESERVATIONS_ENABLED_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @param string $scopeType
* @param string|null $scopeCode
* @return bool
*/
public function isPricePolicyEnabled(
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
?string $scopeCode = null
): bool {
return $this->scopeConfig->isSetFlag(self::PRICE_POLICY_ENABLED_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @param string $scopeType
* @param string|null $scopeCode
* @return string|null
*/
public function getPricePercentIncrease(
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
?string $scopeCode = null
): ?string {
return $this->scopeConfig->getValue(self::PERCENT_INCREASE_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -127,6 +154,18 @@ public function getDescriptionAttributeCode(
return $this->scopeConfig->getValue(self::DESCRIPTION_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @param string $scopeType
* @param string|null $scopeCode
* @return string|null
*/
public function getPriceAttributeCode(
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
?string $scopeCode = null
): ?string {
return $this->scopeConfig->getValue(self::PRICE_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @return int
*/
Expand Down
39 changes: 37 additions & 2 deletions Model/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Macopedia\Allegro\Api\Data\PublicationCommandInterface;
use Macopedia\Allegro\Api\Data\PublicationCommandInterfaceFactory;
use Macopedia\Allegro\Api\OfferRepositoryInterface;
use Macopedia\Allegro\Api\PriceCommandInterface;
use Macopedia\Allegro\Api\PublicationCommandRepositoryInterface;
use Macopedia\Allegro\Api\QuantityCommandInterface;
use Macopedia\Allegro\Logger\Logger;
use Macopedia\Allegro\Model\Api\ClientException;
use Macopedia\Allegro\Model\Api\Credentials;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
Expand Down Expand Up @@ -53,6 +55,12 @@ class Consumer implements ConsumerInterface
/** @var Configuration */
private $config;

/** @var PriceCommandInterface */
private $priceCommand;

/** @var AllegroPrice */
protected $allegroPrice;

/**
* Consumer constructor.
* @param Logger $logger
Expand All @@ -65,6 +73,8 @@ class Consumer implements ConsumerInterface
* @param QuantityCommandInterface $quantityCommand
* @param Configuration $config
* @param Processor $indexerProcessor
* @param PriceCommandInterface $priceCommand
* @param AllegroPrice $allegroPrice
*/
public function __construct(
Logger $logger,
Expand All @@ -76,7 +86,9 @@ public function __construct(
PublicationCommandInterfaceFactory $publicationCommandFactory,
QuantityCommandInterface $quantityCommand,
Configuration $config,
Processor $indexerProcessor
Processor $indexerProcessor,
PriceCommandInterface $priceCommand,
AllegroPrice $allegroPrice
) {
$this->logger = $logger;
$this->productRepository = $productRepository;
Expand All @@ -88,6 +100,8 @@ public function __construct(
$this->config = $config;
$this->quantityCommand = $quantityCommand;
$this->indexerProcessor = $indexerProcessor;
$this->priceCommand = $priceCommand;
$this->allegroPrice = $allegroPrice;
}

/**
Expand Down Expand Up @@ -119,6 +133,8 @@ public function processMessage(MessageInterface $message)
$this->logger->error($exception->getMessage(), $exception->getTrace());
}

$this->updateOfferPrice($product, $allegroOfferId);

$offer = $this->offerRepository->get($allegroOfferId);
$productStock = $this->getSalableQuantityDataBySku->execute($product->getSku());
if (!isset($productStock[0]['qty'])) {
Expand All @@ -144,7 +160,7 @@ public function processMessage(MessageInterface $message)

$this->logger->info(
sprintf(
'Quantity of offer with external id %s has been successfully updated',
'Quantity and price of offer with external id %s have been successfully updated',
$allegroOfferId
)
);
Expand All @@ -170,4 +186,23 @@ private function savePublicationCommand(string $offerId, string $action)

$this->publicationCommandRepository->save($publicationCommand);
}

/**
* @param ProductInterface $product
* @param string $offerId
* @throws AllegroPriceGettingException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function updateOfferPrice(ProductInterface $product, string $offerId)
{
if (!$this->config->isPricePolicyEnabled()) {
return;
}

$price = $this->allegroPrice->getByProductId($product->getId());

if ($price > 0) {
$this->priceCommand->change($offerId, $price);
}
}
}
37 changes: 37 additions & 0 deletions Model/PriceCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Macopedia\Allegro\Model;

use Macopedia\Allegro\Api\PriceCommandInterface;
use Macopedia\Allegro\Model\Api\ClientResponseException;
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers;
use Macopedia\Allegro\Model\Api\ClientException;

class PriceCommand implements PriceCommandInterface
{

/** @var Offers */
private $offers;

/**
* PriceCommand constructor.
* @param Offers $offers
*/
public function __construct(Offers $offers)
{
$this->offers = $offers;
}

/**
* @param string $offerId
* @param float $price
* @return array
* @throws Api\ClientResponseErrorException
* @throws ClientException
* @throws ClientResponseException
*/
public function change(string $offerId, float $price)
{
return $this->offers->changePrice($offerId, $price);
}
}
Loading

0 comments on commit 85dfd3c

Please sign in to comment.