import axios from "axios"; const state = { lastFetch: null, grupo_de_compra: null, pedido_id: null, nombre: null, productos: [], aprobado: null, total: null, total_transporte: null, cantidad_transporte: null, total_sin_devoluciones: null, devoluciones_total: null, devoluciones_notas: null, }; const mutations = { setGrupoDeCompra(state, grupo_de_compra) { state.grupo_de_compra = grupo_de_compra; }, setPedido(state, pedido) { state.lastFetch = new Date(); state.pedido_id = pedido.id; state.nombre = pedido.nombre; state.productos = pedido.productos; state.aprobado = pedido.aprobado; state.total = Number.parseFloat(pedido.total.replace(',','')); state.total_transporte = Number.parseInt(pedido.total_transporte.replace(',','')); state.cantidad_transporte = Number.parseInt(pedido.cantidad_transporte.replace(',','')); state.total_sin_devoluciones = Number.parseFloat(pedido.total_sin_devoluciones.replace(',','')); state.devoluciones_total = Number.parseFloat(pedido.devoluciones_total.replace(',','')); state.devoluciones_notas = pedido.devoluciones_notas; }, reset(state) { state.lastFetch = null; state.pedido_id = null; state.nombre = null; state.productos = null; state.aprobado = null; state.total = null; state.total_transporte = null; state.cantidad_transporte = null; state.total_sin_devoluciones = null; state.devoluciones_total = null; state.devoluciones_notas = null; } }; const actions = { async getGrupoDeCompra({ commit }) { const response = await axios.get('/user/grupo_de_compra'); commit('setGrupoDeCompra', response.data.grupo_de_compra); }, async guardarSesion(_, { pedido_id }) { const body = { id: pedido_id }; await axios.post("/pedido/sesion", body); }, async crearPedido({ commit, dispatch }, { nombre, grupo_de_compra_id, tipo_id }) { const body = { nombre, grupo_de_compra_id, tipo_id }; const response = await axios.post("/api/subpedidos", body); dispatch("guardarSesion", { pedido_id: response.data.data.id}); commit('setPedido', response.data.data); }, async elegirPedido({ commit, dispatch }, { pedido_id }) { const response = await axios.get(`/api/subpedidos/${pedido_id}`); const body = { pedido_id: pedido_id}; dispatch("guardarSesion", body) commit('setPedido', response.data.data); }, async modificarChismosa({ commit, dispatch }, { producto_id, cantidad, notas }) { const body = { cantidad: cantidad, producto_id: producto_id, notas: notas }; try { const response = await axios.post("/api/subpedidos/" + state.pedido_id + "/sync", body); commit('setPedido', response.data.data); dispatch("ui/toast", { mensaje: 'Pedido modificado con éxito' }, { root: true }); } catch (error) { dispatch("ui/error", { error: error }, { root: true }); } }, async modificarDevoluciones({ commit, dispatch }, { monto, notas }) { const body = { total: monto, notas: notas }; try { const response = await axios.post("api/subpedidos/" + state.pedido_id + "/sync_devoluciones", body); commit('setPedido', response.data.data); dispatch("ui/toast", { mensaje: 'Devoluciones modificadas con éxito' }, { root: true }); } catch (error) { dispatch("ui/error", { error: error }, { root: true }); } }, async resetear({ commit, dispatch }) { await axios.delete("/pedido/sesion"); dispatch("productos/getProductos", null, { root: true }); dispatch("ui/resetear", null, { root: true }); commit('reset'); }, async getPedidoDeOllas({ commit }) { const response = await axios.get(`/api/ollas/${state.grupo_de_compra.id}`); commit('setPedido', response.data); }, }; const getters = { pedidoDefinido() { return state.lastFetch !== null; }, enChismosa() { return ((producto_id) => state.productos.some(p => p.id === producto_id)); }, cantidad() { return ((producto_id) => state.productos.find(p => p.id === producto_id)?.pivot.cantidad ?? 0); }, notas() { return ((producto_id) => state.productos.find(p => p.id === producto_id)?.pivot.notas ?? ""); } } export default { namespaced: true, state, mutations, actions, getters, };