| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * accounts.js | 
					
						
							|  |  |  |  * Copyright (c) 2023 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 {getVariable} from "../../store/get-variable.js"; | 
					
						
							| 
									
										
										
										
											2023-07-26 07:07:53 +02:00
										 |  |  | import {setVariable} from "../../store/set-variable.js"; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  | import Dashboard from "../../api/v2/chart/account/dashboard.js"; | 
					
						
							|  |  |  | import formatMoney from "../../util/format-money.js"; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  | import Get from "../../api/v2/model/account/get.js"; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  | import {Chart} from 'chart.js'; | 
					
						
							|  |  |  | import {getDefaultChartSettings} from "../../support/default-chart-settings.js"; | 
					
						
							| 
									
										
										
										
											2023-08-27 18:45:06 +02:00
										 |  |  | import {getColors} from "../../support/get-colors.js"; | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  | import {getCacheKey} from "../../support/get-cache-key.js"; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // this is very ugly, but I have no better ideas at the moment to save the currency info
 | 
					
						
							|  |  |  | // for each series.
 | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  | let currencies = []; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  | let chart = null; | 
					
						
							|  |  |  | let chartData = null; | 
					
						
							|  |  |  | let afterPromises = false; | 
					
						
							| 
									
										
										
										
											2023-08-12 17:41:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  | export default () => ({ | 
					
						
							|  |  |  |     loading: false, | 
					
						
							|  |  |  |     loadingAccounts: false, | 
					
						
							|  |  |  |     accountList: [], | 
					
						
							| 
									
										
										
										
											2023-07-26 07:07:53 +02:00
										 |  |  |     autoConversion: false, | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     chartOptions: null, | 
					
						
							| 
									
										
										
										
											2023-07-26 07:07:53 +02:00
										 |  |  |     switchAutoConversion() { | 
					
						
							|  |  |  |         this.autoConversion = !this.autoConversion; | 
					
						
							|  |  |  |         setVariable('autoConversion', this.autoConversion); | 
					
						
							| 
									
										
										
										
											2023-07-25 16:12:58 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     getFreshData() { | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |         const start = new Date(window.store.get('start')); | 
					
						
							|  |  |  |         const end = new Date(window.store.get('end')); | 
					
						
							| 
									
										
										
										
											2023-10-07 07:20:01 +02:00
										 |  |  |         const chartCacheKey = getCacheKey('dashboard-accounts-chart', start, end) | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |         const cacheValid = window.store.get('cacheValid'); | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |         let cachedData = window.store.get(chartCacheKey); | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (cacheValid && typeof cachedData !== 'undefined') { | 
					
						
							| 
									
										
										
										
											2023-10-07 07:20:01 +02:00
										 |  |  |             console.log(cachedData); | 
					
						
							| 
									
										
										
										
											2023-08-27 18:45:06 +02:00
										 |  |  |             this.drawChart(this.generateOptions(cachedData)); | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             this.loading = false; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         const dashboard = new Dashboard(); | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |         dashboard.dashboard(start, end, null).then((response) => { | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |             this.chartData = response.data; | 
					
						
							|  |  |  |             // cache generated options:
 | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |             window.store.set(chartCacheKey, response.data); | 
					
						
							| 
									
										
										
										
											2023-10-07 07:20:01 +02:00
										 |  |  |             console.log(response.data); | 
					
						
							| 
									
										
										
										
											2023-08-27 18:45:06 +02:00
										 |  |  |             this.drawChart(this.generateOptions(this.chartData)); | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |             this.loading = false; | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     generateOptions(data) { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |         currencies = []; | 
					
						
							|  |  |  |         let options = getDefaultChartSettings('line'); | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |         for (let i = 0; i < data.length; i++) { | 
					
						
							|  |  |  |             if (data.hasOwnProperty(i)) { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                 let yAxis = 'y'; | 
					
						
							|  |  |  |                 let current = data[i]; | 
					
						
							|  |  |  |                 let dataset = {}; | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |                 let collection = []; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // if index = 0, push all keys as labels:
 | 
					
						
							|  |  |  |                 if (0 === i) { | 
					
						
							|  |  |  |                     options.data.labels = Object.keys(current.entries); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 dataset.label = current.label; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |                 // use the "native" currency code and use the "native_entries" as array
 | 
					
						
							|  |  |  |                 if (this.autoConversion) { | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |                     currencies.push(current.native_currency_code); | 
					
						
							|  |  |  |                     dataset.currency_code = current.native_currency_code; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |                     collection = Object.values(current.native_entries); | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |                     yAxis = 'y' + current.native_currency_code; | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (!this.autoConversion) { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                     yAxis = 'y' + current.currency_code; | 
					
						
							|  |  |  |                     dataset.currency_code = current.currency_code; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |                     currencies.push(current.currency_code); | 
					
						
							|  |  |  |                     collection = Object.values(current.entries); | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                 dataset.yAxisID = yAxis; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |                 dataset.data = collection; | 
					
						
							| 
									
										
										
										
											2023-07-25 16:12:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 18:45:06 +02:00
										 |  |  |                 // add colors:
 | 
					
						
							|  |  |  |                 //dataset.backgroundColor = getColors(null, 'background');
 | 
					
						
							|  |  |  |                 //dataset.borderColor = getColors(null, 'background');
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                 // add data set to the correct Y Axis:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |                 options.data.datasets.push(dataset); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |         // for each entry in currencies, add a new y-axis:
 | 
					
						
							|  |  |  |         for (let currency in currencies) { | 
					
						
							|  |  |  |             if (currencies.hasOwnProperty(currency)) { | 
					
						
							|  |  |  |                 let code = 'y' + currencies[currency]; | 
					
						
							|  |  |  |                 if (!options.options.scales.hasOwnProperty(code)) { | 
					
						
							|  |  |  |                     options.options.scales[code] = { | 
					
						
							|  |  |  |                         id: currency, | 
					
						
							|  |  |  |                         type: 'linear', | 
					
						
							|  |  |  |                         position: 1 === parseInt(currency) ? 'right' : 'left', | 
					
						
							|  |  |  |                         ticks: { | 
					
						
							|  |  |  |                             callback: function (value, index, values) { | 
					
						
							|  |  |  |                                 return formatMoney(value, currencies[currency]); | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |         return options; | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     loadChart() { | 
					
						
							|  |  |  |         if (true === this.loading) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.loading = true; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |         if (null === chartData) { | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |             this.getFreshData(); | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |         this.drawChart(this.generateOptions(chartData)); | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |         this.loading = false; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |     drawChart(options) { | 
					
						
							|  |  |  |         if (null !== chart) { | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |             // chart already in place, refresh:
 | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             chart.options = options.options; | 
					
						
							|  |  |  |             chart.data = options.data; | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |             chart.update(); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |         chart = new Chart(document.querySelector("#account-chart"), options); | 
					
						
							| 
									
										
										
										
											2023-08-06 18:33:29 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     loadAccounts() { | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |         if (true === this.loadingAccounts) { | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.loadingAccounts = true; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  |         if (this.accountList.length > 0) { | 
					
						
							|  |  |  |             this.loadingAccounts = false; | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |         const start = new Date(window.store.get('start')); | 
					
						
							|  |  |  |         const end = new Date(window.store.get('end')); | 
					
						
							| 
									
										
										
										
											2023-10-07 07:20:01 +02:00
										 |  |  |         const accountCacheKey = getCacheKey('dashboard-accounts-data', start, end); | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |         const cacheValid = window.store.get('cacheValid'); | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |         let cachedData = window.store.get(accountCacheKey); | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (cacheValid && typeof cachedData !== 'undefined') { | 
					
						
							|  |  |  |             this.accountList = cachedData; | 
					
						
							|  |  |  |             this.loadingAccounts = false; | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |         // console.log('loadAccounts continue!');
 | 
					
						
							|  |  |  |         const max = 10; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |         let totalAccounts = 0; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |         let count = 0; | 
					
						
							|  |  |  |         let accounts = []; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |         Promise.all([getVariable('frontpageAccounts'),]).then((values) => { | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |             totalAccounts = values[0].length; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             //console.log(values[0]);
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |             for (let i in values[0]) { | 
					
						
							|  |  |  |                 let account = values[0]; | 
					
						
							|  |  |  |                 if (account.hasOwnProperty(i)) { | 
					
						
							|  |  |  |                     let accountId = account[i]; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                     // grab account info for box:
 | 
					
						
							|  |  |  |                     (new Get).get(accountId, new Date(window.store.get('end'))).then((response) => { | 
					
						
							| 
									
										
										
										
											2023-08-04 19:10:49 +02:00
										 |  |  |                         let parent = response.data.data; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         // get groups for account:
 | 
					
						
							| 
									
										
										
										
											2023-10-01 06:52:38 +02:00
										 |  |  |                         const params = { | 
					
						
							|  |  |  |                             page: 1, | 
					
						
							|  |  |  |                             start: new Date(window.store.get('start')), | 
					
						
							|  |  |  |                             end: new Date(window.store.get('end')), | 
					
						
							|  |  |  |                         }; | 
					
						
							|  |  |  |                         (new Get).transactions(parent.id, params).then((response) => { | 
					
						
							| 
									
										
										
										
											2023-08-04 19:10:49 +02:00
										 |  |  |                             let groups = []; | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                             for (let ii = 0; ii < response.data.data.length; ii++) { | 
					
						
							|  |  |  |                                 if (ii >= max) { | 
					
						
							|  |  |  |                                     break; | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                                 let current = response.data.data[ii]; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 let group = { | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                                     title: null === current.attributes.group_title ? '' : current.attributes.group_title, | 
					
						
							|  |  |  |                                     id: current.id, | 
					
						
							|  |  |  |                                     transactions: [], | 
					
						
							|  |  |  |                                 }; | 
					
						
							|  |  |  |                                 for (let iii = 0; iii < current.attributes.transactions.length; iii++) { | 
					
						
							|  |  |  |                                     let currentTransaction = current.attributes.transactions[iii]; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  |                                     //console.log(currentTransaction);
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:18:25 +02:00
										 |  |  |                                     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); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                                     group.transactions.push({ | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                         description: currentTransaction.description, | 
					
						
							|  |  |  |                                         id: current.id, | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |                                         type: currentTransaction.type, | 
					
						
							| 
									
										
										
										
											2023-09-20 06:18:25 +02:00
										 |  |  |                                         amount_raw: amountRaw, | 
					
						
							|  |  |  |                                         amount: formatMoney(amountRaw, currentTransaction.currency_code), | 
					
						
							|  |  |  |                                         native_amount_raw: nativeAmountRaw, | 
					
						
							|  |  |  |                                         native_amount: formatMoney(nativeAmountRaw, currentTransaction.native_currency_code), | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                     }); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                                 } | 
					
						
							| 
									
										
										
										
											2023-08-04 19:10:49 +02:00
										 |  |  |                                 groups.push(group); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                             } | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                             // console.log(parent);
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |                             accounts.push({ | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 name: parent.attributes.name, | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |                                 order: parent.attributes.order, | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 id: parent.id, | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |                                 balance_raw: parseFloat(parent.attributes.current_balance), | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 balance: formatMoney(parent.attributes.current_balance, parent.attributes.currency_code), | 
					
						
							| 
									
										
										
										
											2023-08-22 08:25:06 +02:00
										 |  |  |                                 native_balance_raw: parseFloat(parent.attributes.native_current_balance), | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |                                 native_balance: formatMoney(parent.attributes.native_current_balance, parent.attributes.native_currency_code), | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 groups: groups, | 
					
						
							|  |  |  |                             }); | 
					
						
							| 
									
										
										
										
											2023-08-29 07:34:41 +02:00
										 |  |  |                             // console.log(parent.attributes);
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |                             count++; | 
					
						
							|  |  |  |                             if (count === totalAccounts) { | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |                                 accounts.sort((a, b) => a.order - b.order); // b - a for reverse sort
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |                                 this.accountList = accounts; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |                                 this.loadingAccounts = false; | 
					
						
							| 
									
										
										
										
											2023-10-01 07:17:56 +02:00
										 |  |  |                                 window.store.set(accountCacheKey, accounts); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |                             } | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |                         }); | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-06 07:02:55 +02:00
										 |  |  |             //this.loadingAccounts = false;
 | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     init() { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |         // console.log('accounts init');
 | 
					
						
							| 
									
										
										
										
											2023-10-08 16:11:04 +02:00
										 |  |  |         Promise.all([getVariable('viewRange', '1M'), getVariable('autoConversion', false), getVariable('language', 'en_US')]).then((values) => { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             //console.log('accounts after promises');
 | 
					
						
							| 
									
										
										
										
											2023-07-26 07:07:53 +02:00
										 |  |  |             this.autoConversion = values[1]; | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             afterPromises = true; | 
					
						
							| 
									
										
										
										
											2023-08-12 17:41:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 19:36:55 +02:00
										 |  |  |             // main dashboard chart:
 | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |             this.loadChart(); | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |             this.loadAccounts(); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |         window.store.observe('end', () => { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             if (!afterPromises) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // console.log('accounts observe end');
 | 
					
						
							|  |  |  |             chartData = null; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  |             this.accountList = []; | 
					
						
							| 
									
										
										
										
											2023-08-06 19:36:55 +02:00
										 |  |  |             // main dashboard chart:
 | 
					
						
							| 
									
										
										
										
											2023-08-08 14:11:04 +02:00
										 |  |  |             this.loadChart(); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |             this.loadAccounts(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  |         window.store.observe('autoConversion', () => { | 
					
						
							| 
									
										
										
										
											2023-08-12 07:53:11 +02:00
										 |  |  |             if (!afterPromises) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // console.log('accounts observe autoconversion');
 | 
					
						
							| 
									
										
										
										
											2023-08-11 06:03:31 +02:00
										 |  |  |             this.loadChart(); | 
					
						
							|  |  |  |             this.loadAccounts(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2023-07-24 18:58:35 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  | }); |