OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | 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-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.finance.accounting.ui;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.config.Gestion;
import org.openconcerto.erp.core.finance.accounting.report.GrandLivrePDF;
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheetXML;
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorListener;
import org.openconcerto.erp.preferences.DefaultNXProps;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JDate;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.FileUtils;

import java.awt.FlowLayout;
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.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ImpressionGrandLivrePanel extends JPanel implements SpreadSheetGeneratorListener {

    private final JDate dateDeb;
    private final JDate dateEnd;
    private JButton validPDF;
    private JButton validODS;
    private JButton annul;
    private JCheckBox checkImpr;
    private JCheckBox checkVisu;
    private JTextField compteDeb, compteEnd;
    private int mode = GrandLivrePDF.MODEALL;
    private JProgressBar bar = new JProgressBar(0, 3);

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

        final ComptaPropsConfiguration comptaPropsConfiguration = (ComptaPropsConfiguration) Configuration.getInstance();
        SQLRow rowSociete = comptaPropsConfiguration.getRowSociete();
        SQLRow rowExercice = comptaPropsConfiguration.getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));

        this.dateDeb = new JDate();
        this.dateEnd = new JDate();

        // Période
        c.weightx = 0;
        this.add(new JLabel("Période du", SwingConstants.RIGHT), c);
        c.gridx++;
        c.weightx = 0;
        this.add(this.dateDeb, c);
        // Chargement des valeurs par défaut
        String valueDateDeb = DefaultNXProps.getInstance().getStringProperty("GrandLivreDateDeb");
        if (valueDateDeb.trim().length() > 0) {
            Long l = new Long(valueDateDeb);
            this.dateDeb.setValue(new Date(l.longValue()));
        } else {
            this.dateDeb.setValue((Date) rowExercice.getObject("DATE_DEB"));
        }

        c.gridx++;
        c.weightx = 0;
        this.add(new JLabel("au"), c);
        c.gridx++;

        c.fill = GridBagConstraints.NONE;
        this.add(this.dateEnd, c);
        // Chargement des valeurs par défaut
        String valueDateEnd = DefaultNXProps.getInstance().getStringProperty("GrandLivreDateEnd");
        if (valueDateEnd.trim().length() > 0) {
            Long l = new Long(valueDateEnd);
            this.dateEnd.setValue(new Date(l.longValue()));
        } else {
            this.dateEnd.setValue((Date) rowExercice.getObject("DATE_FIN"));
        }

        // Compte
        this.compteDeb = new JTextField(8);
        this.compteEnd = new JTextField(8);
        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(new JLabel("Du compte", SwingConstants.RIGHT), c);
        c.gridx++;
        c.gridwidth = 3;
        c.fill = GridBagConstraints.NONE;
        JPanel pCompte = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        pCompte.add(this.compteDeb);
        pCompte.add(new JLabel(" au compte "));
        pCompte.add(this.compteEnd);

        this.add(pCompte, c);

        c.gridx = 0;
        c.gridwidth = 4;
        c.gridy++;
        this.add(new JLabelBold("Options"), c);
        final JCheckBox boxCumulsAnts = new JCheckBox("Cumuls antérieurs");
        c.gridx = 0;
        c.gridy++;
        c.gridwidth = 2;
        this.add(boxCumulsAnts, c);
        boxCumulsAnts.setSelected(true);

        final JCheckBox boxCentralClient = new JCheckBox("Centralisation comptes clients");
        c.gridx += 2;
        c.gridwidth = 2;
        this.add(boxCentralClient, c);

        final JCheckBox boxCompteSolde = new JCheckBox("Inclure les comptes soldés");
        c.gridx = 0;
        c.gridy++;
        c.gridwidth = 2;
        this.add(boxCompteSolde, c);
        boxCompteSolde.setSelected(true);

        final JCheckBox boxCentralFourn = new JCheckBox("Centralisation comptes fournisseurs");
        c.gridx += 2;
        c.gridwidth = 2;
        this.add(boxCentralFourn, c);

        // Journal à exclure
        c.gridy++;
        c.gridx = 0;
        c.gridwidth = 1;
        this.add(new JLabel("Exclure le journal", SwingConstants.RIGHT), c);
        c.gridx++;
        c.gridwidth = 3;
        final ElementComboBox comboJrnl = new ElementComboBox(true);
        comboJrnl.init(Configuration.getInstance().getDirectory().getElement("JOURNAL"));
        this.add(comboJrnl, c);

        // Radio mode
        JRadioButton radioAll = new JRadioButton(new AbstractAction("Toutes") {
            public void actionPerformed(ActionEvent e) {
                mode = GrandLivrePDF.MODEALL;
            }
        });

        JRadioButton radioLettree = new JRadioButton(new AbstractAction("Lettrées") {
            public void actionPerformed(ActionEvent e) {
                mode = GrandLivrePDF.MODELETTREE;
            }
        });

        JRadioButton radioNonLettree = new JRadioButton(new AbstractAction("Non lettrées") {
            public void actionPerformed(ActionEvent e) {
                mode = GrandLivrePDF.MODENONLETTREE_ALL;
            }
        });

        JRadioButton radioNonLettreePeridod = new JRadioButton(new AbstractAction("Non lettrées sur la période") {
            public void actionPerformed(ActionEvent e) {
                mode = GrandLivrePDF.MODENONLETTREE_PERIODE;
            }
        });

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        c.gridwidth = GridBagConstraints.REMAINDER;

        this.add(new JLabelBold("Ecritures à inclure"), c);
        JPanel panelMode = new JPanel();
        panelMode.add(radioAll);
        panelMode.add(radioLettree);
        panelMode.add(radioNonLettree);
        panelMode.add(radioNonLettreePeridod);

        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weighty = 0;
        c.fill = GridBagConstraints.NONE;
        ButtonGroup group = new ButtonGroup();
        group.add(radioAll);
        group.add(radioLettree);
        group.add(radioNonLettree);
        group.add(radioNonLettreePeridod);
        radioAll.setSelected(true);

        this.add(panelMode, c);

        // Progress bar
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridy++;
        c.gridx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;

        this.add(new JLabelBold("Progression de la creation du grand livre"), c);
        c.gridy++;
        this.bar.setStringPainted(true);
        this.add(this.bar, c);

        this.validPDF = new JButton("PDF");
        this.validODS = new JButton("Tableur");

        this.annul = new JButton("Fermer");

        // OK, Cancel

        c.gridx = 0;
        c.gridy++;
        c.gridwidth = 4;
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.SOUTHEAST;

        JPanel panelOkCancel = new JPanel();
        panelOkCancel.add(this.validODS);
        panelOkCancel.add(this.validPDF);

        panelOkCancel.add(this.annul);
        this.add(panelOkCancel, c);
        this.checkValidity();
        this.validPDF.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
                chooser.setSelectedFile(new File("Grand livre " + df.format(new Date()) + ".pdf"));
                int r = chooser.showSaveDialog(ImpressionGrandLivrePanel.this);
                if (r == JFileChooser.APPROVE_OPTION) {
                    validPDF.setEnabled(false);
                    validODS.setEnabled(false);
                    bar.setString(null);
                    bar.setValue(0);
                    // un fichier a été choisi (sortie par OK)
                    // nom du fichier choisi
                    final File file = chooser.getSelectedFile();
                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                final GrandLivrePDF bSheet = new GrandLivrePDF(comptaPropsConfiguration, dateDeb.getDate(), dateEnd.getDate(), compteDeb.getText().trim(), compteEnd.getText().trim(),
                                        mode, boxCumulsAnts.isSelected(), !boxCompteSolde.isSelected(), boxCentralClient.isSelected(), boxCentralFourn.isSelected(), comboJrnl.getSelectedId());
                                bar.setValue(1);
                                bSheet.getGeneratedPDFFile(file);
                                bar.setValue(2);
                                SwingUtilities.invokeLater(new Runnable() {

                                    @Override
                                    public void run() {
                                        Gestion.openPDF(file);
                                    }
                                });

                            } catch (Exception exn) {
                                ExceptionHandler.handle("Erreur de génération du grand livre", exn);
                                exn.printStackTrace();
                            } finally {
                                taskEnd();
                            }
                        }
                    }).start();
                }

            }
        });
        this.validODS.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                validPDF.setEnabled(false);
                validODS.setEnabled(false);
                bar.setString(null);
                bar.setValue(0);
                // un fichier a été choisi (sortie par OK)
                // nom du fichier choisi
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            final GrandLivreSheetXML bSheet = new GrandLivreSheetXML(dateDeb.getDate(), dateEnd.getDate(), compteDeb.getText().trim(), compteEnd.getText().trim(), mode,
                                    boxCumulsAnts.isSelected(), !boxCompteSolde.isSelected(), boxCentralClient.isSelected(), boxCentralFourn.isSelected(), comboJrnl.getSelectedId());
                            bar.setValue(1);
                            final File file = bSheet.getOrCreateDocumentFile();
                            bar.setValue(2);
                            SwingUtilities.invokeLater(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        FileUtils.openFile(file);
                                    } catch (IOException e) {
                                        ExceptionHandler.handle("Erreur lors de l'ouverture du grand livre.", e);
                                    }
                                }
                            });

                        } catch (Exception exn) {
                            ExceptionHandler.handle("Erreur de génération du grand livre", exn);
                            exn.printStackTrace();
                        } finally {
                            taskEnd();
                        }
                    }
                }).start();
            }

        });
        this.annul.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ((JFrame) SwingUtilities.getRoot(ImpressionGrandLivrePanel.this)).dispose();
            }
        });

        this.dateDeb.addValueListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                checkValidity();
                storeValue();
            }
        });
        this.dateEnd.addValueListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                checkValidity();
                storeValue();
            }
        });

        DocumentListener d = new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void removeUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void changedUpdate(DocumentEvent e) {
                checkValidity();
            }
        };

        this.compteDeb.getDocument().addDocumentListener(d);
        // Chargement des valeurs par défaut
        String valueCompteDeb = DefaultNXProps.getInstance().getStringProperty("GrandLivreCompteDeb");
        if (valueCompteDeb.trim().length() > 0) {
            this.compteDeb.setText(valueCompteDeb);
        } else {
            this.compteDeb.setText("1");
        }

        this.compteEnd.getDocument().addDocumentListener(d);
        // Chargement des valeurs par défaut
        String valueCompteEnd = DefaultNXProps.getInstance().getStringProperty("GrandLivreCompteEnd");
        if (valueCompteEnd.trim().length() > 0) {
            this.compteEnd.setText(valueCompteEnd);
        } else {
            this.compteEnd.setText("8");
        }
    }

    private void checkValidity() {

        Date beginDate = this.dateDeb.getDate();
        Date endDate = this.dateEnd.getDate();

        this.validODS.setEnabled(true);
        this.validPDF.setEnabled(true);
        if (beginDate == null || endDate == null) {
            this.validODS.setEnabled(false);
            this.validPDF.setEnabled(false);
        } else {
            if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
                this.validODS.setEnabled(false);
                this.validPDF.setEnabled(false);
            } else {
                if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
                    this.validODS.setEnabled(false);
                    this.validPDF.setEnabled(false);
                } else {
                    if (beginDate.after(endDate)) {
                        this.validODS.setEnabled(false);
                        this.validPDF.setEnabled(false);
                    }
                }
            }
        }
    }

    private void storeValue() {

        // Set date debut
        Date d = this.dateDeb.getDate();
        if (d != null) {
            DefaultNXProps.getInstance().setProperty("GrandLivreDateDeb", String.valueOf(d.getTime()));
        }

        // Set date Fin
        Date dFin = this.dateEnd.getDate();
        if (dFin != null) {
            DefaultNXProps.getInstance().setProperty("GrandLivreDateEnd", String.valueOf(dFin.getTime()));
        }

        DefaultNXProps.getInstance().setProperty("GrandLivreCompteDeb", this.compteDeb.getText());
        DefaultNXProps.getInstance().setProperty("GrandLivreCompteEnd", this.compteEnd.getText());

        DefaultNXProps.getInstance().store();
    }

    public void taskEnd() {
        bar.setValue(3);
        bar.setString("Terminée");
        validPDF.setEnabled(true);
        validODS.setEnabled(true);
    }
}