OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | 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
 *
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;
72
    private JTextField compteDeb, compteEnd;
73
    private int mode = GrandLivreSheet.MODEALL;
74
    private JProgressBar bar = new JProgressBar(0, 3);
75
 
76
    public ImpressionJournauxPanel() {
77
        this.setLayout(new GridBagLayout());
78
        final GridBagConstraints c = new DefaultGridBagConstraints();
79
 
80
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
81
        SQLRow rowExercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON").getRow(rowSociete.getInt("ID_EXERCICE_COMMON"));
82
 
83
        // this.isValidated = false;
84
        this.dateDeb = new JDate();
85
        this.dateEnd = new JDate();
86
        this.tableJrnl = new JTable(new SelectJournauxModel());
87
 
65 ilm 88
        this.add(new JLabel("Période du", SwingConstants.RIGHT), c);
18 ilm 89
        c.gridx++;
90
        c.weightx = 1;
91
        this.add(this.dateDeb, c);
92
        // Chargement des valeurs par défaut
93
        String valueDateDeb = DefaultNXProps.getInstance().getStringProperty("JournauxDateDeb");
94
        if (valueDateDeb.trim().length() > 0) {
95
            Long l = new Long(valueDateDeb);
96
            this.dateDeb.setValue(new Date(l.longValue()));
97
        } else {
98
            this.dateDeb.setValue((Date) rowExercice.getObject("DATE_DEB"));
99
        }
100
 
101
        c.gridx++;
102
        c.weightx = 0;
103
        this.add(new JLabel("Au"), c);
104
        c.gridx++;
105
        c.weightx = 1;
106
        this.add(this.dateEnd, c);
107
 
108
        // Chargement des valeurs par défaut
109
        String valueDateEnd = DefaultNXProps.getInstance().getStringProperty("JournauxDateEnd");
110
        if (valueDateEnd.trim().length() > 0) {
111
            Long l = new Long(valueDateEnd);
112
            this.dateEnd.setValue(new Date(l.longValue()));
113
        } else {
114
            this.dateEnd.setValue((Date) rowExercice.getObject("DATE_FIN"));
115
        }
116
 
117
        // Compte
118
        this.compteDeb = new JTextField();
119
        this.compteEnd = new JTextField();
120
        c.gridy++;
121
        c.gridx = 0;
65 ilm 122
        this.add(new JLabel("Du compte ", SwingConstants.RIGHT), c);
18 ilm 123
        c.gridx++;
124
        c.weightx = 1;
125
        this.add(this.compteDeb, c);
126
        this.compteDeb.setText("1");
127
        this.compteEnd.setText("9");
128
 
129
        c.gridx++;
130
        c.weightx = 0;
131
        this.add(new JLabel("Au"), c);
132
        c.gridx++;
133
        c.weightx = 1;
134
        this.add(this.compteEnd, c);
135
 
136
        c.gridy++;
137
        c.gridx = 0;
138
        c.gridwidth = GridBagConstraints.REMAINDER;
139
 
140
        this.add(new TitledSeparator("Sélection des journaux"), c);
141
 
142
        c.gridy++;
143
        c.fill = GridBagConstraints.BOTH;
144
        c.weightx = 0;
145
        c.weighty = 1;
146
 
147
        JScrollPane scroll = new JScrollPane(this.tableJrnl);
148
        Dimension d;
149
        if (this.tableJrnl.getPreferredSize().height > 200) {
150
            d = new Dimension(scroll.getPreferredSize().width, 200);
151
        } else {
152
            d = new Dimension(scroll.getPreferredSize().width, this.tableJrnl.getPreferredSize().height + 30);
153
        }
154
        scroll.setPreferredSize(d);
155
 
156
        this.add(scroll, c);
157
 
158
        this.valid = new JButton("Valider");
159
        this.annul = new JButton("Fermer");
160
 
161
        // Radio mode
162
        JRadioButton radioAll = new JRadioButton(new AbstractAction("Toutes") {
163
            public void actionPerformed(ActionEvent e) {
164
                // TODO Auto-generated method stub
165
                mode = GrandLivreSheet.MODEALL;
166
            }
167
        });
168
 
169
        JRadioButton radioLettree = new JRadioButton(new AbstractAction("Lettrées") {
170
            public void actionPerformed(ActionEvent e) {
171
                // TODO Auto-generated method stub
172
                mode = GrandLivreSheet.MODELETTREE;
173
            }
174
        });
175
 
176
        JRadioButton radioNonLettree = new JRadioButton(new AbstractAction("Non lettrées") {
177
            public void actionPerformed(ActionEvent e) {
178
                // TODO Auto-generated method stub
179
                mode = GrandLivreSheet.MODENONLETTREE;
180
            }
181
        });
182
        JPanel panelMode = new JPanel();
183
        panelMode.add(radioAll);
184
        panelMode.add(radioLettree);
185
        panelMode.add(radioNonLettree);
186
 
187
        c.gridy++;
188
        c.gridx = 0;
189
        c.weightx = 1;
190
        c.gridwidth = 2;
191
        c.weighty = 0;
192
        c.fill = GridBagConstraints.NONE;
193
        ButtonGroup group = new ButtonGroup();
194
        group.add(radioAll);
195
        group.add(radioLettree);
196
        group.add(radioNonLettree);
197
        radioAll.setSelected(true);
198
        panelMode.setBorder(BorderFactory.createTitledBorder("Ecritures"));
199
        this.add(panelMode, c);
200
 
201
        // Centralisation par mois
202
        this.checkCentralMois = new JCheckBox("Centralisation par mois");
203
        c.gridx += 2;
204
        c.gridwidth = GridBagConstraints.REMAINDER;
205
        this.add(this.checkCentralMois, c);
206
 
207
        // Progress bar
208
        c.gridwidth = GridBagConstraints.REMAINDER;
209
        c.gridy++;
210
        c.gridx = 0;
211
        c.weightx = 1;
212
        c.weighty = 0;
213
        c.fill = GridBagConstraints.HORIZONTAL;
214
        this.bar.setStringPainted(true);
215
        this.add(this.bar, c);
216
 
217
        c.gridy++;
65 ilm 218
        c.gridx = 3;
219
        c.fill = GridBagConstraints.NONE;
220
        c.anchor = GridBagConstraints.EAST;
221
        c.gridwidth = 1;
222
        final JPanel actionPanel = new JPanel();
223
        actionPanel.add(this.valid);
224
        actionPanel.add(this.annul);
18 ilm 225
 
65 ilm 226
        this.add(actionPanel, c);
18 ilm 227
        checkValidity();
228
 
229
        this.valid.addActionListener(new ActionListener() {
230
            public void actionPerformed(ActionEvent e) {
231
                bar.setString(null);
232
                bar.setValue(1);
233
                valid.setEnabled(false);
234
                new Thread(new Runnable() {
235
                    public void run() {
236
                        int[] idS = ((SelectJournauxModel) tableJrnl.getModel()).getSelectedIds(tableJrnl.getSelectedRows());
237
                        if (checkCentralMois.isSelected()) {
65 ilm 238
                            JournauxSheet bSheet;
18 ilm 239
                            bSheet = new JournauxMoisSheet(idS, dateDeb.getDate(), dateEnd.getDate(), mode);
65 ilm 240
                            final SpreadSheetGeneratorCompta generator = new SpreadSheetGeneratorCompta(bSheet, "Journal_" + Calendar.getInstance().getTimeInMillis(), false, true);
241
                            SwingUtilities.invokeLater(new Runnable() {
242
                                public void run() {
243
                                    bar.setValue(2);
244
                                    generator.addGenerateListener(ImpressionJournauxPanel.this);
245
                                }
246
                            });
18 ilm 247
                        } else {
65 ilm 248
                            for (int i = 0; i < idS.length; i++) {
249
                                final JournauxSheetXML xmlSheet = new JournauxSheetXML(idS[i], dateDeb.getDate(), dateEnd.getDate(), mode, compteDeb.getText().trim(), compteEnd.getText().trim());
250
                                SwingUtilities.invokeLater(new Runnable() {
251
                                    public void run() {
252
                                        bar.setValue(2);
253
                                    }
254
                                });
255
                                try {
256
                                    xmlSheet.createDocument();
257
                                    xmlSheet.getOrCreatePDFDocumentFile(false, true);
258
                                    Gestion.openPDF(xmlSheet.getGeneratedPDFFile());
259
                                } catch (Exception exn) {
260
                                    ExceptionHandler.handle("Erreur lors de la création du journal !", exn);
261
                                }
262
 
263
                            }
264
                            SwingUtilities.invokeLater(new Runnable() {
265
                                public void run() {
266
                                    bar.setValue(3);
267
                                    bar.setString("Terminée");
268
                                    valid.setEnabled(true);
269
                                }
270
                            });
271
 
18 ilm 272
                        }
273
 
274
                    }
275
                }).start();
276
            }
277
        });
278
        this.annul.addActionListener(new ActionListener() {
279
            public void actionPerformed(ActionEvent e) {
280
                ((JFrame) SwingUtilities.getRoot(ImpressionJournauxPanel.this)).dispose();
281
            }
282
        });
283
 
284
        this.dateDeb.addValueListener(new PropertyChangeListener() {
285
            public void propertyChange(PropertyChangeEvent evt) {
286
                checkValidity();
287
                storeValue();
288
            }
289
        });
290
        this.dateEnd.addValueListener(new PropertyChangeListener() {
291
            public void propertyChange(PropertyChangeEvent evt) {
292
                checkValidity();
293
                storeValue();
294
            }
295
        });
296
        this.tableJrnl.addMouseListener(new MouseAdapter() {
297
 
298
            public void mouseReleased(MouseEvent e) {
299
                checkValidity();
300
            }
301
        });
302
        this.tableJrnl.addKeyListener(new KeyAdapter() {
303
            public void keyReleased(KeyEvent e) {
304
                checkValidity();
305
            }
306
        });
307
 
308
        SimpleDocumentListener doc = new SimpleDocumentListener() {
309
            @Override
310
            public void update(DocumentEvent e) {
311
 
312
                checkValidity();
313
            }
314
        };
315
 
316
        this.compteDeb.getDocument().addDocumentListener(doc);
317
        // Chargement des valeurs par défaut
318
 
319
        this.compteDeb.setText("1");
320
 
321
        this.compteEnd.getDocument().addDocumentListener(doc);
322
        // Chargement des valeurs par défaut
323
 
324
        this.compteEnd.setText("8");
325
 
326
    }
327
 
328
    private void checkValidity() {
329
 
330
        Date beginDate = this.dateDeb.getDate();
331
        Date endDate = this.dateEnd.getDate();
332
 
333
        if (beginDate == null || endDate == null) {
334
            this.valid.setEnabled(false);
335
        } else {
336
            if (beginDate.after(endDate)) {
337
                this.valid.setEnabled(false);
338
            } else {
339
                if (this.tableJrnl.getSelectedRows().length == 0) {
340
                    this.valid.setEnabled(false);
341
                } else {
342
                    this.valid.setEnabled(true);
343
                }
344
            }
345
        }
346
 
347
        if (this.compteDeb.getText().trim().length() == 0 || this.compteEnd.getText().trim().length() == 0) {
348
            this.valid.setEnabled(false);
349
        } else {
350
            if (this.compteDeb.getText().trim().compareToIgnoreCase(this.compteEnd.getText().trim()) > 0) {
351
                this.valid.setEnabled(false);
352
            } else {
65 ilm 353
                if (beginDate == null || beginDate.after(endDate)) {
18 ilm 354
                    this.valid.setEnabled(false);
355
                }
356
            }
357
        }
358
    }
359
 
360
    private void storeValue() {
361
 
362
        // Set date debut
363
        Date d = this.dateDeb.getDate();
364
        if (d != null) {
365
            DefaultNXProps.getInstance().setProperty("JournauxDateDeb", String.valueOf(d.getTime()));
366
        }
367
 
368
        // Set date Fin
369
        Date dFin = this.dateEnd.getDate();
370
        if (dFin != null) {
371
            DefaultNXProps.getInstance().setProperty("JournauxDateEnd", String.valueOf(dFin.getTime()));
372
        }
373
 
374
        DefaultNXProps.getInstance().store();
375
    }
376
 
377
    public void taskEnd() {
378
        bar.setValue(3);
379
        bar.setString("Terminée");
380
        valid.setEnabled(true);
381
    }
382
}