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-2019 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.common.element;

import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.sql.users.rights.UserRights;
import org.openconcerto.sql.users.rights.UserRightsManager;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.task.config.ComptaBasePropsConfiguration;

import java.util.List;

/**
 * SQLElement de la base société
 * 
 * @author Administrateur
 * 
 */
public abstract class ComptaSQLConfElement extends SocieteSQLConfElement {

    private static DBRoot baseSociete;

    // Attention limité la modification de table commercial à l'admin pour que le user ne puisse pas
    // réaffecter les liens commercial<->user
    public static String NON_RESTREINT_PAR_COMMERCIAL_RIGHT = "NON_RESTREINT_PAR_COMMERCIAL";

    private static DBRoot getBaseSociete() {
        if (baseSociete == null)
            baseSociete = ((ComptaBasePropsConfiguration) Configuration.getInstance()).getRootSociete();
        return baseSociete;
    }

    protected static SQLTable findTable(String tableName) {
        return getBaseSociete().findTable(tableName, true);
    }

    public ComptaSQLConfElement(String tableName, String singular, String plural) {
        this(findTable(tableName), singular, plural);
    }

    public ComptaSQLConfElement(SQLTable table, String singular, String plural) {
        super(table, singular, plural);
    }

    public ComptaSQLConfElement(String tableName) {
        this(tableName, null);
    }

    public ComptaSQLConfElement(String tableName, String code) {
        this(findTable(tableName), code);
    }

    public ComptaSQLConfElement(SQLTable table) {
        this(table, null);
    }

    public ComptaSQLConfElement(SQLTable table, String code) {
        super(table, code);
    }

    public void addCommercialFilter(final SQLTableModelSource source, final SQLField fieldCommercial) {
        final UserRights rights = UserRightsManager.getCurrentUserRights();
        if (!rights.haveRight(NON_RESTREINT_PAR_COMMERCIAL_RIGHT)) {
            source.getReq().putWhere(NON_RESTREINT_PAR_COMMERCIAL_RIGHT, getWhereCommercial(fieldCommercial));
        }
    }

    private Where getWhereCommercial(final SQLField fieldCommercial) {
        SQLRow row = fieldCommercial.getTable().getDBRoot().findTable("USER_COMMON").getRow(UserManager.getInstance().getCurrentUser().getId());
        List<SQLRow> rows = row.getReferentRows(fieldCommercial.getForeignTable().getField("ID_USER_COMMON"));

        Where w = null;

        if (rows.isEmpty()) {
            w = Where.FALSE;
        } else {
            for (SQLRow sqlRow : rows) {
                if (fieldCommercial.getTable().getName().equals(getTable().getName())) {
                    w = Where.or(w, new Where(fieldCommercial, "=", sqlRow.getID()));
                } else {
                    // CLIENT.ID_COMMERCIAL in CONTACT
                    SQLField field = getTable().getField("ID_" + fieldCommercial.getTable().getName());
                    SQLSelect selIDClient = new SQLSelect();
                    selIDClient.addSelect(fieldCommercial.getTable().getKey());
                    selIDClient.setWhere(new Where(fieldCommercial, "=", sqlRow.getID()));

                    Where wClient = Where.createRaw(field.getSQLName().quote() + " IN (" + selIDClient.asString() + ")", field);
                    w = Where.or(w, wClient);
                }
            }
        }
        return w;
    }

    public void addCommercialFilterCombo(final ComboSQLRequest req, final SQLField fieldCommercial) {
        final UserRights rights = UserRightsManager.getCurrentUserRights();
        if (!rights.haveRight(NON_RESTREINT_PAR_COMMERCIAL_RIGHT)) {
            req.putWhere(NON_RESTREINT_PAR_COMMERCIAL_RIGHT, getWhereCommercial(fieldCommercial));
        }
    }
}