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

264 lines
8.5 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-26 06:39:20 +01:00
<div v-if="visible" class="text-xs d-none d-lg-block d-xl-block">
2021-02-10 06:55:56 +01:00
<span v-if="0 === this.index">{{ $t('firefly.' + this.direction + '_account') }}</span>
2021-02-26 06:39:20 +01:00
<span v-if="this.index > 0" class="text-warning">{{ $t('firefly.first_split_overrules_' + this.direction) }}</span>
2021-02-10 06:55:56 +01:00
</div>
2021-02-26 06:39:20 +01:00
<div v-if="!visible" class="text-xs d-none d-lg-block d-xl-block">
2021-02-10 06:55:56 +01:00
&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"
2021-01-31 07:26:52 +01:00
:inputClass="errors.length > 0 ? 'is-invalid' : ''"
2020-12-30 18:43:16 +01:00
:inputName="direction + '[]'"
:minMatchingChars="3"
2021-02-10 06:55:56 +01:00
:placeholder="$t('firefly.' + direction + '_account')"
2021-02-26 06:39:20 +01:00
:serializer="item => item.name_with_balance"
:showOnFocus=true
2021-01-17 19:52:53 +01:00
@hit="selectedAccount = $event"
2021-02-26 06:39:20 +01:00
@input="lookupAccount"
>
<template slot="suggestion" slot-scope="{ data, htmlText }">
2021-02-26 06:39:20 +01:00
<div :title="data.type" class="d-flex">
<span v-html="htmlText"></span><br>
</div>
</template>
<template slot="append">
<div class="input-group-append">
2021-02-26 06:39:20 +01:00
<button class="btn btn-outline-secondary" tabindex="-1" type="button" v-on:click="clearAccount"><i class="far fa-trash-alt"></i></button>
</div>
</template>
</vue-typeahead-bootstrap>
2021-02-26 06:39:20 +01:00
<div v-if="!visible" class="form-control-static">
2021-02-10 06:55:56 +01:00
<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';
export default {
name: "TransactionAccount",
components: {VueTypeaheadBootstrap},
2021-03-19 09:19:37 +01:00
props: {
index: {
type: Number,
},
direction: {
type: String,
},
value: {
type: Object,
default: () => ({})
},
errors: {
type: Array,
default: () => ([])
},
sourceAllowedTypes: {
type: Array,
default: () => ([])
},
destinationAllowedTypes: {
type: Array,
default: () => ([])
},
allowedOpposingTypes: {
type: Object,
default: () => ({})
},
},
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() {
2021-03-01 15:27:27 +01:00
this.selectedAccountTrigger = true;
this.accountName = this.account.name ?? '';
2020-12-30 18:43:16 +01:00
this.createInitialSet();
},
methods: {
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;
}
2021-02-26 06:39:20 +01:00
// console.log(this.direction + ': Will search for types:');
// console.log(this.accountTypes);
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;
}
2021-02-26 06:39:20 +01:00
// console.log(this.direction + ' initial set searches for');
// console.log(types);
2020-12-30 18:43:16 +01:00
axios.get(this.getACURL(types, ''))
.then(response => {
this.accounts = response.data;
this.initialSet = response.data;
});
}
},
watch: {
2021-02-26 06:39:20 +01:00
// allowedOpposingTypes: function () {
// console.log(this.direction + ' account noticed change in allowedOpposingTypes');
// },
sourceAllowedTypes: function (value) {
// console.log(this.direction + ' account noticed change in sourceAllowedTypes');
// console.log(value);
this.createInitialSet();
},
destinationAllowedTypes: function (value) {
// console.log(this.direction + ' account noticed change in destinationAllowedTypes');
// console.log(value);
this.createInitialSet();
},
2020-12-30 18:43:16 +01:00
selectedAccount: function (value) {
this.selectedAccountTrigger = true;
2021-02-14 19:13:42 +01:00
this.account = value;
2021-02-22 19:58:10 +01:00
this.$emit('set-account',
{
index: this.index,
direction: this.direction,
id: value.id,
type: value.type,
name: value.name,
currency_id: value.currency_id,
currency_code: value.currency_code,
currency_symbol: value.currency_symbol,
}
);
this.accountName = this.account.name_with_balance;
},
accountName: function (value) {
if (false === this.selectedAccountTrigger) {
2021-02-25 06:27:43 +01:00
// console.log('Save to change name!');
2021-02-22 19:58:10 +01:00
this.$emit('set-account',
{
index: this.index,
direction: this.direction,
id: null,
type: null,
name: value,
currency_id: null,
currency_code: null,
currency_symbol: null,
}
);
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) {
2020-12-30 18:43:16 +01:00
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) {
2021-02-26 06:39:20 +01:00
this.$emit('set-dest-types', opposingAccounts);
2020-12-30 18:43:16 +01:00
}
if ('destination' === this.direction) {
2021-02-26 06:39:20 +01:00
this.$emit('set-src-types', opposingAccounts);
2020-12-30 18:43:16 +01:00
}
},
value: function (value) {
2021-02-25 06:27:43 +01:00
// console.log('Index ' + this.index + ' nwAct: ', value);
// console.log(this.direction + ' account overruled by external forces.');
// console.log(value);
this.account = value;
this.selectedAccountTrigger = true;
2021-02-25 06:27:43 +01:00
this.accountName = value.name ?? '';
}
},
computed: {
2021-02-26 06:39:20 +01:00
// 'transactionType',
// 'sourceAllowedTypes',
// 'destinationAllowedTypes',
// 'allowedOpposingTypes'
2020-12-30 18:43:16 +01:00
accountKey: {
get() {
return 'source' === this.direction ? 'source_account' : 'destination_account';
}
},
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>