Skip to content

Commit b63de1d

Browse files
author
Yu Tang
committed
Merge remote-tracking branch 'origin/FearlessKiwis-MAGETWO-31458-fpt-display-on-partial-invoice' into develop
2 parents 2f51a48 + a8bbbf2 commit b63de1d

File tree

6 files changed

+219
-42
lines changed

6 files changed

+219
-42
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function calcRowTotal()
219219
$rowTotalInclTax = $orderItem->getRowTotalInclTax();
220220
$baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
221221

222-
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() > 0) {
222+
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() >= 0) {
223223
$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
224224
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());
225225
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,43 +264,53 @@ public function testCancel()
264264
$this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result);
265265
}
266266

267-
public function testCalcRowTotal()
267+
/**
268+
* @dataProvider calcRowTotalDataProvider
269+
*/
270+
public function testCalcRowTotal($qty)
268271
{
269272
$creditmemoMock = $this->getMockBuilder('\Magento\Sales\Model\Order\Creditmemo')
270273
->disableOriginalConstructor()
271274
->getMock();
272275
$creditmemoMock->expects($this->exactly(4))
273276
->method('roundPrice')
274-
->willReturnMap(
275-
[
276-
[0.375, 'regular', false, 0.4],
277-
[0.375, 'base', false, 0.4],
278-
[1, 'including', false, 1.0],
279-
[1, 'including_base', false, 1.0]
280-
]
281-
);
277+
->will($this->returnCallback(
278+
function ($arg) {
279+
return round($arg, 2);
280+
}
281+
));
282+
283+
$qtyInvoiced = 10;
284+
$qtyRefunded = 2;
285+
$qtyAvailable = $qtyInvoiced - $qtyRefunded;
286+
287+
$rowInvoiced = 5;
288+
$amountRefunded = 2;
289+
290+
$expectedRowTotal = ($rowInvoiced - $amountRefunded) / $qtyAvailable * $qty;
291+
$expectedRowTotal = round($expectedRowTotal, 2);
282292

283293
$orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item')
284294
->disableOriginalConstructor()
285295
->getMock();
286296
$orderItemMock->expects($this->once())
287297
->method('getQtyInvoiced')
288-
->willReturn(10);
298+
->willReturn($qtyInvoiced);
289299
$orderItemMock->expects($this->once())
290300
->method('getQtyRefunded')
291-
->willReturn(2);
301+
->willReturn($qtyRefunded);
292302
$orderItemMock->expects($this->once())
293303
->method('getRowInvoiced')
294-
->willReturn(5);
304+
->willReturn($rowInvoiced);
295305
$orderItemMock->expects($this->once())
296306
->method('getAmountRefunded')
297-
->willReturn(2);
307+
->willReturn($amountRefunded);
298308
$orderItemMock->expects($this->once())
299309
->method('getBaseRowInvoiced')
300-
->willReturn(5);
310+
->willReturn($rowInvoiced);
301311
$orderItemMock->expects($this->once())
302312
->method('getBaseAmountRefunded')
303-
->willReturn(2);
313+
->willReturn($amountRefunded);
304314
$orderItemMock->expects($this->once())
305315
->method('getRowTotalInclTax')
306316
->willReturn(1);
@@ -313,11 +323,28 @@ public function testCalcRowTotal()
313323
$orderItemMock->expects($this->once())
314324
->method('getQtyOrdered')
315325
->willReturn(1);
326+
$orderItemMock->expects($this->any())
327+
->method('getQtyToRefund')
328+
->willReturn($qtyAvailable);
316329

317-
$this->item->setData('qty', 1);
330+
$this->item->setData('qty', $qty);
318331
$this->item->setCreditmemo($creditmemoMock);
319332
$this->item->setOrderItem($orderItemMock);
320333
$result = $this->item->calcRowTotal();
334+
321335
$this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result);
336+
$this->assertEquals($expectedRowTotal, $this->item->getData('row_total'));
337+
$this->assertEquals($expectedRowTotal, $this->item->getData('base_row_total'));
338+
}
339+
340+
/**
341+
* @return array
342+
*/
343+
public function calcRowTotalDataProvider()
344+
{
345+
return [
346+
'qty 1' => [1],
347+
'qty 0' => [0],
348+
];
322349
}
323350
}

app/code/Magento/Weee/Model/Total/Creditmemo/Weee.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,30 @@ public function collect(Creditmemo $creditmemo)
4949

5050
$totalWeeeAmount = 0;
5151
$baseTotalWeeeAmount = 0;
52-
5352
$totalWeeeAmountInclTax = 0;
5453
$baseTotalWeeeAmountInclTax = 0;
55-
56-
$totalTaxAmount = $totalWeeeAmountInclTax - $totalWeeeAmount;
57-
$baseTotalTaxAmount = $baseTotalWeeeAmountInclTax - $baseTotalWeeeAmount;
54+
$totalTaxAmount = 0;
55+
$baseTotalTaxAmount = 0;
5856

5957
foreach ($creditmemo->getAllItems() as $item) {
6058
$orderItem = $item->getOrderItem();
61-
if ($orderItem->isDummy() || $item->getQty() <= 0) {
59+
$orderItemQty = $orderItem->getQtyOrdered();
60+
61+
if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
6262
continue;
6363
}
6464

65-
$ratio = $item->getQty() / $orderItem->getQtyOrdered();
65+
$ratio = $item->getQty() / $orderItemQty;
6666

6767
$orderItemWeeeAmountExclTax = $orderItem->getWeeeTaxAppliedRowAmount();
6868
$orderItemBaseWeeeAmountExclTax = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
6969
$weeeAmountExclTax = $creditmemo->roundPrice($orderItemWeeeAmountExclTax * $ratio);
70-
$baseWeeeAmountExclTax = $creditmemo->roundPrice(
71-
$orderItemBaseWeeeAmountExclTax * $ratio,
72-
'base'
73-
);
70+
$baseWeeeAmountExclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountExclTax * $ratio, 'base');
7471

7572
$orderItemWeeeAmountInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
7673
$orderItemBaseWeeeAmountInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
7774
$weeeAmountInclTax = $creditmemo->roundPrice($orderItemWeeeAmountInclTax * $ratio);
78-
$baseWeeeAmountInclTax = $creditmemo->roundPrice(
79-
$orderItemBaseWeeeAmountInclTax * $ratio,
80-
'base'
81-
);
75+
$baseWeeeAmountInclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountInclTax * $ratio, 'base');
8276

8377
$itemTaxAmount = $weeeAmountInclTax - $weeeAmountExclTax;
8478
$itemBaseTaxAmount = $baseWeeeAmountInclTax - $baseWeeeAmountExclTax;

app/code/Magento/Weee/Model/Total/Invoice/Weee.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
5757
$orderItem = $item->getOrderItem();
5858
$orderItemQty = $orderItem->getQtyOrdered();
5959

60-
if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() <= 0) {
60+
if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
6161
continue;
6262
}
6363

6464
$ratio = $item->getQty() / $orderItemQty;
65+
6566
$orderItemWeeeAmount = $orderItem->getWeeeTaxAppliedRowAmount();
6667
$orderItemBaseWeeeAmount = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
6768
$weeeAmount = $invoice->roundPrice($orderItemWeeeAmount * $ratio);

app/code/Magento/Weee/Test/Unit/Model/Total/Creditmemo/WeeeTest.php

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ function ($price, $type) use (&$roundingDelta) {
169169
public function collectDataProvider()
170170
{
171171
$result = [];
172+
172173
// scenario 1: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, full creditmemo
173174
$result['complete_creditmemo'] = [
174175
'creditmemo_data' => [
@@ -236,7 +237,6 @@ public function collectDataProvider()
236237
'tax_ratio' => serialize(['weee' => 1.0]),
237238
'weee_tax_applied_row_amount' => 30,
238239
'base_weee_tax_applied_row_amount' => 30,
239-
240240
],
241241
],
242242
'creditmemo_data' => [
@@ -248,7 +248,6 @@ public function collectDataProvider()
248248
'base_subtotal' => 300,
249249
'subtotal_incl_tax' => 357.22,
250250
'base_subtotal_incl_tax' => 357.22,
251-
252251
],
253252
],
254253
];
@@ -320,7 +319,6 @@ public function collectDataProvider()
320319
'tax_ratio' => serialize(['weee' => 1.65 / 2.47]),
321320
'weee_tax_applied_row_amount' => 20,
322321
'base_weee_tax_applied_row_amount' => 20,
323-
324322
],
325323
],
326324
'creditmemo_data' => [
@@ -332,7 +330,6 @@ public function collectDataProvider()
332330
'base_subtotal' => 200,
333331
'subtotal_incl_tax' => 238.15,
334332
'base_subtotal_incl_tax' => 238.15,
335-
336333
],
337334
],
338335
];
@@ -404,7 +401,6 @@ public function collectDataProvider()
404401
'tax_ratio' => serialize(['weee' => 0.83 / 2.47]),
405402
'weee_tax_applied_row_amount' => 10,
406403
'base_weee_tax_applied_row_amount' => 10,
407-
408404
],
409405
],
410406
'creditmemo_data' => [
@@ -416,7 +412,79 @@ public function collectDataProvider()
416412
'base_subtotal' => 100,
417413
'subtotal_incl_tax' => 119.07,
418414
'base_subtotal_incl_tax' => 119.07,
415+
],
416+
],
417+
];
419418

419+
// scenario 4: 3 item_1, $100 with $weee, 8.25 tax rate. Returning qty 0.
420+
$result['zero_return'] = [
421+
'creditmemo_data' => [
422+
'items' => [
423+
'item_1' => [
424+
'order_item' => [
425+
'qty_ordered' => 3,
426+
'weee_tax_applied_row_amount' => 30,
427+
'base_weee_tax_applied_row_amnt' => 30,
428+
'row_weee_tax_incl_tax' => 32.47,
429+
'base_row_weee_tax_incl_tax' => 32.47,
430+
'weee_amount_invoiced' => 30,
431+
'base_weee_amount_invoiced' => 30,
432+
'weee_amount_refunded' => 0,
433+
'base_weee_amount_refunded' => 0,
434+
'weee_tax_amount_invoiced' => 2.47,
435+
'base_weee_tax_amount_invoiced' => 2.47,
436+
'weee_tax_amount_refunded' => 0,
437+
'base_weee_tax_amount_refunded' => 0,
438+
'applied_weee' => [
439+
[
440+
'title' => 'recycling_fee',
441+
'base_row_amount' => 30,
442+
'row_amount' => 30,
443+
'base_row_amount_incl_tax' => 32.47,
444+
'row_amount_incl_tax' => 32.47,
445+
],
446+
],
447+
'qty_invoiced' => 3,
448+
],
449+
'is_last' => true,
450+
'data_fields' => [
451+
'qty' => 0,
452+
'applied_weee' => [
453+
[
454+
],
455+
],
456+
],
457+
],
458+
],
459+
'include_in_subtotal' => false,
460+
'data_fields' => [
461+
'grand_total' => 300,
462+
'base_grand_total' => 300,
463+
'subtotal' => 300,
464+
'base_subtotal' => 300,
465+
'subtotal_incl_tax' => 324.75,
466+
'base_subtotal_incl_tax' => 324.75,
467+
'tax_amount' => 0,
468+
'base_tax_amount' => 0,
469+
],
470+
],
471+
'expected_results' => [
472+
'creditmemo_items' => [
473+
'item_1' => [
474+
'applied_weee' => [
475+
[
476+
'title' => 'recycling_fee',
477+
'base_row_amount' => 0,
478+
'row_amount' => 0,
479+
'base_row_amount_incl_tax' => 0,
480+
'row_amount_incl_tax' => 0,
481+
],
482+
],
483+
],
484+
],
485+
'creditmemo_data' => [
486+
'subtotal' => 300,
487+
'base_subtotal' => 300,
420488
],
421489
],
422490
];

0 commit comments

Comments
 (0)