OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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.report;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.EmailComposer;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.FileUtils;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

public class SituationCompteClientPanel extends JPanel {

    public SituationCompteClientPanel(ComptaPropsConfiguration conf) {
        super(new GridBagLayout());

        JLabelBold title = new JLabelBold("Génération d'une situation de compte d'un client");

        final ElementComboBox box = new ElementComboBox(true);
        SQLElement element = Configuration.getInstance().getDirectory().getElement("CLIENT");
        ComboSQLRequest comboRequest = element.getComboRequest(true);
        // comboRequest.setUndefLabel("Tous");
        box.init(element, comboRequest);

        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridwidth = 1;
        c.weightx = 0;
        this.add(title, c);
        c.gridx++;
        c.gridwidth = 1;
        c.weightx = 1;
        this.add(box, c);

        final JCheckBox boxMail = new JCheckBox("Envoie par mail");
        c.gridx++;
        c.weightx = 0;
        this.add(boxMail, c);

        final JButton buttonValid = new JButton(new AbstractAction("Valider") {

            @Override
            public void actionPerformed(ActionEvent e) {

                final SQLRow selectedClient = box.getSelectedRow();
                new Thread() {
                    public void run() {

                        try {
                            SituationCompteXmlSheet sheet = new SituationCompteXmlSheet(conf, selectedClient);
                            File pdf = sheet.getOrCreatePDFDocumentFile(true);
                            if (boxMail.isSelected()) {
                                sheet.createDocument();
                                // sheet.showPrintAndExport(false, false, false);

                                String mail = selectedClient.getString("MAIL");

                                try {
                                    EmailComposer.getInstance().compose(mail, "", "", pdf);
                                } catch (Exception exn) {
                                    ExceptionHandler.handle(null, "Impossible de créer le courriel", exn);
                                }
                            } else {
                                FileUtils.openFile(pdf);
                            }
                        } catch (Exception e) {
                            ExceptionHandler.handle("Une erreur est survenue lors de la création du document", e);
                        }
                    };
                }.start();

            }
        });

        c.gridy++;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        c.weightx = 0;
        this.add(buttonValid, c);

        // Listener enabled/disabled button
        buttonValid.setEnabled(false);
        box.addModelListener("wantedID", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent arg0) {
                buttonValid.setEnabled(box.getWantedID() != SQLRow.NONEXISTANT_ID && box.getWantedID() != box.getRequest().getPrimaryTable().getUndefinedID());
            }
        });
    }
}