2020-12-29 08:33:46 +01:00
|
|
|
<!--
|
|
|
|
|
- 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
|
|
|
|
2020-12-29 08:33:46 +01:00
|
|
|
</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"
|
2020-12-29 08:33:46 +01:00
|
|
|
: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 + '[]'"
|
2020-12-29 08:33:46 +01:00
|
|
|
: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"
|
2020-12-29 08:33:46 +01:00
|
|
|
>
|
2021-02-22 18:43:26 +01:00
|
|
|
|
|
|
|
|
<template slot="suggestion" slot-scope="{ data, htmlText }">
|
2021-02-26 06:39:20 +01:00
|
|
|
<div :title="data.type" class="d-flex">
|
2021-02-22 18:43:26 +01:00
|
|
|
<span v-html="htmlText"></span><br>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
2020-12-29 08:33:46 +01:00
|
|
|
<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>
|
2020-12-29 08:33:46 +01:00
|
|
|
</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>
|
2020-12-29 08:33:46 +01:00
|
|
|
</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: () => ([])
|
|
|
|
|
},
|
2021-04-03 18:48:21 +02:00
|
|
|
transactionType: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: 'any'
|
|
|
|
|
},
|
2021-03-19 09:19:37 +01:00
|
|
|
},
|
2020-12-29 08:33:46 +01:00
|
|
|
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,
|
2021-02-22 18:43:26 +01:00
|
|
|
accountName: '',
|
|
|
|
|
selectedAccountTrigger: false,
|
2020-12-29 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
},
|
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();
|
|
|
|
|
},
|
2020-12-29 08:33:46 +01:00
|
|
|
methods: {
|
2020-12-30 18:43:16 +01:00
|
|
|
getACURL: function (types, query) {
|
2021-02-22 18:43:26 +01:00
|
|
|
return './api/v1/autocomplete/accounts?types=' + types.join(',') + '&query=' + query;
|
2020-12-30 18:43:16 +01:00
|
|
|
},
|
|
|
|
|
clearAccount: function () {
|
|
|
|
|
this.accounts = this.initialSet;
|
2021-02-22 18:43:26 +01:00
|
|
|
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
|
|
|
},
|
2020-12-29 08:33:46 +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) {
|
2020-12-29 08:33:46 +01:00
|
|
|
// 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-29 08:33:46 +01:00
|
|
|
}
|
2021-02-26 06:39:20 +01:00
|
|
|
// console.log(this.direction + ': Will search for types:');
|
|
|
|
|
// console.log(this.accountTypes);
|
2020-12-29 08:33:46 +01:00
|
|
|
|
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))
|
2020-12-29 08:33:46 +01:00
|
|
|
.then(response => {
|
2021-02-17 06:46:26 +01:00
|
|
|
//console.log('Got a response!');
|
2020-12-29 08:33:46 +01:00
|
|
|
this.accounts = response.data;
|
2021-02-17 06:46:26 +01:00
|
|
|
//console.log(response.data);
|
2020-12-29 08:33:46 +01:00
|
|
|
})
|
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
|
|
|
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) {
|
2021-02-22 18:43:26 +01:00
|
|
|
this.selectedAccountTrigger = true;
|
2021-02-14 19:13:42 +01:00
|
|
|
this.account = value;
|
2021-02-22 18:43:26 +01:00
|
|
|
|
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;
|
2021-02-22 18:43:26 +01:00
|
|
|
},
|
|
|
|
|
accountName: function (value) {
|
2021-04-08 11:21:20 +02:00
|
|
|
|
|
|
|
|
if('source' === this.direction && 'Deposit' !== this.transactionType) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if('destination' === this.direction && 'Withdrawal' !== this.transactionType) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-22 18:43:26 +01:00
|
|
|
if (false === this.selectedAccountTrigger) {
|
2021-04-08 11:21:20 +02:00
|
|
|
console.log('User submitted manual thing.');
|
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,
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-02-22 18:43:26 +01:00
|
|
|
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-22 18:43:26 +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);
|
2021-02-22 18:43:26 +01:00
|
|
|
this.account = value;
|
|
|
|
|
this.selectedAccountTrigger = true;
|
2021-02-25 06:27:43 +01:00
|
|
|
this.accountName = value.name ?? '';
|
2021-02-22 18:43:26 +01:00
|
|
|
}
|
2020-12-29 08:33:46 +01:00
|
|
|
},
|
|
|
|
|
computed: {
|
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;
|
|
|
|
|
}
|
2021-04-03 18:48:21 +02:00
|
|
|
// console.log('Direction of account ' + this.index + ' is ' + this.direction + '(' + this.transactionType + ')');
|
|
|
|
|
// console.log(this.transactionType);
|
2021-02-10 06:55:56 +01:00
|
|
|
if ('source' === this.direction) {
|
2021-04-03 18:48:21 +02:00
|
|
|
return 'any' === this.transactionType || 'Deposit' === this.transactionType || typeof this.transactionType === 'undefined';
|
2021-02-10 06:55:56 +01:00
|
|
|
}
|
|
|
|
|
if ('destination' === this.direction) {
|
2021-04-03 18:48:21 +02:00
|
|
|
return 'any' === this.transactionType || 'Withdrawal' === this.transactionType || typeof this.transactionType === 'undefined';
|
2021-02-10 06:55:56 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|