fix(sql): actualizar report_inventoryv2 con firma de produccion

- Ajustar script al esquema real de la funcion en produccion
- Ordenamiento modificado: id_product + id_sto_mov
- Rollback actualizado con firma correcta
This commit is contained in:
Consultoria AS
2026-06-25 12:21:36 -07:00
parent bd9f0e770e
commit f3933328fb
2 changed files with 50 additions and 13 deletions

View File

@@ -25,14 +25,27 @@
-- rollback_report_inventoryv2_orderby.sql
-- ============================================================
CREATE OR REPLACE FUNCTION public.report_inventoryv2(
DROP FUNCTION IF EXISTS public.report_inventoryv2();
CREATE OR REPLACE FUNCTION public.report_inventoryv2()
RETURNS TABLE(
date_movement date,
id_product integer,
name_product character varying,
category_en character varying,
category_esp character varying,
movement character varying,
comments_discard character varying,
housekepper_name character varying,
quantity integer,
average_cost numeric,
before_stock bigint,
stock bigint
)
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
@@ -40,17 +53,21 @@ BEGIN
sm.date_st_mov AS date_movement,
p.id_product,
p.name_product,
pcat.name_prod_category,
pcat.spanish_name,
mt.name_mov_type AS movement,
sd.reason,
emp.name_employee,
/* Cantidad según el tipo de movimiento */
/* Cantidad segun el tipo de movimiento */
CASE
WHEN mt.name_mov_type = 'Purchase' THEN pd.quantity
WHEN mt.name_mov_type = 'Purchase' THEN sm.quantity
WHEN mt.name_mov_type = 'Adjustment' THEN sa.adjustment::int
WHEN mt.name_mov_type = 'New product' THEN sm.stock_current::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.average_cost,
sm.stock_before,
sm.stock_current
FROM stock_movements sm
@@ -63,6 +80,9 @@ BEGIN
INNER JOIN stock st
ON st.id_product = p.id_product
INNER JOIN product_category pcat
ON p.id_prod_category = pcat.id_prod_category
/* AHORA CORRECTO: JOIN por id_stock_mov */
LEFT JOIN purchase_detail pd
ON pd.id_stock_mov = sm.id_sto_mov
@@ -91,9 +111,6 @@ BEGIN
-- 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$;

View File

@@ -6,14 +6,27 @@
-- problemas y se necesita restaurar el comportamiento anterior.
-- ============================================================
CREATE OR REPLACE FUNCTION public.report_inventoryv2(
DROP FUNCTION IF EXISTS public.report_inventoryv2();
CREATE OR REPLACE FUNCTION public.report_inventoryv2()
RETURNS TABLE(
date_movement date,
id_product integer,
name_product character varying,
category_en character varying,
category_esp character varying,
movement character varying,
comments_discard character varying,
housekepper_name character varying,
quantity integer,
average_cost numeric,
before_stock bigint,
stock bigint
)
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
@@ -21,17 +34,21 @@ BEGIN
sm.date_st_mov AS date_movement,
p.id_product,
p.name_product,
pcat.name_prod_category,
pcat.spanish_name,
mt.name_mov_type AS movement,
sd.reason,
emp.name_employee,
/* Cantidad según el tipo de movimiento */
/* Cantidad segun el tipo de movimiento */
CASE
WHEN mt.name_mov_type = 'Purchase' THEN pd.quantity
WHEN mt.name_mov_type = 'Purchase' THEN sm.quantity
WHEN mt.name_mov_type = 'Adjustment' THEN sa.adjustment::int
WHEN mt.name_mov_type = 'New product' THEN sm.stock_current::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.average_cost,
sm.stock_before,
sm.stock_current
FROM stock_movements sm
@@ -44,6 +61,9 @@ BEGIN
INNER JOIN stock st
ON st.id_product = p.id_product
INNER JOIN product_category pcat
ON p.id_prod_category = pcat.id_prod_category
/* AHORA CORRECTO: JOIN por id_stock_mov */
LEFT JOIN purchase_detail pd
ON pd.id_stock_mov = sm.id_sto_mov