diff --git a/app/code/Magento/Sales/Model/Order/Creditmemo/Item.php b/app/code/Magento/Sales/Model/Order/Creditmemo/Item.php index 163a03903f878e254e5817243cff2ac0bd1288b0..35d522427429f1d01def7ad15a2f784ee2e7f31f 100644 --- a/app/code/Magento/Sales/Model/Order/Creditmemo/Item.php +++ b/app/code/Magento/Sales/Model/Order/Creditmemo/Item.php @@ -219,7 +219,7 @@ class Item extends AbstractModel implements CreditmemoItemInterface $rowTotalInclTax = $orderItem->getRowTotalInclTax(); $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax(); - if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() > 0) { + if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() >= 0) { $availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded(); $rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty()); $baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base'); diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/ItemTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/ItemTest.php index afcf792c718dddc69eab5e23a17923b32a8e8a9c..0673aa120a217ae4daa1d549b441a2969c510d81 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/ItemTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/ItemTest.php @@ -264,43 +264,53 @@ class ItemTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result); } - public function testCalcRowTotal() + /** + * @dataProvider calcRowTotalDataProvider + */ + public function testCalcRowTotal($qty) { $creditmemoMock = $this->getMockBuilder('\Magento\Sales\Model\Order\Creditmemo') ->disableOriginalConstructor() ->getMock(); $creditmemoMock->expects($this->exactly(4)) ->method('roundPrice') - ->willReturnMap( - [ - [0.375, 'regular', false, 0.4], - [0.375, 'base', false, 0.4], - [1, 'including', false, 1.0], - [1, 'including_base', false, 1.0] - ] - ); + ->will($this->returnCallback( + function ($arg) { + return round($arg, 2); + } + )); + + $qtyInvoiced = 10; + $qtyRefunded = 2; + $qtyAvailable = $qtyInvoiced - $qtyRefunded; + + $rowInvoiced = 5; + $amountRefunded = 2; + + $expectedRowTotal = ($rowInvoiced - $amountRefunded) / $qtyAvailable * $qty; + $expectedRowTotal = round($expectedRowTotal, 2); $orderItemMock = $this->getMockBuilder('Magento\Sales\Model\Order\Item') ->disableOriginalConstructor() ->getMock(); $orderItemMock->expects($this->once()) ->method('getQtyInvoiced') - ->willReturn(10); + ->willReturn($qtyInvoiced); $orderItemMock->expects($this->once()) ->method('getQtyRefunded') - ->willReturn(2); + ->willReturn($qtyRefunded); $orderItemMock->expects($this->once()) ->method('getRowInvoiced') - ->willReturn(5); + ->willReturn($rowInvoiced); $orderItemMock->expects($this->once()) ->method('getAmountRefunded') - ->willReturn(2); + ->willReturn($amountRefunded); $orderItemMock->expects($this->once()) ->method('getBaseRowInvoiced') - ->willReturn(5); + ->willReturn($rowInvoiced); $orderItemMock->expects($this->once()) ->method('getBaseAmountRefunded') - ->willReturn(2); + ->willReturn($amountRefunded); $orderItemMock->expects($this->once()) ->method('getRowTotalInclTax') ->willReturn(1); @@ -313,11 +323,28 @@ class ItemTest extends \PHPUnit_Framework_TestCase $orderItemMock->expects($this->once()) ->method('getQtyOrdered') ->willReturn(1); + $orderItemMock->expects($this->any()) + ->method('getQtyToRefund') + ->willReturn($qtyAvailable); - $this->item->setData('qty', 1); + $this->item->setData('qty', $qty); $this->item->setCreditmemo($creditmemoMock); $this->item->setOrderItem($orderItemMock); $result = $this->item->calcRowTotal(); + $this->assertInstanceOf('Magento\Sales\Model\Order\Creditmemo\Item', $result); + $this->assertEquals($expectedRowTotal, $this->item->getData('row_total')); + $this->assertEquals($expectedRowTotal, $this->item->getData('base_row_total')); + } + + /** + * @return array + */ + public function calcRowTotalDataProvider() + { + return [ + 'qty 1' => [1], + 'qty 0' => [0], + ]; } } diff --git a/app/code/Magento/Weee/Model/Total/Creditmemo/Weee.php b/app/code/Magento/Weee/Model/Total/Creditmemo/Weee.php index d45eab656ca2a5eaf6c41e5f17125b87dc6b2f8b..7ddf6d66a7d0ada34c68da378ebcde2ed6112387 100644 --- a/app/code/Magento/Weee/Model/Total/Creditmemo/Weee.php +++ b/app/code/Magento/Weee/Model/Total/Creditmemo/Weee.php @@ -49,36 +49,30 @@ class Weee extends \Magento\Sales\Model\Order\Creditmemo\Total\AbstractTotal $totalWeeeAmount = 0; $baseTotalWeeeAmount = 0; - $totalWeeeAmountInclTax = 0; $baseTotalWeeeAmountInclTax = 0; - - $totalTaxAmount = $totalWeeeAmountInclTax - $totalWeeeAmount; - $baseTotalTaxAmount = $baseTotalWeeeAmountInclTax - $baseTotalWeeeAmount; + $totalTaxAmount = 0; + $baseTotalTaxAmount = 0; foreach ($creditmemo->getAllItems() as $item) { $orderItem = $item->getOrderItem(); - if ($orderItem->isDummy() || $item->getQty() <= 0) { + $orderItemQty = $orderItem->getQtyOrdered(); + + if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) { continue; } - $ratio = $item->getQty() / $orderItem->getQtyOrdered(); + $ratio = $item->getQty() / $orderItemQty; $orderItemWeeeAmountExclTax = $orderItem->getWeeeTaxAppliedRowAmount(); $orderItemBaseWeeeAmountExclTax = $orderItem->getBaseWeeeTaxAppliedRowAmnt(); $weeeAmountExclTax = $creditmemo->roundPrice($orderItemWeeeAmountExclTax * $ratio); - $baseWeeeAmountExclTax = $creditmemo->roundPrice( - $orderItemBaseWeeeAmountExclTax * $ratio, - 'base' - ); + $baseWeeeAmountExclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountExclTax * $ratio, 'base'); $orderItemWeeeAmountInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem); $orderItemBaseWeeeAmountInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem); $weeeAmountInclTax = $creditmemo->roundPrice($orderItemWeeeAmountInclTax * $ratio); - $baseWeeeAmountInclTax = $creditmemo->roundPrice( - $orderItemBaseWeeeAmountInclTax * $ratio, - 'base' - ); + $baseWeeeAmountInclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountInclTax * $ratio, 'base'); $itemTaxAmount = $weeeAmountInclTax - $weeeAmountExclTax; $itemBaseTaxAmount = $baseWeeeAmountInclTax - $baseWeeeAmountExclTax; diff --git a/app/code/Magento/Weee/Model/Total/Invoice/Weee.php b/app/code/Magento/Weee/Model/Total/Invoice/Weee.php index 457a08ccd86691eac2615dcbffcc5571bc8eaf6e..a5bed91ec0c366f9d72e7e6762be8f17249a3a3b 100644 --- a/app/code/Magento/Weee/Model/Total/Invoice/Weee.php +++ b/app/code/Magento/Weee/Model/Total/Invoice/Weee.php @@ -57,11 +57,12 @@ class Weee extends \Magento\Sales\Model\Order\Invoice\Total\AbstractTotal $orderItem = $item->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); - if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() <= 0) { + if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) { continue; } $ratio = $item->getQty() / $orderItemQty; + $orderItemWeeeAmount = $orderItem->getWeeeTaxAppliedRowAmount(); $orderItemBaseWeeeAmount = $orderItem->getBaseWeeeTaxAppliedRowAmnt(); $weeeAmount = $invoice->roundPrice($orderItemWeeeAmount * $ratio); diff --git a/app/code/Magento/Weee/Test/Unit/Model/Total/Creditmemo/WeeeTest.php b/app/code/Magento/Weee/Test/Unit/Model/Total/Creditmemo/WeeeTest.php index 47aa7692a51d940fb25b894b0951aac0af7590c3..0fdd0ad9bdab19c2c51da37e8b7751c807dc9620 100644 --- a/app/code/Magento/Weee/Test/Unit/Model/Total/Creditmemo/WeeeTest.php +++ b/app/code/Magento/Weee/Test/Unit/Model/Total/Creditmemo/WeeeTest.php @@ -169,6 +169,7 @@ class WeeeTest extends \PHPUnit_Framework_TestCase public function collectDataProvider() { $result = []; + // scenario 1: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, full creditmemo $result['complete_creditmemo'] = [ 'creditmemo_data' => [ @@ -236,7 +237,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'tax_ratio' => serialize(['weee' => 1.0]), 'weee_tax_applied_row_amount' => 30, 'base_weee_tax_applied_row_amount' => 30, - ], ], 'creditmemo_data' => [ @@ -248,7 +248,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 300, 'subtotal_incl_tax' => 357.22, 'base_subtotal_incl_tax' => 357.22, - ], ], ]; @@ -320,7 +319,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'tax_ratio' => serialize(['weee' => 1.65 / 2.47]), 'weee_tax_applied_row_amount' => 20, 'base_weee_tax_applied_row_amount' => 20, - ], ], 'creditmemo_data' => [ @@ -332,7 +330,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 200, 'subtotal_incl_tax' => 238.15, 'base_subtotal_incl_tax' => 238.15, - ], ], ]; @@ -404,7 +401,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'tax_ratio' => serialize(['weee' => 0.83 / 2.47]), 'weee_tax_applied_row_amount' => 10, 'base_weee_tax_applied_row_amount' => 10, - ], ], 'creditmemo_data' => [ @@ -416,7 +412,79 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 100, 'subtotal_incl_tax' => 119.07, 'base_subtotal_incl_tax' => 119.07, + ], + ], + ]; + // scenario 4: 3 item_1, $100 with $weee, 8.25 tax rate. Returning qty 0. + $result['zero_return'] = [ + 'creditmemo_data' => [ + 'items' => [ + 'item_1' => [ + 'order_item' => [ + 'qty_ordered' => 3, + 'weee_tax_applied_row_amount' => 30, + 'base_weee_tax_applied_row_amnt' => 30, + 'row_weee_tax_incl_tax' => 32.47, + 'base_row_weee_tax_incl_tax' => 32.47, + 'weee_amount_invoiced' => 30, + 'base_weee_amount_invoiced' => 30, + 'weee_amount_refunded' => 0, + 'base_weee_amount_refunded' => 0, + 'weee_tax_amount_invoiced' => 2.47, + 'base_weee_tax_amount_invoiced' => 2.47, + 'weee_tax_amount_refunded' => 0, + 'base_weee_tax_amount_refunded' => 0, + 'applied_weee' => [ + [ + 'title' => 'recycling_fee', + 'base_row_amount' => 30, + 'row_amount' => 30, + 'base_row_amount_incl_tax' => 32.47, + 'row_amount_incl_tax' => 32.47, + ], + ], + 'qty_invoiced' => 3, + ], + 'is_last' => true, + 'data_fields' => [ + 'qty' => 0, + 'applied_weee' => [ + [ + ], + ], + ], + ], + ], + 'include_in_subtotal' => false, + 'data_fields' => [ + 'grand_total' => 300, + 'base_grand_total' => 300, + 'subtotal' => 300, + 'base_subtotal' => 300, + 'subtotal_incl_tax' => 324.75, + 'base_subtotal_incl_tax' => 324.75, + 'tax_amount' => 0, + 'base_tax_amount' => 0, + ], + ], + 'expected_results' => [ + 'creditmemo_items' => [ + 'item_1' => [ + 'applied_weee' => [ + [ + 'title' => 'recycling_fee', + 'base_row_amount' => 0, + 'row_amount' => 0, + 'base_row_amount_incl_tax' => 0, + 'row_amount_incl_tax' => 0, + ], + ], + ], + ], + 'creditmemo_data' => [ + 'subtotal' => 300, + 'base_subtotal' => 300, ], ], ]; diff --git a/app/code/Magento/Weee/Test/Unit/Model/Total/Invoice/WeeeTest.php b/app/code/Magento/Weee/Test/Unit/Model/Total/Invoice/WeeeTest.php index 4d91954b70fa26e81bf385295b50e5c25cf60934..d034947c48d71129a8ca1bbce59d39b69275d341 100644 --- a/app/code/Magento/Weee/Test/Unit/Model/Total/Invoice/WeeeTest.php +++ b/app/code/Magento/Weee/Test/Unit/Model/Total/Invoice/WeeeTest.php @@ -172,6 +172,7 @@ class WeeeTest extends \PHPUnit_Framework_TestCase public function collectDataProvider() { $result = []; + // 3 item_1, $100 with $weee, 8.25 tax rate, full invoice $result['complete_invoice'] = [ 'order_data' => [ @@ -269,7 +270,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 300, 'subtotal_incl_tax' => 344.85, 'base_subtotal_incl_tax' => 344.85, - ], ], ]; @@ -360,7 +360,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'tax_ratio' => serialize(['weee' => 1.65 / 2.47]), 'weee_tax_applied_row_amount' => 20, 'base_weee_tax_applied_row_amount' => 20, - ], ], 'invoice_data' => [ @@ -372,7 +371,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 200, 'subtotal_incl_tax' => 238.15, 'base_subtotal_incl_tax' => 238.15, - ], ], ]; @@ -464,7 +462,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'tax_ratio' => serialize(['weee' => 0.82 / 2.47]), 'weee_tax_applied_row_amount' => 10, 'base_weee_tax_applied_row_amount' => 10, - ], ], 'invoice_data' => [ @@ -476,7 +473,6 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 100, 'subtotal_incl_tax' => 119.07, 'base_subtotal_incl_tax' => 119.07, - ], ], ]; @@ -580,7 +576,98 @@ class WeeeTest extends \PHPUnit_Framework_TestCase 'base_subtotal' => 100, 'subtotal_incl_tax' => 114.95, 'base_subtotal_incl_tax' => 114.95, + ], + ], + ]; + // 3 item_1, $100 with $weee, 8.25 tax rate. Invoicing qty 0. + $result['zero_invoice'] = [ + 'order_data' => [ + 'previous_invoices' => [ + ], + 'data_fields' => [ + 'shipping_tax_amount' => 1.24, + 'base_shipping_tax_amount' => 1.24, + 'shipping_hidden_tax_amount' => 0, + 'base_shipping_hidden_tax_amount' => 0, + 'tax_amount' => 16.09, + 'tax_invoiced' => 0, + 'base_tax_amount' => 16.09, + 'base_tax_amount_invoiced' => 0, + 'subtotal' => '300', + 'base_subtotal' => '300', + ], + ], + 'invoice_data' => [ + 'items' => [ + 'item_1' => [ + 'order_item' => [ + 'qty_ordered' => 3, + 'weee_tax_applied_row_amount' => 30, + 'base_weee_tax_applied_row_amnt' => 30, + 'row_weee_tax_incl_tax' => 32.47, + 'base_row_weee_tax_incl_tax' => 32.47, + 'weee_amount_invoiced' => 0, + 'base_weee_amount_invoiced' => 0, + 'weee_tax_amount_invoiced' => 0, + 'base_weee_tax_amount_invoiced' => 0, + 'applied_weee' => [ + [ + 'title' => 'recycling_fee', + 'base_row_amount' => 30, + 'row_amount' => 30, + 'base_row_amount_incl_tax' => 32.47, + 'row_amount_incl_tax' => 32.47, + ], + ], + 'applied_weee_updated' => [ + 'base_row_amount_invoiced' => 30, + 'row_amount_invoiced' => 30, + 'base_tax_amount_invoiced' => 2.47, + 'tax_amount_invoiced' => 2.47, + ], + 'qty_invoiced' => 0, + ], + 'is_last' => true, + 'data_fields' => [ + 'qty' => 0, + 'applied_weee' => [ + [ + ], + ], + ], + ], + ], + 'is_last' => true, + 'include_in_subtotal' => false, + 'data_fields' => [ + 'grand_total' => 181.09, + 'base_grand_total' => 181.09, + 'subtotal' => 300, + 'base_subtotal' => 300, + 'subtotal_incl_tax' => 314.85, + 'base_subtotal_incl_tax' => 314.85, + 'tax_amount' => 16.09, + 'base_tax_amount' => 16.09, + ], + ], + 'expected_results' => [ + 'invoice_items' => [ + 'item_1' => [ + 'applied_weee' => [ + [ + 'title' => 'recycling_fee', + 'base_row_amount' => 0, + 'row_amount' => 0, + 'base_row_amount_incl_tax' => 0, + 'row_amount_incl_tax' => 0, + ], + ], + ], + ], + 'invoice_data' => [ + 'subtotal' => 300, + 'base_subtotal' => 300, ], ], ];