This commit is contained in:
James Cole 2017-11-24 17:05:22 +01:00
parent 1aa325053e
commit 7a1f698d5e
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 59 additions and 36 deletions

View File

@ -79,7 +79,7 @@ class BudgetController extends Controller
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function amount(Request $request, Budget $budget) public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget)
{ {
$amount = strval($request->get('amount')); $amount = strval($request->get('amount'));
$start = Carbon::createFromFormat('Y-m-d', $request->get('start')); $start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
@ -88,9 +88,15 @@ class BudgetController extends Controller
if (0 === $amount) { if (0 === $amount) {
$budgetLimit = null; $budgetLimit = null;
} }
// calculate left in budget:
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
$currency = app('amount')->getDefaultCurrency();
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
Preferences::mark(); Preferences::mark();
return Response::json(['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]);
} }
/** /**

View File

@ -37,6 +37,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use Navigation; use Navigation;
use stdClass; use stdClass;
@ -611,6 +612,7 @@ class BudgetRepository implements BudgetRepositoryInterface
->where('budget_limits.start_date', $start->format('Y-m-d')) ->where('budget_limits.start_date', $start->format('Y-m-d'))
->where('budget_limits.end_date', $end->format('Y-m-d')) ->where('budget_limits.end_date', $end->format('Y-m-d'))
->get(['budget_limits.*'])->count(); ->get(['budget_limits.*'])->count();
Log::debug(sprintf('Found %d budget limits.', $limits));
// there might be a budget limit for these dates: // there might be a budget limit for these dates:
/** @var BudgetLimit $limit */ /** @var BudgetLimit $limit */
$limit = $budget->budgetlimits() $limit = $budget->budgetlimits()
@ -620,6 +622,7 @@ class BudgetRepository implements BudgetRepositoryInterface
// if more than 1 limit found, delete the others: // if more than 1 limit found, delete the others:
if ($limits > 1 && null !== $limit) { if ($limits > 1 && null !== $limit) {
Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
$budget->budgetlimits() $budget->budgetlimits()
->where('budget_limits.start_date', $start->format('Y-m-d')) ->where('budget_limits.start_date', $start->format('Y-m-d'))
->where('budget_limits.end_date', $end->format('Y-m-d')) ->where('budget_limits.end_date', $end->format('Y-m-d'))
@ -627,19 +630,23 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
// delete if amount is zero. // delete if amount is zero.
if (null !== $limit && bccomp('0', $amount) < 0) { // Returns 0 if the two operands are equal,
// 1 if the left_operand is larger than the right_operand, -1 otherwise.
if (null !== $limit && bccomp($amount, '0') <= 0) {
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
$limit->delete(); $limit->delete();
return new BudgetLimit; return new BudgetLimit;
} }
// update if exists: // update if exists:
if (null !== $limit) { if (null !== $limit) {
Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
$limit->amount = $amount; $limit->amount = $amount;
$limit->save(); $limit->save();
return $limit; return $limit;
} }
Log::debug('No existing budget limit, create a new one');
// or create one and return it. // or create one and return it.
$limit = new BudgetLimit; $limit = new BudgetLimit;
$limit->budget()->associate($budget); $limit->budget()->associate($budget);
@ -647,6 +654,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$limit->end_date = $end; $limit->end_date = $end;
$limit->amount = $amount; $limit->amount = $amount;
$limit->save(); $limit->save();
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
return $limit; return $limit;
} }

View File

@ -38,7 +38,7 @@ $(function () {
/* /*
When the input changes, update the percentages for the budgeted bar: When the input changes, update the percentages for the budgeted bar:
*/ */
$('input[type="number"]').on('input', updateBudgetedAmounts); $('input[type="number"]').on('change', updateBudgetedAmounts);
// //
$('.selectPeriod').change(function (e) { $('.selectPeriod').change(function (e) {
@ -104,46 +104,55 @@ function updateBudgetedAmounts(e) {
"use strict"; "use strict";
var target = $(e.target); var target = $(e.target);
var id = target.data('id'); var id = target.data('id');
var leftCell = $('td[class$="left"][data-id="' + id + '"]');
var link = $('a[data-id="'+id+'"][class="budget-link"]');
var value = target.val(); var value = target.val();
var original = target.data('original'); var original = target.data('original');
var difference = value - original;
var spentCell = $('td[class="spent"][data-id="' + id + '"]'); // disable input
var leftCell = $('td[class="left"][data-id="' + id + '"]'); target.prop('disabled', true);
var spentAmount = parseFloat(spentCell.data('spent'));
var newAmountLeft = spentAmount + parseFloat(value);
var amountLeftString = accounting.formatMoney(newAmountLeft);
if (newAmountLeft < 0) {
leftCell.html('<span class="text-danger">' + amountLeftString + '</span>');
}
if (newAmountLeft > 0) {
leftCell.html('<span class="text-success">' + amountLeftString + '</span>');
}
if (newAmountLeft === 0.0) {
leftCell.html('<span style="color:#999">' + amountLeftString + '</span>');
}
if (difference !== 0) { // replace link (for now)
// add difference to 'budgeted' var link.attr('href','#');
budgeted = budgeted + difference;
// update original: // replace "left" with spinner.
target.data('original', value); leftCell.empty().html('<i class="fa fa-fw fa-spin fa-spinner"></i>');
// run drawBudgetedBar() again:
drawBudgetedBar();
// send a post to Firefly to update the amount: // send a post to Firefly to update the amount:
var newUri = budgetAmountUri.replace("REPLACE", id); var newUri = budgetAmountUri.replace("REPLACE", id);
$.post(newUri, {amount: value, start: periodStart, end: periodEnd}).done(function (data) { $.post(newUri, {amount: value, start: periodStart, end: periodEnd}).done(function (data) {
// difference between new value and original value
var difference = value - original;
// update budgeted value
budgeted = budgeted + difference;
// fill in "left" value:
leftCell.html(data.left);
// update "budgeted" input:
target.val(data.amount);
// enable thing again
target.prop('disabled', false);
// set new original value:
target.data('original', data.amount);
// run drawBudgetedBar() again:
drawBudgetedBar();
// update the link if relevant: // update the link if relevant:
if (data.repetition > 0) { link.attr('href', 'budgets/show/' + id);
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id + '/' + data.repetition); if (data.limit > 0) {
} else { link.attr('href', 'budgets/show/' + id + '/' + data.limit);
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id);
} }
}); });
}
return;
} }
/** /**