mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
More refactoring
This commit is contained in:
parent
3e36a29c23
commit
5166171e5d
@ -42,10 +42,10 @@ interface AccountChartGeneratorInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Account $account
|
* @param Account $account
|
||||||
* @param Carbon $start
|
* @param array $labels
|
||||||
* @param Carbon $end
|
* @param array $dataSet
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function single(Account $account, Carbon $start, Carbon $end): array;
|
public function single(Account $account, array $labels, array $dataSet): array;
|
||||||
}
|
}
|
||||||
|
@ -29,28 +29,6 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
|
|||||||
'labels' => [], 'datasets' => [[
|
'labels' => [], 'datasets' => [[
|
||||||
'label' => trans('firefly.spent'),
|
'label' => trans('firefly.spent'),
|
||||||
'data' => []]]];
|
'data' => []]]];
|
||||||
|
|
||||||
$start->subDay();
|
|
||||||
$ids = $this->getIdsFromCollection($accounts);
|
|
||||||
$startBalances = Steam::balancesById($ids, $start);
|
|
||||||
$endBalances = Steam::balancesById($ids, $end);
|
|
||||||
|
|
||||||
$accounts->each(
|
|
||||||
function (Account $account) use ($startBalances, $endBalances) {
|
|
||||||
$id = $account->id;
|
|
||||||
$startBalance = $this->isInArray($startBalances, $id);
|
|
||||||
$endBalance = $this->isInArray($endBalances, $id);
|
|
||||||
$diff = bcsub($endBalance, $startBalance);
|
|
||||||
$account->difference = round($diff, 2);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$accounts = $accounts->sortByDesc(
|
|
||||||
function (Account $account) {
|
|
||||||
return $account->difference;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
if ($account->difference > 0) {
|
if ($account->difference > 0) {
|
||||||
$data['labels'][] = $account->name;
|
$data['labels'][] = $account->name;
|
||||||
@ -80,7 +58,7 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$set = [
|
$data['datasets'][] = [
|
||||||
'label' => $account->name,
|
'label' => $account->name,
|
||||||
'fillColor' => 'rgba(220,220,220,0.2)',
|
'fillColor' => 'rgba(220,220,220,0.2)',
|
||||||
'strokeColor' => 'rgba(220,220,220,1)',
|
'strokeColor' => 'rgba(220,220,220,1)',
|
||||||
@ -88,20 +66,8 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
|
|||||||
'pointStrokeColor' => '#fff',
|
'pointStrokeColor' => '#fff',
|
||||||
'pointHighlightFill' => '#fff',
|
'pointHighlightFill' => '#fff',
|
||||||
'pointHighlightStroke' => 'rgba(220,220,220,1)',
|
'pointHighlightStroke' => 'rgba(220,220,220,1)',
|
||||||
'data' => [],
|
'data' => $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;
|
|
||||||
|
|
||||||
$set['data'][] = $balance;
|
|
||||||
$previous = $balance;
|
|
||||||
$current->addDay();
|
|
||||||
}
|
|
||||||
$data['datasets'][] = $set;
|
|
||||||
}
|
}
|
||||||
$data['count'] = count($data['datasets']);
|
$data['count'] = count($data['datasets']);
|
||||||
|
|
||||||
@ -110,71 +76,27 @@ class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Account $account
|
* @param Account $account
|
||||||
* @param Carbon $start
|
* @param array $labels
|
||||||
* @param Carbon $end
|
* @param array $dataSet
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function single(Account $account, Carbon $start, Carbon $end): array
|
public function single(Account $account, array $labels, array $dataSet): array
|
||||||
{
|
{
|
||||||
// language:
|
// language:
|
||||||
$format = (string)trans('config.month_and_day');
|
$format = (string)trans('config.month_and_day');
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'count' => 1,
|
'count' => 1,
|
||||||
'labels' => [],
|
'labels' => $labels,
|
||||||
'datasets' => [
|
'datasets' => [
|
||||||
[
|
[
|
||||||
'label' => $account->name,
|
'label' => $account->name,
|
||||||
'data' => [],
|
'data' => $dataSet,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
$range = Steam::balanceInRange($account, $start, $end);
|
|
||||||
$current = clone $start;
|
|
||||||
$previous = array_values($range)[0];
|
|
||||||
|
|
||||||
while ($end >= $current) {
|
|
||||||
$theDate = $current->format('Y-m-d');
|
|
||||||
$balance = $range[$theDate] ?? $previous;
|
|
||||||
|
|
||||||
$data['labels'][] = $current->formatLocalized($format);
|
|
||||||
$data['datasets'][0]['data'][] = $balance;
|
|
||||||
$previous = $balance;
|
|
||||||
$current->addDay();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $collection
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function getIdsFromCollection(Collection $collection): array
|
|
||||||
{
|
|
||||||
$ids = [];
|
|
||||||
foreach ($collection as $entry) {
|
|
||||||
$ids[] = $entry->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_unique($ids);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $array
|
|
||||||
* @param $entryId
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function isInArray($array, $entryId): string
|
|
||||||
{
|
|
||||||
if (isset($array[$entryId])) {
|
|
||||||
return $array[$entryId];
|
|
||||||
}
|
|
||||||
|
|
||||||
return '0';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,13 @@ use Carbon\Carbon;
|
|||||||
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||||
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Preferences;
|
||||||
|
use Response;
|
||||||
|
use Steam;
|
||||||
|
|
||||||
/** checked
|
/** checked
|
||||||
* Class AccountController
|
* Class AccountController
|
||||||
@ -40,26 +45,44 @@ class AccountController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function expenseAccounts(ARI $repository)
|
public function expenseAccounts(ARI $repository)
|
||||||
{
|
{
|
||||||
/*
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
$cache = new CacheProperties;
|
||||||
$accounts = $repository->getAccounts(['Expense account', 'Beneficiary account']);
|
|
||||||
|
|
||||||
// chart properties for cache:
|
|
||||||
$cache = new CacheProperties();
|
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
$cache->addProperty('expenseAccounts');
|
$cache->addProperty('expenseAccounts');
|
||||||
$cache->addProperty('accounts');
|
$cache->addProperty('accounts');
|
||||||
if ($cache->has()) {
|
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);
|
$data = $this->generator->expenseAccounts($accounts, $start, $end);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,42 +94,54 @@ class AccountController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function frontpage(ARI $repository)
|
public function frontpage(ARI $repository)
|
||||||
{
|
{
|
||||||
/*
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||||
$frontPage = Preferences::get('frontPageAccounts', []);
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
||||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
|
||||||
$accounts = $repository->getFrontpageAccounts($frontPage);
|
|
||||||
|
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
$cache->addProperty('frontpage');
|
$cache->addProperty('frontpage');
|
||||||
$cache->addProperty('accounts');
|
$cache->addProperty('accounts');
|
||||||
if ($cache->has()) {
|
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);
|
$data = $this->generator->frontpage($accounts, $start, $end);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the balances for a given set of dates and accounts.
|
* Shows the balances for a given set of dates and accounts.
|
||||||
*
|
*
|
||||||
* @param $reportType
|
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @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:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
@ -114,10 +149,24 @@ class AccountController extends Controller
|
|||||||
$cache->addProperty('all');
|
$cache->addProperty('all');
|
||||||
$cache->addProperty('accounts');
|
$cache->addProperty('accounts');
|
||||||
$cache->addProperty('default');
|
$cache->addProperty('default');
|
||||||
$cache->addProperty($reportType);
|
|
||||||
$cache->addProperty($accounts);
|
$cache->addProperty($accounts);
|
||||||
if ($cache->has()) {
|
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:
|
// make chart:
|
||||||
@ -125,7 +174,6 @@ class AccountController extends Controller
|
|||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,8 +185,6 @@ class AccountController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function single(Account $account)
|
public function single(Account $account)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
|
|
||||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||||
|
|
||||||
@ -150,13 +196,31 @@ class AccountController extends Controller
|
|||||||
$cache->addProperty('single');
|
$cache->addProperty('single');
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
if ($cache->has()) {
|
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);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
<?php namespace FireflyIII\Http\Controllers;
|
<?php namespace FireflyIII\Http\Controllers;
|
||||||
|
|
||||||
use Amount;
|
|
||||||
use Artisan;
|
use Artisan;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\Tag;
|
use FireflyIII\Models\Tag;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||||
@ -111,15 +111,17 @@ class HomeController extends Controller
|
|||||||
$subTitle = trans('firefly.welcomeBack');
|
$subTitle = trans('firefly.welcomeBack');
|
||||||
$mainTitleIcon = 'fa-fire';
|
$mainTitleIcon = 'fa-fire';
|
||||||
$transactions = [];
|
$transactions = [];
|
||||||
$frontPage = Preferences::get('frontPageAccounts', []);
|
$frontPage = Preferences::get(
|
||||||
|
'frontPageAccounts', $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray()
|
||||||
|
);
|
||||||
/** @var Carbon $start */
|
/** @var Carbon $start */
|
||||||
$start = session('start', Carbon::now()->startOfMonth());
|
$start = session('start', Carbon::now()->startOfMonth());
|
||||||
/** @var Carbon $end */
|
/** @var Carbon $end */
|
||||||
$end = session('end', Carbon::now()->endOfMonth());
|
$end = session('end', Carbon::now()->endOfMonth());
|
||||||
$showTour = Preferences::get('tour', true)->data;
|
$showTour = Preferences::get('tour', true)->data;
|
||||||
$accounts = $repository->getAccountsById($frontPage->data);
|
$accounts = $repository->getAccountsById($frontPage->data);
|
||||||
$savings = $repository->getSavingsAccounts();
|
$savings = $repository->getSavingsAccounts($start, $end);
|
||||||
$piggyBankAccounts = $repository->getPiggyBankAccounts();
|
$piggyBankAccounts = $repository->getPiggyBankAccounts($start, $end);
|
||||||
|
|
||||||
|
|
||||||
$savingsTotal = 0;
|
$savingsTotal = 0;
|
||||||
|
@ -191,7 +191,7 @@ Route::group(
|
|||||||
// accounts:
|
// accounts:
|
||||||
Route::get('/chart/account/frontpage', ['uses' => 'Chart\AccountController@frontpage']);
|
Route::get('/chart/account/frontpage', ['uses' => 'Chart\AccountController@frontpage']);
|
||||||
Route::get('/chart/account/expense', ['uses' => 'Chart\AccountController@expenseAccounts']);
|
Route::get('/chart/account/expense', ['uses' => 'Chart\AccountController@expenseAccounts']);
|
||||||
Route::get('/chart/account/report/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\AccountController@report']);
|
Route::get('/chart/account/report/default/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\AccountController@report']);
|
||||||
Route::get('/chart/account/{account}', ['uses' => 'Chart\AccountController@single']);
|
Route::get('/chart/account/{account}', ['uses' => 'Chart\AccountController@single']);
|
||||||
|
|
||||||
|
|
||||||
|
@ -216,9 +216,12 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
/**
|
/**
|
||||||
* Get the accounts of a user that have piggy banks connected to them.
|
* Get the accounts of a user that have piggy banks connected to them.
|
||||||
*
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getPiggyBankAccounts(): Collection
|
public function getPiggyBankAccounts(Carbon $start, Carbon $end): Collection
|
||||||
{
|
{
|
||||||
$collection = new Collection(DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id']));
|
$collection = new Collection(DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id']));
|
||||||
$accountIds = $collection->pluck('account_id')->toArray();
|
$accountIds = $collection->pluck('account_id')->toArray();
|
||||||
@ -228,16 +231,39 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
$accounts = $this->user->accounts()->whereIn('id', $accountIds)->where('accounts.active', 1)->get();
|
$accounts = $this->user->accounts()->whereIn('id', $accountIds)->where('accounts.active', 1)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$accounts->each(
|
||||||
|
function (Account $account) use ($start, $end) {
|
||||||
|
$account->startBalance = Steam::balanceIgnoreVirtual($account, $start);
|
||||||
|
$account->endBalance = Steam::balanceIgnoreVirtual($account, $end);
|
||||||
|
$account->piggyBalance = 0;
|
||||||
|
/** @var PiggyBank $piggyBank */
|
||||||
|
foreach ($account->piggyBanks as $piggyBank) {
|
||||||
|
$account->piggyBalance += $piggyBank->currentRelevantRep()->currentamount;
|
||||||
|
}
|
||||||
|
// sum of piggy bank amounts on this account:
|
||||||
|
// diff between endBalance and piggyBalance.
|
||||||
|
// then, percentage.
|
||||||
|
$difference = bcsub($account->endBalance, $account->piggyBalance);
|
||||||
|
$account->difference = $difference;
|
||||||
|
$account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
return $accounts;
|
return $accounts;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get savings accounts and the balance difference in the period.
|
* Get savings accounts.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getSavingsAccounts(): Collection
|
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection
|
||||||
{
|
{
|
||||||
$accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
|
$accounts = $this->user->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
|
||||||
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
||||||
@ -246,6 +272,32 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
->where('account_meta.data', '"savingAsset"')
|
->where('account_meta.data', '"savingAsset"')
|
||||||
->get(['accounts.*']);
|
->get(['accounts.*']);
|
||||||
|
|
||||||
|
$accounts->each(
|
||||||
|
function (Account $account) use ($start, $end) {
|
||||||
|
$account->startBalance = Steam::balance($account, $start);
|
||||||
|
$account->endBalance = Steam::balance($account, $end);
|
||||||
|
|
||||||
|
// diff (negative when lost, positive when gained)
|
||||||
|
$diff = bcsub($account->endBalance, $account->startBalance);
|
||||||
|
|
||||||
|
if ($diff < 0 && $account->startBalance > 0) {
|
||||||
|
// percentage lost compared to start.
|
||||||
|
$pct = (($diff * -1) / $account->startBalance) * 100;
|
||||||
|
} else {
|
||||||
|
if ($diff >= 0 && $account->startBalance > 0) {
|
||||||
|
$pct = ($diff / $account->startBalance) * 100;
|
||||||
|
} else {
|
||||||
|
$pct = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$pct = $pct > 100 ? 100 : $pct;
|
||||||
|
$account->difference = $diff;
|
||||||
|
$account->percentage = round($pct);
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
return $accounts;
|
return $accounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,16 +91,22 @@ interface AccountRepositoryInterface
|
|||||||
/**
|
/**
|
||||||
* Get the accounts of a user that have piggy banks connected to them.
|
* Get the accounts of a user that have piggy banks connected to them.
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @param Carbon $start
|
||||||
*/
|
* @param Carbon $end
|
||||||
public function getPiggyBankAccounts(): Collection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get savings accounts and the balance difference in the period.
|
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getSavingsAccounts() : Collection;
|
public function getPiggyBankAccounts(Carbon $start, Carbon $end): Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get savings accounts.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getSavingsAccounts(Carbon $start, Carbon $end): Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
|
Loading…
Reference in New Issue
Block a user