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-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.invoice.ui;

import org.openconcerto.erp.core.sales.invoice.report.ReportingVenteXmlSheet;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JDate;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.utils.ExceptionHandler;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class GenReportingVentePanel extends JPanel implements ActionListener {

    private final JButton buttonGen = new JButton("Créer");
    private final JDate du;
    private final JDate au;
    JProgressBar bar = new JProgressBar();
    private JTable table;
    private final boolean commande;

    public GenReportingVentePanel(final boolean commande) {
        super(new GridBagLayout());
        this.commande = commande;
        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.anchor = GridBagConstraints.CENTER;
        this.add(new JLabelBold("Reporting des " + (commande ? "commandes" : "factures")), c);

        c.gridwidth = 1;
        c.gridy++;
        c.anchor = GridBagConstraints.WEST;
        this.add(new JLabel("Du"), c);

        c.gridx++;
        c.weightx = 1;
        this.du = new JDate(true);
        this.add(this.du, c);

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

        c.gridx++;
        c.weightx = 1;
        this.au = new JDate(true);
        this.add(this.au, c);

        this.table = new JTable(new SelectCommerciauxModel());
        JScrollPane scroll = new JScrollPane(this.table);
        Dimension d;
        if (this.table.getPreferredSize().height > 200) {
            d = new Dimension(scroll.getPreferredSize().width, 200);
        } else {
            d = new Dimension(scroll.getPreferredSize().width, this.table.getPreferredSize().height + 30);
        }
        scroll.setPreferredSize(d);
        c.gridx++;
        c.gridwidth = GridBagConstraints.REMAINDER;
        this.add(scroll, c);

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

        c.gridy++;
        c.gridx = 0;
        c.gridwidth = 1;
        JPanel panelButton = new JPanel();
        panelButton.add(this.buttonGen);
        final JButton buttonClose = new JButton("Fermer");
        panelButton.add(buttonClose);
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.SOUTHEAST;
        c.weightx = 0;
        c.weighty = 1;
        this.add(panelButton, c);
        this.buttonGen.addActionListener(this);
        buttonClose.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.buttonGen) {
            final List<Integer> idS = ((SelectCommerciauxModel) table.getModel()).getSelectedIds(table.getSelectedRows());
            final Thread thread = new Thread(new Runnable() {
                public void run() {
                    try {

                        ReportingVenteXmlSheet sheet = new ReportingVenteXmlSheet(idS, GenReportingVentePanel.this.du.getDate(), GenReportingVentePanel.this.au.getDate(),
                                GenReportingVentePanel.this.bar, commande);

                        sheet.createDocumentAsynchronous().get();
                        sheet.showPrintAndExport(true, false, false, false, false, Collections.emptyList());
                    } catch (Exception e) {
                        ExceptionHandler.handle("Erreur de traitement", e);
                    }
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            ((JFrame) SwingUtilities.getRoot(GenReportingVentePanel.this)).dispose();
                        }
                    });
                }
            });
            thread.start();
        } else {
            ((JFrame) SwingUtilities.getRoot(this)).dispose();
        }
    }
}