Skip to content

Commit 60d9ca9

Browse files
committed
Merge branch 'MAGETWO-71307' into MPI-PR
2 parents ce3d259 + 9eb49b2 commit 60d9ca9

File tree

7 files changed

+334
-35
lines changed

7 files changed

+334
-35
lines changed

app/code/Magento/Weee/Helper/Data.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
namespace Magento\Weee\Helper;
77

8-
use Magento\Catalog\Model\Product\Type;
8+
use Magento\Sales\Model\Order\Item as OrderItem;
9+
use Magento\Quote\Model\Quote\Item\AbstractItem as QuoteAbstractItem;
910
use Magento\Store\Model\Store;
1011
use Magento\Store\Model\Website;
1112
use Magento\Weee\Model\Tax as WeeeDisplayConfig;
@@ -313,7 +314,7 @@ public function getProductWeeeAttributes(
313314
/**
314315
* Returns applied weee tax amount
315316
*
316-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
317+
* @param QuoteAbstractItem $item
317318
* @return float
318319
*/
319320
public function getWeeeTaxAppliedAmount($item)
@@ -324,32 +325,45 @@ public function getWeeeTaxAppliedAmount($item)
324325
/**
325326
* Returns applied weee tax amount for the row
326327
*
327-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
328+
* @param QuoteAbstractItem $item
328329
* @return float
329330
*/
330331
public function getWeeeTaxAppliedRowAmount($item)
331332
{
332333
return $this->getRecursiveNumericAmount($item, 'getWeeeTaxAppliedRowAmount');
333334
}
334335

336+
/**
337+
* Returns applied base weee tax amount for the row
338+
*
339+
* @param QuoteAbstractItem|OrderItem $item
340+
* @return float
341+
*/
342+
public function getBaseWeeeTaxAppliedRowAmount($item)
343+
{
344+
return $this->getRecursiveNumericAmount($item, 'getBaseWeeeTaxAppliedRowAmnt');
345+
}
346+
335347
/**
336348
* Returns accumulated amounts for the item
337349
*
338-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
350+
* @param QuoteAbstractItem|OrderItem $item
339351
* @param string $functionName
340352
* @return float
341353
*/
342354
protected function getRecursiveNumericAmount($item, $functionName)
343355
{
344-
if ($item instanceof \Magento\Quote\Model\Quote\Item\AbstractItem) {
356+
if ($item instanceof QuoteAbstractItem || $item instanceof OrderItem) {
345357
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
346358
$result = 0;
347-
foreach ($item->getChildren() as $child) {
359+
$children = $item instanceof OrderItem ? $item->getChildrenItems() : $item->getChildren();
360+
foreach ($children as $child) {
348361
$childData = $this->getRecursiveNumericAmount($child, $functionName);
349362
if (!empty($childData)) {
350363
$result += $childData;
351364
}
352365
}
366+
353367
return $result;
354368
}
355369
}
@@ -364,12 +378,12 @@ protected function getRecursiveNumericAmount($item, $functionName)
364378
/**
365379
* Returns applied weee taxes
366380
*
367-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
381+
* @param QuoteAbstractItem $item
368382
* @return array
369383
*/
370384
public function getApplied($item)
371385
{
372-
if ($item instanceof \Magento\Quote\Model\Quote\Item\AbstractItem) {
386+
if ($item instanceof QuoteAbstractItem) {
373387
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
374388
$result = [];
375389
foreach ($item->getChildren() as $child) {
@@ -393,7 +407,7 @@ public function getApplied($item)
393407
/**
394408
* Sets applied weee taxes
395409
*
396-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
410+
* @param QuoteAbstractItem $item
397411
* @param array $value
398412
* @return $this
399413
*/
@@ -460,7 +474,7 @@ public function getProductWeeeAttributesForRenderer(
460474
/**
461475
* Get the weee tax including tax
462476
*
463-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
477+
* @param QuoteAbstractItem $item
464478
* @return float
465479
*/
466480
public function getWeeeTaxInclTax($item)
@@ -476,7 +490,7 @@ public function getWeeeTaxInclTax($item)
476490
/**
477491
* Get the total base weee tax
478492
*
479-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
493+
* @param QuoteAbstractItem $item
480494
* @return float
481495
*/
482496
public function getBaseWeeeTaxInclTax($item)
@@ -492,7 +506,7 @@ public function getBaseWeeeTaxInclTax($item)
492506
/**
493507
* Get the total weee including tax by row
494508
*
495-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
509+
* @param QuoteAbstractItem $item
496510
* @return float
497511
*/
498512
public function getRowWeeeTaxInclTax($item)
@@ -508,7 +522,7 @@ public function getRowWeeeTaxInclTax($item)
508522
/**
509523
* Get the total base weee including tax by row
510524
*
511-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
525+
* @param QuoteAbstractItem $item
512526
* @return float
513527
*/
514528
public function getBaseRowWeeeTaxInclTax($item)
@@ -524,7 +538,7 @@ public function getBaseRowWeeeTaxInclTax($item)
524538
/**
525539
* Get the total tax applied on weee by unit
526540
*
527-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
541+
* @param QuoteAbstractItem $item
528542
* @return float
529543
*/
530544
public function getTotalTaxAppliedForWeeeTax($item)
@@ -544,7 +558,7 @@ public function getTotalTaxAppliedForWeeeTax($item)
544558
/**
545559
* Get the total tax applied on weee by unit
546560
*
547-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
561+
* @param QuoteAbstractItem $item
548562
* @return float
549563
*/
550564
public function getBaseTotalTaxAppliedForWeeeTax($item)
@@ -562,7 +576,7 @@ public function getBaseTotalTaxAppliedForWeeeTax($item)
562576
}
563577

564578
/**
565-
* @param \Magento\Sales\Model\Order\Item $orderItem
579+
* @param OrderItem $orderItem
566580
* @return float
567581
*/
568582
public function getWeeeAmountInvoiced($orderItem)
@@ -579,7 +593,7 @@ public function getWeeeAmountInvoiced($orderItem)
579593
}
580594

581595
/**
582-
* @param \Magento\Sales\Model\Order\Item $orderItem
596+
* @param OrderItem $orderItem
583597
* @return float
584598
*/
585599
public function getBaseWeeeAmountInvoiced($orderItem)
@@ -596,7 +610,7 @@ public function getBaseWeeeAmountInvoiced($orderItem)
596610
}
597611

598612
/**
599-
* @param \Magento\Sales\Model\Order\Item $orderItem
613+
* @param OrderItem $orderItem
600614
* @return float
601615
*/
602616
public function getWeeeTaxAmountInvoiced($orderItem)
@@ -613,7 +627,7 @@ public function getWeeeTaxAmountInvoiced($orderItem)
613627
}
614628

615629
/**
616-
* @param \Magento\Sales\Model\Order\Item $orderItem
630+
* @param OrderItem $orderItem
617631
* @return float
618632
*/
619633
public function getBaseWeeeTaxAmountInvoiced($orderItem)
@@ -630,7 +644,7 @@ public function getBaseWeeeTaxAmountInvoiced($orderItem)
630644
}
631645

632646
/**
633-
* @param \Magento\Sales\Model\Order\Item $orderItem
647+
* @param OrderItem $orderItem
634648
* @return float
635649
*/
636650
public function getWeeeAmountRefunded($orderItem)
@@ -647,7 +661,7 @@ public function getWeeeAmountRefunded($orderItem)
647661
}
648662

649663
/**
650-
* @param \Magento\Sales\Model\Order\Item $orderItem
664+
* @param OrderItem $orderItem
651665
* @return float
652666
*/
653667
public function getBaseWeeeAmountRefunded($orderItem)
@@ -664,7 +678,7 @@ public function getBaseWeeeAmountRefunded($orderItem)
664678
}
665679

666680
/**
667-
* @param \Magento\Sales\Model\Order\Item $orderItem
681+
* @param OrderItem $orderItem
668682
* @return float
669683
*/
670684
public function getWeeeTaxAmountRefunded($orderItem)
@@ -681,7 +695,7 @@ public function getWeeeTaxAmountRefunded($orderItem)
681695
}
682696

683697
/**
684-
* @param \Magento\Sales\Model\Order\Item $orderItem
698+
* @param OrderItem $orderItem
685699
* @return float
686700
*/
687701
public function getBaseWeeeTaxAmountRefunded($orderItem)
@@ -700,7 +714,7 @@ public function getBaseWeeeTaxAmountRefunded($orderItem)
700714
/**
701715
* Returns the total amount of FPT across all items. Used for displaying the FPT totals line item.
702716
*
703-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem[] $items
717+
* @param QuoteAbstractItem[] $items
704718
* @param null|string|bool|int|Store $store
705719
* @return float
706720
*/
@@ -721,7 +735,7 @@ public function getTotalAmounts($items, $store = null)
721735
/**
722736
* Returns the base total amount of FPT across all items. Used for displaying the FPT totals line item.
723737
*
724-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem[] $items
738+
* @param QuoteAbstractItem[] $items
725739
* @param null|string|bool|int|Store $store
726740
* @return float
727741
* @since 100.1.0
@@ -734,7 +748,7 @@ public function getBaseTotalAmounts($items, $store = null)
734748
if ($displayTotalsInclTax) {
735749
$baseWeeeTotal += $this->getBaseRowWeeeTaxInclTax($item);
736750
} else {
737-
$baseWeeeTotal += $item->getBaseWeeeTaxAppliedRowAmount();
751+
$baseWeeeTotal += $item->getBaseWeeeTaxAppliedRowAmnt();
738752
}
739753
}
740754
return $baseWeeeTotal;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Weee\Observer;
7+
8+
use Magento\Framework\Event\Observer;
9+
use Magento\Framework\Event\ObserverInterface;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Weee\Helper\Data;
12+
13+
/**
14+
* Add Weee item to Payment Cart amount.
15+
*/
16+
class AddPaymentWeeeItem implements ObserverInterface
17+
{
18+
/**
19+
* @var Data
20+
*/
21+
private $weeeData;
22+
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @param Data $weeeData
30+
* @param StoreManagerInterface $storeManager
31+
*/
32+
public function __construct(
33+
Data $weeeData,
34+
StoreManagerInterface $storeManager
35+
) {
36+
$this->weeeData = $weeeData;
37+
$this->storeManager = $storeManager;
38+
}
39+
40+
/**
41+
* Add FPT amount as custom item to payment cart totals.
42+
*
43+
* @param Observer $observer
44+
* @return void
45+
*/
46+
public function execute(Observer $observer)
47+
{
48+
if ($this->shouldBeAddedAsCustomItem() === false) {
49+
return;
50+
}
51+
52+
/** @var \Magento\Payment\Model\Cart $cart */
53+
$cart = $observer->getEvent()->getCart();
54+
$salesEntity = $cart->getSalesModel();
55+
56+
$totalWeee = 0;
57+
foreach ($salesEntity->getAllItems() as $item) {
58+
$originalItem = $item->getOriginalItem();
59+
if (!$originalItem->getParentItem()) {
60+
$totalWeee += $this->weeeData->getBaseWeeeTaxAppliedRowAmount($originalItem);
61+
}
62+
}
63+
64+
if ($totalWeee > 0.0001) {
65+
$cart->addCustomItem(__('FPT'), 1, $totalWeee);
66+
}
67+
}
68+
69+
/**
70+
* Checks if FPT should be added to payment cart as custom item or not.
71+
*
72+
* @return bool
73+
*/
74+
private function shouldBeAddedAsCustomItem()
75+
{
76+
$storeId = $this->storeManager->getStore()->getId();
77+
78+
return $this->weeeData->isEnabled($storeId) && $this->weeeData->includeInSubtotal($storeId) === false;
79+
}
80+
}

app/code/Magento/Weee/Test/Unit/Helper/DataTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,19 +486,19 @@ public function testGetBaseTotalAmounts()
486486
$expected = $item1BaseWeee + $item2BaseWeee;
487487
$itemProductSimple1 = $this->createPartialMock(
488488
\Magento\Quote\Model\Quote\Item::class,
489-
['getBaseWeeeTaxAppliedRowAmount']
489+
['getBaseWeeeTaxAppliedRowAmnt']
490490
);
491491
$itemProductSimple2 = $this->createPartialMock(
492492
\Magento\Quote\Model\Quote\Item::class,
493-
['getBaseWeeeTaxAppliedRowAmount']
493+
['getBaseWeeeTaxAppliedRowAmnt']
494494
);
495495
$items = [$itemProductSimple1, $itemProductSimple2];
496496

497497
$itemProductSimple1->expects($this->any())
498-
->method('getBaseWeeeTaxAppliedRowAmount')
498+
->method('getBaseWeeeTaxAppliedRowAmnt')
499499
->willReturn($item1BaseWeee);
500500
$itemProductSimple2->expects($this->any())
501-
->method('getBaseWeeeTaxAppliedRowAmount')
501+
->method('getBaseWeeeTaxAppliedRowAmnt')
502502
->willReturn($item2BaseWeee);
503503

504504
$this->assertEquals($expected, $this->helperData->getBaseTotalAmounts($items));

app/code/Magento/Weee/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515
<event name="catalog_product_view_config">
1616
<observer name="weee" instance="Magento\Weee\Observer\UpdateProductOptionsObserver" />
1717
</event>
18+
<event name="payment_cart_collect_items_and_amounts">
19+
<observer name="magento_weee" instance="Magento\Weee\Observer\AddPaymentWeeeItem" />
20+
</event>
1821
</config>

0 commit comments

Comments
 (0)