OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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