OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Rev 177 | 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;
156 ilm 21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
18 ilm 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
 
156 ilm 27
import java.util.Arrays;
93 ilm 28
import java.util.Collections;
18 ilm 29
import java.util.HashMap;
80 ilm 30
import java.util.LinkedHashMap;
18 ilm 31
import java.util.List;
32
import java.util.Map;
19 ilm 33
import java.util.Set;
18 ilm 34
 
35
public final class TaxeCache {
36
 
156 ilm 37
    private static final SQLRowValuesListFetcher getSel() {
18 ilm 38
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
39
        final SQLTable table = root.getTable("TAXE");
156 ilm 40
        SQLRowValues rowVals = new SQLRowValues(table);
41
        rowVals.putNulls("TAUX", "CODE", "NOM", "ID_TAXE", "DEFAULT");
42
        List<String> compteFields = Arrays.asList("ID_COMPTE_PCE_COLLECTE", "ID_COMPTE_PCE_DED", "ID_COMPTE_PCE", "ID_COMPTE_PCE_VENTE", "ID_COMPTE_PCE_VENTE_SERVICE", "ID_COMPTE_PCE_COLLECTE_INTRA",
43
                "ID_COMPTE_PCE_DED_INTRA");
44
        for (String foreignFieldName : compteFields) {
45
            rowVals.putRowValues(foreignFieldName).putNulls("NUMERO", "NOM");
46
        }
47
        return SQLRowValuesListFetcher.create(rowVals);
65 ilm 48
    }
49
 
156 ilm 50
    private final Map<Integer, Float> mapTaux = new HashMap<>();
51
    private final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<>();
65 ilm 52
    private static TaxeCache instance;
156 ilm 53
    private SQLRow firstIdTaxe = null;
65 ilm 54
 
55
    private TaxeCache() {
156 ilm 56
        loadCache();
57
    }
18 ilm 58
 
156 ilm 59
    private void loadCache() {
60
 
61
        this.mapRowTaux.clear();
62
        this.mapTaux.clear();
63
        this.firstIdTaxe = null;
64
 
65
        final SQLRowValuesListFetcher sel = getSel();
66
 
67
        final List<SQLRowValues> l = sel.fetch();
68
        for (SQLRowValues sqlRow : l) {
80 ilm 69
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
70
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
71
            if (sqlRow.getBoolean("DEFAULT")) {
156 ilm 72
                this.firstIdTaxe = sqlRow.asRow();
18 ilm 73
            }
80 ilm 74
        }
18 ilm 75
    }
76
 
156 ilm 77
    public static synchronized TaxeCache getCache() {
18 ilm 78
        if (instance == null) {
79
            instance = new TaxeCache();
80
        }
81
        return instance;
82
    }
83
 
156 ilm 84
    public synchronized Float getTauxFromId(final int idTaux) {
85
        Float f = this.mapTaux.get(Integer.valueOf(idTaux));
86
        if (f == null) {
87
            loadCache();
88
            f = this.mapTaux.get(Integer.valueOf(idTaux));
89
        }
90
        return f;
18 ilm 91
    }
92
 
156 ilm 93
    public synchronized SQLRowAccessor getRowFromId(final int idTaux) {
142 ilm 94
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
95
        for (SQLRowAccessor r : s) {
96
            if (r.getID() == idTaux) {
97
                return r;
98
            }
99
        }
156 ilm 100
        loadCache();
101
        for (SQLRowAccessor r : s) {
102
            if (r.getID() == idTaux) {
103
                return r;
104
            }
105
        }
142 ilm 106
        return null;
107
 
108
    }
109
 
156 ilm 110
    public synchronized SQLRow getFirstTaxe() {
18 ilm 111
        if (this.firstIdTaxe == null) {
156 ilm 112
            final SQLRowValuesListFetcher sel = getSel();
113
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
80 ilm 114
            if (rows != null && !rows.isEmpty()) {
115
 
156 ilm 116
                this.firstIdTaxe = rows.get(0).asRow();
80 ilm 117
            } else {
118
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
119
                return mapRowTaux.keySet().iterator().next().asRow();
18 ilm 120
            }
80 ilm 121
 
18 ilm 122
        }
123
        return this.firstIdTaxe;
124
    }
19 ilm 125
 
156 ilm 126
    public synchronized Integer getIdFromTaux(Float tax) {
127
        Set<Integer> s = mapTaux.keySet();
19 ilm 128
        for (Integer integer : s) {
129
            if (this.mapTaux.get(integer).equals(tax)) {
130
                return integer;
131
            }
132
        }
156 ilm 133
        loadCache();
134
        for (Integer integer : s) {
135
            if (this.mapTaux.get(integer).equals(tax)) {
136
                return integer;
137
            }
138
        }
19 ilm 139
        return null;
140
    }
93 ilm 141
 
156 ilm 142
    public synchronized Set<SQLRowAccessor> getAllTaxe() {
93 ilm 143
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
144
    }
145
 
18 ilm 146
}