import axios from "axios"; import { PedidoState } from "./types"; import { aplanarProducto } from "../../comunes"; const state: PedidoState = { lastFetch: undefined, grupo_de_compra: undefined, pedido_id: undefined, nombre: undefined, productos: [], aprobado: undefined, total: undefined, total_transporte: undefined, cantidad_transporte: undefined, total_sin_devoluciones: undefined, devoluciones_total: undefined, devoluciones_notas: undefined, }; const mutations = { setGrupoDeCompra(state: PedidoState, grupo_de_compra) { state.grupo_de_compra = grupo_de_compra; }, setPedido(state: PedidoState, pedido) { state.lastFetch = new Date(); state.pedido_id = pedido.id; state.nombre = pedido.nombre; state.productos = pedido.productos.map(p => aplanarProducto(p)); state.aprobado = pedido.aprobado; state.total = pedido.total; state.total_transporte = pedido.total_transporte; state.cantidad_transporte = pedido.cantidad_transporte; state.total_sin_devoluciones = pedido.total_sin_devoluciones; state.devoluciones_total = pedido.devoluciones_total; state.devoluciones_notas = pedido.devoluciones_notas; }, reset(state: PedidoState) { state.lastFetch = undefined; state.pedido_id = undefined; state.nombre = undefined; state.productos = []; state.aprobado = undefined; state.total = undefined; state.total_transporte = undefined; state.cantidad_transporte = undefined; state.total_sin_devoluciones = undefined; state.devoluciones_total = undefined; state.devoluciones_notas = undefined; } }; const actions = { async getGrupoDeCompra({ commit }) { const { data } = await axios.get('/user/grupo_de_compra'); commit('setGrupoDeCompra', data.grupo_de_compra); }, async guardarSesion(_, { pedido_id }) { await axios.post("/pedido/sesion", { id: pedido_id }); }, async crearPedido({ commit, dispatch }, { nombre, grupo_de_compra_id }) { const { data } = await axios.post("/api/subpedidos", { nombre: nombre, grupo_de_compra_id: grupo_de_compra_id }); dispatch("guardarSesion", { pedido_id: data.data.id}); commit('setPedido', data.data); }, async elegirPedido({ commit, dispatch }, { pedido_id }) { const { data } = await axios.get(`/api/subpedidos/${pedido_id}`); dispatch("guardarSesion", { pedido_id: pedido_id}) commit('setPedido', data.data); }, async modificarChismosa({ commit, dispatch }, { producto_id, cantidad, notas }) { try { const { data } = await axios.post("/api/subpedidos/" + state.pedido_id + "/sync", { cantidad: cantidad, producto_id: producto_id, notas: notas, }); commit('setPedido', 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 }) { try { const { data } = await axios.post("api/subpedidos/" + state.pedido_id + "/sync_devoluciones", { total: monto, notas: notas, }); commit('setPedido', 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'); }, }; const getters = { pedidoDefinido() { return state.lastFetch !== undefined; }, enChismosa() { return ((producto_id) => state.productos.some(p => p.id === producto_id)); }, cantidad() { return ((producto_id) => state.productos.find(p => p.id === producto_id)?.cantidad ?? 0); }, notas() { return ((producto_id) => state.productos.find(p => p.id === producto_id)?.notas ?? ""); } } export default { namespaced: true, state, mutations, actions, getters, };