mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
Improve box for #3071
This commit is contained in:
@@ -28,6 +28,7 @@ use FireflyIII\Helpers\Report\NetWorthInterface;
|
|||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use FireflyIII\Models\AvailableBudget;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
@@ -49,7 +50,10 @@ class BoxController extends Controller
|
|||||||
use RequestInformation;
|
use RequestInformation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How much money user has available.
|
* This box has three types of info to display:
|
||||||
|
* 0) If the user has available amount this period and has overspent: overspent box.
|
||||||
|
* 1) If the user has available amount this period and has NOT overspent: left to spend box.
|
||||||
|
* 2) if the user has no available amount set this period: spent per day
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
@@ -59,7 +63,6 @@ class BoxController extends Controller
|
|||||||
$repository = app(BudgetRepositoryInterface::class);
|
$repository = app(BudgetRepositoryInterface::class);
|
||||||
/** @var OperationsRepositoryInterface $opsRepository */
|
/** @var OperationsRepositoryInterface $opsRepository */
|
||||||
$opsRepository = app(OperationsRepositoryInterface::class);
|
$opsRepository = app(OperationsRepositoryInterface::class);
|
||||||
|
|
||||||
/** @var AvailableBudgetRepositoryInterface $abRepository */
|
/** @var AvailableBudgetRepositoryInterface $abRepository */
|
||||||
$abRepository = app(AvailableBudgetRepositoryInterface::class);
|
$abRepository = app(AvailableBudgetRepositoryInterface::class);
|
||||||
|
|
||||||
@@ -69,6 +72,9 @@ class BoxController extends Controller
|
|||||||
/** @var Carbon $end */
|
/** @var Carbon $end */
|
||||||
$end = session('end', Carbon::now()->endOfMonth());
|
$end = session('end', Carbon::now()->endOfMonth());
|
||||||
$today = new Carbon;
|
$today = new Carbon;
|
||||||
|
$display = 2; // see method docs.
|
||||||
|
$boxTitle = (string)trans('firefly.spent');
|
||||||
|
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
@@ -77,32 +83,47 @@ class BoxController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
// get available amount
|
$leftPerDayAmount = '0';
|
||||||
$currency = app('amount')->getDefaultCurrency();
|
$leftToSpendAmount = '0';
|
||||||
$available = $abRepository->getAvailableBudget($currency, $start, $end);
|
|
||||||
|
|
||||||
// get spent amount:
|
$currency = app('amount')->getDefaultCurrency();
|
||||||
$budgets = $repository->getActiveBudgets();
|
$availableBudgets = $abRepository->getAvailableBudgetsByDate($start, $end);
|
||||||
$budgetInformation = $opsRepository->collectBudgetInformation($budgets, $start, $end);
|
$availableBudgets = $availableBudgets->filter(
|
||||||
$spent = (string)array_sum(array_column($budgetInformation, 'spent'));
|
static function (AvailableBudget $availableBudget) use ($currency) {
|
||||||
$left = bcadd($available, $spent);
|
if ($availableBudget->transaction_currency_id === $currency->id) {
|
||||||
$days = $today->diffInDays($end) + 1;
|
return $availableBudget;
|
||||||
$perDay = '0';
|
}
|
||||||
$text = (string)trans('firefly.left_to_spend');
|
|
||||||
$overspent = false;
|
return null;
|
||||||
if (bccomp($left, '0') === -1) {
|
}
|
||||||
$text = (string)trans('firefly.overspent');
|
);
|
||||||
$overspent = true;
|
// spent in this period, in budgets, for default currency.
|
||||||
|
// also calculate spent per day.
|
||||||
|
$spent = $opsRepository->sumExpenses($start, $end, null, null, $currency);
|
||||||
|
$spentAmount = $spent[(int)$currency->id]['sum'] ?? '0';
|
||||||
|
$spentPerDay = '-1';
|
||||||
|
if (1 === $availableBudgets->count()) {
|
||||||
|
$display = 0; // assume user overspent
|
||||||
|
$boxTitle = (string)trans('firefly.overspent');
|
||||||
|
/** @var AvailableBudget $availableBudget */
|
||||||
|
$availableBudget = $availableBudgets->first();
|
||||||
|
// calculate with available budget.
|
||||||
|
$leftToSpendAmount = bcadd($availableBudget->amount, $spentAmount);
|
||||||
|
if (1 === bccomp($leftToSpendAmount, '0')) {
|
||||||
|
$boxTitle = (string)trans('firefly.left_to_spend');
|
||||||
|
$days = $today->diffInDays($end) + 1;
|
||||||
|
$display = 1; // not overspent
|
||||||
|
$leftPerDayAmount = bcdiv($leftToSpendAmount, (string)$days);
|
||||||
}
|
}
|
||||||
if (0 !== $days && bccomp($left, '0') > -1) {
|
|
||||||
$perDay = bcdiv($left, (string)$days);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$return = [
|
$return = [
|
||||||
'perDay' => app('amount')->formatAnything($currency, $perDay, false),
|
'display' => $display,
|
||||||
'left' => app('amount')->formatAnything($currency, $left, false),
|
'spent_total' => app('amount')->formatAnything($currency, $spentAmount, false),
|
||||||
'text' => $text,
|
'spent_per_day' => app('amount')->formatAnything($currency, $spentPerDay, false),
|
||||||
'overspent' => $overspent,
|
'left_to_spend' => app('amount')->formatAnything($currency, $leftToSpendAmount, false),
|
||||||
|
'left_per_day' => app('amount')->formatAnything($currency, $leftPerDayAmount, false),
|
||||||
|
'title' => $boxTitle,
|
||||||
];
|
];
|
||||||
|
|
||||||
$cache->store($return);
|
$cache->store($return);
|
||||||
|
19
public/v1/js/ff/index.js
vendored
19
public/v1/js/ff/index.js
vendored
@@ -73,12 +73,23 @@ function getNetWorthBox() {
|
|||||||
function getAvailableBox() {
|
function getAvailableBox() {
|
||||||
// box-left-to-spend
|
// box-left-to-spend
|
||||||
// box-left-per-day
|
// box-left-per-day
|
||||||
|
// * 0) If the user has available amount this period and has overspent: overspent box.
|
||||||
|
// * 1) If the user has available amount this period and has NOT overspent: left to spend box.
|
||||||
|
// * 2) if the user has no available amount set this period: spent per day
|
||||||
$.getJSON('json/box/available').done(function (data) {
|
$.getJSON('json/box/available').done(function (data) {
|
||||||
$('#box-left-to-spend').html(data.left);
|
$('#box-left-to-spend-text').text(data.title);
|
||||||
$('#box-left-per-day').html(data.perDay);
|
if (0 === data.display) {
|
||||||
$('#box-left-to-spend-text').text(data.text);
|
|
||||||
if(data.overspent === true) {
|
|
||||||
$('#box-left-to-spend-box').removeClass('bg-green-gradient').addClass('bg-red-gradient');
|
$('#box-left-to-spend-box').removeClass('bg-green-gradient').addClass('bg-red-gradient');
|
||||||
|
$('#box-left-to-spend').html(data.left_to_spend);
|
||||||
|
$('#box-left-per-day').html(data.left_per_day);
|
||||||
|
}
|
||||||
|
if(1=== data.display) {
|
||||||
|
$('#box-left-to-spend').html(data.left_to_spend);
|
||||||
|
$('#box-left-per-day').html(data.left_per_day);
|
||||||
|
}
|
||||||
|
if(2=== data.display) {
|
||||||
|
$('#box-left-to-spend').html(data.spent_total);
|
||||||
|
$('#box-left-per-day').html(data.spent_per_day);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user