@@ -88,6 +88,9 @@ class Tax extends \Magento\Framework\Model\AbstractModel
88
88
*/
89
89
protected $ accountManagement ;
90
90
91
+ /** @var array */
92
+ private $ weeAtrributesCache = [];
93
+
91
94
/**
92
95
* @param \Magento\Framework\Model\Context $context
93
96
* @param \Magento\Framework\Registry $registry
@@ -247,128 +250,143 @@ public function getProductWeeeAttributes(
247
250
$ calculateTax = null ,
248
251
$ round = true
249
252
) {
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
+ );
250
262
$ 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 ();
258
279
}
259
- }
260
- if (!$ websiteId ) {
261
- $ websiteObject = $ this ->_storeManager ->getWebsite ($ website );
262
- $ websiteId = $ websiteObject ->getId ();
263
- $ store = $ websiteObject ->getDefaultGroup ()->getDefaultStore ();
264
- }
265
280
266
- $ allWeee = $ this ->getWeeeTaxAttributeCodes ($ store );
267
- if (!$ allWeee ) {
268
- return $ result ;
269
- }
281
+ $ allWeee = $ this ->getWeeeTaxAttributeCodes ($ store );
282
+ if (!$ allWeee ) {
283
+ return $ result ;
284
+ }
270
285
271
- /** @var \Magento\Tax\Model\Calculation $calculator */
272
- $ calculator = $ this ->_calculationFactory ->create ();
286
+ /** @var \Magento\Tax\Model\Calculation $calculator */
287
+ $ calculator = $ this ->_calculationFactory ->create ();
273
288
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 ();
283
292
} 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 ();
291
308
}
292
- $ customerTaxClass = $ this ->_customerSession ->getCustomerTaxClassId ();
293
309
}
294
- }
295
310
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 );
312
319
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 );
348
346
}
349
- } else {
347
+ $ taxAmount = $ amountInclTax - $ amountInclTax / ( 100 + $ currentPercent ) * 100 ;
350
348
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
+ }
354
364
} 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
+ }
356
372
}
357
373
}
358
374
}
359
- }
360
375
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 ' ]);
369
384
370
- $ result [] = $ one ;
385
+ $ result [] = $ one ;
386
+ }
371
387
}
388
+
389
+ $ this ->weeAtrributesCache [$ cacheKey ] = $ result ;
372
390
}
373
391
return $ result ;
374
392
}
0 commit comments