From dde7bcfc4c36d82975d90017fb9e6e5df1c8c7e9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 25 Jul 2023 09:01:44 +0200 Subject: [PATCH] Expand code for exchange rates --- .../Controllers/Chart/AccountController.php | 51 ++-- app/Console/Commands/Tools/Cron.php | 4 +- app/Jobs/DownloadExchangeRates.php | 2 +- .../Account/AccountRepository.php | 109 ++++++++- .../Account/AccountRepositoryInterface.php | 38 ++- .../Http/Api/ConvertsExchangeRates.php | 173 +++++++------ .../Http/Api/ExchangeRateConverter.php | 59 +++++ .../Administration/AdministrationTrait.php | 15 +- .../Recurring/FiltersWeekends.php | 4 +- app/Support/Steam.php | 231 +++++++++++++++++- config/cer.php | 65 ++--- config/firefly.php | 3 +- 12 files changed, 609 insertions(+), 145 deletions(-) create mode 100644 app/Support/Http/Api/ExchangeRateConverter.php diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index 1c342e1120..3927147fc7 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -29,8 +29,9 @@ use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface; use FireflyIII\Support\Http\Api\ConvertsExchangeRates; +use FireflyIII\User; use Illuminate\Http\JsonResponse; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -63,6 +64,8 @@ class AccountController extends Controller * This endpoint is documented at * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/charts/getChartAccountOverview * + * The native currency is the preferred currency on the page /currencies. + * * @param DateRequest $request * * @return JsonResponse @@ -77,6 +80,12 @@ class AccountController extends Controller $start = $dates['start']; /** @var Carbon $end */ $end = $dates['end']; + /** @var User $user */ + $user = auth()->user(); + + // group ID + $administrationId = $user->getAdministrationId(); + $this->repository->setAdministrationId($administrationId); // user's preferences $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray(); @@ -96,35 +105,41 @@ class AccountController extends Controller if (null === $currency) { $currency = $default; } - $currentSet = [ + $currentSet = [ 'label' => $account->name, + // the currency that belongs to the account. 'currency_id' => (string)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, - 'native_id' => null, - 'native_code' => null, - 'native_symbol' => null, - 'native_decimal_places' => null, + + // the default currency of the user (may be the same!) + 'native_id' => $default->id, + 'native_code' => $default->code, + 'native_symbol' => $default->symbol, + 'native_decimal_places' => $default->decimal_places, 'start_date' => $start->toAtomString(), 'end_date' => $end->toAtomString(), - 'type' => 'line', // line, area or bar - 'yAxisID' => 0, // 0, 1, 2 'entries' => [], + 'converted_entries' => [], ]; - $currentStart = clone $start; - $range = app('steam')->balanceInRange($account, $start, clone $end); + $currentStart = clone $start; + $range = app('steam')->balanceInRange($account, $start, clone $end, $currency); + $rangeConverted = app('steam')->balanceInRangeConverted($account, $start, clone $end, $default); - // 2022-10-11: this method no longer converts to floats - - $previous = array_values($range)[0]; + $previous = array_values($range)[0]; + $previousConverted = array_values($rangeConverted)[0]; while ($currentStart <= $end) { - $format = $currentStart->format('Y-m-d'); - $label = $currentStart->toAtomString(); - $balance = array_key_exists($format, $range) ? $range[$format] : $previous; - $previous = $balance; + $format = $currentStart->format('Y-m-d'); + $label = $currentStart->toAtomString(); + $balance = array_key_exists($format, $range) ? $range[$format] : $previous; + $balanceConverted = array_key_exists($format, $rangeConverted) ? $rangeConverted[$format] : $previousConverted; + $previous = $balance; + $previousConverted = $balanceConverted; + $currentStart->addDay(); - $currentSet['entries'][$label] = $balance; + $currentSet['entries'][$label] = $balance; + $currentSet['converted_entries'][$label] = $balanceConverted; } $currentSet = $this->cerChartSet($currentSet); $chartData[] = $currentSet; diff --git a/app/Console/Commands/Tools/Cron.php b/app/Console/Commands/Tools/Cron.php index 85743f456a..bf9d13ada0 100644 --- a/app/Console/Commands/Tools/Cron.php +++ b/app/Console/Commands/Tools/Cron.php @@ -78,9 +78,9 @@ class Cron extends Command $force = (bool)$this->option('force'); /* - * Fire recurring transaction cron job. + * Fire exchange rates cron job. */ - if (true === config('cer.enabled')) { + if (true === config('cer.download_enabled')) { try { $this->exchangeRatesCronJob($force, $date); } catch (FireflyException $e) { diff --git a/app/Jobs/DownloadExchangeRates.php b/app/Jobs/DownloadExchangeRates.php index f2c36d0c04..79c944537b 100644 --- a/app/Jobs/DownloadExchangeRates.php +++ b/app/Jobs/DownloadExchangeRates.php @@ -121,7 +121,7 @@ class DownloadExchangeRates implements ShouldQueue app('log')->warning(sprintf('Trying to grab "%s" resulted in bad JSON.', $url)); return; } - $date = Carbon::createFromFormat('Y-m-d', $json['date']); + $date = Carbon::createFromFormat('Y-m-d', $json['date'], config('app.timezone')); $this->saveRates($currency, $date, $json['rates']); } diff --git a/app/Repositories/Administration/Account/AccountRepository.php b/app/Repositories/Administration/Account/AccountRepository.php index 8f056c73dd..c70e665eb0 100644 --- a/app/Repositories/Administration/Account/AccountRepository.php +++ b/app/Repositories/Administration/Account/AccountRepository.php @@ -25,6 +25,10 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Administration\Account; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountMeta; +use FireflyIII\Models\AccountType; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Support\Repositories\Administration\AdministrationTrait; use Illuminate\Support\Collection; @@ -35,6 +39,7 @@ class AccountRepository implements AccountRepositoryInterface { use AdministrationTrait; + /** * @inheritDoc */ @@ -42,11 +47,11 @@ class AccountRepository implements AccountRepositoryInterface { // search by group, not by user $dbQuery = $this->userGroup->accounts() - ->where('active', true) - ->orderBy('accounts.order', 'ASC') - ->orderBy('accounts.account_type_id', 'ASC') - ->orderBy('accounts.name', 'ASC') - ->with(['accountType']); + ->where('active', true) + ->orderBy('accounts.order', 'ASC') + ->orderBy('accounts.account_type_id', 'ASC') + ->orderBy('accounts.name', 'ASC') + ->with(['accountType']); if ('' !== $query) { // split query on spaces just in case: $parts = explode(' ', $query); @@ -62,4 +67,98 @@ class AccountRepository implements AccountRepositoryInterface return $dbQuery->take($limit)->get(['accounts.*']); } + + /** + * @inheritDoc + */ + public function getAccountsByType(array $types, ?array $sort = []): Collection + { + $res = array_intersect([AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], $types); + $query = $this->userGroup->accounts(); + if (0 !== count($types)) { + $query->accountTypeIn($types); + } + + // add sort parameters. At this point they're filtered to allowed fields to sort by: + if (0 !== count($sort)) { + foreach ($sort as $param) { + $query->orderBy($param[0], $param[1]); + } + } + + if (0 === count($sort)) { + if (0 !== count($res)) { + $query->orderBy('accounts.order', 'ASC'); + } + $query->orderBy('accounts.active', 'DESC'); + $query->orderBy('accounts.name', 'ASC'); + } + return $query->get(['accounts.*']); + } + + /** + * @param array $accountIds + * + * @return Collection + */ + public function getAccountsById(array $accountIds): Collection + { + $query = $this->userGroup->accounts(); + + if (0 !== count($accountIds)) { + $query->whereIn('accounts.id', $accountIds); + } + $query->orderBy('accounts.order', 'ASC'); + $query->orderBy('accounts.active', 'DESC'); + $query->orderBy('accounts.name', 'ASC'); + + return $query->get(['accounts.*']); + } + + /** + * @param Account $account + * + * @return TransactionCurrency|null + */ + public function getAccountCurrency(Account $account): ?TransactionCurrency + { + $type = $account->accountType->type; + $list = config('firefly.valid_currency_account_types'); + + // return null if not in this list. + if (!in_array($type, $list, true)) { + return null; + } + $currencyId = (int)$this->getMetaValue($account, 'currency_id'); + if ($currencyId > 0) { + return TransactionCurrency::find($currencyId); + } + + return null; + } + + /** + * Return meta value for account. Null if not found. + * + * @param Account $account + * @param string $field + * + * @return null|string + */ + public function getMetaValue(Account $account, string $field): ?string + { + $result = $account->accountMeta->filter( + function (AccountMeta $meta) use ($field) { + return strtolower($meta->name) === strtolower($field); + } + ); + if (0 === $result->count()) { + return null; + } + if (1 === $result->count()) { + return (string)$result->first()->data; + } + + return null; + } } diff --git a/app/Repositories/Administration/Account/AccountRepositoryInterface.php b/app/Repositories/Administration/Account/AccountRepositoryInterface.php index 969cefd6fd..483bdb43e0 100644 --- a/app/Repositories/Administration/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Administration/Account/AccountRepositoryInterface.php @@ -25,6 +25,8 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Administration\Account; +use FireflyIII\Models\Account; +use FireflyIII\Models\TransactionCurrency; use Illuminate\Support\Collection; /** @@ -34,10 +36,42 @@ interface AccountRepositoryInterface { /** * @param string $query - * @param array $types - * @param int $limit + * @param array $types + * @param int $limit * * @return Collection */ public function searchAccount(string $query, array $types, int $limit): Collection; + + /** + * @param array $types + * @param array|null $sort + * + * @return Collection + */ + public function getAccountsByType(array $types, ?array $sort = []): Collection; + + /** + * @param array $accountIds + * + * @return Collection + */ + public function getAccountsById(array $accountIds): Collection; + + /** + * @param Account $account + * + * @return TransactionCurrency|null + */ + public function getAccountCurrency(Account $account): ?TransactionCurrency; + + /** + * Return meta value for account. Null if not found. + * + * @param Account $account + * @param string $field + * + * @return null|string + */ + public function getMetaValue(Account $account, string $field): ?string; } diff --git a/app/Support/Http/Api/ConvertsExchangeRates.php b/app/Support/Http/Api/ConvertsExchangeRates.php index 65cbbfb6fd..30b3e753b4 100644 --- a/app/Support/Http/Api/ConvertsExchangeRates.php +++ b/app/Support/Http/Api/ConvertsExchangeRates.php @@ -26,9 +26,10 @@ namespace FireflyIII\Support\Http\Api; use Carbon\Carbon; use DateTimeInterface; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\TransactionCurrency; -use Illuminate\Support\Facades\Log; +use FireflyIII\Support\CacheProperties; /** * Trait ConvertsExchangeRates @@ -49,6 +50,8 @@ trait ConvertsExchangeRates } // if not enabled, return the same array but without conversion: + return $set; + $this->enabled = false; if (false === $this->enabled) { $set['converted'] = false; return $set; @@ -69,7 +72,7 @@ trait ConvertsExchangeRates $carbon = Carbon::createFromFormat(DateTimeInterface::ATOM, $date); $rate = $this->getRate($currency, $native, $carbon); $rate = '0' === $rate ? '1' : $rate; - Log::debug(sprintf('bcmul("%s", "%s")', (string)$entry, $rate)); + app('log')->debug(sprintf('bcmul("%s", "%s")', (string)$entry, $rate)); $set['entries'][$date] = (float)bcmul((string)$entry, $rate); } return $set; @@ -80,7 +83,7 @@ trait ConvertsExchangeRates */ private function getPreference(): void { - $this->enabled = true; + $this->enabled = config('cer.currency_conversion'); } /** @@ -103,55 +106,65 @@ trait ConvertsExchangeRates * @param Carbon $date * * @return string + * @throws FireflyException */ private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string { - Log::debug(sprintf('getRate(%s, %s, "%s")', $from->code, $to->code, $date->format('Y-m-d'))); + // first attempt: + $rate = $this->getFromDB((int)$from->id, (int)$to->id, $date->format('Y-m-d')); + if (null !== $rate) { + return $rate; + } + // no result. perhaps the other way around? + $rate = $this->getFromDB((int)$to->id, (int)$from->id, $date->format('Y-m-d')); + if (null !== $rate) { + return bcdiv('1', $rate); + } + + // if nothing in place, fall back on the rate for $from to EUR + $first = $this->getEuroRate($from, $date); + $second = $this->getEuroRate($to, $date); + + // combined (if present), they can be used to calculate the necessary conversion rate. + if ('0' === $first || '0' === $second) { + return '0'; + } + + $second = bcdiv('1', $second); + return bcmul($first, $second); + } + + /** + * @param int $from + * @param int $to + * @param string $date + * + * @return string|null + */ + private function getFromDB(int $from, int $to, string $date): ?string + { + $key = sprintf('cer-%d-%d-%s', $from, $to, $date); + + $cache = new CacheProperties(); + $cache->addProperty($key); + if ($cache->has()) { + return $cache->get(); + } + /** @var CurrencyExchangeRate $result */ $result = auth()->user() ->currencyExchangeRates() - ->where('from_currency_id', $from->id) - ->where('to_currency_id', $to->id) - ->where('date', '<=', $date->format('Y-m-d')) + ->where('from_currency_id', $from) + ->where('to_currency_id', $to) + ->where('date', '<=', $date) ->orderBy('date', 'DESC') ->first(); if (null !== $result) { $rate = (string)$result->rate; - Log::debug(sprintf('Rate is %s', $rate)); + $cache->store($rate); return $rate; } - // no result. perhaps the other way around? - /** @var CurrencyExchangeRate $result */ - $result = auth()->user() - ->currencyExchangeRates() - ->where('from_currency_id', $to->id) - ->where('to_currency_id', $from->id) - ->where('date', '<=', $date->format('Y-m-d')) - ->orderBy('date', 'DESC') - ->first(); - if (null !== $result) { - $rate = bcdiv('1', (string)$result->rate); - Log::debug(sprintf('Reversed rate is %s', $rate)); - return $rate; - } - // try euro rates - $result1 = $this->getEuroRate($from, $date); - if ('0' === $result1) { - Log::debug(sprintf('No exchange rate between EUR and %s', $from->code)); - return '0'; - } - $result2 = $this->getEuroRate($to, $date); - if ('0' === $result2) { - Log::debug(sprintf('No exchange rate between EUR and %s', $to->code)); - return '0'; - } - // still need to inverse rate 2: - $result2 = bcdiv('1', $result2); - $rate = bcmul($result1, $result2); - Log::debug(sprintf('Rate %s to EUR is %s', $from->code, $result1)); - Log::debug(sprintf('Rate EUR to %s is %s', $to->code, $result2)); - Log::debug(sprintf('Rate for %s to %s is %s', $from->code, $to->code, $rate)); - return $rate; + return null; } /** @@ -159,47 +172,55 @@ trait ConvertsExchangeRates * @param Carbon $date * * @return string + * @throws FireflyException */ private function getEuroRate(TransactionCurrency $currency, Carbon $date): string { - Log::debug(sprintf('Find rate for %s to Euro', $currency->code)); + $euroId = $this->getEuroId(); + if ($euroId === (int)$currency->id) { + return '1'; + } + $rate = $this->getFromDB((int)$currency->id, $euroId, $date->format('Y-m-d')); + + if (null !== $rate) { +// app('log')->debug(sprintf('Rate for %s to EUR is %s.', $currency->code, $rate)); + return $rate; + } + $rate = $this->getFromDB($euroId, (int)$currency->id, $date->format('Y-m-d')); + if (null !== $rate) { + $rate = bcdiv('1', $rate); +// app('log')->debug(sprintf('Inverted rate for %s to EUR is %s.', $currency->code, $rate)); + return $rate; + } + // grab backup values from config file: + $backup = config(sprintf('cer.rates.%s', $currency->code)); + if (null !== $backup) { + $backup = bcdiv('1', (string)$backup); + // app('log')->debug(sprintf('Backup rate for %s to EUR is %s.', $currency->code, $backup)); + return $backup; + } + +// app('log')->debug(sprintf('No rate for %s to EUR.', $currency->code)); + return '0'; + } + + /** + * @return int + * @throws FireflyException + */ + private function getEuroId(): int + { + $cache = new CacheProperties(); + $cache->addProperty('cer-euro-id'); + if ($cache->has()) { + return $cache->get(); + } $euro = TransactionCurrency::whereCode('EUR')->first(); if (null === $euro) { - app('log')->warning('Cannot do indirect conversion without EUR.'); - return '0'; + throw new FireflyException('Cannot find EUR in system, cannot do currency conversion.'); } - - // try one way: - /** @var CurrencyExchangeRate $result */ - $result = auth()->user() - ->currencyExchangeRates() - ->where('from_currency_id', $currency->id) - ->where('to_currency_id', $euro->id) - ->where('date', '<=', $date->format('Y-m-d')) - ->orderBy('date', 'DESC') - ->first(); - if (null !== $result) { - $rate = (string)$result->rate; - Log::debug(sprintf('Rate for %s to EUR is %s.', $currency->code, $rate)); - return $rate; - } - // try the other way around and inverse it. - /** @var CurrencyExchangeRate $result */ - $result = auth()->user() - ->currencyExchangeRates() - ->where('from_currency_id', $euro->id) - ->where('to_currency_id', $currency->id) - ->where('date', '<=', $date->format('Y-m-d')) - ->orderBy('date', 'DESC') - ->first(); - if (null !== $result) { - $rate = bcdiv('1', (string)$result->rate); - Log::debug(sprintf('Inverted rate for %s to EUR is %s.', $currency->code, $rate)); - return $rate; - } - - Log::debug(sprintf('No rate for %s to EUR.', $currency->code)); - return '0'; + $cache->store((int)$euro->id); + return (int)$euro->id; } /** @@ -268,7 +289,7 @@ trait ConvertsExchangeRates */ private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string { - Log::debug(sprintf('Converting %s from %s to %s', $amount, $from->code, $to->code)); + app('log')->debug(sprintf('Converting %s from %s to %s', $amount, $from->code, $to->code)); $date = $date ?? today(config('app.timezone')); $rate = $this->getRate($from, $to, $date); diff --git a/app/Support/Http/Api/ExchangeRateConverter.php b/app/Support/Http/Api/ExchangeRateConverter.php new file mode 100644 index 0000000000..e40b35e3fc --- /dev/null +++ b/app/Support/Http/Api/ExchangeRateConverter.php @@ -0,0 +1,59 @@ +. + */ + +namespace FireflyIII\Support\Http\Api; + +use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Models\TransactionCurrency; + +/** + * Class ExchangeRateConverter + */ +class ExchangeRateConverter +{ + use ConvertsExchangeRates; + + /** + * @param TransactionCurrency $from + * @param TransactionCurrency $to + * @param Carbon $date + * + * @return string + * @throws FireflyException + */ + public function getCurrencyRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string + { + if (null === $this->enabled) { + $this->getPreference(); + } + + // if not enabled, return "1" + if (false === $this->enabled) { + return '1'; + } + + $rate = $this->getRate($from, $to, $date); + return '0' === $rate ? '1' : $rate; + } + + +} diff --git a/app/Support/Repositories/Administration/AdministrationTrait.php b/app/Support/Repositories/Administration/AdministrationTrait.php index 315288d5fd..533beec861 100644 --- a/app/Support/Repositories/Administration/AdministrationTrait.php +++ b/app/Support/Repositories/Administration/AdministrationTrait.php @@ -36,9 +36,9 @@ use Illuminate\Contracts\Auth\Authenticatable; */ trait AdministrationTrait { - protected ?int $administrationId = null; - protected User $user; - protected ?UserGroup $userGroup = null; + protected ?int $administrationId = null; + protected User $user; + protected ?UserGroup $userGroup = null; /** * @return int @@ -67,12 +67,15 @@ trait AdministrationTrait { if (null !== $this->administrationId) { $memberships = GroupMembership::where('user_id', $this->user->id) - ->where('user_group_id', $this->administrationId) - ->count(); + ->where('user_group_id', $this->administrationId) + ->count(); if (0 === $memberships) { throw new FireflyException(sprintf('User #%d has no access to administration #%d', $this->user->id, $this->administrationId)); } $this->userGroup = UserGroup::find($this->administrationId); + if (null === $this->userGroup) { + throw new FireflyException(sprintf('Unfound administration for user #%d', $this->user->id)); + } return; } throw new FireflyException(sprintf('Cannot validate administration for user #%d', $this->user->id)); @@ -83,7 +86,7 @@ trait AdministrationTrait * * @return void */ - public function setUser(Authenticatable | User | null $user): void + public function setUser(Authenticatable|User|null $user): void { if (null !== $user) { $this->user = $user; diff --git a/app/Support/Repositories/Recurring/FiltersWeekends.php b/app/Support/Repositories/Recurring/FiltersWeekends.php index 49a3e3e700..58da6897ba 100644 --- a/app/Support/Repositories/Recurring/FiltersWeekends.php +++ b/app/Support/Repositories/Recurring/FiltersWeekends.php @@ -57,7 +57,7 @@ trait FiltersWeekends $isWeekend = $date->isWeekend(); if (!$isWeekend) { $return[] = clone $date; - Log::debug(sprintf('Date is %s, not a weekend date.', $date->format('D d M Y'))); + //Log::debug(sprintf('Date is %s, not a weekend date.', $date->format('D d M Y'))); continue; } @@ -82,7 +82,7 @@ trait FiltersWeekends $return[] = $clone; continue; } - Log::debug(sprintf('Date is %s, removed from final result', $date->format('D d M Y'))); + //Log::debug(sprintf('Date is %s, removed from final result', $date->format('D d M Y'))); } // filter unique dates diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 645fbbdb82..fe37d81045 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -31,6 +31,7 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Http\Api\ExchangeRateConverter; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use JsonException; @@ -114,7 +115,6 @@ class Steam */ public function balanceInRange(Account $account, Carbon $start, Carbon $end, ?TransactionCurrency $currency = null): array { - // abuse chart properties: $cache = new CacheProperties(); $cache->addProperty($account->id); $cache->addProperty('balance-in-range'); @@ -238,6 +238,235 @@ class Steam return $balance; } + /** + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return array + * @throws FireflyException + */ + public function balanceInRangeConverted(Account $account, Carbon $start, Carbon $end, TransactionCurrency $native): array + { + + $cache = new CacheProperties(); + $cache->addProperty($account->id); + $cache->addProperty('balance-in-range-converted'); + $cache->addProperty($native->id); + $cache->addProperty($start); + $cache->addProperty($end); + if ($cache->has()) { + return $cache->get(); + } + app('log')->debug(sprintf('balanceInRangeConverted for account #%d to %s', $account->id, $native->code)); + $start->subDay(); + $end->addDay(); + $balances = []; + $formatted = $start->format('Y-m-d'); + $currencies = []; + $startBalance = $this->balanceConverted($account, $start, $native); // already converted to native amount + $balances[$formatted] = $startBalance; + + app('log')->debug(sprintf('Start balance on %s is %s', $formatted, $startBalance)); + + $converter = new ExchangeRateConverter(); + + // not sure why this is happening: + $start->addDay(); + + // grab all transactions between start and end: + $set = $account->transactions() + ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d 23:59:59')) + ->orderBy('transaction_journals.date', 'ASC') + ->whereNull('transaction_journals.deleted_at') + ->get( + [ + 'transaction_journals.date', + 'transactions.transaction_currency_id', + 'transactions.amount', + 'transactions.foreign_currency_id', + 'transactions.foreign_amount', + ] + )->toArray(); + + // loop the set and convert if necessary: + $currentBalance = $startBalance; + /** @var Transaction $transaction */ + foreach ($set as $transaction) { + $day = Carbon::createFromFormat('Y-m-d H:i:s', $transaction['date'], config('app.timezone')); + $format = $day->format('Y-m-d'); + // if the transaction is in the expected currency, change nothing. + if ((int)$transaction['transaction_currency_id'] === (int)$native->id) { + // change the current balance, set it to today, continue the loop. + $currentBalance = bcadd($currentBalance, $transaction['amount']); + $balances[$format] = $currentBalance; + app('log')->debug(sprintf('%s: transaction in %s, new balance is %s.', $format, $native->code, $currentBalance)); + continue; + } + // if foreign currency is in the expected currency, do nothing: + if ((int)$transaction['foreign_currency_id'] === (int)$native->id) { + $currentBalance = bcadd($currentBalance, $transaction['foreign_amount']); + $balances[$format] = $currentBalance; + app('log')->debug(sprintf('%s: transaction in %s (foreign), new balance is %s.', $format, $native->code, $currentBalance)); + continue; + } + // otherwise, convert 'amount' to the necessary currency: + $currencyId = (int)$transaction['transaction_currency_id']; + $currency = $currencies[$currencyId] ?? TransactionCurrency::find($currencyId); + + + $rate = $converter->getCurrencyRate($currency, $native, $day); + $convertedAmount = bcmul($transaction['amount'], $rate); + $currentBalance = bcadd($currentBalance, $convertedAmount); + $balances[$format] = $currentBalance; + + app('log')->debug(sprintf('%s: transaction in %s(!). Conversion rate is %s. %s %s = %s %s', $format, $currency->code, $rate, + $currency->code, $transaction['amount'], + $native->code, $convertedAmount + )); + + + } + + $cache->store($balances); + + return $balances; + } + + /** + * Gets balance at the end of current month by default. Returns the balance converted + * to the indicated currency ($native). + * + * @param Account $account + * @param Carbon $date + * @param TransactionCurrency $native + * + * @return string + * @throws FireflyException + */ + public function balanceConverted(Account $account, Carbon $date, TransactionCurrency $native): string + { + app('log')->debug(sprintf('Now in balanceConverted (%s) for account #%d, converting to %s', $date->format('Y-m-d'), $account->id, $native->code)); + // abuse chart properties: + $cache = new CacheProperties(); + $cache->addProperty($account->id); + $cache->addProperty('balance'); + $cache->addProperty($date); + $cache->addProperty($native ? $native->id : 0); + if ($cache->has()) { + return $cache->get(); + } + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $currency = $repository->getAccountCurrency($account); + if (null === $currency) { + throw new FireflyException('Cannot get converted account balance: no currency found for account.'); + } + + /** + * selection of transactions + * 1: all normal transactions. No foreign currency info. In $currency. Need conversion. + * 2: all normal transactions. No foreign currency info. In $native. Need NO conversion. + * 3: all normal transactions. No foreign currency info. In neither currency. Need conversion. + * Then, select everything with foreign currency info: + * 4. All transactions with foreign currency info in $native. Normal currency value is ignored. Do not need conversion. + * 5. All transactions with foreign currency info NOT in $native, but currency info in $currency. Need conversion. + * 6. All transactions with foreign currency info NOT in $native, and currency info NOT in $currency. Need conversion. + * + */ + + $new = []; + $existing = []; + // 1 + $new[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.transaction_currency_id', $currency->id) + ->whereNull('transactions.foreign_currency_id') + ->get(['transaction_journals.date', 'transactions.amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #1', count($new[0]))); + // 2 + $existing[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.transaction_currency_id', $native->id) + ->whereNull('transactions.foreign_currency_id') + ->get(['transactions.amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #2', count($existing[0]))); + // 3 + $new[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.transaction_currency_id', '!=', $currency->id) + ->where('transactions.transaction_currency_id', '!=', $native->id) + ->whereNull('transactions.foreign_currency_id') + ->get(['transaction_journals.date', 'transactions.amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #3', count($new[1]))); + // 4 + $existing[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.foreign_currency_id', $native->id) + ->whereNotNull('transactions.foreign_amount') + ->get(['transactions.foreign_amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #4', count($existing[1]))); + // 5 + $new[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.transaction_currency_id', $currency->id) + ->where('transactions.foreign_currency_id', '!=', $native->id) + ->whereNotNull('transactions.foreign_amount') + ->get(['transaction_journals.date', 'transactions.amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #5', count($new[2]))); + // 6 + $new[] = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')) + ->where('transactions.transaction_currency_id', '!=', $currency->id) + ->where('transactions.foreign_currency_id', '!=', $native->id) + ->whereNotNull('transactions.foreign_amount') + ->get(['transaction_journals.date', 'transactions.amount'])->toArray(); + app('log')->debug(sprintf('%d transactions in set #6', count($new[3]))); + + // process both sets of transactions. Of course, no need to convert set "existing". + $balance = $this->sumTransactions($existing[0], 'amount'); + $balance = bcadd($balance, $this->sumTransactions($existing[1], 'foreign_amount')); + //app('log')->debug(sprintf('Balance from set #2 and #4 is %f', $balance)); + + // need to convert the others. All sets use the "amount" value as their base (that's easy) + // but we need to convert each transaction separately because the date difference may + // incur huge currency changes. + $converter = new ExchangeRateConverter(); + foreach ($new as $index => $set) { + foreach ($set as $transaction) { + $date = Carbon::createFromFormat('Y-m-d H:i:s', $transaction['date']); + $rate = $converter->getCurrencyRate($currency, $native, $date); + $convertedAmount = bcmul($transaction['amount'], $rate); + $balance = bcadd($balance, $convertedAmount); +// app('log')->debug(sprintf('Date: %s, rate: %s, amount: %s %s, new: %s %s', +// $date->format('Y-m-d'), +// $rate, +// $currency->code, +// $transaction['amount'], +// $native->code, +// $convertedAmount +// )); + } + //app('log')->debug(sprintf('Balance from new set #%d is %f', $index, $balance)); + } + + // add virtual balance + $virtual = null === $account->virtual_balance ? '0' : (string)$account->virtual_balance; + $balance = bcadd($balance, $virtual); + + $cache->store($balance); + + return $balance; + } + /** * This method always ignores the virtual balance. * diff --git a/config/cer.php b/config/cer.php index aff3718313..7445ea37bd 100644 --- a/config/cer.php +++ b/config/cer.php @@ -24,50 +24,53 @@ declare(strict_types=1); return [ - 'url' => 'https://ff3exchangerates.z6.web.core.windows.net', - 'enabled' => env('ENABLE_EXTERNAL_RATES', false), + 'url' => 'https://ff3exchangerates.z6.web.core.windows.net', + 'enabled' => true, + 'download_enabled' => env('ENABLE_EXTERNAL_RATES', false), + // if currencies are added, default rates must be added as well! // last exchange rate update: 6-6-2022 // source: https://www.xe.com/currencyconverter/ - 'date' => '2022-06-06', - 'rates' => [ + 'date' => '2022-06-06', + + // all rates are from EUR to $currency: + 'rates' => [ + // europa - ['EUR', 'HUF', 387.9629], - ['EUR', 'GBP', 0.85420754], - ['EUR', 'UAH', 31.659752], - ['EUR', 'PLN', 4.581788], - ['EUR', 'TRY', 17.801397], - ['EUR', 'DKK', 7.4389753], + 'EUR' => 1, + 'HUF' => 387.9629, + 'GBP' => 0.85420754, + 'UAH' => 31.659752, + 'PLN' => 4.581788, + 'TRY' => 17.801397, + 'DKK' => 7.4389753, // Americas - ['EUR', 'USD', 1.0722281], - ['EUR', 'BRL', 5.0973173], - ['EUR', 'CAD', 1.3459969], - ['EUR', 'MXN', 20.899824], + 'USD' => 1.0722281, + 'BRL' => 5.0973173, + 'CAD' => 1.3459969, + 'MXN' => 20.899824, // Oceania currencies - ['EUR', 'IDR', 15466.299], - ['EUR', 'AUD', 1.4838549], - ['EUR', 'NZD', 1.6425829], + 'IDR' => 15466.299, + 'AUD' => 1.4838549, + 'NZD' => 1.6425829, // africa - ['EUR', 'EGP', 19.99735], - ['EUR', 'MAD', 10.573307], - ['EUR', 'ZAR', 16.413167], + 'EGP' => 19.99735, + 'MAD' => 10.573307, + 'ZAR' => 16.413167, // asia - ['EUR', 'JPY', 140.15257], - ['EUR', 'RMB', 7.1194265], - ['EUR', 'RUB', 66.000895], - ['EUR', 'INR', 83.220481], + 'JPY' => 140.15257, + 'RMB' => 7.1194265, + 'CNY' => 1, + 'RUB' => 66.000895, + 'INR' => 83.220481, // int - ['EUR', 'XBT', 0, 00003417], - ['EUR', 'BCH', 0.00573987], - ['EUR', 'ETH', 0, 00056204], - - ['EUR', 'ILS', 3.5712508], - ['EUR', 'CHF', 1.0323891], - ['EUR', 'HRK', 7.5220845], + 'ILS' => 3.5712508, + 'CHF' => 1.0323891, + 'HRK' => 7.5220845, ], ]; diff --git a/config/firefly.php b/config/firefly.php index 6d207668c1..1d6cdec201 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -106,6 +106,7 @@ return [ 'telemetry' => false, 'webhooks' => true, 'handle_debts' => true, + // see cer.php for exchange rates feature flag. ], 'version' => '6.0.18', 'api_version' => '2.0.4', @@ -127,7 +128,7 @@ return [ 'disable_csp_header' => env('DISABLE_CSP_HEADER', false), 'allow_webhooks' => env('ALLOW_WEBHOOKS', false), - // email flags + // flags 'send_report_journals' => envNonEmpty('SEND_REPORT_JOURNALS', true), // info for demo site