apps.mtl_units_of_measure -- 物料單位表
apps.mtl_uom_conversions -- 物料單位進率轉換表
CREATE OR REPLACE FUNCTION mtl_uom_conversion_func (p_from_uom IN VARCHAR2, -- 要被轉換的 UOM
p_to_uom IN VARCHAR2, -- 將要轉換成的 UOM
p_unit IN number -- 要被轉換的數量
)
RETURN NUMBER
AS
fr_base_uom VARCHAR2 (10);
to_base_uom VARCHAR2 (10);
conv1 NUMBER;
conv2 NUMBER;
result NUMBER;
BEGIN
-- 判斷 被轉的UOM 與 將要轉成的UOM 是否一樣,若一樣的話,將原值返回
IF UPPER (TRIM (p_from_uom)) = UPPER (TRIM (p_to_uom)) THEN
RETURN p_unit;
END IF;
-- 判斷 被轉的UOM 是什麼類型
SELECT UPPER (uom_code)
INTO fr_base_uom
FROM apps.mtl_units_of_measure
WHERE base_uom_flag = 'Y'
AND uom_class = (SELECT uom_class
FROM apps.mtl_units_of_measure
WHERE UPPER (uom_code) = UPPER (TRIM (p_from_uom)));
-- 判斷 將要轉的UOM 是什麼類型
SELECT UPPER (uom_code)
INTO to_base_uom
FROM apps.mtl_units_of_measure
WHERE base_uom_flag = 'Y'
AND uom_class = (SELECT uom_class
FROM apps.mtl_units_of_measure
WHERE UPPER (uom_code) = UPPER (TRIM (p_to_uom)));
-- 判斷 將要被轉UOM 與 被轉的UOM 類型是否一致
if (fr_base_uom <> to_base_uom) then
return -1; -- -1表示類型不同,而不能進行單位轉換
end if;
-- 判斷 被轉的UOM 轉換率
SELECT conversion_rate
INTO conv1
FROM apps.mtl_uom_conversions
WHERE UPPER (uom_code) = UPPER (TRIM (p_from_uom));
-- 判斷 將要轉成的UOM 轉換率
SELECT conversion_rate
INTO conv2
FROM apps.mtl_uom_conversions
WHERE UPPER (uom_code) = UPPER (TRIM (p_to_uom));
-- 依據 兩個單位之間的進率進行公式轉換
result := (p_unit * conv1) / conv2;
RETURN result;
EXCEPTION
WHEN OTHERS
THEN
RETURN -1;
END;