Make report budget charts multi-currency

This commit is contained in:
James Cole
2019-09-01 16:52:49 +02:00
parent ce06fb73b1
commit 5c099008e8
15 changed files with 211 additions and 236 deletions

View File

@@ -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);

View File

@@ -39,7 +39,6 @@ use FireflyIII\Support\Http\Controllers\ChartGeneration;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Log;
/**
* Class CategoryController.
@@ -275,16 +274,19 @@ class CategoryController extends Controller
// loop income and expenses for this category.:
$outSet = $expenses[$currencyId]['categories'][$category->id] ?? ['transaction_journals' => []];
foreach ($outSet['transaction_journals'] as $journal) {
$amount = app('steam')->positive($journal['amount']);
$date = $journal['date']->formatLocalized($format);
$chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0';
$chartData[$outKey]['entries'][$date] = bcadd($journal['amount'], $chartData[$outKey]['entries'][$date]);
$chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]);
}
$inSet = $income[$currencyId]['categories'][$category->id] ?? ['transaction_journals' => []];
foreach ($inSet['transaction_journals'] as $journal) {
$amount = app('steam')->positive($journal['amount']);
$date = $journal['date']->formatLocalized($format);
$chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0';
$chartData[$inKey]['entries'][$date] = bcadd($journal['amount'], $chartData[$inKey]['entries'][$date]);
$chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]);
}
}
@@ -313,7 +315,7 @@ class CategoryController extends Controller
$cache->addProperty('chart.category.period.no-cat');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore
//return response()->json($cache->get()); // @codeCoverageIgnore
}
/** @var NoCategoryRepositoryInterface $noCatRepository */
@@ -358,16 +360,18 @@ class CategoryController extends Controller
// loop income and expenses:
$outSet = $expenses[$currencyId] ?? ['transaction_journals' => []];
foreach ($outSet['transaction_journals'] as $journal) {
$amount = app('steam')->positive($journal['amount']);
$date = $journal['date']->formatLocalized($format);
$chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0';
$chartData[$outKey]['entries'][$date] = bcadd($journal['amount'], $chartData[$outKey]['entries'][$date]);
$chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]);
}
$inSet = $income[$currencyId] ?? ['transaction_journals' => []];
foreach ($inSet['transaction_journals'] as $journal) {
$amount = app('steam')->positive($journal['amount']);
$date = $journal['date']->formatLocalized($format);
$chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0';
$chartData[$inKey]['entries'][$date] = bcadd($journal['amount'], $chartData[$inKey]['entries'][$date]);
$chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]);
}
}
$data = $this->generator->multiSet($chartData);

View File

@@ -24,9 +24,11 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
@@ -46,6 +48,7 @@ class ReportController extends Controller
/**
* ReportController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
@@ -131,8 +134,6 @@ class ReportController extends Controller
/**
* Shows income and expense, debit/credit: operations.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
@@ -151,112 +152,84 @@ class ReportController extends Controller
return response()->json($cache->get()); // @codeCoverageIgnore
}
Log::debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray());
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$source = $this->getChartData($accounts, $start, $end);
$chartData = [
[
'label' => (string)trans('firefly.income'),
$format = app('navigation')->preferredCarbonFormat($start, $end);
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$ids = $accounts->pluck('id')->toArray();
// get journals for entire period:
$data = [];
$chartData = [];
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->setAccounts($accounts)->withAccountInformation();
$journals = $collector->getExtractedJournals();
// loop. group by currency and by period.
/** @var array $journal */
foreach ($journals as $journal) {
$period = $journal['date']->format($format);
$currencyId = (int)$journal['currency_id'];
$data[$currencyId] = $data[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_symbol'],
'currency_name' => $journal['currency_name'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$data[$currencyId][$period] = $data[$currencyId][$period] ?? [
'period' => $period,
'spent' => '0',
'earned' => '0',
];
// in our outgoing?
$key = 'spent';
$amount = app('steam')->positive($journal['amount']);
if (TransactionType::DEPOSIT === $journal['transaction_type_type']
|| (TransactionType::TRANSFER === $journal['transaction_type_type']
&& in_array(
$journal['destination_id'], $ids, true
))) {
$key = 'earned';
}
$data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount);
}
// loop this data, make chart bars for each currency:
/** @var array $currency */
foreach ($data as $currency) {
$income = [
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'entries' => [],
],
[
'label' => (string)trans('firefly.expenses'),
];
$expense = [
'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'entries' => [],
],
];
foreach ($source['earned'] as $date => $amount) {
$carbon = new Carbon($date);
$label = $carbon->formatLocalized($format);
$earned = $chartData[0]['entries'][$label] ?? '0';
$amount = bcmul($amount, '-1');
$chartData[0]['entries'][$label] = bcadd($earned, $amount);
}
foreach ($source['spent'] as $date => $amount) {
$carbon = new Carbon($date);
$label = $carbon->formatLocalized($format);
$spent = $chartData[1]['entries'][$label] ?? '0';
$chartData[1]['entries'][$label] = bcadd($spent, $amount);
}
$data = $this->generator->multiSet($chartData);
$cache->store($data);
];
// loop all possible periods between $start and $end
$currentStart = clone $start;
while ($currentStart <= $end) {
$currentEnd = app('navigation')->endOfPeriod($currentStart, '1M');
$key = $currentStart->format($format);
$title = $currentStart->formatLocalized($titleFormat);
$income['entries'][$title] = round($currency[$key]['earned'] ?? '0', $currency['currency_decimal_places']);
$expense['entries'][$title] = round($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']);
return response()->json($data);
}
$currentStart = app('navigation')->addPeriod($currentStart, '1M', 0);
}
/**
* Shows sum income and expense, debit/credit: operations.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse
*
*/
public function sum(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty('chart.report.sum');
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore
$chartData[] = $income;
$chartData[] = $expense;
}
$source = $this->getChartData($accounts, $start, $end);
$numbers = [
'sum_earned' => '0',
'avg_earned' => '0',
'count_earned' => 0,
'sum_spent' => '0',
'avg_spent' => '0',
'count_spent' => 0,
];
foreach ($source['earned'] as $amount) {
$amount = bcmul($amount, '-1');
$numbers['sum_earned'] = bcadd($amount, $numbers['sum_earned']);
++$numbers['count_earned'];
}
if ($numbers['count_earned'] > 0) {
$numbers['avg_earned'] = $numbers['sum_earned'] / $numbers['count_earned'];
}
foreach ($source['spent'] as $amount) {
$numbers['sum_spent'] = bcadd($amount, $numbers['sum_spent']);
++$numbers['count_spent'];
}
if ($numbers['count_spent'] > 0) {
$numbers['avg_spent'] = $numbers['sum_spent'] / $numbers['count_spent'];
}
$chartData = [
[
'label' => (string)trans('firefly.income'),
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
'entries' => [
(string)trans('firefly.sum_of_period') => $numbers['sum_earned'],
(string)trans('firefly.average_in_period') => $numbers['avg_earned'],
],
],
[
'label' => (string)trans('firefly.expenses'),
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
'entries' => [
(string)trans('firefly.sum_of_period') => $numbers['sum_spent'],
(string)trans('firefly.average_in_period') => $numbers['avg_spent'],
],
],
];
$data = $this->generator->multiSet($chartData);
$cache->store($data);