src-db/database/model/functions/C_CURRENCY_RATE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 785 8dba91261590
child 1106 f75065410ec7
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_CURRENCY_RATE">
adrian@94
     3
    <function name="C_CURRENCY_RATE" type="NUMERIC">
antonio@735
     4
      <parameter name="p_curfrom_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
antonio@735
     7
      <parameter name="p_curto_id" type="NUMERIC" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
antonio@735
    10
      <parameter name="p_convdate" type="TIMESTAMP" mode="in">
antonio@735
    11
        <default/>
antonio@735
    12
      </parameter>
antonio@735
    13
      <parameter name="p_ratetype" type="CHAR" mode="in">
antonio@735
    14
        <default/>
antonio@735
    15
      </parameter>
antonio@735
    16
      <parameter name="p_client_id" type="NUMERIC" mode="in">
antonio@735
    17
        <default><![CDATA[0]]></default>
antonio@735
    18
      </parameter>
antonio@735
    19
      <parameter name="p_org_id" type="NUMERIC" mode="in">
antonio@735
    20
        <default><![CDATA[0]]></default>
antonio@735
    21
      </parameter>
gorkaion@239
    22
      <body><![CDATA[/*************************************************************************
juanpablo@771
    23
* The contents of this file are subject to the Compiere Public
juanpablo@771
    24
* License 1.1 ("License"); You may not use this file except in
juanpablo@771
    25
* compliance with the License. You may obtain a copy of the License in
juanpablo@771
    26
* the legal folder of your Openbravo installation.
carlos@0
    27
* Software distributed under the License is distributed on an
carlos@0
    28
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    29
* implied. See the License for the specific language governing rights
carlos@0
    30
* and limitations under the License.
juanpablo@778
    31
* The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    32
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    33
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    34
* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    35
* All Rights Reserved.
carlos@0
    36
* Contributor(s): Openbravo SL
juanpablo@785
    37
* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    38
*
juanpablo@771
    39
* Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    40
* file and version.
carlos@0
    41
*************************************************************************
carlos@0
    42
* $Id: C_Currency_Rate.sql,v 1.5 2003/03/17 20:32:24 jjanke Exp $
carlos@0
    43
***
carlos@0
    44
* Title: Return Conversion Rate
carlos@0
    45
* Description:
carlos@0
    46
*  from CurrencyFrom_ID to CurrencyTo_ID
carlos@0
    47
*  Returns NULL, if rate not found
carlos@0
    48
* Test
gorkaion@239
    49
*  SELECT C_Currency_Rate(116, 100, null, null) FROM DUAL; => .647169
carlos@0
    50
************************************************************************/
carlos@0
    51
  -- Currency From variables
carlos@0
    52
  v_cf_IsEuro      char(1);
carlos@0
    53
  v_cf_IsEMUMember char(1);
carlos@0
    54
  -- Triangle
carlos@0
    55
  v_CurrencyFrom NUMBER;
carlos@0
    56
  v_CurrencyTo   NUMBER;
carlos@0
    57
  v_CurrencyEuro NUMBER;
carlos@0
    58
  --
antonio@735
    59
  v_ConvDate DATE := now();
carlos@0
    60
  v_RateType CHAR := 'S';
carlos@0
    61
  v_Rate     NUMBER;
carlos@0
    62
BEGIN
carlos@0
    63
  -- No Conversion
carlos@0
    64
  IF(p_CurFrom_ID = p_CurTo_ID) THEN
carlos@0
    65
    RETURN 1;
carlos@0
    66
  END IF;
carlos@0
    67
  -- Default Parameter
carlos@0
    68
  IF(p_ConvDate IS NOT NULL) THEN
carlos@0
    69
    v_ConvDate := p_ConvDate;
carlos@0
    70
  END IF;
carlos@0
    71
  IF(p_RateType IS NOT NULL) THEN
carlos@0
    72
    v_RateType := p_RateType;
carlos@0
    73
  END IF;
carlos@0
    74
  -- Flexible Rates
carlos@0
    75
  v_CurrencyFrom := p_CurFrom_ID;
carlos@0
    76
  v_CurrencyTo := p_CurTo_ID;
carlos@0
    77
  -- Get Rate
carlos@0
    78
  DECLARE
carlos@0
    79
  TYPE RECORD IS REF CURSOR;
carlos@0
    80
    CUR_Rate RECORD;
carlos@0
    81
  BEGIN
carlos@0
    82
    FOR CUR_Rate IN
carlos@0
    83
      (SELECT MultiplyRate
carlos@0
    84
      FROM C_Conversion_Rate
carlos@0
    85
      WHERE C_Currency_ID = v_CurrencyFrom
carlos@0
    86
        AND C_Currency_ID_To = v_CurrencyTo
carlos@0
    87
        AND ConversionRateType = v_RateType
carlos@0
    88
        AND v_ConvDate BETWEEN ValidFrom AND ValidTo
carlos@0
    89
        AND AD_Client_ID IN(0, p_Client_ID)
carlos@0
    90
        AND AD_Org_ID IN(0, p_Org_ID)
carlos@0
    91
      ORDER BY AD_Client_ID DESC,
carlos@0
    92
        AD_Org_ID DESC,
carlos@0
    93
        ValidFrom DESC
carlos@0
    94
      )
carlos@0
    95
    LOOP
carlos@0
    96
      v_Rate := CUR_Rate.MultiplyRate;
carlos@0
    97
      EXIT; -- only first
carlos@0
    98
    END LOOP;
carlos@0
    99
  END;
carlos@0
   100
  -- Not found
carlos@0
   101
  IF(v_Rate IS NULL) THEN
carlos@0
   102
    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) ;
carlos@0
   103
    DBMS_OUTPUT.PUT_LINE('Conversion Rate Not Found') ;
carlos@0
   104
    RETURN NULL;
carlos@0
   105
  END IF;
carlos@0
   106
  -- Currency From was EMU
carlos@0
   107
  RETURN v_Rate;
carlos@0
   108
EXCEPTION
carlos@0
   109
WHEN OTHERS THEN
carlos@0
   110
  DBMS_OUTPUT.PUT_LINE(SQLERRM) ;
carlos@0
   111
  RETURN NULL;
antonio@735
   112
END C_CURRENCY_RATE
gorkaion@239
   113
]]></body>
adrian@94
   114
    </function>
adrian@94
   115
  </database>