OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.finance.tax.model;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.utils.ExceptionHandler;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class TaxeCache {

    private static final SQLRowValuesListFetcher getSel() {
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
        final SQLTable table = root.getTable("TAXE");
        SQLRowValues rowVals = new SQLRowValues(table);
        rowVals.putNulls("TAUX", "CODE", "NOM", "ID_TAXE", "DEFAULT", "DEFAULT_ACHAT");
        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",
                "ID_COMPTE_PCE_DED_INTRA");
        for (String foreignFieldName : compteFields) {
            rowVals.putRowValues(foreignFieldName).putNulls("NUMERO", "NOM");
        }
        return SQLRowValuesListFetcher.create(rowVals);
    }

    private final Map<Integer, Float> mapTaux = new HashMap<>();
    private final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<>();
    private static TaxeCache instance;
    private SQLRow firstIdTaxe = null;
    private SQLRow firstIdTaxeAchat = null;

    private TaxeCache() {
        loadCache();
    }

    private void loadCache() {

        this.mapRowTaux.clear();
        this.mapTaux.clear();
        this.firstIdTaxe = null;
        this.firstIdTaxeAchat = null;

        final SQLRowValuesListFetcher sel = getSel();

        final List<SQLRowValues> l = sel.fetch();
        for (SQLRowValues sqlRow : l) {
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
            if (sqlRow.getBoolean("DEFAULT")) {
                this.firstIdTaxe = sqlRow.asRow();
            }
            if (sqlRow.getBoolean("DEFAULT_ACHAT")) {
                this.firstIdTaxeAchat = sqlRow.asRow();
            }
        }
    }

    public static synchronized TaxeCache getCache() {
        if (instance == null) {
            instance = new TaxeCache();
        }
        return instance;
    }

    public synchronized Float getTauxFromId(final int idTaux) {
        Float f = this.mapTaux.get(Integer.valueOf(idTaux));
        if (f == null) {
            loadCache();
            f = this.mapTaux.get(Integer.valueOf(idTaux));
        }
        return f;
    }

    public synchronized SQLRowAccessor getRowFromId(final int idTaux) {
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
        for (SQLRowAccessor r : s) {
            if (r.getID() == idTaux) {
                return r;
            }
        }
        loadCache();
        for (SQLRowAccessor r : s) {
            if (r.getID() == idTaux) {
                return r;
            }
        }
        return null;

    }

    public synchronized SQLRow getFirstTaxe() {
        if (this.firstIdTaxe == null) {
            final SQLRowValuesListFetcher sel = getSel();
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
            if (rows != null && !rows.isEmpty()) {

                this.firstIdTaxe = rows.get(0).asRow();
            } else {
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
                return mapRowTaux.keySet().iterator().next().asRow();
            }

        }
        return this.firstIdTaxe;
    }

    public synchronized SQLRow getFirstTaxeAchat() {
        if (this.firstIdTaxeAchat == null) {
            final SQLRowValuesListFetcher sel = getSel();
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT_ACHAT"), "=", Boolean.TRUE));
            if (rows != null && !rows.isEmpty()) {
                this.firstIdTaxeAchat = rows.get(0).asRow();
            } else {
                this.firstIdTaxeAchat = getFirstTaxe();
            }

        }
        return this.firstIdTaxeAchat;
    }

    public synchronized Integer getIdFromTaux(Float tax) {
        Set<Integer> s = mapTaux.keySet();
        for (Integer integer : s) {
            if (this.mapTaux.get(integer).equals(tax)) {
                return integer;
            }
        }
        loadCache();
        for (Integer integer : s) {
            if (this.mapTaux.get(integer).equals(tax)) {
                return integer;
            }
        }
        return null;
    }

    public synchronized Set<SQLRowAccessor> getAllTaxe() {
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
    }

}