Skip to content

Commit fadc995

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-31563' into 2.4-develop-pr119
2 parents 72e57bd + d523c82 commit fadc995

File tree

5 files changed

+267
-27
lines changed

5 files changed

+267
-27
lines changed

app/code/Magento/Quote/Model/Quote/Address.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,13 @@ public function requestShippingRates(AbstractItem $item = null)
11151115
*/
11161116
public function getTotals()
11171117
{
1118-
$totalsData = array_merge($this->getData(), ['address_quote_items' => $this->getAllItems()]);
1118+
$totalsData = array_merge(
1119+
$this->getData(),
1120+
[
1121+
'address_quote_items' => $this->getAllItems(),
1122+
'quote_items' => $this->getQuote()->getAllItems(),
1123+
]
1124+
);
11191125
$totals = $this->totalsReader->fetch($this->getQuote(), $totalsData);
11201126
foreach ($totals as $total) {
11211127
$this->addTotal($total);

app/code/Magento/Weee/Model/Total/Quote/WeeeTax.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
*/
66
namespace Magento\Weee\Model\Total\Quote;
77

8+
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
9+
use Magento\Quote\Model\Quote;
10+
use Magento\Quote\Model\Quote\Address;
11+
use Magento\Quote\Model\Quote\Address\Total;
812
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
9-
use Magento\Store\Model\Store;
1013
use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;
1114

1215
class WeeeTax extends Weee
1316
{
1417
/**
1518
* Collect Weee taxes amount and prepare items prices for taxation and discount
1619
*
17-
* @param \Magento\Quote\Model\Quote $quote
18-
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface|\Magento\Quote\Model\Quote\Address $shippingAssignment
19-
* @param \Magento\Quote\Model\Quote\Address\Total $total
20+
* @param Quote $quote
21+
* @param ShippingAssignmentInterface|Address $shippingAssignment
22+
* @param Total $total
2023
* @return $this
2124
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2225
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2326
*/
2427
public function collect(
25-
\Magento\Quote\Model\Quote $quote,
26-
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
27-
\Magento\Quote\Model\Quote\Address\Total $total
28+
Quote $quote,
29+
ShippingAssignmentInterface $shippingAssignment,
30+
Total $total
2831
) {
29-
\Magento\Quote\Model\Quote\Address\Total\AbstractTotal::collect($quote, $shippingAssignment, $total);
32+
AbstractTotal::collect($quote, $shippingAssignment, $total);
3033
$this->_store = $quote->getStore();
3134
if (!$this->weeeData->isEnabled($this->_store)) {
3235
return $this;
@@ -65,13 +68,12 @@ public function collect(
6568
$weeeCodeToWeeeTaxDetailsMap[$weeeCode] = $weeeTaxDetails;
6669
}
6770
}
68-
71+
$productTaxes = [];
6972
//Process each item that has taxable weee
7073
foreach ($itemToWeeeCodeMap as $mapping) {
7174
$item = $mapping['item'];
7275

7376
$this->weeeData->setApplied($item, []);
74-
$productTaxes = [];
7577

7678
$totalValueInclTax = 0;
7779
$baseTotalValueInclTax = 0;
@@ -113,15 +115,17 @@ public function collect(
113115
$baseTotalRowValueExclTax += $baseRowValueExclTax;
114116

115117
$productTaxes[] = [
116-
'title' => $attributeCode, //TODO: fix this
117-
'base_amount' => $baseValueExclTax,
118-
'amount' => $valueExclTax,
119-
'row_amount' => $rowValueExclTax,
120-
'base_row_amount' => $baseRowValueExclTax,
121-
'base_amount_incl_tax' => $baseValueInclTax,
122-
'amount_incl_tax' => $valueInclTax,
123-
'row_amount_incl_tax' => $rowValueInclTax,
124-
'base_row_amount_incl_tax' => $baseRowValueInclTax,
118+
[
119+
'title' => $attributeCode, //TODO: fix this
120+
'base_amount' => $baseValueExclTax,
121+
'amount' => $valueExclTax,
122+
'row_amount' => $rowValueExclTax,
123+
'base_row_amount' => $baseRowValueExclTax,
124+
'base_amount_incl_tax' => $baseValueInclTax,
125+
'amount_incl_tax' => $valueInclTax,
126+
'row_amount_incl_tax' => $rowValueInclTax,
127+
'base_row_amount_incl_tax' => $baseRowValueInclTax,
128+
],
125129
];
126130
}
127131

@@ -143,8 +147,11 @@ public function collect(
143147
$baseTotalRowValueInclTax
144148
);
145149

146-
$this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
147150
}
151+
$this->weeeData->setApplied(
152+
$item,
153+
array_merge($this->weeeData->getApplied($item), ...$productTaxes)
154+
);
148155
}
149156
return $this;
150157
}
@@ -196,7 +203,7 @@ protected function createItemToWeeeCodeMapping($weeeCodeToItemMap)
196203
/**
197204
* Process row amount based on FPT total amount configuration setting
198205
*
199-
* @param \Magento\Quote\Model\Quote\Address\Total $total
206+
* @param Total $total
200207
* @param float $rowValueExclTax
201208
* @param float $baseRowValueExclTax
202209
* @param float $rowValueInclTax
@@ -226,14 +233,13 @@ protected function processTotalAmount(
226233
/**
227234
* Fetch the Weee total amount for display in totals block when building the initial quote
228235
*
229-
* @param \Magento\Quote\Model\Quote $quote
230-
* @param \Magento\Quote\Model\Quote\Address\Total $total
236+
* @param Quote $quote
237+
* @param Total $total
231238
* @return array
232239
*/
233-
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
240+
public function fetch(Quote $quote, Total $total)
234241
{
235-
/** @var $items \Magento\Sales\Model\Order\Item[] */
236-
$items = isset($total['address_quote_items']) ? $total['address_quote_items'] : [];
242+
$items = $total['quote_items'] ?? [];
237243

238244
$weeeTotal = $this->weeeData->getTotalAmounts($items, $quote->getStore());
239245
if ($weeeTotal) {
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+
declare(strict_types=1);
7+
8+
namespace Magento\Weee\Model\Total\Quote;
9+
10+
use Magento\Checkout\Api\Data\TotalsInformationInterface;
11+
use Magento\Checkout\Model\TotalsInformationManagement;
12+
use Magento\Quote\Model\Quote\Address;
13+
use Magento\Quote\Model\Quote\AddressFactory;
14+
use Magento\Quote\Model\QuoteFactory;
15+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
16+
use Magento\TestFramework\ObjectManager;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Quote totals calculate tests class
21+
*/
22+
class CalculateTest extends TestCase
23+
{
24+
/**
25+
* @var ObjectManager
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var TotalsInformationManagement
31+
*/
32+
private $totalsManagement;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
$this->objectManager = ObjectManager::getInstance();
40+
$this->totalsManagement = $this->objectManager->get(TotalsInformationManagement::class);
41+
}
42+
43+
/**
44+
* Multishipping quote with FPT Weee TAX totals calculation test
45+
*
46+
* @magentoDataFixture Magento/Weee/_files/quote_multishipping.php
47+
* @magentoConfigFixture default_store tax/weee/enable 1
48+
* @magentoConfigFixture default_store tax/weee/apply_vat 1
49+
*/
50+
public function testGetWeeTaxTotals()
51+
{
52+
/** @var QuoteFactory $quoteFactory */
53+
$quoteFactory = $this->objectManager->get(QuoteFactory::class);
54+
/** @var QuoteResource $quoteResource */
55+
$quoteResource = $this->objectManager->get(QuoteResource::class);
56+
$quote = $quoteFactory->create();
57+
$quoteResource->load($quote, 'multishipping_fpt_quote_id', 'reserved_order_id');
58+
$cartId = $quote->getId();
59+
$addressFactory = $this->objectManager->get(AddressFactory::class);
60+
/** @var Address $newAddress */
61+
$newAddress = $addressFactory->create()->setAddressType('shipping');
62+
$newAddress->setCountryId('US')->setRegionId(12)->setRegion('California')->setPostcode('90230');
63+
$addressInformation = $this->objectManager->create(
64+
TotalsInformationInterface::class,
65+
[
66+
'data' => [
67+
'address' => $newAddress,
68+
'shipping_method_code' => 'flatrate',
69+
'shipping_carrier_code' => 'flatrate',
70+
],
71+
]
72+
);
73+
74+
$actual = $this->totalsManagement->calculate($cartId, $addressInformation);
75+
76+
$items = $actual->getTotalSegments();
77+
$this->assertTrue(array_key_exists('weee_tax', $items));
78+
$this->assertEquals(25.4, $items['weee_tax']->getValue());
79+
}
80+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Quote\Api\Data\AddressInterface;
12+
use Magento\Quote\Api\Data\PaymentInterface;
13+
use Magento\Quote\Model\Quote;
14+
use Magento\Quote\Model\Quote\Address\Rate;
15+
use Magento\Quote\Model\Quote\Item;
16+
use Magento\Quote\Model\QuoteFactory;
17+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\ObjectManager;
21+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
22+
23+
/** @var ObjectManager $objectManager */
24+
$objectManager = Bootstrap::getObjectManager();
25+
/** @var QuoteFactory $quoteFactory */
26+
$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class);
27+
/** @var QuoteResource $quoteResource */
28+
$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class);
29+
/** @var StoreManagerInterface $storeManager */
30+
$storeManager = $objectManager->get(StoreManagerInterface::class);
31+
/** @var CartRepositoryInterface $quoteRepository */
32+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
33+
$store = $storeManager->getStore();
34+
/** @var Quote $quote */
35+
$quote = $quoteFactory->create();
36+
$quote->setReservedOrderId('multishipping_fpt_quote_id')
37+
->setCustomerEmail('customer001@test.com')
38+
->setStoreId($storeManager->getStore()->getId());
39+
40+
$shipping = [
41+
'firstname' => 'Jonh',
42+
'lastname' => 'Doe',
43+
'telephone' => '0333-233-221',
44+
'street' => ['Main Division 1'],
45+
'city' => 'Culver City',
46+
'region' => 'CA',
47+
'postcode' => 90800,
48+
'country_id' => 'US',
49+
'email' => 'customer001@shipping.test',
50+
'address_type' => 'shipping',
51+
];
52+
$methodCode = 'flatrate_flatrate';
53+
/** @var Rate $rate */
54+
$rate = $objectManager->create(Rate::class);
55+
$rate->setCode($methodCode)
56+
->setPrice(5.00);
57+
58+
$address = $objectManager->create(AddressInterface::class, ['data' => $shipping]);
59+
$address->setShippingMethod($methodCode)
60+
->addShippingRate($rate)
61+
->setShippingAmount(5.00)
62+
->setBaseShippingAmount(5.00);
63+
64+
$quote->addAddress($address);
65+
66+
/** @var AddressInterface $address */
67+
$address = $objectManager->create(
68+
AddressInterface::class,
69+
[
70+
'data' => [
71+
'firstname' => 'Jonh',
72+
'lastname' => 'Doe',
73+
'telephone' => '0333-233-221',
74+
'street' => ['Third Division 1'],
75+
'city' => 'New York',
76+
'region' => 'NY',
77+
'postcode' => 10029,
78+
'country_id' => 'US',
79+
'email' => 'customer001@billing.test',
80+
'address_type' => 'billing',
81+
],
82+
]
83+
);
84+
$quote->setBillingAddress($address);
85+
86+
$quote->setIsMultiShipping(1);
87+
$quoteRepository->save($quote);
88+
89+
/** @var ProductRepositoryInterface $productRepository */
90+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
91+
Resolver::getInstance()->requireDataFixture('Magento/Weee/_files/product_with_fpt.php');
92+
/** @var Product $product */
93+
$product = $productRepository->get('simple-with-ftp');
94+
95+
/** @var Item $item */
96+
$item = $objectManager->create(Item::class);
97+
$item->setProduct($product)
98+
->setPrice($product->getPrice())
99+
->setQty(2);
100+
$quote->addItem($item);
101+
$quoteRepository->save($quote);
102+
103+
$addressList = $quote->getAllShippingAddresses();
104+
$address = reset($addressList);
105+
$item->setQty(2);
106+
$address->setTotalQty(2);
107+
$address->addItem($item);
108+
$quoteRepository->save($quote);
109+
110+
$quote = $quoteFactory->create();
111+
$quoteResource->load($quote, 'multishipping_fpt_quote_id', 'reserved_order_id');
112+
/** @var PaymentInterface $payment */
113+
$payment = $objectManager->create(PaymentInterface::class);
114+
$payment->setMethod('checkmo');
115+
$quote->setPayment($payment);
116+
$quote->collectTotals();
117+
$quoteRepository->save($quote);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Magento\Quote\Api\CartRepositoryInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\ObjectManager;
12+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
13+
14+
/** @var ObjectManager $objectManager */
15+
$objectManager = Bootstrap::getObjectManager();
16+
17+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
18+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
19+
$searchCriteria = $searchCriteriaBuilder
20+
->addFilter('reserved_order_id', 'multishipping_fpt_quote_id')
21+
->create();
22+
/** @var CartRepositoryInterface $quoteRepository */
23+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
24+
$quotesList = $quoteRepository->getList($searchCriteria)->getItems();
25+
26+
if (!empty($quotesList)) {
27+
$quote = array_pop($quotesList);
28+
$quoteRepository->delete($quote);
29+
}
30+
31+
Resolver::getInstance()->requireDataFixture('Magento/Weee/_files/product_with_fpt_rollback.php');

0 commit comments

Comments
 (0)