OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 128 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Créé le 3 juin 2012
 */
package org.openconcerto.modules.project;

import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.config.Gestion;
import org.openconcerto.erp.core.reports.history.ui.ListeHistoriquePanel;
import org.openconcerto.erp.core.supplychain.order.action.ImportProductsToOrder;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLBase;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.view.IListPanel;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.PanelFrame;
import org.openconcerto.ui.SwingThreadUtils;
import org.openconcerto.ui.state.WindowStateManager;

public class ProjectHistory {
    private PanelFrame panelFrame;
    private ListeHistoriquePanel listPanel;

    public ListeHistoriquePanel getHistoriquePanel() {
        return this.listPanel;
    }

    private static Map<String, List<String>> moduleTab = new HashMap<String, List<String>>();

    public static void addTab(String tabName, List<String> table) {
        moduleTab.put(tabName, table);
    }

    public ProjectHistory() {

        final ComptaPropsConfiguration comptaPropsConfiguration = ((ComptaPropsConfiguration) Configuration.getInstance());
        final SQLBase b = comptaPropsConfiguration.getSQLBaseSociete();

        final Map<String, List<String>> mapList = new LinkedHashMap<String, List<String>>();
        mapList.put("Devis", Arrays.asList("DEVIS"));
        mapList.put("Bons de commande", Arrays.asList("COMMANDE_CLIENT"));
        mapList.put("Factures", Arrays.asList("SAISIE_VENTE_FACTURE"));
        mapList.put("Avoirs", Arrays.asList("AVOIR_CLIENT"));
        mapList.put("Demandes d'achat", Arrays.asList("DEMANDE_ACHAT_ELEMENT"));
        mapList.put("Demandes de prix", Arrays.asList("DEMANDE_PRIX"));
        mapList.put("Commandes", Arrays.asList("COMMANDE"));
        mapList.put("Achats", Arrays.asList("SAISIE_ACHAT"));

        if (Configuration.getInstance().getRoot().findTable("AFFAIRE_TEMPS") != null) {
            mapList.put("Temps", Arrays.asList("AFFAIRE_TEMPS"));
        }

        mapList.putAll(moduleTab);

        final ComboSQLRequest request = new org.openconcerto.sql.request.ComboSQLRequest(b.getTable("AFFAIRE"), Arrays.asList("NUMERO", "ID_CLIENT"));
        request.setUndefLabel("Toutes les affaires");
        request.setFieldSeparator(" ");

        this.listPanel = new ListeHistoriquePanel("Affaires", request, mapList, null, null, "Toutes les affaires", true, null, null);

        final IListPanel listeDevis = listPanel.getIListePanelFromTableName("DEVIS");
        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridy = 4;
        c.fill = GridBagConstraints.BOTH;
        final ProjectHistoryDevisBottomPanel devisPanel = new ProjectHistoryDevisBottomPanel();
        listeDevis.add(devisPanel, c);
        listeDevis.getListe().getTableModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                devisPanel.updateDevis(listeDevis.getListe());
                devisPanel.updateTimeDevis(listeDevis.getListe());
            }
        });

        final IListPanel listeDmdAchat = listPanel.getIListePanelFromTableName("DEMANDE_ACHAT_ELEMENT");
        PredicateRowAction actionDrop = new PredicateRowAction(new AbstractAction("Importer depuis Fichier Inventor") {

            @Override
            public void actionPerformed(ActionEvent e) {
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
                final FileDialog fd = new FileDialog(frame, "Import fichier inventor", FileDialog.LOAD);
                fd.setFilenameFilter(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".xls");
                    }
                });
                fd.setVisible(true);
                if (fd.getFile() != null) {

                    ImportProductsToOrder importer = new ImportProductsToOrder();

                    int a = JOptionPane.showConfirmDialog(frame, "Etes vous sûr de vouloir importer ces éléments dans l'affaire N°" + listPanel.getSelectedRow().getString("NUMERO") + "?",
                            "Import invertor", JOptionPane.YES_NO_OPTION);
                    if (a == JOptionPane.YES_OPTION) {
                        importer.setRowAffaire(listPanel.getSelectedRow());
                        try {
                            importer.importFile(new File(fd.getDirectory(), fd.getFile()), b.getTable("DEMANDE_ACHAT_ELEMENT").getDBRoot());
                            listeDmdAchat.getListe().getModel().updateAll();
                        } catch (IOException e1) {
                            org.openconcerto.utils.ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
                        } catch (SQLException e1) {
                            org.openconcerto.utils.ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
                        }
                    }
                }
            }
        }, true);
        actionDrop.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
        listeDmdAchat.getListe().addIListeAction(actionDrop);

        final IListPanel listeCmd = listPanel.getIListePanelFromTableName("COMMANDE_CLIENT");
        final ProjectHistoryCmdBottomPanel cmdPanel = new ProjectHistoryCmdBottomPanel();

        listeCmd.add(cmdPanel, c);
        listeCmd.getListe().getTableModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                cmdPanel.updateCmd(listeCmd.getListe());
                cmdPanel.updateTimeCmd(listeCmd.getListe());
            }
        });

        // If time tracking is installed
        final IListPanel listeTemps = listPanel.getIListePanelFromTableName("AFFAIRE_TEMPS");
        if (listeTemps != null) {
            final ProjectHistoryTimeBottomPanel timePanel = new ProjectHistoryTimeBottomPanel();
            listeTemps.add(timePanel, c);
            listeTemps.getListe().getTableModel().addTableModelListener(new TableModelListener() {

                @Override
                public void tableChanged(TableModelEvent e) {
                    timePanel.updateTime(listeTemps.getListe());
                }
            });
        }
        // History
        this.panelFrame = new PanelFrame(this.listPanel, "Historique affaires");
        this.panelFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                listPanel.removeAllTableListener();
            };
        });

        this.panelFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    public PanelFrame getFrame() {
        this.panelFrame.setIconImages(Gestion.getFrameIcon());
        final WindowStateManager stateManager = new WindowStateManager(this.panelFrame,
                new File(Configuration.getInstance().getConfDir(), "Configuration" + File.separator + "Frame" + File.separator + "HistoAffaires.xml"), true);
        this.panelFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.panelFrame.pack();
        this.panelFrame.setLocationRelativeTo(null);
        stateManager.loadState();
        return this.panelFrame;
    }
}