Skip to content

Commit

Permalink
Merge pull request #4671 from magento-tango/MC-18954
Browse files Browse the repository at this point in the history
[tango] MC-18954: Nightly build jobs were failing lately after un-skipping tests in MC-5777
  • Loading branch information
dhorytskyi authored Aug 24, 2019
2 parents e56640c + 6995158 commit 099c639
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 175 deletions.
5 changes: 4 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Type/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ public function calculatePrice(
) {
\Magento\Framework\Profiler::start('__PRODUCT_CALCULATE_PRICE__');
if ($wId instanceof Store) {
$sId = $wId->getId();
$wId = $wId->getWebsiteId();
} else {
$sId = $this->_storeManager->getWebsite($wId)->getDefaultGroup()->getDefaultStoreId();
}

$finalPrice = $basePrice;
Expand All @@ -610,7 +613,7 @@ public function calculatePrice(
);

if ($rulePrice === false) {
$date = $this->_localeDate->date(null, null, false);
$date = $this->_localeDate->scopeDate($sId);
$rulePrice = $this->_ruleFactory->create()->getRulePrice($date, $wId, $gId, $productId);
}

Expand Down
51 changes: 32 additions & 19 deletions app/code/Magento/CatalogRule/Model/Indexer/ReindexRuleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@

use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
use Magento\Framework\App\ObjectManager;
use Magento\CatalogRule\Model\Rule;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Reindex rule relations with products.
*/
class ReindexRuleProduct
{
/**
* @var \Magento\Framework\App\ResourceConnection
* @var ResourceConnection
*/
private $resource;

Expand All @@ -31,36 +34,40 @@ class ReindexRuleProduct
private $tableSwapper;

/**
* @param \Magento\Framework\App\ResourceConnection $resource
* @var TimezoneInterface
*/
private $localeDate;

/**
* @param ResourceConnection $resource
* @param ActiveTableSwitcher $activeTableSwitcher
* @param TableSwapper|null $tableSwapper
* @param TableSwapper $tableSwapper
* @param TimezoneInterface $localeDate
*/
public function __construct(
\Magento\Framework\App\ResourceConnection $resource,
ResourceConnection $resource,
ActiveTableSwitcher $activeTableSwitcher,
TableSwapper $tableSwapper = null
TableSwapper $tableSwapper,
TimezoneInterface $localeDate
) {
$this->resource = $resource;
$this->activeTableSwitcher = $activeTableSwitcher;
$this->tableSwapper = $tableSwapper ??
ObjectManager::getInstance()->get(TableSwapper::class);
$this->tableSwapper = $tableSwapper;
$this->localeDate = $localeDate;
}

/**
* Reindex information about rule relations with products.
*
* @param \Magento\CatalogRule\Model\Rule $rule
* @param Rule $rule
* @param int $batchCount
* @param bool $useAdditionalTable
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute(
\Magento\CatalogRule\Model\Rule $rule,
$batchCount,
$useAdditionalTable = false
) {
public function execute(Rule $rule, $batchCount, $useAdditionalTable = false)
{
if (!$rule->getIsActive() || empty($rule->getWebsiteIds())) {
return false;
}
Expand All @@ -84,21 +91,26 @@ public function execute(

$ruleId = $rule->getId();
$customerGroupIds = $rule->getCustomerGroupIds();
$fromTime = strtotime($rule->getFromDate());
$toTime = strtotime($rule->getToDate());
$toTime = $toTime ? $toTime + \Magento\CatalogRule\Model\Indexer\IndexBuilder::SECONDS_IN_DAY - 1 : 0;
$sortOrder = (int)$rule->getSortOrder();
$actionOperator = $rule->getSimpleAction();
$actionAmount = $rule->getDiscountAmount();
$actionStop = $rule->getStopRulesProcessing();

$rows = [];
foreach ($websiteIds as $websiteId) {
$scopeTz = new \DateTimeZone(
$this->localeDate->getConfigTimezone(ScopeInterface::SCOPE_WEBSITE, $websiteId)
);
$fromTime = (new \DateTime($rule->getFromDate(), $scopeTz))->getTimestamp();
$toTime = $rule->getToDate()
? (new \DateTime($rule->getToDate(), $scopeTz))->getTimestamp() + IndexBuilder::SECONDS_IN_DAY - 1
: 0;

foreach ($productIds as $productId => $validationByWebsite) {
foreach ($websiteIds as $websiteId) {
foreach ($productIds as $productId => $validationByWebsite) {
if (empty($validationByWebsite[$websiteId])) {
continue;
}

foreach ($customerGroupIds as $customerGroupId) {
$rows[] = [
'rule_id' => $ruleId,
Expand All @@ -123,6 +135,7 @@ public function execute(
if (!empty($rows)) {
$connection->insertMultiple($indexTable, $rows);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,89 @@

namespace Magento\CatalogRule\Model\Indexer;

use Magento\Catalog\Model\Product;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Reindex product prices according rule settings.
*/
class ReindexRuleProductPrice
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder
* @var RuleProductsSelectBuilder
*/
private $ruleProductsSelectBuilder;

/**
* @var \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator
* @var ProductPriceCalculator
*/
private $productPriceCalculator;

/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
* @var TimezoneInterface
*/
private $dateTime;
private $localeDate;

/**
* @var \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor
* @var RuleProductPricesPersistor
*/
private $pricesPersistor;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param StoreManagerInterface $storeManager
* @param RuleProductsSelectBuilder $ruleProductsSelectBuilder
* @param ProductPriceCalculator $productPriceCalculator
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor
* @param TimezoneInterface $localeDate
* @param RuleProductPricesPersistor $pricesPersistor
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder $ruleProductsSelectBuilder,
\Magento\CatalogRule\Model\Indexer\ProductPriceCalculator $productPriceCalculator,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor
StoreManagerInterface $storeManager,
RuleProductsSelectBuilder $ruleProductsSelectBuilder,
ProductPriceCalculator $productPriceCalculator,
TimezoneInterface $localeDate,
RuleProductPricesPersistor $pricesPersistor
) {
$this->storeManager = $storeManager;
$this->ruleProductsSelectBuilder = $ruleProductsSelectBuilder;
$this->productPriceCalculator = $productPriceCalculator;
$this->dateTime = $dateTime;
$this->localeDate = $localeDate;
$this->pricesPersistor = $pricesPersistor;
}

/**
* Reindex product prices.
*
* @param int $batchCount
* @param \Magento\Catalog\Model\Product|null $product
* @param Product|null $product
* @param bool $useAdditionalTable
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute(
$batchCount,
\Magento\Catalog\Model\Product $product = null,
$useAdditionalTable = false
) {
$fromDate = mktime(0, 0, 0, date('m'), date('d') - 1);
$toDate = mktime(0, 0, 0, date('m'), date('d') + 1);

public function execute($batchCount, Product $product = null, $useAdditionalTable = false)
{
/**
* Update products rules prices per each website separately
* because of max join limit in mysql
* because for each website date in website's timezone should be used
*/
foreach ($this->storeManager->getWebsites() as $website) {
$productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $product, $useAdditionalTable);
$dayPrices = [];
$stopFlags = [];
$prevKey = null;

$storeGroup = $this->storeManager->getGroup($website->getDefaultGroupId());
$currentDate = $this->localeDate->scopeDate($storeGroup->getDefaultStoreId(), null, true);
$previousDate = (clone $currentDate)->modify('-1 day');
$previousDate->setTime(23, 59, 59);
$nextDate = (clone $currentDate)->modify('+1 day');
$nextDate->setTime(0, 0, 0);

while ($ruleData = $productsStmt->fetch()) {
$ruleProductId = $ruleData['product_id'];
$productKey = $ruleProductId .
Expand All @@ -100,12 +105,11 @@ public function execute(
}
}

$ruleData['from_time'] = $this->roundTime($ruleData['from_time']);
$ruleData['to_time'] = $this->roundTime($ruleData['to_time']);
/**
* Build prices for each day
*/
for ($time = $fromDate; $time <= $toDate; $time += IndexBuilder::SECONDS_IN_DAY) {
foreach ([$previousDate, $currentDate, $nextDate] as $date) {
$time = $date->getTimestamp();
if (($ruleData['from_time'] == 0 ||
$time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 ||
$time <= $ruleData['to_time'])
Expand All @@ -118,7 +122,7 @@ public function execute(

if (!isset($dayPrices[$priceKey])) {
$dayPrices[$priceKey] = [
'rule_date' => $time,
'rule_date' => $date,
'website_id' => $ruleData['website_id'],
'customer_group_id' => $ruleData['customer_group_id'],
'product_id' => $ruleProductId,
Expand Down Expand Up @@ -151,18 +155,7 @@ public function execute(
}
$this->pricesPersistor->execute($dayPrices, $useAdditionalTable);
}
return true;
}

/**
* @param int $timeStamp
* @return int
*/
private function roundTime($timeStamp)
{
if (is_numeric($timeStamp) && $timeStamp != 0) {
$timeStamp = $this->dateTime->timestamp($this->dateTime->date('Y-m-d 00:00:00', $timeStamp));
}
return $timeStamp;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function addPriceData(ProductCollection $productCollection, $joinColumn =
),
$connection->quoteInto(
'catalog_rule.rule_date = ?',
$this->dateTime->formatDate($this->localeDate->date(null, null, false), false)
$this->dateTime->formatDate($this->localeDate->scopeDate($store->getId()), false)
),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function __construct(
*/
public function build($productId)
{
$currentDate = $this->dateTime->formatDate($this->localeDate->date(null, null, false), false);
$timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
$currentDate = $this->dateTime->formatDate($timestamp, false);
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productTable = $this->resource->getTableName('catalog_product_entity');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
if ($observer->getEvent()->hasDate()) {
$date = new \DateTime($observer->getEvent()->getDate());
} else {
$date = $this->localeDate->date(null, null, false);
$date = (new \DateTime())->setTimestamp($this->localeDate->scopeTimeStamp($store));
}

$productIds = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function __construct(
public function execute(\Magento\Framework\Event\Observer $observer)
{
$product = $observer->getEvent()->getProduct();
$date = $this->localeDate->date(null, null, false);
$storeId = $product->getStoreId();
$date = $this->localeDate->scopeDate($storeId);
$key = false;

$ruleData = $this->coreRegistry->registry('rule_data');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
if ($observer->hasDate()) {
$date = new \DateTime($observer->getEvent()->getDate());
} else {
$date = $this->localeDate->date(null, null, false);
$date = $this->localeDate->scopeDate($storeId);
}

if ($observer->hasWebsiteId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getValue()
$this->value = (float)$this->product->getData(self::PRICE_CODE) ?: false;
} else {
$this->value = $this->ruleResource->getRulePrice(
$this->dateTime->date(null, null, false),
$this->dateTime->scopeDate($this->storeManager->getStore()->getId()),
$this->storeManager->getStore()->getWebsiteId(),
$this->customerSession->getCustomerGroupId(),
$this->product->getId()
Expand Down
Loading

0 comments on commit 099c639

Please sign in to comment.