Skip to content

Firefly III: a personal finances manager

Improve amount handling.

James Coled5e4e219e866
diff --git a/resources/assets/v1/src/components/transactions/Amount.vue b/resources/assets/v1/src/components/transactions/Amount.vue
index d1b358e311..14b9ad4c96 100644
--- a/resources/assets/v1/src/components/transactions/Amount.vue
+++ b/resources/assets/v1/src/components/transactions/Amount.vue
@@ -17,137 +17,3 @@
- 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/>.
-->
-
-<template>
- <div class="form-group" v-bind:class="{ 'has-error': hasError()}">
- <div class="col-sm-8 col-sm-offset-4 text-sm">
- {{ $t('firefly.amount') }}
- </div>
- <label ref="cur" class="col-sm-4 control-label"></label>
- <div class="col-sm-8">
- <div class="input-group">
- <input ref="amount" spellcheck="false"
- :title="$t('firefly.amount')"
- :value="value"
- autocomplete="off"
- class="form-control"
- name="amount[]"
- step="any"
- type="number"
- v-bind:placeholder="$t('firefly.amount')"
- @input="handleInput">
-
- <span class="input-group-btn">
- <button
- class="btn btn-default"
- tabIndex="-1"
- type="button"
- v-on:click="clearAmount"><i class="fa fa-trash-o"></i></button>
- </span>
- </div>
- </div>
-
- <ul v-for="error in this.error" class="list-unstyled">
- <li class="text-danger">{{ error }}</li>
- </ul>
- </div>
-</template>
-
-<script>
-export default {
- name: "Amount",
- props: ['source', 'destination', 'transactionType', 'value', 'error'],
- data() {
- return {
- sourceAccount: this.source,
- destinationAccount: this.destination,
- type: this.transactionType
- }
- },
- methods: {
- handleInput(e) {
- this.$emit('input', this.$refs.amount.value);
- },
- clearAmount: function () {
- this.$refs.amount.value = '';
- this.$emit('input', this.$refs.amount.value);
- // some event?
- this.$emit('clear:amount')
- },
- hasError: function () {
- return this.error.length > 0;
- },
- changeData: function () {
- //console.log('Triggered amount changeData()');
- let transactionType = this.transactionType;
- // reset of all are empty:
- if (!transactionType && !this.source.name && !this.destination.name) {
- $(this.$refs.cur).text('');
-
- return;
- }
- if (null === transactionType) {
- transactionType = '';
- }
- if ('' === transactionType && '' !== this.source.currency_name) {
- $(this.$refs.cur).text(this.source.currency_name);
- return;
- }
- if ('' === transactionType && '' !== this.destination.currency_name) {
- $(this.$refs.cur).text(this.destination.currency_name);
- return;
- }
- // for normal transactions, the source leads the currency
- if (transactionType.toLowerCase() === 'withdrawal' ||
- transactionType.toLowerCase() === 'reconciliation' ||
- transactionType.toLowerCase() === 'transfer') {
- $(this.$refs.cur).text(this.source.currency_name);
- return;
- }
- // for deposits, the destination leads the currency
- // but source must not be a liability
- if (transactionType.toLowerCase() === 'deposit'
- &&
- !('debt' === this.source.type.toLowerCase() ||
- 'loan' === this.source.type.toLowerCase() ||
- 'mortgage' === this.source.type.toLowerCase()
- )
- ) {
- $(this.$refs.cur).text(this.destination.currency_name);
- }
- // for deposits, the destination leads the currency
- // unless source is liability, then source leads:
- if (transactionType.toLowerCase() === 'deposit'
- &&
- ('debt' === this.source.type.toLowerCase() ||
- 'loan' === this.source.type.toLowerCase() ||
- 'mortgage' === this.source.type.toLowerCase()
- )
- ) {
- $(this.$refs.cur).text(this.source.currency_name);
- }
- }
- },
- watch: {
- source: function () {
- // console.log('amount: watch source triggered');
- this.changeData();
- },
- value: function () {
- // console.log('amount: value changed');
- },
- destination: function () {
- // console.log('amount: watch destination triggered');
- this.changeData();
- },
- transactionType: function () {
- // console.log('amount: watch transaction type triggered');
- this.changeData();
- }
- },
- mounted() {
- // console.log('amount: mounted');
- this.changeData();
- }
-}
-</script>
diff --git a/resources/assets/v3/js/pages/transactions/create.js b/resources/assets/v3/js/pages/transactions/create.js
index 36c6db541e..ecb0156afe 100644
--- a/resources/assets/v3/js/pages/transactions/create.js
+++ b/resources/assets/v3/js/pages/transactions/create.js
@@ -75,8 +75,9 @@ let create = function () {
// form data (except transactions) is stored in formData
formData: {
- primaryCurrency: null,
- defaultCurrency: null,
+ // primaryCurrency: null,
+ // defaultCurrency: null,
+ amountCurrency: null, // this is the currency of the amount field
enabledCurrencies: [],
primaryCurrencies: [],
foreignCurrencies: [],
@@ -164,7 +165,7 @@ let create = function () {
// 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.');
- this.filterPrimaryCurrencies(this.entries[0].source_account.currency_code);
+ this.determineAmountCurrency(this.entries[0].source_account.currency_code);
this.filterForeignCurrencies(this.entries[0].destination_account.currency_code);
this.disableSplitAccounts();
return;
@@ -173,22 +174,21 @@ let create = function () {
if ('Asset account' === sourceType && ['Expense account', 'Debt', 'Loan', 'Mortgage'].includes(destType)) {
this.groupProperties.transactionType = 'withdrawal';
console.log('[a] Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
- this.filterPrimaryCurrencies(this.entries[0].source_account.currency_code);
+ this.determineAmountCurrency(this.entries[0].source_account.currency_code);
this.disableSplitAccounts();
return;
}
if ('Asset account' === sourceType && 'unknown' === destType) {
this.groupProperties.transactionType = 'withdrawal';
console.log('[b] Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
- console.log(this.entries[0].source_account);
- this.filterPrimaryCurrencies(this.entries[0].source_account.currency_code);
+ this.determineAmountCurrency(this.entries[0].source_account.currency_code);
this.disableSplitAccounts();
return;
}
if (['Debt', 'Loan', 'Mortgage'].includes(sourceType) && 'Expense account' === destType) {
this.groupProperties.transactionType = 'withdrawal';
console.log('[c] Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
- this.filterPrimaryCurrencies(this.entries[0].source_account.currency_code);
+ this.determineAmountCurrency(this.entries[0].source_account.currency_code);
this.disableSplitAccounts();
return;
}
@@ -198,11 +198,13 @@ let create = function () {
this.groupProperties.transactionType = 'deposit';
console.log('Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
this.disableSplitAccounts();
+ this.determineAmountCurrency(this.entries[0].destination_account.currency_code);
return;
}
if ('unknown' === sourceType && ['Asset account', 'Debt', 'Loan', 'Mortgage'].includes(destType)) {
this.groupProperties.transactionType = 'deposit';
console.log('Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
+ this.determineAmountCurrency(this.entries[0].destination_account.currency_code);
this.disableSplitAccounts();
return;
}
@@ -210,12 +212,14 @@ let create = function () {
this.groupProperties.transactionType = 'deposit';
console.warn('FORCE transaction type to be "' + this.groupProperties.transactionType + '".');
this.entries[0].source_account.id = '';
+ this.determineAmountCurrency(this.entries[0].destination_account.currency_code);
this.disableSplitAccounts();
return;
}
if (['Debt', 'Loan', 'Mortgage'].includes(sourceType) && 'Asset account' === destType) {
this.groupProperties.transactionType = 'deposit';
console.log('Transaction type is detected to be "' + this.groupProperties.transactionType + '".');
+ this.determineAmountCurrency(this.entries[0].destination_account.currency_code);
this.disableSplitAccounts();
return;
}
@@ -261,7 +265,7 @@ let create = function () {
}
},
- filterPrimaryCurrencies(code) {
+ determineAmountCurrency(code) {
let list = [];
let currency;
for (let i in this.formData.enabledCurrencies) {
@@ -273,12 +277,13 @@ let create = function () {
}
}
list.push(currency);
- this.formData.primaryCurrencies = 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;
+ if(1 === list.length) {
+ this.formData.amountCurrency = list[0];
+ // 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;
+ }
}
}
},
@@ -342,8 +347,7 @@ let create = function () {
// load currencies and save in form data.
loadCurrencies().then(data => {
this.formStates.loadingCurrencies = false;
- this.formData.primaryCurrency = data.primaryCurrency;
- this.formData.defaultCurrency = data.defaultCurrency;
+ this.formData.amountCurrency = data.primaryCurrency;
this.formData.enabledCurrencies = data.enabledCurrencies;
this.formData.primaryCurrencies = data.primaryCurrencies;
this.formData.foreignCurrencies = data.foreignCurrencies;
diff --git a/resources/views/partials/form/transaction/amount.blade.php b/resources/views/partials/form/transaction/amount.blade.php
index d6a30f73e4..3e9d99482e 100644
--- a/resources/views/partials/form/transaction/amount.blade.php
+++ b/resources/views/partials/form/transaction/amount.blade.php
@@ -1,23 +1,8 @@
<div class="row mb-3">
- <!-- select for currency -->
- <div class="col-sm-3">
- <!-- is loading currencies -->
- <template x-if="formStates.loadingCurrencies">
- <span class="form-control-plaintext"><div class="spinner-border spinner-border-sm" role="status">
- <span class="visually-hidden">Loading...</span>
-</div></span>
- </template>
- <!-- is no longer loading currencies -->
- <template x-if="!formStates.loadingCurrencies">
- <select class="form-control" :id="'currency_code_' + index" x-model="transaction.currency_code">
- <template x-for="currency in formData.primaryCurrencies">
- <option :selected="currency.id == formData.primaryCurrency.id"
- :label="currency.name" :value="currency.code"
- x-text="currency.name"></option>
- </template>
- </select>
- </template>
- </div>
+ <!-- text label for currency -->
+ <label :for="'amount_' + index"
+ class="col-sm-3 col-form-label d-none d-sm-block" x-text="formData.amountCurrency ? formData.amountCurrency.name : ''">
+ </label>
<!-- actual amount -->
<div class="col-sm-9">
<input type="number" step="any" min="0"