mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-19 16:10:00 +00:00
More refactoring
This commit is contained in:
@@ -7,8 +7,13 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Support\Collection;
|
||||
use Preferences;
|
||||
use Response;
|
||||
use Steam;
|
||||
|
||||
/** checked
|
||||
* Class AccountController
|
||||
@@ -40,26 +45,44 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function expenseAccounts(ARI $repository)
|
||||
{
|
||||
/*
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
$accounts = $repository->getAccounts(['Expense account', 'Beneficiary account']);
|
||||
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty('expenseAccounts');
|
||||
$cache->addProperty('accounts');
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get());
|
||||
// return Response::json($cache->get());
|
||||
}
|
||||
$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;
|
||||
}
|
||||
);
|
||||
|
||||
$data = $this->generator->expenseAccounts($accounts, $start, $end);
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,42 +94,54 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function frontpage(ARI $repository)
|
||||
{
|
||||
/*
|
||||
$frontPage = Preferences::get('frontPageAccounts', []);
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
$accounts = $repository->getFrontpageAccounts($frontPage);
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
|
||||
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty('frontpage');
|
||||
$cache->addProperty('accounts');
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get());
|
||||
// return Response::json($cache->get());
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
$data = $this->generator->frontpage($accounts, $start, $end);
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the balances for a given set of dates and accounts.
|
||||
*
|
||||
* @param $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function report(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
public function report(Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
/*
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($start);
|
||||
@@ -114,10 +149,24 @@ class AccountController extends Controller
|
||||
$cache->addProperty('all');
|
||||
$cache->addProperty('accounts');
|
||||
$cache->addProperty('default');
|
||||
$cache->addProperty($reportType);
|
||||
$cache->addProperty($accounts);
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get());
|
||||
// return Response::json($cache->get());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// make chart:
|
||||
@@ -125,7 +174,6 @@ class AccountController extends Controller
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,8 +185,6 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function single(Account $account)
|
||||
{
|
||||
/*
|
||||
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
|
||||
@@ -150,13 +196,31 @@ class AccountController extends Controller
|
||||
$cache->addProperty('single');
|
||||
$cache->addProperty($account->id);
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get());
|
||||
// return Response::json($cache->get());
|
||||
}
|
||||
|
||||
$data = $this->generator->single($account, $start, $end);
|
||||
$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);
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user