pedi2/resources/js/store/modules/pedido/index.ts
ale d5e70dc291 Merge branch 'master' into funcion/actualizaciones
# Conflicts:
#	package-lock.json
#	resources/js/components/comisiones/Body.vue
#	resources/js/store/modules/comisiones/index.ts
#	resources/js/store/modules/pedido/index.ts
2025-07-20 11:08:48 -03:00

149 lines
5.5 KiB
TypeScript

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,
cantidad_de_ollas: 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 = 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: 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;
},
setCantidadOllas(state, { cantidad }) {
if (cantidad >= 0)
state.cantidad_de_ollas = cantidad;
}
};
const actions = {
async getGrupoDeCompra({ commit }) {
const { data } = await axios.get('/user/grupo_de_compra');
commit('setGrupoDeCompra', 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 { data } = await axios.post("/api/subpedidos", body);
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');
},
async getPedidoDeOllas({ commit }) {
const response = await axios.get(`/ollas/${state.grupo_de_compra.id}`);
commit('setPedidoDeOllas', response.data);
},
};
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,
};