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 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.payment.action;

import org.openconcerto.erp.action.CreateFrameAbstractAction;
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.view.IListFrame;
import org.openconcerto.sql.view.ListeAddPanel;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.ui.DefaultGridBagConstraints;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ListeDesEncaissementsAction extends CreateFrameAbstractAction {

    public ListeDesEncaissementsAction() {
        super();
        this.putValue(Action.NAME, "Liste des encaissements");
    }

    public JFrame createFrame() {

        final SQLElement elementEchClient = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT");

        ListeAddPanel panel = new ListeAddPanel(elementEchClient, new IListe(elementEchClient.getTableSource(true))) {
            // FIXME faire les checks en plus dans l'element
            @Override
            protected void handleAction(JButton source, ActionEvent evt) {
                if (source == this.buttonEffacer) {
                    List<SQLRowValues> rowValsSel = this.getListe().getSelectedRows();

                    if (isEncaissementEditable(rowValsSel, "effacer")) {
                        super.handleAction(source, evt);
                    }
                } else if (source == this.buttonModifier) {
                    List<SQLRowValues> rowValsSel = this.getListe().getSelectedRows();

                    if (isEncaissementEditable(rowValsSel, "supprimer")) {
                        super.handleAction(source, evt);
                    }
                } else {
                    super.handleAction(source, evt);
                }
            }

            private boolean isEncaissementEditable(List<SQLRowValues> rowValsSel, String action) {
                for (SQLRowValues sqlRowValues : rowValsSel) {
                    final SQLRow asRow = sqlRowValues.asRow();
                    Collection<? extends SQLRowAccessor> rowItems = asRow.getReferentRows(sqlRowValues.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
                    for (SQLRowAccessor sqlRowValues2 : rowItems) {
                        if (sqlRowValues2.isForeignEmpty("ID_ECHEANCE_CLIENT")) {
                            JOptionPane.showMessageDialog(null, "Impossible de " + action + " un encaissement qui ne vient pas d'une échéance");
                            return false;
                        }
                    }
                    SQLRowAccessor rowAcMvt = asRow.getNonEmptyForeign("ID_MOUVEMENT");
                    if (rowAcMvt != null) {
                        Collection<? extends SQLRowAccessor> rowItemsEcr = rowAcMvt.getReferentRows(rowAcMvt.getTable().getTable("ECRITURE").getField("ID_MOUVEMENT"));
                        for (SQLRowAccessor sqlRowValues2 : rowItemsEcr) {
                            if (sqlRowValues2.getBoolean("VALIDE")) {
                                JOptionPane.showMessageDialog(null, "Impossible de " + action + " un encaissement dont les écritures sont validées.");
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
        };

        panel.setAddVisible(false);

        // panel.setDeleteVisible(false);
        IListFrame frame = new IListFrame(panel);

        List<SQLField> fields = new ArrayList<SQLField>(2);
        fields.add(elementEchClient.getTable().getField("MONTANT"));

        IListTotalPanel totalPanel = new IListTotalPanel(frame.getPanel().getListe(), fields, "Total Global");

        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0;

        // Total panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        c.weightx = 1;
        c.gridy = 4;

        frame.getPanel().add(totalPanel, c);

        // Date panel
        IListFilterDatePanel datePanel = new IListFilterDatePanel(frame.getPanel().getListe(), elementEchClient.getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
        c.gridy++;
        c.anchor = GridBagConstraints.CENTER;
        frame.getPanel().add(datePanel, c);

        return frame;
    }
}