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

529 lines
18 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
/**
* AccountController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
2016-12-11 09:02:04 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2016-11-20 11:31:29 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
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;
2016-11-20 11:31:29 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2016-05-13 10:22:24 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-17 11:03:16 -05:00
use Illuminate\Support\Collection;
use Log;
use Navigation;
2016-05-13 10:22:24 -05:00
use Preferences;
use Response;
use Steam;
2015-05-16 02:41:14 -05:00
/** checked
2015-05-16 02:41:14 -05:00
* Class AccountController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class AccountController extends Controller
{
2016-12-11 10:05:48 -06:00
/** @var GeneratorInterface */
protected $generator;
/**
2016-02-04 00:27:03 -06:00
*
*/
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
/**
* @param Account $account
*
* @return \Illuminate\Http\JsonResponse
*/
public function all(Account $account)
{
$cache = new CacheProperties();
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.all');
2016-12-11 04:15:19 -06:00
$cache->addProperty($account->id);
if ($cache->has()) {
2016-12-11 09:38:21 -06:00
return Response::json($cache->get());
2016-12-11 04:15:19 -06:00
}
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$start = $repository->oldestJournalDate($account);
$end = new Carbon;
2016-12-11 10:05:48 -06:00
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$chartData = [];
2016-12-11 04:15:19 -06:00
while ($end >= $current) {
2016-12-11 09:25:25 -06:00
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->formatLocalized($format);
$chartData[$label] = $balance;
$previous = $balance;
2016-12-11 04:15:19 -06:00
$current->addDay();
}
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);
return Response::json($data);
}
2015-12-12 03:41:51 -06:00
/**
2016-05-13 08:53:39 -05:00
* Shows the balances for all the user's expense accounts.
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
*
2016-05-20 04:02:07 -05:00
* @return \Illuminate\Http\JsonResponse
2015-12-12 03:41:51 -06:00
*/
2016-10-10 00:49:39 -05:00
public function expenseAccounts(AccountRepositoryInterface $repository)
2015-12-12 03:41:51 -06:00
{
2016-05-13 10:22:24 -05:00
$start = clone session('start', Carbon::now()->startOfMonth());
$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()) {
2016-12-11 10:05:48 -06:00
return Response::json($cache->get());
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
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
2016-05-13 10:22:24 -05:00
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
2016-12-11 09:38:21 -06:00
$chartData = [];
foreach ($accounts as $account) {
$id = $account->id;
$startBalance = $startBalances[$id] ?? '0';
$endBalance = $endBalances[$id] ?? '0';
$diff = bcsub($endBalance, $startBalance);
if (bccomp($diff, '0') !== 0) {
$chartData[$account->name] = round($diff, 2);
2016-05-13 10:22:24 -05:00
}
2016-12-11 09:38:21 -06:00
}
arsort($chartData);
2016-12-11 10:05:48 -06:00
$data = $this->generator->singleSet(trans('firefly.spent'), $chartData);
2015-12-12 03:41:51 -06:00
$cache->store($data);
return Response::json($data);
2015-05-17 11:03:16 -05:00
}
2016-11-20 11:31:29 -06:00
/**
* @param JournalCollectorInterface $collector
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse
*/
2016-12-05 23:52:17 -06:00
public function expenseBudget(JournalCollectorInterface $collector, Account $account, Carbon $start, Carbon $end)
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()) {
return Response::json($cache->get());
}
2016-12-11 10:05:48 -06:00
$collector->setAccounts(new Collection([$account]))
->setRange($start, $end)
->withBudgetInformation()
->setTypes([TransactionType::WITHDRAWAL]);
2016-11-20 11:31:29 -06:00
$transactions = $collector->getJournals();
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) {
2016-12-11 10:05:48 -06:00
$jrnlBudgetId = intval($transaction->transaction_journal_budget_id);
$transBudgetId = intval($transaction->transaction_budget_id);
$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);
return Response::json($data);
}
/**
* @param JournalCollectorInterface $collector
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse
*/
2016-12-05 23:52:17 -06:00
public function expenseCategory(JournalCollectorInterface $collector, Account $account, Carbon $start, Carbon $end)
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()) {
return Response::json($cache->get());
}
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::WITHDRAWAL]);
$transactions = $collector->getJournals();
$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) {
2016-12-11 10:05:48 -06:00
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$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);
return Response::json($data);
}
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
*
2016-05-20 04:02:07 -05:00
* @return \Illuminate\Http\JsonResponse
2015-08-01 00:04:41 -05:00
*/
2016-10-10 00:49:39 -05:00
public function frontpage(AccountRepositoryInterface $repository)
2015-08-01 00:04:41 -05:00
{
2016-11-16 13:59:21 -06:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
2016-10-10 00:49:39 -05:00
$frontPage = Preferences::get('frontPageAccounts', $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray());
2016-10-10 00:25:27 -05:00
$accounts = $repository->getAccountsById($frontPage->data);
2016-05-13 10:22:24 -05:00
2016-12-05 23:52:17 -06:00
return Response::json($this->accountBalanceChart($accounts, $start, $end));
2015-08-01 00:04:41 -05:00
}
2016-11-20 11:31:29 -06:00
/**
2016-11-26 02:16:06 -06:00
* @param JournalCollectorInterface $collector
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse
2016-11-20 11:31:29 -06:00
*/
2016-12-05 23:52:17 -06:00
public function incomeCategory(JournalCollectorInterface $collector, Account $account, Carbon $start, Carbon $end)
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()) {
return Response::json($cache->get());
}
// grab all journals:
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->withCategoryInformation()->setTypes([TransactionType::DEPOSIT]);
$transactions = $collector->getJournals();
$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) {
2016-12-11 10:05:48 -06:00
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$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);
return Response::json($data);
}
2016-12-11 04:15:19 -06:00
/**
* @param Account $account
* @param string $date
*
* @return \Illuminate\Http\JsonResponse
* @throws FireflyException
*/
public function period(Account $account, string $date)
{
try {
$start = new Carbon($date);
} catch (Exception $e) {
Log::error($e->getMessage());
throw new FireflyException('"' . e($date) . '" does not seem to be a valid date. Should be in the format YYYY-MM-DD');
}
$range = Preferences::get('viewRange', '1M')->data;
$end = Navigation::endOfPeriod($start, $range);
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.period');
2016-12-11 04:15:19 -06:00
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get());
}
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$chartData = [];
while ($end >= $current) {
2016-12-11 10:05:48 -06:00
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->formatLocalized($format);
$chartData[$label] = $balance;
$previous = $balance;
2016-12-11 04:15:19 -06:00
$current->addDay();
}
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);
return Response::json($data);
}
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
*
2016-05-13 08:53:39 -05:00
* @return \Illuminate\Http\JsonResponse
2015-05-16 02:41:14 -05:00
*/
2016-12-05 23:52:17 -06:00
public function report(Collection $accounts, Carbon $start, Carbon $end)
2015-05-16 02:41:14 -05:00
{
2016-12-05 23:52:17 -06:00
return Response::json($this->accountBalanceChart($accounts, $start, $end));
2015-05-16 02:41:14 -05:00
}
2016-10-14 12:59:10 -05:00
/**
* Shows the balances for all the user's revenue accounts.
*
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*/
public function revenueAccounts(AccountRepositoryInterface $repository)
{
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()) {
2016-12-11 10:05:48 -06:00
return Response::json($cache->get());
2016-10-14 12:59:10 -05:00
}
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
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');
2016-12-11 09:38:21 -06:00
if (bccomp($diff, '0') !== 0) {
$chartData[$account->name] = round($diff, 2);
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 09:38:21 -06:00
asort($chartData);
2016-12-11 10:05:48 -06:00
$data = $this->generator->singleSet(trans('firefly.spent'), $chartData);
2016-10-14 12:59:10 -05:00
$cache->store($data);
return Response::json($data);
}
2015-05-16 02:41:14 -05:00
/**
* Shows an account's balance for a single month.
*
* @param Account $account
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 01:38:27 -05:00
public function single(Account $account)
2015-05-16 02:41:14 -05:00
{
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
2015-05-16 02:41:14 -05:00
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-11 10:05:48 -06:00
$cache->addProperty('chart.account.single');
2015-06-03 14:25:11 -05:00
$cache->addProperty($account->id);
if ($cache->has()) {
2016-05-15 11:36:40 -05:00
return Response::json($cache->get());
}
2016-05-13 10:22:24 -05:00
$format = (string)trans('config.month_and_day');
$range = Steam::balanceInRange($account, $start, $end);
$current = clone $start;
$previous = array_values($range)[0];
$chartData = [];
while ($end >= $current) {
2016-12-11 10:05:48 -06:00
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->formatLocalized($format);
$chartData[$label] = $balance;
$previous = $balance;
$current->addDay();
}
2016-12-11 10:05:48 -06:00
$data = $this->generator->singleSet($account->name, $chartData);
$cache->store($data);
return Response::json($data);
}
2016-11-16 13:59:21 -06:00
/**
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
*/
2016-12-05 23:52:17 -06:00
private function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array
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()) {
return $cache->get();
}
2016-12-11 09:25:25 -06:00
$chartData = [];
2016-11-16 13:59:21 -06:00
foreach ($accounts as $account) {
2016-12-11 09:02:04 -06:00
$currentSet = [
'label' => $account->name,
'entries' => [],
];
$currentStart = clone $start;
$range = Steam::balanceInRange($account, $start, clone $end);
$previous = round(array_values($range)[0], 2);
while ($currentStart <= $end) {
2016-12-11 09:25:25 -06:00
$format = $currentStart->format('Y-m-d');
$label = $currentStart->formatLocalized(strval(trans('config.month_and_day')));
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
$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;
}
2016-11-20 11:31:29 -06:00
/**
* @param array $budgetIds
*
* @return array
*/
private function getBudgetNames(array $budgetIds): array
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
$grouped = $budgets->groupBy('id')->toArray();
$return = [];
foreach ($budgetIds as $budgetId) {
if (isset($grouped[$budgetId])) {
$return[$budgetId] = $grouped[$budgetId][0]['name'];
}
}
$return[0] = trans('firefly.no_budget');
return $return;
}
/**
* Small helper function for some of the charts.
*
* @param array $categoryIds
*
* @return array
*/
private function getCategoryNames(array $categoryIds): array
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$grouped = $categories->groupBy('id')->toArray();
$return = [];
foreach ($categoryIds as $categoryId) {
if (isset($grouped[$categoryId])) {
$return[$categoryId] = $grouped[$categoryId][0]['name'];
}
}
$return[0] = trans('firefly.noCategory');
return $return;
}
2015-05-20 12:56:14 -05:00
}