diff --git a/.ci/phpstan.sh b/.ci/phpstan.sh index 3b3beea910..6d11094b0a 100755 --- a/.ci/phpstan.sh +++ b/.ci/phpstan.sh @@ -30,6 +30,6 @@ SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) # Do static code analysis. # ./vendor/bin/phpstan analyse -c .ci/phpstan.neon --no-progress -./vendor/bin/phpstan analyse -c .ci/phpstan.neon --xdebug --error-format=table > phpstan-report.txt +./vendor/bin/phpstan analyse -c .ci/phpstan.neon --error-format=table > phpstan-report.txt echo 'The PHPstan report can be found in phpstan-report.txt' diff --git a/app/Api/V1/Controllers/Models/Account/UpdateController.php b/app/Api/V1/Controllers/Models/Account/UpdateController.php index 1be3f42970..de97df921a 100644 --- a/app/Api/V1/Controllers/Models/Account/UpdateController.php +++ b/app/Api/V1/Controllers/Models/Account/UpdateController.php @@ -79,7 +79,7 @@ class UpdateController extends Controller $account = $this->repository->update($account, $data); $manager = $this->getManager(); $account->refresh(); - Preferences::mark(); + app('preferences')->mark(); /** @var AccountTransformer $transformer */ $transformer = app(AccountTransformer::class); diff --git a/app/Api/V1/Controllers/Summary/BasicController.php b/app/Api/V1/Controllers/Summary/BasicController.php index 4958b2a72d..3c0ca7d3c8 100644 --- a/app/Api/V1/Controllers/Summary/BasicController.php +++ b/app/Api/V1/Controllers/Summary/BasicController.php @@ -107,8 +107,8 @@ class BasicController extends Controller $balanceData = $this->getBalanceInformation($start, $end); $billData = $this->getBillInformation($start, $end); $spentData = $this->getLeftToSpendInfo($start, $end); - $networthData = $this->getNetWorthInfo($start, $end); - $total = array_merge($balanceData, $billData, $spentData, $networthData); + $netWorthData = $this->getNetWorthInfo($start, $end); + $total = array_merge($balanceData, $billData, $spentData, $netWorthData); // give new keys $return = []; @@ -368,30 +368,30 @@ class BasicController extends Controller } ); - $netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date); + $netWorthSet = $netWorthHelper->byAccounts($filtered, $date); $return = []; - foreach ($netWorthSet as $data) { - /** @var TransactionCurrency $currency */ - $currency = $data['currency']; + foreach ($netWorthSet as $key => $data) { + if('native' === $key) { + continue; + } $amount = $data['balance']; if (0 === bccomp($amount, '0')) { continue; } // return stuff $return[] = [ - 'key' => sprintf('net-worth-in-%s', $currency->code), - 'title' => trans('firefly.box_net_worth_in_currency', ['currency' => $currency->symbol]), + 'key' => sprintf('net-worth-in-%s', $data['currency_code']), + 'title' => trans('firefly.box_net_worth_in_currency', ['currency' => $data['currency_symbol']]), 'monetary_value' => $amount, - 'currency_id' => (string)$currency->id, - 'currency_code' => $currency->code, - 'currency_symbol' => $currency->symbol, - 'currency_decimal_places' => $currency->decimal_places, - 'value_parsed' => app('amount')->formatAnything($currency, $data['balance'], false), + 'currency_id' => (string)$data['currency_id'], + 'currency_code' => $data['currency_code'], + 'currency_symbol' => $data['currency_symbol'], + 'currency_decimal_places' => $data['currency_decimal_places'], + 'value_parsed' => app('amount')->formatFlat($data['currency_symbol'], $data['currency_decimal_places'], $data['balance'], false), 'local_icon' => 'line-chart', 'sub_title' => '', ]; } - return $return; } diff --git a/app/Helpers/Report/NetWorth.php b/app/Helpers/Report/NetWorth.php index de4a6d0d90..d7027aa3f8 100644 --- a/app/Helpers/Report/NetWorth.php +++ b/app/Helpers/Report/NetWorth.php @@ -54,6 +54,11 @@ class NetWorth implements NetWorthInterface private null | UserGroup $userGroup; /** + * This method collects the user's net worth in ALL the user's currencies + * (1, 4 and 8) and also in the 'native' currency for ease of use. + * + * The set of accounts has to be fed to it. + * * @param Collection $accounts * @param Carbon $date * @@ -150,88 +155,6 @@ class NetWorth implements NetWorthInterface return $this->adminAccountRepository; } - /** - * Returns the user's net worth in an array with the following layout: - * - * - - * - currency: TransactionCurrency object - * - date: the current date - * - amount: the user's net worth in that currency. - * - * This repeats for each currency the user has transactions in. - * Result of this method is cached. - * - * @param Collection $accounts - * @param Carbon $date - * - * @return array - * @throws JsonException - * @throws FireflyException - * @deprecated - */ - public function getNetWorthByCurrency(Collection $accounts, Carbon $date): array - { - // start in the past, end in the future? use $date - $cache = new CacheProperties(); - $cache->addProperty($date); - $cache->addProperty('net-worth-by-currency'); - $cache->addProperty(implode(',', $accounts->pluck('id')->toArray())); - if ($cache->has()) { - return $cache->get(); - } - - $netWorth = []; - $result = []; - // Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d'))); - - // get default currency - $default = app('amount')->getDefaultCurrencyByUser($this->user); - - // get all balances: - $balances = app('steam')->balancesByAccounts($accounts, $date); - - // get the preferred currency for this account - /** @var Account $account */ - foreach ($accounts as $account) { - // Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name)); - $currencyId = (int)$this->getRepository()->getMetaValue($account, 'currency_id'); - $currencyId = 0 === $currencyId ? $default->id : $currencyId; - - // Log::debug(sprintf('Currency ID is #%d', $currencyId)); - - // balance in array: - $balance = $balances[$account->id] ?? '0'; - - //Log::debug(sprintf('Balance for %s is %s', $date->format('Y-m-d'), $balance)); - - // always subtract virtual balance. - $virtualBalance = (string)$account->virtual_balance; - if ('' !== $virtualBalance) { - $balance = bcsub($balance, $virtualBalance); - } - - // Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance)); - - if (!array_key_exists($currencyId, $netWorth)) { - $netWorth[$currencyId] = '0'; - } - $netWorth[$currencyId] = bcadd($balance, $netWorth[$currencyId]); - // Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId])); - } - ksort($netWorth); - - // loop results and add currency information: - foreach ($netWorth as $currencyId => $balance) { - $result[] = [ - 'currency' => $this->currencyRepos->find($currencyId), - 'balance' => $balance, - ]; - } - $cache->store($result); - - return $result; - } - /** * @param User|Authenticatable|null $user */ diff --git a/app/Helpers/Report/NetWorthInterface.php b/app/Helpers/Report/NetWorthInterface.php index 61f4c9455c..5604683ecd 100644 --- a/app/Helpers/Report/NetWorthInterface.php +++ b/app/Helpers/Report/NetWorthInterface.php @@ -50,27 +50,6 @@ interface NetWorthInterface */ public function byAccounts(Collection $accounts, Carbon $date): array; - /** - * TODO unsure why this is deprecated. - * - * Returns the user's net worth in an array with the following layout: - * - * - - * - currency: TransactionCurrency object - * - date: the current date - * - amount: the user's net worth in that currency. - * - * This repeats for each currency the user has transactions in. - * Result of this method is cached. - * - * @param Collection $accounts - * @param Carbon $date - * - * @return array - * @deprecated - */ - public function getNetWorthByCurrency(Collection $accounts, Carbon $date): array; - /** * @param User|Authenticatable|null $user */ diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 426e84c66b..2b0f3b98ea 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -109,19 +109,22 @@ class ReportController extends Controller while ($current < $end) { // get balances by date, grouped by currency. - $result = $helper->getNetWorthByCurrency($filtered, $current); + $result = $helper->byAccounts($filtered, $current); // loop result, add to array. /** @var array $netWorthItem */ - foreach ($result as $netWorthItem) { - $currencyId = $netWorthItem['currency']->id; + foreach ($result as $key => $netWorthItem) { + if('native' === $key) { + continue; + } + $currencyId = $netWorthItem['currency_id']; $label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale)); if (!array_key_exists($currencyId, $chartData)) { $chartData[$currencyId] = [ - 'label' => 'Net worth in ' . $netWorthItem['currency']->name, + 'label' => 'Net worth in ' . $netWorthItem['currency_name'], 'type' => 'line', - 'currency_symbol' => $netWorthItem['currency']->symbol, - 'currency_code' => $netWorthItem['currency']->code, + 'currency_symbol' => $netWorthItem['currency_symbol'], + 'currency_code' => $netWorthItem['currency_code'], 'entries' => [], ]; } diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.php index 5e270878d3..48fc94f130 100644 --- a/app/Http/Controllers/Json/BoxController.php +++ b/app/Http/Controllers/Json/BoxController.php @@ -268,12 +268,13 @@ class BoxController extends Controller } ); - $netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date); + $netWorthSet = $netWorthHelper->byAccounts($filtered, $date); $return = []; - foreach ($netWorthSet as $data) { - /** @var TransactionCurrency $currency */ - $currency = $data['currency']; - $return[$currency->id] = app('amount')->formatAnything($currency, $data['balance'], false); + foreach ($netWorthSet as $key => $data) { + if('native' === $key) { + continue; + } + $return[$data['currency_id']] = app('amount')->formatFlat($data['currency_symbol'], $data['currency_decimal_places'], $data['balance'], false); } $return = [ 'net_worths' => array_values($return), diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.php index 42cd599107..90479444c3 100644 --- a/app/Http/Controllers/System/InstallController.php +++ b/app/Http/Controllers/System/InstallController.php @@ -158,7 +158,7 @@ class InstallController extends Controller } // clear cache as well. Cache::clear(); - Preferences::mark(); + app('preferences')->mark(); return true; } diff --git a/app/Http/Middleware/InterestingMessage.php b/app/Http/Middleware/InterestingMessage.php index dca641ef2e..c3fb086f17 100644 --- a/app/Http/Middleware/InterestingMessage.php +++ b/app/Http/Middleware/InterestingMessage.php @@ -54,23 +54,23 @@ class InterestingMessage } if ($this->groupMessage($request)) { - Preferences::mark(); + app('preferences')->mark(); $this->handleGroupMessage($request); } if ($this->accountMessage($request)) { - Preferences::mark(); + app('preferences')->mark(); $this->handleAccountMessage($request); } if ($this->billMessage($request)) { - Preferences::mark(); + app('preferences')->mark(); $this->handleBillMessage($request); } if ($this->webhookMessage($request)) { - Preferences::mark(); + app('preferences')->mark(); $this->handleWebhookMessage($request); } if ($this->currencyMessage($request)) { - Preferences::mark(); + app('preferences')->mark(); $this->handleCurrencyMessage($request); } diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 335f246abf..d40999afa2 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -535,7 +535,7 @@ class Steam $result = []; /** @var Account $account */ foreach ($accounts as $account) { - $default = app('amount')->getDefaultCurrencyByUser($account->user); + $default = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup); $result[(int)$account->id] = [ 'balance' => $this->balance($account, $date),