Add budget warnings #1202

This commit is contained in:
James Cole
2018-03-24 06:46:37 +01:00
parent dafddfa39a
commit 310ed9f504
6 changed files with 630 additions and 543 deletions

View File

@@ -85,6 +85,10 @@ class BudgetController extends Controller
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
$largeDiff = false;
$warnText = '';
$average = '0';
$current = '0';
if (0 === bccomp($amount, '0')) {
$budgetLimit = null;
}
@@ -94,9 +98,43 @@ class BudgetController extends Controller
$currency = app('amount')->getDefaultCurrency();
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
// over or under budgeting, compared to previous budgets?
$average = $this->repository->budgetedPerDay($budget);
// current average per day:
$diff = $start->diffInDays($end);
$current = $amount;
if ($diff > 0) {
$current = bcdiv($amount, strval($diff));
}
if (bccomp(bcmul('1.1', $average), $current) === -1) {
$largeDiff = true;
$warnText = strval(
trans(
'firefly.over_budget_warn',
[
'amount' => app('amount')->formatAnything($currency, $average, false),
'over_amount' => app('amount')->formatAnything($currency, $current, false),
]
)
);
}
Preferences::mark();
return response()->json(['left' => $left, 'name' => $budget->name, 'limit' => $budgetLimit ? $budgetLimit->id : 0, 'amount' => $amount]);
return response()->json(
[
'left' => $left,
'name' => $budget->name,
'limit' => $budgetLimit ? $budgetLimit->id : 0,
'amount' => $amount,
'current' => $current,
'average' => $average,
'large_diff' => $largeDiff,
'warn_text' => $warnText,
]
);
}
/**
@@ -443,6 +481,7 @@ class BudgetController extends Controller
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
$limits = $this->getLimits($budget, $start, $end);
$repetition = null;
// collector:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);