OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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;
17
import org.openconcerto.erp.core.finance.accounting.action.ImpressionJournauxAction;
18
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
19
import org.openconcerto.erp.element.objet.Journal;
20
import org.openconcerto.erp.model.LoadingTableListener;
21
import org.openconcerto.erp.rights.ComptaUserRight;
22
import org.openconcerto.sql.Configuration;
19 ilm 23
import org.openconcerto.sql.element.SQLElement;
18 ilm 24
import org.openconcerto.sql.model.SQLBackgroundTableCache;
25
import org.openconcerto.sql.model.SQLBase;
26
import org.openconcerto.sql.model.SQLRow;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
144 ilm 29
import org.openconcerto.sql.users.rights.UserRightsManager;
65 ilm 30
import org.openconcerto.ui.DefaultGridBagConstraints;
18 ilm 31
import org.openconcerto.utils.GestionDevise;
32
 
33
import java.awt.Dimension;
34
import java.awt.GridBagConstraints;
35
import java.awt.GridBagLayout;
36
import java.awt.Insets;
37
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionListener;
39
import java.awt.event.MouseAdapter;
40
import java.awt.event.MouseEvent;
41
import java.text.DateFormat;
42
import java.text.SimpleDateFormat;
43
import java.util.Calendar;
44
import java.util.Date;
45
import java.util.List;
46
import java.util.concurrent.ExecutionException;
47
 
48
import javax.swing.AbstractAction;
49
import javax.swing.BorderFactory;
50
import javax.swing.JButton;
51
import javax.swing.JFrame;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
54
import javax.swing.JPopupMenu;
55
import javax.swing.JScrollPane;
56
import javax.swing.JTabbedPane;
57
import javax.swing.SwingConstants;
58
import javax.swing.SwingUtilities;
59
import javax.swing.SwingWorker;
60
import javax.swing.event.EventListenerList;
61
 
62
import org.apache.commons.dbutils.handlers.ArrayListHandler;
63
 
64
public class EtatJournauxPanel extends JPanel {
65
 
66
    private JTabbedPane tabbedJournaux;
67
    private EventListenerList loadingListener = new EventListenerList();
144 ilm 68
    private final DateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
18 ilm 69
    private static final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
70
    private static final String baseName = ((ComptaPropsConfiguration) Configuration.getInstance()).getSocieteBaseName();
71
 
72
    public EtatJournauxPanel() {
73
        super();
74
 
75
        this.tabbedJournaux = new JTabbedPane();
76
 
77
        this.setLayout(new GridBagLayout());
78
 
65 ilm 79
        final GridBagConstraints c = new DefaultGridBagConstraints();
18 ilm 80
        c.fill = GridBagConstraints.BOTH;
81
        c.anchor = GridBagConstraints.NORTHWEST;
82
        c.gridwidth = 2;
83
        c.gridheight = 1;
84
        c.weightx = 1;
85
        c.weighty = 1;
86
 
87
        this.add(this.tabbedJournaux, c);
88
 
89
        JButton buttonImpression = new JButton("Impression");
90
        JButton buttonClose = new JButton("Fermer");
91
        c.gridx = 0;
92
        c.gridy++;
93
        c.weightx = 1;
94
        c.weighty = 0;
95
        c.gridwidth = 1;
96
        c.fill = GridBagConstraints.NONE;
97
        c.anchor = GridBagConstraints.EAST;
98
        this.add(buttonImpression, c);
99
        c.gridx++;
100
        c.weightx = 0;
101
        this.add(buttonClose, c);
102
 
103
        buttonClose.addActionListener(new ActionListener() {
104
            public void actionPerformed(ActionEvent e) {
105
                ((JFrame) SwingUtilities.getRoot(EtatJournauxPanel.this)).dispose();
106
            };
107
        });
108
        buttonImpression.addActionListener(new ImpressionJournauxAction());
109
    }
110
 
111
    public void loadAsynchronous() {
112
 
113
        // On recupere les differents journaux
114
        SQLTable journalTable = base.getTable("JOURNAL");
115
 
116
        List<SQLRow> liste = SQLBackgroundTableCache.getInstance().getCacheForTable(journalTable).getRows();
117
        for (int k = 0; k < liste.size(); k++) {
118
            SQLRow row = liste.get(k);
119
            fireIsLoading(true);
120
            final Journal jrnlTmp = new Journal(row.getID(), row.getString("NOM"), row.getString("CODE"));
121
            new SwingWorker<JPanel, Object>() {
122
                @Override
123
                protected JPanel doInBackground() throws Exception {
124
 
125
                    final JPanel initJournalPanel = initJournalPanel(jrnlTmp);
126
                    return initJournalPanel;
127
                }
128
 
129
                @Override
130
                protected void done() {
131
                    JPanel initJournalPanel;
132
                    try {
133
                        initJournalPanel = get();
134
 
135
                        initJournalPanel.setOpaque(false);
136
                        JScrollPane scroll = new JScrollPane(initJournalPanel);
137
                        scroll.setBorder(null);
138
                        scroll.setOpaque(false);
139
                        scroll.getViewport().setOpaque(false);
140
                        EtatJournauxPanel.this.tabbedJournaux.addTab(jrnlTmp.getNom(), scroll);
141
                    } catch (InterruptedException e) {
142
                        // TODO Auto-generated catch block
143
                        e.printStackTrace();
144
                    } catch (ExecutionException e) {
145
                        // TODO Auto-generated catch block
146
                        e.printStackTrace();
147
                    }
148
                    fireIsLoading(false);
149
                    super.done();
150
                }
151
            }.execute();
152
 
153
        }
154
    }
155
 
156
    /*
157
     * Crée un panel destiné à l'onglet de jrnl
158
     */
159
    private JPanel initJournalPanel(final Journal jrnl) {
160
 
161
        final JPanel panelTmp = new JPanel();
162
        long totalDebitJournal = 0;
163
        long totalCreditJournal = 0;
164
 
165
        panelTmp.setLayout(new GridBagLayout());
166
 
167
        final GridBagConstraints c = new GridBagConstraints();
168
        c.insets = new Insets(2, 2, 1, 2);
169
        c.fill = GridBagConstraints.HORIZONTAL;
170
        c.anchor = GridBagConstraints.NORTHWEST;
171
        c.gridx = 0;
172
        c.gridy = 0;
173
        c.gridwidth = 1;
174
        c.gridheight = 1;
175
        c.weightx = 1;
176
        c.weighty = 0;
177
 
178
        String req = "SELECT DISTINCT EXTRACT(YEAR FROM \"" + baseName + "\".\"ECRITURE\".\"DATE\"), " + "EXTRACT(MONTH FROM \"" + baseName + "\".\"ECRITURE\".\"DATE\")," + " SUM(\"" + baseName
179
                + "\".\"ECRITURE\".\"DEBIT\"), " + "SUM(\"" + baseName + "\".\"ECRITURE\".\"CREDIT\")" + " FROM \"" + baseName + "\".\"ECRITURE\" " + "WHERE (\"" + baseName
180
                + "\".\"ECRITURE\".\"ID\" != 1) " + "AND ((\"" + baseName + "\".\"ECRITURE\".\"ARCHIVE\" = 0) " + "AND (\"" + baseName + "\".\"ECRITURE\".\"ID_JOURNAL\" = " + jrnl.getId() + ")) "
181
                + "GROUP BY EXTRACT(YEAR FROM \"" + baseName + "\".\"ECRITURE\".\"DATE\"), " + "EXTRACT(MONTH FROM \"" + baseName + "\".\"ECRITURE\".\"DATE\") " + "ORDER BY EXTRACT(YEAR FROM \""
182
                + baseName + "\".\"ECRITURE\".\"DATE\"), " + "EXTRACT(MONTH FROM \"" + baseName + "\".\"ECRITURE\".\"DATE\")";
183
        System.out.println(req);
184
 
185
        Object ob = base.getDataSource().execute(req, new ArrayListHandler());
186
 
187
        List myList = (List) ob;
188
 
189
        // System.err.println("TEST DATE " + t);
190
 
191
        if (myList.size() != 0) {
192
 
193
            for (int i = 0; i < myList.size(); i++) {
194
 
195
                Object[] objTmp = (Object[]) myList.get(i);
196
 
197
                Calendar cal = Calendar.getInstance();
198
                cal.set(Calendar.DATE, 1);
199
                int month = (new Double(objTmp[1].toString()).intValue() - 1);
200
                System.err.println(jrnl.getNom() + " SET MONTH " + month);
201
                cal.set(Calendar.MONTH, month);
202
                System.err.println(month + " = " + cal.getTime());
203
 
204
                cal.set(Calendar.YEAR, new Double(objTmp[0].toString()).intValue());
205
 
206
                long debitMois = ((Number) objTmp[2]).longValue();
207
                long creditMois = ((Number) objTmp[3]).longValue();
208
 
209
                c.gridwidth = GridBagConstraints.REMAINDER;
210
                final JPanel creerJournalMoisPanel = creerJournalMoisPanel(cal.getTime(), debitMois, creditMois, jrnl);
211
                creerJournalMoisPanel.setOpaque(false);
212
                panelTmp.add(creerJournalMoisPanel, c);
213
 
214
                c.gridy++;
215
 
216
                totalDebitJournal += debitMois;
217
                totalCreditJournal += creditMois;
218
            }
219
        }
220
        c.gridx = 0;
221
        c.weighty = 1;
222
        c.gridwidth = 1;
223
        final JLabel label = new JLabel("Journal " + jrnl.getNom());
224
        label.setOpaque(false);
225
        panelTmp.add(label, c);
226
        c.gridx = GridBagConstraints.RELATIVE;
227
        panelTmp.add(new JLabel(" Total débit : " + GestionDevise.currencyToString(totalDebitJournal)), c);
228
        panelTmp.add(new JLabel(" Total crédit : " + GestionDevise.currencyToString(totalCreditJournal)), c);
229
 
230
        return panelTmp;
231
    }
232
 
233
    /*
234
     * Panel du mois d'un journal
235
     */
236
    private JPanel creerJournalMoisPanel(final Date date, long debit, long credit, final Journal jrnl) {
237
 
238
        final JPanel panelMoisCompte = new JPanel();
239
        panelMoisCompte.setLayout(new GridBagLayout());
240
        final GridBagConstraints c = new GridBagConstraints();
241
        c.insets = new Insets(2, 2, 1, 2);
242
        c.fill = GridBagConstraints.HORIZONTAL;
243
        c.anchor = GridBagConstraints.NORTHWEST;
244
        c.gridx = 0;
245
        c.gridy = 0;
246
        c.gridwidth = 1;
247
        c.gridheight = 1;
248
        c.weightx = 1;
249
        c.weighty = 0;
250
 
251
        panelMoisCompte.setBorder(BorderFactory.createTitledBorder(dateFormat.format(date)));
252
 
253
        // Date du mois
254
        panelMoisCompte.add(new JLabel(dateFormat.format(date)), c);
255
 
256
        // Totaux du mois
257
        c.gridx++;
258
        panelMoisCompte.add(new JLabel(" débit : " + GestionDevise.currencyToString(debit)), c);
259
        c.gridx++;
260
        panelMoisCompte.add(new JLabel(" crédit : " + GestionDevise.currencyToString(credit)), c);
261
 
262
        // Bouton détails
263
        JButton boutonShow = new JButton("+/-");
264
        boutonShow.setOpaque(false);
265
        boutonShow.setHorizontalAlignment(SwingConstants.LEFT);
266
 
267
        c.weightx = 0;
268
        c.gridx++;
269
        panelMoisCompte.add(boutonShow, c);
270
 
271
        boutonShow.addActionListener(new ActionListener() {
272
            private boolean isShow = false;
273
            private ListPanelEcritures listEcriture;
274
 
275
            public void actionPerformed(ActionEvent e) {
276
 
277
                System.err.println(this.isShow);
278
 
279
                // Afficher la JTable du compte
280
                if (!this.isShow) {
19 ilm 281
                    final SQLElement element = Configuration.getInstance().getDirectory().getElement("ECRITURE");
282
                    final SQLTable ecrTable = element.getTable();
18 ilm 283
 
19 ilm 284
                    Calendar cal = Calendar.getInstance();
285
 
286
                    cal.setTime(date);
287
                    cal.set(Calendar.DATE, 1);
288
                    Date inf = cal.getTime();
289
 
290
                    cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
291
                    Date sup = cal.getTime();
292
 
293
                    System.out.println("Inf : " + inf + " Sup : " + sup);
294
                    Where w = new Where(ecrTable.getField("ID_JOURNAL"), "=", jrnl.getId());
295
                    Where w2 = new Where(ecrTable.getField("DATE"), inf, sup);
296
 
144 ilm 297
                    if (!UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
19 ilm 298
                        // TODO Show Restricted acces in UI
299
                        w = w.and(new Where(ecrTable.getField("COMPTE_NUMERO"), "LIKE", "411%"));
300
                    }
301
 
302
                    this.listEcriture = new ListPanelEcritures(element, w.and(w2));
18 ilm 303
                    this.listEcriture.setModificationVisible(false);
304
                    this.listEcriture.setAjoutVisible(false);
305
                    this.listEcriture.setSuppressionVisible(false);
151 ilm 306
                    this.listEcriture.getListe().setModificationAllowed(false);
18 ilm 307
 
308
                    Dimension d;
309
                    // Taille limitée à 200 maximum
310
                    if (this.listEcriture.getListe().getPreferredSize().height > 200) {
311
                        d = new Dimension(this.listEcriture.getListe().getPreferredSize().width, 200);
312
                    } else {
313
                        d = new Dimension(this.listEcriture.getListe().getPreferredSize().width, this.listEcriture.getListe().getPreferredSize().height + 30);
314
                    }
315
                    this.listEcriture.getListe().setPreferredSize(d);
316
 
317
                    // c.gridy = 2;
318
                    c.gridx = 0;
319
                    c.gridy = 1;
320
 
321
                    c.gridwidth = 4;
322
                    c.weightx = 1;
323
                    c.weighty = 1;
324
                    c.fill = GridBagConstraints.BOTH;
325
 
151 ilm 326
                    this.listEcriture.getListe().setModificationAllowed(false);
18 ilm 327
 
328
                    panelMoisCompte.add(this.listEcriture, c);
329
                    this.listEcriture.getListe().getJTable().addMouseListener(new MouseAdapter() {
330
                        public void mousePressed(MouseEvent e) {
331
                            if (e.getButton() == MouseEvent.BUTTON3) {
332
                                JPopupMenu menu = new JPopupMenu();
333
                                menu.add(new AbstractAction("Voir la source") {
334
                                    public void actionPerformed(ActionEvent e) {
335
 
336
                                        SQLRow row = base.getTable("ECRITURE").getRow(listEcriture.getListe().getSelectedId());
337
 
338
                                        MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
339
                                    }
340
                                });
341
 
342
                                menu.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
343
 
344
                            }
345
                        }
346
                    });
347
 
348
                } else {
349
 
350
                    panelMoisCompte.remove(this.listEcriture);
351
                    System.out.println("Hide ListEcriture");
352
 
353
                    panelMoisCompte.repaint();
354
                    panelMoisCompte.revalidate();
355
                }
356
 
357
                this.isShow = !this.isShow;
358
                SwingUtilities.getRoot(panelMoisCompte).repaint();
359
            }
360
        });
361
 
362
        return panelMoisCompte;
363
    }
364
 
365
    int nbLoading = 0;
366
 
367
    public void fireIsLoading(boolean isLoading) {
368
        if (isLoading) {
369
            nbLoading++;
370
        } else {
371
            nbLoading--;
372
        }
373
        for (LoadingTableListener l : this.loadingListener.getListeners(LoadingTableListener.class)) {
374
            l.isLoading(nbLoading > 0);
375
        }
376
    }
377
 
378
    public void addLoadingListener(LoadingTableListener l) {
379
        this.loadingListener.add(LoadingTableListener.class, l);
380
    }
381
}