Files
firefly-iii/frontend/src/components/store/modules/transactions/create.js

258 lines
8.0 KiB
JavaScript
Raw Normal View History

2020-12-24 18:48:00 +01:00
/*
* create.js
* 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/>.
*/
2020-12-30 18:43:16 +01:00
const lodashClonedeep = require('lodash.clonedeep');
2020-12-24 18:48:00 +01:00
// initial state
const state = () => ({
transactionType: 'any',
2021-01-17 19:52:53 +01:00
date: new Date,
2021-01-31 07:26:52 +01:00
groupTitle: '',
transactions: [],
2020-12-30 18:43:16 +01:00
allowedOpposingTypes: {},
accountToTransaction: {},
2021-01-03 07:11:44 +01:00
sourceAllowedTypes: ['Asset account', 'Loan', 'Debt', 'Mortgage', 'Revenue account'],
destinationAllowedTypes: ['Asset account', 'Loan', 'Debt', 'Mortgage', 'Expense account'],
customDateFields: {
interest_date: false,
book_date: false,
process_date: false,
due_date: false,
payment_date: false,
invoice_date: false,
},
2021-01-31 07:26:52 +01:00
defaultErrors: {
description: [],
amount: [],
source: [],
destination: [],
currency: [],
foreign_currency: [],
foreign_amount: [],
date: [],
custom_dates: [],
budget: [],
category: [],
bill: [],
tags: [],
piggy_bank: []
},
defaultTransaction: {
// basic
description: '',
// accounts:
source_account: {
id: 0,
name: "",
2020-12-30 18:43:16 +01:00
name_with_balance: "",
type: "",
currency_id: 0,
currency_name: '',
currency_code: '',
currency_decimal_places: 2
},
2020-12-30 18:43:16 +01:00
destination_account: {
id: 0,
name: "",
type: "",
currency_id: 0,
currency_name: '',
currency_code: '',
currency_decimal_places: 2
},
2021-01-03 07:11:44 +01:00
// amount:
amount: '',
currency_id: 0,
foreign_amount: '',
foreign_currency_id: 0,
// meta data
2021-01-30 12:24:55 +01:00
category: null,
2021-01-03 07:11:44 +01:00
budget_id: 0,
2021-01-17 08:52:40 +01:00
bill_id: 0,
piggy_bank_id: 0,
tags: [],
2021-01-03 07:11:44 +01:00
// optional date fields (6x):
interest_date: null,
book_date: null,
process_date: null,
due_date: null,
payment_date: null,
invoice_date: null,
2021-01-17 08:52:40 +01:00
// optional other fields:
internal_reference: null,
external_url: null,
2021-01-30 12:24:55 +01:00
external_id: null,
2021-01-26 19:57:20 +01:00
notes: null,
// transaction links:
links: [],
2021-01-30 12:24:55 +01:00
attachments: [],
// error handling
2021-01-31 07:26:52 +01:00
errors: {},
},
}
)
2020-12-24 18:48:00 +01:00
// getters
const getters = {
transactions: state => {
return state.transactions;
},
2021-01-17 19:52:53 +01:00
date: state => {
return state.date;
},
2021-01-31 07:26:52 +01:00
groupTitle: state => {
return state.groupTitle;
},
2020-12-24 18:48:00 +01:00
transactionType: state => {
return state.transactionType;
},
defaultTransaction: state => {
return state.defaultTransaction;
},
sourceAllowedTypes: state => {
return state.sourceAllowedTypes;
},
2020-12-30 18:43:16 +01:00
destinationAllowedTypes: state => {
return state.destinationAllowedTypes;
},
allowedOpposingTypes: state => {
return state.allowedOpposingTypes;
},
2021-01-03 07:11:44 +01:00
customDateFields: state => {
return state.customDateFields;
}
2020-12-24 18:48:00 +01:00
// // `getters` is localized to this module's getters
// // you can use rootGetters via 4th argument of getters
// someGetter (state, getters, rootState, rootGetters) {
// getters.someOtherGetter // -> 'foo/someOtherGetter'
// rootGetters.someOtherGetter // -> 'someOtherGetter'
// rootGetters['bar/someOtherGetter'] // -> 'bar/someOtherGetter'
// },
}
// actions
2020-12-30 18:43:16 +01:00
const actions = {
calcTransactionType(context) {
let source = context.state.transactions[0].source_account;
let dest = context.state.transactions[0].destination_account;
if (null === source || null === dest) {
// console.log('transactionType any');
context.commit('setTransactionType', 'any');
return;
}
if ('' === source.type || '' === dest.type) {
// console.log('transactionType any');
context.commit('setTransactionType', 'any');
return;
}
// ok so type is set on both:
let expectedDestinationTypes = context.state.accountToTransaction[source.type];
if ('undefined' !== typeof expectedDestinationTypes) {
let transactionType = expectedDestinationTypes[dest.type];
if ('undefined' !== typeof expectedDestinationTypes[dest.type]) {
// console.log('Found a type: ' + transactionType);
context.commit('setTransactionType', transactionType);
return;
}
}
// console.log('Found no type for ' + source.type + ' --> ' + dest.type);
2021-01-03 07:11:44 +01:00
if ('Asset account' !== source.type) {
2020-12-30 18:43:16 +01:00
console.log('Drop ID from source. TODO');
2021-01-17 08:52:40 +01:00
2020-12-30 18:43:16 +01:00
// source.id =null
// context.commit('updateField', {field: 'source_account',index: })
// context.state.transactions[0].source_account.id = null;
}
2021-01-03 07:11:44 +01:00
if ('Asset account' !== dest.type) {
2020-12-30 18:43:16 +01:00
console.log('Drop ID from destination. TODO');
//context.state.transactions[0].destination_account.id = null;
}
context.commit('setTransactionType', 'any');
}
}
2020-12-24 18:48:00 +01:00
// mutations
const mutations = {
addTransaction(state) {
2020-12-30 18:43:16 +01:00
let newTransaction = lodashClonedeep(state.defaultTransaction);
2021-01-31 07:26:52 +01:00
newTransaction.errors = lodashClonedeep(state.defaultErrors);
2020-12-30 18:43:16 +01:00
state.transactions.push(newTransaction);
2020-12-25 12:49:28 +01:00
},
2021-01-31 07:26:52 +01:00
resetErrors(state, payload) {
console.log('resetErrors for index ' + payload.index);
state.transactions[payload.index].errors = lodashClonedeep(state.defaultErrors);
},
2021-01-17 19:52:53 +01:00
setDate(state, payload) {
state.date = payload.date;
},
2021-01-31 07:26:52 +01:00
setGroupTitle(state, payload) {
state.groupTitle = payload.groupTitle;
},
2021-01-03 07:11:44 +01:00
setCustomDateFields(state, payload) {
state.customDateFields = payload;
},
2021-01-17 08:52:40 +01:00
deleteTransaction(state, payload) {
state.transactions.splice(payload.index, 1);
2020-12-30 18:43:16 +01:00
},
setTransactionType(state, transactionType) {
state.transactionType = transactionType;
},
setAllowedOpposingTypes(state, allowedOpposingTypes) {
state.allowedOpposingTypes = allowedOpposingTypes;
},
setAccountToTransaction(state, payload) {
state.accountToTransaction = payload;
2020-12-25 12:49:28 +01:00
},
updateField(state, payload) {
state.transactions[payload.index][payload.field] = payload.value;
2020-12-30 18:43:16 +01:00
},
2021-01-31 07:26:52 +01:00
setTransactionError(state, payload) {
console.log('Will set transactions[' + payload.index + '][errors][' + payload.field + '] to ');
console.log(payload.errors);
state.transactions[payload.index].errors[payload.field] = payload.errors;
},
2020-12-30 18:43:16 +01:00
setDestinationAllowedTypes(state, payload) {
// console.log('Destination allowed types was changed!');
state.destinationAllowedTypes = payload;
},
setSourceAllowedTypes(state, payload) {
state.sourceAllowedTypes = payload;
2020-12-24 18:48:00 +01:00
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}