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:
@@ -25,14 +25,27 @@
|
|||||||
-- rollback_report_inventoryv2_orderby.sql
|
-- 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'
|
LANGUAGE 'plpgsql'
|
||||||
COST 100
|
COST 100
|
||||||
VOLATILE PARALLEL UNSAFE
|
VOLATILE PARALLEL UNSAFE
|
||||||
ROWS 1000
|
ROWS 1000
|
||||||
|
|
||||||
AS $BODY$
|
AS $BODY$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN QUERY
|
RETURN QUERY
|
||||||
@@ -40,17 +53,21 @@ BEGIN
|
|||||||
sm.date_st_mov AS date_movement,
|
sm.date_st_mov AS date_movement,
|
||||||
p.id_product,
|
p.id_product,
|
||||||
p.name_product,
|
p.name_product,
|
||||||
|
pcat.name_prod_category,
|
||||||
|
pcat.spanish_name,
|
||||||
mt.name_mov_type AS movement,
|
mt.name_mov_type AS movement,
|
||||||
sd.reason,
|
sd.reason,
|
||||||
emp.name_employee,
|
emp.name_employee,
|
||||||
/* Cantidad según el tipo de movimiento */
|
/* Cantidad segun el tipo de movimiento */
|
||||||
CASE
|
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 = '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 = 'Discard' THEN sd.quantity*(-1)
|
||||||
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
||||||
ELSE NULL
|
ELSE NULL
|
||||||
END AS quantity,
|
END AS quantity,
|
||||||
|
sm.average_cost,
|
||||||
sm.stock_before,
|
sm.stock_before,
|
||||||
sm.stock_current
|
sm.stock_current
|
||||||
FROM stock_movements sm
|
FROM stock_movements sm
|
||||||
@@ -63,6 +80,9 @@ BEGIN
|
|||||||
INNER JOIN stock st
|
INNER JOIN stock st
|
||||||
ON st.id_product = p.id_product
|
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 */
|
/* AHORA CORRECTO: JOIN por id_stock_mov */
|
||||||
LEFT JOIN purchase_detail pd
|
LEFT JOIN purchase_detail pd
|
||||||
ON pd.id_stock_mov = sm.id_sto_mov
|
ON pd.id_stock_mov = sm.id_sto_mov
|
||||||
@@ -91,9 +111,6 @@ BEGIN
|
|||||||
-- Fecha de cambio: 2026-06-09
|
-- Fecha de cambio: 2026-06-09
|
||||||
-- Referencia: Cambio de ordenamiento en Inventory Report
|
-- 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;
|
ORDER BY p.id_product, sm.id_sto_mov;
|
||||||
END;
|
END;
|
||||||
$BODY$;
|
$BODY$;
|
||||||
|
|||||||
@@ -6,14 +6,27 @@
|
|||||||
-- problemas y se necesita restaurar el comportamiento anterior.
|
-- 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'
|
LANGUAGE 'plpgsql'
|
||||||
COST 100
|
COST 100
|
||||||
VOLATILE PARALLEL UNSAFE
|
VOLATILE PARALLEL UNSAFE
|
||||||
ROWS 1000
|
ROWS 1000
|
||||||
|
|
||||||
AS $BODY$
|
AS $BODY$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN QUERY
|
RETURN QUERY
|
||||||
@@ -21,17 +34,21 @@ BEGIN
|
|||||||
sm.date_st_mov AS date_movement,
|
sm.date_st_mov AS date_movement,
|
||||||
p.id_product,
|
p.id_product,
|
||||||
p.name_product,
|
p.name_product,
|
||||||
|
pcat.name_prod_category,
|
||||||
|
pcat.spanish_name,
|
||||||
mt.name_mov_type AS movement,
|
mt.name_mov_type AS movement,
|
||||||
sd.reason,
|
sd.reason,
|
||||||
emp.name_employee,
|
emp.name_employee,
|
||||||
/* Cantidad según el tipo de movimiento */
|
/* Cantidad segun el tipo de movimiento */
|
||||||
CASE
|
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 = '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 = 'Discard' THEN sd.quantity*(-1)
|
||||||
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
WHEN mt.name_mov_type = 'Consumption' THEN sc.consumption_quantity*(-1)
|
||||||
ELSE NULL
|
ELSE NULL
|
||||||
END AS quantity,
|
END AS quantity,
|
||||||
|
sm.average_cost,
|
||||||
sm.stock_before,
|
sm.stock_before,
|
||||||
sm.stock_current
|
sm.stock_current
|
||||||
FROM stock_movements sm
|
FROM stock_movements sm
|
||||||
@@ -44,6 +61,9 @@ BEGIN
|
|||||||
INNER JOIN stock st
|
INNER JOIN stock st
|
||||||
ON st.id_product = p.id_product
|
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 */
|
/* AHORA CORRECTO: JOIN por id_stock_mov */
|
||||||
LEFT JOIN purchase_detail pd
|
LEFT JOIN purchase_detail pd
|
||||||
ON pd.id_stock_mov = sm.id_sto_mov
|
ON pd.id_stock_mov = sm.id_sto_mov
|
||||||
|
|||||||
Reference in New Issue
Block a user