OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 146 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

import java.awt.GridBagConstraints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
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.modules.project.panel.HistoriqueAffaireBilanPanel;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLBase;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.SQLTableEvent;
import org.openconcerto.sql.model.SQLTableModifiedListener;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.view.IListPanel;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.PanelFrame;
import org.openconcerto.ui.state.WindowStateManager;
import org.openconcerto.utils.cc.ITransformer;

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("Chiffrage", Arrays.asList("CHIFFRAGE_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("Bons de réception", Arrays.asList("BON_RECEPTION"));
        mapList.put("Achats", Arrays.asList("SAISIE_ACHAT"));
        // mapList.put("Factures fournisseurs", Arrays.asList("FACTURE_FOURNISSEUR"));
        mapList.put("Avoirs fournisseurs", Arrays.asList("AVOIR_FOURNISSEUR"));

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

        mapList.putAll(moduleTab);

        final HistoriqueAffaireBilanPanel bilanPanel = new HistoriqueAffaireBilanPanel();

        final ComboSQLRequest request = new org.openconcerto.sql.request.ComboSQLRequest(b.getTable("AFFAIRE"), Arrays.asList("NUMERO", "ID_CLIENT"));
        if (b.getTable("AFFAIRE").contains("ID_SOCIETE_COMMON")) {

            final int socID = comptaPropsConfiguration.getSocieteID();
            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
                @Override
                public SQLSelect transformChecked(SQLSelect input) {
                    Where w = new Where(b.getTable("AFFAIRE").getField("ID_SOCIETE_COMMON"), "=", (Object) null).or(new Where(b.getTable("AFFAIRE").getField("ID_SOCIETE_COMMON"), "=", 1))
                            .or(new Where(b.getTable("AFFAIRE").getField("ID_SOCIETE_COMMON"), "=", socID));

                    input.andWhere(w);
                    return input;
                }
            });
        }
        request.setUndefLabel("Toutes les affaires");
        request.setFieldSeparator(" ");
        Map<String, String> splitter = new HashMap<String, String>();
        splitter.put("DEMANDE_ACHAT_ELEMENT", "ID_FAMILLE_ARTICLE");
        Map<SQLTable, SQLField> fieldLink = new HashMap<SQLTable, SQLField>();
        fieldLink.put(b.getTable("CHIFFRAGE_COMMANDE_CLIENT"), b.getTable("CHIFFRAGE_COMMANDE_CLIENT").getField("ID_COMMANDE_CLIENT"));
        this.listPanel = new ListeHistoriquePanel("Affaires", request, mapList, bilanPanel, fieldLink, "Toutes les affaires", true, null, null, splitter);

        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 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);

        final SQLTableModifiedListener cmdClientListener = new SQLTableModifiedListener() {

            @Override
            public void tableModified(SQLTableEvent evt) {
                if (bilanPanel != null) {
                    final SQLRowAccessor selectedRow = ProjectHistory.this.listPanel.getSelectedRow();
                    if (selectedRow != null) {
                        bilanPanel.updateCommandeClient(selectedRow.asRow());
                    }
                }
            }
        };
        final SQLTableModifiedListener devisListener = new SQLTableModifiedListener() {

            @Override
            public void tableModified(SQLTableEvent evt) {
                if (bilanPanel != null) {
                    final SQLRowAccessor selectedRow = ProjectHistory.this.listPanel.getSelectedRow();
                    if (selectedRow != null) {
                        bilanPanel.updateDevis(selectedRow.asRow());
                    }
                }
            }
        };
        this.panelFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                listPanel.removeAllTableListener();
                b.getTable("COMMANDE_CLIENT").removeTableModifiedListener(cmdClientListener);
                b.getTable("DEVIS").removeTableModifiedListener(devisListener);
            };
        });

        b.getTable("COMMANDE_CLIENT").addTableModifiedListener(cmdClientListener);
        b.getTable("DEVIS").addTableModifiedListener(devisListener);

        this.listPanel.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent arg0) {

                final SQLRowAccessor selectedRow = ProjectHistory.this.listPanel.getSelectedRow();
                if (selectedRow != null) {
                    bilanPanel.updateCommandeClient(selectedRow.asRow());
                    bilanPanel.updateDevis(selectedRow.asRow());
                    bilanPanel.updateBilanPreBilan(ProjectHistory.this.listPanel.getIListeFromTableName("COMMANDE"));
                }
            }
        });

        this.listPanel.addListenerTable(new TableModelListener() {
            public void tableChanged(TableModelEvent arg0) {
                final SQLRowAccessor selectedRow = ProjectHistory.this.listPanel.getSelectedRow();
                if (selectedRow != null) {
                    bilanPanel.updateBilanPreBilan(ProjectHistory.this.listPanel.getIListeFromTableName("COMMANDE"));
                }
            }
        }, "COMMANDE");

        this.listPanel.addListenerTable(new TableModelListener() {
            public void tableChanged(TableModelEvent arg0) {
                bilanPanel.updateAchat(ProjectHistory.this.listPanel.getIListeFromTableName("SAISIE_ACHAT"), ProjectHistory.this.listPanel.getIListeFromTableName("AVOIR_FOURNISSEUR"));
            }
        }, "SAISIE_ACHAT");
        this.listPanel.addListenerTable(new TableModelListener() {
            public void tableChanged(TableModelEvent arg0) {
                bilanPanel.updateAchat(ProjectHistory.this.listPanel.getIListeFromTableName("SAISIE_ACHAT"), ProjectHistory.this.listPanel.getIListeFromTableName("AVOIR_FOURNISSEUR"));
            }
        }, "AVOIR_FOURNISSEUR");

        this.listPanel.addListenerTable(new TableModelListener() {
            public void tableChanged(TableModelEvent arg0) {
                bilanPanel.updateFacturer(ProjectHistory.this.listPanel.getIListeFromTableName("SAISIE_VENTE_FACTURE"), ProjectHistory.this.listPanel.getIListeFromTableName("AVOIR_CLIENT"));
            }
        }, "SAISIE_VENTE_FACTURE");

        this.listPanel.addListenerTable(new TableModelListener() {
            public void tableChanged(TableModelEvent arg0) {
                bilanPanel.updateFacturer(ProjectHistory.this.listPanel.getIListeFromTableName("SAISIE_VENTE_FACTURE"), ProjectHistory.this.listPanel.getIListeFromTableName("AVOIR_CLIENT"));
            }
        }, "AVOIR_CLIENT");
    }

    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;
    }
}