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.supplychain.order.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.common.ui.DeviseField;
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.UISQLComponent;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.preferences.SQLPreferences;
import org.openconcerto.sql.request.UpdateBuilder;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel.EditMode;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.ui.FrameUtil;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JTextField;

public class CommandeElementSQLElement extends ComptaSQLConfElement {

    public CommandeElementSQLElement() {
        super("COMMANDE_ELEMENT", "un element de commande", "éléments de commande");

        PredicateRowAction rowActionCmd = new PredicateRowAction(new AbstractAction("Modifier la commande associée") {

            @Override
            public void actionPerformed(ActionEvent e) {
                SQLRowValues selectedRow = IListe.get(e).getSelectedRow();
                EditFrame f = new EditFrame(getForeignElement("ID_COMMANDE"), EditMode.MODIFICATION);
                f.getSQLComponent().select(selectedRow.getForeignID("ID_COMMANDE"));
                FrameUtil.showPacked(f);
            }

        }, true);
        rowActionCmd.setPredicate(IListeEvent.getSingleSelectionPredicate());
        getRowActions().add(rowActionCmd);

        if (getTable().getForeignTable("ID_USER_COMMON_CREATE").getRow(UserManager.getUserID()).getBoolean("ADMIN")) {
            PredicateRowAction rowActionA = new PredicateRowAction(new AbstractAction("Forcer la réception") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    updateForceLivrer(e, Boolean.TRUE);
                }

            }, true);
            rowActionA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
            getRowActions().add(rowActionA);
            PredicateRowAction rowActionC = new PredicateRowAction(new AbstractAction("Annuler forcer la réception") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    updateForceLivrer(e, Boolean.FALSE);
                }

            }, true);
            rowActionC.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
            getRowActions().add(rowActionC);
        }
    }

    private void updateForceLivrer(ActionEvent e, Boolean state) {
        final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
        final Set<Integer> ids = new HashSet<Integer>();
        for (SQLRowValues sqlRowValues : selectedRows) {
            ids.add(sqlRowValues.getID());
        }
        UpdateBuilder build = new UpdateBuilder(getTable());
        build.setObject("RECU_FORCED", state);
        build.setWhere(new Where(getTable().getKey(), ids));
        getTable().getDBSystemRoot().getDataSource().execute(build.asString());
        IListe.get(e).getModel().updateAll();
    }

    @Override
    protected String getParentFFName() {
        return "ID_COMMANDE";
    }

    protected List<String> getListFields() {
        final List<String> l = new ArrayList<String>();
        l.add("ID_STYLE");
        l.add("CODE");
        l.add("NOM");
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false)) {

            for (String fieldName : getTable().getFieldsName()) {
                if (fieldName.startsWith("ID_ARTICLE_DECLINAISON_")) {
                    l.add(fieldName);
                }
            }
        }
        l.add("ID_COMMANDE");
        l.add("ID_ARTICLE");
        l.add("PA_HT");
        l.add("PV_HT");
        l.add("T_PA_HT");
        l.add("T_PV_HT");
        l.add("ID_TAXE");
        l.add("QTE");
        l.add("QTE_UNITAIRE");
        l.add("QTE_RECUE");
        l.add("POIDS");
        l.add("RECU_FORCED");
        l.add("RECU");
        return l;
    }

    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<String>();
        l.add("CODE");
        l.add("NOM");
        l.add("PA_HT");
        l.add("PV_HT");
        return l;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.SQLElement#getComponent()
     */
    public SQLComponent createComponent() {
        return new UISQLComponent(this) {
            public void addViews() {
                this.addRequiredSQLObject(new JTextField(), "NOM", "left");
                this.addRequiredSQLObject(new JTextField(), "CODE", "right");

                this.addSQLObject(new ElementComboBox(), "ID_STYLE", "left");

                this.addRequiredSQLObject(new DeviseField(), "PA_HT", "left");
                this.addSQLObject(new DeviseField(), "PV_HT", "right");

                this.addSQLObject(new JTextField(), "POIDS", "left");
                this.addSQLObject(new ElementComboBox(), "ID_TAXE", "right");
            }
        };
    }

    @Override
    protected String createCode() {
        return createCodeOfPackage() + ".item";
    }
}