Files
firefly-iii/frontend/src/components/transactions/TransactionAccount.vue

262 lines
8.7 KiB
Vue
Raw Normal View History

<!--
- TransactionAccount.vue
- Copyright (c) 2020 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/>.
-->
<template>
<div class="form-group">
2021-02-10 06:55:56 +01:00
<div class="text-xs d-none d-lg-block d-xl-block" v-if="visible">
<span v-if="0 === this.index">{{ $t('firefly.' + this.direction + '_account') }}</span>
<span class="text-warning" v-if="this.index > 0">{{ $t('firefly.first_split_overrules_' + this.direction) }}</span>
</div>
<div class="text-xs d-none d-lg-block d-xl-block" v-if="!visible">
&nbsp;
</div>
<vue-typeahead-bootstrap
2021-02-10 06:55:56 +01:00
v-if="visible"
2021-02-14 19:13:42 +01:00
v-model="accountName"
:data="accounts"
2020-12-30 18:43:16 +01:00
:showOnFocus=true
2021-01-31 07:26:52 +01:00
:inputClass="errors.length > 0 ? 'is-invalid' : ''"
2020-12-30 18:43:16 +01:00
:inputName="direction + '[]'"
:serializer="item => item.name_with_balance"
:minMatchingChars="3"
2021-02-10 06:55:56 +01:00
:placeholder="$t('firefly.' + direction + '_account')"
@input="lookupAccount"
2021-01-17 19:52:53 +01:00
@hit="selectedAccount = $event"
>
<template slot="suggestion" slot-scope="{ data, htmlText }">
<div class="d-flex" :title="data.type">
<span v-html="htmlText"></span><br>
</div>
</template>
<template slot="append">
<div class="input-group-append">
2021-02-10 06:55:56 +01:00
<button tabindex="-1" class="btn btn-outline-secondary" v-on:click="clearAccount" type="button"><i class="far fa-trash-alt"></i></button>
</div>
</template>
</vue-typeahead-bootstrap>
2021-02-10 06:55:56 +01:00
<div class="form-control-static" v-if="!visible">
<span class="small text-muted"><em>{{ $t('firefly.first_split_decides') }}</em></span>
</div>
2021-01-31 07:26:52 +01:00
<span v-if="errors.length > 0">
<span v-for="error in errors" class="text-danger small">{{ error }}<br/></span>
</span>
</div>
</template>
<script>
import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';
import {debounce} from 'lodash';
import {createNamespacedHelpers} from "vuex";
const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('transactions/create')
export default {
name: "TransactionAccount",
components: {VueTypeaheadBootstrap},
2021-01-31 07:26:52 +01:00
props: ['index', 'direction', 'value', 'errors'],
data() {
return {
query: '',
accounts: [],
2020-12-30 18:43:16 +01:00
accountTypes: [],
2021-01-17 19:52:53 +01:00
initialSet: [],
2021-02-14 19:13:42 +01:00
selectedAccount: {},
account: this.value,
accountName: '',
selectedAccountTrigger: false,
}
},
2020-12-30 18:43:16 +01:00
created() {
this.createInitialSet();
},
methods: {
...mapMutations(
[
'updateField',
2020-12-30 18:43:16 +01:00
'setDestinationAllowedTypes',
'setSourceAllowedTypes'
],
),
2020-12-30 18:43:16 +01:00
getACURL: function (types, query) {
return './api/v1/autocomplete/accounts?types=' + types.join(',') + '&query=' + query;
2020-12-30 18:43:16 +01:00
},
clearAccount: function () {
this.accounts = this.initialSet;
this.account = {name: '', type: 'no_type', id: null, currency_id: null, currency_code: null, currency_symbol: null};
2021-02-14 19:13:42 +01:00
this.accountName = '';
2020-12-30 18:43:16 +01:00
},
lookupAccount: debounce(function () {
2021-02-17 06:46:26 +01:00
//console.log('In lookupAccount()');
2020-12-30 18:43:16 +01:00
if (0 === this.accountTypes.length) {
// set the types from the default types for this direction:
2020-12-30 18:43:16 +01:00
this.accountTypes = 'source' === this.direction ? this.sourceAllowedTypes : this.destinationAllowedTypes;
}
2020-12-30 18:43:16 +01:00
// update autocomplete URL:
2021-02-17 06:46:26 +01:00
axios.get(this.getACURL(this.accountTypes, this.accountName))
.then(response => {
2021-02-17 06:46:26 +01:00
//console.log('Got a response!');
this.accounts = response.data;
2021-02-17 06:46:26 +01:00
//console.log(response.data);
})
2020-12-30 18:43:16 +01:00
}, 300),
2021-02-14 19:13:42 +01:00
2020-12-30 18:43:16 +01:00
createInitialSet: function () {
let types = this.sourceAllowedTypes;
if ('destination' === this.direction) {
types = this.destinationAllowedTypes;
}
axios.get(this.getACURL(types, ''))
.then(response => {
this.accounts = response.data;
this.initialSet = response.data;
});
}
},
watch: {
selectedAccount: function (value) {
//console.log('Emit on selected account');
this.selectedAccountTrigger = true;
2021-02-14 19:13:42 +01:00
this.account = value;
this.$emit(this.emitAccountId, value.id);
this.$emit(this.emitAccountType, value.type);
this.$emit(this.emitAccountName, value.name);
this.$emit(this.emitAccountCurrencyId, value.currency_id);
this.$emit(this.emitAccountCurrencyCode, value.currency_code);
this.$emit(this.emitAccountCurrencySymbol, value.currency_symbol);
//this.$emit(this.emitAccount, value);
2021-02-17 06:46:26 +01:00
this.accountName = this.account.name_with_balance;
// call method to set what the opposing accounts should be.
// and what the
},
accountName: function (value) {
if (false === this.selectedAccountTrigger) {
console.log('Save to change name!');
this.$emit(this.emitAccountId, null);
this.$emit(this.emitAccountType, null);
this.$emit(this.emitAccountName, value);
this.$emit(this.emitAccountCurrencyId, null);
this.$emit(this.emitAccountCurrencyCode, null);
this.$emit(this.emitAccountCurrencySymbol, null);
//this.$emit(this.emitAccount, {name: value, type: null, id: null, currency_id: null, currency_code: null, currency_symbol: null});
// also reset local account thing, but dont be weird about it
this.accountTrigger = false;
this.account = {name: value, type: null, id: null, currency_id: null, currency_code: null, currency_symbol: null};
}
this.selectedAccountTrigger = false;
2021-01-17 19:52:53 +01:00
},
2021-02-14 19:13:42 +01:00
account: function (value) {
//this.updateField({field: this.accountKey, index: this.index, value: value});
2020-12-30 18:43:16 +01:00
// set the opposing account allowed set.
let opposingAccounts = [];
let type = value.type ? value.type : 'no_type';
if ('undefined' !== typeof this.allowedOpposingTypes[this.direction]) {
if ('undefined' !== typeof this.allowedOpposingTypes[this.direction][type]) {
opposingAccounts = this.allowedOpposingTypes[this.direction][type];
}
}
if ('source' === this.direction) {
this.setDestinationAllowedTypes(opposingAccounts);
}
if ('destination' === this.direction) {
this.setSourceAllowedTypes(opposingAccounts);
}
},
value: function (value) {
console.log(this.direction + ' account overruled by external forces.');
this.account = value;
this.selectedAccountTrigger = true;
this.accountName = value.name;
}
},
computed: {
...mapGetters([
'transactionType',
2020-12-30 18:43:16 +01:00
'sourceAllowedTypes',
'destinationAllowedTypes',
'allowedOpposingTypes'
]),
2020-12-30 18:43:16 +01:00
accountKey: {
get() {
return 'source' === this.direction ? 'source_account' : 'destination_account';
}
},
emitAccountId: {
get() {
return 'set-' + this.direction + '-account-id';
}
},
emitAccount: {
get() {
return 'set-' + this.direction + '-account';
}
},
emitAccountName: {
get() {
return 'set-' + this.direction + '-account-name';
}
},
emitAccountType: {
get() {
return 'set-' + this.direction + '-account-type';
}
},
emitAccountCurrencyId: {
get() {
return 'set-' + this.direction + '-account-currency-id';
}
},
emitAccountCurrencyCode: {
get() {
return 'set-' + this.direction + '-account-currency-code';
}
},
emitAccountCurrencySymbol: {
get() {
return 'set-' + this.direction + '-account-currency-symbol';
}
},
2021-02-10 06:55:56 +01:00
visible: {
get() {
// index 0 is always visible:
if (0 === this.index) {
return true;
}
if ('source' === this.direction) {
return 'any' === this.transactionType || 'Deposit' === this.transactionType
}
if ('destination' === this.direction) {
return 'any' === this.transactionType || 'Withdrawal' === this.transactionType;
}
return false;
}
}
}
}
</script>