import { SELECT_MOVE_INVOICE, SELECT_MOVE, REMOVE_MOVE_PAYMENTS, ADD_MOVE_IN_INVOICE, REMOVE_MOVE_PAYMENTS_ALL } from '@/constants/actionTypes' import { SET_MOVES, SET_SELECTED_MOVE_LINE, SET_SELECTED_MOVE_PAYMENTS, REMOVE_MOVE_LINE, REMOVE_PAYMENTS_MOVE, SET_TOTAL_MOVE_PAYMENTS } from '@/constants/mutationTypes' const initialState = { moves : null, selectedMoveLine: null, movesPayments: [], movesPaymentsTotal: 0, movesPaymentsBalance: 0, } const state = { moves: initialState.moves, selectedMoveLine: initialState.selectedMoveLine, movesPayments: initialState.movesPayments, movesPaymentsTotal: initialState.movesPaymentsTotal, movesPaymentsBalance: initialState.movesPaymentsBalance, } const getters = { /** * [moves description] * @param {[type]} state [description] * @return {[type]} [description] */ moves (state) { return state.moves }, /** * [selectedMoveLine description] * @param {[type]} state [description] * @return {[type]} [description] */ selectedMoveLine (state){ return state.selectedMoveLine }, /** * [movesPayments description] * @param {[type]} state [description] * @return {[type]} [description] */ movesPayments (state) { return state.movesPayments }, /** * [movesPaymentsTotal description] * @param {[type]} state [description] * @return {[type]} [description] */ movesPaymentsTotal (state) { return state.movesPaymentsTotal }, /** * [movesPaymentsBalance description] * @param {[type]} state [description] * @return {[type]} [description] */ movesPaymentsBalance (state) { return state.movesPaymentsBalance } } const mutations = { /** * [moves description] * @type {[type]} * @param {[type]} state [description] * @param {[type]} payload [description] */ [SET_MOVES] (state, payload) { state.moves = payload }, /** * [selectedMoveLine description] * @type {[type]} * @param {[type]} state [description] * @param {[type]} payload [description] */ [SET_SELECTED_MOVE_LINE] (state, payload) { state.selectedMoveLine = payload }, /** * [movesPayments description] * @type {[type]} * @param {[type]} state [description] * @param {[type]} payload [description] */ [SET_SELECTED_MOVE_PAYMENTS] (state, payload) { state.movesPayments = [payload, ...state.movesPayments] }, /** * [actions description] * @type {Object} * @param {[type]} state [description] * @param {[type]} payload [description] */ [REMOVE_MOVE_LINE] (state, payload) { let moveFoundIndex = state.moves.findIndex(item => item.id === payload.id) state.moves.splice(moveFoundIndex, 1) }, /** * [actions description] * @type {Object} * @param {[type]} state [description] * @param {[type]} payload [description] */ [REMOVE_PAYMENTS_MOVE]( state, payload) { if (payload.mode ==='full') { payload.moveLine.forEach(moves => { let movesFoundIndex = state.moves.find(item => item.id === moves.id) if (movesFoundIndex) return /*ADD MOVE LINE*/ state.moves = [moves, ...state.moves] /* REMOVE MOVE CARD*/ let movesPaymentsFoundIndex = state.movesPayments.findIndex(item => item.id === moves.id) state.movesPayments.splice(movesPaymentsFoundIndex, 1) }) } else { let movesFound = state.moves.find(item => item.id === payload.moveLine.id) if (movesFound) return state.moves = [payload.moveLine, ...state.moves] let movesFoundIndex = state.movesPayments.findIndex(item => item.id === payload.moveLine.id) state.movesPayments.splice(movesFoundIndex, 1) } }, /** * [total description] * @type {[type]} * @param {[type]} state [description] * @param {[type]} payload [description] */ [SET_TOTAL_MOVE_PAYMENTS] (state) { let total =0 state.movesPayments.forEach(item => { total = total + item.amountResidual }) state.movesPaymentsTotal = total } } const actions = { /** * [SELECT_MOVE_INVOICE] * @param {*} param0 * @param {*} payload */ [SELECT_MOVE_INVOICE] ({ commit }, payload) { commit(SET_MOVES, payload) }, /** * [SELECT_MOVE] * @param {*} param0 * @param {*} payload */ [SELECT_MOVE] ({ commit }, payload) { commit(SET_SELECTED_MOVE_PAYMENTS, payload) commit(SET_SELECTED_MOVE_LINE, payload) /* Remover Move Line */ commit(REMOVE_MOVE_LINE, payload) commit(SET_TOTAL_MOVE_PAYMENTS) }, /** * * @param {*} param0 * @param {*} payload */ [REMOVE_MOVE_PAYMENTS] ({ commit, dispatch }, payload) { dispatch(ADD_MOVE_IN_INVOICE, { moveLine: payload, mode: 'partial' }) commit(REMOVE_PAYMENTS_MOVE, { moveLine: payload, mode: 'partial' }) commit(SET_TOTAL_MOVE_PAYMENTS) }, /** * [length description] * @type {[type]} */ [REMOVE_MOVE_PAYMENTS_ALL] ({commit, dispatch, state}, payload) { console.log(payload); if (!payload) return if (state.movesPayments.length === 0 ) return dispatch(ADD_MOVE_IN_INVOICE,{ moveLine: state.movesPayments.map(item => { return item }), mode: 'full' }) commit(REMOVE_PAYMENTS_MOVE, { moveLine: state.movesPayments.map(item => { return item }), mode: 'full' }) commit(SET_TOTAL_MOVE_PAYMENTS) } } export default { state, getters, mutations, actions }