firefly-iii/app/Http/Controllers/Chart/AccountController.php

585 lines
21 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
/**
* AccountController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-09 12:24:08 -05:00
/** @noinspection NullPointerExceptionInspection */
declare(strict_types=1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-12-11 09:02:04 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
2016-05-13 10:22:24 -05:00
use FireflyIII\Models\AccountType;
2016-11-20 11:31:29 -06:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
2016-10-10 00:25:27 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2016-05-13 10:22:24 -05:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
2018-07-14 16:22:08 -05:00
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-08 05:08:53 -05:00
use Illuminate\Http\JsonResponse;
2015-05-17 11:03:16 -05:00
use Illuminate\Support\Collection;
use Log;
2015-05-16 02:41:14 -05:00
2018-07-14 16:22:08 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AccountController.
2018-07-14 16:22:08 -05:00
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2015-05-16 02:41:14 -05:00
*/
class AccountController extends Controller
{
use DateCalculation, AugumentData;
2018-07-14 16:22:08 -05:00
2018-07-21 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
protected $generator;
/**
2018-07-21 01:06:24 -05:00
* AccountController constructor.
*/
public function __construct()
{
parent::__construct();
2016-12-11 10:05:48 -06:00
$this->generator = app(GeneratorInterface::class);
}
2016-12-11 04:15:19 -06:00
2015-12-12 03:41:51 -06:00
/**
* Shows the balances for all the user's expense accounts (on the front page).
2015-12-28 00:38:02 -06:00
*
2016-10-10 00:49:39 -05:00
* @param AccountRepositoryInterface $repository
2015-12-12 03:41:51 -06:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2015-12-12 03:41:51 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseAccounts(AccountRepositoryInterface $repository): JsonResponse
2015-12-12 03:41:51 -06:00
{
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2016-05-13 10:22:24 -05:00
$start = clone session('start', Carbon::now()->startOfMonth());
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2016-05-13 10:22:24 -05:00
$end = clone session('end', Carbon::now()->endOfMonth());
$cache = new CacheProperties;
2015-12-12 03:41:51 -06:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-accounts');
2015-12-12 03:41:51 -06:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2015-12-12 03:41:51 -06:00
}
2016-05-13 10:22:24 -05:00
$start->subDay();
2016-12-11 10:05:48 -06:00
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [];
$chartData = [
[
'label' => (string)trans('firefly.spent'),
'type' => 'bar',
'currency_symbol' => '¤',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
'entries' => [],
],
];
2016-12-11 10:05:48 -06:00
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$accountNames = $this->extractNames($accounts);
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
$tempData = [];
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int)$accountId;
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int)$currencyId;
$startAmount = $startBalances[$accountId][$currencyId] ?? '0';
$diff = bcsub($endAmount, $startAmount);
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$title = (string)trans(
'firefly.account_in_currency', ['account' => $accountNames[$accountId], 'currency' => $currencies[$currencyId]->name]
);
$tempData[$title] = $diff;
2016-05-13 10:22:24 -05:00
}
2016-12-11 09:38:21 -06:00
}
arsort($tempData, SORT_NUMERIC);
foreach ($tempData as $label => $entry) {
if (0 !== bccomp($entry, '0')) {
$chartData[0]['entries'][$label] = $entry;
}
}
if (1 === \count($currencies)) {
$first = array_first($currencies);
$chartData[0]['currency_symbol'] = $first->symbol;
}
$data = $this->generator->multiSet($chartData);
2015-12-12 03:41:51 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-05-17 11:03:16 -05:00
}
2018-07-08 05:08:53 -05:00
2016-11-20 11:31:29 -06:00
/**
2018-07-21 01:06:24 -05:00
* Expenses per budget, as shown on account overview.
*
2017-02-25 06:19:42 -06:00
* @param Account $account
* @param Carbon $start
* @param Carbon $end
2016-11-20 11:31:29 -06:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2016-11-20 11:31:29 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseBudget(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-budget');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-11-20 11:31:29 -06:00
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2017-02-25 06:19:42 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withBudgetInformation()->setTypes([TransactionType::WITHDRAWAL]);
$transactions = $collector->getTransactions();
2016-12-11 10:05:48 -06:00
$chartData = [];
$result = [];
2016-11-20 11:31:29 -06:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
2018-04-02 08:10:40 -05:00
$jrnlBudgetId = (int)$transaction->transaction_journal_budget_id;
$transBudgetId = (int)$transaction->transaction_budget_id;
2016-12-11 10:05:48 -06:00
$budgetId = max($jrnlBudgetId, $transBudgetId);
2016-11-20 11:31:29 -06:00
$result[$budgetId] = $result[$budgetId] ?? '0';
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
}
2016-12-11 10:05:48 -06:00
2016-11-20 11:31:29 -06:00
$names = $this->getBudgetNames(array_keys($result));
2016-12-11 10:05:48 -06:00
foreach ($result as $budgetId => $amount) {
$chartData[$names[$budgetId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
/**
2018-07-21 01:06:24 -05:00
* Expenses per budget for all time, as shown on account overview.
*
2017-02-25 06:19:42 -06:00
* @param AccountRepositoryInterface $repository
* @param Account $account
2016-11-20 11:31:29 -06:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2016-11-20 11:31:29 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseBudgetAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
2017-02-25 06:19:42 -06:00
{
2018-08-07 12:29:10 -05:00
$start = $repository->oldestJournalDate($account) ?? Carbon::now()->startOfMonth();
2017-02-25 06:19:42 -06:00
$end = Carbon::now();
return $this->expenseBudget($account, $start, $end);
}
2018-07-08 05:08:53 -05:00
2017-02-25 06:19:42 -06:00
/**
2018-07-21 01:06:24 -05:00
* Expenses per category for one single account.
*
2017-02-25 06:19:42 -06:00
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2017-02-25 06:19:42 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseCategory(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.expense-category');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-11-20 11:31:29 -06:00
}
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2016-11-20 11:31:29 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::WITHDRAWAL]);
$transactions = $collector->getTransactions();
2016-11-20 11:31:29 -06:00
$result = [];
2016-12-11 10:05:48 -06:00
$chartData = [];
2016-11-20 11:31:29 -06:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
2018-04-02 08:10:40 -05:00
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
$transCatId = (int)$transaction->transaction_category_id;
2016-12-11 10:05:48 -06:00
$categoryId = max($jrnlCatId, $transCatId);
2016-11-20 11:31:29 -06:00
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
2016-12-11 10:05:48 -06:00
2016-11-20 11:31:29 -06:00
$names = $this->getCategoryNames(array_keys($result));
2016-12-11 10:05:48 -06:00
foreach ($result as $categoryId => $amount) {
$chartData[$names[$categoryId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
2017-02-25 06:19:42 -06:00
/**
2018-07-21 01:06:24 -05:00
* Expenses grouped by category for account.
*
2017-02-25 06:19:42 -06:00
* @param AccountRepositoryInterface $repository
* @param Account $account
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2017-02-25 06:19:42 -06:00
*/
2018-07-08 05:08:53 -05:00
public function expenseCategoryAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
2017-02-25 06:19:42 -06:00
{
2018-08-07 12:29:10 -05:00
$start = $repository->oldestJournalDate($account) ?? Carbon::now()->startOfMonth();
2017-02-25 06:19:42 -06:00
$end = Carbon::now();
return $this->expenseCategory($account, $start, $end);
}
2018-07-09 12:24:08 -05:00
2015-08-01 00:04:41 -05:00
/**
2016-05-13 08:53:39 -05:00
* Shows the balances for all the user's frontpage accounts.
2015-08-01 00:04:41 -05:00
*
2016-10-10 00:25:27 -05:00
* @param AccountRepositoryInterface $repository
2015-08-01 00:04:41 -05:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2015-08-01 00:04:41 -05:00
*/
2018-07-08 05:08:53 -05:00
public function frontpage(AccountRepositoryInterface $repository): JsonResponse
2015-08-01 00:04:41 -05:00
{
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
$defaultSet = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray();
Log::debug('Default set is ', $defaultSet);
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
2018-07-09 12:24:08 -05:00
Log::debug('Frontpage preference set is ', $frontPage->data);
2018-04-27 23:23:13 -05:00
if (0 === \count($frontPage->data)) {
$frontPage->data = $defaultSet;
Log::debug('frontpage set is empty!');
$frontPage->save();
}
$accounts = $repository->getAccountsById($frontPage->data);
2016-05-13 10:22:24 -05:00
2018-03-10 13:30:09 -06:00
return response()->json($this->accountBalanceChart($accounts, $start, $end));
2015-08-01 00:04:41 -05:00
}
2018-07-08 05:08:53 -05:00
2016-11-20 11:31:29 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows all income per account for each category.
*
2017-02-25 06:19:42 -06:00
* @param Account $account
* @param Carbon $start
* @param Carbon $end
2016-11-26 02:16:06 -06:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2016-11-20 11:31:29 -06:00
*/
2018-07-08 05:08:53 -05:00
public function incomeCategory(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-11-20 11:31:29 -06:00
{
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.income-category');
2016-11-20 11:31:29 -06:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-11-20 11:31:29 -06:00
}
// grab all journals:
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2016-11-20 11:31:29 -06:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::DEPOSIT]);
$transactions = $collector->getTransactions();
2016-11-20 11:31:29 -06:00
$result = [];
2016-12-11 10:05:48 -06:00
$chartData = [];
2016-11-20 11:31:29 -06:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
2018-04-02 08:10:40 -05:00
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
$transCatId = (int)$transaction->transaction_category_id;
2016-12-11 10:05:48 -06:00
$categoryId = max($jrnlCatId, $transCatId);
2016-11-20 11:31:29 -06:00
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
2016-12-11 10:05:48 -06:00
2016-11-20 11:31:29 -06:00
$names = $this->getCategoryNames(array_keys($result));
2016-12-11 10:05:48 -06:00
foreach ($result as $categoryId => $amount) {
$chartData[$names[$categoryId]] = $amount;
}
$data = $this->generator->pieChart($chartData);
2016-11-20 11:31:29 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-20 11:31:29 -06:00
}
2017-02-25 06:19:42 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows the income grouped by category for an account, in all time.
*
2017-02-25 06:19:42 -06:00
* @param AccountRepositoryInterface $repository
* @param Account $account
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2017-02-25 06:19:42 -06:00
*/
2018-07-08 05:08:53 -05:00
public function incomeCategoryAll(AccountRepositoryInterface $repository, Account $account): JsonResponse
2017-02-25 06:19:42 -06:00
{
2018-08-07 12:29:10 -05:00
$start = $repository->oldestJournalDate($account) ?? Carbon::now()->startOfMonth();
2017-02-25 06:19:42 -06:00
$end = Carbon::now();
return $this->incomeCategory($account, $start, $end);
}
2018-07-08 05:08:53 -05:00
2016-12-11 04:15:19 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows overview of account during a single period.
*
2016-12-11 04:15:19 -06:00
* @param Account $account
2017-06-05 04:12:50 -05:00
* @param Carbon $start
2016-12-11 04:15:19 -06:00
*
2018-03-11 10:24:07 -05:00
* @param Carbon $end
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2018-07-14 16:22:08 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-12-11 04:15:19 -06:00
*/
2018-07-08 05:08:53 -05:00
public function period(Account $account, Carbon $start, Carbon $end): JsonResponse
2016-12-11 04:15:19 -06:00
{
2018-02-09 09:47:01 -06:00
$cache = new CacheProperties;
$cache->addProperty('chart.account.period');
2016-12-11 04:15:19 -06:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($account->id);
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-12-11 04:15:19 -06:00
}
2018-07-14 16:22:08 -05:00
$step = $this->calculateStep($start, $end);
2016-12-11 04:15:19 -06:00
$chartData = [];
2018-02-09 09:47:01 -06:00
$current = clone $start;
switch ($step) {
case '1D':
$format = (string)trans('config.month_and_day');
$range = app('steam')->balanceInRange($account, $start, $end);
2018-02-09 09:47:01 -06:00
$previous = array_values($range)[0];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->formatLocalized($format);
2018-04-02 08:10:40 -05:00
$chartData[$label] = (float)$balance;
2018-02-09 09:47:01 -06:00
$previous = $balance;
$current->addDay();
}
break;
2018-03-03 07:24:06 -06:00
// @codeCoverageIgnoreStart
2018-02-09 09:47:01 -06:00
case '1W':
2018-03-03 07:24:06 -06:00
case '1M':
case '1Y':
2018-02-09 09:47:01 -06:00
while ($end >= $current) {
2018-04-02 08:10:40 -05:00
$balance = (float)app('steam')->balance($account, $current);
2018-02-09 09:47:01 -06:00
$label = app('navigation')->periodShow($current, $step);
$chartData[$label] = $balance;
2018-04-15 12:11:10 -05:00
$current = app('navigation')->addPeriod($current, $step, 0);
2018-02-09 09:47:01 -06:00
}
break;
2018-03-03 07:24:06 -06:00
// @codeCoverageIgnoreEnd
2016-12-11 04:15:19 -06:00
}
2016-12-11 10:05:48 -06:00
$data = $this->generator->singleSet($account->name, $chartData);
2016-12-11 04:15:19 -06:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-12-11 04:15:19 -06:00
}
2015-05-16 02:41:14 -05:00
/**
2016-05-13 08:53:39 -05:00
* Shows the balances for a given set of dates and accounts.
2015-05-16 02:41:14 -05:00
*
2016-05-13 08:53:39 -05:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
2015-05-16 02:41:14 -05:00
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2015-05-16 02:41:14 -05:00
*/
2018-07-08 05:08:53 -05:00
public function report(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2015-05-16 02:41:14 -05:00
{
2018-03-10 13:30:09 -06:00
return response()->json($this->accountBalanceChart($accounts, $start, $end));
2015-05-16 02:41:14 -05:00
}
2018-07-08 05:08:53 -05:00
2016-10-14 12:59:10 -05:00
/**
* Shows the balances for all the user's revenue accounts.
*
* @param AccountRepositoryInterface $repository
*
2018-07-08 05:08:53 -05:00
* @return JsonResponse
2016-10-14 12:59:10 -05:00
*/
2018-07-08 05:08:53 -05:00
public function revenueAccounts(AccountRepositoryInterface $repository): JsonResponse
2016-10-14 12:59:10 -05:00
{
2016-12-11 09:38:21 -06:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
$chartData = [];
$cache = new CacheProperties;
2016-10-14 12:59:10 -05:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.revenue-accounts');
2016-10-14 12:59:10 -05:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-10-14 12:59:10 -05:00
}
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
$start->subDay();
$startBalances = app('steam')->balancesByAccounts($accounts, $start);
$endBalances = app('steam')->balancesByAccounts($accounts, $end);
2016-10-14 12:59:10 -05:00
2016-12-11 09:38:21 -06:00
foreach ($accounts as $account) {
2016-12-11 10:05:48 -06:00
$id = $account->id;
$startBalance = $startBalances[$id] ?? '0';
$endBalance = $endBalances[$id] ?? '0';
$diff = bcsub($endBalance, $startBalance);
$diff = bcmul($diff, '-1');
2017-11-15 05:25:49 -06:00
if (0 !== bccomp($diff, '0')) {
2016-12-30 06:45:02 -06:00
$chartData[$account->name] = $diff;
2016-10-14 12:59:10 -05:00
}
2016-12-11 09:38:21 -06:00
}
2016-10-14 12:59:10 -05:00
2016-12-11 10:32:48 -06:00
arsort($chartData);
2018-04-02 08:10:40 -05:00
$data = $this->generator->singleSet((string)trans('firefly.earned'), $chartData);
2016-10-14 12:59:10 -05:00
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-10-14 12:59:10 -05:00
}
2018-07-08 05:08:53 -05:00
2016-11-16 13:59:21 -06:00
/**
2018-07-21 01:06:24 -05:00
* Shows an overview of the account balances for a set of accounts.
*
2016-12-05 23:52:17 -06:00
* @param Collection $accounts
2016-11-16 13:59:21 -06:00
* @param Carbon $start
* @param Carbon $end
*
* @return array
2018-07-14 16:22:08 -05:00
*
2018-07-15 12:17:26 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-11-16 13:59:21 -06:00
*/
protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method.
2016-11-16 13:59:21 -06:00
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.account-balance-chart');
2016-11-16 13:59:21 -06:00
$cache->addProperty($accounts);
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-11-16 13:59:21 -06:00
}
2016-12-16 01:07:31 -06:00
Log::debug('Regenerate chart.account.account-balance-chart from scratch.');
2016-11-16 13:59:21 -06:00
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
2018-07-22 10:06:10 -05:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$default = app('amount')->getDefaultCurrency();
$chartData = [];
/** @var Account $account */
2016-11-16 13:59:21 -06:00
foreach ($accounts as $account) {
2018-07-22 10:06:10 -05:00
$currency = $repository->findNull((int)$accountRepos->getMetaValue($account, 'currency_id'));
2018-04-08 09:21:17 -05:00
if (null === $currency) {
$currency = $default;
}
$currentSet = [
'label' => $account->name,
'currency_symbol' => $currency->symbol,
'entries' => [],
2016-12-11 09:02:04 -06:00
];
2018-04-08 09:17:29 -05:00
2016-12-11 09:02:04 -06:00
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end);
2016-12-30 06:45:02 -06:00
$previous = array_values($range)[0];
2016-12-11 09:02:04 -06:00
while ($currentStart <= $end) {
2016-12-11 09:25:25 -06:00
$format = $currentStart->format('Y-m-d');
2018-04-02 08:10:40 -05:00
$label = $currentStart->formatLocalized((string)trans('config.month_and_day'));
2016-12-30 06:45:02 -06:00
$balance = isset($range[$format]) ? round($range[$format], 12) : $previous;
2016-12-11 09:25:25 -06:00
$previous = $balance;
2016-12-11 09:02:04 -06:00
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
2016-11-16 13:59:21 -06:00
}
2016-12-11 09:25:25 -06:00
$chartData[] = $currentSet;
2016-11-16 13:59:21 -06:00
}
2016-12-11 10:05:48 -06:00
$data = $this->generator->multiSet($chartData);
2016-11-16 13:59:21 -06:00
$cache->store($data);
return $data;
}
/**
* @param Collection $accounts
*
* @return array
*/
private function extractNames(Collection $accounts): array
{
$return = [];
/** @var Account $account */
foreach ($accounts as $account) {
$return[$account->id] = $account->name;
}
return $return;
}
/**
* This method extracts the unique currency ID's from an array of balances.
*
* The given array is expected to be in this format:
*
* accountID1:
* currencyID1: balance
* currencyID2: balance
* accountID2:
* currencyID1: balance
* currencyID2: balance
*
*
* @param array $balances
*
* @return array
*/
private function getCurrencyIDs(array $balances): array
{
$currencies = [];
/**
* @var int $accountId
* @var array $info
*/
foreach ($balances as $accountId => $info) {
$currencies = array_merge(array_keys($info));
}
return array_unique($currencies);
}
2015-05-20 12:56:14 -05:00
}