diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index eba8d2a3cd..f128d61bdd 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -116,7 +116,14 @@ class AccountController extends Controller $subTitle = Config::get('firefly.subTitlesByIdentifier.' . $what); $subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what); $types = Config::get('firefly.accountTypesByIdentifier.' . $what); - $accounts = Auth::user()->accounts()->accountTypeIn($types)->get(['accounts.*']); + + // move to repository: + $accounts = Auth::user()->accounts()->with( + ['accountmeta' => function ($query) { + $query->where('name', 'accountRole'); + }] + )->accountTypeIn($types)->get(['accounts.*']); + return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts')); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index e6607bd2f0..2faee4c964 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -46,7 +46,7 @@ class AccountRepository implements AccountRepositoryInterface */ public function getJournals(Account $account, $page, $range = 'session') { - $offset = ($page-1) * 50; + $offset = ($page - 1) * 50; $query = Auth::user() ->transactionJournals() ->withRelevantData() @@ -69,6 +69,23 @@ class AccountRepository implements AccountRepositoryInterface } + /** + * @param Account $account + * + * @return float + */ + public function leftOnAccount(Account $account) + { + $balance = \Steam::balance($account); + /** @var PiggyBank $p */ + foreach ($account->piggybanks()->get() as $p) { + $balance -= $p->currentRelevantRep()->currentamount; + } + + return $balance; + + } + /** * @param Account $account * @@ -123,13 +140,7 @@ class AccountRepository implements AccountRepositoryInterface $account->save(); // update meta data: - /** @var AccountMeta $meta */ - foreach ($account->accountMeta()->get() as $meta) { - if ($meta->name == 'accountRole') { - $meta->data = $data['accountRole']; - $meta->save(); - } - } + $this->_updateMetadata($account, $data); $openingBalance = $this->openingBalanceTransaction($account); @@ -313,18 +324,35 @@ class AccountRepository implements AccountRepositoryInterface /** * @param Account $account - * - * @return float + * @param array $data */ - public function leftOnAccount(Account $account) + protected function _updateMetadata(Account $account, array $data) { - $balance = \Steam::balance($account); - /** @var PiggyBank $p */ - foreach ($account->piggybanks()->get() as $p) { - $balance -= $p->currentRelevantRep()->currentamount; + $metaEntries = $account->accountMeta()->get(); + $updated = false; + + /** @var AccountMeta $entry */ + foreach ($metaEntries as $entry) { + if ($entry->name == 'accountRole') { + $entry->data = $data['accountRole']; + $updated = true; + $entry->save(); + } } - return $balance; + if ($updated === false) { + $metaData = new AccountMeta( + [ + 'account_id' => $account->id, + 'name' => 'accountRole', + 'data' => $data['accountRole'] + ] + ); + if (!$metaData->isValid()) { + App::abort(500); + } + $metaData->save(); + } } } \ No newline at end of file diff --git a/resources/views/list/accounts.blade.php b/resources/views/list/accounts.blade.php index d1258ec14b..01013341a5 100644 --- a/resources/views/list/accounts.blade.php +++ b/resources/views/list/accounts.blade.php @@ -19,7 +19,13 @@ {{{$account->name}}} @if(isset($what) && $what == 'asset') - {{{$account->accountRole}}} + + @foreach($account->accountmeta as $entry) + @if($entry->name == 'accountRole') + {{Config::get('firefly.accountRoles.'.$entry->data)}} + @endif + @endforeach + @endif {!! Amount::format(Steam::balance($account)) !!}