sozsoft-platform/configs/seeds/postgres/object/Inv_F_UomConvert.sql
2026-10-13 00:43:05 +03:00

24 lines
1.2 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Miktarı bir ölçü biriminden diğerine çevirir (aynı kategori içinde, Adm_T_Uom.Type + Ratio).
-- Referans birim 1; büyük birim 1 birim = Ratio referans; küçük birim Ratio birim = 1 referans. Kategori farklıysa ya da birim yoksa miktar aynen döner.
CREATE OR REPLACE FUNCTION public."Inv_F_UomConvert"(p_quantity numeric, p_from_uom_id varchar, p_to_uom_id varchar)
RETURNS numeric
LANGUAGE plpgsql STABLE AS $$
DECLARE v_from double precision; v_to double precision;
BEGIN
IF p_quantity IS NULL OR p_from_uom_id IS NULL OR p_to_uom_id IS NULL OR p_from_uom_id = p_to_uom_id THEN
RETURN p_quantity;
END IF;
SELECT CASE f."Type" WHEN 'BiggerThanReference' THEN f."Ratio" WHEN 'SmallerThanReference' THEN 1.0 / NULLIF(f."Ratio", 0) ELSE 1 END,
CASE t."Type" WHEN 'BiggerThanReference' THEN t."Ratio" WHEN 'SmallerThanReference' THEN 1.0 / NULLIF(t."Ratio", 0) ELSE 1 END
INTO v_from, v_to
FROM "Adm_T_Uom" f
INNER JOIN "Adm_T_Uom" t ON t."Id" = p_to_uom_id AND t."UomCategoryId" = f."UomCategoryId"
WHERE f."Id" = p_from_uom_id;
IF v_from IS NULL OR NULLIF(v_to, 0) IS NULL THEN
RETURN p_quantity;
END IF;
RETURN ROUND((p_quantity * v_from / v_to)::numeric, 4);
END;
$$;