OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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.supplychain.supplier.component;

import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.sqlobject.SQLTextCombo;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JDate;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class MouvementStockSQLComponent extends BaseSQLComponent {

    public MouvementStockSQLComponent(SQLElement element) {
        super(element);
    }

    @Override
    public Set<String> getPartialResetNames() {
        Set<String> s = new HashSet<>(2);
        s.add("QTE");
        s.add("ID_ARTICLE");
        return s;
    }

    public void addViews() {
        this.setLayout(new GridBagLayout());
        final GridBagConstraints c = new DefaultGridBagConstraints();

        // Libellé
        final JLabel labelLib = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);
        this.add(labelLib, c);

        c.gridx++;
        c.weightx = 1;
        final SQLTextCombo textLib = new SQLTextCombo();
        this.add(textLib, c);

        // Date
        c.gridx++;
        c.weightx = 0;
        JLabel labelDate = new JLabel(getLabelFor("DATE"), SwingConstants.RIGHT);
        this.add(labelDate, c);

        c.gridx++;
        final JDate date = new JDate(true);
        this.add(date, c);

        // Article
        final ElementComboBox articleSelect = new ElementComboBox();

        c.gridx = 0;
        c.gridy++;
        final JLabel labelArticle = new JLabel(getLabelFor("ID_ARTICLE"), SwingConstants.RIGHT);
        this.add(labelArticle, c);

        c.gridx++;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1;
        this.add(articleSelect, c);

        // Article
        final SQLRequestComboBox articleStock = new SQLRequestComboBox();
        c.gridwidth = 1;
        c.weightx = 0;
        c.gridx = 0;
        c.gridy++;
        final JLabel labelStock = new JLabel(getLabelFor("ID_STOCK"), SwingConstants.RIGHT);
        this.add(labelStock, c);

        c.gridx++;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1;
        this.add(articleStock, c);

        // QTE
        c.gridwidth = 1;
        c.weightx = 0;
        c.gridy++;
        c.gridx = 0;
        c.anchor = GridBagConstraints.EAST;
        final JLabel labelQte = new JLabel(getLabelFor("QTE"), SwingConstants.RIGHT);
        this.add(labelQte, c);

        c.gridx++;
        c.fill = GridBagConstraints.NONE;
        final JTextField textQte = new JTextField(6);
        c.weighty = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.add(textQte, c);

        c.gridx++;
        final JCheckBox boxReel = new JCheckBox(getLabelFor("REEL"));
        this.add(boxReel, c);
        addView(boxReel, "REEL");

        c.gridy++;
        c.weighty = 1;
        final JPanel comp = new JPanel();
        comp.setOpaque(false);
        this.add(comp, c);
        DefaultGridBagConstraints.lockMinimumSize(textQte);
        DefaultGridBagConstraints.lockMaximumSize(textQte);
        this.addRequiredSQLObject(textQte, "QTE");
        this.addSQLObject(textLib, "NOM");
        this.addRequiredSQLObject(articleSelect, "ID_ARTICLE");
        this.addRequiredSQLObject(articleStock, "ID_STOCK");
        this.addRequiredSQLObject(date, "DATE");

        articleStock.getRequest().setWhere(Where.FALSE);
        articleSelect.addModelListener("wantedID", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                articleStock.getRequest().setWhere(new Where(articleStock.getRequest().getPrimaryTable().getField("ID_ARTICLE"), "=", articleSelect.getWantedID()));
            }
        });

    }

    @Override
    protected SQLRowValues createDefaults() {
        SQLRowValues rowVals = new SQLRowValues(getTable());
        rowVals.put("REEL", Boolean.TRUE);
        return rowVals;
    }

    @Override
    public int insert(SQLRow order) {
        int id = super.insert(order);
        try {
            ((MouvementStockSQLElement) getElement()).updateStock(Arrays.asList(getTable().getRow(id)), false);
        } catch (SQLException e) {
            throw new IllegalStateException("Impossible de metter à jour les stocks pour l'id " + id, e);
        }
        return id;
    }

    @Override
    public void update() {
        int id = getSelectedID();
        try {
            ((MouvementStockSQLElement) getElement()).updateStock(Arrays.asList(getTable().getRow(id)), true);
            super.update();
            ((MouvementStockSQLElement) getElement()).updateStock(Arrays.asList(getTable().getRow(id)), false);
        } catch (SQLException e) {
            throw new IllegalStateException("Impossible de metter à jour les stocks pour l'id " + id, e);
        }
    }

}