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 | 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;
65 ilm 17
import org.openconcerto.erp.config.Gestion;
18 ilm 18
import org.openconcerto.erp.core.finance.accounting.model.SelectJournauxModel;
19
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheet;
20
import org.openconcerto.erp.core.finance.accounting.report.JournauxMoisSheet;
21
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheet;
65 ilm 22
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheetXML;
18 ilm 23
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorCompta;
24
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorListener;
25
import org.openconcerto.erp.preferences.DefaultNXProps;
26
import org.openconcerto.sql.Configuration;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.JDate;
30
import org.openconcerto.ui.TitledSeparator;
65 ilm 31
import org.openconcerto.utils.ExceptionHandler;
18 ilm 32
import org.openconcerto.utils.text.SimpleDocumentListener;
33
 
34
import java.awt.Dimension;
35
import java.awt.GridBagConstraints;
36
import java.awt.GridBagLayout;
37
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionListener;
39
import java.awt.event.KeyAdapter;
40
import java.awt.event.KeyEvent;
41
import java.awt.event.MouseAdapter;
42
import java.awt.event.MouseEvent;
43
import java.beans.PropertyChangeEvent;
44
import java.beans.PropertyChangeListener;
45
import java.util.Calendar;
46
import java.util.Date;
47
 
48
import javax.swing.AbstractAction;
49
import javax.swing.BorderFactory;
50
import javax.swing.ButtonGroup;
51
import javax.swing.JButton;
52
import javax.swing.JCheckBox;
53
import javax.swing.JFrame;
54
import javax.swing.JLabel;
55
import javax.swing.JPanel;
56
import javax.swing.JProgressBar;
57
import javax.swing.JRadioButton;
58
import javax.swing.JScrollPane;
59
import javax.swing.JTable;
60
import javax.swing.JTextField;
65 ilm 61
import javax.swing.SwingConstants;
18 ilm 62
import javax.swing.SwingUtilities;
63
import javax.swing.event.DocumentEvent;
64
 
65
public class ImpressionJournauxPanel extends JPanel implements SpreadSheetGeneratorListener {
66
 
67
    private final JDate dateDeb, dateEnd;
68
    private JTable tableJrnl;
69
    private JButton valid;
70
    private JButton annul;
71
    private JCheckBox checkCentralMois;
132 ilm 72
    private JCheckBox checkAncienModele;
18 ilm 73
    private JTextField compteDeb, compteEnd;
74
    private int mode = GrandLivreSheet.MODEALL;
75
    private JProgressBar bar = new JProgressBar(0, 3);
76
 
77
    public ImpressionJournauxPanel() {
78
        this.setLayout(new GridBagLayout());
79
        final GridBagConstraints c = new DefaultGridBagConstraints();
80
 
81
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
82
        SQLRow rowExercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));
83
 
84
        // this.isValidated = false;
85
        this.dateDeb = new JDate();
86
        this.dateEnd = new JDate();
87
        this.tableJrnl = new JTable(new SelectJournauxModel());
88
 
65 ilm 89
        this.add(new JLabel("Période du", SwingConstants.RIGHT), c);
18 ilm 90
        c.gridx++;
91
        c.weightx = 1;
92
        this.add(this.dateDeb, c);
93
        // Chargement des valeurs par défaut
94
        String valueDateDeb = DefaultNXProps.getInstance().getStringProperty("JournauxDateDeb");
95
        if (valueDateDeb.trim().length() > 0) {
96
            Long l = new Long(valueDateDeb);
97
            this.dateDeb.setValue(new Date(l.longValue()));
98
        } else {
99
            this.dateDeb.setValue((Date) rowExercice.getObject("DATE_DEB"));
100
        }
101
 
102
        c.gridx++;
103
        c.weightx = 0;
104
        this.add(new JLabel("Au"), c);
105
        c.gridx++;
106
        c.weightx = 1;
107
        this.add(this.dateEnd, c);
108
 
109
        // Chargement des valeurs par défaut
110
        String valueDateEnd = DefaultNXProps.getInstance().getStringProperty("JournauxDateEnd");
111
        if (valueDateEnd.trim().length() > 0) {
112
            Long l = new Long(valueDateEnd);
113
            this.dateEnd.setValue(new Date(l.longValue()));
114
        } else {
115
            this.dateEnd.setValue((Date) rowExercice.getObject("DATE_FIN"));
116
        }
117
 
118
        // Compte
119
        this.compteDeb = new JTextField();
120
        this.compteEnd = new JTextField();
121
        c.gridy++;
122
        c.gridx = 0;
65 ilm 123
        this.add(new JLabel("Du compte ", SwingConstants.RIGHT), c);
18 ilm 124
        c.gridx++;
125
        c.weightx = 1;
126
        this.add(this.compteDeb, c);
127
        this.compteDeb.setText("1");
128
        this.compteEnd.setText("9");
129
 
130
        c.gridx++;
131
        c.weightx = 0;
132
        this.add(new JLabel("Au"), c);
133
        c.gridx++;
134
        c.weightx = 1;
135
        this.add(this.compteEnd, c);
136
 
137
        c.gridy++;
138
        c.gridx = 0;
139
        c.gridwidth = GridBagConstraints.REMAINDER;
140
 
141
        this.add(new TitledSeparator("Sélection des journaux"), c);
142
 
143
        c.gridy++;
144
        c.fill = GridBagConstraints.BOTH;
145
        c.weightx = 0;
146
        c.weighty = 1;
147
 
148
        JScrollPane scroll = new JScrollPane(this.tableJrnl);
149
        Dimension d;
150
        if (this.tableJrnl.getPreferredSize().height > 200) {
151
            d = new Dimension(scroll.getPreferredSize().width, 200);
152
        } else {
153
            d = new Dimension(scroll.getPreferredSize().width, this.tableJrnl.getPreferredSize().height + 30);
154
        }
155
        scroll.setPreferredSize(d);
156
 
157
        this.add(scroll, c);
158
 
159
        this.valid = new JButton("Valider");
160
        this.annul = new JButton("Fermer");
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
132 ilm 180
                mode = GrandLivreSheet.MODENONLETTREE_PERIODE;
18 ilm 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
 
132 ilm 208
        // Ancien modele
209
        this.checkAncienModele = new JCheckBox("Ancien modèle");
210
        // c.gridy++;
211
        // c.gridx += 2;
212
        // c.gridwidth = GridBagConstraints.REMAINDER;
213
        // this.add(this.checkAncienModele, c);
214
 
18 ilm 215
        // Progress bar
216
        c.gridwidth = GridBagConstraints.REMAINDER;
217
        c.gridy++;
218
        c.gridx = 0;
219
        c.weightx = 1;
220
        c.weighty = 0;
221
        c.fill = GridBagConstraints.HORIZONTAL;
222
        this.bar.setStringPainted(true);
223
        this.add(this.bar, c);
224
 
225
        c.gridy++;
65 ilm 226
        c.gridx = 3;
227
        c.fill = GridBagConstraints.NONE;
228
        c.anchor = GridBagConstraints.EAST;
229
        c.gridwidth = 1;
230
        final JPanel actionPanel = new JPanel();
231
        actionPanel.add(this.valid);
232
        actionPanel.add(this.annul);
18 ilm 233
 
65 ilm 234
        this.add(actionPanel, c);
18 ilm 235
        checkValidity();
236
 
237
        this.valid.addActionListener(new ActionListener() {
238
            public void actionPerformed(ActionEvent e) {
239
                bar.setString(null);
240
                bar.setValue(1);
241
                valid.setEnabled(false);
242
                new Thread(new Runnable() {
243
                    public void run() {
244
                        int[] idS = ((SelectJournauxModel) tableJrnl.getModel()).getSelectedIds(tableJrnl.getSelectedRows());
132 ilm 245
                        if (checkAncienModele.isSelected()) {
65 ilm 246
                            JournauxSheet bSheet;
132 ilm 247
                            bSheet = new JournauxSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
248
                            final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), false, true);
249
                            SwingUtilities.invokeLater(new Runnable() {
250
                                public void run() {
251
                                    bar.setValue(2);
252
                                    generator.addGenerateListener(ImpressionJournauxPanel.this);
253
                                }
254
                            });
255
                        } else if (checkCentralMois.isSelected()) {
256
                            JournauxSheet bSheet;
18 ilm 257
                            bSheet = new JournauxMoisSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode);
65 ilm 258
                            final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), false, true);
259
                            SwingUtilities.invokeLater(new Runnable() {
260
                                public void run() {
261
                                    bar.setValue(2);
262
                                    generator.addGenerateListener(ImpressionJournauxPanel.this);
263
                                }
264
                            });
18 ilm 265
                        } else {
65 ilm 266
                            for (int i = 0; i < idS.length; i++) {
267
                                final JournauxSheetXML xmlSheet = new JournauxSheetXML(idS[i], dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
268
                                SwingUtilities.invokeLater(new Runnable() {
269
                                    public void run() {
270
                                        bar.setValue(2);
271
                                    }
272
                                });
273
                                try {
274
                                    xmlSheet.createDocument();
275
                                    xmlSheet.getOrCreatePDFDocumentFile(false, true);
276
                                    Gestion.openPDF(xmlSheet.getGeneratedPDFFile());
277
                                } catch (Exception exn) {
278
                                    ExceptionHandler.handle("Erreur lors de la création du journal !", exn);
279
                                }
280
 
281
                            }
282
                            SwingUtilities.invokeLater(new Runnable() {
283
                                public void run() {
284
                                    bar.setValue(3);
285
                                    bar.setString("Terminée");
286
                                    valid.setEnabled(true);
287
                                }
288
                            });
289
 
18 ilm 290
                        }
291
 
292
                    }
293
                }).start();
294
            }
295
        });
296
        this.annul.addActionListener(new ActionListener() {
297
            public void actionPerformed(ActionEvent e) {
298
                ((JFrame) SwingUtilities.getRoot(ImpressionJournauxPanel.this)).dispose();
299
            }
300
        });
301
 
302
        this.dateDeb.addValueListener(new PropertyChangeListener() {
303
            public void propertyChange(PropertyChangeEvent evt) {
304
                checkValidity();
305
                storeValue();
306
            }
307
        });
308
        this.dateEnd.addValueListener(new PropertyChangeListener() {
309
            public void propertyChange(PropertyChangeEvent evt) {
310
                checkValidity();
311
                storeValue();
312
            }
313
        });
314
        this.tableJrnl.addMouseListener(new MouseAdapter() {
315
 
316
            public void mouseReleased(MouseEvent e) {
317
                checkValidity();
318
            }
319
        });
320
        this.tableJrnl.addKeyListener(new KeyAdapter() {
321
            public void keyReleased(KeyEvent e) {
322
                checkValidity();
323
            }
324
        });
325
 
326
        SimpleDocumentListener doc = new SimpleDocumentListener() {
327
            @Override
328
            public void update(DocumentEvent e) {
329
 
330
                checkValidity();
331
            }
332
        };
333
 
334
        this.compteDeb.getDocument().addDocumentListener(doc);
335
        // Chargement des valeurs par défaut
336
 
337
        this.compteDeb.setText("1");
338
 
339
        this.compteEnd.getDocument().addDocumentListener(doc);
340
        // Chargement des valeurs par défaut
341
 
342
        this.compteEnd.setText("8");
343
 
344
    }
345
 
346
    private void checkValidity() {
347
 
348
        Date beginDate = this.dateDeb.getDate();
349
        Date endDate = this.dateEnd.getDate();
350
 
351
        if (beginDate == null || endDate == null) {
352
            this.valid.setEnabled(false);
353
        } else {
354
            if (beginDate.after(endDate)) {
355
                this.valid.setEnabled(false);
356
            } else {
357
                if (this.tableJrnl.getSelectedRows().length == 0) {
358
                    this.valid.setEnabled(false);
359
                } else {
360
                    this.valid.setEnabled(true);
361
                }
362
            }
363
        }
364
 
365
        if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
366
            this.valid.setEnabled(false);
367
        } else {
368
            if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
369
                this.valid.setEnabled(false);
370
            } else {
65 ilm 371
                if (beginDate == null || beginDate.after(endDate)) {
18 ilm 372
                    this.valid.setEnabled(false);
373
                }
374
            }
375
        }
376
    }
377
 
378
    private void storeValue() {
379
 
380
        // Set date debut
381
        Date d = this.dateDeb.getDate();
382
        if (d != null) {
383
            DefaultNXProps.getInstance().setProperty("JournauxDateDeb", String.valueOf(d.getTime()));
384
        }
385
 
386
        // Set date Fin
387
        Date dFin = this.dateEnd.getDate();
388
        if (dFin != null) {
389
            DefaultNXProps.getInstance().setProperty("JournauxDateEnd", String.valueOf(dFin.getTime()));
390
        }
391
 
392
        DefaultNXProps.getInstance().store();
393
    }
394
 
395
    public void taskEnd() {
396
        bar.setValue(3);
397
        bar.setString("Terminée");
398
        valid.setEnabled(true);
399
    }
400
}