diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php index e95c7de9c6..f82178e241 100644 --- a/app/Http/Controllers/Account/IndexController.php +++ b/app/Http/Controllers/Account/IndexController.php @@ -29,10 +29,12 @@ use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Account; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Http\Controllers\BasicDataSupport; use Illuminate\Contracts\View\Factory; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Facades\Log; use Illuminate\View\View; /** @@ -71,22 +73,22 @@ class IndexController extends Controller * */ public function inactive(Request $request, string $objectType) { - $inactivePage = true; - $subTitle = (string) trans(sprintf('firefly.%s_accounts_inactive', $objectType)); - $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); - $types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); - $collection = $this->repository->getInactiveAccountsByType($types); - $total = $collection->count(); - $page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); - $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; - $accounts = $collection->slice(($page - 1) * $pageSize, $pageSize); + $inactivePage = true; + $subTitle = (string) trans(sprintf('firefly.%s_accounts_inactive', $objectType)); + $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); + $types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); + $collection = $this->repository->getInactiveAccountsByType($types); + $total = $collection->count(); + $page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); + $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; + $accounts = $collection->slice(($page - 1) * $pageSize, $pageSize); unset($collection); /** @var Carbon $start */ - $start = clone session('start', today(config('app.timezone'))->startOfMonth()); + $start = clone session('start', today(config('app.timezone'))->startOfMonth()); /** @var Carbon $end */ - $end = clone session('end', today(config('app.timezone'))->endOfMonth()); + $end = clone session('end', today(config('app.timezone'))->endOfMonth()); $start->subDay(); $ids = $accounts->pluck('id')->toArray(); @@ -99,8 +101,8 @@ class IndexController extends Controller function (Account $account) use ($activities, $startBalances, $endBalances): void { $currency = $this->repository->getAccountCurrency($account); $account->lastActivityDate = $this->isInArrayDate($activities, $account->id); - $account->startBalances = $this->getBalance($account, $currency, $startBalances); - $account->endBalances = $this->getBalance($account, $currency, $endBalances); + $account->startBalances = Steam::filterAccountBalance($startBalances[$account->id] ?? [], $account, $this->convertToNative, $currency); + $account->endBalances = Steam::filterAccountBalance($endBalances[$account->id] ?? [], $account, $this->convertToNative, $currency); $account->differences = $this->subtract($account->startBalances, $account->endBalances); $account->interest = app('steam')->bcround($this->repository->getMetaValue($account, 'interest'), 4); $account->interestPeriod = (string) trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))); @@ -111,7 +113,7 @@ class IndexController extends Controller ); // make paginator: - $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page); + $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page); $accounts->setPath(route('accounts.inactive.index', [$objectType])); return view('accounts.index', compact('objectType', 'inactivePage', 'subTitleIcon', 'subTitle', 'page', 'accounts')); @@ -127,9 +129,9 @@ class IndexController extends Controller public function index(Request $request, string $objectType) { app('log')->debug(sprintf('Now at %s', __METHOD__)); - $subTitle = (string) trans(sprintf('firefly.%s_accounts', $objectType)); - $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); - $types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); + $subTitle = (string) trans(sprintf('firefly.%s_accounts', $objectType)); + $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); + $types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); $this->repository->resetAccountOrder(); @@ -145,10 +147,10 @@ class IndexController extends Controller unset($collection); /** @var Carbon $start */ - $start = clone session('start', today(config('app.timezone'))->startOfMonth()); + $start = clone session('start', today(config('app.timezone'))->startOfMonth()); /** @var Carbon $end */ - $end = clone session('end', today(config('app.timezone'))->endOfMonth()); + $end = clone session('end', today(config('app.timezone'))->endOfMonth()); $start->subDay(); $ids = $accounts->pluck('id')->toArray(); @@ -159,12 +161,12 @@ class IndexController extends Controller $accounts->each( function (Account $account) use ($activities, $startBalances, $endBalances): void { - $interest = (string) $this->repository->getMetaValue($account, 'interest'); - $interest = '' === $interest ? '0' : $interest; - $currency = $this->repository->getAccountCurrency($account); + $interest = (string) $this->repository->getMetaValue($account, 'interest'); + $interest = '' === $interest ? '0' : $interest; + $currency = $this->repository->getAccountCurrency($account); - $account->startBalances = $this->getBalance($account, $currency, $startBalances); - $account->endBalances = $this->getBalance($account, $currency, $endBalances); + $account->startBalances = Steam::filterAccountBalance($startBalances[$account->id] ?? [], $account, $this->convertToNative, $currency); + $account->endBalances = Steam::filterAccountBalance($endBalances[$account->id] ?? [], $account, $this->convertToNative, $currency); $account->differences = $this->subtract($account->startBalances, $account->endBalances); $account->lastActivityDate = $this->isInArrayDate($activities, $account->id); $account->interest = app('steam')->bcround($interest, 4); @@ -175,7 +177,7 @@ class IndexController extends Controller $account->location = $this->repository->getLocation($account); $account->liability_direction = $this->repository->getMetaValue($account, 'liability_direction'); $account->current_debt = $this->repository->getMetaValue($account, 'current_debt') ?? '-'; - $account->currency = $currency; + $account->currency = $currency ?? $this->defaultCurrency; $account->iban = implode(' ', str_split((string) $account->iban, 4)); @@ -185,7 +187,7 @@ class IndexController extends Controller app('log')->debug(sprintf('Count of accounts before LAP: %d', $accounts->count())); /** @var LengthAwarePaginator $accounts */ - $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page); + $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page); $accounts->setPath(route('accounts.index', [$objectType])); app('log')->debug(sprintf('Count of accounts after LAP (1): %d', $accounts->count())); @@ -194,22 +196,6 @@ class IndexController extends Controller return view('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts')); } - private function getBalance(Account $account, ?TransactionCurrency $currency, array $balances): array - { - if (!array_key_exists($account->id, $balances)) { - return []; - } - $set = $balances[$account->id]; - - if ($this->convertToNative && $this->defaultCurrency->id === $currency?->id) { - unset($set['native_balance'], $set[$this->defaultCurrency->code]); - } - if ($this->convertToNative && null !== $currency && $this->defaultCurrency->id !== $currency->id) { - unset($set['balance']); - } - - return $set; - } private function subtract(array $startBalances, array $endBalances) { diff --git a/app/Http/Controllers/Account/ShowController.php b/app/Http/Controllers/Account/ShowController.php index 5602b5a46c..471237d3e5 100644 --- a/app/Http/Controllers/Account/ShowController.php +++ b/app/Http/Controllers/Account/ShowController.php @@ -30,6 +30,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Http\Controllers\PeriodOverview; use Illuminate\Contracts\View\Factory; @@ -90,7 +91,7 @@ class ShowController extends Controller // @var Carbon $end $end ??= session('end'); - if ($end < $start) { + if ($end->lt($start)) { [$start, $end] = [$end, $start]; } $location = $this->repository->getLocation($account); @@ -99,7 +100,8 @@ class ShowController extends Controller $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $account->accountType->type)); $page = (int) $request->get('page'); $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; - $currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); + $accountCurrency = $this->repository->getAccountCurrency($account); + $currency = $accountCurrency ?? Amount::getDefaultCurrency(); $fStart = $start->isoFormat($this->monthAndDayFormat); $fEnd = $end->isoFormat($this->monthAndDayFormat); $subTitle = (string) trans('firefly.journals_in_period_for_account', ['name' => $account->name, 'start' => $fStart, 'end' => $fEnd]); @@ -129,7 +131,7 @@ class ShowController extends Controller $groups->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')])); $showAll = false; - $balance = Steam::finalAccountBalance($account, $end)['balance'] ?? '0'; // TODO fix me + $balances = Steam::filterAccountBalance(Steam::finalAccountBalance($account, $end), $account, $this->convertToNative, $accountCurrency); return view( 'accounts.show', @@ -148,7 +150,7 @@ class ShowController extends Controller 'end', 'chartUrl', 'location', - 'balance' + 'balances' ) ); } @@ -175,7 +177,7 @@ class ShowController extends Controller $subTitleIcon = config('firefly.subIconsByIdentifier.'.$account->accountType->type); $page = (int) $request->get('page'); $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; - $currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); + $currency = $this->repository->getAccountCurrency($account) ?? Amount::getDefaultCurrency(); $subTitle = (string) trans('firefly.all_journals_for_account', ['name' => $account->name]); $periods = new Collection(); diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ea1e44f940..651d73fdc2 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -431,11 +431,10 @@ class AccountRepository implements AccountRepositoryInterface public function getUsedCurrencies(Account $account): Collection { - $info = $account->transactions()->get(['transaction_currency_id', 'foreign_currency_id'])->toArray(); + $info = $account->transactions()->distinct()->groupBy('transaction_currency_id')->get(['transaction_currency_id'])->toArray(); $currencyIds = []; foreach ($info as $entry) { $currencyIds[] = (int) $entry['transaction_currency_id']; - $currencyIds[] = (int) $entry['foreign_currency_id']; } $currencyIds = array_unique($currencyIds); diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 72807b4c0f..fd5a8a9cd7 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -355,6 +355,62 @@ class Steam return array_merge($return, $others); } + public function filterAccountBalances(array $total, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array { + $return = []; + foreach($total as $key => $value) { + $return[$key] = $this->filterAccountBalance($value, $account, $convertToNative, $currency); + } + return $return; + } + + public function filterAccountBalance(array $set, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array { + if(0 === count($set)) { + Log::debug(sprintf('Return empty array for account #%d', $account->id)); + return []; + } + $defaultCurrency = app('amount')->getDefaultCurrency(); + if($convertToNative) { + if ($defaultCurrency->id === $currency?->id) { + Log::debug(sprintf('Unset "native_balance" and "%s" for account #%d', $defaultCurrency->code, $account->id)); + unset($set['native_balance'], $set[$defaultCurrency->code]); + } + if (null !== $currency && $defaultCurrency->id !== $currency->id) { + Log::debug(sprintf('Unset balance for account #%d', $account->id)); + unset($set['balance']); + } + + if (null === $currency) { + Log::debug(sprintf('TEMP DO NOT Drop defaultCurrency balance for account #%d', $account->id)); + //unset($set[$this->defaultCurrency->code]); + } + } + + if(!$convertToNative) { + if (null === $currency) { + Log::debug(sprintf('Unset native_balance and make defaultCurrency balance the balance for account #%d', $account->id)); + $set['balance'] = $set[$this->defaultCurrency->code] ?? '0'; + unset($set['native_balance'], $set[$defaultCurrency->code]); + } + + if (null !== $currency) { + Log::debug(sprintf('Unset native_balance + defaultCurrency + currencyCode balance for account #%d', $account->id)); + unset($set['native_balance'], $set[$defaultCurrency->code], $set[$currency->code]); + } + } + + + // put specific value first in array. + if (array_key_exists('native_balance', $set)) { + $set = ['native_balance' => $set['native_balance']] + $set; + } + if (array_key_exists('balance', $set)) { + $set = ['balance' => $set['balance']] + $set; + } + Log::debug(sprintf('Return #%d', $account->id), $set); + + return $set; + } + private function groupAndSumTransactions(array $array, string $group, string $field): array { $return = []; diff --git a/resources/views/accounts/show.twig b/resources/views/accounts/show.twig index 0210282438..0d4d1c7da8 100644 --- a/resources/views/accounts/show.twig +++ b/resources/views/accounts/show.twig @@ -11,9 +11,15 @@