Skip to content

Commit e676f9f

Browse files
committed
MAGETWO-58987: Copy paste detector fails in Tax module
1 parent 86eed24 commit e676f9f

File tree

11 files changed

+456
-309
lines changed

11 files changed

+456
-309
lines changed

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

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\Pricing\PriceCurrencyInterface;
99
use Magento\Store\Model\Store;
1010
use Magento\Customer\Model\Address;
11+
use Magento\Customer\Model\Session;
1112
use Magento\Tax\Model\Config;
1213
use Magento\Customer\Model\Session as CustomerSession;
1314
use Magento\Tax\Api\OrderTaxManagementInterface;
@@ -100,6 +101,13 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
100101
*/
101102
private $serializer;
102103

104+
/**
105+
* Customer session model.
106+
*
107+
* @var Session
108+
*/
109+
protected $customerSession;
110+
103111
/**
104112
* Constructor
105113
*
@@ -114,6 +122,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
114122
* @param OrderTaxManagementInterface $orderTaxManagement
115123
* @param PriceCurrencyInterface $priceCurrency
116124
* @param Json $serializer
125+
* @param Session|null $customerSession
117126
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
118127
*/
119128
public function __construct(
@@ -127,7 +136,8 @@ public function __construct(
127136
\Magento\Catalog\Helper\Data $catalogHelper,
128137
OrderTaxManagementInterface $orderTaxManagement,
129138
PriceCurrencyInterface $priceCurrency,
130-
Json $serializer = null
139+
Json $serializer = null,
140+
Session $customerSession = null
131141
) {
132142
parent::__construct($context);
133143
$this->priceCurrency = $priceCurrency;
@@ -140,6 +150,7 @@ public function __construct(
140150
$this->catalogHelper = $catalogHelper;
141151
$this->orderTaxManagement = $orderTaxManagement;
142152
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
153+
$this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(Session::class);
143154
}
144155

145156
/**
@@ -797,4 +808,95 @@ public function isCatalogPriceDisplayAffectedByTax($store = null)
797808
return $this->displayPriceIncludingTax();
798809
}
799810
}
811+
812+
/**
813+
* Set default Tax Billing and Shipping address into customer session after address save.
814+
*
815+
* @param Address $address
816+
* @return void
817+
*/
818+
public function setAddressCustomerSessionAddressSave(Address $address)
819+
{
820+
if ($this->isDefaultBilling($address)) {
821+
$this->customerSession->setDefaultTaxBillingAddress(
822+
[
823+
'country_id' => $address->getCountryId(),
824+
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
825+
'postcode' => $address->getPostcode(),
826+
]
827+
);
828+
}
829+
if ($this->isDefaultShipping($address)) {
830+
$this->customerSession->setDefaultTaxShippingAddress(
831+
[
832+
'country_id' => $address->getCountryId(),
833+
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
834+
'postcode' => $address->getPostcode(),
835+
]
836+
);
837+
}
838+
}
839+
840+
/**
841+
* Set default Tax Shipping and Billing addresses into customer session after login.
842+
*
843+
* @param \Magento\Customer\Api\Data\AddressInterface[] $addresses
844+
* @return void
845+
*/
846+
public function setAddressCustomerSessionLogIn(array $addresses)
847+
{
848+
$defaultShippingFound = false;
849+
$defaultBillingFound = false;
850+
foreach ($addresses as $address) {
851+
if ($address->isDefaultBilling()) {
852+
$defaultBillingFound = true;
853+
$this->customerSession->setDefaultTaxBillingAddress(
854+
[
855+
'country_id' => $address->getCountryId(),
856+
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
857+
'postcode' => $address->getPostcode(),
858+
]
859+
);
860+
}
861+
if ($address->isDefaultShipping()) {
862+
$defaultShippingFound = true;
863+
$this->customerSession->setDefaultTaxShippingAddress(
864+
[
865+
'country_id' => $address->getCountryId(),
866+
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
867+
'postcode' => $address->getPostcode(),
868+
]
869+
);
870+
}
871+
if ($defaultShippingFound && $defaultBillingFound) {
872+
break;
873+
}
874+
}
875+
}
876+
877+
/**
878+
* Check whether specified billing address is default for customer from address.
879+
*
880+
* @param Address $address
881+
* @return bool
882+
*/
883+
private function isDefaultBilling(Address $address)
884+
{
885+
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
886+
|| $address->getIsPrimaryBilling()
887+
|| $address->getIsDefaultBilling();
888+
}
889+
890+
/**
891+
* Check whether specified shipping address is default for customer from address.
892+
*
893+
* @param Address $address
894+
* @return bool
895+
*/
896+
private function isDefaultShipping(Address $address)
897+
{
898+
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
899+
|| $address->getIsPrimaryShipping()
900+
|| $address->getIsDefaultShipping();
901+
}
800902
}

app/code/Magento/Tax/Observer/AfterAddressSaveObserver.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Tax\Observer;
77

88
use Magento\Customer\Model\Address;
9-
use Magento\Customer\Model\Session;
109
use Magento\Framework\Event\Observer;
1110
use Magento\Framework\Event\ObserverInterface;
1211
use Magento\Framework\Module\Manager;
@@ -15,11 +14,6 @@
1514

1615
class AfterAddressSaveObserver implements ObserverInterface
1716
{
18-
/**
19-
* @var Session
20-
*/
21-
protected $customerSession;
22-
2317
/**
2418
* @var Data
2519
*/
@@ -40,18 +34,15 @@ class AfterAddressSaveObserver implements ObserverInterface
4034
private $cacheConfig;
4135

4236
/**
43-
* @param Session $customerSession
4437
* @param Data $taxHelper
4538
* @param Manager $moduleManager
4639
* @param Config $cacheConfig
4740
*/
4841
public function __construct(
49-
Session $customerSession,
5042
Data $taxHelper,
5143
Manager $moduleManager,
5244
Config $cacheConfig
5345
) {
54-
$this->customerSession = $customerSession;
5546
$this->taxHelper = $taxHelper;
5647
$this->moduleManager = $moduleManager;
5748
$this->cacheConfig = $cacheConfig;
@@ -64,31 +55,13 @@ public function __construct(
6455
*/
6556
public function execute(Observer $observer)
6657
{
67-
if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() &&
68-
$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
58+
if ($this->moduleManager->isEnabled('Magento_PageCache')
59+
&& $this->cacheConfig->isEnabled()
60+
&& $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
61+
) {
6962
/** @var $customerAddress Address */
7063
$address = $observer->getCustomerAddress();
71-
72-
// Check if the address is either the default billing, shipping, or both
73-
if ($this->isDefaultBilling($address)) {
74-
$this->customerSession->setDefaultTaxBillingAddress(
75-
[
76-
'country_id' => $address->getCountryId(),
77-
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
78-
'postcode' => $address->getPostcode(),
79-
]
80-
);
81-
}
82-
83-
if ($this->isDefaultShipping($address)) {
84-
$this->customerSession->setDefaultTaxShippingAddress(
85-
[
86-
'country_id' => $address->getCountryId(),
87-
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
88-
'postcode' => $address->getPostcode(),
89-
]
90-
);
91-
}
64+
$this->taxHelper->setAddressCustomerSessionAddressSave($address);
9265
}
9366
}
9467

app/code/Magento/Tax/Observer/CustomerLoggedInObserver.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,20 @@ public function __construct(
7272
*/
7373
public function execute(Observer $observer)
7474
{
75-
if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() &&
76-
$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
75+
if ($this->moduleManager->isEnabled('Magento_PageCache')
76+
&& $this->cacheConfig->isEnabled()
77+
&& $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
78+
) {
7779
/** @var \Magento\Customer\Model\Data\Customer $customer */
7880
$customer = $observer->getData('customer');
7981
$customerGroupId = $customer->getGroupId();
8082
$customerGroup = $this->groupRepository->getById($customerGroupId);
8183
$customerTaxClassId = $customerGroup->getTaxClassId();
8284
$this->customerSession->setCustomerTaxClassId($customerTaxClassId);
8385

84-
/** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
8586
$addresses = $customer->getAddresses();
8687
if (isset($addresses)) {
87-
$defaultShippingFound = false;
88-
$defaultBillingFound = false;
89-
foreach ($addresses as $address) {
90-
if ($address->isDefaultBilling()) {
91-
$defaultBillingFound = true;
92-
$this->customerSession->setDefaultTaxBillingAddress(
93-
[
94-
'country_id' => $address->getCountryId(),
95-
'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null,
96-
'postcode' => $address->getPostcode(),
97-
]
98-
);
99-
}
100-
if ($address->isDefaultShipping()) {
101-
$defaultShippingFound = true;
102-
$this->customerSession->setDefaultTaxShippingAddress(
103-
[
104-
'country_id' => $address->getCountryId(),
105-
'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null,
106-
'postcode' => $address->getPostcode(),
107-
]
108-
);
109-
}
110-
if ($defaultShippingFound && $defaultBillingFound) {
111-
break;
112-
}
113-
}
88+
$this->taxHelper->setAddressCustomerSessionLogIn($addresses);
11489
}
11590
}
11691
}

0 commit comments

Comments
 (0)