Skip to content

Commit

Permalink
Merge pull request #27 from mygento/LIT-74
Browse files Browse the repository at this point in the history
Fixed Division by zero
  • Loading branch information
luckyraul authored Mar 24, 2021
2 parents dc8e518 + b1dfd9d commit 2406e0b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Helper/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,14 @@ private function addTaxValue($taxAttributeCode, $item)
* It checks do we need to spread discount on all units and sets flag
* $this->spreadDiscOnAllUnits
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function checkSpread()
{
$items = $this->getAllItems();

$this->discountlessSum = 0.00;
$discountSum = 0;
foreach ($items as $item) {
$qty = $item->getQty() ?: $item->getQtyOrdered();
$discountAmountInclTax = $this->getItemDiscountAmountInclTax($item);
Expand All @@ -795,10 +797,13 @@ private function checkSpread()

$this->wryItemUnitPriceExists = $decimals > 2 ? true : false;
}

$discountSum += $discountAmountInclTax;
}

//Есть ли общая скидка на Чек. bccomp returns 0 if operands are equal
if (bccomp((string) $this->getGlobalDiscount(), '0.00', 2) !== 0) {
$isGlobalDiscountExist = bccomp((string) $this->getGlobalDiscount(), '0.00', 2) !== 0;
if ($isGlobalDiscountExist) {
$this->generalHelper->debug('1. Global discount on whole cheque.');

return true;
Expand All @@ -811,7 +816,8 @@ private function checkSpread()
return true;
}

if ($this->spreadDiscOnAllUnits) {
$isDiscountExist = bccomp($discountSum, '0.00', 2) !== 0;
if ($this->spreadDiscOnAllUnits && $isDiscountExist) {
$this->generalHelper->debug('3. SpreadDiscount = Yes.');

return true;
Expand Down
19 changes: 19 additions & 0 deletions Test/Unit/DiscountAffectsShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,25 @@ protected static function getExpected()
],
];

$actualData[parent::TEST_CASE_NAME_27] = [
'sum' => '0.00',
'origGrandTotal' => 100.0,
'items' => [
100630 => [
'price' => 0.0,
'quantity' => 1.0,
'sum' => 0.0,
'tax' => 'vat20',
],
'shipping' => [
'price' => 100.0,
'quantity' => 1.0,
'sum' => 100.0,
'tax' => '',
],
],
];

return $actualData;
}
}
6 changes: 6 additions & 0 deletions Test/Unit/DiscountGeneralTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DiscountGeneralTestCase extends TestCase
const TEST_CASE_NAME_24 = '#case 24. Bug with shipping discount. Доставка со скидкой 100%. Настройки налогов как в #23';
const TEST_CASE_NAME_25 = '#case 25. Баг с отрицательной суммой товара (макс. цена в заказе) и отрицательной суммой доставки';
const TEST_CASE_NAME_26 = '#case 26. Баг с отрицательной стоимостью товара если есть Reward Points';
const TEST_CASE_NAME_27 = '#case 27. Division by zero';

const CHARS_LOWERS = 'abcdefghijklmnopqrstuvwxyz';
const CHARS_UPPERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand Down Expand Up @@ -437,6 +438,11 @@ public function getOrders()
$this->addItem($order, $this->getItem(0.0000, 0.0000, 0.0000, 1, 20, 0));
$final[self::TEST_CASE_NAME_26] = $order;

//Заказ бесплатного пробника. Клиент оплачивает только доставку
$order = $this->getNewOrderInstance(0.0000, 100.0000, 100, 0);
$this->addItem($order, $this->getItem(0.0000, 0.0000, 0.0000, 1, 20, 0));
$final[self::TEST_CASE_NAME_27] = $order;

return $final;
}

Expand Down
19 changes: 19 additions & 0 deletions Test/Unit/DiscountSplitItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,25 @@ protected static function getExpected()
],
];

$actualData[parent::TEST_CASE_NAME_27] = [
'sum' => '0.00',
'origGrandTotal' => 100.0,
'items' => [
100630 => [
'price' => 0.0,
'quantity' => 1.0,
'sum' => 0.0,
'tax' => 'vat20',
],
'shipping' => [
'price' => 100.0,
'quantity' => 1.0,
'sum' => 100.0,
'tax' => '',
],
],
];

return $actualData;
}
}
19 changes: 19 additions & 0 deletions Test/Unit/DiscountSpreadAndSplitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,25 @@ protected static function getExpected()
],
];

$actualData[parent::TEST_CASE_NAME_27] = [
'sum' => '0.00',
'origGrandTotal' => 100.0,
'items' => [
100630 => [
'price' => 0.0,
'quantity' => 1.0,
'sum' => 0.0,
'tax' => 'vat20',
],
'shipping' => [
'price' => 100.0,
'quantity' => 1.0,
'sum' => 100.0,
'tax' => '',
],
],
];

return $actualData;
}
}
19 changes: 19 additions & 0 deletions Test/Unit/DiscountSpreadOnAllItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,25 @@ protected static function getExpected()
],
];

$actualData[parent::TEST_CASE_NAME_27] = [
'sum' => '0.00',
'origGrandTotal' => 100.0,
'items' => [
100630 => [
'price' => 0.0,
'quantity' => 1.0,
'sum' => 0.0,
'tax' => 'vat20',
],
'shipping' => [
'price' => 100.0,
'quantity' => 1.0,
'sum' => 100.0,
'tax' => '',
],
],
];

return $actualData;
}
}
19 changes: 19 additions & 0 deletions Test/Unit/DiscountWithoutCalcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,25 @@ protected static function getExpected()
],
];

$actualData[parent::TEST_CASE_NAME_27] = [
'sum' => '0.00',
'origGrandTotal' => 100.0,
'items' => [
100630 => [
'price' => 1,
'quantity' => 1.0,
'sum' => 0.0,
'tax' => 'vat20',
],
'shipping' => [
'price' => 100.0,
'quantity' => 1.0,
'sum' => 100.0,
'tax' => '',
],
],
];

return $actualData;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mygento/module-base",
"type": "magento2-module",
"version": "2.3.17",
"version": "2.3.18",
"license": "OSL-3.0",
"homepage": "https://github.com/mygento/module-base",
"description": "Mygento Base",
Expand Down

0 comments on commit 2406e0b

Please sign in to comment.