src-db/database/model/functions/C_CURRENCY_RATE.xml
changeset 756 ae11e4610537
parent 735 daced7e311c9
child 771 f0f606b4acf4
equal deleted inserted replaced
755:16b55e593268 756:ae11e4610537
       
     1 <?xml version="1.0"?>
       
     2   <database name="FUNCTION C_CURRENCY_RATE">
       
     3     <function name="C_CURRENCY_RATE" type="NUMERIC">
       
     4       <parameter name="p_curfrom_id" type="NUMERIC" mode="in">
       
     5         <default/>
       
     6       </parameter>
       
     7       <parameter name="p_curto_id" type="NUMERIC" mode="in">
       
     8         <default/>
       
     9       </parameter>
       
    10       <parameter name="p_convdate" type="TIMESTAMP" mode="in">
       
    11         <default/>
       
    12       </parameter>
       
    13       <parameter name="p_ratetype" type="CHAR" mode="in">
       
    14         <default/>
       
    15       </parameter>
       
    16       <parameter name="p_client_id" type="NUMERIC" mode="in">
       
    17         <default><![CDATA[0]]></default>
       
    18       </parameter>
       
    19       <parameter name="p_org_id" type="NUMERIC" mode="in">
       
    20         <default><![CDATA[0]]></default>
       
    21       </parameter>
       
    22       <body><![CDATA[/*************************************************************************
       
    23 * The contents of this file are subject to the Compiere License
       
    24 * Version 2.5.0 ("License"); You may not use this file except in
       
    25 * compliance with the License. You may obtain a copy of the License at
       
    26 * http://www.compiere.org/license.html
       
    27 * Software distributed under the License is distributed on an
       
    28 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
       
    29 * implied. See the License for the specific language governing rights
       
    30 * and limitations under the License.
       
    31 * The Original Code is  Compiere  ERP &  Business Solution
       
    32 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere,
       
    33 Inc.
       
    34 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
       
    35 * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
       
    36 * All Rights Reserved.
       
    37 * Contributor(s): Openbravo SL
       
    38 * Contributions are Copyright(C) 1999-2005 Openbravo, S.L
       
    39 *************************************************************************
       
    40 * $Id: C_Currency_Rate.sql,v 1.5 2003/03/17 20:32:24 jjanke Exp $
       
    41 ***
       
    42 * Title: Return Conversion Rate
       
    43 * Description:
       
    44 *  from CurrencyFrom_ID to CurrencyTo_ID
       
    45 *  Returns NULL, if rate not found
       
    46 * Test
       
    47 *  SELECT C_Currency_Rate(116, 100, null, null) FROM DUAL; => .647169
       
    48 ************************************************************************/
       
    49   -- Currency From variables
       
    50   v_cf_IsEuro      char(1);
       
    51   v_cf_IsEMUMember char(1);
       
    52   v_cf_EMUEntryDate DATE;
       
    53   v_cf_EMURate NUMBER;
       
    54   -- Currency To variables
       
    55   v_ct_IsEuro      CHAR(1) ;
       
    56   v_ct_IsEMUMember CHAR(1) ;
       
    57   v_ct_EMUEntryDate DATE;
       
    58   v_ct_EMURate NUMBER;
       
    59   -- Triangle
       
    60   v_CurrencyFrom NUMBER;
       
    61   v_CurrencyTo   NUMBER;
       
    62   v_CurrencyEuro NUMBER;
       
    63   --
       
    64   v_ConvDate DATE := now();
       
    65   v_RateType CHAR := 'S';
       
    66   v_Rate     NUMBER;
       
    67 BEGIN
       
    68   -- No Conversion
       
    69   IF(p_CurFrom_ID = p_CurTo_ID) THEN
       
    70     RETURN 1;
       
    71   END IF;
       
    72   -- Default Parameter
       
    73   IF(p_ConvDate IS NOT NULL) THEN
       
    74     v_ConvDate := p_ConvDate;
       
    75   END IF;
       
    76   IF(p_RateType IS NOT NULL) THEN
       
    77     v_RateType := p_RateType;
       
    78   END IF;
       
    79   -- Get Currency Info
       
    80   SELECT to_char(max(IsEuro)),
       
    81     to_char(max(IsEMUMember)),
       
    82     TO_DATE(MAX(EMUEntryDate)),
       
    83     MAX(EMURate)
       
    84   INTO   v_cf_IsEuro,
       
    85     v_cf_IsEMUMember,
       
    86     v_cf_EMUEntryDate,
       
    87     v_cf_EMURate
       
    88   FROM C_Currency
       
    89   WHERE C_Currency_ID = p_CurFrom_ID;
       
    90   -- Not Found
       
    91   IF(v_cf_IsEuro IS NULL) THEN
       
    92     DBMS_OUTPUT.PUT_LINE('From Currency Not Found') ;
       
    93     RETURN NULL;
       
    94   END IF;
       
    95   SELECT TO_CHAR(MAX(IsEuro)),
       
    96     TO_CHAR(MAX(IsEMUMember)),
       
    97     TO_DATE(MAX(EMUEntryDate)),
       
    98     MAX(EMURate)
       
    99   INTO v_ct_IsEuro,
       
   100     v_ct_IsEMUMember,
       
   101     v_ct_EMUEntryDate,
       
   102     v_ct_EMURate
       
   103   FROM C_Currency
       
   104   WHERE C_Currency_ID = p_CurTo_ID;
       
   105   -- Not Found
       
   106   IF(v_ct_IsEuro IS NULL) THEN
       
   107     DBMS_OUTPUT.PUT_LINE('To Currency Not Found') ;
       
   108     RETURN NULL;
       
   109   END IF;
       
   110   -- Fixed - From Euro to EMU
       
   111   IF(v_cf_IsEuro = 'Y' AND v_ct_IsEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
       
   112     RETURN v_ct_EMURate;
       
   113   END IF;
       
   114   -- Fixed - From EMU to Euro
       
   115   IF(v_ct_IsEuro = 'Y' AND v_cf_IsEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
       
   116     RETURN 1 / v_cf_EMURate;
       
   117   END IF;
       
   118   -- Fixed - From EMU to EMU
       
   119   IF(v_cf_IsEMUMember = 'Y' AND v_cf_IsEMUMember = 'Y'  AND v_ConvDate >= v_cf_EMUEntryDate AND v_ConvDate >= v_ct_EMUEntryDate) THEN
       
   120     RETURN v_ct_EMURate / v_cf_EMURate;
       
   121   END IF;
       
   122   -- Flexible Rates
       
   123   v_CurrencyFrom := p_CurFrom_ID;
       
   124   v_CurrencyTo := p_CurTo_ID;
       
   125   -- if EMU Member involved, replace From/To Currency
       
   126   IF((v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) OR(v_ct_isEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate)) THEN
       
   127     SELECT MAX(C_Currency_ID)
       
   128     INTO v_CurrencyEuro
       
   129     FROM C_Currency
       
   130     WHERE IsEuro = 'Y';
       
   131     -- Conversion Rate not Found
       
   132     IF(v_CurrencyEuro IS NULL) THEN
       
   133       DBMS_OUTPUT.PUT_LINE('Euro Not Found') ;
       
   134       RETURN NULL;
       
   135     END IF;
       
   136     IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
       
   137       v_CurrencyFrom := v_CurrencyEuro;
       
   138     ELSE
       
   139       v_CurrencyTo := v_CurrencyEuro;
       
   140     END IF;
       
   141   END IF;
       
   142   -- Get Rate
       
   143   DECLARE
       
   144   TYPE RECORD IS REF CURSOR;
       
   145     CUR_Rate RECORD;
       
   146   BEGIN
       
   147     FOR CUR_Rate IN
       
   148       (SELECT MultiplyRate
       
   149       FROM C_Conversion_Rate
       
   150       WHERE C_Currency_ID = v_CurrencyFrom
       
   151         AND C_Currency_ID_To = v_CurrencyTo
       
   152         AND ConversionRateType = v_RateType
       
   153         AND v_ConvDate BETWEEN ValidFrom AND ValidTo
       
   154         AND AD_Client_ID IN(0, p_Client_ID)
       
   155         AND AD_Org_ID IN(0, p_Org_ID)
       
   156       ORDER BY AD_Client_ID DESC,
       
   157         AD_Org_ID DESC,
       
   158         ValidFrom DESC
       
   159       )
       
   160     LOOP
       
   161       v_Rate := CUR_Rate.MultiplyRate;
       
   162       EXIT; -- only first
       
   163     END LOOP;
       
   164   END;
       
   165   -- Not found
       
   166   IF(v_Rate IS NULL) THEN
       
   167     DBMS_OUTPUT.PUT_LINE('v_CurrencyFrom:'||v_CurrencyFrom||'-v_CurrencyTo:'||v_CurrencyTo||'-v_RateType:'||v_RateType||'-v_ConvDate'||v_ConvDate||'-p_Client_ID:'||p_Client_ID||'-p_Org_ID:'||p_Org_ID) ;
       
   168     DBMS_OUTPUT.PUT_LINE('Conversion Rate Not Found') ;
       
   169     RETURN NULL;
       
   170   END IF;
       
   171   -- Currency From was EMU
       
   172   IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
       
   173     RETURN v_Rate / v_cf_EMURate;
       
   174   END IF;
       
   175   -- Currency To was EMU
       
   176   IF(v_ct_isEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
       
   177     RETURN v_Rate * v_ct_EMURate;
       
   178   END IF;
       
   179   RETURN v_Rate;
       
   180 EXCEPTION
       
   181 WHEN OTHERS THEN
       
   182   DBMS_OUTPUT.PUT_LINE(SQLERRM) ;
       
   183   RETURN NULL;
       
   184 END C_CURRENCY_RATE
       
   185 ]]></body>
       
   186     </function>
       
   187   </database>