src-db/database/model/functions/C_CURRENCY_RATE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Mon, 28 Apr 2008 06:36:50 +0000
changeset 781 d68cf344696f
parent 778 6150609a1a0a
child 785 8dba91261590
permissions -rw-r--r--
Update copyright years in database files
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@781
    37
* Contributions are Copyright (C) 2001-2008 Openbravo, S.
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
  v_cf_EMUEntryDate DATE;
carlos@0
    55
  v_cf_EMURate NUMBER;
carlos@0
    56
  -- Currency To variables
carlos@0
    57
  v_ct_IsEuro      CHAR(1) ;
carlos@0
    58
  v_ct_IsEMUMember CHAR(1) ;
carlos@0
    59
  v_ct_EMUEntryDate DATE;
carlos@0
    60
  v_ct_EMURate NUMBER;
carlos@0
    61
  -- Triangle
carlos@0
    62
  v_CurrencyFrom NUMBER;
carlos@0
    63
  v_CurrencyTo   NUMBER;
carlos@0
    64
  v_CurrencyEuro NUMBER;
carlos@0
    65
  --
antonio@735
    66
  v_ConvDate DATE := now();
carlos@0
    67
  v_RateType CHAR := 'S';
carlos@0
    68
  v_Rate     NUMBER;
carlos@0
    69
BEGIN
carlos@0
    70
  -- No Conversion
carlos@0
    71
  IF(p_CurFrom_ID = p_CurTo_ID) THEN
carlos@0
    72
    RETURN 1;
carlos@0
    73
  END IF;
carlos@0
    74
  -- Default Parameter
carlos@0
    75
  IF(p_ConvDate IS NOT NULL) THEN
carlos@0
    76
    v_ConvDate := p_ConvDate;
carlos@0
    77
  END IF;
carlos@0
    78
  IF(p_RateType IS NOT NULL) THEN
carlos@0
    79
    v_RateType := p_RateType;
carlos@0
    80
  END IF;
carlos@0
    81
  -- Get Currency Info
carlos@0
    82
  SELECT to_char(max(IsEuro)),
carlos@0
    83
    to_char(max(IsEMUMember)),
antonio@735
    84
    TO_DATE(MAX(EMUEntryDate)),
carlos@0
    85
    MAX(EMURate)
carlos@0
    86
  INTO   v_cf_IsEuro,
carlos@0
    87
    v_cf_IsEMUMember,
carlos@0
    88
    v_cf_EMUEntryDate,
carlos@0
    89
    v_cf_EMURate
carlos@0
    90
  FROM C_Currency
carlos@0
    91
  WHERE C_Currency_ID = p_CurFrom_ID;
carlos@0
    92
  -- Not Found
carlos@0
    93
  IF(v_cf_IsEuro IS NULL) THEN
carlos@0
    94
    DBMS_OUTPUT.PUT_LINE('From Currency Not Found') ;
carlos@0
    95
    RETURN NULL;
carlos@0
    96
  END IF;
carlos@0
    97
  SELECT TO_CHAR(MAX(IsEuro)),
carlos@0
    98
    TO_CHAR(MAX(IsEMUMember)),
carlos@0
    99
    TO_DATE(MAX(EMUEntryDate)),
carlos@0
   100
    MAX(EMURate)
carlos@0
   101
  INTO v_ct_IsEuro,
carlos@0
   102
    v_ct_IsEMUMember,
carlos@0
   103
    v_ct_EMUEntryDate,
carlos@0
   104
    v_ct_EMURate
carlos@0
   105
  FROM C_Currency
carlos@0
   106
  WHERE C_Currency_ID = p_CurTo_ID;
carlos@0
   107
  -- Not Found
carlos@0
   108
  IF(v_ct_IsEuro IS NULL) THEN
carlos@0
   109
    DBMS_OUTPUT.PUT_LINE('To Currency Not Found') ;
carlos@0
   110
    RETURN NULL;
carlos@0
   111
  END IF;
carlos@0
   112
  -- Fixed - From Euro to EMU
gorkaion@239
   113
  IF(v_cf_IsEuro = 'Y' AND v_ct_IsEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
carlos@0
   114
    RETURN v_ct_EMURate;
carlos@0
   115
  END IF;
carlos@0
   116
  -- Fixed - From EMU to Euro
gorkaion@239
   117
  IF(v_ct_IsEuro = 'Y' AND v_cf_IsEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   118
    RETURN 1 / v_cf_EMURate;
carlos@0
   119
  END IF;
carlos@0
   120
  -- Fixed - From EMU to EMU
gorkaion@239
   121
  IF(v_cf_IsEMUMember = 'Y' AND v_cf_IsEMUMember = 'Y'  AND v_ConvDate >= v_cf_EMUEntryDate AND v_ConvDate >= v_ct_EMUEntryDate) THEN
carlos@0
   122
    RETURN v_ct_EMURate / v_cf_EMURate;
carlos@0
   123
  END IF;
carlos@0
   124
  -- Flexible Rates
carlos@0
   125
  v_CurrencyFrom := p_CurFrom_ID;
carlos@0
   126
  v_CurrencyTo := p_CurTo_ID;
carlos@0
   127
  -- if EMU Member involved, replace From/To Currency
gorkaion@239
   128
  IF((v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) OR(v_ct_isEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate)) THEN
carlos@0
   129
    SELECT MAX(C_Currency_ID)
carlos@0
   130
    INTO v_CurrencyEuro
carlos@0
   131
    FROM C_Currency
carlos@0
   132
    WHERE IsEuro = 'Y';
carlos@0
   133
    -- Conversion Rate not Found
carlos@0
   134
    IF(v_CurrencyEuro IS NULL) THEN
carlos@0
   135
      DBMS_OUTPUT.PUT_LINE('Euro Not Found') ;
carlos@0
   136
      RETURN NULL;
carlos@0
   137
    END IF;
gorkaion@239
   138
    IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   139
      v_CurrencyFrom := v_CurrencyEuro;
carlos@0
   140
    ELSE
carlos@0
   141
      v_CurrencyTo := v_CurrencyEuro;
carlos@0
   142
    END IF;
carlos@0
   143
  END IF;
carlos@0
   144
  -- Get Rate
carlos@0
   145
  DECLARE
carlos@0
   146
  TYPE RECORD IS REF CURSOR;
carlos@0
   147
    CUR_Rate RECORD;
carlos@0
   148
  BEGIN
carlos@0
   149
    FOR CUR_Rate IN
carlos@0
   150
      (SELECT MultiplyRate
carlos@0
   151
      FROM C_Conversion_Rate
carlos@0
   152
      WHERE C_Currency_ID = v_CurrencyFrom
carlos@0
   153
        AND C_Currency_ID_To = v_CurrencyTo
carlos@0
   154
        AND ConversionRateType = v_RateType
carlos@0
   155
        AND v_ConvDate BETWEEN ValidFrom AND ValidTo
carlos@0
   156
        AND AD_Client_ID IN(0, p_Client_ID)
carlos@0
   157
        AND AD_Org_ID IN(0, p_Org_ID)
carlos@0
   158
      ORDER BY AD_Client_ID DESC,
carlos@0
   159
        AD_Org_ID DESC,
carlos@0
   160
        ValidFrom DESC
carlos@0
   161
      )
carlos@0
   162
    LOOP
carlos@0
   163
      v_Rate := CUR_Rate.MultiplyRate;
carlos@0
   164
      EXIT; -- only first
carlos@0
   165
    END LOOP;
carlos@0
   166
  END;
carlos@0
   167
  -- Not found
carlos@0
   168
  IF(v_Rate IS NULL) THEN
carlos@0
   169
    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
   170
    DBMS_OUTPUT.PUT_LINE('Conversion Rate Not Found') ;
carlos@0
   171
    RETURN NULL;
carlos@0
   172
  END IF;
carlos@0
   173
  -- Currency From was EMU
gorkaion@239
   174
  IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   175
    RETURN v_Rate / v_cf_EMURate;
carlos@0
   176
  END IF;
carlos@0
   177
  -- Currency To was EMU
gorkaion@239
   178
  IF(v_ct_isEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
carlos@0
   179
    RETURN v_Rate * v_ct_EMURate;
carlos@0
   180
  END IF;
carlos@0
   181
  RETURN v_Rate;
carlos@0
   182
EXCEPTION
carlos@0
   183
WHEN OTHERS THEN
carlos@0
   184
  DBMS_OUTPUT.PUT_LINE(SQLERRM) ;
carlos@0
   185
  RETURN NULL;
antonio@735
   186
END C_CURRENCY_RATE
gorkaion@239
   187
]]></body>
adrian@94
   188
    </function>
adrian@94
   189
  </database>