OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
25
 
26
import java.math.BigDecimal;
132 ilm 27
import java.util.ArrayList;
93 ilm 28
import java.util.Arrays;
29
import java.util.Calendar;
30
import java.util.Date;
31
import java.util.List;
32
import java.util.TimeZone;
33
 
34
public class CurrencyConverter {
35
    private String companyCurrencyCode;
36
    private final DBRoot root;
37
 
38
    private String baseCurrencyCode;
39
 
40
    public CurrencyConverter(DBRoot rootSociete, String companyCurrencyCode, String baseCurrencyCode) {
41
        if (companyCurrencyCode == null || companyCurrencyCode.isEmpty()) {
42
            this.companyCurrencyCode = "EUR";
43
        } else {
44
            this.companyCurrencyCode = companyCurrencyCode.trim().toUpperCase();
45
        }
46
        this.baseCurrencyCode = baseCurrencyCode;
47
        this.root = rootSociete;
48
    }
49
 
50
    public CurrencyConverter() {
142 ilm 51
        this(ComptaPropsConfiguration.getInstanceCompta().getRootSociete(), ComptaPropsConfiguration.getInstanceCompta().getCurrency().getCode(), "EUR");
93 ilm 52
    }
53
 
54
    public String getCompanyCurrencyCode() {
55
        return companyCurrencyCode;
56
    }
57
 
58
    public String getBaseCurrencyCode() {
59
        return baseCurrencyCode;
60
    }
61
 
62
    /**
63
     * Converter an amount to an other currency
132 ilm 64
     */
93 ilm 65
    public BigDecimal convert(BigDecimal amount, String from, String to) {
66
        return convert(amount, from, to, Calendar.getInstance().getTime());
67
    }
68
 
69
    /**
70
     * Converter an amount to an other currency at a precise date
132 ilm 71
     */
93 ilm 72
    public BigDecimal convert(BigDecimal amount, String from, String to, Date date) {
73
        return convert(amount, from, to, date, false);
74
    }
75
 
76
    /**
77
     * Converter an amount to an other currency at a precise date
132 ilm 78
     */
93 ilm 79
    public BigDecimal convert(BigDecimal amount, String from, String to, Date date, boolean useBiased) {
80
 
81
        if (from.equalsIgnoreCase(to)) {
82
            return amount;
83
        }
84
 
85
        // Clean date
86
        final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
87
        c.setTimeInMillis(date.getTime());
88
        c.set(Calendar.HOUR_OF_DAY, 0);
89
        c.set(Calendar.MINUTE, 0);
90
        c.set(Calendar.SECOND, 0);
91
        c.set(Calendar.MILLISECOND, 0);
92
        final Date d = c.getTime();
93
 
94
        // Get conversion info
132 ilm 95
        final List<SQLRow> rowsFrom = getRates(from, d);
96
        final List<SQLRow> rowsTo = getRates(to, d);
93 ilm 97
        BigDecimal r1 = null;
98
        BigDecimal r2 = null;
132 ilm 99
 
100
        // Récupération des taux par défaut dans DEVISE si aucun taux dans l'historique
101
        List<SQLRow> rowsDevise = new ArrayList<SQLRow>();
102
        if (rowsTo.isEmpty() || rowsFrom.isEmpty()) {
103
            SQLSelect sel = new SQLSelect();
104
            sel.addSelectStar(root.findTable("DEVISE"));
105
            rowsDevise.addAll(SQLRowListRSH.execute(sel));
106
        }
107
 
108
        if (rowsTo.isEmpty()) {
109
            for (SQLRow sqlRow : rowsDevise) {
110
                if (sqlRow.getString("CODE").equalsIgnoreCase(to)) {
111
                    r2 = (useBiased ? sqlRow.getBigDecimal("TAUX_COMMERCIAL") : sqlRow.getBigDecimal("TAUX"));
112
                }
113
            }
114
        }
115
        if (rowsFrom.isEmpty()) {
116
            for (SQLRow sqlRow : rowsDevise) {
117
                if (sqlRow.getString("CODE").equalsIgnoreCase(from)) {
118
                    r1 = (useBiased ? sqlRow.getBigDecimal("TAUX_COMMERCIAL") : sqlRow.getBigDecimal("TAUX"));
119
                }
120
            }
121
        }
122
 
123
        List<SQLRow> rows = new ArrayList<SQLRow>();
124
        rows.addAll(rowsTo);
125
        rows.addAll(rowsFrom);
93 ilm 126
        for (SQLRow sqlRow : rows) {
127
            if (sqlRow.getString("DST").equals(from)) {
128
                if (useBiased) {
129
                    r1 = sqlRow.getBigDecimal("TAUX_COMMERCIAL");
130
                } else {
131
                    r1 = sqlRow.getBigDecimal("TAUX");
132
                }
133
            }
134
            if (sqlRow.getString("DST").equals(to)) {
135
                if (useBiased) {
136
                    r2 = sqlRow.getBigDecimal("TAUX_COMMERCIAL");
137
                } else {
138
                    r2 = sqlRow.getBigDecimal("TAUX");
139
                }
140
            }
141
        }
142
        if (from.equals(this.baseCurrencyCode)) {
143
            r1 = BigDecimal.ONE;
144
        }
145
        if (to.equals(this.baseCurrencyCode)) {
146
            r2 = BigDecimal.ONE;
147
        }
148
        if (r1 == null) {
149
            throw new IllegalStateException("No conversion rate for " + from);
150
        }
151
        if (r2 == null) {
152
            throw new IllegalStateException("No conversion rate for " + to);
153
        }
154
        final BigDecimal result = amount.multiply(r2, DecimalUtils.HIGH_PRECISION).divide(r1, DecimalUtils.HIGH_PRECISION);
155
        return result;
156
    }
132 ilm 157
 
158
    public List<SQLRow> getRates(String currencyCode, final Date d) {
159
        final SQLSelect select = new SQLSelect();
160
        final SQLTable t = this.root.getTable("DEVISE_HISTORIQUE");
161
        select.addAllSelect(t, Arrays.asList("ID", "DATE", "SRC", "DST", "TAUX", "TAUX_COMMERCIAL"));
162
        Where w = new Where(t.getField("SRC"), "=", baseCurrencyCode);
163
        w = w.and(new Where(t.getField("DST"), "=", currencyCode));
164
        w = w.and(new Where(t.getField("DATE"), "<=", d));
165
        select.setWhere(w);
166
        select.addFieldOrder(t.getField("DATE"), Order.desc());
167
        select.setLimit(2);
168
        final List<SQLRow> rows = SQLRowListRSH.execute(select);
169
        return rows;
170
    }
93 ilm 171
}