OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18
import org.openconcerto.erp.core.common.ui.DeviseNiceTableCellRenderer;
19
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
20
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
180 ilm 21
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
18 ilm 22
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
23
import org.openconcerto.erp.core.finance.accounting.model.LettrageModel;
24
import org.openconcerto.erp.model.ISQLCompteSelector;
25
import org.openconcerto.erp.rights.ComptaUserRight;
180 ilm 26
import org.openconcerto.erp.utils.LowerCaseFormatFilter;
18 ilm 27
import org.openconcerto.erp.utils.UpperCaseFormatFilter;
28
import org.openconcerto.sql.Configuration;
29
import org.openconcerto.sql.element.SQLElement;
149 ilm 30
import org.openconcerto.sql.element.SQLElementDirectory;
18 ilm 31
import org.openconcerto.sql.model.SQLBase;
32
import org.openconcerto.sql.model.SQLRow;
180 ilm 33
import org.openconcerto.sql.model.SQLRowAccessor;
34
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 35
import org.openconcerto.sql.model.SQLRowValues;
180 ilm 36
import org.openconcerto.sql.model.SQLSelect;
149 ilm 37
import org.openconcerto.sql.model.SQLSystem;
18 ilm 38
import org.openconcerto.sql.model.SQLTable;
180 ilm 39
import org.openconcerto.sql.model.UndefinedRowValuesCache;
18 ilm 40
import org.openconcerto.sql.model.Where;
149 ilm 41
import org.openconcerto.sql.request.ComboSQLRequest;
144 ilm 42
import org.openconcerto.sql.users.rights.UserRightsManager;
180 ilm 43
import org.openconcerto.sql.view.EditFrame;
44
import org.openconcerto.sql.view.EditPanel.EditMode;
45
import org.openconcerto.sql.view.EditPanelListener;
19 ilm 46
import org.openconcerto.sql.view.list.IListe;
180 ilm 47
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
48
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 49
import org.openconcerto.ui.DefaultGridBagConstraints;
151 ilm 50
import org.openconcerto.ui.FontUtils;
18 ilm 51
import org.openconcerto.ui.JDate;
52
import org.openconcerto.ui.TitledSeparator;
53
import org.openconcerto.ui.warning.JLabelWarning;
180 ilm 54
import org.openconcerto.utils.ExceptionHandler;
55
import org.openconcerto.utils.ListMap;
18 ilm 56
import org.openconcerto.utils.text.DocumentFilterList;
19 ilm 57
import org.openconcerto.utils.text.DocumentFilterList.FilterType;
18 ilm 58
import org.openconcerto.utils.text.SimpleDocumentListener;
59
 
60
import java.awt.Color;
61
import java.awt.Component;
62
import java.awt.Dimension;
63
import java.awt.GridBagConstraints;
64
import java.awt.GridBagLayout;
65
import java.awt.Insets;
66
import java.awt.Window;
67
import java.awt.event.ActionEvent;
68
import java.awt.event.ActionListener;
69
import java.awt.event.KeyAdapter;
70
import java.awt.event.KeyEvent;
71
import java.awt.event.MouseAdapter;
72
import java.awt.event.MouseEvent;
73
import java.beans.PropertyChangeEvent;
74
import java.beans.PropertyChangeListener;
75
import java.sql.SQLException;
76
import java.text.ParseException;
77
import java.util.ArrayList;
78
import java.util.Date;
180 ilm 79
import java.util.HashMap;
18 ilm 80
import java.util.List;
180 ilm 81
import java.util.Map;
82
import java.util.Map.Entry;
18 ilm 83
 
84
import javax.swing.AbstractAction;
85
import javax.swing.BorderFactory;
86
import javax.swing.ButtonGroup;
87
import javax.swing.JButton;
88
import javax.swing.JCheckBox;
89
import javax.swing.JLabel;
90
import javax.swing.JPanel;
91
import javax.swing.JRadioButton;
92
import javax.swing.JScrollPane;
93
import javax.swing.JTable;
94
import javax.swing.JTextField;
95
import javax.swing.SwingConstants;
96
import javax.swing.SwingUtilities;
97
import javax.swing.event.DocumentEvent;
98
import javax.swing.text.AbstractDocument;
99
import javax.swing.text.MaskFormatter;
100
 
101
public class LettragePanel extends JPanel {
102
 
103
    private ListPanelEcritures ecriturePanel;
180 ilm 104
    private JTextField codeLettrage, codeLettragePartiel;
18 ilm 105
    private ISQLCompteSelector selCompte;
106
    private JCheckBox boxValidEcriture, boxAddSousCompte;
107
    private JPanel warningPanel, warningSolde;
108
 
109
    private final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
110
    private final SQLTable tableEcr = this.base.getTable("ECRITURE");
111
    private final SQLTable tableComptePCE = this.base.getTable("COMPTE_PCE");
112
 
113
    private final static int allEcriture = 0;
114
    private final static int ecritureLettree = 1;
115
    private final static int ecritureNonLettree = 2;
116
 
117
    private int modeSelect;
118
    private LettrageModel model;
119
    private JButton buttonLettrer;
180 ilm 120
    private final JButton buttonRegler = new JButton("Régler");
18 ilm 121
    private JDate dateDeb, dateFin, dateLettrage;
122
 
123
    public LettragePanel() {
177 ilm 124
        this(ComptePCESQLElement.getId("4"));
125
    }
126
 
127
    public LettragePanel(int idCompte) {
18 ilm 128
        this.setLayout(new GridBagLayout());
129
        GridBagConstraints c = new DefaultGridBagConstraints();
149 ilm 130
        final SQLElementDirectory directory = Configuration.getInstance().getDirectory();
18 ilm 131
 
132
        this.modeSelect = allEcriture;
133
 
134
        // Selection du compte à lettrer
135
        JLabel labelPointageCompte = new JLabel("Lettrage du compte");
136
        labelPointageCompte.setHorizontalAlignment(SwingConstants.RIGHT);
137
 
138
        this.add(labelPointageCompte, c);
139
        this.selCompte = new ISQLCompteSelector();
149 ilm 140
        SQLElement eltCpt = directory.getElement("COMPTE_PCE");
141
        final ComboSQLRequest createComboRequest = eltCpt.createComboRequest();
142
        String function = "REGEXP";
143
        if (Configuration.getInstance().getBase().getServer().getSQLSystem() == SQLSystem.POSTGRESQL) {
144
            function = "~";
145
        }
146
        createComboRequest.setWhere(new Where(eltCpt.getTable().getField("NUMERO"), function, "^4.*$"));
147
        this.selCompte.init(eltCpt, createComboRequest);
177 ilm 148
        this.selCompte.setValue(idCompte);
18 ilm 149
 
150
        c.gridx++;
151
        c.weightx = 1;
152
        c.gridwidth = GridBagConstraints.REMAINDER;
153
        this.add(this.selCompte, c);
154
 
155
        c.gridwidth = 1;
156
 
157
        // Gestion du lettrage
158
        c.insets = new Insets(2, 2, 1, 2);
159
        TitledSeparator sepGestionLettrage = new TitledSeparator("Gestion du lettrage");
160
        c.fill = GridBagConstraints.HORIZONTAL;
161
        c.gridy++;
162
        c.gridx = 0;
163
        c.gridwidth = GridBagConstraints.REMAINDER;
164
        this.add(sepGestionLettrage, c);
165
 
166
        // Code de lettrage
167
        JLabel labelCode = new JLabel("Code lettrage");
168
        labelCode.setHorizontalAlignment(SwingConstants.RIGHT);
169
        c.gridx = 0;
170
        c.gridy++;
171
        c.gridwidth = 1;
172
        c.weightx = 0;
173
        this.add(labelCode, c);
174
 
175
        this.codeLettrage = new JTextField(10);
176
        DocumentFilterList.add((AbstractDocument) this.codeLettrage.getDocument(), new UpperCaseFormatFilter(), FilterType.SIMPLE_FILTER);
177
        c.gridx++;
178
        c.weightx = 1;
179
        this.add(this.codeLettrage, c);
180
        this.codeLettrage.setText(NumerotationAutoSQLElement.getNextCodeLettrage());
181
 
180 ilm 182
        JLabel labelCodepartiel = new JLabel("Code lettrage partiel");
183
        labelCodepartiel.setHorizontalAlignment(SwingConstants.RIGHT);
184
        c.gridx++;
185
        c.gridwidth = 1;
186
        c.weightx = 0;
187
        this.add(labelCodepartiel, c);
188
 
189
        this.codeLettragePartiel = new JTextField(10);
190
        DocumentFilterList.add((AbstractDocument) this.codeLettragePartiel.getDocument(), new LowerCaseFormatFilter(), FilterType.SIMPLE_FILTER);
191
        c.gridx++;
192
        c.weightx = 1;
193
        this.add(this.codeLettragePartiel, c);
194
        this.codeLettragePartiel.setText(getNextCodeLettragePartiel());
195
 
18 ilm 196
        // Warning si aucun code rentré
197
        createPanelWarning();
198
        c.gridwidth = GridBagConstraints.REMAINDER;
199
        c.weightx = 0;
200
        c.gridx++;
201
        this.add(this.warningPanel, c);
202
 
203
        // Date de lettrage
204
        JLabel labelDate = new JLabel("Date");
205
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
206
        c.gridx = 0;
207
        c.gridy++;
208
        c.weightx = 0;
209
        c.gridwidth = 1;
210
        this.add(labelDate, c);
211
        this.dateLettrage = new JDate(true);
212
        c.gridx++;
213
        this.add(this.dateLettrage, c);
214
 
215
        // Warning si solde non nul
216
        c.gridx++;
217
        createPanelWarningSolde();
218
        c.gridwidth = GridBagConstraints.REMAINDER;
219
        c.weightx = 0;
220
        this.add(this.warningSolde, c);
221
 
222
        c.gridwidth = 1;
223
 
224
        TitledSeparator sepPeriode = new TitledSeparator("Filtre ");
225
        c.gridy++;
226
        c.gridx = 0;
227
        c.anchor = GridBagConstraints.WEST;
228
        c.gridwidth = GridBagConstraints.REMAINDER;
229
        c.fill = GridBagConstraints.HORIZONTAL;
230
        this.add(sepPeriode, c);
231
 
232
        JPanel panelSelectEcritures = createPanelSelectionEcritures();
233
        c.gridy++;
234
        c.gridx = 0;
235
        c.gridwidth = 1;
236
        c.weightx = 0;
237
 
238
        JLabel labelEcr = new JLabel("Ecritures");
239
        labelEcr.setHorizontalAlignment(SwingConstants.RIGHT);
240
        this.add(labelEcr, c);
241
        c.gridx++;
242
        c.weightx = 1;
243
        c.gridwidth = GridBagConstraints.REMAINDER;
244
        c.fill = GridBagConstraints.NONE;
245
        this.add(panelSelectEcritures, c);
246
 
247
        JPanel panelPeriode = new JPanel();
248
        // Date de début
249
        this.dateDeb = new JDate();
250
        c.gridy++;
251
        c.gridx = 0;
252
        c.gridwidth = 1;
253
        c.weightx = 0;
254
        JLabel labelPerio = new JLabel("Période du ");
255
        labelPerio.setHorizontalAlignment(SwingConstants.RIGHT);
256
        c.fill = GridBagConstraints.HORIZONTAL;
257
        this.add(labelPerio, c);
258
 
259
        panelPeriode.add(this.dateDeb);
260
        this.dateDeb.addValueListener(new PropertyChangeListener() {
261
            public void propertyChange(PropertyChangeEvent evt) {
262
                changeListRequest();
263
            }
264
        });
265
 
266
        // Date de fin
267
        this.dateFin = new JDate(true);
268
        panelPeriode.add(new JLabel("au"));
269
        this.dateFin.addValueListener(new PropertyChangeListener() {
270
            public void propertyChange(PropertyChangeEvent evt) {
271
                changeListRequest();
272
            }
273
        });
274
 
275
        panelPeriode.add(this.dateFin);
276
        c.gridx++;
277
        c.gridwidth = GridBagConstraints.REMAINDER;
278
        c.weightx = 1;
279
        c.fill = GridBagConstraints.NONE;
280
        this.add(panelPeriode, c);
281
 
282
        c.gridx = 0;
283
        c.gridy++;
284
        this.boxAddSousCompte = new JCheckBox("Ajouter les sous comptes");
285
        this.boxAddSousCompte.addActionListener(new ActionListener() {
286
            public void actionPerformed(ActionEvent e) {
287
                // TODO Auto-generated method stub
288
                changeListRequest();
289
            }
290
        });
291
 
292
        this.add(this.boxAddSousCompte, c);
293
 
294
        TitledSeparator sepEcriture = new TitledSeparator("Ecritures ");
295
        c.gridy++;
296
        c.gridx = 0;
297
        c.weightx = 1;
298
        c.gridwidth = GridBagConstraints.REMAINDER;
299
        c.fill = GridBagConstraints.HORIZONTAL;
300
        this.add(sepEcriture, c);
301
 
302
        // Liste des ecritures
149 ilm 303
        final EcritureSQLElement ecritureElem = directory.getElement(EcritureSQLElement.class);
19 ilm 304
        this.ecriturePanel = new ListPanelEcritures(ecritureElem, new IListe(ecritureElem.createLettrageTableSource()));
18 ilm 305
        c.gridx = 0;
306
        c.gridy++;
307
        c.weighty = 1;
308
        c.weightx = 1;
309
        c.fill = GridBagConstraints.BOTH;
310
        c.gridwidth = GridBagConstraints.REMAINDER;
311
        this.ecriturePanel.getListe().setPreferredSize(new Dimension(this.ecriturePanel.getListe().getPreferredSize().width, 200));
312
        this.add(this.ecriturePanel, c);
313
 
314
        // JTable Totaux
315
        c.gridy++;
316
        c.gridx = 0;
317
        c.weighty = 0;
318
        c.weightx = 1;
319
        c.fill = GridBagConstraints.BOTH;
320
        c.gridwidth = 3;
321
        c.gridheight = 3;
322
        this.model = new LettrageModel(this.selCompte.getSelectedId());
323
        JTable table = new JTable(this.model);
151 ilm 324
        table.setRowHeight(FontUtils.getPreferredRowHeight(table));
18 ilm 325
        // AlternateTableCellRenderer.setAllColumns(table);
326
        for (int i = 0; i < table.getColumnCount(); i++) {
327
            // if (table.getColumnClass(i) == Long.class || table.getColumnClass(i) ==
328
            // BigInteger.class) {
329
            table.getColumnModel().getColumn(i).setCellRenderer(new DeviseNiceTableCellRenderer());
330
            // }
331
        }
332
        JScrollPane sPane = new JScrollPane(table);
333
 
334
        // TODO Gerer la taille des colonnes
335
        Dimension d = new Dimension(table.getPreferredSize().width, table.getPreferredSize().height + table.getTableHeader().getPreferredSize().height + 4);
336
        sPane.setPreferredSize(d);
337
        this.add(sPane, c);
338
 
339
        // Legende
340
        c.gridx = 4;
341
        c.gridwidth = 1;
342
        c.anchor = GridBagConstraints.EAST;
343
        c.fill = GridBagConstraints.NONE;
344
        c.weightx = 0;
345
        this.add(createPanelLegende(), c);
346
 
347
        c.gridheight = 1;
348
        final JButton buttonDelettrer = new JButton("Délettrer");
349
        this.buttonLettrer = new JButton("Lettrer");
350
        // Validation des ecritures pointées
351
        this.boxValidEcriture = new JCheckBox("Valider les écritures lettrées");
352
        c.gridx++;
353
        c.gridwidth = GridBagConstraints.REMAINDER;
354
        this.add(this.boxValidEcriture, c);
355
 
356
        JPanel panelButton = new JPanel();
180 ilm 357
        panelButton.add(this.buttonRegler, c);
18 ilm 358
 
359
        // Boutton lettrer
360
 
361
        panelButton.add(this.buttonLettrer, c);
362
 
363
        // Boutton Delettrer
364
 
365
        panelButton.add(buttonDelettrer, c);
366
 
367
        c.gridy++;
368
        c.gridx = 5;
369
        c.gridwidth = GridBagConstraints.REMAINDER;
370
        c.gridheight = 1;
371
        c.weightx = 1;
372
        c.fill = GridBagConstraints.NONE;
373
        c.anchor = GridBagConstraints.EAST;
374
        this.add(panelButton, c);
375
 
376
        c.gridy++;
377
        c.anchor = GridBagConstraints.SOUTHEAST;
378
        JButton buttonClose = new JButton("Fermer");
379
        buttonClose.addActionListener(new ActionListener() {
380
 
381
            @Override
382
            public void actionPerformed(ActionEvent e) {
383
                ((Window) SwingUtilities.getRoot((Component) e.getSource())).dispose();
384
 
385
            }
386
        });
387
        this.add(buttonClose, c);
388
        this.buttonLettrer.addActionListener(new ActionListener() {
389
            public void actionPerformed(ActionEvent e) {
180 ilm 390
                try {
391
                    int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
392
                    actionLettrage(rowIndex, false);
393
                } catch (SQLException e1) {
394
                    ExceptionHandler.handle("Erreur de lettrage", e1);
395
                }
18 ilm 396
 
180 ilm 397
            }
398
        });
18 ilm 399
 
180 ilm 400
        this.buttonRegler.addActionListener(new ActionListener() {
401
            public void actionPerformed(ActionEvent e) {
18 ilm 402
 
180 ilm 403
                final List<SQLRowValues> res = LettragePanel.this.ecriturePanel.getListe().getSelectedRows();
404
 
405
                final SQLTable tableKm = LettragePanel.this.ecriturePanel.getListe().getSource().getPrimaryTable().getTable("SAISIE_KM");
406
                SQLRowValues rowValsKm = new SQLRowValues(tableKm);
407
                rowValsKm.put("DATE", new Date());
408
                rowValsKm.put("ID_JOURNAL", JournalSQLElement.BANQUES);
409
 
410
                long solde = LettragePanel.this.model.getSoldeSelection();
411
 
412
                final SQLTable tableKmItem = tableKm.getTable("SAISIE_KM_ELEMENT");
413
                List<String> pieces = new ArrayList<>();
414
 
415
                for (SQLRowValues sqlRowValues : res) {
416
                    SQLRowValues rowValsKmItemTiers = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableKmItem));
417
                    rowValsKmItemTiers.put("NUMERO", sqlRowValues.getForeign("ID_COMPTE_PCE").getString("NUMERO"));
418
                    rowValsKmItemTiers.put("NOM", sqlRowValues.getForeign("ID_COMPTE_PCE").getString("NOM"));
419
                    final String pieceNom;
420
                    if (sqlRowValues.getString("NOM_PIECE").trim().length() > 0) {
421
                        pieceNom = sqlRowValues.getString("NOM_PIECE");
422
                    } else {
423
                        pieceNom = sqlRowValues.getForeign("ID_MOUVEMENT").getForeign("ID_PIECE").getString("NOM");
424
                    }
425
                    rowValsKmItemTiers.put("NOM_PIECE", pieceNom);
426
                    if (pieceNom != null && pieceNom.trim().length() > 0) {
427
                        pieces.add(pieceNom);
428
                    }
429
                    rowValsKmItemTiers.put("CREDIT", sqlRowValues.getLong("DEBIT"));
430
                    rowValsKmItemTiers.put("DEBIT", sqlRowValues.getLong("CREDIT"));
431
                    if (rowValsKmItemTiers.getTable().contains("MONTANT_ECHEANCE")) {
432
                        rowValsKmItemTiers.put("MONTANT_ECHEANCE", sqlRowValues.getLong("CREDIT"));
433
                    }
434
                    rowValsKmItemTiers.put("ID_SAISIE_KM", rowValsKm);
435
                }
436
 
437
                SQLRowValues rowValsKmItemBq = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableKmItem));
438
                boolean achat = solde < 0;
439
                // Compte bq
440
                int idPce = tableKm.getTable("TYPE_REGLEMENT").getRow(2).getInt("ID_COMPTE_PCE_" + (achat ? "FOURN" : "CLIENT"));
441
                if (idPce <= 1) {
442
                    try {
443
                        idPce = ComptePCESQLElement.getIdComptePceDefault("VenteCB");
444
                    } catch (Exception e1) {
445
                        // TODO Auto-generated catch block
446
                        e1.printStackTrace();
447
                    }
448
                }
449
                final SQLTable tableAccount = tableKmItem.getTable("COMPTE_PCE");
450
                SQLRow rowCptBq = tableAccount.getRow(idPce);
451
                rowValsKmItemBq.put("NUMERO", rowCptBq.getString("NUMERO"));
452
                rowValsKmItemBq.put("NOM", rowCptBq.getString("NOM"));
453
                if (!pieces.isEmpty()) {
454
                    StringBuilder build = new StringBuilder();
455
                    int nbPieces = pieces.size();
456
                    int i = 0;
457
                    for (String string : pieces) {
458
                        build.append(string);
459
                        i++;
460
                        if (i < nbPieces) {
461
                            build.append(", ");
462
                        }
463
 
464
                    }
465
                    rowValsKmItemBq.put("NOM_PIECE", build.toString());
466
                }
467
 
468
                if (solde > 0) {
469
                    rowValsKmItemBq.put("CREDIT", solde);
470
                    rowValsKmItemBq.put("DEBIT", 0L);
471
                } else {
472
                    rowValsKmItemBq.put("DEBIT", -solde);
473
                    rowValsKmItemBq.put("CREDIT", 0L);
474
                }
475
                rowValsKmItemBq.put("ID_SAISIE_KM", rowValsKm);
476
 
477
                EditFrame frame = new EditFrame(Configuration.getInstance().getDirectory().getElement("SAISIE_KM"), EditMode.CREATION);
478
                frame.getSQLComponent().select(rowValsKm);
479
                frame.setVisible(true);
480
                frame.addEditPanelListener(new EditPanelListener() {
481
 
482
                    @Override
483
                    public void modified() {
484
                        // TODO Auto-generated method stub
485
 
486
                    }
487
 
488
                    @Override
489
                    public void inserted(int id) {
490
                        List<SQLRow> rowsInserted = tableKm.getRow(id).getReferentRows(tableKmItem);
491
                        List<String> piece = new ArrayList<>();
492
                        List<SQLRowAccessor> rowsToLettre = new ArrayList<>();
493
                        long solde = 0;
494
                        for (SQLRowValues sqlRowValues : res) {
495
                            rowsToLettre.add(sqlRowValues);
496
                            final String nomPiece = sqlRowValues.getString("NOM_PIECE");
497
                            if (sqlRowValues.getForeign("ID_COMPTE_PCE").getString("NUMERO").startsWith("4") && nomPiece.trim().length() > 0) {
498
                                piece.add(nomPiece);
499
                            }
500
                            solde += sqlRowValues.getLong("DEBIT");
501
                            solde -= sqlRowValues.getLong("CREDIT");
502
                        }
503
 
504
                        for (SQLRow sqlRow : rowsInserted) {
505
                            SQLRow rowEcr = sqlRow.getForeign("ID_ECRITURE");
506
 
507
                            final String nomPiece = sqlRow.getString("NOM_PIECE");
508
                            if (rowEcr.getString("COMPTE_NUMERO").startsWith("4")) {
509
                                solde += rowEcr.getLong("DEBIT");
510
                                solde -= rowEcr.getLong("CREDIT");
511
                                if (nomPiece.trim().length() > 0) {
512
                                    piece.add(nomPiece);
513
                                }
514
                                rowsToLettre.add(rowEcr);
515
                            }
516
                        }
517
 
518
                        if (solde == 0) {
519
                            final String codeLettre = codeLettrage.getText().trim();
520
 
521
                            for (SQLRowAccessor row2 : rowsToLettre) {
522
 
523
                                SQLRowValues rowVals = new SQLRowValues(row2.getTable());
524
 
525
                                // Lettrage
526
                                // On lettre ou relettre la ligne avec le code saisi
527
                                if (codeLettre.length() > 0) {
528
                                    rowVals.put("LETTRAGE_PARTIEL", "");
529
                                    rowVals.put("LETTRAGE", codeLettre);
530
                                    rowVals.put("DATE_LETTRAGE", dateLettrage.getDate());
531
                                    try {
532
                                        rowVals.update(row2.getID());
533
                                    } catch (SQLException e1) {
534
 
535
                                        e1.printStackTrace();
536
                                    }
537
                                }
538
                            }
539
                            // Mise à jour du code de lettrage
540
                            SQLElement elt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
541
                            SQLRowValues rowVals = elt.getTable().getRow(2).createEmptyUpdateRow();
542
                            rowVals.put("CODE_LETTRAGE", codeLettre);
543
                            try {
544
                                rowVals.update();
545
                            } catch (SQLException e) {
546
                                e.printStackTrace();
547
                            }
548
                            codeLettrage.setText(getNextCodeLettrage());
549
 
550
                            model.updateTotauxCompte();
551
                        } else {
552
                            String codeLettreP = codeLettragePartiel.getText().trim();
553
                            String codeLettre = codeLettrage.getText().trim();
554
 
555
                            SQLSelect selEcr = new SQLSelect();
556
                            SQLTable tableEcr = tableKm.getTable("ECRITURE");
557
                            selEcr.addSelect(tableEcr.getKey());
558
                            selEcr.addSelect(tableEcr.getField("NOM_PIECE"));
559
                            selEcr.addSelect(tableEcr.getField("DEBIT"));
560
                            selEcr.addSelect(tableEcr.getField("CREDIT"));
561
                            selEcr.addSelect(tableEcr.getField("LETTRAGE"));
562
                            selEcr.addSelect(tableEcr.getField("LETTRAGE_PARTIEL"));
563
                            Where w2 = new Where(tableEcr.getField("NOM_PIECE"), piece);
564
                            w2 = w2.and(new Where(tableEcr.getField("COMPTE_NUMERO"), "LIKE", "40%").or(new Where(tableEcr.getField("COMPTE_NUMERO"), "LIKE", "41%")));
565
                            w2 = w2.and(new Where(tableEcr.getField("DATE_LETTRAGE"), "=", (Object) null));
566
                            selEcr.setWhere(w2);
567
 
568
                            List<SQLRow> rows = SQLRowListRSH.execute(selEcr);
569
                            ListMap<String, SQLRow> mapPiece = new ListMap<>();
570
                            Map<String, Long> soldePiece = new HashMap<>();
571
                            for (SQLRow sqlRow : rows) {
572
                                String pieceName = sqlRow.getString("NOM_PIECE");
573
                                mapPiece.add(pieceName, sqlRow);
574
                                long soldeRow = sqlRow.getLong("DEBIT") - sqlRow.getLong("CREDIT");
575
                                if (soldePiece.containsKey(pieceName)) {
576
                                    soldePiece.put(pieceName, soldePiece.get(pieceName) + soldeRow);
577
                                } else {
578
                                    soldePiece.put(pieceName, soldeRow);
579
                                }
580
                            }
581
 
582
                            for (Entry<String, List<SQLRow>> entry : mapPiece.entrySet()) {
583
 
584
                                if (soldePiece.get(entry.getKey()) == 0) {
585
                                    try {
586
                                        for (SQLRow rowEcr : entry.getValue()) {
587
                                            SQLRowValues rowVals = rowEcr.createEmptyUpdateRow();
588
 
589
                                            // Lettrage
590
                                            // On lettre ou relettre la ligne avec le code saisi
591
                                            if (codeLettre.length() > 0) {
592
                                                rowVals.put("LETTRAGE_PARTIEL", "");
593
                                                rowVals.put("LETTRAGE", codeLettre);
594
                                                rowVals.put("DATE_LETTRAGE", dateLettrage.getDate());
595
                                                rowVals.update();
596
                                            }
597
                                        }
598
                                        // Mise à jour du code de lettrage
599
                                        SQLElement elt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
600
                                        SQLRowValues rowVals = elt.getTable().getRow(2).createEmptyUpdateRow();
601
                                        rowVals.put("CODE_LETTRAGE", codeLettre);
602
                                        rowVals.update();
603
 
604
                                        codeLettre = getNextCodeLettrage();
605
                                    } catch (SQLException e) {
606
                                        e.printStackTrace();
607
                                    }
608
                                } else {
609
                                    try {
610
                                        for (SQLRow rowEcr : entry.getValue()) {
611
                                            SQLRowValues rowVals = rowEcr.createEmptyUpdateRow();
612
 
613
                                            // Lettrage
614
                                            // On lettre ou relettre la ligne avec le code saisi
615
                                            if (codeLettreP.length() > 0) {
616
 
617
                                                rowVals.put("LETTRAGE_PARTIEL", codeLettreP);
618
                                                rowVals.update();
619
 
620
                                            }
621
                                        }
622
                                        // Mise à jour du code de lettrage
623
                                        SQLElement elt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
624
                                        SQLRowValues rowVals = elt.getTable().getRow(2).createEmptyUpdateRow();
625
                                        rowVals.put("CODE_LETTRAGE_PARTIEL", codeLettreP);
626
                                        rowVals.update();
627
                                        codeLettreP = getNextCodeLettragePartiel();
628
                                    } catch (SQLException e) {
629
                                        e.printStackTrace();
630
                                    }
631
                                }
632
                            }
633
                            codeLettrage.setText(getNextCodeLettrage());
634
 
635
                            codeLettragePartiel.setText(getNextCodeLettragePartiel());
636
                            model.updateTotauxCompte();
637
                        }
638
                    }
639
 
640
                    @Override
641
                    public void deleted() {
642
                        // TODO Auto-generated method stub
643
 
644
                    }
645
 
646
                    @Override
647
                    public void cancelled() {
648
                        // TODO Auto-generated method stub
649
 
650
                    }
651
                });
18 ilm 652
            }
653
        });
654
 
655
        buttonDelettrer.addActionListener(new ActionListener() {
656
            public void actionPerformed(ActionEvent e) {
657
 
658
                int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
180 ilm 659
                actionDelettrage(rowIndex, false);
18 ilm 660
            }
661
        });
662
 
663
        // Changement de compte
664
        this.selCompte.addValueListener(new PropertyChangeListener() {
665
            public void propertyChange(PropertyChangeEvent evt) {
666
 
667
                changeListRequest();
668
            };
669
        });
670
 
671
        // Action Souris sur la IListe
672
 
673
        addActionMenuDroit();
674
        this.ecriturePanel.getListe().getJTable().addMouseListener(new MouseAdapter() {
675
 
676
            public void mouseReleased(MouseEvent e) {
677
                System.err.println("Mouse released");
678
                int[] selectedRows = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
679
                int[] idRows = new int[selectedRows.length];
680
                for (int i = 0; i < idRows.length; i++) {
681
                    idRows[i] = LettragePanel.this.ecriturePanel.getListe().idFromIndex(selectedRows[i]);
682
                }
683
 
684
                LettragePanel.this.model.updateSelection(idRows);
685
                LettragePanel.this.warningSolde.setVisible(LettragePanel.this.model.getSoldeSelection() != 0);
686
                buttonDelettrer.setEnabled(LettragePanel.this.model.getSoldeSelection() == 0);
687
                LettragePanel.this.buttonLettrer.setEnabled(LettragePanel.this.model.getSoldeSelection() == 0);
180 ilm 688
                LettragePanel.this.buttonRegler.setEnabled(!LettragePanel.this.ecriturePanel.getListe().getSelectedRows().isEmpty());
18 ilm 689
            }
690
        });
691
 
692
        // action sur la IListe
693
        this.ecriturePanel.getListe().getJTable().addKeyListener(new KeyAdapter() {
694
            public void keyReleased(KeyEvent e) {
695
 
696
                System.err.println("Key released");
697
                int[] selectedRows = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
698
                int[] idRows = new int[selectedRows.length];
699
                for (int i = 0; i < idRows.length; i++) {
700
                    idRows[i] = LettragePanel.this.ecriturePanel.getListe().idFromIndex(selectedRows[i]);
701
                }
702
 
703
                LettragePanel.this.model.updateSelection(idRows);
704
                LettragePanel.this.warningPanel.setVisible((LettragePanel.this.codeLettrage.getText().trim().length() == 0));
705
                LettragePanel.this.warningSolde.setVisible(LettragePanel.this.model.getSoldeSelection() != 0);
706
            }
707
        });
708
 
709
        // Gestion du code
710
        this.codeLettrage.getDocument().addDocumentListener(new SimpleDocumentListener() {
711
 
712
            @Override
713
            public void update(DocumentEvent e) {
714
                // TODO Auto-generated method stub
715
                LettragePanel.this.warningPanel.setVisible((LettragePanel.this.codeLettrage.getText().trim().length() == 0));
716
                LettragePanel.this.buttonLettrer.setEnabled((LettragePanel.this.codeLettrage.getText().trim().length() != 0));
717
            }
718
 
719
        });
720
 
721
        changeListRequest();
722
        this.warningPanel.setVisible((this.codeLettrage.getText().trim().length() == 0));
723
        this.buttonLettrer.setEnabled((this.codeLettrage.getText().trim().length() != 0));
724
    }
725
 
180 ilm 726
    private String getNextCodeLettragePartiel() {
727
        return Configuration.getInstance().getDirectory().getElement(NumerotationAutoSQLElement.class).getNextCodeLettragePartiel();
728
    }
729
 
730
    private String getNextCodeLettrage() {
731
        return Configuration.getInstance().getDirectory().getElement(NumerotationAutoSQLElement.class).getNextCodeLettrage();
732
    }
733
 
18 ilm 734
    /* Menu clic Droit */
735
    private void addActionMenuDroit() {
736
        // JPopupMenu menu = new JPopupMenu();
737
 
180 ilm 738
        PredicateRowAction action = new PredicateRowAction(new AbstractAction() {
18 ilm 739
            public void actionPerformed(ActionEvent e) {
740
 
80 ilm 741
                SQLRow rowEcr = LettragePanel.this.ecriturePanel.getListe().fetchSelectedRow();
18 ilm 742
                MouvementSQLElement.showSource(rowEcr.getInt("ID_MOUVEMENT"));
743
            }
180 ilm 744
        }, false, "financing.accouning.entries.source.show");
745
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());
18 ilm 746
 
180 ilm 747
        this.ecriturePanel.getListe().addIListeAction(action);
748
 
67 ilm 749
        final AbstractAction abstractAction = new AbstractAction() {
18 ilm 750
            public void actionPerformed(ActionEvent e) {
751
 
180 ilm 752
                try {
753
                    int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
754
                    actionLettrage(rowIndex, false);
755
                } catch (SQLException e1) {
756
                    ExceptionHandler.handle("erreur de lettrage", e1);
757
                }
18 ilm 758
            }
759
        };
180 ilm 760
        PredicateRowAction actionLettre = new PredicateRowAction(abstractAction, false, "financing.accouning.entries.match");
761
        actionLettre.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
762
        this.ecriturePanel.getListe().addIListeAction(actionLettre);
763
 
764
        final AbstractAction abstractActionPartiel = new AbstractAction("Lettrage Partiel") {
765
            public void actionPerformed(ActionEvent e) {
766
 
767
                try {
768
                    int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
769
                    actionLettrage(rowIndex, true);
770
                } catch (SQLException e1) {
771
                    ExceptionHandler.handle("erreur de lettrage", e1);
772
                }
773
            }
774
        };
775
        PredicateRowAction actionLettreP = new PredicateRowAction(abstractActionPartiel, false, "financing.accouning.entries.match.partial");
776
        actionLettreP.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
777
        this.ecriturePanel.getListe().addIListeAction(actionLettreP);
778
 
18 ilm 779
        // }
780
        this.codeLettrage.getDocument().addDocumentListener(new SimpleDocumentListener() {
781
            @Override
782
            public void update(DocumentEvent e) {
783
                abstractAction.setEnabled(LettragePanel.this.codeLettrage.getText().trim().length() > 0);
784
            }
785
        });
786
 
787
        this.ecriturePanel.getListe().addRowAction(new AbstractAction("Délettrer") {
788
            public void actionPerformed(ActionEvent e) {
789
 
790
                int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
180 ilm 791
                actionDelettrage(rowIndex, false);
18 ilm 792
            }
67 ilm 793
        }, "financing.accouning.entries.unmatch");
18 ilm 794
 
180 ilm 795
        this.ecriturePanel.getListe().addRowAction(new AbstractAction("Délettrer partiel") {
796
            public void actionPerformed(ActionEvent e) {
797
 
798
                int[] rowIndex = LettragePanel.this.ecriturePanel.getListe().getJTable().getSelectedRows();
799
                actionDelettrage(rowIndex, true);
800
            }
801
        }, "financing.accouning.entries.unmatch.partial");
802
 
18 ilm 803
    }
804
 
805
    /* Panel Warning no numero releve */
806
    private void createPanelWarning() {
807
 
808
        this.warningPanel = new JPanel();
809
        this.warningPanel.setLayout(new GridBagLayout());
810
        // this.warningPanel.setBorder(BorderFactory.createTitledBorder("Warning"));
811
 
812
        GridBagConstraints c = new GridBagConstraints();
813
        c.anchor = GridBagConstraints.WEST;
814
        c.fill = GridBagConstraints.NONE;
815
        c.gridheight = 1;
816
        c.gridwidth = 1;
817
        c.gridx = 0;
818
        c.gridy = 0;
819
        c.weightx = 0;
820
        c.weighty = 0;
821
 
822
        final JLabel warningNoCodeImg = new JLabelWarning();
823
        warningNoCodeImg.setHorizontalAlignment(SwingConstants.RIGHT);
824
        this.warningPanel.add(warningNoCodeImg, c);
825
        final JLabel warningNoCodeText = new JLabel("Impossible de lettrer tant que le code de lettrage n'est pas saisi!");
826
        c.gridx++;
827
        this.warningPanel.add(warningNoCodeText, c);
828
    }
829
 
830
    /* Panel Warning solde invalide */
831
    private void createPanelWarningSolde() {
832
 
833
        this.warningSolde = new JPanel();
834
        this.warningSolde.setLayout(new GridBagLayout());
835
        // this.warningPanel.setBorder(BorderFactory.createTitledBorder("Warning"));
836
 
837
        GridBagConstraints c = new GridBagConstraints();
838
        c.anchor = GridBagConstraints.WEST;
839
        c.fill = GridBagConstraints.NONE;
840
        c.gridheight = 1;
841
        c.gridwidth = 1;
842
        c.gridx = 0;
843
        c.gridy = 0;
844
        c.weightx = 0;
845
        c.weighty = 0;
846
 
847
        final JLabel warningNoCodeImg = new JLabelWarning();
848
        warningNoCodeImg.setHorizontalAlignment(SwingConstants.RIGHT);
849
        this.warningSolde.add(warningNoCodeImg, c);
850
        final JLabel warningNoCodeText = new JLabel("Impossible de lettrer tant que le solde sélectionné n'est pas nul!");
851
        c.gridx++;
852
        this.warningSolde.add(warningNoCodeText, c);
853
    }
854
 
855
    // Lettre la ligne passée en parametre
856
 
180 ilm 857
    private void actionLettrage(int[] rowIndex, boolean partiel) throws SQLException {
858
 
859
        String codeLettre;
860
        if (partiel) {
861
            codeLettre = this.codeLettragePartiel.getText().trim();
862
        } else {
863
            codeLettre = this.codeLettrage.getText().trim();
864
        }
865
        // FIXME : transaction
866
 
18 ilm 867
        List<SQLRow> rowsSelected = new ArrayList<SQLRow>(rowIndex.length);
868
 
869
        long solde = 0;
870
        for (int i = 0; i < rowIndex.length; i++) {
871
            int id = this.ecriturePanel.getListe().idFromIndex(rowIndex[i]);
872
            SQLRow row = this.tableEcr.getRow(id);
873
            rowsSelected.add(row);
874
 
875
            solde += ((Long) row.getObject("DEBIT")).longValue();
876
            solde -= ((Long) row.getObject("CREDIT")).longValue();
877
        }
878
 
180 ilm 879
        if (partiel || solde == 0) {
18 ilm 880
 
881
            for (SQLRow row2 : rowsSelected) {
882
 
883
                SQLRowValues rowVals = new SQLRowValues(this.tableEcr);
884
 
885
                // Lettrage
886
                // On lettre ou relettre la ligne avec le code saisi
887
                if (codeLettre.length() > 0) {
888
 
889
                    // Si la ligne est en brouillard on valide le mouvement associé
890
                    if (this.boxValidEcriture.isSelected() && (!row2.getBoolean("VALIDE"))) {
891
                        EcritureSQLElement.validationEcritures(row2.getInt("ID_MOUVEMENT"));
892
                    }
893
 
180 ilm 894
                    if (partiel) {
895
                        rowVals.put("LETTRAGE_PARTIEL", codeLettre);
896
                    } else {
897
                        rowVals.put("LETTRAGE", codeLettre);
898
                        rowVals.put("DATE_LETTRAGE", this.dateLettrage.getDate());
899
                    }
18 ilm 900
                    try {
901
                        rowVals.update(row2.getID());
902
                    } catch (SQLException e1) {
903
 
904
                        e1.printStackTrace();
905
                    }
906
                }
907
            }
908
            // Mise à jour du code de lettrage
909
            SQLElement elt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
910
            SQLRowValues rowVals = elt.getTable().getRow(2).createEmptyUpdateRow();
180 ilm 911
            if (partiel) {
912
                rowVals.put("CODE_LETTRAGE_PARTIEL", codeLettre);
913
            } else {
914
                rowVals.put("CODE_LETTRAGE", codeLettre);
915
            }
18 ilm 916
            try {
917
                rowVals.update();
918
            } catch (SQLException e) {
919
                e.printStackTrace();
920
            }
921
            this.codeLettrage.setText(NumerotationAutoSQLElement.getNextCodeLettrage());
922
 
180 ilm 923
            if (partiel) {
924
                this.codeLettragePartiel.setText(getNextCodeLettragePartiel());
925
            } else {
926
                this.codeLettrage.setText(getNextCodeLettrage());
927
            }
928
 
18 ilm 929
            this.model.updateTotauxCompte();
930
        }
931
    }
932
 
933
    protected MaskFormatter createFormatter() {
934
        MaskFormatter formatter = null;
935
        try {
936
            formatter = new MaskFormatter("UUU");
937
        } catch (ParseException e) {
938
            // TODO Auto-generated catch block
939
            e.printStackTrace();
940
        }
941
        return formatter;
942
    }
943
 
944
    // Pointe la ligne passée en parametre
180 ilm 945
    private void actionDelettrage(int[] rowIndex, boolean partiel) {
18 ilm 946
 
947
        List<SQLRow> rowsSelected = new ArrayList<SQLRow>(rowIndex.length);
948
 
949
        long solde = 0;
950
        for (int i = 0; i < rowIndex.length; i++) {
951
            int id = this.ecriturePanel.getListe().idFromIndex(rowIndex[i]);
952
            SQLRow row = this.tableEcr.getRow(id);
953
            rowsSelected.add(row);
954
 
955
            solde += ((Long) row.getObject("DEBIT")).longValue();
956
            solde -= ((Long) row.getObject("CREDIT")).longValue();
957
        }
958
 
180 ilm 959
        if (partiel || solde == 0) {
18 ilm 960
            for (SQLRow row : rowsSelected) {
961
 
962
                SQLRowValues rowVals = new SQLRowValues(this.tableEcr);
180 ilm 963
                if (partiel) {
964
                    // Dépointage
965
                    if (row.getString("LETTRAGE_PARTIEL").trim().length() != 0) {
18 ilm 966
 
180 ilm 967
                        rowVals.put("LETTRAGE_PARTIEL", "");
968
                        try {
969
                            rowVals.update(row.getID());
970
                        } catch (SQLException e1) {
971
                            e1.printStackTrace();
972
                        }
973
                    }
974
                } else {
975
                    // Dépointage
976
                    if (row.getString("LETTRAGE").trim().length() != 0) {
18 ilm 977
 
180 ilm 978
                        rowVals.put("LETTRAGE", "");
979
                        rowVals.put("DATE_LETTRAGE", null);
980
                        try {
981
                            rowVals.update(row.getID());
982
                        } catch (SQLException e1) {
983
                            e1.printStackTrace();
984
                        }
18 ilm 985
                    }
986
                }
987
            }
988
        }
989
        this.model.updateTotauxCompte();
990
    }
991
 
992
    /*
993
     * MaJ de la requete pour remplir la IListe en fonction du compte sélectionner et du mode de
994
     * sélection
995
     */
996
    private void changeListRequest() {
997
        Object idCpt = this.selCompte.getSelectedId();
998
 
999
        SQLRow row = this.tableComptePCE.getRow(Integer.valueOf(idCpt.toString()));
1000
 
1001
        // filtre de selection
1002
        Where w = new Where(this.tableEcr.getField("ID_COMPTE_PCE"), "=", this.tableComptePCE.getKey());
1003
 
144 ilm 1004
        if (!UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
18 ilm 1005
            // TODO Show Restricted acces in UI
1006
            w = w.and(new Where(this.tableEcr.getField("COMPTE_NUMERO"), "LIKE", "411%"));
1007
        }
1008
 
1009
        if (row != null) {
1010
            String num = row.getString("NUMERO");
1011
            Where w2;
1012
            if (this.boxAddSousCompte.isSelected()) {
1013
                w2 = new Where(this.tableComptePCE.getField("NUMERO"), "LIKE", num + "%");
1014
            } else {
1015
                w2 = new Where(this.tableComptePCE.getField("NUMERO"), "=", num);
1016
            }
1017
            w = w.and(w2);
1018
        } else {
1019
            w = w.and(new Where(this.tableComptePCE.getKey(), "=", idCpt));
1020
        }
1021
 
144 ilm 1022
        if (this.tableEcr.getFields().contains("HIDE_LETTRAGE")) {
1023
            w = w.and(new Where(this.tableEcr.getField("HIDE_LETTRAGE"), "=", Boolean.FALSE));
1024
        }
1025
 
18 ilm 1026
        // final Calendar cal = Calendar.getInstance();
1027
        // cal.setTimeInMillis(this.rangeSlide.getValue(0));
1028
        //
1029
        // Date dInf = cal.getTime();
1030
        // cal.setTimeInMillis(this.rangeSlide.getValue(1));
1031
        // Date dSup = cal.getTime();
1032
 
1033
        // w = w.and(new Where(this.tableEcr.getField("DATE"), dInf, dSup));
1034
 
1035
        Date d1 = this.dateDeb.getValue();
1036
        Date d2 = this.dateFin.getValue();
1037
 
1038
        if (d1 == null && d2 != null) {
1039
            w = w.and(new Where(this.tableEcr.getField("DATE"), "<=", d2));
1040
        } else {
1041
            if (d1 != null && d2 == null) {
1042
                w = w.and(new Where(this.tableEcr.getField("DATE"), ">=", d1));
1043
            } else {
1044
                if (d1 != null && d2 != null) {
1045
                    w = w.and(new Where(this.tableEcr.getField("DATE"), d1, d2));
1046
                }
1047
            }
1048
        }
1049
 
1050
        if (this.modeSelect == ecritureLettree) {
1051
            w = w.and(new Where(this.tableEcr.getField("LETTRAGE"), "!=", ""));
1052
        } else {
1053
            if (this.modeSelect == ecritureNonLettree) {
1054
 
1055
                Where wLettre = new Where(this.tableEcr.getField("LETTRAGE"), "=", "");
1056
                String s = null;
1057
                wLettre = wLettre.or(new Where(this.tableEcr.getField("LETTRAGE"), "=", s));
1058
                w = w.and(wLettre);
1059
            }
1060
        }
1061
 
19 ilm 1062
        this.ecriturePanel.getListe().getRequest().setWhere(w);
151 ilm 1063
        this.ecriturePanel.getListe().setModificationAllowed(false);
18 ilm 1064
 
1065
        this.model.setIdCompte(Integer.parseInt(idCpt.toString()));
1066
    }
1067
 
1068
    /*
1069
     * Panel de sélection du mode d'affichage des ecritures
1070
     */
1071
    private JPanel createPanelSelectionEcritures() {
1072
 
1073
        JPanel panelSelectEcritures = new JPanel();
1074
 
1075
        GridBagConstraints cPanel = new GridBagConstraints();
1076
        cPanel.anchor = GridBagConstraints.NORTHWEST;
1077
        cPanel.fill = GridBagConstraints.HORIZONTAL;
1078
        cPanel.gridheight = 1;
1079
        cPanel.gridwidth = 1;
1080
        cPanel.gridx = 0;
1081
        cPanel.gridy = 0;
1082
        cPanel.weightx = 0;
1083
        cPanel.weighty = 0;
1084
 
1085
        panelSelectEcritures.setLayout(new GridBagLayout());
1086
 
1087
        final JRadioButton buttonBoth = new JRadioButton("Toutes");
1088
        panelSelectEcritures.add(buttonBoth, cPanel);
1089
        cPanel.gridx++;
1090
        final JRadioButton buttonNonLettre = new JRadioButton("Non lettrées");
1091
        panelSelectEcritures.add(buttonNonLettre, cPanel);
1092
        cPanel.gridx++;
1093
        final JRadioButton buttonLettre = new JRadioButton("Lettrées");
1094
        panelSelectEcritures.add(buttonLettre, cPanel);
1095
 
1096
        ButtonGroup group = new ButtonGroup();
1097
        group.add(buttonBoth);
1098
        group.add(buttonNonLettre);
1099
        group.add(buttonLettre);
1100
        buttonBoth.setSelected(true);
1101
 
1102
        buttonLettre.addActionListener(new ActionListener() {
1103
            public void actionPerformed(ActionEvent e) {
1104
                if (buttonLettre.isSelected()) {
1105
                    LettragePanel.this.modeSelect = ecritureLettree;
1106
                    changeListRequest();
1107
                }
1108
            }
1109
        });
1110
 
1111
        buttonNonLettre.addActionListener(new ActionListener() {
1112
            public void actionPerformed(ActionEvent e) {
1113
                if (buttonNonLettre.isSelected()) {
1114
                    LettragePanel.this.modeSelect = ecritureNonLettree;
1115
                    changeListRequest();
1116
                }
1117
            }
1118
        });
1119
 
1120
        buttonBoth.addActionListener(new ActionListener() {
1121
            public void actionPerformed(ActionEvent e) {
1122
                if (buttonBoth.isSelected()) {
1123
                    LettragePanel.this.modeSelect = allEcriture;
1124
                    changeListRequest();
1125
                }
1126
            }
1127
        });
1128
 
1129
        return panelSelectEcritures;
1130
    }
1131
 
1132
    /*
1133
     * Creation du panel de la legende
1134
     */
1135
    private JPanel createPanelLegende() {
1136
        JPanel panelLegende = new JPanel();
1137
 
1138
        GridBagConstraints c = new GridBagConstraints();
1139
        c.anchor = GridBagConstraints.NORTHWEST;
1140
        c.fill = GridBagConstraints.HORIZONTAL;
1141
        c.gridheight = 1;
1142
        c.gridwidth = 1;
1143
        c.gridx = 0;
1144
        c.gridy = GridBagConstraints.RELATIVE;
1145
        c.weightx = 0;
1146
        c.weighty = 0;
1147
        c.insets = new Insets(2, 0, 0, 0);
1148
 
1149
        GridBagConstraints cPanel = new GridBagConstraints();
1150
        cPanel.anchor = GridBagConstraints.NORTHWEST;
1151
        cPanel.fill = GridBagConstraints.HORIZONTAL;
1152
        cPanel.gridheight = 1;
1153
        cPanel.gridwidth = 1;
1154
        cPanel.gridx = 0;
1155
        cPanel.gridy = GridBagConstraints.RELATIVE;
1156
        cPanel.weightx = 0;
1157
        cPanel.weighty = 0;
1158
        cPanel.insets = new Insets(0, 0, 0, 0);
1159
 
1160
        panelLegende.setLayout(new GridBagLayout());
1161
        panelLegende.setBorder(BorderFactory.createTitledBorder("Légendes"));
1162
 
1163
        JPanel ecritureValidPanel = new JPanel();
1164
        ecritureValidPanel.setLayout(new GridBagLayout());
1165
        ecritureValidPanel.setBackground(Color.WHITE);
1166
        ecritureValidPanel.add(new JLabel("Ecritures validées"), cPanel);
1167
        panelLegende.add(ecritureValidPanel, c);
1168
 
1169
        JPanel ecritureNonValidPanel = new JPanel();
1170
        ecritureNonValidPanel.setLayout(new GridBagLayout());
19 ilm 1171
        ecritureNonValidPanel.setBackground(PointageRenderer.getCouleurEcritureNonValide());
18 ilm 1172
        ecritureNonValidPanel.add(new JLabel("Ecritures non validées"), cPanel);
1173
        panelLegende.add(ecritureNonValidPanel, c);
1174
 
1175
        JPanel ecritureNonValidTodayPanel = new JPanel();
1176
        ecritureNonValidTodayPanel.setLayout(new GridBagLayout());
1177
        ecritureNonValidTodayPanel.setBackground(PointageRenderer.getCouleurEcritureToDay());
1178
        ecritureNonValidTodayPanel.add(new JLabel("Ecritures non validées du jour"), cPanel);
1179
        panelLegende.add(ecritureNonValidTodayPanel, c);
1180
 
1181
        JPanel ecriturePointePanel = new JPanel();
1182
        ecriturePointePanel.setLayout(new GridBagLayout());
1183
        ecriturePointePanel.setBackground(PointageRenderer.getCouleurEcriturePointee());
1184
        ecriturePointePanel.add(new JLabel("Ecritures lettrées"), cPanel);
1185
        panelLegende.add(ecriturePointePanel, c);
1186
 
1187
        return panelLegende;
1188
    }
1189
}