Files
firefly-iii/resources/assets/js/components/transactions/ForeignAmountSelect.vue

190 lines
8.0 KiB
Vue
Raw Normal View History

<!--
- ForeignAmountSelect.vue
2020-01-25 06:08:56 +01:00
- Copyright (c) 2019 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>
2020-02-09 09:32:33 +01:00
<!--
Show if:
- more than one currency enabled in system.
-->
2019-11-09 15:30:52 +01:00
<div class="form-group" v-bind:class="{ 'has-error': hasError()}" v-if="
2020-02-09 09:32:33 +01:00
this.enabledCurrencies.length > 1">
2019-12-30 15:24:02 +01:00
<div class="col-sm-8 col-sm-offset-4 text-sm">
{{ $t('form.foreign_amount') }}
</div>
<div class="col-sm-4">
2019-09-06 17:18:33 +02:00
<select class="form-control" ref="currency_select" name="foreign_currency[]" @input="handleInput">
2019-05-11 05:32:09 +02:00
<option
v-for="currency in this.enabledCurrencies"
v-if="currency.enabled"
:value="currency.id"
:label="currency.name"
2019-09-14 18:06:46 +02:00
:selected="value.currency_id === currency.id"
2019-05-11 05:32:09 +02:00
>
{{ currency.name }}
</option>
</select>
</div>
<div class="col-sm-8">
2019-05-11 05:32:09 +02:00
<input type="number" @input="handleInput" ref="amount" :value="value.amount" step="any" class="form-control"
name="foreign_amount[]" v-if="this.enabledCurrencies.length > 0"
2019-10-05 13:11:12 +02:00
:title="this.title" autocomplete="off" :placeholder="this.title">
2019-05-24 05:29:04 +02:00
<ul class="list-unstyled" v-for="error in this.error">
<li class="text-danger">{{ error }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
name: "ForeignAmountSelect",
2019-11-09 15:30:52 +01:00
props: ['source', 'destination', 'transactionType', 'value', 'error', 'no_currency', 'title',],
mounted() {
2019-11-09 16:50:13 +01:00
//console.log('loadCurrencies()');
2019-11-09 15:30:52 +01:00
this.liability = false;
this.loadCurrencies();
},
data() {
return {
currencies: [],
enabledCurrencies: [],
2019-11-09 15:30:52 +01:00
exclude: null,
// liability overrules the drop down list if the source or dest is a liability
liability: false
}
},
watch: {
source: function () {
2019-11-09 16:50:13 +01:00
// console.log('watch source in foreign currency');
this.changeData();
},
destination: function () {
2019-11-09 16:50:13 +01:00
// console.log('watch destination in foreign currency');
this.changeData();
},
transactionType: function () {
2019-11-09 16:50:13 +01:00
// console.log('watch transaction type in foreign currency');
this.changeData();
}
},
methods: {
2019-05-24 05:29:04 +02:00
hasError: function () {
2019-11-09 16:50:13 +01:00
// console.log('Has error');
2019-05-24 05:29:04 +02:00
return this.error.length > 0;
},
2019-05-11 05:32:09 +02:00
handleInput(e) {
2019-11-09 16:50:13 +01:00
// console.log('handleInput');
2019-11-09 15:30:52 +01:00
let obj = {
amount: this.$refs.amount.value,
2019-05-11 05:32:09 +02:00
currency_id: this.$refs.currency_select.value,
2019-11-09 15:30:52 +01:00
};
2019-11-09 16:50:13 +01:00
// console.log(obj);
2019-11-09 15:30:52 +01:00
this.$emit('input', obj
2019-05-11 05:32:09 +02:00
);
},
changeData: function () {
2019-11-09 16:50:13 +01:00
//console.log('Now in changeData()');
this.enabledCurrencies = [];
2019-11-09 15:30:52 +01:00
let destType = this.destination.type ? this.destination.type.toLowerCase() : 'invalid';
let srcType = this.source.type ? this.source.type.toLowerCase() : 'invalid';
let tType =this.transactionType ? this.transactionType.toLowerCase() : 'invalid';
let liabilities = ['loan','debt','mortgage'];
let sourceIsLiability = liabilities.indexOf(srcType) !== -1;
let destIsLiability = liabilities.indexOf(destType) !== -1;
2019-11-09 16:50:13 +01:00
// console.log(srcType + ' (source) is a liability: ' + sourceIsLiability);
// console.log(destType + ' (dest) is a liability: ' + destIsLiability);
2019-11-09 15:30:52 +01:00
if (tType === 'transfer' || destIsLiability || sourceIsLiability) {
2019-11-09 16:50:13 +01:00
//console.log('Source is liability OR dest is liability, OR transfer. Lock list on currency of destination.');
2019-11-09 15:30:52 +01:00
this.liability = true;
// lock dropdown list on on currencyID of destination.
for (const key in this.currencies) {
if (this.currencies.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
if (this.currencies[key].id === this.destination.currency_id) {
this.enabledCurrencies.push(this.currencies[key]);
}
}
}
2019-11-09 16:50:13 +01:00
//console.log('Enabled currencies length is now ' + this.enabledCurrencies.length);
return;
}
2019-11-09 15:30:52 +01:00
// if type is withdrawal, list all but skip the source account ID.
2019-11-09 15:30:52 +01:00
if (tType === 'withdrawal' && this.source && false === sourceIsLiability) {
for (const key in this.currencies) {
if (this.currencies.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
if (this.source.currency_id !== this.currencies[key].id) {
this.enabledCurrencies.push(this.currencies[key]);
}
}
}
return;
}
// if type is deposit, list all but skip the source account ID.
2019-11-09 15:30:52 +01:00
if (tType === 'deposit' && this.destination) {
for (const key in this.currencies) {
if (this.currencies.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
if (this.destination.currency_id !== this.currencies[key].id) {
this.enabledCurrencies.push(this.currencies[key]);
}
}
}
return;
}
for (const key in this.currencies) {
if (this.currencies.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
this.enabledCurrencies.push(this.currencies[key]);
}
}
},
loadCurrencies: function () {
2019-11-09 16:50:13 +01:00
// console.log('loadCurrencies');
let URI = document.getElementsByTagName('base')[0].href + "json/currencies";
axios.get(URI, {}).then((res) => {
this.currencies = [
{
name: this.no_currency,
id: 0,
enabled: true
}
];
for (const key in res.data) {
if (res.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
2019-08-18 09:07:08 +02:00
if (res.data[key].enabled) {
this.currencies.push(res.data[key]);
this.enabledCurrencies.push(res.data[key]);
}
}
}
});
}
}
}
</script>
<style scoped>
</style>