Skip to content

Commit 906dd7a

Browse files
committed
MAGETWO-53060: Cannot refund order partially
1 parent 2a722c7 commit 906dd7a

File tree

1 file changed

+16
-22
lines changed
  • app/code/Magento/Sales/Model/Order/Creditmemo

1 file changed

+16
-22
lines changed

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ class Item extends AbstractModel implements CreditmemoItemInterface
4242
*/
4343
protected $_orderItemFactory;
4444

45-
/**
46-
* @var bool
47-
*/
48-
private $qtyProcessed = false;
49-
5045
/**
5146
* @param \Magento\Framework\Model\Context $context
5247
* @param \Magento\Framework\Registry $registry
@@ -163,7 +158,6 @@ private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem
163158
public function setQty($qty)
164159
{
165160
$this->setData('qty', $qty);
166-
$this->qtyProcessed = false;
167161
return $this;
168162
}
169163

@@ -176,7 +170,8 @@ public function register()
176170
{
177171
$orderItem = $this->getOrderItem();
178172

179-
$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $this->getQty());
173+
$qty = $this->processQty();
174+
$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $qty);
180175
$orderItem->setTaxRefunded($orderItem->getTaxRefunded() + $this->getTaxAmount());
181176
$orderItem->setBaseTaxRefunded($orderItem->getBaseTaxRefunded() + $this->getBaseTaxAmount());
182177
$orderItem->setDiscountTaxCompensationRefunded(
@@ -194,7 +189,7 @@ public function register()
194189
}
195190

196191
/**
197-
* @return void
192+
* @return int|float
198193
* @throws \Magento\Framework\Exception\LocalizedException
199194
*/
200195
private function processQty()
@@ -208,7 +203,7 @@ private function processQty()
208203
}
209204
$qty = $qty > 0 ? $qty : 0;
210205
if ($this->isQtyAvailable($qty, $orderItem)) {
211-
$this->setData('qty', $qty);
206+
return $qty;
212207
} else {
213208
throw new \Magento\Framework\Exception\LocalizedException(
214209
__('We found an invalid quantity to refund item "%1".', $this->getName())
@@ -221,17 +216,18 @@ private function processQty()
221216
*/
222217
public function cancel()
223218
{
224-
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $this->getQty());
219+
$qty = $this->processQty();
220+
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $qty);
225221
$this->getOrderItem()->setTaxRefunded(
226222
$this->getOrderItem()->getTaxRefunded() -
227223
$this->getOrderItem()->getBaseTaxAmount() *
228-
$this->getQty() /
224+
$qty /
229225
$this->getOrderItem()->getQtyOrdered()
230226
);
231227
$this->getOrderItem()->setDiscountTaxCompensationRefunded(
232228
$this->getOrderItem()->getDiscountTaxCompensationRefunded() -
233229
$this->getOrderItem()->getDiscountTaxCompensationAmount() *
234-
$this->getQty() /
230+
$qty /
235231
$this->getOrderItem()->getQtyOrdered()
236232
);
237233
return $this;
@@ -253,21 +249,22 @@ public function calcRowTotal()
253249
$rowTotalInclTax = $orderItem->getRowTotalInclTax();
254250
$baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
255251

256-
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() >= 0) {
252+
$qty = $this->processQty();
253+
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $qty >= 0) {
257254
$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
258-
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());
259-
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
255+
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $qty);
256+
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $qty, 'base');
260257
}
261258
$this->setRowTotal($rowTotal);
262259
$this->setBaseRowTotal($baseRowTotal);
263260

264261
if ($rowTotalInclTax && $baseRowTotalInclTax) {
265262
$orderItemQty = $orderItem->getQtyOrdered();
266263
$this->setRowTotalInclTax(
267-
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
264+
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $qty, 'including')
268265
);
269266
$this->setBaseRowTotalInclTax(
270-
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
267+
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $qty, 'including_base')
271268
);
272269
}
273270
return $this;
@@ -281,7 +278,8 @@ public function calcRowTotal()
281278
public function isLast()
282279
{
283280
$orderItem = $this->getOrderItem();
284-
if ((string)(double)$this->getQty() == (string)(double)$orderItem->getQtyToRefund()) {
281+
$qty = $this->processQty();
282+
if ((string)(double)$qty == (string)(double)$orderItem->getQtyToRefund()) {
285283
return true;
286284
}
287285
return false;
@@ -514,10 +512,6 @@ public function getProductId()
514512
*/
515513
public function getQty()
516514
{
517-
if (!$this->qtyProcessed) {
518-
$this->processQty();
519-
$this->qtyProcessed = true;
520-
}
521515
return $this->getData(CreditmemoItemInterface::QTY);
522516
}
523517

0 commit comments

Comments
 (0)