Skip to content

Commit db7a113

Browse files
committed
MC-20244: Pricing :: FPT calculation
- revert caching
1 parent 5ec8aa5 commit db7a113

File tree

1 file changed

+105
-122
lines changed

1 file changed

+105
-122
lines changed

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

Lines changed: 105 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ class Tax extends \Magento\Framework\Model\AbstractModel
9090
*/
9191
protected $accountManagement;
9292

93-
/** @var array */
94-
private $weeAtrributesCache = [];
95-
9693
/**
9794
* @param \Magento\Framework\Model\Context $context
9895
* @param \Magento\Framework\Registry $registry
@@ -261,144 +258,130 @@ public function getProductWeeeAttributes(
261258
$calculateTax = null,
262259
$round = true
263260
) {
264-
$cacheKey = sprintf(
265-
'%s-%s-%s-%s-%s',
266-
$product->getId(),
267-
$shipping && $shipping->getId() ? $shipping->getId() : '',
268-
$billing && $billing->getId() ? $billing->getId() : '',
269-
$website && $website->getId() ? $website->getId() : $website ? $website : '',
270-
$calculateTax,
271-
$round
272-
);
273261
$result = [];
274-
if (isset($this->weeAtrributesCache[$cacheKey])) {
275-
$result = $this->weeAtrributesCache[$cacheKey];
276-
} else {
277-
$websiteId = null;
278-
/** @var \Magento\Store\Model\Store $store */
279-
$store = null;
280-
if (!$website) {
281-
$store = $product->getStore();
282-
if ($store) {
283-
$websiteId = $store->getWebsiteId();
284-
}
285-
}
286-
if (!$websiteId) {
287-
$websiteObject = $this->_storeManager->getWebsite($website);
288-
$websiteId = $websiteObject->getId();
289-
$store = $websiteObject->getDefaultGroup()->getDefaultStore();
262+
$websiteId = null;
263+
/** @var \Magento\Store\Model\Store $store */
264+
$store = null;
265+
if (!$website) {
266+
$store = $product->getStore();
267+
if ($store) {
268+
$websiteId = $store->getWebsiteId();
290269
}
270+
}
271+
if (!$websiteId) {
272+
$websiteObject = $this->_storeManager->getWebsite($website);
273+
$websiteId = $websiteObject->getId();
274+
$store = $websiteObject->getDefaultGroup()->getDefaultStore();
275+
}
291276

292-
$allWeee = $this->getWeeeTaxAttributeCodes($store);
293-
if (!$allWeee) {
294-
return $result;
295-
}
277+
$allWeee = $this->getWeeeTaxAttributeCodes($store);
278+
if (!$allWeee) {
279+
return $result;
280+
}
296281

297-
/** @var Calculation $calculator */
298-
$calculator = $this->_calculationFactory->create();
282+
/** @var Calculation $calculator */
283+
$calculator = $this->_calculationFactory->create();
299284

300-
$customerId = $this->_customerSession->getCustomerId();
301-
if ($shipping && $shipping->getCountryId()) {
302-
$customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
285+
$customerId = $this->_customerSession->getCustomerId();
286+
if ($shipping && $shipping->getCountryId()) {
287+
$customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
288+
} else {
289+
// if customer logged use it default shipping and billing address
290+
if ($customerId) {
291+
$shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
292+
$billing = $this->accountManagement->getDefaultBillingAddress($customerId);
293+
$customerTaxClass = null;
303294
} else {
304-
// if customer logged use it default shipping and billing address
305-
if ($customerId) {
306-
$shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
307-
$billing = $this->accountManagement->getDefaultBillingAddress($customerId);
308-
$customerTaxClass = null;
309-
} else {
310-
$shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
311-
$billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
312-
if (!empty($billingAddressArray)) {
313-
$billing = new \Magento\Framework\DataObject($billingAddressArray);
314-
}
315-
if (!empty($shippingAddressArray)) {
316-
$shipping = new \Magento\Framework\DataObject($shippingAddressArray);
317-
}
318-
$customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
295+
$shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
296+
$billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
297+
if (!empty($billingAddressArray)) {
298+
$billing = new \Magento\Framework\DataObject($billingAddressArray);
319299
}
300+
if (!empty($shippingAddressArray)) {
301+
$shipping = new \Magento\Framework\DataObject($shippingAddressArray);
302+
}
303+
$customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
320304
}
305+
}
321306

322-
$rateRequest = $calculator->getRateRequest(
323-
$shipping,
324-
$billing,
325-
$customerTaxClass,
326-
$store,
327-
$customerId
328-
);
329-
$defaultRateRequest = $calculator->getDefaultRateRequest($store);
330-
331-
$productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
332-
$rateRequest->getCountryId(),
333-
$rateRequest->getRegionId(),
334-
$websiteId,
335-
$store->getId(),
336-
$product->getId()
337-
);
338-
339-
foreach ($productAttributes as $attribute) {
340-
$value = $attribute['weee_value'];
341-
if ($value) {
342-
$taxAmount = $amount = 0;
343-
$amount = $value;
344-
$amountExclTax = $value;
345-
if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
346-
/** @var Calculation $calculator */
347-
$defaultPercent = $calculator->getRate(
348-
$defaultRateRequest->setProductClassId($product->getTaxClassId())
349-
);
350-
$currentPercent = $calculator->getRate(
351-
$rateRequest->setProductClassId($product->getTaxClassId())
352-
);
353-
if ($this->_taxData->priceIncludesTax($store)) {
354-
$amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
355-
if ($round) {
356-
$amountInclTax = $this->priceCurrency->round($amountInclTax);
357-
}
358-
$taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
359-
if ($round) {
360-
$taxAmount = $this->priceCurrency->round($taxAmount);
361-
}
362-
$amountExclTax = $amountInclTax - $taxAmount;
363-
} else {
364-
$appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
365-
if (is_array($appliedRates) && count($appliedRates) > 1) {
366-
$taxAmount = 0;
367-
foreach ($appliedRates as $appliedRate) {
368-
$taxRate = $appliedRate['percent'];
369-
if ($round) {
370-
$taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
371-
} else {
372-
$taxAmount += $value * $taxRate / 100;
373-
}
374-
}
375-
} else {
307+
$rateRequest = $calculator->getRateRequest(
308+
$shipping,
309+
$billing,
310+
$customerTaxClass,
311+
$store,
312+
$customerId
313+
);
314+
$defaultRateRequest = $calculator->getDefaultRateRequest($store);
315+
316+
$productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
317+
$rateRequest->getCountryId(),
318+
$rateRequest->getRegionId(),
319+
$websiteId,
320+
$store->getId(),
321+
$product->getId()
322+
);
323+
324+
foreach ($productAttributes as $attribute) {
325+
$value = $attribute['weee_value'];
326+
if ($value) {
327+
$taxAmount = $amount = 0;
328+
$amount = $value;
329+
$amountExclTax = $value;
330+
if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
331+
/** @var Calculation $calculator */
332+
$defaultPercent = $calculator->getRate(
333+
$defaultRateRequest->setProductClassId($product->getTaxClassId())
334+
);
335+
$currentPercent = $calculator->getRate(
336+
$rateRequest->setProductClassId($product->getTaxClassId())
337+
);
338+
if ($this->_taxData->priceIncludesTax($store)) {
339+
$amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
340+
if ($round) {
341+
$amountInclTax = $this->priceCurrency->round($amountInclTax);
342+
}
343+
$taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
344+
if ($round) {
345+
$taxAmount = $this->priceCurrency->round($taxAmount);
346+
}
347+
$amountExclTax = $amountInclTax - $taxAmount;
348+
} else {
349+
$appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
350+
if (is_array($appliedRates) && count($appliedRates) > 1) {
351+
$taxAmount = 0;
352+
foreach ($appliedRates as $appliedRate) {
353+
$taxRate = $appliedRate['percent'];
376354
if ($round) {
377-
$taxAmount = $this->priceCurrency->round(
378-
$value * $currentPercent / 100
379-
);
355+
$taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
380356
} else {
381-
$taxAmount = $value * $currentPercent / 100;
357+
$taxAmount += $value * $taxRate / 100;
382358
}
383359
}
360+
} else {
361+
if ($round) {
362+
$taxAmount = $this->priceCurrency->round(
363+
$value * $currentPercent / 100
364+
);
365+
} else {
366+
$taxAmount = $value * $currentPercent / 100;
367+
}
384368
}
385369
}
370+
}
386371

387-
$one = new \Magento\Framework\DataObject();
388-
$one->setName(
389-
$attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
390-
)
391-
->setAmount($amount)
392-
->setTaxAmount($taxAmount)
393-
->setAmountExclTax($amountExclTax)
394-
->setCode($attribute['attribute_code']);
372+
$one = new \Magento\Framework\DataObject();
373+
$one->setName(
374+
$attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
375+
)
376+
->setAmount($amount)
377+
->setTaxAmount($taxAmount)
378+
->setAmountExclTax($amountExclTax)
379+
->setCode($attribute['attribute_code']);
395380

396-
$result[] = $one;
397-
}
381+
$result[] = $one;
398382
}
399-
400-
$this->weeAtrributesCache[$cacheKey] = $result;
401383
}
384+
402385
return $result;
403386
}
404387

0 commit comments

Comments
 (0)