diff --git a/app/Support/Binder/EitherConfigKey.php b/app/Support/Binder/EitherConfigKey.php index 719f98d481..a5a8494c76 100644 --- a/app/Support/Binder/EitherConfigKey.php +++ b/app/Support/Binder/EitherConfigKey.php @@ -34,6 +34,10 @@ class EitherConfigKey { public static array $static = [ + // currency conversion + 'cer.enabled', + + // firefly iii settings 'firefly.version', 'firefly.api_version', 'firefly.default_location', diff --git a/resources/assets/v2/api/v1/configuration/get.js b/resources/assets/v2/api/v1/configuration/get.js new file mode 100644 index 0000000000..6289f405fe --- /dev/null +++ b/resources/assets/v2/api/v1/configuration/get.js @@ -0,0 +1,27 @@ +/* + * basic.js + * Copyright (c) 2021 james@firefly-iii.org + * + * This file is part of Firefly III (https://github.com/firefly-iii). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import {api} from "../../../boot/axios"; + +export default class Get { + getByName(name) { + return api.get('/api/v1/configuration/' + name); + } +} diff --git a/resources/assets/v2/pages/dashboard/accounts.js b/resources/assets/v2/pages/dashboard/accounts.js index aa0983092d..179c49f965 100644 --- a/resources/assets/v2/pages/dashboard/accounts.js +++ b/resources/assets/v2/pages/dashboard/accounts.js @@ -27,6 +27,7 @@ import {Chart} from 'chart.js'; import {getDefaultChartSettings} from "../../support/default-chart-settings.js"; import {getColors} from "../../support/get-colors.js"; import {getCacheKey} from "../../support/get-cache-key.js"; +import {getConfiguration} from "../../store/get-configuration.js"; // this is very ugly, but I have no better ideas at the moment to save the currency info // for each series. @@ -40,6 +41,7 @@ export default () => ({ loadingAccounts: false, accountList: [], autoConversion: false, + autoConversionAvailable: false, chartOptions: null, switchAutoConversion() { this.autoConversion = !this.autoConversion; @@ -215,8 +217,16 @@ export default () => ({ for (let iii = 0; iii < current.attributes.transactions.length; iii++) { let currentTransaction = current.attributes.transactions[iii]; //console.log(currentTransaction); - const nativeAmountRaw = 'withdrawal' === currentTransaction.type ? parseFloat(currentTransaction.native_amount) * -1 : parseFloat(currentTransaction.native_amount); - const amountRaw = 'withdrawal' === currentTransaction.type ? parseFloat(currentTransaction.amount) * -1 : parseFloat(currentTransaction.amount); + let nativeAmountRaw = 'withdrawal' === currentTransaction.type ? parseFloat(currentTransaction.native_amount) * -1 : parseFloat(currentTransaction.native_amount); + let amountRaw = 'withdrawal' === currentTransaction.type ? parseFloat(currentTransaction.amount) * -1 : parseFloat(currentTransaction.amount); + + // if transfer and source is this account, multiply again + if('transfer' === currentTransaction.type && parseInt(currentTransaction.source_id) === accountId) { // + console.log('transfer', parseInt(currentTransaction.source_id), accountId); + nativeAmountRaw = nativeAmountRaw * -1; + amountRaw = amountRaw * -1; + } + group.transactions.push({ description: currentTransaction.description, id: current.id, @@ -259,9 +269,12 @@ export default () => ({ init() { // console.log('accounts init'); - Promise.all([getVariable('viewRange', '1M'), getVariable('autoConversion', false), getVariable('language', 'en_US')]).then((values) => { + Promise.all([getVariable('viewRange', '1M'), getVariable('autoConversion', false), getVariable('language', 'en_US'), + getConfiguration('cer.enabled', false) + ]).then((values) => { //console.log('accounts after promises'); - this.autoConversion = values[1]; + this.autoConversion = values[1] && values[3]; + this.autoConversionAvailable = values[3]; afterPromises = true; // main dashboard chart: diff --git a/resources/assets/v2/store/get-configuration.js b/resources/assets/v2/store/get-configuration.js new file mode 100644 index 0000000000..814f906c81 --- /dev/null +++ b/resources/assets/v2/store/get-configuration.js @@ -0,0 +1,45 @@ +/* + * get-configuration.js + * Copyright (c) 2024 james@firefly-iii.org. + * + * This file is part of Firefly III (https://github.com/firefly-iii). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +import Get from "../api/v1/configuration/get.js"; +import {parseResponse} from "./get-variable.js"; + +export function getConfiguration(name, defaultValue = null) { + const validCache = window.store.get('cacheValid'); + // currently unused, window.X can be used by the blade template + // to make things available quicker than if the store has to grab it through the API. + // then again, it's not that slow. + if (validCache && window.hasOwnProperty(name)) { + // console.log('Get from window'); + return Promise.resolve(window[name]); + } + // load from store2, if it's present. + const fromStore = window.store.get(name); + if (validCache && typeof fromStore !== 'undefined') { + return Promise.resolve(fromStore); + } + let getter = (new Get); + return getter.getByName(name).then((response) => { + // console.log('Get "' + name + '" from API'); + return Promise.resolve(parseResponse(name, response)); + }).catch(() => { + return defaultValue; + }); +} diff --git a/resources/assets/v2/store/get-variable.js b/resources/assets/v2/store/get-variable.js index e9ba25a721..56d506de46 100644 --- a/resources/assets/v2/store/get-variable.js +++ b/resources/assets/v2/store/get-variable.js @@ -52,7 +52,7 @@ export function getVariable(name, defaultValue = null) { }); } -function parseResponse(name, response) { +export function parseResponse(name, response) { let value = response.data.data.attributes.data; window.store.set(name, value); // console.log('Store "' + name + '" in localStorage'); diff --git a/resources/views/v2/partials/dashboard/account-chart.blade.php b/resources/views/v2/partials/dashboard/account-chart.blade.php index 4e94a5276c..40a4fc9db1 100644 --- a/resources/views/v2/partials/dashboard/account-chart.blade.php +++ b/resources/views/v2/partials/dashboard/account-chart.blade.php @@ -9,22 +9,24 @@
- diff --git a/resources/views/v2/partials/dashboard/boxes.blade.php b/resources/views/v2/partials/dashboard/boxes.blade.php index 5600c968b6..14a602c84e 100644 --- a/resources/views/v2/partials/dashboard/boxes.blade.php +++ b/resources/views/v2/partials/dashboard/boxes.blade.php @@ -63,7 +63,7 @@ -