Skip to content

Commit f879552

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-53060' into pr
2 parents 401f0a7 + 02da096 commit f879552

File tree

2 files changed

+82
-149
lines changed

2 files changed

+82
-149
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Item.php

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,16 @@ public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
127127
public function getOrderItem()
128128
{
129129
if ($this->_orderItem === null) {
130-
$this->_orderItem = $this->getOrderItemWithoutCaching();
130+
if ($this->getCreditmemo()) {
131+
$orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
132+
} else {
133+
$orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
134+
}
135+
$this->_orderItem = $orderItem;
131136
}
132137
return $this->_orderItem;
133138
}
134139

135-
/**
136-
* Retrieve order item instance without set it to property.
137-
* It is need for ability to process setQty on api when credit memo and order has not built yet.
138-
*
139-
* @return \Magento\Sales\Model\Order\Item
140-
*/
141-
private function getOrderItemWithoutCaching()
142-
{
143-
if ($this->getCreditmemo()) {
144-
$orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
145-
} else {
146-
$orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
147-
}
148-
149-
return $orderItem;
150-
}
151-
152140
/**
153141
* Checks if quantity available for refund
154142
*
@@ -164,26 +152,12 @@ private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem
164152
/**
165153
* Declare qty
166154
*
167-
* @param float $qty
155+
* @param float $qty
168156
* @return $this
169-
* @throws \Magento\Framework\Exception\LocalizedException
170157
*/
171158
public function setQty($qty)
172159
{
173-
$orderItem = $this->getOrderItemWithoutCaching();
174-
if ($orderItem->getIsQtyDecimal()) {
175-
$qty = (double)$qty;
176-
} else {
177-
$qty = (int)$qty;
178-
}
179-
$qty = $qty > 0 ? $qty : 0;
180-
if ($this->isQtyAvailable($qty, $orderItem)) {
181-
$this->setData('qty', $qty);
182-
} else {
183-
throw new \Magento\Framework\Exception\LocalizedException(
184-
__('We found an invalid quantity to refund item "%1".', $this->getName())
185-
);
186-
}
160+
$this->setData(CreditmemoItemInterface::QTY, $qty);
187161
return $this;
188162
}
189163

@@ -196,7 +170,8 @@ public function register()
196170
{
197171
$orderItem = $this->getOrderItem();
198172

199-
$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $this->getQty());
173+
$qty = $this->processQty();
174+
$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $qty);
200175
$orderItem->setTaxRefunded($orderItem->getTaxRefunded() + $this->getTaxAmount());
201176
$orderItem->setBaseTaxRefunded($orderItem->getBaseTaxRefunded() + $this->getBaseTaxAmount());
202177
$orderItem->setDiscountTaxCompensationRefunded(
@@ -213,22 +188,46 @@ public function register()
213188
return $this;
214189
}
215190

191+
/**
192+
* @return int|float
193+
* @throws \Magento\Framework\Exception\LocalizedException
194+
*/
195+
private function processQty()
196+
{
197+
$orderItem = $this->getOrderItem();
198+
$qty = $this->getQty();
199+
if ($orderItem->getIsQtyDecimal()) {
200+
$qty = (double)$qty;
201+
} else {
202+
$qty = (int)$qty;
203+
}
204+
$qty = $qty > 0 ? $qty : 0;
205+
if ($this->isQtyAvailable($qty, $orderItem)) {
206+
return $qty;
207+
} else {
208+
throw new \Magento\Framework\Exception\LocalizedException(
209+
__('We found an invalid quantity to refund item "%1".', $this->getName())
210+
);
211+
}
212+
}
213+
216214
/**
217215
* @return $this
218216
*/
219217
public function cancel()
220218
{
221-
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $this->getQty());
219+
$qty = $this->processQty();
220+
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $qty);
222221
$this->getOrderItem()->setTaxRefunded(
223222
$this->getOrderItem()->getTaxRefunded() -
224223
$this->getOrderItem()->getBaseTaxAmount() *
225-
$this->getQty() /
224+
$qty /
226225
$this->getOrderItem()->getQtyOrdered()
227226
);
228227
$this->getOrderItem()->setDiscountTaxCompensationRefunded(
229228
$this->getOrderItem()->getDiscountTaxCompensationRefunded() -
230229
$this->getOrderItem()->getDiscountTaxCompensationAmount() *
231-
$this->getQty() /
230+
$qty /
232231
$this->getOrderItem()->getQtyOrdered()
233232
);
234233
return $this;
@@ -250,21 +249,22 @@ public function calcRowTotal()
250249
$rowTotalInclTax = $orderItem->getRowTotalInclTax();
251250
$baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
252251

253-
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() >= 0) {
252+
$qty = $this->processQty();
253+
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $qty >= 0) {
254254
$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
255-
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());
256-
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
255+
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $qty);
256+
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $qty, 'base');
257257
}
258258
$this->setRowTotal($rowTotal);
259259
$this->setBaseRowTotal($baseRowTotal);
260260

261261
if ($rowTotalInclTax && $baseRowTotalInclTax) {
262262
$orderItemQty = $orderItem->getQtyOrdered();
263263
$this->setRowTotalInclTax(
264-
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
264+
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $qty, 'including')
265265
);
266266
$this->setBaseRowTotalInclTax(
267-
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
267+
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $qty, 'including_base')
268268
);
269269
}
270270
return $this;
@@ -278,7 +278,8 @@ public function calcRowTotal()
278278
public function isLast()
279279
{
280280
$orderItem = $this->getOrderItem();
281-
if ((string)(double)$this->getQty() == (string)(double)$orderItem->getQtyToRefund()) {
281+
$qty = $this->processQty();
282+
if ((string)(double)$qty == (string)(double)$orderItem->getQtyToRefund()) {
282283
return true;
283284
}
284285
return false;

app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/ItemTest.php

Lines changed: 37 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -92,87 +92,9 @@ public function testGetOrderItemFromFactory()
9292
$this->assertInstanceOf('Magento\Sales\Model\Order\Item', $result);
9393
}
9494

95-
/**
96-
* @expectedException \Magento\Framework\Exception\LocalizedException
97-
* @expectedExceptionMessage We found an invalid quantity to refund item "test_item_name".
98-
*/
99-
public function testSetQtyDecimalException()
100-
{
101-
$qty = 100;
102-
$orderItemQty = 10;
103-
$name = 'test_item_name';
104-
105-
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
106-
->disableOriginalConstructor()
107-
->getMock();
108-
$orderItemMock->expects($this->once())
109-
->method('getIsQtyDecimal')
110-
->willReturn(true);
111-
$orderItemMock->expects($this->once())
112-
->method('getQtyToRefund')
113-
->willReturn($orderItemQty);
114-
$orderItemMock->expects($this->atLeastOnce())
115-
->method('load')
116-
->willReturnSelf();
117-
$this->orderItemFactoryMock->expects($this->atLeastOnce())
118-
->method('create')
119-
->willReturn($orderItemMock);
120-
$this->item->setData(CreditmemoItemInterface::NAME, $name);
121-
$this->item->setOrderItem($orderItemMock);
122-
$this->item->setQty($qty);
123-
}
124-
125-
/**
126-
* @expectedException \Magento\Framework\Exception\LocalizedException
127-
* @expectedExceptionMessage We found an invalid quantity to refund item "test_item_name2".
128-
*/
129-
public function testSetQtyNumericException()
130-
{
131-
$qty = 100;
132-
$orderItemQty = 10;
133-
$name = 'test_item_name2';
134-
135-
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
136-
->disableOriginalConstructor()
137-
->getMock();
138-
$orderItemMock->expects($this->once())
139-
->method('getIsQtyDecimal')
140-
->willReturn(false);
141-
$orderItemMock->expects($this->once())
142-
->method('getQtyToRefund')
143-
->willReturn($orderItemQty);
144-
$orderItemMock->expects($this->atLeastOnce())
145-
->method('load')
146-
->willReturnSelf();
147-
$this->orderItemFactoryMock->expects($this->atLeastOnce())
148-
->method('create')
149-
->willReturn($orderItemMock);
150-
$this->item->setData(CreditmemoItemInterface::NAME, $name);
151-
$this->item->setOrderItem($orderItemMock);
152-
$this->item->setQty($qty);
153-
}
154-
15595
public function testSetQty()
15696
{
15797
$qty = 10;
158-
$orderItemQty = 100;
159-
160-
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
161-
->disableOriginalConstructor()
162-
->getMock();
163-
$orderItemMock->expects($this->once())
164-
->method('getIsQtyDecimal')
165-
->willReturn(false);
166-
$orderItemMock->expects($this->once())
167-
->method('getQtyToRefund')
168-
->willReturn($orderItemQty);
169-
$orderItemMock->expects($this->atLeastOnce())
170-
->method('load')
171-
->willReturnSelf();
172-
$this->orderItemFactoryMock->expects($this->atLeastOnce())
173-
->method('create')
174-
->willReturn($orderItemMock);
175-
$this->item->setOrderItem($orderItemMock);
17698
$this->item->setQty($qty);
17799
$this->assertEquals($qty, $this->item->getQty());
178100
}
@@ -209,19 +131,39 @@ public function testRegister()
209131
$orderItemMock->expects($this->once())
210132
->method('getBaseDiscountRefunded')
211133
->willReturn(1);
212-
$data = [
213-
'qty' => 1,
214-
'tax_amount' => 1,
215-
'base_tax_amount' => 1,
216-
'discount_tax_compensation_amount' => 1,
217-
'base_discount_tax_compensation_amount' => 1,
218-
'row_total' => 1,
219-
'base_row_total' => 1,
220-
'discount_amount' => 1,
221-
'base_discount_amount' => 1
222-
];
134+
$orderItemMock->expects($this->once())
135+
->method('getQtyToRefund')
136+
->willReturn(1);
137+
$this->item->setQty(1);
138+
$this->item->setTaxAmount(1);
139+
$this->item->setBaseTaxAmount(1);
140+
$this->item->setDiscountTaxCompensationAmount(1);
141+
$this->item->setBaseDiscountTaxCompensationAmount(1);
142+
$this->item->setRowTotal(1);
143+
$this->item->setBaseRowTotal(1);
144+
$this->item->setDiscountAmount(1);
145+
$this->item->setBaseDiscountAmount(1);
146+
$this->item->setOrderItem($orderItemMock);
147+
$result = $this->item->register();
148+
$this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result);
149+
}
150+
151+
/**
152+
* @expectedException \Magento\Framework\Exception\LocalizedException
153+
* @expectedExceptionMessage We found an invalid quantity to refund item "test".
154+
*/
155+
public function testRegisterWithException()
156+
{
157+
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
158+
->disableOriginalConstructor()
159+
->setMethods(['getQtyRefunded'])
160+
->getMock();
161+
$orderItemMock->expects($this->once())
162+
->method('getQtyRefunded')
163+
->willReturn(1);
164+
$this->item->setQty(2);
223165
$this->item->setOrderItem($orderItemMock);
224-
$this->item->setData($data);
166+
$this->item->setName('test');
225167
$result = $this->item->register();
226168
$this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result);
227169
}
@@ -230,19 +172,6 @@ public function testCancel()
230172
{
231173
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
232174
->disableOriginalConstructor()
233-
->setMethods(
234-
[
235-
'setQtyRefunded',
236-
'getQtyRefunded',
237-
'getTaxRefunded',
238-
'getBaseTaxAmount',
239-
'getQtyOrdered',
240-
'setTaxRefunded',
241-
'setDiscountTaxCompensationRefunded',
242-
'getDiscountTaxCompensationRefunded',
243-
'getDiscountTaxCompensationAmount'
244-
]
245-
)
246175
->getMock();
247176
$orderItemMock->expects($this->once())
248177
->method('getQtyRefunded')
@@ -272,8 +201,11 @@ public function testCancel()
272201
$orderItemMock->expects($this->once())
273202
->method('getDiscountTaxCompensationAmount')
274203
->willReturn(10);
204+
$orderItemMock->expects($this->once())
205+
->method('getQtyToRefund')
206+
->willReturn(1);
275207

276-
$this->item->setData('qty', 1);
208+
$this->item->setQty(1);
277209
$this->item->setOrderItem($orderItemMock);
278210
$result = $this->item->cancel();
279211
$this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result);
@@ -342,7 +274,7 @@ function ($arg) {
342274
->method('getQtyToRefund')
343275
->willReturn($qtyAvailable);
344276

345-
$this->item->setData('qty', $qty);
277+
$this->item->setQty($qty);
346278
$this->item->setCreditmemo($creditmemoMock);
347279
$this->item->setOrderItem($orderItemMock);
348280
$result = $this->item->calcRowTotal();

0 commit comments

Comments
 (0)