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

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