OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | 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.accounting.ui;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.ISpinnerIntegerModel;
import org.openconcerto.ui.coreanimation.Animator;
import org.openconcerto.utils.cc.ITransformer;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.ibm.icu.util.Calendar;

public class SaisieJournalPanel extends JPanel {
    final SQLRequestComboBox boxMois = new SQLRequestComboBox();
    final SQLRequestComboBox boxJournal = new SQLRequestComboBox();
    final Calendar cal = Calendar.getInstance();
    final JSpinner spin = new JSpinner(new ISpinnerIntegerModel(2000, cal.get(Calendar.YEAR) + 3, cal.get(Calendar.YEAR)));

    public SaisieJournalPanel(SQLElement ecrElt) {
        super(new GridBagLayout());

        boxMois.uiInit(ecrElt.getDirectory().getElement("MOIS").createComboRequest());

        boxJournal.uiInit(ecrElt.getDirectory().getElement("JOURNAL").createComboRequest());

        animate();

        final IListe listeEcr = new IListe(ecrElt.createTableSource());
        listeEcr.getSource().getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

            @Override
            public SQLSelect transformChecked(SQLSelect input) {
                input.setWhere(Where.FALSE);
                return input;
            }
        });
        GridBagConstraints cListe = new DefaultGridBagConstraints();
        cListe.fill = GridBagConstraints.BOTH;
        cListe.gridwidth = 1;
        cListe.weightx = 1;
        cListe.weighty = 1;

        JPanel panelEcr = new JPanel(new GridBagLayout());
        panelEcr.add(listeEcr, cListe);
        cListe.weighty = 0;
        cListe.weightx = 0;
        cListe.gridy = 4;
        cListe.gridx = 0;
        cListe.fill = GridBagConstraints.NONE;
        cListe.anchor = GridBagConstraints.EAST;

        List<SQLField> lFields = new ArrayList<SQLField>();
        lFields.add(ecrElt.getTable().getField("DEBIT"));
        lFields.add(ecrElt.getTable().getField("CREDIT"));

        IListTotalPanel comp2 = new IListTotalPanel(listeEcr, lFields);
        panelEcr.add(comp2, cListe);

        final PropertyChangeListener l = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                final int wantedID = boxMois.getWantedID();
                final int journalID = boxJournal.getSelectedId();
                final Integer year = (Integer) spin.getValue();
                setFilter(listeEcr, wantedID, journalID, year);
                animate();
            }

        };
        boxMois.addModelListener("wantedID", l);
        boxJournal.addModelListener("wantedID", l);
        spin.getModel().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                l.propertyChange(null);

            }
        });

        JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 6, 4));
        headerPanel.add(new JLabel("Journal"));
        headerPanel.add(boxJournal);
        headerPanel.add(new JLabel("Mois"));
        headerPanel.add(boxMois);
        headerPanel.add(new JLabel("Année"));
        headerPanel.add(spin);

        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridx = 0;
        c.weightx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.BOTH;
        this.add(headerPanel, c);

        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridx = 0;
        c.gridy++;
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
        split.setTopComponent(panelEcr);
        final SQLRowValues defaultRowVals = new SQLRowValues(ecrElt.getTable().getTable("SAISIE_KM_ELEMENT"));
        defaultRowVals.put("DEBIT", 0L);
        defaultRowVals.put("CREDIT", 0L);

        final JCheckBox boxAutoInsert = new JCheckBox("Insertion automatique");

        final SaisieJournalItemTable table = new SaisieJournalItemTable(defaultRowVals, boxAutoInsert);
        table.setPanel(this);

        split.setBottomComponent(table);

        this.add(split, c);

        c.weightx = 0;
        c.weighty = 0;

        JPanel footerPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 6, 4));

        //

        boxAutoInsert.setSelected(true);
        footerPanel.add(boxAutoInsert);

        final JButton buttonAdd = new JButton("Ajouter");
        buttonAdd.setEnabled(false);
        buttonAdd.setVisible(false);
        boxAutoInsert.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAdd.setVisible(!boxAutoInsert.isSelected());
                buttonAdd.setEnabled(!boxAutoInsert.isSelected() && table.isSaisieValid());
            }
        });
        footerPanel.add(buttonAdd);

        final JButton buttonClose = new JButton("Fermer");
        footerPanel.add(buttonClose);

        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridx = 0;
        c.gridy++;
        c.weightx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(footerPanel, c);
        buttonAdd.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                table.createSaisie(defaultRowVals);
            }
        });
        buttonClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ((Window) SwingUtilities.getRoot(buttonClose)).dispose();
            }
        });
        table.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                buttonAdd.setEnabled(!boxAutoInsert.isSelected() && table.isSaisieValid());

            }
        });
    }

    public SQLRequestComboBox getBoxMois() {
        return boxMois;
    }

    public SQLRequestComboBox getBoxJournal() {
        return boxJournal;
    }

    private void animate() {
        Animator.getInstance().animate(boxJournal, boxJournal.isEmpty());
        Animator.getInstance().animate(boxMois, boxMois.isEmpty());
    }

    public int getSelectedYear() {
        return (Integer) this.spin.getValue();
    }

    public int getSelectedMonth() {
        return this.boxMois.getSelectedId() - 2;
    }

    public int getSelectedJournal() {
        return this.boxJournal.getSelectedId();
    }

    public void setFilter(final IListe listeEcr, final int wantedID, final int journalID, final Integer year) {
        if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID && journalID > 1) {
            listeEcr.getSource().getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

                @Override
                public SQLSelect transformChecked(SQLSelect input) {
                    Calendar c = Calendar.getInstance();
                    c.set(Calendar.YEAR, year);
                    c.set(Calendar.MONTH, wantedID - 2);
                    c.set(Calendar.DAY_OF_MONTH, 1);
                    c.set(Calendar.HOUR, 0);
                    c.set(Calendar.MINUTE, 0);
                    Date d1 = c.getTime();
                    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
                    c.set(Calendar.HOUR, 23);
                    c.set(Calendar.MINUTE, 59);
                    Date d2 = c.getTime();
                    input.setWhere(new Where(input.getTable("ECRITURE").getField("ID_JOURNAL"), "=", journalID).and(new Where(input.getTable("ECRITURE").getField("DATE"), d1, d2)));
                    return input;
                }
            });
        } else {
            listeEcr.getSource().getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

                @Override
                public SQLSelect transformChecked(SQLSelect input) {
                    input.setWhere(Where.FALSE);
                    return input;
                }
            });
        }
    }
}