fix: error handling for arithmetic operations and improve calculations in Document and TaxSummary classes. Added checks for division and modulo by zero, and adjusted percentage calculation logic to handle zero sub-total cases.

This commit is contained in:
Hayatunnabi Nabil 2025-11-13 13:41:12 +06:00
parent b5d8499ac7
commit b56a3bda56
3 changed files with 20 additions and 4 deletions

View File

@ -546,11 +546,19 @@ abstract class Report
public function divArithmeticAmount(&$current, $amount)
{
if ($amount == 0) {
throw new \InvalidArgumentException('Division by zero is not allowed');
}
$current = $current / $amount;
}
public function modArithmeticAmount(&$current, $amount)
{
if ($amount == 0) {
throw new \InvalidArgumentException('Modulo by zero is not allowed');
}
$current = $current % $amount;
}

View File

@ -355,7 +355,11 @@ class Document extends Model
if ($discount) {
$sub_total = $this->totals->where('code', 'sub_total')->makeHidden('title')->pluck('amount')->first();
$percent = number_format((($discount * 100) / $sub_total), 0);
if ($sub_total && $sub_total > 0) {
$percent = number_format((($discount * 100) / $sub_total), 0);
} else {
$percent = 0;
}
}
return $percent;
@ -423,7 +427,7 @@ class Document extends Model
}
if ($transaction->reconciled) {
$reconciled_amount = +$amount;
$reconciled_amount += $amount;
}
}
}

View File

@ -126,8 +126,12 @@ class TaxSummary extends Report
}
if ($date_field == 'paid_at') {
$rate = ($item->amount * 100) / $type_item->amount;
$item_amount = ($item_total->amount / 100) * $rate;
if ($type_item->amount != 0) {
$rate = ($item->amount * 100) / $type_item->amount;
$item_amount = ($item_total->amount / 100) * $rate;
} else {
$item_amount = $item_total->amount;
}
} else {
$item_amount = $item_total->amount;
}