Skip to content

Commit 144b9ee

Browse files
committed
MC-20244: Pricing :: FPT calculation
- refactor to list individual attributes
1 parent fb12624 commit 144b9ee

File tree

4 files changed

+209
-143
lines changed

4 files changed

+209
-143
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Magento\Framework\Exception\LocalizedException;
1717
use Magento\Framework\Pricing\SaleableInterface;
1818
use Magento\Store\Api\Data\StoreInterface;
19-
use Magento\Framework\Pricing\Amount\AmountInterface;
2019

2120
/**
2221
* Format product's pricing information for price_range field
@@ -63,10 +62,16 @@ public function resolve(
6362
$product = $value['model'];
6463
$product->unsetData('minimal_price');
6564

66-
return [
67-
'minimum_price' => $this->getMinimumProductPrice($product, $store),
68-
'maximum_price' => $this->getMaximumProductPrice($product, $store)
69-
];
65+
$requestedFields = $info->getFieldSelection(10);
66+
$returnArray = [];
67+
68+
if (isset($requestedFields['minimum_price'])) {
69+
$returnArray['minimum_price'] = $this->getMinimumProductPrice($product, $store);
70+
}
71+
if (isset($requestedFields['maximum_price'])) {
72+
$returnArray['maximum_price'] = $this->getMaximumProductPrice($product, $store);
73+
}
74+
return $returnArray;
7075
}
7176

7277
/**

app/code/Magento/Weee/Model/ResourceModel/Tax.php

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Tax extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
2121
*/
2222
protected $dateTime;
2323

24+
/**
25+
* @var array
26+
*/
27+
private $weeeTaxCalculationsByEntityCache = [];
28+
2429
/**
2530
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
2631
* @param \Magento\Framework\Stdlib\DateTime $dateTime
@@ -100,43 +105,57 @@ public function isWeeeInLocation($countryId, $regionId, $websiteId)
100105
*/
101106
public function fetchWeeeTaxCalculationsByEntity($countryId, $regionId, $websiteId, $storeId, $entityId)
102107
{
103-
$attributeSelect = $this->getConnection()->select();
104-
$attributeSelect->from(
105-
['eavTable' => $this->getTable('eav_attribute')],
106-
['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
107-
)->joinLeft(
108-
['eavLabel' => $this->getTable('eav_attribute_label')],
109-
'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' .((int) $storeId),
110-
'eavLabel.value as label_value'
111-
)->joinInner(
112-
['weeeTax' => $this->getTable('weee_tax')],
113-
'weeeTax.attribute_id = eavTable.attribute_id',
114-
'weeeTax.value as weee_value'
115-
)->where(
116-
'eavTable.frontend_input = ?',
117-
'weee'
118-
)->where(
119-
'weeeTax.website_id IN(?)',
120-
[$websiteId, 0]
121-
)->where(
122-
'weeeTax.country = ?',
123-
$countryId
124-
)->where(
125-
'weeeTax.state IN(?)',
126-
[$regionId, 0]
127-
)->where(
128-
'weeeTax.entity_id = ?',
129-
(int)$entityId
108+
$cacheKey = sprintf(
109+
'%s-%s-%s-%s-%s',
110+
$countryId,
111+
$regionId,
112+
$websiteId,
113+
$storeId,
114+
$entityId
130115
);
116+
if (!isset($this->weeeTaxCalculationsByEntityCache[$cacheKey])) {
117+
$attributeSelect = $this->getConnection()->select();
118+
$attributeSelect->from(
119+
['eavTable' => $this->getTable('eav_attribute')],
120+
['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
121+
)->joinLeft(
122+
['eavLabel' => $this->getTable('eav_attribute_label')],
123+
'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' . ((int)$storeId),
124+
'eavLabel.value as label_value'
125+
)->joinInner(
126+
['weeeTax' => $this->getTable('weee_tax')],
127+
'weeeTax.attribute_id = eavTable.attribute_id',
128+
'weeeTax.value as weee_value'
129+
)->where(
130+
'eavTable.frontend_input = ?',
131+
'weee'
132+
)->where(
133+
'weeeTax.website_id IN(?)',
134+
[$websiteId, 0]
135+
)->where(
136+
'weeeTax.country = ?',
137+
$countryId
138+
)->where(
139+
'weeeTax.state IN(?)',
140+
[$regionId, 0]
141+
)->where(
142+
'weeeTax.entity_id = ?',
143+
(int)$entityId
144+
);
145+
131146

132-
$order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
133-
'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
134-
$attributeSelect->order($order);
147+
$order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
148+
'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
149+
$attributeSelect->order($order);
135150

136-
$values = $this->getConnection()->fetchAll($attributeSelect);
151+
$values = $this->getConnection()->fetchAll($attributeSelect);
137152

138-
if ($values) {
139-
return $values;
153+
if ($values) {
154+
$this->weeeTaxCalculationsByEntityCache[$cacheKey] = $values;
155+
return $values;
156+
}
157+
} else {
158+
return $this->weeeTaxCalculationsByEntityCache[$cacheKey];
140159
}
141160

142161
return [];

app/code/Magento/Weee/Model/Tax.php

Lines changed: 121 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class Tax extends \Magento\Framework\Model\AbstractModel
8888
*/
8989
protected $accountManagement;
9090

91+
/** @var array */
92+
private $weeAtrributesCache = [];
93+
9194
/**
9295
* @param \Magento\Framework\Model\Context $context
9396
* @param \Magento\Framework\Registry $registry
@@ -247,128 +250,143 @@ public function getProductWeeeAttributes(
247250
$calculateTax = null,
248251
$round = true
249252
) {
253+
$cacheKey = sprintf(
254+
'%s-%s-%s-%s-%s',
255+
$product->getId(),
256+
$shipping && $shipping->getId() ? $shipping->getId() : '',
257+
$billing && $billing->getId() ? $billing->getId() : '',
258+
$website && $website->getId() ? $website->getId() : $website ? $website : '',
259+
$calculateTax,
260+
$round
261+
);
250262
$result = [];
251-
$websiteId = null;
252-
/** @var \Magento\Store\Model\Store $store */
253-
$store = null;
254-
if (!$website) {
255-
$store = $product->getStore();
256-
if ($store) {
257-
$websiteId = $store->getWebsiteId();
263+
if (isset($this->weeAtrributesCache[$cacheKey])) {
264+
$result = $this->weeAtrributesCache[$cacheKey];
265+
} else {
266+
$websiteId = null;
267+
/** @var \Magento\Store\Model\Store $store */
268+
$store = null;
269+
if (!$website) {
270+
$store = $product->getStore();
271+
if ($store) {
272+
$websiteId = $store->getWebsiteId();
273+
}
274+
}
275+
if (!$websiteId) {
276+
$websiteObject = $this->_storeManager->getWebsite($website);
277+
$websiteId = $websiteObject->getId();
278+
$store = $websiteObject->getDefaultGroup()->getDefaultStore();
258279
}
259-
}
260-
if (!$websiteId) {
261-
$websiteObject = $this->_storeManager->getWebsite($website);
262-
$websiteId = $websiteObject->getId();
263-
$store = $websiteObject->getDefaultGroup()->getDefaultStore();
264-
}
265280

266-
$allWeee = $this->getWeeeTaxAttributeCodes($store);
267-
if (!$allWeee) {
268-
return $result;
269-
}
281+
$allWeee = $this->getWeeeTaxAttributeCodes($store);
282+
if (!$allWeee) {
283+
return $result;
284+
}
270285

271-
/** @var \Magento\Tax\Model\Calculation $calculator */
272-
$calculator = $this->_calculationFactory->create();
286+
/** @var \Magento\Tax\Model\Calculation $calculator */
287+
$calculator = $this->_calculationFactory->create();
273288

274-
$customerId = $this->_customerSession->getCustomerId();
275-
if ($shipping && $shipping->getCountryId()) {
276-
$customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
277-
} else {
278-
// if customer logged use it default shipping and billing address
279-
if ($customerId) {
280-
$shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
281-
$billing = $this->accountManagement->getDefaultBillingAddress($customerId);
282-
$customerTaxClass = null;
289+
$customerId = $this->_customerSession->getCustomerId();
290+
if ($shipping && $shipping->getCountryId()) {
291+
$customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
283292
} else {
284-
$shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
285-
$billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
286-
if (!empty($billingAddressArray)) {
287-
$billing = new \Magento\Framework\DataObject($billingAddressArray);
288-
}
289-
if (!empty($shippingAddressArray)) {
290-
$shipping = new \Magento\Framework\DataObject($shippingAddressArray);
293+
// if customer logged use it default shipping and billing address
294+
if ($customerId) {
295+
$shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
296+
$billing = $this->accountManagement->getDefaultBillingAddress($customerId);
297+
$customerTaxClass = null;
298+
} else {
299+
$shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
300+
$billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
301+
if (!empty($billingAddressArray)) {
302+
$billing = new \Magento\Framework\DataObject($billingAddressArray);
303+
}
304+
if (!empty($shippingAddressArray)) {
305+
$shipping = new \Magento\Framework\DataObject($shippingAddressArray);
306+
}
307+
$customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
291308
}
292-
$customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
293309
}
294-
}
295310

296-
$rateRequest = $calculator->getRateRequest(
297-
$shipping,
298-
$billing,
299-
$customerTaxClass,
300-
$store,
301-
$customerId
302-
);
303-
$defaultRateRequest = $calculator->getDefaultRateRequest($store);
304-
305-
$productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
306-
$rateRequest->getCountryId(),
307-
$rateRequest->getRegionId(),
308-
$websiteId,
309-
$store->getId(),
310-
$product->getId()
311-
);
311+
$rateRequest = $calculator->getRateRequest(
312+
$shipping,
313+
$billing,
314+
$customerTaxClass,
315+
$store,
316+
$customerId
317+
);
318+
$defaultRateRequest = $calculator->getDefaultRateRequest($store);
312319

313-
foreach ($productAttributes as $attribute) {
314-
$value = $attribute['weee_value'];
315-
if ($value) {
316-
$taxAmount = $amount = 0;
317-
$amount = $value;
318-
$amountExclTax = $value;
319-
if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
320-
/** @var \Magento\Tax\Model\Calculation $calculator */
321-
$defaultPercent = $calculator->getRate(
322-
$defaultRateRequest->setProductClassId($product->getTaxClassId())
323-
);
324-
$currentPercent = $calculator->getRate(
325-
$rateRequest->setProductClassId($product->getTaxClassId())
326-
);
327-
if ($this->_taxData->priceIncludesTax($store)) {
328-
$amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
329-
if ($round) {
330-
$amountInclTax = $this->priceCurrency->round($amountInclTax);
331-
}
332-
$taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
333-
if ($round) {
334-
$taxAmount = $this->priceCurrency->round($taxAmount);
335-
}
336-
$amountExclTax = $amountInclTax - $taxAmount;
337-
} else {
338-
$appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
339-
if (is_array($appliedRates) && count($appliedRates) > 1) {
340-
$taxAmount = 0;
341-
foreach ($appliedRates as $appliedRate) {
342-
$taxRate = $appliedRate['percent'];
343-
if ($round) {
344-
$taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
345-
} else {
346-
$taxAmount += $value * $taxRate / 100;
347-
}
320+
$productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
321+
$rateRequest->getCountryId(),
322+
$rateRequest->getRegionId(),
323+
$websiteId,
324+
$store->getId(),
325+
$product->getId()
326+
);
327+
328+
foreach ($productAttributes as $attribute) {
329+
$value = $attribute['weee_value'];
330+
if ($value) {
331+
$taxAmount = $amount = 0;
332+
$amount = $value;
333+
$amountExclTax = $value;
334+
if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
335+
/** @var \Magento\Tax\Model\Calculation $calculator */
336+
$defaultPercent = $calculator->getRate(
337+
$defaultRateRequest->setProductClassId($product->getTaxClassId())
338+
);
339+
$currentPercent = $calculator->getRate(
340+
$rateRequest->setProductClassId($product->getTaxClassId())
341+
);
342+
if ($this->_taxData->priceIncludesTax($store)) {
343+
$amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
344+
if ($round) {
345+
$amountInclTax = $this->priceCurrency->round($amountInclTax);
348346
}
349-
} else {
347+
$taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
350348
if ($round) {
351-
$taxAmount = $this->priceCurrency->round(
352-
$value * $currentPercent / 100
353-
);
349+
$taxAmount = $this->priceCurrency->round($taxAmount);
350+
}
351+
$amountExclTax = $amountInclTax - $taxAmount;
352+
} else {
353+
$appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
354+
if (is_array($appliedRates) && count($appliedRates) > 1) {
355+
$taxAmount = 0;
356+
foreach ($appliedRates as $appliedRate) {
357+
$taxRate = $appliedRate['percent'];
358+
if ($round) {
359+
$taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
360+
} else {
361+
$taxAmount += $value * $taxRate / 100;
362+
}
363+
}
354364
} else {
355-
$taxAmount = $value * $currentPercent / 100;
365+
if ($round) {
366+
$taxAmount = $this->priceCurrency->round(
367+
$value * $currentPercent / 100
368+
);
369+
} else {
370+
$taxAmount = $value * $currentPercent / 100;
371+
}
356372
}
357373
}
358374
}
359-
}
360375

361-
$one = new \Magento\Framework\DataObject();
362-
$one->setName(
363-
$attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
364-
)
365-
->setAmount($amount)
366-
->setTaxAmount($taxAmount)
367-
->setAmountExclTax($amountExclTax)
368-
->setCode($attribute['attribute_code']);
376+
$one = new \Magento\Framework\DataObject();
377+
$one->setName(
378+
$attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
379+
)
380+
->setAmount($amount)
381+
->setTaxAmount($taxAmount)
382+
->setAmountExclTax($amountExclTax)
383+
->setCode($attribute['attribute_code']);
369384

370-
$result[] = $one;
385+
$result[] = $one;
386+
}
371387
}
388+
389+
$this->weeAtrributesCache[$cacheKey] = $result;
372390
}
373391
return $result;
374392
}

0 commit comments

Comments
 (0)