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

161 lines
4.6 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;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
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;
/**
* 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:
$this->generator = app('FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface');
}
2015-12-12 03:41:51 -06:00
/**
* Shows the balances for a given set of dates and accounts.
2015-12-28 00:38:02 -06:00
*
2015-12-28 13:04:54 -06:00
* @param $reportType
2015-12-28 00:31:48 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
2015-12-12 03:41:51 -06:00
*
2015-12-28 00:31:48 -06:00
* @return \Illuminate\Http\JsonResponse
2015-12-12 03:41:51 -06:00
*/
2016-02-05 02:25:15 -06:00
public function report(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
2015-12-12 03:41:51 -06:00
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('all');
$cache->addProperty('accounts');
$cache->addProperty('default');
2016-01-01 14:49:27 -06:00
$cache->addProperty($reportType);
2015-12-12 10:51:07 -06:00
$cache->addProperty($accounts);
2015-12-12 03:41:51 -06:00
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
// make chart:
2015-12-27 02:35:24 -06:00
$data = $this->generator->frontpage($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
/**
* Shows the balances for all the user's expense accounts.
*
* @param ARI $repository
2015-08-01 00:04:41 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function expenseAccounts(ARI $repository)
2015-08-01 00:04:41 -05:00
{
2016-02-04 00:27:03 -06:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('end', Carbon::now()->endOfMonth());
2015-08-01 00:04:41 -05:00
$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 ARI $repository
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function frontpage(ARI $repository)
2015-05-16 02:41:14 -05:00
{
$frontPage = Preferences::get('frontPageAccounts', []);
2016-02-04 00:27:03 -06:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = clone session('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
2016-02-04 00:27:03 -06:00
$start = session('start', Carbon::now()->startOfMonth());
$end = 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()) {
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
}