OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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);
177 ilm 41
        rowVals.putNulls("TAUX", "CODE", "NOM", "ID_TAXE", "DEFAULT", "DEFAULT_ACHAT");
156 ilm 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",
180 ilm 43
                "ID_COMPTE_PCE_DED_INTRA", "ID_COMPTE_PCE_COLLECTE_ENCAISSEMENT");
156 ilm 44
        for (String foreignFieldName : compteFields) {
180 ilm 45
            if (table.contains(foreignFieldName)) {
46
                rowVals.putRowValues(foreignFieldName).putNulls("NUMERO", "NOM");
47
            }
156 ilm 48
        }
49
        return SQLRowValuesListFetcher.create(rowVals);
65 ilm 50
    }
51
 
156 ilm 52
    private final Map<Integer, Float> mapTaux = new HashMap<>();
53
    private final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<>();
65 ilm 54
    private static TaxeCache instance;
156 ilm 55
    private SQLRow firstIdTaxe = null;
177 ilm 56
    private SQLRow firstIdTaxeAchat = null;
65 ilm 57
 
58
    private TaxeCache() {
156 ilm 59
        loadCache();
60
    }
18 ilm 61
 
156 ilm 62
    private void loadCache() {
63
 
64
        this.mapRowTaux.clear();
65
        this.mapTaux.clear();
66
        this.firstIdTaxe = null;
177 ilm 67
        this.firstIdTaxeAchat = null;
156 ilm 68
 
69
        final SQLRowValuesListFetcher sel = getSel();
70
 
71
        final List<SQLRowValues> l = sel.fetch();
72
        for (SQLRowValues sqlRow : l) {
80 ilm 73
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
74
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
75
            if (sqlRow.getBoolean("DEFAULT")) {
156 ilm 76
                this.firstIdTaxe = sqlRow.asRow();
18 ilm 77
            }
177 ilm 78
            if (sqlRow.getBoolean("DEFAULT_ACHAT")) {
79
                this.firstIdTaxeAchat = sqlRow.asRow();
80
            }
80 ilm 81
        }
18 ilm 82
    }
83
 
156 ilm 84
    public static synchronized TaxeCache getCache() {
18 ilm 85
        if (instance == null) {
86
            instance = new TaxeCache();
87
        }
88
        return instance;
89
    }
90
 
156 ilm 91
    public synchronized Float getTauxFromId(final int idTaux) {
92
        Float f = this.mapTaux.get(Integer.valueOf(idTaux));
93
        if (f == null) {
94
            loadCache();
95
            f = this.mapTaux.get(Integer.valueOf(idTaux));
96
        }
97
        return f;
18 ilm 98
    }
99
 
156 ilm 100
    public synchronized SQLRowAccessor getRowFromId(final int idTaux) {
142 ilm 101
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
102
        for (SQLRowAccessor r : s) {
103
            if (r.getID() == idTaux) {
104
                return r;
105
            }
106
        }
156 ilm 107
        loadCache();
108
        for (SQLRowAccessor r : s) {
109
            if (r.getID() == idTaux) {
110
                return r;
111
            }
112
        }
142 ilm 113
        return null;
114
 
115
    }
116
 
156 ilm 117
    public synchronized SQLRow getFirstTaxe() {
18 ilm 118
        if (this.firstIdTaxe == null) {
156 ilm 119
            final SQLRowValuesListFetcher sel = getSel();
120
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
80 ilm 121
            if (rows != null && !rows.isEmpty()) {
122
 
156 ilm 123
                this.firstIdTaxe = rows.get(0).asRow();
80 ilm 124
            } else {
125
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
126
                return mapRowTaux.keySet().iterator().next().asRow();
18 ilm 127
            }
80 ilm 128
 
18 ilm 129
        }
130
        return this.firstIdTaxe;
131
    }
19 ilm 132
 
177 ilm 133
    public synchronized SQLRow getFirstTaxeAchat() {
134
        if (this.firstIdTaxeAchat == null) {
135
            final SQLRowValuesListFetcher sel = getSel();
136
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT_ACHAT"), "=", Boolean.TRUE));
137
            if (rows != null && !rows.isEmpty()) {
138
                this.firstIdTaxeAchat = rows.get(0).asRow();
139
            } else {
140
                this.firstIdTaxeAchat = getFirstTaxe();
141
            }
142
 
143
        }
144
        return this.firstIdTaxeAchat;
145
    }
146
 
156 ilm 147
    public synchronized Integer getIdFromTaux(Float tax) {
148
        Set<Integer> s = mapTaux.keySet();
19 ilm 149
        for (Integer integer : s) {
150
            if (this.mapTaux.get(integer).equals(tax)) {
151
                return integer;
152
            }
153
        }
156 ilm 154
        loadCache();
155
        for (Integer integer : s) {
156
            if (this.mapTaux.get(integer).equals(tax)) {
157
                return integer;
158
            }
159
        }
19 ilm 160
        return null;
161
    }
93 ilm 162
 
156 ilm 163
    public synchronized Set<SQLRowAccessor> getAllTaxe() {
93 ilm 164
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
165
    }
166
 
18 ilm 167
}