2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* AccountController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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-05-20 12:41:23 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-05-16 09:41:14 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-05-20 15:02:52 +02:00
|
|
|
use Exception;
|
2016-05-20 11:02:07 +02:00
|
|
|
use FireflyIII\Crud\Account\AccountCrudInterface;
|
2016-05-20 15:02:52 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Account;
|
2016-05-13 17:22:24 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Support\CacheProperties;
|
2015-05-17 18:03:16 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2016-05-20 15:02:52 +02:00
|
|
|
use Log;
|
|
|
|
use Navigation;
|
2016-05-13 17:22:24 +02:00
|
|
|
use Preferences;
|
|
|
|
use Response;
|
|
|
|
use Steam;
|
2015-05-16 09:41:14 +02:00
|
|
|
|
2016-05-05 21:25:20 +02:00
|
|
|
/** checked
|
2015-05-16 09:41:14 +02:00
|
|
|
* Class AccountController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Chart
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2015-06-27 08:18:47 +02:00
|
|
|
|
2016-01-28 21:50:20 +01:00
|
|
|
/** @var \FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface */
|
2015-06-27 08:18:47 +02:00
|
|
|
protected $generator;
|
|
|
|
|
|
|
|
/**
|
2016-02-04 07:27:03 +01:00
|
|
|
*
|
2015-06-27 08:18:47 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
// create chart generator:
|
2016-05-02 20:49:19 +02:00
|
|
|
$this->generator = app(AccountChartGeneratorInterface::class);
|
2015-06-27 08:18:47 +02:00
|
|
|
}
|
|
|
|
|
2015-12-12 10:41:51 +01:00
|
|
|
/**
|
2016-05-13 15:53:39 +02:00
|
|
|
* Shows the balances for all the user's expense accounts.
|
2015-12-28 07:38:02 +01:00
|
|
|
*
|
2016-05-20 11:02:07 +02:00
|
|
|
* @param AccountCrudInterface $crud
|
2015-12-12 10:41:51 +01:00
|
|
|
*
|
2016-05-20 11:02:07 +02:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-12-12 10:41:51 +01:00
|
|
|
*/
|
2016-05-20 11:02:07 +02:00
|
|
|
public function expenseAccounts(AccountCrudInterface $crud)
|
2015-12-12 10:41:51 +01:00
|
|
|
{
|
2016-05-13 17:22:24 +02:00
|
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
|
|
|
$cache = new CacheProperties;
|
2015-12-12 10:41:51 +01:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-05-13 15:53:39 +02:00
|
|
|
$cache->addProperty('expenseAccounts');
|
2015-12-12 10:41:51 +01:00
|
|
|
$cache->addProperty('accounts');
|
|
|
|
if ($cache->has()) {
|
2016-05-15 18:36:40 +02:00
|
|
|
return Response::json($cache->get());
|
2015-12-12 10:41:51 +01:00
|
|
|
}
|
2016-05-20 11:02:07 +02:00
|
|
|
$accounts = $crud->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
|
2016-05-13 17:22:24 +02:00
|
|
|
|
|
|
|
$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 10:41:51 +01:00
|
|
|
|
2016-05-13 15:53:39 +02:00
|
|
|
$data = $this->generator->expenseAccounts($accounts, $start, $end);
|
2015-12-12 10:41:51 +01:00
|
|
|
$cache->store($data);
|
2015-06-02 17:44:50 +02:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-17 18:03:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 07:04:41 +02:00
|
|
|
/**
|
2016-05-13 15:53:39 +02:00
|
|
|
* Shows the balances for all the user's frontpage accounts.
|
2015-08-01 07:04:41 +02:00
|
|
|
*
|
2016-05-20 11:02:07 +02:00
|
|
|
* @param AccountCrudInterface $crud
|
2015-08-01 07:04:41 +02:00
|
|
|
*
|
2016-05-20 11:02:07 +02:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-08-01 07:04:41 +02:00
|
|
|
*/
|
2016-05-20 11:02:07 +02:00
|
|
|
public function frontpage(AccountCrudInterface $crud)
|
2015-08-01 07:04:41 +02:00
|
|
|
{
|
2016-05-13 17:22:24 +02:00
|
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
|
|
|
|
2015-08-01 07:04:41 +02:00
|
|
|
|
|
|
|
// chart properties for cache:
|
2016-05-13 17:22:24 +02:00
|
|
|
$cache = new CacheProperties;
|
2015-08-01 07:04:41 +02:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-05-13 15:53:39 +02:00
|
|
|
$cache->addProperty('frontpage');
|
2015-08-01 07:04:41 +02:00
|
|
|
$cache->addProperty('accounts');
|
|
|
|
if ($cache->has()) {
|
2016-05-15 18:36:40 +02:00
|
|
|
return Response::json($cache->get());
|
2015-08-01 07:04:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 11:02:07 +02:00
|
|
|
$frontPage = Preferences::get('frontPageAccounts', $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray());
|
|
|
|
$accounts = $crud->getAccountsById($frontPage->data);
|
2016-05-13 17:22:24 +02: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;
|
|
|
|
}
|
2016-05-13 15:53:39 +02:00
|
|
|
$data = $this->generator->frontpage($accounts, $start, $end);
|
2015-08-01 07:04:41 +02:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
}
|
|
|
|
|
2015-05-16 09:41:14 +02:00
|
|
|
/**
|
2016-05-13 15:53:39 +02:00
|
|
|
* Shows the balances for a given set of dates and accounts.
|
2015-05-16 09:41:14 +02:00
|
|
|
*
|
2016-05-13 15:53:39 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-05-16 09:41:14 +02:00
|
|
|
*
|
2016-05-13 15:53:39 +02:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-05-16 09:41:14 +02:00
|
|
|
*/
|
2016-05-13 17:22:24 +02:00
|
|
|
public function report(Carbon $start, Carbon $end, Collection $accounts)
|
2015-05-16 09:41:14 +02:00
|
|
|
{
|
2015-06-01 18:41:18 +02:00
|
|
|
// chart properties for cache:
|
2015-06-03 21:25:11 +02:00
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-05-13 15:53:39 +02:00
|
|
|
$cache->addProperty('all');
|
2015-06-03 21:25:11 +02:00
|
|
|
$cache->addProperty('accounts');
|
2016-05-13 15:53:39 +02:00
|
|
|
$cache->addProperty('default');
|
|
|
|
$cache->addProperty($accounts);
|
2015-06-03 21:25:11 +02:00
|
|
|
if ($cache->has()) {
|
2016-05-15 18:36:40 +02:00
|
|
|
return Response::json($cache->get());
|
2016-05-13 17:22:24 +02: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 18:41:18 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 15:53:39 +02:00
|
|
|
// make chart:
|
2015-06-27 08:38:27 +02:00
|
|
|
$data = $this->generator->frontpage($accounts, $start, $end);
|
2015-06-03 21:25:11 +02:00
|
|
|
$cache->store($data);
|
2015-06-01 18:41:18 +02:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an account's balance for a single month.
|
|
|
|
*
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2015-06-27 08:38:27 +02:00
|
|
|
public function single(Account $account)
|
2015-05-16 09:41:14 +02:00
|
|
|
{
|
2016-05-06 10:32:26 +02:00
|
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
2015-05-16 09:41:14 +02:00
|
|
|
|
2015-06-02 17:44:50 +02:00
|
|
|
// chart properties for cache:
|
2015-06-03 21:25:11 +02:00
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('frontpage');
|
|
|
|
$cache->addProperty('single');
|
|
|
|
$cache->addProperty($account->id);
|
|
|
|
if ($cache->has()) {
|
2016-05-15 18:36:40 +02:00
|
|
|
return Response::json($cache->get());
|
2015-06-02 17:44:50 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 17:22:24 +02: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 21:25:11 +02:00
|
|
|
$cache->store($data);
|
2015-06-02 17:44:50 +02:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
2016-05-13 17:22:24 +02:00
|
|
|
|
2016-05-20 15:02:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param string $date
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function specificPeriod(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);
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('frontpage');
|
|
|
|
$cache->addProperty('specificPeriod');
|
|
|
|
$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];
|
|
|
|
$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);
|
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
}
|
|
|
|
|
2015-05-20 19:56:14 +02:00
|
|
|
}
|