2020-01-17 00:01:10 +00:00
|
|
|
import axios from 'axios'
|
|
|
|
import url from '@/plugins/routes'
|
|
|
|
|
|
|
|
const state = {
|
|
|
|
user: null,
|
2020-01-23 22:25:21 +00:00
|
|
|
creditList: [],
|
2020-01-26 22:55:59 +00:00
|
|
|
loading: false,
|
|
|
|
addLoading: false,
|
2020-01-27 18:56:56 +00:00
|
|
|
error: '',
|
2020-02-27 14:52:58 +00:00
|
|
|
days: [],
|
|
|
|
message: []
|
2020-01-17 00:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mutations = {
|
|
|
|
setUser: (state, user) => {
|
|
|
|
state.user = user
|
|
|
|
let list = {}
|
|
|
|
for (let creditList in user['creditList']) {
|
2020-01-23 12:24:12 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2020-01-17 00:01:10 +00:00
|
|
|
console.log(creditList)
|
|
|
|
let amount = mutations.createAmount(user['creditList'][creditList])
|
|
|
|
let credit = mutations.createCredit(user['creditList'][creditList])
|
|
|
|
let sum = mutations.createSum(credit, amount)
|
|
|
|
list[creditList] = [{ ...credit }, { ...amount }, { ...sum }]
|
|
|
|
}
|
|
|
|
state.user.creditList = list
|
|
|
|
state.creditList = []
|
2020-01-26 22:55:59 +00:00
|
|
|
state.error = ''
|
2020-01-23 12:24:12 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2020-01-17 00:01:10 +00:00
|
|
|
console.log(state.user)
|
|
|
|
},
|
|
|
|
createAmount(creditList) {
|
|
|
|
let amount = {
|
|
|
|
type: 'Schulden',
|
|
|
|
jan_amount: 0 - creditList.jan.depts,
|
|
|
|
feb_amount: 0 - creditList.feb.depts,
|
|
|
|
maer_amount: 0 - creditList.maer.depts,
|
|
|
|
apr_amount: 0 - creditList.apr.depts,
|
|
|
|
mai_amount: 0 - creditList.mai.depts,
|
|
|
|
jun_amount: 0 - creditList.jun.depts,
|
|
|
|
jul_amount: 0 - creditList.jul.depts,
|
|
|
|
aug_amount: 0 - creditList.aug.depts,
|
|
|
|
sep_amount: 0 - creditList.sep.depts,
|
|
|
|
okt_amount: 0 - creditList.okt.depts,
|
|
|
|
nov_amount: 0 - creditList.nov.depts,
|
|
|
|
dez_amount: 0 - creditList.dez.depts,
|
|
|
|
last: 0 - creditList['last']
|
|
|
|
}
|
|
|
|
|
|
|
|
amount.sum =
|
|
|
|
amount.jan_amount +
|
|
|
|
amount.feb_amount +
|
|
|
|
amount.maer_amount +
|
|
|
|
amount.apr_amount +
|
|
|
|
amount.mai_amount +
|
|
|
|
amount.jun_amount +
|
|
|
|
amount.jul_amount +
|
|
|
|
amount.aug_amount +
|
|
|
|
amount.sep_amount +
|
|
|
|
amount.okt_amount +
|
|
|
|
amount.nov_amount +
|
|
|
|
amount.dez_amount
|
|
|
|
return amount
|
|
|
|
},
|
|
|
|
createCredit(creditList) {
|
|
|
|
let credit = {
|
|
|
|
type: 'Guthaben',
|
|
|
|
jan_amount: creditList.jan.credit,
|
|
|
|
feb_amount: creditList.feb.credit,
|
|
|
|
maer_amount: creditList.maer.credit,
|
|
|
|
apr_amount: creditList.apr.credit,
|
|
|
|
mai_amount: creditList.mai.credit,
|
|
|
|
jun_amount: creditList.jun.credit,
|
|
|
|
jul_amount: creditList.jul.credit,
|
|
|
|
aug_amount: creditList.aug.credit,
|
|
|
|
sep_amount: creditList.sep.credit,
|
|
|
|
okt_amount: creditList.okt.credit,
|
|
|
|
nov_amount: creditList.nov.credit,
|
|
|
|
dez_amount: creditList.dez.credit
|
|
|
|
}
|
|
|
|
credit.sum =
|
|
|
|
credit.jan_amount +
|
|
|
|
credit.feb_amount +
|
|
|
|
credit.maer_amount +
|
|
|
|
credit.apr_amount +
|
|
|
|
credit.mai_amount +
|
|
|
|
credit.jun_amount +
|
|
|
|
credit.jul_amount +
|
|
|
|
credit.aug_amount +
|
|
|
|
credit.sep_amount +
|
|
|
|
credit.okt_amount +
|
|
|
|
credit.nov_amount +
|
|
|
|
credit.dez_amount
|
|
|
|
return credit
|
|
|
|
},
|
|
|
|
createSum(credit, amount) {
|
|
|
|
let sum = {
|
|
|
|
type: 'Summe',
|
|
|
|
jan_amount: credit.jan_amount + amount.jan_amount,
|
|
|
|
feb_amount: credit.feb_amount + amount.feb_amount,
|
|
|
|
maer_amount: credit.maer_amount + amount.maer_amount,
|
|
|
|
apr_amount: credit.apr_amount + amount.apr_amount,
|
|
|
|
mai_amount: credit.mai_amount + amount.mai_amount,
|
|
|
|
jun_amount: credit.jun_amount + amount.jun_amount,
|
|
|
|
jul_amount: credit.jul_amount + amount.jul_amount,
|
|
|
|
aug_amount: credit.aug_amount + amount.aug_amount,
|
|
|
|
sep_amount: credit.sep_amount + amount.sep_amount,
|
|
|
|
okt_amount: credit.okt_amount + amount.okt_amount,
|
|
|
|
nov_amount: credit.nov_amount + amount.nov_amount,
|
|
|
|
dez_amount: credit.dez_amount + amount.dez_amount
|
|
|
|
}
|
|
|
|
sum.sum =
|
|
|
|
sum.jan_amount +
|
|
|
|
sum.feb_amount +
|
|
|
|
sum.maer_amount +
|
|
|
|
sum.apr_amount +
|
|
|
|
sum.mai_amount +
|
|
|
|
sum.jun_amount +
|
|
|
|
sum.jul_amount +
|
|
|
|
sum.aug_amount +
|
|
|
|
sum.sep_amount +
|
|
|
|
sum.okt_amount +
|
|
|
|
sum.nov_amount +
|
|
|
|
sum.dez_amount
|
|
|
|
return sum
|
2020-01-23 22:25:21 +00:00
|
|
|
},
|
|
|
|
setLoading(state, value) {
|
|
|
|
state.loading = value
|
2020-01-26 22:55:59 +00:00
|
|
|
},
|
|
|
|
setAddLoading(state, value) {
|
|
|
|
state.addLoading = value
|
|
|
|
},
|
|
|
|
setError(state, value) {
|
|
|
|
state.error = value
|
|
|
|
console.log(state)
|
2020-01-27 18:56:56 +00:00
|
|
|
},
|
|
|
|
createDays(state, date) {
|
|
|
|
let days = []
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
|
|
|
days.push({
|
|
|
|
date: new Date(
|
|
|
|
date.getFullYear(),
|
|
|
|
date.getMonth(),
|
|
|
|
date.getDate() + i,
|
|
|
|
12
|
|
|
|
),
|
|
|
|
job: false,
|
|
|
|
workers: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
state.days = days
|
|
|
|
},
|
|
|
|
updateDay(state, data) {
|
|
|
|
const date = data.date
|
|
|
|
for (let day in state.days) {
|
|
|
|
if (state.days[day].date - date === 0) {
|
|
|
|
state.days[day].job = data.job
|
|
|
|
if (data.workers) state.days[day].workers = data.workers
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 14:52:58 +00:00
|
|
|
},
|
|
|
|
addMessage: (state, data) => {
|
|
|
|
var message = ''
|
|
|
|
if (data.error) {
|
|
|
|
message =
|
|
|
|
'Konnte ' +
|
|
|
|
(data.amount / 100).toFixed(2) +
|
|
|
|
'€ zu ' +
|
|
|
|
data.user.firstname +
|
|
|
|
' ' +
|
|
|
|
data.user.lastname +
|
|
|
|
' hinzufügen.'
|
|
|
|
} else {
|
|
|
|
message =
|
|
|
|
'' +
|
|
|
|
(data.amount / 100).toFixed(2) +
|
|
|
|
'€ wurde zu ' +
|
|
|
|
data.user.firstname +
|
|
|
|
' ' +
|
|
|
|
data.user.lastname +
|
|
|
|
' hinzugefügt.'
|
|
|
|
}
|
|
|
|
state.message.unshift({
|
|
|
|
message: message,
|
|
|
|
user: data.user,
|
|
|
|
error: data.error,
|
|
|
|
storno: false,
|
|
|
|
loading: false,
|
|
|
|
visible: true,
|
|
|
|
amount: data.amount,
|
|
|
|
date: new Date()
|
|
|
|
})
|
|
|
|
console.log(state.message)
|
|
|
|
},
|
|
|
|
updateMessage: (state, data) => {
|
|
|
|
var message = state.message.find(msg => {
|
|
|
|
return msg.date - data.date === 0 ? true : false
|
|
|
|
})
|
|
|
|
if (message) {
|
|
|
|
if (data.storno !== undefined) message.storno = data.storno
|
|
|
|
if (data.loading !== undefined) message.loading = data.loading
|
|
|
|
}
|
2020-01-17 00:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
async getUser({ commit, rootState, dispatch }) {
|
2020-01-26 11:55:39 +00:00
|
|
|
commit('setLoading', true)
|
2020-01-17 00:01:10 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.get(url.userMain, {
|
|
|
|
headers: { Token: rootState.login.user.accessToken }
|
|
|
|
})
|
|
|
|
commit('setUser', response.data)
|
2020-01-26 22:55:59 +00:00
|
|
|
commit('setError', '')
|
2020-01-17 00:01:10 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.response)
|
|
|
|
if (e.response.status === 401) dispatch('logout', null, { root: true })
|
|
|
|
}
|
2020-01-26 11:55:39 +00:00
|
|
|
commit('setLoading', false)
|
2020-01-17 00:01:10 +00:00
|
|
|
},
|
|
|
|
async addAmount({ commit, rootState, dispatch }, amount) {
|
2020-01-26 22:55:59 +00:00
|
|
|
commit('setAddLoading', true)
|
2020-01-17 00:01:10 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.post(
|
|
|
|
url.userAddAmount,
|
|
|
|
{ amount: amount },
|
|
|
|
{ headers: { Token: rootState.login.user.accessToken } }
|
|
|
|
)
|
2020-02-27 14:52:58 +00:00
|
|
|
console.log(response.data)
|
2020-01-17 00:01:10 +00:00
|
|
|
commit('setUser', response.data)
|
2020-02-27 14:52:58 +00:00
|
|
|
commit('addMessage', {
|
|
|
|
user: rootState.login.user,
|
|
|
|
amount: amount,
|
|
|
|
error: false
|
|
|
|
})
|
2020-01-26 22:55:59 +00:00
|
|
|
commit('setError', '')
|
2020-01-17 00:01:10 +00:00
|
|
|
} catch (e) {
|
2020-02-27 14:52:58 +00:00
|
|
|
commit('addMessage', {
|
|
|
|
user: rootState.login.user,
|
|
|
|
amount: amount,
|
|
|
|
error: true
|
|
|
|
})
|
2020-01-17 00:01:10 +00:00
|
|
|
if (e.response)
|
|
|
|
if (e.response.status === 401) dispatch('logout', null, { root: true })
|
|
|
|
}
|
2020-01-26 22:55:59 +00:00
|
|
|
commit('setAddLoading', false)
|
|
|
|
},
|
|
|
|
async saveConfig({ commit, rootState, dispatch }, data) {
|
|
|
|
commit('setLoading', true)
|
|
|
|
try {
|
2020-01-27 18:56:56 +00:00
|
|
|
const response = await axios.post(
|
|
|
|
url.user.config,
|
|
|
|
{ ...data },
|
|
|
|
{ headers: { Token: rootState.login.user.accessToken } }
|
|
|
|
)
|
2020-01-26 22:55:59 +00:00
|
|
|
console.log(response.data)
|
|
|
|
commit('setUser', response.data)
|
|
|
|
commit('setError', '')
|
|
|
|
} catch (e) {
|
|
|
|
if (e.response) {
|
2020-01-27 18:56:56 +00:00
|
|
|
if (e.response.status === 401) dispatch('logout', null, { root: true })
|
2020-01-26 22:55:59 +00:00
|
|
|
if (e.response.data) {
|
|
|
|
console.log(e.response.data)
|
|
|
|
commit('setError', e.response.data.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
commit('setLoading', false)
|
2020-01-27 18:56:56 +00:00
|
|
|
},
|
|
|
|
createDays({ commit }, date) {
|
|
|
|
commit('createDays', date)
|
|
|
|
},
|
|
|
|
async updateDay({ commit, rootState, dispatch }, data) {
|
|
|
|
commit('setLoading', true)
|
|
|
|
try {
|
|
|
|
console.log('hier bin ich')
|
|
|
|
const response = await axios.post(
|
|
|
|
url.user.job,
|
|
|
|
{ date: data.date.getTime() / 1000 },
|
|
|
|
{ headers: { Token: rootState.login.user.accessToken } }
|
|
|
|
)
|
|
|
|
console.log(response.data)
|
|
|
|
commit('updateDay', { ...response.data, date: data.date })
|
|
|
|
} catch (e) {
|
|
|
|
if (e.response) {
|
|
|
|
if (e.response.status === 401) dispatch('logout', null, { root: true })
|
|
|
|
if (e.response.data) {
|
|
|
|
console.log(e.response.data, data.date)
|
|
|
|
commit('setError', e.response.data.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
commit('setLoading', false)
|
2020-02-27 14:52:58 +00:00
|
|
|
},
|
|
|
|
async storno({ commit, rootState, dispatch }, data) {
|
|
|
|
commit('updateMessage', { date: data.date, loading: true })
|
|
|
|
try {
|
|
|
|
const response = await axios.post(
|
|
|
|
url.user.storno,
|
|
|
|
{
|
|
|
|
amount: data.amount
|
|
|
|
},
|
|
|
|
{ headers: { Token: rootState.login.user.accessToken } }
|
|
|
|
)
|
|
|
|
console.log(response.data)
|
|
|
|
commit('setUser', response.data)
|
|
|
|
commit('updateMessage', { date: data.date, storno: true })
|
|
|
|
} catch (e) {
|
|
|
|
if (e.response)
|
|
|
|
if (e.response.status === 401) dispatch('logout', null, { root: true })
|
|
|
|
}
|
|
|
|
commit('updateMessage', { date: data.date, loading: false })
|
2020-01-17 00:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getters = {
|
|
|
|
user: state => {
|
|
|
|
return state.user
|
|
|
|
},
|
|
|
|
year: state => {
|
|
|
|
let year = 0
|
|
|
|
for (let creditList in state.user.creditList) {
|
|
|
|
let currentYear = parseInt(creditList)
|
|
|
|
if (currentYear > year) year = currentYear
|
|
|
|
}
|
|
|
|
return year
|
2020-01-23 22:25:21 +00:00
|
|
|
},
|
|
|
|
loading: state => {
|
|
|
|
return state.loading
|
2020-01-26 22:55:59 +00:00
|
|
|
},
|
|
|
|
addLoading: state => {
|
|
|
|
return state.addLoading
|
|
|
|
},
|
|
|
|
error: state => {
|
|
|
|
return state.error
|
2020-01-27 18:56:56 +00:00
|
|
|
},
|
|
|
|
days: state => {
|
|
|
|
return state.days
|
2020-02-27 14:52:58 +00:00
|
|
|
},
|
|
|
|
messages: state => {
|
|
|
|
return state.message
|
2020-01-17 00:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
getters
|
|
|
|
}
|