Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 4d613f2

Browse files
author
Oleksii Korshenko
authored
MAGETWO-81680: Fix #10856: Sync billing with shipping address on Admin Order Page #11385
2 parents 3f8b857 + 8e8cce8 commit 4d613f2

File tree

5 files changed

+134
-60
lines changed

5 files changed

+134
-60
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ public function validateMinimumAmount()
11701170
*/
11711171
public function getAppliedTaxes()
11721172
{
1173-
return $this->serializer->unserialize($this->getData('applied_taxes'));
1173+
$taxes = $this->getData('applied_taxes');
1174+
return $taxes ? $this->serializer->unserialize($taxes) : [];
11741175
}
11751176

11761177
/**

app/code/Magento/Quote/Test/Unit/Model/Quote/AddressTest.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magento\Quote\Test\Unit\Model\Quote;
1010

1111
use Magento\Directory\Model\Currency;
12-
use \Magento\Quote\Model\Quote\Address;
12+
use Magento\Quote\Model\Quote\Address;
1313
use Magento\Quote\Model\Quote\Address\Rate;
1414
use Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory as RateCollectionFactory;
1515
use Magento\Quote\Model\ResourceModel\Quote\Address\Rate\Collection as RatesCollection;
@@ -28,6 +28,7 @@
2828
use Magento\Store\Api\Data\StoreInterface;
2929
use Magento\Store\Api\Data\WebsiteInterface;
3030
use Magento\Quote\Model\Quote\Address\RateResult\AbstractResult;
31+
use Magento\Framework\Serialize\Serializer\Json;
3132

3233
/**
3334
* Test class for sales quote address model
@@ -117,7 +118,7 @@ protected function setUp()
117118
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
118119

119120
$this->scopeConfig = $this->createMock(\Magento\Framework\App\Config::class);
120-
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
121+
$this->serializer = new Json();
121122

122123
$this->requestFactory = $this->getMockBuilder(RateRequestFactory::class)
123124
->disableOriginalConstructor()
@@ -273,20 +274,17 @@ public function testValidateMiniumumAmountNegative()
273274
public function testSetAndGetAppliedTaxes()
274275
{
275276
$data = ['data'];
276-
$result = json_encode($data);
277-
278-
$this->serializer->expects($this->once())
279-
->method('serialize')
280-
->with($data)
281-
->willReturn($result);
282-
283-
$this->serializer->expects($this->once())
284-
->method('unserialize')
285-
->with($result)
286-
->willReturn($data);
277+
self::assertInstanceOf(Address::class, $this->address->setAppliedTaxes($data));
278+
self::assertEquals($data, $this->address->getAppliedTaxes());
279+
}
287280

288-
$this->assertInstanceOf(\Magento\Quote\Model\Quote\Address::class, $this->address->setAppliedTaxes($data));
289-
$this->assertEquals($data, $this->address->getAppliedTaxes());
281+
/**
282+
* Checks a case, when applied taxes are not provided.
283+
*/
284+
public function testGetAppliedTaxesWithEmptyValue()
285+
{
286+
$this->address->setData('applied_taxes', null);
287+
self::assertEquals([], $this->address->getAppliedTaxes());
290288
}
291289

292290
/**

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use Magento\Customer\Api\AddressMetadataInterface;
1212
use Magento\Customer\Model\Metadata\Form as CustomerForm;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Quote\Model\Quote\Address;
1315
use Magento\Quote\Model\Quote\Item;
1416

1517
/**
@@ -323,7 +325,7 @@ public function __construct(
323325
$this->dataObjectHelper = $dataObjectHelper;
324326
$this->orderManagement = $orderManagement;
325327
$this->quoteFactory = $quoteFactory;
326-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
328+
$this->serializer = $serializer ?: ObjectManager::getInstance()
327329
->get(\Magento\Framework\Serialize\Serializer\Json::class);
328330
parent::__construct($data);
329331
}
@@ -1449,32 +1451,36 @@ public function getBillingAddress()
14491451
*/
14501452
public function setBillingAddress($address)
14511453
{
1452-
if (is_array($address)) {
1453-
$billingAddress = $this->_objectManager->create(
1454-
\Magento\Quote\Model\Quote\Address::class
1455-
)->setData(
1456-
$address
1457-
)->setAddressType(
1458-
\Magento\Quote\Model\Quote\Address::TYPE_BILLING
1459-
);
1460-
$this->_setQuoteAddress($billingAddress, $address);
1461-
/**
1462-
* save_in_address_book is not a valid attribute and is filtered out by _setQuoteAddress,
1463-
* that is why it should be added after _setQuoteAddress call
1464-
*/
1465-
$saveInAddressBook = (int)(!empty($address['save_in_address_book']));
1466-
$billingAddress->setData('save_in_address_book', $saveInAddressBook);
1467-
1468-
if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) {
1469-
$shippingAddress = clone $billingAddress;
1470-
$shippingAddress->setSameAsBilling(true);
1471-
$shippingAddress->setSaveInAddressBook(false);
1472-
$address['save_in_address_book'] = 0;
1473-
$this->setShippingAddress($address);
1474-
}
1454+
if (!is_array($address)) {
1455+
return $this;
1456+
}
1457+
1458+
$billingAddress = $this->_objectManager->create(Address::class)
1459+
->setData($address)
1460+
->setAddressType(Address::TYPE_BILLING);
1461+
1462+
$this->_setQuoteAddress($billingAddress, $address);
1463+
1464+
/**
1465+
* save_in_address_book is not a valid attribute and is filtered out by _setQuoteAddress,
1466+
* that is why it should be added after _setQuoteAddress call
1467+
*/
1468+
$saveInAddressBook = (int)(!empty($address['save_in_address_book']));
1469+
$billingAddress->setData('save_in_address_book', $saveInAddressBook);
1470+
1471+
$quote = $this->getQuote();
1472+
if (!$quote->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) {
1473+
$address['save_in_address_book'] = 0;
1474+
$this->setShippingAddress($address);
1475+
}
14751476

1476-
$this->getQuote()->setBillingAddress($billingAddress);
1477+
// not assigned billing address should be saved as new
1478+
// but if quote already has the billing address it won't be overridden
1479+
if (empty($billingAddress->getCustomerAddressId())) {
1480+
$billingAddress->setCustomerAddressId(null);
1481+
$quote->getBillingAddress()->setCustomerAddressId(null);
14771482
}
1483+
$quote->setBillingAddress($billingAddress);
14781484

14791485
return $this;
14801486
}
@@ -1775,6 +1781,7 @@ public function _prepareCustomer()
17751781
$address = $this->getShippingAddress()->setCustomerId($this->getQuote()->getCustomer()->getId());
17761782
$this->setShippingAddress($address);
17771783
}
1784+
$this->getBillingAddress()->setCustomerId($customer->getId());
17781785
$this->getQuote()->updateCustomerData($this->getQuote()->getCustomer());
17791786

17801787
$customer = $this->getQuote()->getCustomer();

app/code/Magento/Sales/view/adminhtml/web/order/create/scripts.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ define([
157157
}
158158
if(this.addresses[id]){
159159
this.fillAddressFields(container, this.addresses[id]);
160+
160161
}
161162
else{
162163
this.fillAddressFields(container, {});
@@ -190,43 +191,53 @@ define([
190191
}
191192
},
192193

193-
changeAddressField : function(event){
194-
var field = Event.element(event);
195-
var re = /[^\[]*\[([^\]]*)_address\]\[([^\]]*)\](\[(\d)\])?/;
196-
var matchRes = field.name.match(re);
194+
/**
195+
* Triggers on each form's element changes.
196+
*
197+
* @param {Object} event
198+
*/
199+
changeAddressField: function (event) {
200+
var field = Event.element(event),
201+
re = /[^\[]*\[([^\]]*)_address\]\[([^\]]*)\](\[(\d)\])?/,
202+
matchRes = field.name.match(re),
203+
type,
204+
name,
205+
data;
197206

198207
if (!matchRes) {
199208
return;
200209
}
201210

202-
var type = matchRes[1];
203-
var name = matchRes[2];
204-
var data;
211+
type = matchRes[1];
212+
name = matchRes[2];
205213

206-
if(this.isBillingField(field.id)){
207-
data = this.serializeData(this.billingAddressContainer)
208-
}
209-
else{
210-
data = this.serializeData(this.shippingAddressContainer)
214+
if (this.isBillingField(field.id)) {
215+
data = this.serializeData(this.billingAddressContainer);
216+
} else {
217+
data = this.serializeData(this.shippingAddressContainer);
211218
}
212219
data = data.toObject();
213220

214-
if( (type == 'billing' && this.shippingAsBilling)
215-
|| (type == 'shipping' && !this.shippingAsBilling) ) {
221+
if (type === 'billing' && this.shippingAsBilling || type === 'shipping' && !this.shippingAsBilling) {
216222
data['reset_shipping'] = true;
217223
}
218224

219-
data['order['+type+'_address][customer_address_id]'] = $('order-'+type+'_address_customer_address_id').value;
225+
data['order[' + type + '_address][customer_address_id]'] = null;
226+
data['shipping_as_billing'] = jQuery('[name="shipping_same_as_billing"]').is(':checked') ? 1 : 0;
227+
228+
if (name === 'customer_address_id') {
229+
data['order[' + type + '_address][customer_address_id]'] =
230+
$('order-' + type + '_address_customer_address_id').value;
231+
}
220232

221233
if (data['reset_shipping']) {
222234
this.resetShippingMethod(data);
223235
} else {
224236
this.saveData(data);
225-
if (name == 'country_id' || name == 'customer_address_id') {
237+
238+
if (name === 'country_id' || name === 'customer_address_id') {
226239
this.loadArea(['shipping_method', 'billing_method', 'totals', 'items'], true, data);
227240
}
228-
// added for reloading of default sender and default recipient for giftmessages
229-
//this.loadArea(['giftmessage'], true, data);
230241
}
231242
},
232243

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/CreateTest.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Sales\Controller\Adminhtml\Order;
77

8+
use Magento\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Backend\Model\Session\Quote;
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
812
/**
913
* @magentoAppArea adminhtml
1014
* @magentoDbIsolation enabled
@@ -158,7 +162,7 @@ public function testIndexAction()
158162
*/
159163
public function testGetAclResource($actionName, $reordered, $expectedResult)
160164
{
161-
$this->_objectManager->get(\Magento\Backend\Model\Session\Quote::class)->setReordered($reordered);
165+
$this->_objectManager->get(Quote::class)->setReordered($reordered);
162166
$orderController = $this->_objectManager->get(
163167
\Magento\Sales\Controller\Adminhtml\Order\Stub\OrderCreateStub::class
164168
);
@@ -229,4 +233,57 @@ public function testDeniedSaveAction()
229233
$this->dispatch('backend/sales/order_create/save');
230234
$this->assertEquals('403', $this->getResponse()->getHttpResponseCode());
231235
}
236+
237+
/**
238+
* Checks a case when shipping is the same as billing and billing address details was changed by request.
239+
* Both billing and shipping addresses should be updated.
240+
*
241+
* @magentoAppArea adminhtml
242+
* @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
243+
*/
244+
public function testSyncBetweenQuoteAddresses()
245+
{
246+
/** @var CustomerRepositoryInterface $customerRepository */
247+
$customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
248+
$customer = $customerRepository->get('[email protected]');
249+
250+
/** @var CartRepositoryInterface $quoteRepository */
251+
$quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
252+
$quote = $quoteRepository->getActiveForCustomer($customer->getId());
253+
254+
$session = $this->_objectManager->get(Quote::class);
255+
$session->setQuoteId($quote->getId());
256+
257+
$data = [
258+
'firstname' => 'John',
259+
'lastname' => 'Doe',
260+
'street' => ['Soborna 23'],
261+
'city' => 'Kyiv',
262+
'country_id' => 'UA',
263+
'region' => 'Kyivska',
264+
'region_id' => 1
265+
];
266+
$this->getRequest()->setPostValue(
267+
[
268+
'order' => ['billing_address' => $data],
269+
'reset_shipping' => 1,
270+
'customer_id' => $customer->getId(),
271+
'store_id' => 1,
272+
'json' => true
273+
]
274+
);
275+
276+
$this->dispatch('backend/sales/order_create/loadBlock/block/shipping_address');
277+
self::assertEquals(200, $this->getResponse()->getHttpResponseCode());
278+
279+
$updatedQuote = $quoteRepository->get($quote->getId());
280+
281+
$billingAddress = $updatedQuote->getBillingAddress();
282+
self::assertEquals($data['region_id'], $billingAddress->getRegionId());
283+
self::assertEquals($data['country_id'], $billingAddress->getCountryId());
284+
285+
$shippingAddress = $updatedQuote->getShippingAddress();
286+
self::assertEquals($data['city'], $shippingAddress->getCity());
287+
self::assertEquals($data['street'], $shippingAddress->getStreet());
288+
}
232289
}

0 commit comments

Comments
 (0)