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

561 lines
19 KiB
Vue
Raw Normal View History

2020-11-12 05:58:06 +01:00
<!--
- Create.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>
2020-12-24 18:48:00 +01:00
<div>
2021-01-17 08:52:40 +01:00
<div class="row" v-for="(transaction, index) in this.transactions">
2020-12-24 18:48:00 +01:00
<div class="col">
<div class="card">
<div class="card-header">
2020-12-25 12:49:28 +01:00
<h3 class="card-title">
2021-01-30 12:24:55 +01:00
<span v-if="1 === transactions.length">{{ $t('firefly.create_new_transaction') }}</span>
<span v-if="transactions.length > 1">{{ $t('firefly.single_split') }} {{ index + 1 }} / {{ transactions.length }}</span>
2020-12-25 12:49:28 +01:00
</h3>
<div v-if="transactions.length > 1" class="card-tools">
2021-01-17 08:52:40 +01:00
<button class="btn btn-xs btn-danger" type="button" v-on:click="removeTransaction(index)"><i
2020-12-25 12:49:28 +01:00
class="fa fa-trash"></i></button>
</div>
2020-12-24 18:48:00 +01:00
</div>
<!-- /.card-header -->
<div class="card-body">
2021-01-30 12:24:55 +01:00
<h5>{{ $t('firefly.basic_journal_information') }}</h5>
<!-- description etc, 3 rows -->
<div class="row">
<div class="col">
<TransactionDescription
2021-01-17 19:52:53 +01:00
v-model="transaction.description"
:index="index"
></TransactionDescription>
</div>
</div>
<!-- source and destination -->
<div class="row">
<div class="col-xl-5 col-lg-5 col-md-10 col-sm-12 col-xs-12">
<!-- SOURCE -->
<TransactionAccount
2021-01-17 19:52:53 +01:00
v-model="transaction.source_account"
direction="source"
:index="index"
2020-12-30 18:43:16 +01:00
/>
</div>
<!-- switcharoo! -->
<div class="col-xl-2 col-lg-2 col-md-2 col-sm-12 text-center d-none d-sm-block">
2020-12-30 18:43:16 +01:00
<SwitchAccount
2021-01-03 07:11:44 +01:00
:index="index"
2020-12-30 18:43:16 +01:00
/>
</div>
<!-- destination -->
<div class="col-xl-5 col-lg-5 col-md-12 col-sm-12 col-xs-12">
2020-12-30 18:43:16 +01:00
<!-- DESTINATION -->
<TransactionAccount
2021-01-17 19:52:53 +01:00
v-model="transaction.destination_account"
2020-12-30 18:43:16 +01:00
direction="destination"
:index="index"
/>
</div>
</div>
<!-- amount -->
<div class="row">
<div class="col-xl-5 col-lg-5 col-md-10 col-sm-12 col-xs-12">
2020-12-30 18:43:16 +01:00
<!-- AMOUNT -->
2021-01-03 07:11:44 +01:00
<TransactionAmount :index="index"/>
<!--
2020-12-30 18:43:16 +01:00
-->
</div>
<div class="col-xl-2 col-lg-2 col-md-2 col-sm-12 text-center d-none d-sm-block">
2021-01-03 07:11:44 +01:00
<TransactionForeignCurrency :index="index"/>
2020-11-12 05:58:06 +01:00
</div>
<div class="col-xl-5 col-lg-5 col-md-12 col-sm-12 col-xs-12">
2021-01-03 07:11:44 +01:00
<TransactionForeignAmount :index="index"/>
</div>
</div>
<!-- dates -->
<div class="row">
2020-12-30 18:43:16 +01:00
<div class="col-xl-5 col-lg-5 col-md-12 col-sm-12 col-xs-12">
<TransactionDate
:index="index"
/>
</div>
2020-12-30 18:43:16 +01:00
<div class="col-xl-5 col-lg-5 col-md-12 col-sm-12 col-xs-12 offset-xl-2 offset-lg-2">
2021-01-20 18:44:28 +01:00
<TransactionCustomDates :index="index" :enabled-dates="customDateFields"/>
</div>
</div>
<h4>{{ $t('firefly.transaction_journal_meta') }}</h4>
<!-- meta -->
<div class="row">
<div class="col">
<TransactionBudget
2021-01-17 19:52:53 +01:00
v-model="transaction.budget_id"
:index="index"
/>
2021-01-17 08:52:40 +01:00
<TransactionCategory
2021-01-17 19:52:53 +01:00
v-model="transaction.category"
2021-01-17 08:52:40 +01:00
:index="index"
/>
</div>
<div class="col">
2021-01-17 08:52:40 +01:00
<TransactionBill
2021-01-17 19:52:53 +01:00
v-model="transaction.bill_id"
2021-01-17 08:52:40 +01:00
:index="index"
/>
<TransactionTags
:index="index"
2021-01-17 19:52:53 +01:00
v-model="transaction.tags"
2021-01-17 08:52:40 +01:00
/>
<TransactionPiggyBank
:index="index"
2021-01-17 19:52:53 +01:00
v-model="transaction.piggy_bank_id"
2021-01-17 08:52:40 +01:00
/>
2020-11-12 05:58:06 +01:00
</div>
</div>
<h4>{{ $t('firefly.transaction_journal_extra') }}</h4>
<div class="row">
<div class="col">
2021-01-17 08:52:40 +01:00
<TransactionInternalReference
:index="index"
2021-01-17 19:52:53 +01:00
v-model="transaction.internal_reference"
2021-01-17 08:52:40 +01:00
/>
<TransactionExternalUrl
:index="index"
2021-01-17 19:52:53 +01:00
v-model="transaction.external_url"
2021-01-17 08:52:40 +01:00
/>
<TransactionNotes
:index="index"
2021-01-17 19:52:53 +01:00
v-model="transaction.notes"
2021-01-17 08:52:40 +01:00
/>
</div>
<div class="col">
2021-01-26 19:57:20 +01:00
<TransactionAttachments
:index="index"
v-model="transaction.attachments"
/>
<TransactionLinks :index="index"
v-model="transaction.links"
/>
</div>
</div>
2021-01-17 19:52:53 +01:00
2020-11-12 05:58:06 +01:00
</div>
2020-12-24 18:48:00 +01:00
<!-- /.card-body -->
2020-11-12 05:58:06 +01:00
</div>
</div>
2020-12-24 18:48:00 +01:00
</div>
<!-- buttons -->
<!-- button -->
<div class="row">
<div class="col">
<button @click="addTransaction" class="btn btn-primary">{{ $t('firefly.add_another_split') }}</button>
2020-11-12 05:58:06 +01:00
</div>
2020-12-24 18:48:00 +01:00
<div class="col">
<p class="float-right">
<button @click="submitTransaction" :disabled="isSubmitting" class="btn btn-success">Store transaction</button>
<br/>
</p>
</div>
</div>
<div class="row">
<div class="col float-right">
<p class="text-right">
<small class="text-muted">Create another another another <input type="checkbox"/></small><br/>
<small class="text-muted">Return here <input type="checkbox"/></small><br/>
</p>
2020-11-12 05:58:06 +01:00
</div>
</div>
</div>
2021-01-20 18:44:28 +01:00
2020-11-12 05:58:06 +01:00
</template>
<script>
2021-01-17 08:52:40 +01:00
import {createNamespacedHelpers} from 'vuex'
2020-12-24 18:48:00 +01:00
import TransactionDescription from "./TransactionDescription";
import TransactionDate from "./TransactionDate";
import TransactionBudget from "./TransactionBudget";
import TransactionAccount from "./TransactionAccount";
2020-12-30 18:43:16 +01:00
import SwitchAccount from "./SwitchAccount";
import TransactionAmount from "./TransactionAmount";
2021-01-03 07:11:44 +01:00
import TransactionForeignAmount from "./TransactionForeignAmount";
import TransactionForeignCurrency from "./TransactionForeignCurrency";
import TransactionCustomDates from "./TransactionCustomDates";
2021-01-17 08:52:40 +01:00
import TransactionCategory from "./TransactionCategory";
import TransactionBill from "./TransactionBill";
import TransactionTags from "./TransactionTags";
import TransactionPiggyBank from "./TransactionPiggyBank";
import TransactionInternalReference from "./TransactionInternalReference";
import TransactionExternalUrl from "./TransactionExternalUrl";
import TransactionNotes from "./TransactionNotes";
2021-01-26 19:57:20 +01:00
import TransactionLinks from "./TransactionLinks";
import TransactionAttachments from "./TransactionAttachments";
2020-12-24 18:48:00 +01:00
const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('transactions/create')
2020-11-12 05:58:06 +01:00
export default {
2020-12-24 18:48:00 +01:00
name: "Create",
2021-01-03 07:11:44 +01:00
components: {
2021-01-26 19:57:20 +01:00
TransactionAttachments,
2021-01-17 08:52:40 +01:00
TransactionNotes,
TransactionExternalUrl,
TransactionInternalReference,
TransactionPiggyBank,
TransactionTags,
2021-01-26 19:57:20 +01:00
TransactionLinks,
2021-01-17 08:52:40 +01:00
TransactionBill,
TransactionCategory,
2021-01-03 07:11:44 +01:00
TransactionCustomDates,
TransactionForeignCurrency,
TransactionForeignAmount, TransactionAmount, SwitchAccount, TransactionAccount, TransactionBudget, TransactionDescription, TransactionDate
},
2020-12-24 18:48:00 +01:00
created() {
2020-12-30 18:43:16 +01:00
this.storeAllowedOpposingTypes();
this.storeAccountToTransaction();
2021-01-03 07:11:44 +01:00
this.storeCustomDateFields();
2020-12-24 18:48:00 +01:00
this.addTransaction();
},
data() {
return {
groupTitle: '',
2021-01-26 19:57:20 +01:00
isSubmitting: false,
linkSearchResults: [],
2021-01-30 12:24:55 +01:00
errorMessage: null,
successMessage: null,
2020-12-24 18:48:00 +01:00
}
},
computed: {
...mapGetters([
'transactionType', // -> this.someGetter
'transactions', // -> this.someOtherGetter
2021-01-17 19:52:53 +01:00
'customDateFields',
'date'
2020-12-24 18:48:00 +01:00
])
},
methods: {
...mapMutations(
[
'addTransaction',
2020-12-30 18:43:16 +01:00
'deleteTransaction',
'setAllowedOpposingTypes',
'setAccountToTransaction',
],
2020-12-24 18:48:00 +01:00
),
2021-01-20 18:44:28 +01:00
removeTransaction: function (index) {
2021-01-17 08:52:40 +01:00
// store.commit('addCustomer'
this.$store.commit('transactions/create/deleteTransaction', {index: index});
},
2021-01-03 07:11:44 +01:00
storeCustomDateFields: function () {
// TODO may include all custom fields in the future.
axios.get('./api/v1/preferences/transaction_journal_optional_fields').then(response => {
let fields = response.data.data.attributes.data;
let allDateFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date'];
let selectedDateFields = {
interest_date: false,
book_date: false,
process_date: false,
due_date: false,
payment_date: false,
invoice_date: false,
};
for (let key in fields) {
if (fields.hasOwnProperty(key)) {
2021-01-17 08:52:40 +01:00
if (-1 !== allDateFields.indexOf(key)) {
2021-01-03 07:11:44 +01:00
selectedDateFields[key] = fields[key];
}
}
}
this.$store.commit('transactions/create/setCustomDateFields', selectedDateFields);
});
},
2020-12-30 18:43:16 +01:00
/**
*
*/
storeAllowedOpposingTypes: function () {
this.setAllowedOpposingTypes(window.allowedOpposingTypes);
},
2021-01-03 07:11:44 +01:00
storeAccountToTransaction: function () {
2020-12-30 18:43:16 +01:00
this.setAccountToTransaction(window.accountToTransaction);
},
2020-12-24 18:48:00 +01:00
/**
*
*/
submitTransaction: function () {
this.isSubmitting = true;
2020-12-30 18:43:16 +01:00
// console.log('Now in submit()');
2021-01-30 12:24:55 +01:00
const url = './api/v1/transactions';
2020-12-24 18:48:00 +01:00
const data = this.convertData();
2021-01-30 12:24:55 +01:00
console.log('Submitting:');
2020-12-24 18:48:00 +01:00
console.log(data);
2021-01-30 12:24:55 +01:00
axios.post(url, data)
.then(response => {
console.log('Axios post OK');
})
.catch(error => {
console.log('Error in transaction submission.');
this.parseErrors(error.response.data);
});
2020-12-24 18:48:00 +01:00
this.isSubmitting = false;
},
2021-01-30 12:24:55 +01:00
parseErrors: function(errors) {
// set the error message:
this.successMessage = null;
this.errorMessage = this.$t('firefly.errors_submission');
if (typeof errors.errors === 'undefined') {
this.successMessage = null;
this.errorMessage = errors.message;
}
let transactionIndex;
let fieldName;
// fairly basic way of exploding the error array.
for (const key in errors.errors) {
if (errors.errors.hasOwnProperty(key)) {
if (key === 'group_title') {
this.group_title_errors = errors.errors[key];
}
if (key !== 'group_title') {
// lol dumbest way to explode "transactions.0.something" ever.
transactionIndex = parseInt(key.split('.')[1]);
fieldName = key.split('.')[2];
// set error in this object thing.
switch (fieldName) {
case 'amount':
case 'date':
case 'budget_id':
case 'bill_id':
case 'description':
case 'tags':
//this.transactions[transactionIndex].errors[fieldName] = errors.errors[key];
break;
case 'source_name':
case 'source_id':
//this.transactions[transactionIndex].errors.source_account = this.transactions[transactionIndex].errors.source_account.concat(errors.errors[key]);
break;
case 'destination_name':
case 'destination_id':
//this.transactions[transactionIndex].errors.destination_account = this.transactions[transactionIndex].errors.destination_account.concat(errors.errors[key]);
break;
case 'foreign_amount':
case 'foreign_currency_id':
//this.transactions[transactionIndex].errors.foreign_amount = this.transactions[transactionIndex].errors.foreign_amount.concat(errors.errors[key]);
break;
}
}
// unique some things
if (typeof this.transactions[transactionIndex] !== 'undefined') {
//this.transactions[transactionIndex].errors.source_account = Array.from(new Set(this.transactions[transactionIndex].errors.source_account));
//this.transactions[transactionIndex].errors.destination_account = Array.from(new Set(this.transactions[transactionIndex].errors.destination_account));
}
}
}
},
2020-12-24 18:48:00 +01:00
/**
*
*/
convertData: function () {
2021-01-30 12:24:55 +01:00
console.log('now in convertData');
2020-12-24 18:48:00 +01:00
let data = {
//'group_title': null,
'transactions': []
};
for (let key in this.transactions) {
if (this.transactions.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
data.transactions.push(this.convertSplit(key, this.transactions[key]));
}
}
return data;
},
/**
*
* @param key
* @param array
*/
convertSplit: function (key, array) {
2021-01-30 12:24:55 +01:00
console.log('now in convertSplit');
2020-12-24 18:48:00 +01:00
let currentSplit = {
// basic
description: array.description,
2021-01-30 12:24:55 +01:00
date: this.toW3CString(this.date),
type: this.transactionType,
2020-12-30 18:43:16 +01:00
// account
source_id: array.source_account.id ?? null,
source_name: array.source_account.name ?? null,
destination_id: array.destination_account.id ?? null,
destination_name: array.destination_account.name ?? null,
2021-01-03 07:11:44 +01:00
// amount:
currency_id: array.currency_id,
amount: array.amount,
foreign_currency_id: array.foreign_currency_id,
foreign_amount: array.foreign_amount,
2021-01-17 08:52:40 +01:00
// meta data
budget_id: array.budget_id,
2021-01-30 12:24:55 +01:00
category_name: array.category,
2021-01-17 08:52:40 +01:00
bill_id: array.bill_id,
tags: array.tags,
piggy_bank_id: array.piggy_bank_id,
// optional date fields (6x):
interest_date: array.interest_date,
book_date: array.book_date,
process_date: array.process_date,
due_date: array.due_date,
payment_date: array.payment_date,
invoice_date: array.invoice_date,
// other optional fields:
internal_reference: array.internal_reference,
external_url: array.external_url,
notes: array.notes,
2021-01-30 12:24:55 +01:00
external_id: array.external_id,
2021-01-26 19:57:20 +01:00
2021-01-30 12:24:55 +01:00
// from thing:
order: 0,
reconciled: false,
2020-12-24 18:48:00 +01:00
};
2021-01-26 19:57:20 +01:00
2021-01-30 12:24:55 +01:00
// do transaction type
let transactionType;
let firstSource;
let firstDestination;
// get transaction type from first transaction
transactionType = this.transactionType ? this.transactionType.toLowerCase() : 'invalid';
// if the transaction type is invalid, might just be that we can deduce it from
// the presence of a source or destination account
firstSource = this.transactions[0].source_account.type;
firstDestination = this.transactions[0].destination_account.type;
// console.log('Type of first source is ' + firstSource);
if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstSource)) {
transactionType = 'withdrawal';
}
if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstDestination)) {
transactionType = 'deposit';
}
currentSplit.type = transactionType;
let links = [];
for (let i in array.links) {
if (array.links.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = array.links[i];
let linkTypeParts = current.link_type_id.split('-');
let inwardId = 'inward' === linkTypeParts[1] ? 0 : parseInt(current.transaction_journal_id);
let outwardId = 'outward' === linkTypeParts[1] ? 0 : parseInt(current.transaction_journal_id);
let newLink = {
link_type_id: parseInt(linkTypeParts[0]),
inward_id: inwardId,
outward_id: outwardId,
};
links.push(newLink);
2021-01-26 19:57:20 +01:00
}
}
2021-01-30 12:24:55 +01:00
currentSplit.links = links;
2020-12-24 18:48:00 +01:00
// return it.
return currentSplit;
2021-01-30 12:24:55 +01:00
},
toW3CString: function (date) {
// https://gist.github.com/tristanlins/6585391
let year = date.getFullYear();
let month = date.getMonth();
month++;
if (month < 10) {
month = '0' + month;
}
let day = date.getDate();
if (day < 10) {
day = '0' + day;
}
let hours = date.getHours();
if (hours < 10) {
hours = '0' + hours;
}
let minutes = date.getMinutes();
if (minutes < 10) {
minutes = '0' + minutes;
}
let seconds = date.getSeconds();
if (seconds < 10) {
seconds = '0' + seconds;
}
let offset = -date.getTimezoneOffset();
let offsetHours = Math.abs(Math.floor(offset / 60));
let offsetMinutes = Math.abs(offset) - offsetHours * 60;
if (offsetHours < 10) {
offsetHours = '0' + offsetHours;
}
if (offsetMinutes < 10) {
offsetMinutes = '0' + offsetMinutes;
}
let offsetSign = '+';
if (offset < 0) {
offsetSign = '-';
}
return year + '-' + month + '-' + day +
'T' + hours + ':' + minutes + ':' + seconds +
offsetSign + offsetHours + ':' + offsetMinutes;
2020-12-24 18:48:00 +01:00
}
// addTransactionToArray: function (e) {
// console.log('Now in addTransactionToArray()');
// this.$store.
//
// this.transactions.push({
// description: '',
// });
// if (e) {
// e.preventDefault();
// }
},
2020-11-12 05:58:06 +01:00
}
</script>
<style scoped>
</style>