import axios from 'axios' const newUrl = '/eiru_purchases/new' const createSupplierUrl = '/eiru_purchases/create_supplier' const createProductUrl = '/eiru_purchases/create_product' const createUrl = '/eiru_purchases/create' const actions = { /** * Display notification with odoo style * @param {*} param0 * @param {*} payload */ notify({ commit }, payload) { openerp.web.notification.do_warn('Atención', payload) return false }, /** * Get all resources to init purchase app * @param {*} param0 */ initPurchase({ dispatch }) { return axios.get(newUrl).then(response => { dispatch('explodeData', response.data) }).catch(error => { console.log(error) }) }, /** * * @param {*} param0 * @param {*} payload */ explodeData({ commit }, payload) { Object.keys(payload).forEach(key => { commit(`set${key[0].toLocaleUpperCase()}${key.slice(1)}`, payload[key]) }) }, /** * Send data to create a supplier * @param {*} param0 * @param {*} payload */ createSupplier({ commit }, payload) { const data = { jsonrpc: '2.0', method: 'call', params: { ...payload } } return axios.post(createSupplierUrl, data).then(response => { console.log(response) }).catch(error => { console.log(error) }) }, /** * Check if a supplier is selected or notify * @param {*} param0 */ checkSupplier({ getters, dispatch }) { return !!getters.supplierSelected || dispatch('notify', 'Necesitas seleccionar un proveedor para continuar') }, /** * Send data to create a product * @param {*} param0 * @param {*} payload */ createProduct({ commit }, payload) { const data = { jsonrpc: '2.0', method: 'call', params: { ...payload } } return axios.post(createProductUrl, data).then(response => { console.log(response) }).catch(error => { console.log(error) }) }, /** * Check if exist product in cart or notify * @param {*} param0 */ checkCart({ getters, dispatch }) { return !!getters.cartItems.length || dispatch('notify', 'Necesitas agregar productos al carrito para continuar') }, /** * Send data to create concrete purchase * @param {*} param0 */ createPurchase({ getters, dispatch }) { const data = { jsonrpc: '2.0', method: 'call', params: { supplier: getters.supplierSelected.id } } return axios.post(createUrl, data).then(response => { console.log(response) // dispatch('resetPurchase') }).catch(error => { console.log(error) }) }, /** * Reset all default app state * @param {*} param0 */ resetPurchase({ state, commit }) { Object.keys(state).forEach(value => { Object.keys(state[value]).forEach(key => { commit(`set${key[0].toLocaleUpperCase()}${key.slice(1)}`, state[value][key].default) }) }) } } export default actions