Expand currency options in transaction form.

This commit is contained in:
James Cole 2023-10-29 05:24:57 +01:00
parent f1a8d3cc81
commit 447ec63299
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 61 additions and 13 deletions

View File

@ -67,22 +67,31 @@ let transactions = function () {
if (sourceType === destType && ['Asset account', 'Loan', 'Debt', 'Mortgage'].includes(sourceType)) {
this.transactionType = 'transfer';
console.log('Transaction type is detected to be "' + this.transactionType + '".');
// this also locks the amount into the amount of the source account
// and the foreign amount (if different) in that of the destination account.
console.log('filter down currencies for transfer.');
return;
}
// withdrawals:
if ('Asset account' === sourceType && ['Expense account', 'Debt', 'Loan', 'Mortgage'].includes(destType)) {
this.transactionType = 'withdrawal';
console.log('Transaction type is detected to be "' + this.transactionType + '".');
console.log('[a] Transaction type is detected to be "' + this.transactionType + '".');
this.filterNativeCurrencies(this.entries[0].source_account.currency_code);
return;
}
if ('Asset account' === sourceType && 'unknown' === destType) {
this.transactionType = 'withdrawal';
console.log('Transaction type is detected to be "' + this.transactionType + '".');
console.log('[b] Transaction type is detected to be "' + this.transactionType + '".');
console.log(this.entries[0].source_account);
this.filterNativeCurrencies(this.entries[0].source_account.currency_code);
return;
}
if (['Debt', 'Loan', 'Mortgage'].includes(sourceType) && 'Expense account' === destType) {
this.transactionType = 'withdrawal';
console.log('Transaction type is detected to be "' + this.transactionType + '".');
console.log('[c] Transaction type is detected to be "' + this.transactionType + '".');
this.filterNativeCurrencies(this.entries[0].source_account.currency_code);
return;
}
@ -105,9 +114,33 @@ let transactions = function () {
{
id: item.id,
name: item.name,
alpine_name: item.name,
type: item.type,
currency_code: item.currency_code,
};
console.log('Changed source account into a known ' + item.type.toLowerCase());
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
},
filterNativeCurrencies(code) {
console.log('filterNativeCurrencies("' + code + '")');
let list = [];
let currency;
for (let i in this.enabledCurrencies) {
if (this.enabledCurrencies.hasOwnProperty(i)) {
let current = this.enabledCurrencies[i];
if (current.code === code) {
currency = current;
}
}
}
list.push(currency);
this.nativeCurrencies = list;
// this also forces the currency_code on ALL entries.
for(let i in this.entries) {
if(this.entries.hasOwnProperty(i)) {
this.entries[i].currency_code = code;
}
}
},
changedAmount(e) {
const index = parseInt(e.target.dataset.index);
@ -126,9 +159,12 @@ let transactions = function () {
{
id: item.id,
name: item.name,
alpine_name: item.name,
type: item.type,
currency_code: item.currency_code,
};
console.log('Changed destination account into a known ' + item.type.toLowerCase());
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
},
loadCurrencies() {
console.log('Loading user currencies.');
@ -138,10 +174,10 @@ let transactions = function () {
};
let getter = new Get();
getter.list({}).then((response) => {
for(let i in response.data.data) {
if(response.data.data.hasOwnProperty(i)) {
for (let i in response.data.data) {
if (response.data.data.hasOwnProperty(i)) {
let current = response.data.data[i];
if(current.attributes.enabled) {
if (current.attributes.enabled) {
let obj =
{
@ -153,10 +189,11 @@ let transactions = function () {
decimal_places: current.attributes.decimal_places,
};
if(obj.default) {
if (obj.default) {
this.defaultCurrency = obj;
}
this.enabledCurrencies.push(obj);
this.nativeCurrencies.push(obj);
}
}
}
@ -165,33 +202,43 @@ let transactions = function () {
});
},
changeSourceAccount(item, ac) {
console.log('changeSourceAccount');
if (typeof item === 'undefined') {
const index = parseInt(ac._searchInput.attributes['data-index'].value);
let source = document.querySelector('#form')._x_dataStack[0].$data.entries[index].source_account;
if (source.name === ac._searchInput.value) {
console.warn('Ignore hallucinated source account name change to "' + ac._searchInput.value + '"');
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
return;
}
document.querySelector('#form')._x_dataStack[0].$data.entries[index].source_account =
{
name: ac._searchInput.value,
alpine_name: ac._searchInput.value,
};
console.log('Changed source account into a unknown account called "' + ac._searchInput.value + '"');
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
}
},
changeDestAccount(item, ac) {
let destination = document.querySelector('#form')._x_dataStack[0].$data.entries[0].destination_account;
if (typeof item === 'undefined') {
const index = parseInt(ac._searchInput.attributes['data-index'].value);
let destination = document.querySelector('#form')._x_dataStack[0].$data.entries[index].destination_account;
if (destination.name === ac._searchInput.value) {
console.warn('Ignore hallucinated destination account name change to "' + ac._searchInput.value + '"');
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
return;
}
document.querySelector('#form')._x_dataStack[0].$data.entries[index].destination_account =
{
name: ac._searchInput.value,
alpine_name: ac._searchInput.value,
};
console.log('Changed destination account into a unknown account called "' + ac._searchInput.value + '"');
document.querySelector('#form')._x_dataStack[0].detectTransactionType();
}
},

View File

@ -25,6 +25,7 @@ function getAccount() {
return {
id: '',
name: '',
alpine_name: '',
};
}

View File

@ -38,10 +38,10 @@ export function parseFromEntries(entries, transactionType) {
current.currency_code = entry.currency_code;
// if ID is set:
if ('' !== entry.source_account.id.toString()) {
if (typeof entry.source_account.id !== 'undefined' && '' !== entry.source_account.id.toString()) {
current.source_id = entry.source_account.id;
}
if ('' !== entry.destination_account.id.toString()) {
if (typeof entry.destination_account.id !== 'undefined' && '' !== entry.destination_account.id.toString()) {
current.destination_id = entry.destination_account.id;
}

View File

@ -98,7 +98,7 @@
<input type="text"
class="form-control ac-source"
:id="'source_' + index"
x-model="transaction.source_account.name"
x-model="transaction.source_account.alpine_name"
:data-index="index"
placeholder="{{ __('firefly.source_account') }}">
</div>
@ -112,7 +112,7 @@
<input type="text"
class="form-control ac-dest"
:id="'dest_' + index"
x-model="transaction.destination_account.name"
x-model="transaction.destination_account.alpine_name"
:data-index="index"
placeholder="{{ __('firefly.destination_account') }}">
</div>
@ -148,7 +148,7 @@
<select class="form-control" :id="'currency_code_' + index"
x-model="transaction.currency_code"
>
<template x-for="currency in enabledCurrencies">
<template x-for="currency in nativeCurrencies">
<option :selected="currency.id == defaultCurrency.id" :label="currency.name" :value="currency.code" x-text="currency.name"></option>
</template>
</select>
@ -160,7 +160,7 @@
:data-index="index"
x-model="transaction.amount"
@change="changedAmount"
placeholder="Amount">
placeholder="0.00">
</div>
</div>
</div>