mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-10 04:21:20 +00:00
Make report budget charts multi-currency
This commit is contained in:
@@ -29,6 +29,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
@@ -388,16 +389,15 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a budget overview chart (spent and budgeted).
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
* @param Budget $budget
|
||||
* @param TransactionCurrency $currency
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function period(Budget $budget, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
||||
public function period(Budget $budget, TransactionCurrency $currency, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
@@ -405,27 +405,52 @@ class BudgetController extends Controller
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty($accounts);
|
||||
$cache->addProperty($budget->id);
|
||||
$cache->addProperty($currency->id);
|
||||
$cache->addProperty('chart.budget.period');
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
// return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$periods = app('navigation')->listOfPeriods($start, $end);
|
||||
$entries = $this->opsRepository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end); // get the expenses
|
||||
$budgeted = $this->getBudgetedInPeriod($budget, $start, $end);
|
||||
|
||||
// join them into one set of data:
|
||||
$chartData = [
|
||||
['label' => (string)trans('firefly.spent'), 'type' => 'bar', 'entries' => []],
|
||||
['label' => (string)trans('firefly.budgeted'), 'type' => 'bar', 'entries' => []],
|
||||
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
||||
$chartData = [
|
||||
[
|
||||
'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency->name]),
|
||||
'type' => 'bar',
|
||||
'entries' => [],
|
||||
'currency_symbol' => $currency->symbol,
|
||||
],
|
||||
[
|
||||
'label' => (string)trans('firefly.box_budgeted_in_currency', ['currency' => $currency->name]),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'entries' => [],
|
||||
],
|
||||
];
|
||||
|
||||
foreach (array_keys($periods) as $period) {
|
||||
$label = $periods[$period];
|
||||
$spent = $entries[$budget->id]['entries'][$period] ?? '0';
|
||||
$limit = (int)($budgeted[$period] ?? 0);
|
||||
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
|
||||
$chartData[1]['entries'][$label] = $limit;
|
||||
|
||||
$currentStart = clone $start;
|
||||
while ($currentStart <= $end) {
|
||||
$title = $currentStart->formatLocalized($titleFormat);
|
||||
$currentEnd = app('navigation')->endOfPeriod($currentStart, '1M');
|
||||
|
||||
// default limit is no limit:
|
||||
$chartData[0]['entries'][$title] = 0;
|
||||
$chartData[1]['entries'][$title] = 0;
|
||||
|
||||
|
||||
// get budget limit in this period for this currency.
|
||||
$limit = $this->blRepository->find($budget, $currency, $currentStart, $currentEnd);
|
||||
if (null !== $limit) {
|
||||
$chartData[1]['entries'][$title] = round($limit->amount, $currency->decimal_places);
|
||||
}
|
||||
|
||||
// get spent amount in this period for this currency.
|
||||
$sum = $this->opsRepository->sumExpenses($currentStart, $currentEnd, $accounts, new Collection([$budget]), $currency);
|
||||
$amount = app('steam')->positive($sum[$currency->id]['sum'] ?? '0');
|
||||
$chartData[0]['entries'][$title] = round($amount, $currency->decimal_places);
|
||||
|
||||
$currentStart = app('navigation')->addPeriod($currentStart, '1M', 0);
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
@@ -436,37 +461,39 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a chart for transactions without a budget.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param TransactionCurrency $currency
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function periodNoBudget(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
||||
public function periodNoBudget(TransactionCurrency $currency, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty($accounts);
|
||||
$cache->addProperty($currency->id);
|
||||
$cache->addProperty('chart.budget.no-budget');
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// the expenses:
|
||||
$periods = app('navigation')->listOfPeriods($start, $end);
|
||||
$entries = $this->nbRepository->getNoBudgetPeriodReport($accounts, $start, $end);
|
||||
$chartData = [];
|
||||
|
||||
// join them:
|
||||
foreach (array_keys($periods) as $period) {
|
||||
$label = $periods[$period];
|
||||
$spent = $entries['entries'][$period] ?? '0';
|
||||
$chartData[$label] = bcmul($spent, '-1');
|
||||
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
||||
$chartData = [];
|
||||
$currentStart = clone $start;
|
||||
while ($currentStart <= $end) {
|
||||
$currentEnd = app('navigation')->endOfPeriod($currentStart, '1M');
|
||||
$title = $currentStart->formatLocalized($titleFormat);
|
||||
$sum = $this->nbRepository->sumExpenses($currentStart, $currentEnd, $accounts, $currency);
|
||||
$amount = app('steam')->positive($sum[$currency->id]['sum'] ?? '0');
|
||||
$chartData[$title] = round($amount, $currency->decimal_places);
|
||||
$currentStart = app('navigation')->addPeriod($currentStart, '1M', 0);
|
||||
}
|
||||
|
||||
$data = $this->generator->singleSet((string)trans('firefly.spent'), $chartData);
|
||||
$cache->store($data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user