pedi2/resources/js/store/modules/pedido.js

143 lines
5.2 KiB
JavaScript
Vendored

import axios from "axios";
const state = {
lastFetch: null,
grupo_de_compra: null,
pedido_id: 0,
nombre: "",
productos: [],
aprobado: false,
total: 0,
total_transporte: 0,
cantidad_transporte: 0,
total_sin_devoluciones: 0,
devoluciones_total: 0,
devoluciones_notas: "",
cantidad_de_ollas: 0,
};
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;
},
setPedidoDeOllas(state, pedido) {
state.lastFetch = new Date();
state.pedido_id = pedido.id;
state.nombre = pedido.nombre;
state.productos = pedido.productos;
state.total = Number.parseFloat(pedido.total.replace(',',''));
state.cantidad_de_ollas = Number.parseInt(pedido.cantidad_de_ollas);
delete state.aprobado;
delete state.total_transporte;
delete state.cantidad_transporte;
delete state.total_sin_devoluciones;
delete state.devoluciones_total;
delete state.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;
},
setCantidadOllas(state, { cantidad }) {
if (cantidad >= 0)
state.cantidad_de_ollas = cantidad;
}
};
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(`/ollas/${state.grupo_de_compra.id}`);
commit('setPedidoDeOllas', 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,
};