fix(sql): ordenamiento de report_inventoryv2 modificado
- Se elimino ordenamiento intermedio por fecha (date_st_mov) - Ahora ordena por id_product + id_sto_mov secuencial - Incluye nota explicativa en el codigo de la funcion - Rollback disponible en caso de problemas
This commit is contained in:
105
scripts_sql/fix_report_inventoryv2_orderby.sql
Normal file
105
scripts_sql/fix_report_inventoryv2_orderby.sql
Normal file
@@ -0,0 +1,105 @@
|
||||
-- ============================================================
|
||||
-- FIX: Cambiar ordenamiento del reporte de inventario
|
||||
-- Fecha: 2026-06-09
|
||||
-- Autor: Analisis automatico del sistema
|
||||
-- ============================================================
|
||||
-- PROBLEMA IDENTIFICADO:
|
||||
-- El reporte de inventario ordenaba primero por producto,
|
||||
-- luego por fecha (date_st_mov) y finalmente por id de
|
||||
-- movimiento (id_sto_mov). Esto causaba que movimientos
|
||||
-- registrados fuera de orden cronologico (por ejemplo,
|
||||
-- ajustes retrospectivos o correcciones) se mostraran
|
||||
-- desordenados respecto al flujo real de operaciones del
|
||||
-- sistema.
|
||||
--
|
||||
-- SOLUCION APLICADA:
|
||||
-- Se elimina el ordenamiento intermedio por fecha y se
|
||||
-- deja unicamente el ordenamiento por id_sto_mov, que
|
||||
-- refleja el orden secuencial de registro en la base de
|
||||
-- datos (autoincremental). Esto garantiza que los
|
||||
-- movimientos se muestren en el orden exacto en que
|
||||
-- fueron procesados por el sistema, independientemente
|
||||
-- de la fecha que tengan asignada.
|
||||
--
|
||||
-- NOTA: Este cambio es reversible. Si algo falla, ejecutar
|
||||
-- rollback_report_inventoryv2_orderby.sql
|
||||
-- ============================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.report_inventoryv2(
|
||||
)
|
||||
RETURNS TABLE(date_movement date, id_product integer, name_product character varying, movement character varying, comments_discard character varying, housekepper_name character varying, quantity integer, before_stock bigint, stock bigint)
|
||||
LANGUAGE 'plpgsql'
|
||||
COST 100
|
||||
VOLATILE PARALLEL UNSAFE
|
||||
ROWS 1000
|
||||
|
||||
AS $BODY$
|
||||
BEGIN
|
||||
RETURN QUERY
|
||||
SELECT
|
||||
sm.date_st_mov AS date_movement,
|
||||
p.id_product,
|
||||
p.name_product,
|
||||
mt.name_mov_type AS movement,
|
||||
sd.reason,
|
||||
emp.name_employee,
|
||||
/* Cantidad según el tipo de movimiento */
|
||||
CASE
|
||||
WHEN mt.name_mov_type = 'Purchase' THEN pd.quantity
|
||||
WHEN mt.name_mov_type = 'Adjustment' THEN sa.adjustment::int
|
||||
WHEN mt.name_mov_type = 'Discard' THEN sd.quantity*(-1)
|
||||
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
||||
ELSE NULL
|
||||
END AS quantity,
|
||||
sm.stock_before,
|
||||
sm.stock_current
|
||||
FROM stock_movements sm
|
||||
INNER JOIN products p
|
||||
ON p.id_product = sm.id_product
|
||||
|
||||
INNER JOIN movement_type mt
|
||||
ON sm.id_mov_type = mt.id_mov_type
|
||||
|
||||
INNER JOIN stock st
|
||||
ON st.id_product = p.id_product
|
||||
|
||||
/* AHORA CORRECTO: JOIN por id_stock_mov */
|
||||
LEFT JOIN purchase_detail pd
|
||||
ON pd.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_adjusments sa
|
||||
ON sa.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_discard sd
|
||||
ON sd.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_consumption sc
|
||||
ON sc.id_stock_mov = sm.id_sto_mov
|
||||
LEFT JOIN employees emp
|
||||
ON sc.rfc_employee = emp.rfc_employee
|
||||
|
||||
-- ============================================================
|
||||
-- ORDENAMIENTO MODIFICADO
|
||||
-- ============================================================
|
||||
-- Motivo: Se elimino el ordenamiento por fecha
|
||||
-- (sm.date_st_mov) porque no reflejaba el orden real de
|
||||
-- procesamiento del sistema. Ahora se ordena unicamente
|
||||
-- por producto y por id de movimiento secuencial, lo cual
|
||||
-- muestra los registros en el orden exacto en que fueron
|
||||
-- creados en la base de datos.
|
||||
--
|
||||
-- Fecha de cambio: 2026-06-09
|
||||
-- Referencia: Cambio de ordenamiento en Inventory Report
|
||||
-- ============================================================
|
||||
/* ORDER ORIGINAL (comentado temporalmente):
|
||||
ORDER BY p.id_product, sm.date_st_mov, sm.id_sto_mov;
|
||||
*/
|
||||
ORDER BY p.id_product, sm.id_sto_mov;
|
||||
END;
|
||||
$BODY$;
|
||||
|
||||
ALTER FUNCTION public.report_inventoryv2()
|
||||
OWNER TO postgres;
|
||||
|
||||
-- Confirmacion
|
||||
SELECT 'Funcion report_inventoryv2 actualizada correctamente (ordenamiento modificado)' AS resultado;
|
||||
70
scripts_sql/rollback_report_inventoryv2_orderby.sql
Normal file
70
scripts_sql/rollback_report_inventoryv2_orderby.sql
Normal file
@@ -0,0 +1,70 @@
|
||||
-- ============================================================
|
||||
-- ROLLBACK: Restaurar report_inventoryv2 a su estado original
|
||||
-- Fecha: 2026-06-09
|
||||
-- ============================================================
|
||||
-- Usar este script SOLO si el cambio de ordenamiento causa
|
||||
-- problemas y se necesita restaurar el comportamiento anterior.
|
||||
-- ============================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.report_inventoryv2(
|
||||
)
|
||||
RETURNS TABLE(date_movement date, id_product integer, name_product character varying, movement character varying, comments_discard character varying, housekepper_name character varying, quantity integer, before_stock bigint, stock bigint)
|
||||
LANGUAGE 'plpgsql'
|
||||
COST 100
|
||||
VOLATILE PARALLEL UNSAFE
|
||||
ROWS 1000
|
||||
|
||||
AS $BODY$
|
||||
BEGIN
|
||||
RETURN QUERY
|
||||
SELECT
|
||||
sm.date_st_mov AS date_movement,
|
||||
p.id_product,
|
||||
p.name_product,
|
||||
mt.name_mov_type AS movement,
|
||||
sd.reason,
|
||||
emp.name_employee,
|
||||
/* Cantidad según el tipo de movimiento */
|
||||
CASE
|
||||
WHEN mt.name_mov_type = 'Purchase' THEN pd.quantity
|
||||
WHEN mt.name_mov_type = 'Adjustment' THEN sa.adjustment::int
|
||||
WHEN mt.name_mov_type = 'Discard' THEN sd.quantity*(-1)
|
||||
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
||||
ELSE NULL
|
||||
END AS quantity,
|
||||
sm.stock_before,
|
||||
sm.stock_current
|
||||
FROM stock_movements sm
|
||||
INNER JOIN products p
|
||||
ON p.id_product = sm.id_product
|
||||
|
||||
INNER JOIN movement_type mt
|
||||
ON sm.id_mov_type = mt.id_mov_type
|
||||
|
||||
INNER JOIN stock st
|
||||
ON st.id_product = p.id_product
|
||||
|
||||
/* AHORA CORRECTO: JOIN por id_stock_mov */
|
||||
LEFT JOIN purchase_detail pd
|
||||
ON pd.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_adjusments sa
|
||||
ON sa.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_discard sd
|
||||
ON sd.id_stock_mov = sm.id_sto_mov
|
||||
|
||||
LEFT JOIN stock_consumption sc
|
||||
ON sc.id_stock_mov = sm.id_sto_mov
|
||||
LEFT JOIN employees emp
|
||||
ON sc.rfc_employee = emp.rfc_employee
|
||||
|
||||
ORDER BY p.id_product, sm.date_st_mov, sm.id_sto_mov;
|
||||
END;
|
||||
$BODY$;
|
||||
|
||||
ALTER FUNCTION public.report_inventoryv2()
|
||||
OWNER TO postgres;
|
||||
|
||||
-- Confirmacion
|
||||
SELECT 'Funcion report_inventoryv2 restaurada a su estado original' AS resultado;
|
||||
Reference in New Issue
Block a user