OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | 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.sales.product.action;

import org.openconcerto.erp.core.common.ui.NumericTextField;
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JDate;
import org.openconcerto.utils.text.SimpleDocumentListener;
import org.openconcerto.utils.Tuple2;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;

public class TransfertStockPanel extends JPanel {

    private static String defaultLabel = "Transfert de stock";

    public TransfertStockPanel(Configuration instance) {
        super(new GridBagLayout());

        final JButton buttonUpdate = new JButton("Mettre à jour");

        final ReferenceArticleSQLElement articleElt = instance.getDirectory().getElement(ReferenceArticleSQLElement.class);

        final SQLRequestComboBox comboArticle = new SQLRequestComboBox();

        final ComboSQLRequest createComboRequest = articleElt.createComboRequest();
        createComboRequest.putWhere("obsolete", new Where(articleElt.getTable().getField("OBSOLETE"), "=", Boolean.FALSE));
        comboArticle.uiInit(createComboRequest);

        final SQLElement stockElt = instance.getDirectory().getElement("DEPOT_STOCK");
        final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
        comboStockDepart.uiInit(stockElt.createComboRequest());

        final SQLRequestComboBox comboStockArrive = new SQLRequestComboBox();
        comboStockArrive.uiInit(stockElt.createComboRequest());

        final NumericTextField fieldReel = new NumericTextField();

        fieldReel.getDocument().addDocumentListener(new SimpleDocumentListener() {

            @Override
            public void update(DocumentEvent e) {
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
            }
        });

        comboArticle.addModelListener("wantedID", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
            }
        });

        comboStockArrive.addModelListener("wantedID", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
            }
        });

        comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
            }
        });

        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridx = 0;
        this.add(new JLabel("Intitulé"), c);
        final JTextField label = new JTextField();
        c.gridx++;
        c.gridwidth = 1;
        c.weightx = 1;
        this.add(label, c);
        label.setText(defaultLabel);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
        final JDate date = new JDate(true);
        c.gridx++;
        this.add(date, c);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Article", SwingConstants.RIGHT), c);
        c.gridx++;
        this.add(comboArticle, c);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Départ", SwingConstants.RIGHT), c);
        c.gridx++;
        this.add(comboStockDepart, c);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;

        this.add(new JLabel("Arrivée", SwingConstants.RIGHT), c);
        c.gridx++;
        this.add(comboStockArrive, c);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Quantité", SwingConstants.RIGHT), c);
        c.gridx++;
        this.add(fieldReel, c);

        c.gridy++;
        c.gridx = 0;
        JButton buttonCancel = new JButton("Annuler");
        JPanel pButton = new JPanel();
        pButton.add(buttonCancel);
        pButton.add(buttonUpdate);
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.SOUTHEAST;
        c.weightx = 0;
        c.fill = GridBagConstraints.NONE;
        this.add(pButton, c);
        buttonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
            }
        });
        buttonUpdate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                buttonUpdate.setEnabled(false);
                BigDecimal qteReel = fieldReel.getValue();

                articleElt.transfert(Arrays.asList(Tuple2.create(comboArticle.getSelectedRow(), qteReel)), comboStockDepart.getSelectedRow(), comboStockArrive.getSelectedRow(), label.getText(),
                        date.getValue());

                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
            }

        });
    }

    private void updateButtons(final JButton buttonUpdate, final SQLRequestComboBox comboArticle, final SQLRequestComboBox comboStockDepart, final SQLRequestComboBox comboStockArrive,
            final NumericTextField fieldReel) {
        buttonUpdate.setEnabled(fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null
                && comboStockDepart.getSelectedRow() != null && comboStockArrive.getSelectedRow().getID() != comboStockDepart.getSelectedRow().getID());
    }

}