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

174 lines
5.2 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-17 11:03:16 -05:00
use Illuminate\Support\Collection;
2015-05-16 02:41:14 -05:00
use Preferences;
use Response;
use Session;
/**
* Class AccountController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class AccountController extends Controller
{
/** @var \FireflyIII\Generator\Chart\Account\AccountChartGenerator */
protected $generator;
/**
2015-06-28 03:03:34 -05:00
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2015-07-07 12:09:45 -05:00
$this->generator = app('FireflyIII\Generator\Chart\Account\AccountChartGenerator');
}
2015-05-17 11:03:16 -05:00
/**
* Shows the balances for all the user's accounts.
*
* @param AccountRepositoryInterface $repository
*
2015-05-26 01:17:58 -05:00
* @param $year
* @param $month
* @param bool $shared
*
2015-05-17 11:03:16 -05:00
* @return \Symfony\Component\HttpFoundation\Response
*/
public function all(AccountRepositoryInterface $repository, $year, $month, $shared = false)
2015-05-17 11:03:16 -05:00
{
$start = new Carbon($year . '-' . $month . '-01');
$end = clone $start;
$end->endOfMonth();
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('all');
$cache->addProperty('accounts');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-05-17 11:03:16 -05:00
/** @var Collection $accounts */
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
if ($shared === false) {
/** @var Account $account */
foreach ($accounts as $index => $account) {
if ($account->getMeta('accountRole') == 'sharedAsset') {
$accounts->forget($index);
}
}
}
// make chart:
$data = $this->generator->all($accounts, $start, $end);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-17 11:03:16 -05:00
}
2015-08-01 00:04:41 -05:00
/**
* Shows the balances for all the user's expense accounts.
*
* @param AccountRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function expenseAccounts(AccountRepositoryInterface $repository)
{
$start = clone Session::get('start', Carbon::now()->startOfMonth());
$end = clone Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getAccounts(['Expense account', 'Beneficiary account']);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expenseAccounts');
$cache->addProperty('accounts');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$data = $this->generator->expenseAccounts($accounts, $start, $end);
$cache->store($data);
return Response::json($data);
}
2015-05-16 02:41:14 -05:00
/**
* Shows the balances for all the user's frontpage accounts.
*
* @param AccountRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 01:38:27 -05:00
public function frontpage(AccountRepositoryInterface $repository)
2015-05-16 02:41:14 -05:00
{
$frontPage = Preferences::get('frontPageAccounts', []);
$start = clone Session::get('start', Carbon::now()->startOfMonth());
$end = clone Session::get('end', Carbon::now()->endOfMonth());
2015-05-16 02:41:14 -05:00
$accounts = $repository->getFrontpageAccounts($frontPage);
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);
$cache->addProperty('frontpage');
$cache->addProperty('accounts');
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-01 11:41:18 -05:00
}
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
{
2015-06-27 01:38:27 -05:00
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('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()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-06-27 01:38:27 -05:00
$data = $this->generator->single($account, $start, $end);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
2015-05-20 12:56:14 -05:00
}