src-db/database/model/functions/C_CURRENCY_RATE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Thu, 24 Apr 2008 19:34:53 +0000
changeset 771 f0f606b4acf4
parent 756 ae11e4610537
child 778 6150609a1a0a
permissions -rw-r--r--
Update database files with the correct license
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.
juanpablo@771
    27
carlos@0
    28
* Software distributed under the License is distributed on an
carlos@0
    29
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    30
* implied. See the License for the specific language governing rights
juanpablo@771
    31
carlos@0
    32
* and limitations under the License.
juanpablo@771
    33
* The Original Code is  Compiere  ERP \&  Business Solution
juanpablo@771
    34
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
juanpablo@771
    35
carlos@0
    36
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    37
* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    38
* All Rights Reserved.
carlos@0
    39
* Contributor(s): Openbravo SL
juanpablo@771
    40
juanpablo@771
    41
* Contributions are Copyright (C) 1999-2008 Openbravo, S.L
juanpablo@771
    42
*
juanpablo@771
    43
* Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    44
* file and version.
carlos@0
    45
*************************************************************************
carlos@0
    46
* $Id: C_Currency_Rate.sql,v 1.5 2003/03/17 20:32:24 jjanke Exp $
carlos@0
    47
***
carlos@0
    48
* Title: Return Conversion Rate
carlos@0
    49
* Description:
carlos@0
    50
*  from CurrencyFrom_ID to CurrencyTo_ID
carlos@0
    51
*  Returns NULL, if rate not found
carlos@0
    52
* Test
gorkaion@239
    53
*  SELECT C_Currency_Rate(116, 100, null, null) FROM DUAL; => .647169
carlos@0
    54
************************************************************************/
carlos@0
    55
  -- Currency From variables
carlos@0
    56
  v_cf_IsEuro      char(1);
carlos@0
    57
  v_cf_IsEMUMember char(1);
carlos@0
    58
  v_cf_EMUEntryDate DATE;
carlos@0
    59
  v_cf_EMURate NUMBER;
carlos@0
    60
  -- Currency To variables
carlos@0
    61
  v_ct_IsEuro      CHAR(1) ;
carlos@0
    62
  v_ct_IsEMUMember CHAR(1) ;
carlos@0
    63
  v_ct_EMUEntryDate DATE;
carlos@0
    64
  v_ct_EMURate NUMBER;
carlos@0
    65
  -- Triangle
carlos@0
    66
  v_CurrencyFrom NUMBER;
carlos@0
    67
  v_CurrencyTo   NUMBER;
carlos@0
    68
  v_CurrencyEuro NUMBER;
carlos@0
    69
  --
antonio@735
    70
  v_ConvDate DATE := now();
carlos@0
    71
  v_RateType CHAR := 'S';
carlos@0
    72
  v_Rate     NUMBER;
carlos@0
    73
BEGIN
carlos@0
    74
  -- No Conversion
carlos@0
    75
  IF(p_CurFrom_ID = p_CurTo_ID) THEN
carlos@0
    76
    RETURN 1;
carlos@0
    77
  END IF;
carlos@0
    78
  -- Default Parameter
carlos@0
    79
  IF(p_ConvDate IS NOT NULL) THEN
carlos@0
    80
    v_ConvDate := p_ConvDate;
carlos@0
    81
  END IF;
carlos@0
    82
  IF(p_RateType IS NOT NULL) THEN
carlos@0
    83
    v_RateType := p_RateType;
carlos@0
    84
  END IF;
carlos@0
    85
  -- Get Currency Info
carlos@0
    86
  SELECT to_char(max(IsEuro)),
carlos@0
    87
    to_char(max(IsEMUMember)),
antonio@735
    88
    TO_DATE(MAX(EMUEntryDate)),
carlos@0
    89
    MAX(EMURate)
carlos@0
    90
  INTO   v_cf_IsEuro,
carlos@0
    91
    v_cf_IsEMUMember,
carlos@0
    92
    v_cf_EMUEntryDate,
carlos@0
    93
    v_cf_EMURate
carlos@0
    94
  FROM C_Currency
carlos@0
    95
  WHERE C_Currency_ID = p_CurFrom_ID;
carlos@0
    96
  -- Not Found
carlos@0
    97
  IF(v_cf_IsEuro IS NULL) THEN
carlos@0
    98
    DBMS_OUTPUT.PUT_LINE('From Currency Not Found') ;
carlos@0
    99
    RETURN NULL;
carlos@0
   100
  END IF;
carlos@0
   101
  SELECT TO_CHAR(MAX(IsEuro)),
carlos@0
   102
    TO_CHAR(MAX(IsEMUMember)),
carlos@0
   103
    TO_DATE(MAX(EMUEntryDate)),
carlos@0
   104
    MAX(EMURate)
carlos@0
   105
  INTO v_ct_IsEuro,
carlos@0
   106
    v_ct_IsEMUMember,
carlos@0
   107
    v_ct_EMUEntryDate,
carlos@0
   108
    v_ct_EMURate
carlos@0
   109
  FROM C_Currency
carlos@0
   110
  WHERE C_Currency_ID = p_CurTo_ID;
carlos@0
   111
  -- Not Found
carlos@0
   112
  IF(v_ct_IsEuro IS NULL) THEN
carlos@0
   113
    DBMS_OUTPUT.PUT_LINE('To Currency Not Found') ;
carlos@0
   114
    RETURN NULL;
carlos@0
   115
  END IF;
carlos@0
   116
  -- Fixed - From Euro to EMU
gorkaion@239
   117
  IF(v_cf_IsEuro = 'Y' AND v_ct_IsEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
carlos@0
   118
    RETURN v_ct_EMURate;
carlos@0
   119
  END IF;
carlos@0
   120
  -- Fixed - From EMU to Euro
gorkaion@239
   121
  IF(v_ct_IsEuro = 'Y' AND v_cf_IsEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   122
    RETURN 1 / v_cf_EMURate;
carlos@0
   123
  END IF;
carlos@0
   124
  -- Fixed - From EMU to EMU
gorkaion@239
   125
  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
   126
    RETURN v_ct_EMURate / v_cf_EMURate;
carlos@0
   127
  END IF;
carlos@0
   128
  -- Flexible Rates
carlos@0
   129
  v_CurrencyFrom := p_CurFrom_ID;
carlos@0
   130
  v_CurrencyTo := p_CurTo_ID;
carlos@0
   131
  -- if EMU Member involved, replace From/To Currency
gorkaion@239
   132
  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
   133
    SELECT MAX(C_Currency_ID)
carlos@0
   134
    INTO v_CurrencyEuro
carlos@0
   135
    FROM C_Currency
carlos@0
   136
    WHERE IsEuro = 'Y';
carlos@0
   137
    -- Conversion Rate not Found
carlos@0
   138
    IF(v_CurrencyEuro IS NULL) THEN
carlos@0
   139
      DBMS_OUTPUT.PUT_LINE('Euro Not Found') ;
carlos@0
   140
      RETURN NULL;
carlos@0
   141
    END IF;
gorkaion@239
   142
    IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   143
      v_CurrencyFrom := v_CurrencyEuro;
carlos@0
   144
    ELSE
carlos@0
   145
      v_CurrencyTo := v_CurrencyEuro;
carlos@0
   146
    END IF;
carlos@0
   147
  END IF;
carlos@0
   148
  -- Get Rate
carlos@0
   149
  DECLARE
carlos@0
   150
  TYPE RECORD IS REF CURSOR;
carlos@0
   151
    CUR_Rate RECORD;
carlos@0
   152
  BEGIN
carlos@0
   153
    FOR CUR_Rate IN
carlos@0
   154
      (SELECT MultiplyRate
carlos@0
   155
      FROM C_Conversion_Rate
carlos@0
   156
      WHERE C_Currency_ID = v_CurrencyFrom
carlos@0
   157
        AND C_Currency_ID_To = v_CurrencyTo
carlos@0
   158
        AND ConversionRateType = v_RateType
carlos@0
   159
        AND v_ConvDate BETWEEN ValidFrom AND ValidTo
carlos@0
   160
        AND AD_Client_ID IN(0, p_Client_ID)
carlos@0
   161
        AND AD_Org_ID IN(0, p_Org_ID)
carlos@0
   162
      ORDER BY AD_Client_ID DESC,
carlos@0
   163
        AD_Org_ID DESC,
carlos@0
   164
        ValidFrom DESC
carlos@0
   165
      )
carlos@0
   166
    LOOP
carlos@0
   167
      v_Rate := CUR_Rate.MultiplyRate;
carlos@0
   168
      EXIT; -- only first
carlos@0
   169
    END LOOP;
carlos@0
   170
  END;
carlos@0
   171
  -- Not found
carlos@0
   172
  IF(v_Rate IS NULL) THEN
carlos@0
   173
    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
   174
    DBMS_OUTPUT.PUT_LINE('Conversion Rate Not Found') ;
carlos@0
   175
    RETURN NULL;
carlos@0
   176
  END IF;
carlos@0
   177
  -- Currency From was EMU
gorkaion@239
   178
  IF(v_cf_isEMUMember = 'Y' AND v_ConvDate >= v_cf_EMUEntryDate) THEN
carlos@0
   179
    RETURN v_Rate / v_cf_EMURate;
carlos@0
   180
  END IF;
carlos@0
   181
  -- Currency To was EMU
gorkaion@239
   182
  IF(v_ct_isEMUMember = 'Y' AND v_ConvDate >= v_ct_EMUEntryDate) THEN
carlos@0
   183
    RETURN v_Rate * v_ct_EMURate;
carlos@0
   184
  END IF;
carlos@0
   185
  RETURN v_Rate;
carlos@0
   186
EXCEPTION
carlos@0
   187
WHEN OTHERS THEN
carlos@0
   188
  DBMS_OUTPUT.PUT_LINE(SQLERRM) ;
carlos@0
   189
  RETURN NULL;
antonio@735
   190
END C_CURRENCY_RATE
gorkaion@239
   191
]]></body>
adrian@94
   192
    </function>
adrian@94
   193
  </database>