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

150 lines
5.5 KiB
Vue
Raw Normal View History

<!--
- Amount.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>
2019-05-24 05:29:04 +02:00
<div class="form-group" v-bind:class="{ 'has-error': hasError()}">
2019-12-30 15:24:02 +01:00
<div class="col-sm-8 col-sm-offset-4 text-sm">
{{ $t('firefly.amount') }}
</div>
<label class="col-sm-4 control-label" ref="cur"></label>
<div class="col-sm-8">
2020-03-13 06:22:50 +01:00
<div class="input-group">
<input type="number"
@input="handleInput"
ref="amount"
:value="value"
step="any"
class="form-control"
name="amount[]"
:title="$t('firefly.amount')"
autocomplete="off"
v-bind:placeholder="$t('firefly.amount')">
<span class="input-group-btn">
<button
v-on:click="clearAmount"
tabIndex="-1"
class="btn btn-default"
type="button"><i class="fa fa-trash-o"></i></button>
</span>
</div>
</div>
2020-03-13 06:22:50 +01: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: "Amount",
2019-05-24 05:29:04 +02:00
props: ['source', 'destination', 'transactionType', 'value', 'error'],
data() {
return {
sourceAccount: this.source,
destinationAccount: this.destination,
2019-05-11 05:32:09 +02:00
type: this.transactionType
}
},
methods: {
2019-05-11 05:32:09 +02:00
handleInput(e) {
this.$emit('input', this.$refs.amount.value);
},
2020-03-13 06:22:50 +01:00
clearAmount: function () {
this.$refs.amount.value = '';
this.$emit('input', this.$refs.amount.value);
// some event?
this.$emit('clear:amount')
},
2019-05-24 05:29:04 +02:00
hasError: function () {
return this.error.length > 0;
},
changeData: function () {
2019-06-01 20:38:18 +02:00
let transactionType = this.transactionType;
2019-05-25 09:17:46 +02:00
// reset of all are empty:
2019-06-01 20:38:18 +02:00
if (!transactionType && !this.source.name && !this.destination.name) {
2019-05-25 09:17:46 +02:00
$(this.$refs.cur).text('');
2019-12-20 07:15:40 +01:00
2019-05-25 09:17:46 +02:00
return;
}
2019-06-01 20:38:18 +02:00
if(null === transactionType) {
transactionType = '';
}
if ('' === transactionType && '' !== this.source.currency_name) {
2019-05-25 09:17:46 +02:00
$(this.$refs.cur).text(this.source.currency_name);
return;
}
2019-06-01 20:38:18 +02:00
if ('' === transactionType && '' !== this.destination.currency_name) {
2019-05-25 09:17:46 +02:00
$(this.$refs.cur).text(this.destination.currency_name);
return;
}
2020-03-11 10:39:53 +01:00
// for normal transactions, the source leads the currency
2019-12-20 07:15:40 +01:00
if (transactionType.toLowerCase() === 'withdrawal' ||
transactionType.toLowerCase() === 'reconciliation' ||
transactionType.toLowerCase() === 'transfer') {
2019-05-25 09:17:46 +02:00
$(this.$refs.cur).text(this.source.currency_name);
return;
}
2020-03-11 10:39:53 +01:00
// 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()
)
) {
2019-05-25 09:17:46 +02:00
$(this.$refs.cur).text(this.destination.currency_name);
}
2020-03-11 10:39:53 +01:00
// 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 () {
this.changeData();
},
destination: function () {
this.changeData();
},
transactionType: function () {
this.changeData();
}
},
mounted() {
this.changeData();
}
}
</script>
<style scoped>
</style>