Skip to content

Commit

Permalink
Fix Division by zero on PHP8.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
nanasess committed Sep 19, 2023
1 parent 70717b9 commit 988f46c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion data/class/helper/SC_Helper_TaxRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public static function getTaxPerTaxRate(array $arrTaxableTotal, $discount_total
$taxable_total = array_sum($arrTaxableTotal);
$result = [];
foreach ($arrTaxableTotal as $rate => $total) {
$reduced_total = $total - $discount_total * $total / array_sum($arrTaxableTotal);
if ($taxable_total > 0) {
$reduced_total = $total - $discount_total * $total / $taxable_total;
}

$tax = $reduced_total * ($rate / (100 + $rate));

Check failure on line 91 in data/class/helper/SC_Helper_TaxRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $reduced_total might not be defined.
$result[$rate] = [
'total' => intval(static::roundByCalcRule($reduced_total, $arrDefaultTaxRule['calc_rule'])),

Check failure on line 93 in data/class/helper/SC_Helper_TaxRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $reduced_total might not be defined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,51 @@ public function testGetTaxPerTaxRateWithRound()
);
}

public function testGetTaxPerTaxRateWithZero()
{
$this->setUpTaxRule([
[
'tax_rule_id' => 1004,
'apply_date' => '2019-10-01 00:00:00',
'tax_rate' => '10',
'calc_rule' => '1',
'product_id' => '0',
'product_class_id' => '0',
'del_flg' => '0',
'member_id' => 1,
'create_date' => '2000-01-01 00:00:00',
'update_date' => '2000-01-01 00:00:00',
],
]);

$arrTaxableTotal = [
10 => 0,
8 => 0,
];
$discount_total = 0;

$actual = SC_Helper_TaxRule_Ex::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
self::assertSame(
[
8 => [
'total' => 0,
'tax' => 0
],
10 => [
'total' => 0,
'tax' => 0
]
],
$actual
);

self::assertSame(
'(8%対象: 0円 内消費税: 0円)'.PHP_EOL.
'(10%対象: 0円 内消費税: 0円)'.PHP_EOL,
SC_Helper_TaxRule_Ex::getTaxDetail($arrTaxableTotal, $discount_total)
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
Expand Down

0 comments on commit 988f46c

Please sign in to comment.