OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | Go to most recent revision | Details | 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.model.SelectJournauxModel;
18
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheet;
19
import org.openconcerto.erp.core.finance.accounting.report.JournauxMoisSheet;
20
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheet;
21
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorCompta;
22
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorListener;
23
import org.openconcerto.erp.preferences.DefaultNXProps;
24
import org.openconcerto.sql.Configuration;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.ui.DefaultGridBagConstraints;
27
import org.openconcerto.ui.JDate;
28
import org.openconcerto.ui.TitledSeparator;
29
import org.openconcerto.utils.text.SimpleDocumentListener;
30
 
31
import java.awt.Dimension;
32
import java.awt.GridBagConstraints;
33
import java.awt.GridBagLayout;
34
import java.awt.event.ActionEvent;
35
import java.awt.event.ActionListener;
36
import java.awt.event.KeyAdapter;
37
import java.awt.event.KeyEvent;
38
import java.awt.event.MouseAdapter;
39
import java.awt.event.MouseEvent;
40
import java.beans.PropertyChangeEvent;
41
import java.beans.PropertyChangeListener;
42
import java.util.Calendar;
43
import java.util.Date;
44
 
45
import javax.swing.AbstractAction;
46
import javax.swing.BorderFactory;
47
import javax.swing.ButtonGroup;
48
import javax.swing.JButton;
49
import javax.swing.JCheckBox;
50
import javax.swing.JFrame;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53
import javax.swing.JProgressBar;
54
import javax.swing.JRadioButton;
55
import javax.swing.JScrollPane;
56
import javax.swing.JTable;
57
import javax.swing.JTextField;
58
import javax.swing.SwingUtilities;
59
import javax.swing.event.DocumentEvent;
60
 
61
public class ImpressionJournauxPanel extends JPanel implements SpreadSheetGeneratorListener {
62
 
63
    private final JDate dateDeb, dateEnd;
64
    private JTable tableJrnl;
65
    // private boolean isValidated;
66
    private JButton valid;
67
    private JButton annul;
68
    private JCheckBox checkImpr;
69
    private JCheckBox checkVisu;
70
    private JCheckBox checkCentralMois;
71
    private JTextField compteDeb, compteEnd;
72
    private int mode = GrandLivreSheet.MODEALL;
73
    private JProgressBar bar = new JProgressBar(0, 3);
74
 
75
    public ImpressionJournauxPanel() {
76
        this.setLayout(new GridBagLayout());
77
        final GridBagConstraints c = new DefaultGridBagConstraints();
78
 
79
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
80
        SQLRow rowExercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));
81
 
82
        // this.isValidated = false;
83
        this.dateDeb = new JDate();
84
        this.dateEnd = new JDate();
85
        this.tableJrnl = new JTable(new SelectJournauxModel());
86
 
87
        this.add(new JLabel("Période du"), c);
88
        c.gridx++;
89
        c.weightx = 1;
90
        this.add(this.dateDeb, c);
91
        // Chargement des valeurs par défaut
92
        String valueDateDeb = DefaultNXProps.getInstance().getStringProperty("JournauxDateDeb");
93
        if (valueDateDeb.trim().length() > 0) {
94
            Long l = new Long(valueDateDeb);
95
            this.dateDeb.setValue(new Date(l.longValue()));
96
        } else {
97
            this.dateDeb.setValue((Date) rowExercice.getObject("DATE_DEB"));
98
        }
99
 
100
        c.gridx++;
101
        c.weightx = 0;
102
        this.add(new JLabel("Au"), c);
103
        c.gridx++;
104
        c.weightx = 1;
105
        this.add(this.dateEnd, c);
106
 
107
        // Chargement des valeurs par défaut
108
        String valueDateEnd = DefaultNXProps.getInstance().getStringProperty("JournauxDateEnd");
109
        if (valueDateEnd.trim().length() > 0) {
110
            Long l = new Long(valueDateEnd);
111
            this.dateEnd.setValue(new Date(l.longValue()));
112
        } else {
113
            this.dateEnd.setValue((Date) rowExercice.getObject("DATE_FIN"));
114
        }
115
 
116
        // Compte
117
        this.compteDeb = new JTextField();
118
        this.compteEnd = new JTextField();
119
        c.gridy++;
120
        c.gridx = 0;
121
        this.add(new JLabel("Du compte "), c);
122
        c.gridx++;
123
        c.weightx = 1;
124
        this.add(this.compteDeb, c);
125
        this.compteDeb.setText("1");
126
        this.compteEnd.setText("9");
127
 
128
        c.gridx++;
129
        c.weightx = 0;
130
        this.add(new JLabel("Au"), c);
131
        c.gridx++;
132
        c.weightx = 1;
133
        this.add(this.compteEnd, c);
134
 
135
        c.gridy++;
136
        c.gridx = 0;
137
        c.gridwidth = GridBagConstraints.REMAINDER;
138
 
139
        this.add(new TitledSeparator("Sélection des journaux"), c);
140
 
141
        c.gridy++;
142
        c.fill = GridBagConstraints.BOTH;
143
        c.weightx = 0;
144
        c.weighty = 1;
145
 
146
        JScrollPane scroll = new JScrollPane(this.tableJrnl);
147
        Dimension d;
148
        if (this.tableJrnl.getPreferredSize().height > 200) {
149
            d = new Dimension(scroll.getPreferredSize().width, 200);
150
        } else {
151
            d = new Dimension(scroll.getPreferredSize().width, this.tableJrnl.getPreferredSize().height + 30);
152
        }
153
        scroll.setPreferredSize(d);
154
 
155
        this.add(scroll, c);
156
 
157
        this.valid = new JButton("Valider");
158
        this.annul = new JButton("Fermer");
159
        this.checkImpr = new JCheckBox("Impression");
160
        this.checkVisu = new JCheckBox("Visualisation");
161
 
162
        // Radio mode
163
        JRadioButton radioAll = new JRadioButton(new AbstractAction("Toutes") {
164
            public void actionPerformed(ActionEvent e) {
165
                // TODO Auto-generated method stub
166
                mode = GrandLivreSheet.MODEALL;
167
            }
168
        });
169
 
170
        JRadioButton radioLettree = new JRadioButton(new AbstractAction("Lettrées") {
171
            public void actionPerformed(ActionEvent e) {
172
                // TODO Auto-generated method stub
173
                mode = GrandLivreSheet.MODELETTREE;
174
            }
175
        });
176
 
177
        JRadioButton radioNonLettree = new JRadioButton(new AbstractAction("Non lettrées") {
178
            public void actionPerformed(ActionEvent e) {
179
                // TODO Auto-generated method stub
180
                mode = GrandLivreSheet.MODENONLETTREE;
181
            }
182
        });
183
        JPanel panelMode = new JPanel();
184
        panelMode.add(radioAll);
185
        panelMode.add(radioLettree);
186
        panelMode.add(radioNonLettree);
187
 
188
        c.gridy++;
189
        c.gridx = 0;
190
        c.weightx = 1;
191
        c.gridwidth = 2;
192
        c.weighty = 0;
193
        c.fill = GridBagConstraints.NONE;
194
        ButtonGroup group = new ButtonGroup();
195
        group.add(radioAll);
196
        group.add(radioLettree);
197
        group.add(radioNonLettree);
198
        radioAll.setSelected(true);
199
        panelMode.setBorder(BorderFactory.createTitledBorder("Ecritures"));
200
        this.add(panelMode, c);
201
 
202
        // Centralisation par mois
203
        this.checkCentralMois = new JCheckBox("Centralisation par mois");
204
        c.gridx += 2;
205
        c.gridwidth = GridBagConstraints.REMAINDER;
206
        this.add(this.checkCentralMois, c);
207
 
208
        // Progress bar
209
        c.gridwidth = GridBagConstraints.REMAINDER;
210
        c.gridy++;
211
        c.gridx = 0;
212
        c.weightx = 1;
213
        c.weighty = 0;
214
        c.fill = GridBagConstraints.HORIZONTAL;
215
        this.bar.setStringPainted(true);
216
        this.add(this.bar, c);
217
 
218
        c.gridwidth = 2;
219
        c.gridy++;
220
        c.weightx = 0;
221
        c.weighty = 0;
222
        this.add(this.checkImpr, c);
223
        this.checkImpr.setSelected(true);
224
        c.gridx += 2;
225
        this.add(this.checkVisu, c);
226
 
227
        c.gridy++;
228
        c.gridx = 0;
229
        c.fill = GridBagConstraints.NONE;
230
        c.anchor = GridBagConstraints.CENTER;
231
        this.add(this.valid, c);
232
        c.gridx += 2;
233
        this.add(this.annul, c);
234
        checkValidity();
235
 
236
        this.valid.addActionListener(new ActionListener() {
237
            public void actionPerformed(ActionEvent e) {
238
                bar.setString(null);
239
                bar.setValue(1);
240
                valid.setEnabled(false);
241
                new Thread(new Runnable() {
242
                    public void run() {
243
                        int[] idS = ((SelectJournauxModel) tableJrnl.getModel()).getSelectedIds(tableJrnl.getSelectedRows());
244
                        JournauxSheet bSheet;
245
                        if (checkCentralMois.isSelected()) {
246
                            bSheet = new JournauxMoisSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode);
247
                        } else {
248
                            bSheet = new JournauxSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
249
                        }
250
 
251
                        final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), checkImpr.isSelected(), checkVisu
252
                                .isSelected());
253
                        SwingUtilities.invokeLater(new Runnable() {
254
                            public void run() {
255
                                bar.setValue(2);
256
                                generator.addGenerateListener(ImpressionJournauxPanel.this);
257
                            }
258
                        });
259
                    }
260
                }).start();
261
            }
262
        });
263
        this.annul.addActionListener(new ActionListener() {
264
            public void actionPerformed(ActionEvent e) {
265
                ((JFrame) SwingUtilities.getRoot(ImpressionJournauxPanel.this)).dispose();
266
            }
267
        });
268
 
269
        this.dateDeb.addValueListener(new PropertyChangeListener() {
270
            public void propertyChange(PropertyChangeEvent evt) {
271
                checkValidity();
272
                storeValue();
273
            }
274
        });
275
        this.dateEnd.addValueListener(new PropertyChangeListener() {
276
            public void propertyChange(PropertyChangeEvent evt) {
277
                checkValidity();
278
                storeValue();
279
            }
280
        });
281
        this.tableJrnl.addMouseListener(new MouseAdapter() {
282
 
283
            public void mouseReleased(MouseEvent e) {
284
                checkValidity();
285
            }
286
        });
287
        this.tableJrnl.addKeyListener(new KeyAdapter() {
288
            public void keyReleased(KeyEvent e) {
289
                checkValidity();
290
            }
291
        });
292
 
293
        SimpleDocumentListener doc = new SimpleDocumentListener() {
294
            @Override
295
            public void update(DocumentEvent e) {
296
 
297
                checkValidity();
298
            }
299
        };
300
 
301
        this.compteDeb.getDocument().addDocumentListener(doc);
302
        // Chargement des valeurs par défaut
303
 
304
        this.compteDeb.setText("1");
305
 
306
        this.compteEnd.getDocument().addDocumentListener(doc);
307
        // Chargement des valeurs par défaut
308
 
309
        this.compteEnd.setText("8");
310
 
311
    }
312
 
313
    private void checkValidity() {
314
 
315
        Date beginDate = this.dateDeb.getDate();
316
        Date endDate = this.dateEnd.getDate();
317
 
318
        // System.err.println("Check validity between ");
319
        if (beginDate == null || endDate == null) {
320
            this.valid.setEnabled(false);
321
        } else {
322
            if (beginDate.after(endDate)) {
323
                this.valid.setEnabled(false);
324
            } else {
325
                if (this.tableJrnl.getSelectedRows().length == 0) {
326
                    this.valid.setEnabled(false);
327
                } else {
328
                    this.valid.setEnabled(true);
329
                }
330
            }
331
        }
332
 
333
        if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
334
            this.valid.setEnabled(false);
335
        } else {
336
            if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
337
                this.valid.setEnabled(false);
338
            } else {
339
                if (beginDate.after(endDate)) {
340
                    this.valid.setEnabled(false);
341
                }
342
            }
343
        }
344
    }
345
 
346
    private void storeValue() {
347
 
348
        // Set date debut
349
        Date d = this.dateDeb.getDate();
350
        if (d != null) {
351
            DefaultNXProps.getInstance().setProperty("JournauxDateDeb", String.valueOf(d.getTime()));
352
        }
353
 
354
        // Set date Fin
355
        Date dFin = this.dateEnd.getDate();
356
        if (dFin != null) {
357
            DefaultNXProps.getInstance().setProperty("JournauxDateEnd", String.valueOf(dFin.getTime()));
358
        }
359
 
360
        DefaultNXProps.getInstance().store();
361
    }
362
 
363
    public void taskEnd() {
364
        bar.setValue(3);
365
        bar.setString("Terminée");
366
        valid.setEnabled(true);
367
    }
368
}