Respond to "convert to native".

This commit is contained in:
James Cole
2025-07-27 20:45:08 +02:00
parent 28b2ddde18
commit a69b6d9ce2
11 changed files with 46 additions and 47 deletions

View File

@@ -87,6 +87,7 @@ class AccountController extends Controller
// move date to end of day
$queryParameters['start']->startOfDay();
$queryParameters['end']->endOfDay();
Log::debug(sprintf('dashboard(), convert to native: %s', var_export($this->convertToNative, true)));
// loop each account, and collect info:
/** @var Account $account */

View File

@@ -147,6 +147,7 @@ class ShowController extends Controller
$enrichment->setUser($admin);
$selectedGroup = $enrichment->enrichSingle($selectedGroup);
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters($this->parameters);

View File

@@ -558,7 +558,6 @@ class GroupCollector implements GroupCollectorInterface
$groups[$groupId]['transactions'][$journalId] = $this->parseAugmentedJournal($augumentedJournal);
}
}
$groups = $this->parseSums($groups);
return new Collection($groups);

View File

@@ -121,6 +121,15 @@ class TransactionGroupTransformer extends AbstractTransformer
if (null !== $transaction['foreign_amount'] && '' !== $transaction['foreign_amount'] && 0 !== bccomp('0', (string) $transaction['foreign_amount'])) {
$foreignAmount = app('steam')->positive($transaction['foreign_amount']);
}
// set native amount to the normal amount if the currency matches.
if($transaction['native_currency']['id'] ?? null === $transaction['currency_id']) {
$transaction['native_amount'] = $amount;
}
if(array_key_exists('native_amount', $transaction) && null !== $transaction['native_amount']) {
$transaction['native_amount'] = app('steam')->positive($transaction['native_amount']);
}
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionTypeEnum::WITHDRAWAL->value);
// must be 0 (int) or NULL

View File

@@ -25,7 +25,7 @@ export default class Dashboard {
dashboard(start, end) {
let startStr = format(start, 'y-MM-dd');
let endStr = format(end, 'y-MM-dd');
return api.get('/api/v1/chart/account/dashboard', {params: {fix: true, start: startStr, end: endStr}});
return api.get('/api/v1/chart/account/dashboard', {params: {start: startStr, end: endStr}});
}
expense(start, end) {

View File

@@ -40,19 +40,14 @@ export default () => ({
loadingAccounts: false,
accountList: [],
convertToNative: false,
convertToNativeAvailable: false,
chartOptions: null,
switchConvertToNative() {
this.convertToNative = !this.convertToNative;
setVariable('convert_to_native', this.convertToNative);
},
localCacheKey(type) {
return 'ds_accounts_' + type;
},
eventListeners: {
['@convert-to-native.window'](event){
console.log('I heard that! it is now ' + event.detail);
console.log('I heard that! (dashboard/accounts)');
this.convertToNative = event.detail;
this.accountList = [];
chartData = null;
@@ -62,19 +57,16 @@ export default () => ({
},
doSomeReload() {
console.log('doSomeReload');
},
getFreshData() {
console.log('get fresh data');
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const chartCacheKey = getCacheKey(this.localCacheKey('chart'), {start: start, end: end})
const chartCacheKey = getCacheKey(this.localCacheKey('chart'), {convertToNative: this.convertToNative, start: start, end: end})
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(chartCacheKey);
if (cacheValid && typeof cachedData !== 'undefined') {
console.log('Generate from cache: ', chartCacheKey);
this.drawChart(this.generateOptions(cachedData));
this.loading = false;
return;
@@ -84,6 +76,7 @@ export default () => ({
this.chartData = response.data;
// cache generated options:
window.store.set(chartCacheKey, response.data);
console.log('Generate FRESH!');
this.drawChart(this.generateOptions(this.chartData));
this.loading = false;
});
@@ -108,14 +101,17 @@ export default () => ({
// use the "native" currency code and use the "native_entries" as array
if (this.convertToNative) {
console.log('Convert to native!');
currencies.push(current.native_currency_code);
dataset.currency_code = current.native_currency_code;
collection = Object.values(current.native_entries);
if(!current.hasOwnProperty('native_entries')) {
console.error('No native entries ('+this.convertToNative+') found for account: ', current);
}
if(current.hasOwnProperty('native_entries')) {
collection = Object.values(current.native_entries);
}
yAxis = 'y' + current.native_currency_code;
}
if (!this.convertToNative) {
console.log('NO convert to native!', this.convertToNative);
yAxis = 'y' + current.currency_code;
dataset.currency_code = current.currency_code;
currencies.push(current.currency_code);
@@ -155,9 +151,7 @@ export default () => ({
return options;
},
loadChart() {
console.log('loadChart');
if (true === this.loading) {
console.log('already loading chart');
return;
}
this.loading = true;
@@ -175,7 +169,6 @@ export default () => ({
chart.options = options.options;
chart.data = options.data;
chart.update();
console.log('refresh chart');
return;
}
chart = new Chart(document.querySelector("#account-chart"), options);
@@ -303,9 +296,8 @@ export default () => ({
]).then((values) => {
//console.log('accounts after promises');
this.convertToNative = values[1] && values[3];
this.convertToNativeAvailable = values[3];
afterPromises = true;
console.log('convertToNative in accounts.js: ', values);
//console.log('convertToNative in accounts.js: ', values);
// main dashboard chart:
this.loadChart();

View File

@@ -35,11 +35,21 @@ export default () => ({
loading: false,
boxData: null,
boxOptions: null,
eventListeners: {
['@convert-to-native.window'](event){
this.convertToNative = event.detail;
this.accountList = [];
console.log('I heard that!');
this.boxData = null;
this.loadBoxes();
}
},
getFreshData() {
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
// TODO cache key is hard coded, problem?
const boxesCacheKey = getCacheKey('ds_boxes_data', {start: start, end: end});
const boxesCacheKey = getCacheKey('ds_boxes_data', {convertToNative: this.convertToNative, start: start, end: end});
cleanupCache();
//const cacheValid = window.store.get('cacheValid');

View File

@@ -166,6 +166,11 @@ export default () => ({
// properties of the transaction, used in the generation of the chart:
let transaction = group.attributes.transactions[ii];
let currencyCode = this.convertToNative ? transaction.native_currency_code : transaction.currency_code;
if(this.convertToNative && (!transaction.hasOwnProperty('native_amount') || null === transaction.native_amount)) {
// skip this transaction, it has no native amount.
console.error('No native amount for transaction #' + group.id + ' ('+this.convertToNative+')');
continue;
}
let amount = this.convertToNative ? parseFloat(transaction.native_amount) : parseFloat(transaction.amount);
let flowKey;
@@ -313,7 +318,7 @@ export default () => ({
downloadTransactions(params) {
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const cacheKey = getCacheKey(SANKEY_CACHE_KEY, {start: start, end: end});
const cacheKey = getCacheKey(SANKEY_CACHE_KEY, {convertToNative: this.convertToNative, start: start, end: end});
//console.log('Downloading page ' + params.page + '...');
const getter = new Get();
@@ -353,9 +358,6 @@ export default () => ({
this.convertToNative = values[0];
convertToNative = values[0];
this.convertToNative = false;
convertToNative = false;
// some translations:
translations.all_money = i18next.t('firefly.all_money');
translations.category = i18next.t('firefly.category');

View File

@@ -28,7 +28,7 @@ export function setVariable(name, value = null) {
// then again, it's not that slow.
// set in window.x
// window[name] = value;
window[name] = value;
// set in store:
window.store.set(name, value);
@@ -36,11 +36,14 @@ export function setVariable(name, value = null) {
// post to user preferences (because why not):
let putter = new Put();
putter.put(name, value).then((response) => {
}).catch(() => {
console.log('set "'+name+'" to value: ', value);
}).catch((error) => {
console.error(error);
// preference does not exist (yet).
// POST it
let poster = (new Post);
poster.post(name, value).then((response) => {
console.log('POST "'+name+'" to value: ', value);
});
});
}

View File

@@ -9,24 +9,6 @@
<div class="card-body p-0" style="position: relative;height:400px;">
<canvas id="account-chart"></canvas>
</div>
<template x-if="convertToNativeAvailable">
<div class="card-footer text-end">
<template x-if="convertToNative">
<button type="button" @click="switchConvertToNative"
class="btn btn-outline-info btm-sm">
<span
class="fa-solid fa-comments-dollar"></span> {{ __('firefly.disable_auto_convert') }}
</button>
</template>
<template x-if="!convertToNative">
<button type="button" @click="switchConvertToNative"
class="btn btn-outline-info btm-sm">
<span
class="fa-solid fa-comments-dollar"></span> {{ __('firefly.enable_auto_convert') }}
</button>
</template>
</div>
</template>
</div>
</div>

View File

@@ -1,4 +1,4 @@
<div class="row mb-2" x-data="boxes">
<div class="row mb-2" x-data="boxes" x-bind="eventListeners">
<div class="col-xl-3 col-lg-6 col-md-12 col-sm-12">
<div class="small-box text-bg-primary">
<div class="inner balance-box">