Some fixing up for #1598

This commit is contained in:
James Cole
2018-08-07 17:34:43 +02:00
parent 5908c0ce8c
commit b496ca6a2c
4 changed files with 134 additions and 120 deletions

View File

@@ -80,24 +80,34 @@ class AmountController extends Controller
*/
public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget): JsonResponse
{
$amount = (string)$request->get('amount');
$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);
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
$currency = app('amount')->getDefaultCurrency();
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
$largeDiff = false;
$warnText = '';
$leftPerDay = null;
$periodLength = $start->diffInDays($end);
$dayDifference = $this->getDayDifference($start, $end);
// grab vars from URI
$amount = (string)$request->get('amount');
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
// grab other useful vars
$currency = app('amount')->getDefaultCurrency();
$activeDaysLeft = $this->activeDaysLeft($start, $end);
$periodLength = $start->diffInDays($end) + 1; // absolute period length.
// update limit amount:
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
// calculate what the user has spent in current period.
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
// given the new budget, this is what they have left (and left per day?)
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
$leftPerDay = null; //
// If the user budgets ANY amount per day for this budget (anything but zero) Firefly III calculates how much he could spend per day.
if (1 === bccomp(bcadd($amount, $spent), '0')) {
$leftPerDay = app('amount')->formatAnything($currency, bcdiv(bcadd($amount, $spent), (string)$dayDifference), true);
$leftPerDay = app('amount')->formatAnything($currency, bcdiv(bcadd($amount, $spent), (string)$activeDaysLeft), true);
}
$largeDiff = false;
$warnText = '';
// Get the average amount of money the user budgets for this budget. And calculate the same for the current amount.
// If the difference is very large, give the user a notification.
$average = $this->repository->budgetedPerDay($budget);
@@ -116,8 +126,17 @@ class AmountController extends Controller
app('preferences')->mark();
return response()->json(
['left' => $left, 'name' => $budget->name, 'limit' => $budgetLimit ? $budgetLimit->id : 0, 'amount' => $amount, 'current' => $current,
'average' => $average, 'large_diff' => $largeDiff, 'left_per_day' => $leftPerDay, 'warn_text' => $warnText,]
[
'left' => $left,
'name' => $budget->name,
'limit' => $budgetLimit ? $budgetLimit->id : 0,
'amount' => $amount,
'current' => $current,
'average' => $average,
'large_diff' => $largeDiff,
'left_per_day' => $leftPerDay,
'warn_text' => $warnText,
]
);
}