const state = { customers: [], filtered: [], selectedCustomer: null, addCustomer: false } const getters = { customers(state) { return state.filtered.length === 0 ? state.customers : state.filtered }, hasSelectedCustomer(state) { return !!state.selectedCustomer }, selectedCustomer(state) { return state.selectedCustomer }, addCustomer(state) { return state.addCustomer } } const mutations = { pushCustomers(state, payload) { state.customers = [...payload, ...state.customers] }, selectCustomer(state, payload) { state.selectedCustomer = payload.customer }, applyCustomersFilter(state, payload) { state.filtered = payload }, addCustomer(state) { state.addCustomer = !state.addCustomer } } const actions = { fetchCustomers({ commit, dispatch }) { return new Promise((resolve, reject) => { let ResPartner = new openerp.web.Model('res.partner') ResPartner.call('get_customers', { context: new openerp.web.CompoundContext() }).then(response => { commit('pushCustomers', response) dispatch('loaded', 'customers') resolve() }).fail(error => { console.log(error) reject(error) }) }) }, selectCustomer({ commit }, payload) { commit('selectCustomer', { customer: payload }) }, filterCustomers({ commit }, payload) { commit('applyCustomersFilter', payload) }, addCustomer({ commit }) { commit('addCustomer') }, saveCustomer({ commit, dispatch }, payload) { if (payload) { if (!payload.name || !payload.ruc || !payload.phone) { dispatch('notify', 'Complete los datos para guardar') return } let ResPartner = new openerp.web.Model('res.partner') ResPartner.call('create_from_pos', [ payload ], { context: new openerp.web.CompoundContext() }).then(id => { commit('pushCustomers', [{ id, ...payload }]) }) } commit('addCustomer') } } export default { state, getters, mutations, actions }