OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.tax.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.model.DBRoot;
80 ilm 19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
80 ilm 24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.utils.ExceptionHandler;
18 ilm 26
 
93 ilm 27
import java.util.Collections;
18 ilm 28
import java.util.HashMap;
80 ilm 29
import java.util.LinkedHashMap;
18 ilm 30
import java.util.List;
31
import java.util.Map;
19 ilm 32
import java.util.Set;
18 ilm 33
 
34
public final class TaxeCache {
35
 
65 ilm 36
    static private final SQLSelect getSel() {
80 ilm 37
        // FIXME récupérer les comptes de TVA pour eviter les fetchs
18 ilm 38
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
39
        final SQLTable table = root.getTable("TAXE");
80 ilm 40
        final SQLSelect sel = new SQLSelect();
18 ilm 41
        sel.addSelect(table.getField("ID_TAXE"));
42
        sel.addSelect(table.getField("TAUX"));
80 ilm 43
        sel.addSelect(table.getField("DEFAULT"));
65 ilm 44
        return sel;
45
    }
46
 
47
    private transient final Map<Integer, Float> mapTaux = new HashMap<Integer, Float>();
80 ilm 48
    private transient final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<SQLRowAccessor, Float>();
65 ilm 49
    private static TaxeCache instance;
80 ilm 50
    private transient SQLRow firstIdTaxe = null;
65 ilm 51
 
52
    private TaxeCache() {
53
        final SQLSelect sel = getSel();
18 ilm 54
 
80 ilm 55
        final List<SQLRow> l = SQLRowListRSH.execute(sel);
56
        for (SQLRow sqlRow : l) {
57
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
58
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
59
            if (sqlRow.getBoolean("DEFAULT")) {
60
                this.firstIdTaxe = sqlRow;
18 ilm 61
            }
80 ilm 62
        }
18 ilm 63
    }
64
 
65
    synchronized public static TaxeCache getCache() {
66
        if (instance == null) {
67
            instance = new TaxeCache();
68
        }
69
        return instance;
70
    }
71
 
72
    public Float getTauxFromId(final int idTaux) {
73
        return this.mapTaux.get(Integer.valueOf(idTaux));
74
    }
75
 
142 ilm 76
    public SQLRowAccessor getRowFromId(final int idTaux) {
77
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
78
        for (SQLRowAccessor r : s) {
79
            if (r.getID() == idTaux) {
80
                return r;
81
            }
82
        }
83
        return null;
84
 
85
    }
86
 
80 ilm 87
    public SQLRow getFirstTaxe() {
18 ilm 88
        if (this.firstIdTaxe == null) {
65 ilm 89
            final SQLSelect sel = getSel();
80 ilm 90
            sel.setWhere(new Where(sel.getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
91
            final List<SQLRow> rows = SQLRowListRSH.execute(sel);
92
            if (rows != null && !rows.isEmpty()) {
93
 
94
                this.firstIdTaxe = rows.get(0);
95
            } else {
96
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
97
                return mapRowTaux.keySet().iterator().next().asRow();
18 ilm 98
            }
80 ilm 99
 
18 ilm 100
        }
101
        return this.firstIdTaxe;
102
    }
19 ilm 103
 
104
    public Integer getIdFromTaux(Float tax) {
105
        Set<Integer> s = (Set<Integer>) mapTaux.keySet();
106
        for (Integer integer : s) {
107
            if (this.mapTaux.get(integer).equals(tax)) {
108
                return integer;
109
            }
110
        }
111
        return null;
112
    }
93 ilm 113
 
114
    public Set<SQLRowAccessor> getAllTaxe() {
115
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
116
    }
117
 
18 ilm 118
}