mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Fix another division by zero
This commit is contained in:
@@ -83,7 +83,7 @@ class BudgetLimitController extends Controller
|
|||||||
$budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end);
|
$budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end);
|
||||||
|
|
||||||
// remove already budgeted currencies with the same date range
|
// remove already budgeted currencies with the same date range
|
||||||
$currencies = $collection->filter(
|
$currencies = $collection->filter(
|
||||||
static function (TransactionCurrency $currency) use ($budgetLimits, $start, $end) {
|
static function (TransactionCurrency $currency) use ($budgetLimits, $start, $end) {
|
||||||
/** @var BudgetLimit $limit */
|
/** @var BudgetLimit $limit */
|
||||||
foreach ($budgetLimits as $limit) {
|
foreach ($budgetLimits as $limit) {
|
||||||
@@ -116,7 +116,7 @@ class BudgetLimitController extends Controller
|
|||||||
*
|
*
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function store(Request $request): JsonResponse|RedirectResponse
|
public function store(Request $request): JsonResponse | RedirectResponse
|
||||||
{
|
{
|
||||||
app('log')->debug('Going to store new budget-limit.', $request->all());
|
app('log')->debug('Going to store new budget-limit.', $request->all());
|
||||||
// first search for existing one and update it if necessary.
|
// first search for existing one and update it if necessary.
|
||||||
@@ -125,14 +125,14 @@ class BudgetLimitController extends Controller
|
|||||||
if (null === $currency || null === $budget) {
|
if (null === $currency || null === $budget) {
|
||||||
throw new FireflyException('No valid currency or budget.');
|
throw new FireflyException('No valid currency or budget.');
|
||||||
}
|
}
|
||||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||||
|
|
||||||
if (false === $start || false === $end) {
|
if (false === $start || false === $end) {
|
||||||
return response()->json([]);
|
return response()->json([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$amount = (string)$request->get('amount');
|
$amount = (string)$request->get('amount');
|
||||||
$start->startOfDay();
|
$start->startOfDay();
|
||||||
$end->startOfDay();
|
$end->startOfDay();
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ class BudgetLimitController extends Controller
|
|||||||
|
|
||||||
app('log')->debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
app('log')->debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
||||||
|
|
||||||
$limit = $this->blRepository->find($budget, $currency, $start, $end);
|
$limit = $this->blRepository->find($budget, $currency, $start, $end);
|
||||||
|
|
||||||
// sanity check on amount:
|
// sanity check on amount:
|
||||||
if (0 === bccomp($amount, '0')) {
|
if (0 === bccomp($amount, '0')) {
|
||||||
@@ -177,15 +177,15 @@ class BudgetLimitController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($request->expectsJson()) {
|
if ($request->expectsJson()) {
|
||||||
$array = $limit->toArray();
|
$array = $limit->toArray();
|
||||||
// add some extra metadata:
|
// add some extra metadata:
|
||||||
$spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency);
|
$spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency);
|
||||||
$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';
|
$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';
|
||||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||||
$array['days_left'] = (string)$this->activeDaysLeft($start, $end);
|
$array['days_left'] = (string)$this->activeDaysLeft($start, $end);
|
||||||
// left per day:
|
// left per day:
|
||||||
$array['left_per_day'] = bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
$array['left_per_day'] = bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||||
|
|
||||||
// left per day formatted.
|
// left per day formatted.
|
||||||
$array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']);
|
$array['left_per_day_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $array['left_per_day']);
|
||||||
@@ -198,7 +198,7 @@ class BudgetLimitController extends Controller
|
|||||||
|
|
||||||
public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse
|
public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse
|
||||||
{
|
{
|
||||||
$amount = (string)$request->get('amount');
|
$amount = (string)$request->get('amount');
|
||||||
if ('' === $amount) {
|
if ('' === $amount) {
|
||||||
$amount = '0';
|
$amount = '0';
|
||||||
}
|
}
|
||||||
@@ -210,7 +210,7 @@ class BudgetLimitController extends Controller
|
|||||||
$budgetId = $budgetLimit->budget_id;
|
$budgetId = $budgetLimit->budget_id;
|
||||||
$currency = $budgetLimit->transactionCurrency;
|
$currency = $budgetLimit->transactionCurrency;
|
||||||
$this->blRepository->destroyBudgetLimit($budgetLimit);
|
$this->blRepository->destroyBudgetLimit($budgetLimit);
|
||||||
$array = [
|
$array = [
|
||||||
'budget_id' => $budgetId,
|
'budget_id' => $budgetId,
|
||||||
'left_formatted' => app('amount')->formatAnything($currency, '0'),
|
'left_formatted' => app('amount')->formatAnything($currency, '0'),
|
||||||
'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'),
|
'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'),
|
||||||
@@ -224,23 +224,23 @@ class BudgetLimitController extends Controller
|
|||||||
$amount = bcmul($amount, '-1');
|
$amount = bcmul($amount, '-1');
|
||||||
}
|
}
|
||||||
|
|
||||||
$limit = $this->blRepository->update($budgetLimit, ['amount' => $amount]);
|
$limit = $this->blRepository->update($budgetLimit, ['amount' => $amount]);
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
$array = $limit->toArray();
|
$array = $limit->toArray();
|
||||||
|
|
||||||
$spentArr = $this->opsRepository->sumExpenses(
|
$spentArr = $this->opsRepository->sumExpenses(
|
||||||
$limit->start_date,
|
$limit->start_date,
|
||||||
$limit->end_date,
|
$limit->end_date,
|
||||||
null,
|
null,
|
||||||
new Collection([$budgetLimit->budget]),
|
new Collection([$budgetLimit->budget]),
|
||||||
$budgetLimit->transactionCurrency
|
$budgetLimit->transactionCurrency
|
||||||
);
|
);
|
||||||
$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';
|
$daysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date);
|
||||||
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';
|
||||||
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
$array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount']));
|
||||||
$array['days_left'] = (string)$this->activeDaysLeft($limit->start_date, $limit->end_date);
|
$array['amount_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, $limit['amount']);
|
||||||
// left per day:
|
$array['days_left'] = (string)$daysLeft;
|
||||||
$array['left_per_day'] = bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
$array['left_per_day'] = 0 === $daysLeft ? bcadd($array['spent'], $array['amount']) : bcdiv(bcadd($array['spent'], $array['amount']), $array['days_left']);
|
||||||
|
|
||||||
// left per day formatted.
|
// left per day formatted.
|
||||||
$array['amount'] = app('steam')->bcround($limit['amount'], $limit->transactionCurrency->decimal_places);
|
$array['amount'] = app('steam')->bcround($limit['amount'], $limit->transactionCurrency->decimal_places);
|
||||||
|
Reference in New Issue
Block a user