93 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.erp.core.finance.accounting.model;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
17 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
18 |
import org.openconcerto.sql.model.Order;
|
|
|
19 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
20 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
|
|
21 |
import org.openconcerto.sql.model.SQLSelect;
|
|
|
22 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
23 |
import org.openconcerto.sql.model.Where;
|
|
|
24 |
import org.openconcerto.utils.DecimalUtils;
|
156 |
ilm |
25 |
import org.openconcerto.utils.Tuple2;
|
|
|
26 |
import org.openconcerto.utils.Tuple3;
|
93 |
ilm |
27 |
|
|
|
28 |
import java.math.BigDecimal;
|
132 |
ilm |
29 |
import java.util.ArrayList;
|
93 |
ilm |
30 |
import java.util.Arrays;
|
|
|
31 |
import java.util.Calendar;
|
|
|
32 |
import java.util.Date;
|
156 |
ilm |
33 |
import java.util.HashMap;
|
93 |
ilm |
34 |
import java.util.List;
|
156 |
ilm |
35 |
import java.util.Map;
|
93 |
ilm |
36 |
import java.util.TimeZone;
|
|
|
37 |
|
|
|
38 |
public class CurrencyConverter {
|
|
|
39 |
private String companyCurrencyCode;
|
|
|
40 |
private final DBRoot root;
|
156 |
ilm |
41 |
private boolean useCache = false;
|
93 |
ilm |
42 |
private String baseCurrencyCode;
|
|
|
43 |
|
|
|
44 |
public CurrencyConverter(DBRoot rootSociete, String companyCurrencyCode, String baseCurrencyCode) {
|
|
|
45 |
if (companyCurrencyCode == null || companyCurrencyCode.isEmpty()) {
|
|
|
46 |
this.companyCurrencyCode = "EUR";
|
|
|
47 |
} else {
|
|
|
48 |
this.companyCurrencyCode = companyCurrencyCode.trim().toUpperCase();
|
|
|
49 |
}
|
|
|
50 |
this.baseCurrencyCode = baseCurrencyCode;
|
|
|
51 |
this.root = rootSociete;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public CurrencyConverter() {
|
142 |
ilm |
55 |
this(ComptaPropsConfiguration.getInstanceCompta().getRootSociete(), ComptaPropsConfiguration.getInstanceCompta().getCurrency().getCode(), "EUR");
|
93 |
ilm |
56 |
}
|
|
|
57 |
|
|
|
58 |
public String getCompanyCurrencyCode() {
|
|
|
59 |
return companyCurrencyCode;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public String getBaseCurrencyCode() {
|
|
|
63 |
return baseCurrencyCode;
|
|
|
64 |
}
|
|
|
65 |
|
156 |
ilm |
66 |
public void setUseCache(boolean useCache) {
|
|
|
67 |
this.useCache = useCache;
|
|
|
68 |
}
|
|
|
69 |
|
93 |
ilm |
70 |
/**
|
|
|
71 |
* Converter an amount to an other currency
|
132 |
ilm |
72 |
*/
|
93 |
ilm |
73 |
public BigDecimal convert(BigDecimal amount, String from, String to) {
|
|
|
74 |
return convert(amount, from, to, Calendar.getInstance().getTime());
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Converter an amount to an other currency at a precise date
|
132 |
ilm |
79 |
*/
|
93 |
ilm |
80 |
public BigDecimal convert(BigDecimal amount, String from, String to, Date date) {
|
|
|
81 |
return convert(amount, from, to, date, false);
|
|
|
82 |
}
|
|
|
83 |
|
156 |
ilm |
84 |
Map<Tuple3<String, String, Date>, Tuple2<BigDecimal, BigDecimal>> cacheBiased = new HashMap<Tuple3<String, String, Date>, Tuple2<BigDecimal, BigDecimal>>();
|
|
|
85 |
Map<Tuple3<String, String, Date>, Tuple2<BigDecimal, BigDecimal>> cacheNotBiased = new HashMap<Tuple3<String, String, Date>, Tuple2<BigDecimal, BigDecimal>>();
|
|
|
86 |
|
|
|
87 |
public void clearCache() {
|
|
|
88 |
cacheBiased.clear();
|
|
|
89 |
cacheNotBiased.clear();
|
|
|
90 |
}
|
|
|
91 |
|
93 |
ilm |
92 |
/**
|
|
|
93 |
* Converter an amount to an other currency at a precise date
|
132 |
ilm |
94 |
*/
|
93 |
ilm |
95 |
public BigDecimal convert(BigDecimal amount, String from, String to, Date date, boolean useBiased) {
|
|
|
96 |
|
|
|
97 |
if (from.equalsIgnoreCase(to)) {
|
|
|
98 |
return amount;
|
|
|
99 |
}
|
|
|
100 |
|
156 |
ilm |
101 |
if (amount.signum() == 0) {
|
|
|
102 |
return BigDecimal.ZERO;
|
|
|
103 |
}
|
93 |
ilm |
104 |
|
|
|
105 |
BigDecimal r1 = null;
|
|
|
106 |
BigDecimal r2 = null;
|
132 |
ilm |
107 |
|
156 |
ilm |
108 |
if (useCache) {
|
|
|
109 |
Tuple3<String, String, Date> key = Tuple3.create(from, to, date);
|
|
|
110 |
if (useBiased && cacheBiased.containsKey(key)) {
|
|
|
111 |
Tuple2<BigDecimal, BigDecimal> value = cacheBiased.get(key);
|
|
|
112 |
r1 = value.get0();
|
|
|
113 |
r2 = value.get1();
|
|
|
114 |
} else if (!useBiased && cacheNotBiased.containsKey(key)) {
|
|
|
115 |
Tuple2<BigDecimal, BigDecimal> value = cacheNotBiased.get(key);
|
|
|
116 |
r1 = value.get0();
|
|
|
117 |
r2 = value.get1();
|
|
|
118 |
}
|
132 |
ilm |
119 |
}
|
|
|
120 |
|
156 |
ilm |
121 |
if (r1 == null || r2 == null) {
|
|
|
122 |
// Clean date
|
|
|
123 |
final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
|
|
124 |
c.setTimeInMillis(date.getTime());
|
|
|
125 |
c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
126 |
c.set(Calendar.MINUTE, 0);
|
|
|
127 |
c.set(Calendar.SECOND, 0);
|
|
|
128 |
c.set(Calendar.MILLISECOND, 0);
|
|
|
129 |
final Date d = c.getTime();
|
|
|
130 |
|
|
|
131 |
// Get conversion info
|
|
|
132 |
final List<SQLRow> rowsFrom = getRates(from, d);
|
|
|
133 |
final List<SQLRow> rowsTo = getRates(to, d);
|
|
|
134 |
|
|
|
135 |
List<SQLRow> rows = new ArrayList<SQLRow>();
|
|
|
136 |
rows.addAll(rowsTo);
|
|
|
137 |
rows.addAll(rowsFrom);
|
|
|
138 |
// Pour le taux réel, on récupére celui de la veille
|
|
|
139 |
for (SQLRow sqlRow : rows) {
|
|
|
140 |
if (sqlRow.getString("DST").equals(from)) {
|
|
|
141 |
if (useBiased) {
|
|
|
142 |
if (r1 == null) {
|
|
|
143 |
r1 = sqlRow.getBigDecimal("TAUX_COMMERCIAL");
|
|
|
144 |
}
|
|
|
145 |
} else {
|
|
|
146 |
r1 = sqlRow.getBigDecimal("TAUX");
|
|
|
147 |
}
|
132 |
ilm |
148 |
}
|
156 |
ilm |
149 |
if (sqlRow.getString("DST").equals(to)) {
|
|
|
150 |
if (useBiased) {
|
|
|
151 |
if (r2 == null) {
|
|
|
152 |
r2 = sqlRow.getBigDecimal("TAUX_COMMERCIAL");
|
|
|
153 |
}
|
|
|
154 |
} else {
|
|
|
155 |
r2 = sqlRow.getBigDecimal("TAUX");
|
|
|
156 |
}
|
132 |
ilm |
157 |
}
|
|
|
158 |
}
|
156 |
ilm |
159 |
if (from.equals(this.baseCurrencyCode)) {
|
|
|
160 |
r1 = BigDecimal.ONE;
|
|
|
161 |
}
|
|
|
162 |
if (to.equals(this.baseCurrencyCode)) {
|
|
|
163 |
r2 = BigDecimal.ONE;
|
|
|
164 |
}
|
132 |
ilm |
165 |
|
156 |
ilm |
166 |
if (r1 == null || r2 == null) {
|
|
|
167 |
// Récupération des taux par défaut dans DEVISE si aucun taux dans l'historique
|
|
|
168 |
List<SQLRow> rowsDevise = new ArrayList<SQLRow>();
|
|
|
169 |
if (rowsTo.isEmpty() || rowsFrom.isEmpty()) {
|
|
|
170 |
SQLSelect sel = new SQLSelect();
|
|
|
171 |
sel.addSelectStar(root.findTable("DEVISE"));
|
|
|
172 |
rowsDevise.addAll(SQLRowListRSH.execute(sel));
|
93 |
ilm |
173 |
}
|
156 |
ilm |
174 |
if (r2 == null) {
|
|
|
175 |
for (SQLRow sqlRow : rowsDevise) {
|
|
|
176 |
if (sqlRow.getString("CODE").equalsIgnoreCase(to)) {
|
|
|
177 |
r2 = (useBiased ? sqlRow.getBigDecimal("TAUX_COMMERCIAL") : sqlRow.getBigDecimal("TAUX"));
|
|
|
178 |
}
|
|
|
179 |
}
|
93 |
ilm |
180 |
}
|
156 |
ilm |
181 |
if (r1 == null) {
|
|
|
182 |
for (SQLRow sqlRow : rowsDevise) {
|
|
|
183 |
if (sqlRow.getString("CODE").equalsIgnoreCase(from)) {
|
|
|
184 |
r1 = (useBiased ? sqlRow.getBigDecimal("TAUX_COMMERCIAL") : sqlRow.getBigDecimal("TAUX"));
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
}
|
93 |
ilm |
188 |
}
|
156 |
ilm |
189 |
|
93 |
ilm |
190 |
}
|
|
|
191 |
if (r1 == null) {
|
|
|
192 |
throw new IllegalStateException("No conversion rate for " + from);
|
|
|
193 |
}
|
|
|
194 |
if (r2 == null) {
|
|
|
195 |
throw new IllegalStateException("No conversion rate for " + to);
|
|
|
196 |
}
|
156 |
ilm |
197 |
if (useCache) {
|
|
|
198 |
Tuple3<String, String, Date> key = Tuple3.create(from, to, date);
|
|
|
199 |
|
|
|
200 |
if (useBiased) {
|
|
|
201 |
cacheBiased.put(key, Tuple2.create(r1, r2));
|
|
|
202 |
} else {
|
|
|
203 |
cacheNotBiased.put(key, Tuple2.create(r1, r2));
|
|
|
204 |
}
|
|
|
205 |
}
|
93 |
ilm |
206 |
final BigDecimal result = amount.multiply(r2, DecimalUtils.HIGH_PRECISION).divide(r1, DecimalUtils.HIGH_PRECISION);
|
|
|
207 |
return result;
|
|
|
208 |
}
|
132 |
ilm |
209 |
|
|
|
210 |
public List<SQLRow> getRates(String currencyCode, final Date d) {
|
|
|
211 |
final SQLSelect select = new SQLSelect();
|
|
|
212 |
final SQLTable t = this.root.getTable("DEVISE_HISTORIQUE");
|
|
|
213 |
select.addAllSelect(t, Arrays.asList("ID", "DATE", "SRC", "DST", "TAUX", "TAUX_COMMERCIAL"));
|
|
|
214 |
Where w = new Where(t.getField("SRC"), "=", baseCurrencyCode);
|
|
|
215 |
w = w.and(new Where(t.getField("DST"), "=", currencyCode));
|
|
|
216 |
w = w.and(new Where(t.getField("DATE"), "<=", d));
|
|
|
217 |
select.setWhere(w);
|
|
|
218 |
select.addFieldOrder(t.getField("DATE"), Order.desc());
|
156 |
ilm |
219 |
// Limit pour récupérer le taux de la veille
|
132 |
ilm |
220 |
select.setLimit(2);
|
|
|
221 |
final List<SQLRow> rows = SQLRowListRSH.execute(select);
|
|
|
222 |
return rows;
|
|
|
223 |
}
|
93 |
ilm |
224 |
}
|