Some sqlj to pgplsql translations.

replaces org.compiere.sqlj.Currency class
Both badly commented and tested.
Making commit for others to test.
This commit is contained in:
kontro 2006-12-21 20:16:54 +00:00
parent 3a75ad0a50
commit 09f00b0bc9
4 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,34 @@
CREATE OR REPLACE FUNCTION currencybase(
IN NUMERIC, -- $1 p_Amount
IN INTEGER, -- $2 p_C_CurrencyFrom_ID
IN TIMESTAMP WITH TIME ZONE, -- $3 p_ConversionDate
IN INTEGER, -- $4 p_AD_Client_ID
IN INTEGER -- $5 p_AD_Org_ID
) RETURNS NUMERIC
AS $$
DECLARE
currency_to INTEGER;
BEGIN
IF $1 IS NULL OR $2 IS NULL THEN
RETURN NULL;
END IF;
IF $1 = 0 THEN
RETURN 0;
END IF;
/*Finds out org's default currency*/
SELECT ac.C_Currency_ID
INTO currency_to
FROM AD_ClientInfo AS ci
INNER JOIN C_AcctSchema AS ac
ON (ci.C_AcctSchema1_ID = ac.C_AcctSchema_ID)
WHERE ci.AD_Client_ID = $4;
IF currency_to IS NULL THEN
RETURN NULL;
END IF;
IF $2 = currency_to THEN
RETURN $1;
END IF;
RETURN currencyconvert($1, $2, currency_to, $3,0,$4,$5);
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,28 @@
CREATE OR REPLACE FUNCTION currencyconvert(
IN NUMERIC, -- $1 amount to convert
IN INTEGER, -- $2 from currency id
IN INTEGER, -- $3 to currency id
IN TIMESTAMP WITH TIME ZONE, -- $4 conversion date
IN INTEGER, -- $5 conversion type id
IN INTEGER, -- $6 client id
IN INTEGER -- $7 org id
) RETURNS numeric AS
$$
DECLARE
rate NUMERIC;
BEGIN
IF $1 IS NULL OR $2 IS NULL OR $3 IS NULL THEN
RETURN NULL;
END IF;
IF $1 = 0 OR $2 = $3 THEN
RETURN $1;
END IF;
rate := currencyrate($2,$3,$4,$5,$6,$7);
IF rate IS NULL THEN
RETURN NULL;
END IF;
RETURN currencyround((rate * $1), $3, FALSE);
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,40 @@
CREATE OR REPLACE FUNCTION currencyrate(
IN INTEGER, -- $1 currency from id
IN INTEGER, -- $2 currency to id
IN timestamp with time zone, -- $3 conversiondate
IN INTEGER, -- $4 conversiontype id
IN INTEGER, -- $5 client id
IN INTEGER -- $6 org id
) RETURNS NUMERIC AS
$$
DECLARE
conversion_type_id INTEGER;
conversion_date DATE;
rate NUMERIC;
BEGIN
IF $1 = $2 THEN
RETURN 1;
END IF;
conversion_type_id = COALESCE($4, 0);
conversion_date = CAST($3 AS DATE);
rate = NULL;
IF conversion_type_id = 0 THEN
SELECT C_ConversionType_ID
INTO conversion_type_id
FROM C_ConversionType
WHERE IsDefault = 'Y' AND AD_Client_ID IN (0, $5);
END IF;
SELECT t.MultiplyRate
INTO rate
FROM C_Conversion_Rate AS t
WHERE t.C_Currency_ID = $1
AND t.C_Currency_To = $2
AND date_trunc('day',t.ValidFrom) <= $3
AND date_trunc('day',t.ValidTo) >= $3
AND t.AD_Client_ID IN (0,$5)
AND t.AD_Org_ID IN (0, $6)
ORDER BY AD_Client_ID DESC, AD_Org_ID DESC, ValidFrom DESC
LIMIT 1;
return rate;
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,27 @@
CREATE OR REPLACE FUNCTION currencyround (
IN NUMERIC, -- $1 amount
IN INTEGER, -- $2 Currency_ID
IN BOOLEAN -- $3 Costing
) RETURNS NUMERIC AS
$$
DECLARE
precision INTEGER;
BEGIN
IF $1 IS NULL OR $2 IS NULL THEN
RETURN $1;
END IF;
IF COALESCE($3,FALSE) THEN
SELECT t.CostingPrecision
INTO precision
FROM C_Currency AS t
WHERE C_Currency_ID = $2;
ELSE
SELECT t.stdprecision
INTO precision
FROM C_Currency AS t
WHERE C_Currency_ID = $2;
END IF;
RETURN ROUND($1, precision);
END;
$$ LANGUAGE plpgsql;