OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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;
182 ilm 19
import org.openconcerto.erp.core.finance.accounting.report.GrandLivrePDF;
18 ilm 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;
182 ilm 45
import java.io.File;
18 ilm 46
import java.util.Calendar;
47
import java.util.Date;
48
 
49
import javax.swing.AbstractAction;
50
import javax.swing.BorderFactory;
51
import javax.swing.ButtonGroup;
52
import javax.swing.JButton;
53
import javax.swing.JCheckBox;
54
import javax.swing.JFrame;
55
import javax.swing.JLabel;
56
import javax.swing.JPanel;
57
import javax.swing.JProgressBar;
58
import javax.swing.JRadioButton;
59
import javax.swing.JScrollPane;
60
import javax.swing.JTable;
61
import javax.swing.JTextField;
65 ilm 62
import javax.swing.SwingConstants;
18 ilm 63
import javax.swing.SwingUtilities;
64
import javax.swing.event.DocumentEvent;
65
 
66
public class ImpressionJournauxPanel extends JPanel implements SpreadSheetGeneratorListener {
67
 
68
    private final JDate dateDeb, dateEnd;
69
    private JTable tableJrnl;
70
    private JButton valid;
71
    private JButton annul;
72
    private JCheckBox checkCentralMois;
132 ilm 73
    private JCheckBox checkAncienModele;
182 ilm 74
    private JTextField compteDeb;
75
    private JTextField compteEnd;
76
    private int mode = GrandLivrePDF.MODEALL;
18 ilm 77
    private JProgressBar bar = new JProgressBar(0, 3);
78
 
79
    public ImpressionJournauxPanel() {
80
        this.setLayout(new GridBagLayout());
81
        final GridBagConstraints c = new DefaultGridBagConstraints();
82
 
83
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
84
        SQLRow rowExercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));
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) {
182 ilm 165
 
166
                ImpressionJournauxPanel.this.mode = GrandLivrePDF.MODEALL;
18 ilm 167
            }
168
        });
169
 
170
        JRadioButton radioLettree = new JRadioButton(new AbstractAction("Lettrées") {
171
            public void actionPerformed(ActionEvent e) {
182 ilm 172
 
173
                ImpressionJournauxPanel.this.mode = GrandLivrePDF.MODELETTREE;
18 ilm 174
            }
175
        });
176
 
177
        JRadioButton radioNonLettree = new JRadioButton(new AbstractAction("Non lettrées") {
178
            public void actionPerformed(ActionEvent e) {
182 ilm 179
 
180
                ImpressionJournauxPanel.this.mode = GrandLivrePDF.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
 
18 ilm 211
        // Progress bar
212
        c.gridwidth = GridBagConstraints.REMAINDER;
213
        c.gridy++;
214
        c.gridx = 0;
215
        c.weightx = 1;
216
        c.weighty = 0;
217
        c.fill = GridBagConstraints.HORIZONTAL;
218
        this.bar.setStringPainted(true);
219
        this.add(this.bar, c);
220
 
221
        c.gridy++;
65 ilm 222
        c.gridx = 3;
223
        c.fill = GridBagConstraints.NONE;
224
        c.anchor = GridBagConstraints.EAST;
225
        c.gridwidth = 1;
226
        final JPanel actionPanel = new JPanel();
227
        actionPanel.add(this.valid);
228
        actionPanel.add(this.annul);
18 ilm 229
 
65 ilm 230
        this.add(actionPanel, c);
18 ilm 231
        checkValidity();
232
 
233
        this.valid.addActionListener(new ActionListener() {
234
            public void actionPerformed(ActionEvent e) {
235
                bar.setString(null);
236
                bar.setValue(1);
237
                valid.setEnabled(false);
238
                new Thread(new Runnable() {
239
                    public void run() {
240
                        int[] idS = ((SelectJournauxModel) tableJrnl.getModel()).getSelectedIds(tableJrnl.getSelectedRows());
132 ilm 241
                        if (checkAncienModele.isSelected()) {
65 ilm 242
                            JournauxSheet bSheet;
132 ilm 243
                            bSheet = new JournauxSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
244
                            final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), false, true);
245
                            SwingUtilities.invokeLater(new Runnable() {
246
                                public void run() {
247
                                    bar.setValue(2);
248
                                    generator.addGenerateListener(ImpressionJournauxPanel.this);
249
                                }
250
                            });
251
                        } else if (checkCentralMois.isSelected()) {
252
                            JournauxSheet bSheet;
18 ilm 253
                            bSheet = new JournauxMoisSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode);
182 ilm 254
 
65 ilm 255
                            final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), false, true);
256
                            SwingUtilities.invokeLater(new Runnable() {
257
                                public void run() {
258
                                    bar.setValue(2);
259
                                    generator.addGenerateListener(ImpressionJournauxPanel.this);
260
                                }
261
                            });
18 ilm 262
                        } else {
65 ilm 263
                            for (int i = 0; i < idS.length; i++) {
182 ilm 264
 
65 ilm 265
                                final JournauxSheetXML xmlSheet = new JournauxSheetXML(idS[i], dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
182 ilm 266
 
65 ilm 267
                                SwingUtilities.invokeLater(new Runnable() {
268
                                    public void run() {
269
                                        bar.setValue(2);
270
                                    }
271
                                });
272
                                try {
273
                                    xmlSheet.createDocument();
274
                                    xmlSheet.getOrCreatePDFDocumentFile(false, true);
182 ilm 275
                                    final File generatedPDFFile = xmlSheet.getGeneratedPDFFile();
276
                                    if (generatedPDFFile != null)
277
                                        Gestion.openPDF(generatedPDFFile);
65 ilm 278
                                } catch (Exception exn) {
279
                                    ExceptionHandler.handle("Erreur lors de la création du journal !", exn);
280
                                }
281
 
282
                            }
283
                            SwingUtilities.invokeLater(new Runnable() {
284
                                public void run() {
285
                                    bar.setValue(3);
286
                                    bar.setString("Terminée");
287
                                    valid.setEnabled(true);
288
                                }
289
                            });
290
 
18 ilm 291
                        }
292
 
293
                    }
294
                }).start();
295
            }
296
        });
297
        this.annul.addActionListener(new ActionListener() {
298
            public void actionPerformed(ActionEvent e) {
299
                ((JFrame) SwingUtilities.getRoot(ImpressionJournauxPanel.this)).dispose();
300
            }
301
        });
302
 
303
        this.dateDeb.addValueListener(new PropertyChangeListener() {
304
            public void propertyChange(PropertyChangeEvent evt) {
305
                checkValidity();
306
                storeValue();
307
            }
308
        });
309
        this.dateEnd.addValueListener(new PropertyChangeListener() {
310
            public void propertyChange(PropertyChangeEvent evt) {
311
                checkValidity();
312
                storeValue();
313
            }
314
        });
315
        this.tableJrnl.addMouseListener(new MouseAdapter() {
316
 
317
            public void mouseReleased(MouseEvent e) {
318
                checkValidity();
319
            }
320
        });
321
        this.tableJrnl.addKeyListener(new KeyAdapter() {
322
            public void keyReleased(KeyEvent e) {
323
                checkValidity();
324
            }
325
        });
326
 
327
        SimpleDocumentListener doc = new SimpleDocumentListener() {
328
            @Override
329
            public void update(DocumentEvent e) {
330
 
331
                checkValidity();
332
            }
333
        };
334
 
335
        this.compteDeb.getDocument().addDocumentListener(doc);
336
        // Chargement des valeurs par défaut
337
 
338
        this.compteDeb.setText("1");
339
 
340
        this.compteEnd.getDocument().addDocumentListener(doc);
341
        // Chargement des valeurs par défaut
342
 
343
        this.compteEnd.setText("8");
344
 
345
    }
346
 
347
    private void checkValidity() {
348
 
349
        Date beginDate = this.dateDeb.getDate();
350
        Date endDate = this.dateEnd.getDate();
351
 
352
        if (beginDate == null || endDate == null) {
353
            this.valid.setEnabled(false);
354
        } else {
355
            if (beginDate.after(endDate)) {
356
                this.valid.setEnabled(false);
357
            } else {
358
                if (this.tableJrnl.getSelectedRows().length == 0) {
359
                    this.valid.setEnabled(false);
360
                } else {
361
                    this.valid.setEnabled(true);
362
                }
363
            }
364
        }
365
 
366
        if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
367
            this.valid.setEnabled(false);
368
        } else {
369
            if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
370
                this.valid.setEnabled(false);
371
            } else {
65 ilm 372
                if (beginDate == null || beginDate.after(endDate)) {
18 ilm 373
                    this.valid.setEnabled(false);
374
                }
375
            }
376
        }
377
    }
378
 
379
    private void storeValue() {
380
 
381
        // Set date debut
382
        Date d = this.dateDeb.getDate();
383
        if (d != null) {
384
            DefaultNXProps.getInstance().setProperty("JournauxDateDeb", String.valueOf(d.getTime()));
385
        }
386
 
387
        // Set date Fin
388
        Date dFin = this.dateEnd.getDate();
389
        if (dFin != null) {
390
            DefaultNXProps.getInstance().setProperty("JournauxDateEnd", String.valueOf(dFin.getTime()));
391
        }
392
 
393
        DefaultNXProps.getInstance().store();
394
    }
395
 
396
    public void taskEnd() {
397
        bar.setValue(3);
398
        bar.setString("Terminée");
399
        valid.setEnabled(true);
400
    }
401
}