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 | 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;
61 ilm 17
import org.openconcerto.erp.config.Gestion;
182 ilm 18
import org.openconcerto.erp.core.finance.accounting.report.GrandLivrePDF;
61 ilm 19
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheetXML;
18 ilm 20
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorListener;
21
import org.openconcerto.erp.preferences.DefaultNXProps;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.model.SQLRow;
19 ilm 24
import org.openconcerto.sql.sqlobject.ElementComboBox;
18 ilm 25
import org.openconcerto.ui.DefaultGridBagConstraints;
26
import org.openconcerto.ui.JDate;
41 ilm 27
import org.openconcerto.ui.JLabelBold;
182 ilm 28
import org.openconcerto.utils.ExceptionHandler;
29
import org.openconcerto.utils.FileUtils;
18 ilm 30
 
41 ilm 31
import java.awt.FlowLayout;
18 ilm 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.beans.PropertyChangeEvent;
37
import java.beans.PropertyChangeListener;
182 ilm 38
import java.io.File;
39
import java.io.IOException;
40
import java.text.SimpleDateFormat;
18 ilm 41
import java.util.Date;
42
 
43
import javax.swing.AbstractAction;
44
import javax.swing.ButtonGroup;
45
import javax.swing.JButton;
46
import javax.swing.JCheckBox;
182 ilm 47
import javax.swing.JFileChooser;
18 ilm 48
import javax.swing.JFrame;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JProgressBar;
52
import javax.swing.JRadioButton;
53
import javax.swing.JTextField;
41 ilm 54
import javax.swing.SwingConstants;
18 ilm 55
import javax.swing.SwingUtilities;
56
import javax.swing.event.DocumentEvent;
57
import javax.swing.event.DocumentListener;
58
 
59
public class ImpressionGrandLivrePanel extends JPanel implements SpreadSheetGeneratorListener {
60
 
182 ilm 61
    private final JDate dateDeb;
62
    private final JDate dateEnd;
63
    private JButton validPDF;
64
    private JButton validODS;
18 ilm 65
    private JButton annul;
66
    private JCheckBox checkImpr;
67
    private JCheckBox checkVisu;
68
    private JTextField compteDeb, compteEnd;
182 ilm 69
    private int mode = GrandLivrePDF.MODEALL;
18 ilm 70
    private JProgressBar bar = new JProgressBar(0, 3);
71
 
72
    public ImpressionGrandLivrePanel() {
73
        this.setLayout(new GridBagLayout());
74
        final GridBagConstraints c = new DefaultGridBagConstraints();
75
 
182 ilm 76
        final ComptaPropsConfiguration comptaPropsConfiguration = (ComptaPropsConfiguration) Configuration.getInstance();
77
        SQLRow rowSociete = comptaPropsConfiguration.getRowSociete();
78
        SQLRow rowExercice = comptaPropsConfiguration.getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));
18 ilm 79
 
80
        this.dateDeb = new JDate();
81
        this.dateEnd = new JDate();
82
 
83
        // Période
41 ilm 84
        c.weightx = 0;
85
        this.add(new JLabel("Période du", SwingConstants.RIGHT), c);
18 ilm 86
        c.gridx++;
182 ilm 87
        c.weightx = 0;
18 ilm 88
        this.add(this.dateDeb, c);
89
        // Chargement des valeurs par défaut
90
        String valueDateDeb = DefaultNXProps.getInstance().getStringProperty("GrandLivreDateDeb");
91
        if (valueDateDeb.trim().length() > 0) {
92
            Long l = new Long(valueDateDeb);
93
            this.dateDeb.setValue(new Date(l.longValue()));
94
        } else {
95
            this.dateDeb.setValue((Date) rowExercice.getObject("DATE_DEB"));
96
        }
97
 
98
        c.gridx++;
99
        c.weightx = 0;
41 ilm 100
        this.add(new JLabel("au"), c);
18 ilm 101
        c.gridx++;
41 ilm 102
 
103
        c.fill = GridBagConstraints.NONE;
18 ilm 104
        this.add(this.dateEnd, c);
105
        // Chargement des valeurs par défaut
106
        String valueDateEnd = DefaultNXProps.getInstance().getStringProperty("GrandLivreDateEnd");
107
        if (valueDateEnd.trim().length() > 0) {
108
            Long l = new Long(valueDateEnd);
109
            this.dateEnd.setValue(new Date(l.longValue()));
110
        } else {
111
            this.dateEnd.setValue((Date) rowExercice.getObject("DATE_FIN"));
112
        }
113
 
114
        // Compte
41 ilm 115
        this.compteDeb = new JTextField(8);
116
        this.compteEnd = new JTextField(8);
18 ilm 117
        c.gridy++;
118
        c.gridx = 0;
119
        c.weightx = 0;
41 ilm 120
        c.fill = GridBagConstraints.HORIZONTAL;
121
        this.add(new JLabel("Du compte", SwingConstants.RIGHT), c);
18 ilm 122
        c.gridx++;
41 ilm 123
        c.gridwidth = 3;
124
        c.fill = GridBagConstraints.NONE;
125
        JPanel pCompte = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
126
        pCompte.add(this.compteDeb);
127
        pCompte.add(new JLabel(" au compte "));
128
        pCompte.add(this.compteEnd);
18 ilm 129
 
41 ilm 130
        this.add(pCompte, c);
131
 
132
        c.gridx = 0;
133
        c.gridwidth = 4;
134
        c.gridy++;
135
        this.add(new JLabelBold("Options"), c);
18 ilm 136
        final JCheckBox boxCumulsAnts = new JCheckBox("Cumuls antérieurs");
137
        c.gridx = 0;
138
        c.gridy++;
139
        c.gridwidth = 2;
140
        this.add(boxCumulsAnts, c);
141
        boxCumulsAnts.setSelected(true);
142
 
143
        final JCheckBox boxCentralClient = new JCheckBox("Centralisation comptes clients");
144
        c.gridx += 2;
145
        c.gridwidth = 2;
146
        this.add(boxCentralClient, c);
147
 
148
        final JCheckBox boxCompteSolde = new JCheckBox("Inclure les comptes soldés");
149
        c.gridx = 0;
150
        c.gridy++;
151
        c.gridwidth = 2;
152
        this.add(boxCompteSolde, c);
153
        boxCompteSolde.setSelected(true);
154
 
155
        final JCheckBox boxCentralFourn = new JCheckBox("Centralisation comptes fournisseurs");
156
        c.gridx += 2;
157
        c.gridwidth = 2;
158
        this.add(boxCentralFourn, c);
159
 
19 ilm 160
        // Journal à exclure
161
        c.gridy++;
162
        c.gridx = 0;
41 ilm 163
        c.gridwidth = 1;
164
        this.add(new JLabel("Exclure le journal", SwingConstants.RIGHT), c);
19 ilm 165
        c.gridx++;
41 ilm 166
        c.gridwidth = 3;
19 ilm 167
        final ElementComboBox comboJrnl = new ElementComboBox(true);
168
        comboJrnl.init(Configuration.getInstance().getDirectory().getElement("JOURNAL"));
169
        this.add(comboJrnl, c);
170
 
18 ilm 171
        // Radio mode
172
        JRadioButton radioAll = new JRadioButton(new AbstractAction("Toutes") {
173
            public void actionPerformed(ActionEvent e) {
182 ilm 174
                mode = GrandLivrePDF.MODEALL;
18 ilm 175
            }
176
        });
177
 
178
        JRadioButton radioLettree = new JRadioButton(new AbstractAction("Lettrées") {
179
            public void actionPerformed(ActionEvent e) {
182 ilm 180
                mode = GrandLivrePDF.MODELETTREE;
18 ilm 181
            }
182
        });
183
 
184
        JRadioButton radioNonLettree = new JRadioButton(new AbstractAction("Non lettrées") {
185
            public void actionPerformed(ActionEvent e) {
182 ilm 186
                mode = GrandLivrePDF.MODENONLETTREE_ALL;
18 ilm 187
            }
188
        });
41 ilm 189
 
132 ilm 190
        JRadioButton radioNonLettreePeridod = new JRadioButton(new AbstractAction("Non lettrées sur la période") {
191
            public void actionPerformed(ActionEvent e) {
182 ilm 192
                mode = GrandLivrePDF.MODENONLETTREE_PERIODE;
132 ilm 193
            }
194
        });
195
 
41 ilm 196
        c.gridy++;
197
        c.gridx = 0;
198
        c.weightx = 0;
199
        c.gridwidth = GridBagConstraints.REMAINDER;
200
 
201
        this.add(new JLabelBold("Ecritures à inclure"), c);
18 ilm 202
        JPanel panelMode = new JPanel();
203
        panelMode.add(radioAll);
204
        panelMode.add(radioLettree);
205
        panelMode.add(radioNonLettree);
132 ilm 206
        panelMode.add(radioNonLettreePeridod);
18 ilm 207
 
208
        c.gridy++;
209
        c.gridx = 0;
210
        c.weightx = 0;
211
        c.gridwidth = GridBagConstraints.REMAINDER;
212
        c.weighty = 0;
213
        c.fill = GridBagConstraints.NONE;
214
        ButtonGroup group = new ButtonGroup();
215
        group.add(radioAll);
216
        group.add(radioLettree);
217
        group.add(radioNonLettree);
132 ilm 218
        group.add(radioNonLettreePeridod);
18 ilm 219
        radioAll.setSelected(true);
41 ilm 220
 
18 ilm 221
        this.add(panelMode, c);
222
 
223
        // Progress bar
224
        c.gridwidth = GridBagConstraints.REMAINDER;
225
        c.gridy++;
226
        c.gridx = 0;
227
        c.weighty = 0;
228
        c.fill = GridBagConstraints.HORIZONTAL;
41 ilm 229
 
230
        this.add(new JLabelBold("Progression de la creation du grand livre"), c);
231
        c.gridy++;
18 ilm 232
        this.bar.setStringPainted(true);
233
        this.add(this.bar, c);
234
 
182 ilm 235
        this.validPDF = new JButton("PDF");
236
        this.validODS = new JButton("Tableur");
237
 
18 ilm 238
        this.annul = new JButton("Fermer");
239
 
182 ilm 240
        // OK, Cancel
41 ilm 241
 
242
        c.gridx = 0;
18 ilm 243
        c.gridy++;
41 ilm 244
        c.gridwidth = 4;
245
        c.weightx = 1;
246
        c.weighty = 1;
247
        c.fill = GridBagConstraints.NONE;
248
        c.anchor = GridBagConstraints.SOUTHEAST;
249
 
250
        JPanel panelOkCancel = new JPanel();
182 ilm 251
        panelOkCancel.add(this.validODS);
252
        panelOkCancel.add(this.validPDF);
253
 
41 ilm 254
        panelOkCancel.add(this.annul);
255
        this.add(panelOkCancel, c);
256
        this.checkValidity();
182 ilm 257
        this.validPDF.addActionListener(new ActionListener() {
18 ilm 258
            public void actionPerformed(ActionEvent e) {
182 ilm 259
                JFileChooser chooser = new JFileChooser();
260
                SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
261
                chooser.setSelectedFile(new File("Grand livre " + df.format(new Date()) + ".pdf"));
262
                int r = chooser.showSaveDialog(ImpressionGrandLivrePanel.this);
263
                if (r == JFileChooser.APPROVE_OPTION) {
264
                    validPDF.setEnabled(false);
265
                    validODS.setEnabled(false);
266
                    bar.setString(null);
267
                    bar.setValue(0);
268
                    // un fichier a été choisi (sortie par OK)
269
                    // nom du fichier choisi
270
                    final File file = chooser.getSelectedFile();
271
                    new Thread(new Runnable() {
272
                        public void run() {
273
                            try {
274
                                final GrandLivrePDF bSheet = new GrandLivrePDF(comptaPropsConfiguration, dateDeb.getDate(), dateEnd.getDate(), compteDeb.getText().trim(), compteEnd.getText().trim(),
275
                                        mode, boxCumulsAnts.isSelected(), !boxCompteSolde.isSelected(), boxCentralClient.isSelected(), boxCentralFourn.isSelected(), comboJrnl.getSelectedId());
276
                                bar.setValue(1);
277
                                bSheet.getGeneratedPDFFile(file);
278
                                bar.setValue(2);
279
                                SwingUtilities.invokeLater(new Runnable() {
280
 
281
                                    @Override
282
                                    public void run() {
283
                                        Gestion.openPDF(file);
284
                                    }
285
                                });
286
 
287
                            } catch (Exception exn) {
288
                                ExceptionHandler.handle("Erreur de génération du grand livre", exn);
289
                                exn.printStackTrace();
290
                            } finally {
291
                                taskEnd();
292
                            }
293
                        }
294
                    }).start();
295
                }
296
 
297
            }
298
        });
299
        this.validODS.addActionListener(new ActionListener() {
300
            public void actionPerformed(ActionEvent e) {
301
 
302
                validPDF.setEnabled(false);
303
                validODS.setEnabled(false);
18 ilm 304
                bar.setString(null);
182 ilm 305
                bar.setValue(0);
306
                // un fichier a été choisi (sortie par OK)
307
                // nom du fichier choisi
18 ilm 308
                new Thread(new Runnable() {
309
                    public void run() {
61 ilm 310
                        try {
182 ilm 311
                            final GrandLivreSheetXML bSheet = new GrandLivreSheetXML(dateDeb.getDate(), dateEnd.getDate(), compteDeb.getText().trim(), compteEnd.getText().trim(), mode,
312
                                    boxCumulsAnts.isSelected(), !boxCompteSolde.isSelected(), boxCentralClient.isSelected(), boxCentralFourn.isSelected(), comboJrnl.getSelectedId());
313
                            bar.setValue(1);
314
                            final File file = bSheet.getOrCreateDocumentFile();
315
                            bar.setValue(2);
316
                            SwingUtilities.invokeLater(new Runnable() {
61 ilm 317
 
182 ilm 318
                                @Override
319
                                public void run() {
320
                                    try {
321
                                        FileUtils.openFile(file);
322
                                    } catch (IOException e) {
323
                                        ExceptionHandler.handle("Erreur lors de l'ouverture du grand livre.", e);
61 ilm 324
                                    }
41 ilm 325
                                }
182 ilm 326
                            });
61 ilm 327
 
182 ilm 328
                        } catch (Exception exn) {
329
                            ExceptionHandler.handle("Erreur de génération du grand livre", exn);
61 ilm 330
                            exn.printStackTrace();
331
                        } finally {
332
                            taskEnd();
41 ilm 333
                        }
18 ilm 334
                    }
335
                }).start();
182 ilm 336
            }
18 ilm 337
 
338
        });
339
        this.annul.addActionListener(new ActionListener() {
182 ilm 340
 
18 ilm 341
            public void actionPerformed(ActionEvent e) {
342
                ((JFrame) SwingUtilities.getRoot(ImpressionGrandLivrePanel.this)).dispose();
343
            }
344
        });
345
 
346
        this.dateDeb.addValueListener(new PropertyChangeListener() {
347
            public void propertyChange(PropertyChangeEvent evt) {
348
                checkValidity();
349
                storeValue();
350
            }
351
        });
352
        this.dateEnd.addValueListener(new PropertyChangeListener() {
353
            public void propertyChange(PropertyChangeEvent evt) {
354
                checkValidity();
355
                storeValue();
356
            }
357
        });
358
 
359
        DocumentListener d = new DocumentListener() {
360
 
361
            public void insertUpdate(DocumentEvent e) {
362
                checkValidity();
363
            }
364
 
365
            public void removeUpdate(DocumentEvent e) {
366
                checkValidity();
367
            }
368
 
369
            public void changedUpdate(DocumentEvent e) {
370
                checkValidity();
371
            }
372
        };
373
 
374
        this.compteDeb.getDocument().addDocumentListener(d);
375
        // Chargement des valeurs par défaut
376
        String valueCompteDeb = DefaultNXProps.getInstance().getStringProperty("GrandLivreCompteDeb");
377
        if (valueCompteDeb.trim().length() > 0) {
378
            this.compteDeb.setText(valueCompteDeb);
379
        } else {
380
            this.compteDeb.setText("1");
381
        }
382
 
383
        this.compteEnd.getDocument().addDocumentListener(d);
384
        // Chargement des valeurs par défaut
385
        String valueCompteEnd = DefaultNXProps.getInstance().getStringProperty("GrandLivreCompteEnd");
386
        if (valueCompteEnd.trim().length() > 0) {
387
            this.compteEnd.setText(valueCompteEnd);
388
        } else {
389
            this.compteEnd.setText("8");
390
        }
391
    }
392
 
393
    private void checkValidity() {
394
 
395
        Date beginDate = this.dateDeb.getDate();
396
        Date endDate = this.dateEnd.getDate();
397
 
182 ilm 398
        this.validODS.setEnabled(true);
399
        this.validPDF.setEnabled(true);
18 ilm 400
        if (beginDate == null || endDate == null) {
182 ilm 401
            this.validODS.setEnabled(false);
402
            this.validPDF.setEnabled(false);
18 ilm 403
        } else {
404
            if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
182 ilm 405
                this.validODS.setEnabled(false);
406
                this.validPDF.setEnabled(false);
18 ilm 407
            } else {
408
                if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
182 ilm 409
                    this.validODS.setEnabled(false);
410
                    this.validPDF.setEnabled(false);
18 ilm 411
                } else {
412
                    if (beginDate.after(endDate)) {
182 ilm 413
                        this.validODS.setEnabled(false);
414
                        this.validPDF.setEnabled(false);
18 ilm 415
                    }
416
                }
417
            }
418
        }
419
    }
420
 
421
    private void storeValue() {
422
 
423
        // Set date debut
424
        Date d = this.dateDeb.getDate();
425
        if (d != null) {
426
            DefaultNXProps.getInstance().setProperty("GrandLivreDateDeb", String.valueOf(d.getTime()));
427
        }
428
 
429
        // Set date Fin
430
        Date dFin = this.dateEnd.getDate();
431
        if (dFin != null) {
432
            DefaultNXProps.getInstance().setProperty("GrandLivreDateEnd", String.valueOf(dFin.getTime()));
433
        }
434
 
435
        DefaultNXProps.getInstance().setProperty("GrandLivreCompteDeb", this.compteDeb.getText());
436
        DefaultNXProps.getInstance().setProperty("GrandLivreCompteEnd", this.compteEnd.getText());
437
 
438
        DefaultNXProps.getInstance().store();
439
    }
440
 
441
    public void taskEnd() {
442
        bar.setValue(3);
443
        bar.setString("Terminée");
182 ilm 444
        validPDF.setEnabled(true);
445
        validODS.setEnabled(true);
18 ilm 446
    }
447
}