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

227 lines
7.1 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
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;
2016-05-02 13:49:19 -05:00
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
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;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
2016-05-13 10:22:24 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-17 11:03:16 -05:00
use Illuminate\Support\Collection;
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
{
/** @var \FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface */
protected $generator;
/**
2016-02-04 00:27:03 -06:00
*
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-05-02 13:49:19 -05:00
$this->generator = app(AccountChartGeneratorInterface::class);
}
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-05-13 08:53:39 -05:00
* @param ARI $repository
2015-12-12 03:41:51 -06:00
*
2016-05-13 08:53:39 -05:00
* @return \Symfony\Component\HttpFoundation\Response
2015-12-12 03:41:51 -06:00
*/
2016-05-13 08:53:39 -05:00
public function expenseAccounts(ARI $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-05-13 08:53:39 -05:00
$cache->addProperty('expenseAccounts');
2015-12-12 03:41:51 -06:00
$cache->addProperty('accounts');
if ($cache->has()) {
return Response::json($cache->get());
2015-12-12 03:41:51 -06:00
}
2016-05-13 10:22:24 -05:00
$accounts = $repository->getAccountsByType(['Expense account', 'Beneficiary account']);
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
$accounts->each(
function (Account $account) use ($startBalances, $endBalances) {
$id = $account->id;
$startBalance = $startBalances[$id] ?? '0';
$endBalance = $endBalances[$id] ?? '0';
$diff = bcsub($endBalance, $startBalance);
$account->difference = round($diff, 2);
}
);
$accounts = $accounts->sortByDesc(
function (Account $account) {
return $account->difference;
}
);
2015-12-12 03:41:51 -06:00
2016-05-13 08:53:39 -05:00
$data = $this->generator->expenseAccounts($accounts, $start, $end);
2015-12-12 03:41:51 -06:00
$cache->store($data);
return Response::json($data);
2015-05-17 11:03:16 -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
*
* @param ARI $repository
2015-08-01 00:04:41 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2016-05-13 08:53:39 -05:00
public function frontpage(ARI $repository)
2015-08-01 00:04:41 -05:00
{
2016-05-13 10:22:24 -05:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
2015-08-01 00:04:41 -05:00
// chart properties for cache:
2016-05-13 10:22:24 -05:00
$cache = new CacheProperties;
2015-08-01 00:04:41 -05:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-05-13 08:53:39 -05:00
$cache->addProperty('frontpage');
2015-08-01 00:04:41 -05:00
$cache->addProperty('accounts');
if ($cache->has()) {
return Response::json($cache->get());
2015-08-01 00:04:41 -05:00
}
2016-05-13 10:22:24 -05:00
$frontPage = Preferences::get('frontPageAccounts', $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray());
$accounts = $repository->getAccountsById($frontPage->data);
foreach ($accounts as $account) {
$balances = [];
$current = clone $start;
$range = Steam::balanceInRange($account, $start, clone $end);
$previous = round(array_values($range)[0], 2);
while ($current <= $end) {
$format = $current->format('Y-m-d');
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
$previous = $balance;
$balances[] = $balance;
$current->addDay();
}
$account->balances = $balances;
}
2016-05-13 08:53:39 -05:00
$data = $this->generator->frontpage($accounts, $start, $end);
2015-08-01 00:04:41 -05: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-05-13 10:22:24 -05:00
public function report(Carbon $start, Carbon $end, Collection $accounts)
2015-05-16 02:41:14 -05:00
{
2015-06-01 11:41:18 -05:00
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2016-05-13 08:53:39 -05:00
$cache->addProperty('all');
2015-06-03 14:25:11 -05:00
$cache->addProperty('accounts');
2016-05-13 08:53:39 -05:00
$cache->addProperty('default');
$cache->addProperty($accounts);
2015-06-03 14:25:11 -05:00
if ($cache->has()) {
return Response::json($cache->get());
2016-05-13 10:22:24 -05:00
}
foreach ($accounts as $account) {
$balances = [];
$current = clone $start;
$range = Steam::balanceInRange($account, $start, clone $end);
$previous = round(array_values($range)[0], 2);
while ($current <= $end) {
$format = $current->format('Y-m-d');
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
$previous = $balance;
$balances[] = $balance;
$current->addDay();
}
$account->balances = $balances;
2015-06-01 11:41:18 -05:00
}
2016-05-13 08:53:39 -05:00
// make chart:
2015-06-27 01:38:27 -05:00
$data = $this->generator->frontpage($accounts, $start, $end);
2015-06-03 14:25:11 -05:00
$cache->store($data);
2015-06-01 11:41:18 -05:00
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);
$cache->addProperty('frontpage');
$cache->addProperty('single');
$cache->addProperty($account->id);
if ($cache->has()) {
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];
$labels = [];
$chartData = [];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$labels[] = $current->formatLocalized($format);
$chartData[] = $balance;
$previous = $balance;
$current->addDay();
}
$data = $this->generator->single($account, $labels, $chartData);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
2016-05-13 10:22:24 -05:00
2015-05-20 12:56:14 -05:00
}