OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.finance.accounting.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.model.SQLBase;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLTable;
180 ilm 23
import org.openconcerto.sql.model.Where;
18 ilm 24
import org.openconcerto.ui.DefaultGridBagConstraints;
25
import org.openconcerto.ui.warning.JLabelWarning;
26
 
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
180 ilm 31
import java.util.ArrayList;
32
import java.util.HashSet;
18 ilm 33
import java.util.List;
180 ilm 34
import java.util.Set;
18 ilm 35
 
36
import javax.swing.JButton;
37
import javax.swing.JFrame;
38
import javax.swing.JLabel;
94 ilm 39
import javax.swing.JOptionPane;
18 ilm 40
import javax.swing.JPanel;
41
import javax.swing.SwingUtilities;
42
 
43
import org.apache.commons.dbutils.handlers.ArrayListHandler;
44
 
45
public class SuppressionEcrituresPanel extends JPanel {
46
 
47
    public SuppressionEcrituresPanel(final int idMvt) {
48
        this.setLayout(new GridBagLayout());
49
        final GridBagConstraints c = new DefaultGridBagConstraints();
50
        c.weightx = 1;
51
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
52
        SQLTable tableMouvement = base.getTable("MOUVEMENT");
53
        SQLRow rowMvt = tableMouvement.getRow(idMvt);
54
        JLabel label = new JLabel("Vous allez supprimer la piéce n°" + rowMvt.getInt("ID_PIECE"));
55
        JLabelWarning warning = new JLabelWarning();
56
        JPanel panelLabel = new JPanel();
57
        panelLabel.add(warning);
58
        panelLabel.add(label);
59
 
60
        c.gridwidth = GridBagConstraints.REMAINDER;
61
        this.add(panelLabel, c);
62
 
63
        // TODO afficher les numeros de mouvement implique
64
        int[] idS = getMouvement(rowMvt.getInt("ID_PIECE"));
65
        if (idS == null) {
156 ilm 66
            JOptionPane.showMessageDialog(null, "Aucun mouvement associé à la piéce n°" + ((rowMvt != null) ? rowMvt.getInt("ID_PIECE") : "mouvement nul"));
18 ilm 67
        } else {
68
            StringBuffer s = new StringBuffer();
69
            s.append("Elle est composée par les mouvements : (");
70
            JLabel labelMouv = new JLabel();
71
            // c.gridwidth = 1;
72
            c.gridy++;
73
            c.gridx = 0;
74
            this.add(labelMouv, c);
75
            s.append(idS[0]);
180 ilm 76
            List<Integer> idMvts = new ArrayList<>();
18 ilm 77
            for (int i = 1; i < idS.length; i++) {
180 ilm 78
                idMvts.add(idS[i]);
18 ilm 79
                s.append(", ");
80
                s.append(idS[i]);
81
            }
82
            s.append(')');
180 ilm 83
 
84
            Set<String> lettrage = getLettrage(idMvts);
85
            if (lettrage != null && !lettrage.isEmpty()) {
86
                s.append("\nAttention certaines écritures sont lettrées avec les codes(" + org.openconcerto.utils.CollectionUtils.join(lettrage, ",") + "!");
87
            }
18 ilm 88
            labelMouv.setText(s.toString());
180 ilm 89
 
18 ilm 90
        }
91
 
92
        JButton buttonOK = new JButton("OK");
93
        JButton buttonCancel = new JButton("Annuler");
94
 
95
        c.gridwidth = 1;
96
        c.gridy++;
97
        c.gridx = 0;
98
        this.add(buttonOK, c);
99
        c.gridx++;
100
        this.add(buttonCancel, c);
101
 
102
        buttonOK.addActionListener(new ActionListener() {
103
            public void actionPerformed(ActionEvent e) {
104
                ((JFrame) SwingUtilities.getRoot(SuppressionEcrituresPanel.this)).dispose();
94 ilm 105
                try {
106
                    EcritureSQLElement elt = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
107
                    elt.archiveMouvement(idMvt);
108
                } catch (Exception ex) {
109
                    JOptionPane.showMessageDialog(null, "Impossible de supprimer le mouvement associé.\n" + ex.getMessage());
110
                }
111
 
18 ilm 112
            }
113
        });
114
        buttonCancel.addActionListener(new ActionListener() {
115
            public void actionPerformed(ActionEvent e) {
116
                ((JFrame) SwingUtilities.getRoot(SuppressionEcrituresPanel.this)).dispose();
117
            }
118
        });
119
    }
120
 
121
    private int[] getMouvement(int idPiece) {
122
 
123
        int[] idS = null;
124
 
125
        SQLBase b = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
126
        SQLTable tableMvt = b.getTable("MOUVEMENT");
127
 
156 ilm 128
        SQLSelect sel = new SQLSelect();
18 ilm 129
        sel.addSelect(tableMvt.getField("NUMERO"));
65 ilm 130
        sel.setWhere(tableMvt.getField("ID_PIECE"), "=", idPiece);
18 ilm 131
 
132
        List l = (List) b.getDataSource().execute(sel.asString(), new ArrayListHandler());
133
 
156 ilm 134
        if (!l.isEmpty()) {
18 ilm 135
            idS = new int[l.size()];
136
        }
137
 
138
        for (int i = 0; i < l.size(); i++) {
139
            Object[] tmp = (Object[]) l.get(i);
156 ilm 140
            idS[i] = ((Number) tmp[0]).intValue();
18 ilm 141
        }
142
 
143
        return idS;
144
    }
145
 
180 ilm 146
    private Set<String> getLettrage(List<Integer> mvtIds) {
147
 
148
        Set<String> codes = new HashSet<>();
149
 
150
        SQLBase b = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
151
        SQLTable tableEcr = b.getTable("ECRITURE");
152
 
153
        SQLSelect sel = new SQLSelect();
154
        sel.setDistinct(true);
155
        sel.addSelect(tableEcr.getField("LETTRAGE"));
156
        sel.setWhere(new Where(tableEcr.getField("ID_MOUVEMENT"), mvtIds).and(new Where(tableEcr.getField("LETTRAGE"), "=", "").and(new Where(tableEcr.getField("LETTRAGE"), "!=", (Object) null))));
157
 
158
        List l = (List) b.getDataSource().execute(sel.asString(), new ArrayListHandler());
159
 
160
        for (int i = 0; i < l.size(); i++) {
161
            Object[] tmp = (Object[]) l.get(i);
162
            codes.add(((String) tmp[0]));
163
        }
164
 
165
        return codes;
166
    }
167
 
18 ilm 168
}