core-jgi/db/postgresql/functions/C_Currency_Convert.sql

60 lines
1.7 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION currencyConvert(
p_Amount NUMERIC,
p_CurFrom_ID NUMERIC,
p_CurTo_ID NUMERIC,
p_ConvDate timestamp with time zone,
p_ConversionType_ID IN NUMERIC,
p_Client_ID NUMERIC,
p_Org_ID NUMERIC
)
RETURNS numeric AS $body$
/*************************************************************************
* The contents of this file are subject to the Compiere License. You may
* obtain a copy of the License at http://www.compiere.org/license.html
* Software is on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for details. Code: Compiere ERP+CRM
* Copyright (C) 1999-2001 Jorg Janke, ComPiere, Inc. All Rights Reserved.
*
* converted to postgreSQL by Karsten Thiemann (Schaeffer AG),
* kthiemann@adempiere.org
*************************************************************************
***
* Title: Convert Amount (using IDs)
* Description:
* from CurrencyFrom_ID to CurrencyTo_ID
* Returns NULL, if conversion not found
* Standard Rounding
* Test:
* SELECT currencyConvert(100,116,100,null,null,null,null) FROM AD_System; => 64.72
************************************************************************/
DECLARE
v_Rate NUMERIC;
BEGIN
-- Return Amount
IF (p_Amount = 0 OR p_CurFrom_ID = p_CurTo_ID) THEN
RETURN p_Amount;
END IF;
-- Return NULL
IF (p_Amount IS NULL OR p_CurFrom_ID IS NULL OR p_CurTo_ID IS NULL) THEN
RETURN NULL;
END IF;
-- Get Rate
v_Rate := currencyRate (p_CurFrom_ID, p_CurTo_ID, p_ConvDate, p_ConversionType_ID, p_Client_ID, p_Org_ID);
IF (v_Rate IS NULL) THEN
RETURN NULL;
END IF;
-- Standard Precision
RETURN currencyRound(p_Amount * v_Rate, p_CurTo_ID, null);
END;
$body$ LANGUAGE plpgsql STABLE;