2001-08-20 19:24:20 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-euro.c -- utilities for EURO currency *
|
2000-04-12 00:41:58 -05:00
|
|
|
* *
|
|
|
|
* Copyright (C) 2000 Herbert Thoma *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2001-09-07 16:51:13 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-11-01 02:52:30 -06:00
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
#include "gnc-commodity.h"
|
2001-08-20 19:24:20 -05:00
|
|
|
#include "gnc-euro.h"
|
2001-10-03 05:07:45 -05:00
|
|
|
#include "gnc-ui-util.h"
|
2000-09-13 17:33:15 -05:00
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
/* local structs */
|
2001-10-03 05:07:45 -05:00
|
|
|
typedef struct
|
|
|
|
{
|
2000-04-12 00:41:58 -05:00
|
|
|
const char *currency;
|
|
|
|
double rate;
|
2000-10-23 04:41:51 -05:00
|
|
|
} gnc_euro_rate_struct;
|
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
|
|
|
|
/* This array MUST be sorted ! */
|
2001-04-24 05:46:01 -05:00
|
|
|
/* The rates are per EURO */
|
2001-10-03 05:07:45 -05:00
|
|
|
static gnc_euro_rate_struct gnc_euro_rates[] =
|
2000-04-12 00:41:58 -05:00
|
|
|
{
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ATS", 13.7603 }, /* austrian schilling */
|
|
|
|
{ "BEF", 40.3399 }, /* belgian franc */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "BFR", 40.3399 }, /* belgian franc */
|
2008-01-05 10:51:39 -06:00
|
|
|
{ "CYP", .585274 }, /* cyprus pound */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "DEM", 1.95583 }, /* german mark */
|
|
|
|
{ "DM", 1.95583 }, /* german mark */
|
|
|
|
{ "ESC", 200.482 }, /* portugese escudo */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ESP", 166.386 }, /* spanish peseta */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "EUR", 1.00000 }, /* euro */
|
|
|
|
{ "EURO", 1.00000 }, /* euro */
|
|
|
|
{ "FF", 6.55957 }, /* french franc */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "FIM", 5.94573 }, /* finnmark */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "FMK", 5.94573 }, /* finnmark */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "FRF", 6.55957 }, /* french franc */
|
2001-01-11 16:51:26 -06:00
|
|
|
{ "GRD", 340.750 }, /* greek drachma */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "HFL", 2.20371 }, /* netherland gulden */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "IEP", .787564 }, /* irish pound */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "IRP", .787564 }, /* irish pound */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ITL", 1936.27 }, /* italian lira */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "LFR", 40.3399 }, /* luxembourg franc */
|
|
|
|
{ "LIT", 1936.27 }, /* italian lira */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "LUF", 40.3399 }, /* luxembourg franc */
|
2008-01-05 10:51:39 -06:00
|
|
|
{ "MTL", .429300 }, /* maltese lira */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "NLG", 2.20371 }, /* netherland gulden */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "PTA", 166.386 }, /* spanish peseta */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "PTE", 200.482 }, /* portugese escudo */
|
|
|
|
{ "S", 13.7603 }, /* austrian schilling */
|
2008-01-05 10:51:39 -06:00
|
|
|
{ "SCH", 13.7603 }, /* austrian schilling */
|
2009-01-02 04:32:47 -06:00
|
|
|
{ "SIT", 239.640 }, /* slovenian tolar */
|
|
|
|
{ "SKK", 30.1260 } /* slovak koruna */
|
2000-04-12 00:41:58 -05:00
|
|
|
};
|
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
static int
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_compare (const void * key, const void * value)
|
2000-04-12 00:41:58 -05:00
|
|
|
{
|
2000-10-23 04:41:51 -05:00
|
|
|
const gnc_commodity * curr = key;
|
|
|
|
const gnc_euro_rate_struct * euro = value;
|
2000-11-08 00:36:51 -06:00
|
|
|
|
|
|
|
if (!key || !value)
|
|
|
|
return -1;
|
2000-10-23 04:41:51 -05:00
|
|
|
|
|
|
|
return strcasecmp(gnc_commodity_get_mnemonic(curr), euro->currency);
|
2001-10-03 05:07:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gnc_euro_rate_compare_code (const void * key, const void * value)
|
|
|
|
{
|
|
|
|
const char *code = key;
|
|
|
|
const gnc_euro_rate_struct * euro = value;
|
2000-10-23 04:41:51 -05:00
|
|
|
|
2001-10-03 05:07:45 -05:00
|
|
|
if (!key || !value)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return strcasecmp (code, euro->currency);
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-10-27 15:30:30 -05:00
|
|
|
gboolean
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_is_euro_currency_code (const char *code)
|
2001-02-24 18:42:44 -06:00
|
|
|
{
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_struct *result;
|
|
|
|
|
|
|
|
if (!code) return FALSE;
|
2000-04-12 00:41:58 -05:00
|
|
|
|
2001-10-03 05:07:45 -05:00
|
|
|
result = bsearch (code,
|
|
|
|
gnc_euro_rates,
|
|
|
|
sizeof(gnc_euro_rates) / sizeof(gnc_euro_rate_struct),
|
|
|
|
sizeof(gnc_euro_rate_struct),
|
|
|
|
gnc_euro_rate_compare_code);
|
|
|
|
|
|
|
|
return result != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_is_euro_currency(const gnc_commodity * currency)
|
|
|
|
{
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_euro_rate_struct *result;
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
if (currency == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2003-04-29 01:15:34 -05:00
|
|
|
if (!gnc_commodity_is_iso(currency))
|
2000-10-27 15:30:30 -05:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
result = bsearch(currency,
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rates,
|
|
|
|
sizeof(gnc_euro_rates) / sizeof(gnc_euro_rate_struct),
|
2000-10-27 15:30:30 -05:00
|
|
|
sizeof(gnc_euro_rate_struct),
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_compare);
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-11-08 00:36:51 -06:00
|
|
|
gnc_numeric
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_convert_to_euro(const gnc_commodity * currency, gnc_numeric value)
|
|
|
|
{
|
2000-04-12 00:41:58 -05:00
|
|
|
gnc_euro_rate_struct *result;
|
|
|
|
|
2000-10-27 15:30:30 -05:00
|
|
|
if (currency == NULL)
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
2000-10-27 15:30:30 -05:00
|
|
|
|
2003-04-29 01:15:34 -05:00
|
|
|
if (!gnc_commodity_is_iso(currency))
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
result = bsearch(currency,
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rates,
|
|
|
|
sizeof(gnc_euro_rates) / sizeof(gnc_euro_rate_struct),
|
2000-10-27 15:30:30 -05:00
|
|
|
sizeof(gnc_euro_rate_struct),
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_compare);
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
if (result == NULL)
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
/* round to 2 decimal places */
|
2000-11-08 00:36:51 -06:00
|
|
|
{
|
|
|
|
gnc_numeric rate;
|
|
|
|
|
|
|
|
rate = double_to_gnc_numeric (result->rate, 100000, GNC_RND_ROUND);
|
|
|
|
|
2001-12-11 03:16:14 -06:00
|
|
|
/* Which rounding should be used here? H. Thoma said
|
|
|
|
GNC_RND_FLOOR, but I (cstim) think he's wrong -- the official
|
|
|
|
rules say you *have* to use GNC_RND_ROUND! */
|
|
|
|
return gnc_numeric_div (value, rate, 100, GNC_RND_ROUND);
|
2000-11-08 00:36:51 -06:00
|
|
|
}
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-11-08 00:36:51 -06:00
|
|
|
gnc_numeric
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_convert_from_euro(const gnc_commodity * currency, gnc_numeric value)
|
|
|
|
{
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_euro_rate_struct * result;
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
if (currency == NULL)
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
2000-10-27 15:30:30 -05:00
|
|
|
|
2003-04-29 01:15:34 -05:00
|
|
|
if (!gnc_commodity_is_iso(currency))
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
result = bsearch(currency,
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rates,
|
|
|
|
sizeof(gnc_euro_rates) / sizeof(gnc_euro_rate_struct),
|
2000-10-27 15:30:30 -05:00
|
|
|
sizeof(gnc_euro_rate_struct),
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_compare);
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
if (result == NULL)
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_zero ();
|
|
|
|
|
|
|
|
{
|
|
|
|
gnc_numeric rate;
|
|
|
|
|
|
|
|
rate = double_to_gnc_numeric (result->rate, 100000, GNC_RND_ROUND);
|
2000-10-27 15:30:30 -05:00
|
|
|
|
2000-11-08 00:36:51 -06:00
|
|
|
return gnc_numeric_mul (value, rate, gnc_commodity_get_fraction (currency),
|
|
|
|
GNC_RND_ROUND);
|
|
|
|
}
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
2000-11-01 02:52:30 -06:00
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2001-04-24 05:46:01 -05:00
|
|
|
gnc_numeric
|
|
|
|
gnc_euro_currency_get_rate (const gnc_commodity *currency)
|
|
|
|
{
|
|
|
|
gnc_euro_rate_struct * result;
|
|
|
|
|
|
|
|
if (currency == NULL)
|
|
|
|
return gnc_numeric_zero ();
|
|
|
|
|
2003-04-29 01:15:34 -05:00
|
|
|
if (!gnc_commodity_is_iso(currency))
|
2001-04-24 05:46:01 -05:00
|
|
|
return gnc_numeric_zero ();
|
|
|
|
|
|
|
|
result = bsearch(currency,
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rates,
|
|
|
|
sizeof(gnc_euro_rates) / sizeof(gnc_euro_rate_struct),
|
2001-04-24 05:46:01 -05:00
|
|
|
sizeof(gnc_euro_rate_struct),
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_euro_rate_compare);
|
2001-04-24 05:46:01 -05:00
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return gnc_numeric_zero ();
|
|
|
|
|
|
|
|
return double_to_gnc_numeric (result->rate, GNC_DENOM_AUTO,
|
|
|
|
GNC_DENOM_SIGFIGS(6) | GNC_RND_ROUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2001-02-24 18:42:44 -06:00
|
|
|
gnc_commodity *
|
2000-11-01 02:52:30 -06:00
|
|
|
gnc_get_euro (void)
|
|
|
|
{
|
2001-10-03 05:07:45 -05:00
|
|
|
gnc_commodity_table *table;
|
|
|
|
|
|
|
|
table = gnc_book_get_commodity_table (gnc_get_current_book ());
|
|
|
|
|
2006-04-13 21:36:00 -05:00
|
|
|
return gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, "EUR");
|
2000-11-01 02:52:30 -06:00
|
|
|
}
|