Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-euro.c -- utilities for EURO currency *
3 : : * *
4 : : * Copyright (C) 2000 Herbert Thoma *
5 : : * *
6 : : * This program is free software; you can redistribute it and/or *
7 : : * modify it under the terms of the GNU General Public License as *
8 : : * published by the Free Software Foundation; either version 2 of *
9 : : * the License, or (at your option) any later version. *
10 : : * *
11 : : * This program is distributed in the hope that it will be useful, *
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : : * GNU General Public License for more details. *
15 : : * *
16 : : * You should have received a copy of the GNU General Public License*
17 : : * along with this program; if not, write to the Free Software *
18 : : * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 : : * *
20 : : \********************************************************************/
21 : :
22 : : #include <string>
23 : : #include <map>
24 : : #include <optional>
25 : :
26 : : #include "gnc-euro.h"
27 : : #include "gnc-session.h"
28 : :
29 : : /* The rates are per EURO and are converted to GncNumeric */
30 : : static const std::map<std::string,double> gnc_euro_rates =
31 : : {
32 : : { "ATS", 13.7603 }, /* austrian schilling */
33 : : { "BEF", 40.3399 }, /* belgian franc */
34 : : { "BGN", 1.95583 }, /* Bulgarian lev */
35 : : { "CYP", .585274 }, /* cyprus pound */
36 : : { "DEM", 1.95583 }, /* german mark */
37 : : { "EEK", 15.6466 }, /* Estonian Kroon */
38 : : { "ESP", 166.386 }, /* spanish peseta */
39 : : { "EUR", 1.00000 }, /* euro */
40 : : { "FIM", 5.94573 }, /* finnmark */
41 : : { "FRF", 6.55957 }, /* french franc */
42 : : { "GRD", 340.750 }, /* greek drachma */
43 : : { "HRK", 7.53450 }, /* Croatian kuna */
44 : : { "IEP", .787564 }, /* irish pound */
45 : : { "ITL", 1936.27 }, /* italian lira */
46 : : { "LUF", 40.3399 }, /* luxembourg franc */
47 : : { "LVL", .702804 }, /* latvian lats */
48 : : { "MTL", .429300 }, /* maltese lira */
49 : : { "NLG", 2.20371 }, /* netherland gulden */
50 : : { "PTE", 200.482 }, /* portuguese escudo */
51 : : { "SIT", 239.640 }, /* slovenian tolar */
52 : : { "SKK", 30.1260 } /* slovak koruna */
53 : : };
54 : :
55 : : static std::optional<double>
56 : 7466 : get_euro_rate (const gnc_commodity * currency)
57 : : {
58 : 7466 : if (!currency || !gnc_commodity_is_iso(currency))
59 : 0 : return {};
60 : :
61 : 7466 : auto it = gnc_euro_rates.find (gnc_commodity_get_mnemonic(currency));
62 : 7466 : if (it == gnc_euro_rates.end())
63 : 7411 : return {};
64 : :
65 : 55 : return it->second;
66 : : }
67 : :
68 : : /* ------------------------------------------------------ */
69 : :
70 : : gboolean
71 : 7425 : gnc_is_euro_currency(const gnc_commodity * currency)
72 : : {
73 : 7425 : return get_euro_rate (currency).has_value();
74 : : }
75 : :
76 : : /* ------------------------------------------------------ */
77 : :
78 : : gnc_numeric
79 : 11 : gnc_convert_to_euro(const gnc_commodity * currency, gnc_numeric value)
80 : : {
81 : 11 : auto euro_rate = get_euro_rate (currency);
82 : 11 : if (!euro_rate)
83 : 0 : return gnc_numeric_zero();
84 : :
85 : 11 : auto rate = double_to_gnc_numeric (*euro_rate, 100000, GNC_HOW_RND_ROUND_HALF_UP);
86 : :
87 : : /* round to 2 decimal places */
88 : : /* EC Regulation 1103/97 states we should use "Round half away from zero"
89 : : * See https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX%3A31997R1103&qid=1662917247821
90 : : */
91 : 11 : return gnc_numeric_div (value, rate, 100, GNC_HOW_RND_ROUND_HALF_UP);
92 : : }
93 : :
94 : : /* ------------------------------------------------------ */
95 : :
96 : : gnc_numeric
97 : 10 : gnc_convert_from_euro(const gnc_commodity * currency, gnc_numeric value)
98 : : {
99 : 10 : auto euro_rate = get_euro_rate (currency);
100 : 10 : if (!euro_rate)
101 : 0 : return gnc_numeric_zero();
102 : :
103 : 10 : auto rate = double_to_gnc_numeric (*euro_rate, 100000, GNC_HOW_RND_ROUND_HALF_UP);
104 : :
105 : : /* EC Regulation 1103/97 states we should use "Round half away from zero"
106 : : * See http://europa.eu/legislation_summaries/economic_and_monetary_affairs/institutional_and_economic_framework/l25025_en.htm */
107 : 10 : return gnc_numeric_mul (value, rate, gnc_commodity_get_fraction (currency),
108 : 10 : GNC_HOW_RND_ROUND_HALF_UP);
109 : : }
110 : :
111 : : /* ------------------------------------------------------ */
112 : :
113 : : gnc_numeric
114 : 20 : gnc_euro_currency_get_rate (const gnc_commodity *currency)
115 : : {
116 : 20 : auto euro_rate = get_euro_rate (currency);
117 : 20 : if (!euro_rate)
118 : 0 : return gnc_numeric_zero();
119 : :
120 : 20 : return double_to_gnc_numeric (*euro_rate, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
121 : : }
122 : :
123 : : /* ------------------------------------------------------ */
124 : :
125 : : gnc_commodity *
126 : 0 : gnc_get_euro (void)
127 : : {
128 : 0 : QofBook* book = qof_session_get_book (gnc_get_current_session ());
129 : 0 : gnc_commodity_table *table = gnc_commodity_table_get_table (book);
130 : :
131 : 0 : return gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, "EUR");
132 : : }
|