OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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