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

25 lines
1.1 KiB
Transact-SQL
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 ALTER FUNCTION [dbo].[Inv_F_UomConvert]
(
@Quantity decimal(18, 4),
@FromUomId nvarchar(64),
@ToUomId nvarchar(64)
)
RETURNS decimal(18, 4)
AS
BEGIN
IF @Quantity IS NULL OR @FromUomId IS NULL OR @ToUomId IS NULL OR @FromUomId = @ToUomId
RETURN @Quantity;
DECLARE @From float, @To float;
SELECT @From = CASE f.Type WHEN 'BiggerThanReference' THEN f.Ratio WHEN 'SmallerThanReference' THEN 1.0 / NULLIF(f.Ratio, 0) ELSE 1 END,
@To = CASE t.Type WHEN 'BiggerThanReference' THEN t.Ratio WHEN 'SmallerThanReference' THEN 1.0 / NULLIF(t.Ratio, 0) ELSE 1 END
FROM Adm_T_Uom f
INNER JOIN Adm_T_Uom t ON t.Id = @ToUomId AND t.UomCategoryId = f.UomCategoryId
WHERE f.Id = @FromUomId;
IF @From IS NULL OR NULLIF(@To, 0) IS NULL
RETURN @Quantity;
RETURN ROUND(@Quantity * @From / @To, 4);
END