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