Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default tax class rate to the price index #1003

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\PriceData as ResourceModel;
use Magento\Tax\Api\TaxCalculationInterface;
use Magento\Tax\Model\Config;

/**
* Datasource used to append prices data to product during indexing.
Expand All @@ -36,16 +39,40 @@ class PriceData implements DatasourceInterface
*/
private $priceReaderPool = [];

/**
* @var TaxCalculationInterface
*/
private $taxCalculation;

/**
* @var PriceCurrencyInterface
*/
private $priceCurrency;

/**
* @var Config
*/
private $taxConfig;

/**
* Constructor.
*
* @param ResourceModel $resourceModel Resource model
* @param PriceData\PriceDataReaderInterface[] $priceReaderPool Price modifiers pool.
*/
public function __construct(ResourceModel $resourceModel, $priceReaderPool = [])
public function __construct(
ResourceModel $resourceModel,
$priceReaderPool = [],
TaxCalculationInterface $taxCalculation,
PriceCurrencyInterface $priceCurrency,
Config $taxConfig
)
{
$this->resourceModel = $resourceModel;
$this->resourceModel = $resourceModel;
$this->priceReaderPool = $priceReaderPool;
$this->taxCalculation = $taxCalculation;
$this->priceCurrency = $priceCurrency;
$this->taxConfig = $taxConfig;
}

/**
Expand All @@ -60,11 +87,18 @@ public function addData($storeId, array $indexData)
foreach ($priceData as $priceDataRow) {
$productId = (int) $priceDataRow['entity_id'];
$productTypeId = $indexData[$productId]['type_id'];

$priceModifier = $this->getPriceDataReader($productTypeId);

$originalPrice = $priceModifier->getOriginalPrice($priceDataRow);
$price = $priceModifier->getPrice($priceDataRow);

if ($this->taxConfig->needPriceConversion($storeId)) {
$rate = $this->taxCalculation->getCalculatedRate($priceDataRow['tax_class_id']);
$price = $this->priceCurrency->round($price * (1 + ($rate / 100)));
$originalPrice = $this->priceCurrency->round($originalPrice * (1 + ($rate / 100)));
}

$indexData[$productId]['price'][] = [
'price' => $price,
'original_price' => $originalPrice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

namespace Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer;
use Magento\Eav\Model\ResourceModel\Entity\Attribute;

/**
* Prices data datasource resource model.
Expand All @@ -25,6 +30,24 @@
*/
class PriceData extends Indexer
{
/**
* @var Attribute
*/
private $eavAttribute;

public function __construct(
ResourceConnection $resource,
StoreManagerInterface $storeManager,
MetadataPool $metadataPool,
Attribute $eavAttribute
)
{
$this->eavAttribute = $eavAttribute;

parent::__construct($resource, $storeManager, $metadataPool);
}


/**
* Load prices data for a list of product ids and a given store.
*
Expand All @@ -37,11 +60,36 @@ public function loadPriceData($storeId, $productIds)
{
$websiteId = $this->getStore($storeId)->getWebsiteId();

$taxClassAttrId = $this->eavAttribute->getIdByCode('catalog_product', 'tax_class_id');

$select = $this->getConnection()->select()
->from(['p' => $this->getTable('catalog_product_index_price')])
->join(
['tcd' => $this->getTable('catalog_product_entity_int')],
$this->taxClassJoinCondition('tcd', $taxClassAttrId, Store::DEFAULT_STORE_ID),
[]
)
->joinLeft(
['tcs' => $this->getTable('catalog_product_entity_int')],
$this->taxClassJoinCondition('tcs', $taxClassAttrId, $storeId),
[]
)
->columns([
'tax_class_id' => 'IFNULL(tcs.value, tcd.value)'
])
->where('p.website_id = ?', $websiteId)
->where('p.entity_id IN(?)', $productIds);

return $this->getConnection()->fetchAll($select);
}

private function taxClassJoinCondition($alias, $taxClassAttrId, $storeId)
{
return join(' AND ', [
$alias . '.row_id = p.entity_id',
$this->getConnection()->quoteInto($alias . '.store_id = ?', $storeId),
$this->getConnection()->quoteInto($alias . '.attribute_id = ?', $taxClassAttrId)
]
);
}
}