Sousmargot/src/store/travail/index.js

131 lines
4.3 KiB
JavaScript

import { readFile } from 'fs'
import Papa from 'papaparse'
import { getYear } from 'date-fns'
const today = new Date()
const travail = {
namespaced: true,
state() {
return {
empty: {
caTheo: null, // ca théorique basé sur les séances effectuées
sessionQty: null, // Nombre de séances effectuées sur le mois
caRetro: null, // ca au moment de la rétrocession
caReact: null, // ca réactualisé
retro: 0, // montant de la rétrocession
remuneration: 0, // rémunération décidée
proPercentage: 50, // Pourcentage du CA pour la partie pro
},
months: {
},
range: {
start: `${getYear(today)}-01`,
end: `${getYear(today)}-12`,
},
}
},
getters: {
TheEmptyMonth(state) { return { ...state.empty } },
range(state) { return state.range },
MonthsDate(state) {
// Get months inside the range
return Object.keys(state.months).filter(date => (date >= state.range.start) && (date <= state.range.end)).sort().reverse()
},
MonthsAllDate(state) {
// Get all the months
return Object.keys(state.months).sort().reverse()
},
lastMonthDate(state) {
// Return the date of the last registered month
return Object.keys(state.months).sort().reverse()[0]
},
months: (state, getters) => {
// Get in range months
const a = Object.keys(state.months)
.filter(a => getters.MonthsDate.includes(a))
.reduce((acc, v) => {
acc[v] = state.months[v];
return acc;
}, {})
return a
},
monthsAll: (state) => {
// Get in range months
return state.months
},
getMonth: (state) => (date) => {
// Get a month by its date
return state.months[date]
},
count: (state, getters) => {
// Amount of mounts
return Object.keys(getters.months).length
},
years: (state) => {
// list of years with data
return Object.keys(state.months).map(k => k.slice(0,4)).filter((v, i, a) => a.indexOf(v) === i)
}
},
mutations: {
cleanMonths (state) {
// erase months
state.months = []
},
importMonths(state, months) {
// overwrite months
state.months = months
},
updateMonth(state, { date, month }) {
state.months[date] = month
},
createMonth (state, { date, month }) {
state.months[date] = month
},
setRange(state, range) {
state.range = range
},
},
actions: {
cleanMonths (context) {
context.commit("cleanMonths")
},
loadMonths (context) {
// import all months from storage
readFile(context.rootGetters["config/dataFilePath"], (err, data) => {
if (err) throw err;
const months = Papa.parse(data.toString(), {header: true, dynamicTyping:true, skipEmptyLines:true})
.data
.reduce(
(acc, el) => {
acc[el.date] = el;
return acc
}, {})
context.commit("importMonths", months)
})
},
updateMonth(context, { date, month }) {
// update month's datas
if (date in context.state.months) {
context.commit('updateMonth', { date, month })
} else {
console.log("This month does not exists")
}
},
createMonth(context, { date, month }) {
// Create a new month
if (!(date in context.state.months)) {
context.commit('createMonth', { date, month })
} else {
console.log("This month already exists")
}
},
setRange(context, range) {
context.commit("setRange", range)
},
},
}
export default travail