OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.finance.accounting.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
19
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
20
import org.openconcerto.erp.core.finance.accounting.model.SommeCompte;
21
import org.openconcerto.erp.element.objet.Compte;
22
import org.openconcerto.erp.generationEcritures.GenerationMvtVirement;
23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.model.SQLBase;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLSystem;
29
import org.openconcerto.sql.model.SQLTable;
30
import org.openconcerto.sql.model.Where;
142 ilm 31
import org.openconcerto.sql.utils.SQLUtils;
80 ilm 32
import org.openconcerto.ui.DefaultGridBagConstraints;
18 ilm 33
import org.openconcerto.ui.JDate;
34
import org.openconcerto.ui.JLabelBold;
73 ilm 35
import org.openconcerto.utils.ExceptionHandler;
18 ilm 36
 
37
import java.awt.Component;
38
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
40
import java.awt.Insets;
41
import java.awt.event.ActionEvent;
42
import java.awt.event.ActionListener;
43
import java.beans.PropertyChangeEvent;
44
import java.beans.PropertyChangeListener;
45
import java.sql.SQLException;
46
import java.text.DateFormat;
47
import java.text.SimpleDateFormat;
48
import java.util.Calendar;
49
import java.util.Date;
50
import java.util.HashMap;
51
import java.util.List;
52
import java.util.Map;
53
 
54
import javax.swing.JButton;
55
import javax.swing.JCheckBox;
56
import javax.swing.JFrame;
57
import javax.swing.JLabel;
58
import javax.swing.JOptionPane;
59
import javax.swing.JPanel;
60
import javax.swing.JProgressBar;
61
import javax.swing.SwingUtilities;
62
 
63
import org.apache.commons.dbutils.handlers.ArrayListHandler;
64
 
65
public class CloturePanel extends JPanel {
66
    private final JDate dateOuv = new JDate();
67
    private final JDate dateFerm = new JDate();
68
    private SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
69
    private final SQLTable societe = Configuration.getInstance().getBase().getTable("SOCIETE_COMMON");
70
    private final SQLTable exercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON");
71
    private final SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
72
    private final SQLRow rowExercice = this.exercice.getRow(this.rowSociete.getInt("ID_EXERCICE_COMMON"));
73
    private final Map<Object, Long> mRAN = new HashMap<Object, Long>();
74
    private JButton valider = new JButton("Valider");
75
    private JButton annul = new JButton("Annuler");
142 ilm 76
    private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
18 ilm 77
    private JLabel opEnCours = new JLabel("Etat: en attente de validation");
80 ilm 78
    private JCheckBox boxValid = new JCheckBox("Je confirme avoir effectué toutes les opérations nécessaires.");
18 ilm 79
 
80
    private JProgressBar bar = new JProgressBar(0, 4);
81
 
82
    public CloturePanel() {
83
        this.setLayout(new GridBagLayout());
80 ilm 84
        final GridBagConstraints c = new DefaultGridBagConstraints();
18 ilm 85
        c.fill = GridBagConstraints.HORIZONTAL;
86
        c.anchor = GridBagConstraints.WEST;
87
        c.gridx = 0;
88
        c.gridy = 0;
89
        c.gridwidth = GridBagConstraints.REMAINDER;
90
        c.gridheight = 1;
91
        c.weightx = 0;
92
        c.weighty = 0;
93
 
94
        JLabel rappel = new JLabelBold("Opérations à effectuer avant de continuer: ");
95
        JLabel label = new JLabel("- report des charges et produits constatés d'avance");
96
        JLabel label2 = new JLabel("- report des charges à payer et produits à recevoir");
97
        JLabel label3 = new JLabel("- impression du bilan, compte de résultat, grand livre, journaux et balance");
180 ilm 98
        JLabel label5 = new JLabel("- génération des écritures comptables des payes");
18 ilm 99
        JLabel label4 = new JLabel("Il est préférable de réaliser une sauvegarde avant de continuer.");
100
 
101
        JLabel op = new JLabelBold("Opérations qui vont etre effectuées: ");
102
        JLabel labelValid = new JLabel("- validation de toutes les écritures concernant la période de l'exercice.");
103
        JLabel labelSoldeGestion = new JLabel("- soldes de tous les comptes de gestions 6* et 7*");
104
        JLabel labelSoldeBilan = new JLabel("- soldes de tous les comptes de bilan");
105
        JLabel labelAN = new JLabel("- report des à nouveaux");
106
 
107
        c.gridy = GridBagConstraints.RELATIVE;
108
        c.gridx = 0;
109
 
110
        // Date de l'ancien exercice
111
        Calendar dDebut = this.rowExercice.getDate("DATE_DEB");
112
        Calendar dFin = this.rowExercice.getDate("DATE_FIN");
113
        JLabel labelAncienExercice = new JLabel("Clôture de l'exercice du " + dateFormat.format(dDebut.getTime()) + " au " + dateFormat.format(dFin.getTime()));
114
        this.add(labelAncienExercice, c);
80 ilm 115
        c.insets = new Insets(10, 2, 1, 2);
18 ilm 116
        this.add(rappel, c);
117
        this.add(label, c);
118
        this.add(label2, c);
119
        this.add(label3, c);
120
        this.add(label5, c);
121
        this.add(label4, c);
122
 
123
        c.insets = new Insets(15, 2, 1, 2);
124
        this.add(op, c);
125
 
126
        c.insets = new Insets(10, 2, 1, 2);
127
        this.add(labelValid, c);
128
        this.add(labelSoldeGestion, c);
129
        this.add(labelSoldeBilan, c);
130
        this.add(labelAN, c);
131
 
132
        // Date du prochain exercice
133
        c.gridwidth = 1;
134
        c.gridy = GridBagConstraints.RELATIVE;
135
        c.gridx = 0;
136
        c.gridx = GridBagConstraints.RELATIVE;
137
        c.fill = GridBagConstraints.NONE;
138
 
139
        this.add(new JLabel("Date du nouvel exercice du "), c);
140
 
141
        dDebut.set(Calendar.YEAR, dDebut.get(Calendar.YEAR) + 1);
142
        this.dateOuv.setValue(dDebut.getTime());
143
        this.add(this.dateOuv, c);
144
        this.add(new JLabel("au"), c);
145
        dFin.set(Calendar.YEAR, dFin.get(Calendar.YEAR) + 1);
146
        this.dateFerm.setValue(dFin.getTime());
147
        this.add(this.dateFerm, c);
148
 
149
        c.gridx = 0;
150
        c.fill = GridBagConstraints.HORIZONTAL;
151
        c.gridwidth = 2;
152
        c.weightx = 1;
153
        this.add(this.opEnCours, c);
154
 
155
        c.gridwidth = 4;
156
        c.gridx = 0;
157
        c.weightx = 1;
158
        this.add(this.bar, c);
159
 
160
        //
161
        this.add(this.boxValid, c);
162
 
163
        // Button
164
        final JPanel buttonBar = new JPanel();
165
        buttonBar.add(this.valider);
166
        buttonBar.add(this.annul);
167
 
168
        c.fill = GridBagConstraints.NONE;
169
        c.anchor = GridBagConstraints.EAST;
170
        c.gridx = 0;
171
        this.add(buttonBar, c);
172
 
173
        final PropertyChangeListener listener = new PropertyChangeListener() {
174
            public void propertyChange(PropertyChangeEvent evt) {
175
                CloturePanel.this.valider.setEnabled(isDateValid());
176
            }
177
        };
178
        this.dateFerm.addValueListener(listener);
179
        this.dateOuv.addValueListener(listener);
180
 
181
        // TODO afficher le deroulement de etapes apres validation
182
 
183
        this.valider.addActionListener(new ActionListener() {
184
            public void actionPerformed(ActionEvent e) {
185
 
142 ilm 186
                valider.setEnabled(false);
187
                dateFerm.setEnabled(false);
188
                dateOuv.setEnabled(false);
189
 
190
                new Thread(new Runnable() {
191
                    public void run() {
192
                        try {
193
                            clotureExercice();
194
                            SwingUtilities.invokeLater(new Runnable() {
195
                                public void run() {
196
                                    // show OK works fine
197
                                    Component comp = SwingUtilities.getRoot(CloturePanel.this);
198
                                    JOptionPane.showMessageDialog(CloturePanel.this, "Exercice clôturé", "Fin de la clôture", JOptionPane.INFORMATION_MESSAGE);
199
                                    if (comp != null) {
200
                                        ((JFrame) comp).dispose();
201
                                    }
202
                                }
203
                            });
204
                        } catch (Exception ex) {
205
                            ExceptionHandler.handle("Erreur lors de la clôture", ex);
206
                        }
207
                    }
208
                }).start();
209
 
18 ilm 210
            }
211
        });
212
 
213
        this.valider.setEnabled(isDateValid());
214
 
215
        this.boxValid.addActionListener(new ActionListener() {
216
 
217
            @Override
218
            public void actionPerformed(ActionEvent e) {
219
                CloturePanel.this.valider.setEnabled(isDateValid());
220
            }
221
        });
222
 
223
        this.annul.addActionListener(new ActionListener() {
224
            public void actionPerformed(ActionEvent e) {
225
 
226
                ((JFrame) SwingUtilities.getRoot(CloturePanel.this)).dispose();
227
            }
228
        });
229
    }
230
 
231
    private boolean isDateValid() {
232
        final Date d = (Date) this.rowExercice.getObject("DATE_FIN");
132 ilm 233
        return this.boxValid.isSelected() && (((!this.dateFerm.isEmpty()) && (!this.dateOuv.isEmpty()) && (this.dateFerm.getValue().getTime() > this.dateOuv.getValue().getTime())
234
                && (this.dateOuv.getValue().getTime() > d.getTime())));
18 ilm 235
    }
236
 
237
    private final SQLTable tablePrefCompte = this.base.getTable("PREFS_COMPTE");
238
 
73 ilm 239
    private void clotureExercice() throws SQLException {
18 ilm 240
 
142 ilm 241
        final SQLRow rowPrefCompte = this.tablePrefCompte.getRow(2);
242
        final int id_Compte_Bilan_Ouverture;
243
        if (rowPrefCompte.getInt("ID_COMPTE_PCE_BILAN_O") <= 1) {
18 ilm 244
            id_Compte_Bilan_Ouverture = ComptePCESQLElement.getId("890");
142 ilm 245
        } else {
246
            id_Compte_Bilan_Ouverture = rowPrefCompte.getInt("ID_COMPTE_PCE_BILAN_O");
18 ilm 247
        }
248
 
142 ilm 249
        final int id_Compte_Bilan_Cloture;
250
        if (rowPrefCompte.getInt("ID_COMPTE_PCE_BILAN_F") <= 1) {
18 ilm 251
            id_Compte_Bilan_Cloture = ComptePCESQLElement.getId("891");
142 ilm 252
        } else {
253
            id_Compte_Bilan_Cloture = rowPrefCompte.getInt("ID_COMPTE_PCE_BILAN_F");
18 ilm 254
        }
255
 
256
        /*******************************************************************************************
257
         * Validation des écritures
258
         ******************************************************************************************/
259
        EcritureSQLElement.validationEcrituresBefore((Date) this.rowExercice.getObject("DATE_FIN"), true);
260
 
142 ilm 261
        SQLUtils.executeAtomic(this.tablePrefCompte.getDBSystemRoot().getDataSource(), new SQLUtils.SQLFactory<Object>() {
262
            @Override
263
            public Object create() throws SQLException {
18 ilm 264
 
142 ilm 265
                /*******************************************************************************************
266
                 * Solde des comptes de gestion 6* et 7* (génération du résultat)
267
                 ******************************************************************************************/
268
                SwingUtilities.invokeLater(new Runnable() {
269
                    public void run() {
270
                        CloturePanel.this.opEnCours.setText("En cours: solde des comptes 6 et 7");
271
                    }
272
                });
273
                long time = new Date().getTime();
274
                System.err.println("Start :: " + time);
275
                soldeCompte(false);
18 ilm 276
 
142 ilm 277
                /*******************************************************************************************
278
                 * Solde des autres comptes (comptes de bilan)
279
                 ******************************************************************************************/
280
                SwingUtilities.invokeLater(new Runnable() {
281
                    public void run() {
282
                        CloturePanel.this.opEnCours.setText("En cours: solde des comptes autres que 6 et 7");
283
                        CloturePanel.this.bar.setValue(1);
284
                    }
285
                });
286
                soldeCompte(true);
18 ilm 287
 
142 ilm 288
                long time2 = new Date().getTime();
289
                System.err.println("Stop :: " + time2);
290
                System.err.println("Time :: " + (time2 - time));
18 ilm 291
 
142 ilm 292
                /*******************************************************************************************
293
                 * Reouverture des comptes de bilan
294
                 ******************************************************************************************/
295
                SwingUtilities.invokeLater(new Runnable() {
296
                    public void run() {
297
                        CloturePanel.this.opEnCours.setText("En cours: report des à nouveaux");
298
                        CloturePanel.this.bar.setValue(2);
299
                    }
300
                });
301
                // transfert du compte bilan fermeture vers le compte bilan ouverture
302
                SQLTable ecritureTable = CloturePanel.this.base.getTable("ECRITURE");
303
                SQLTable compteTable = CloturePanel.this.base.getTable("COMPTE_PCE");
304
                SQLSelect sel = new SQLSelect(CloturePanel.this.base);
18 ilm 305
 
142 ilm 306
                sel.addSelect(compteTable.getKey());
307
                sel.addSelect(compteTable.getField("NUMERO"));
308
                sel.addSelect(compteTable.getField("NOM"));
309
                sel.addSelect(ecritureTable.getField("DEBIT"), "SUM");
310
                sel.addSelect(ecritureTable.getField("CREDIT"), "SUM");
18 ilm 311
 
142 ilm 312
                Where w = new Where(ecritureTable.getField("ID_COMPTE_PCE"), "=", id_Compte_Bilan_Cloture);
313
                w = w.and(new Where(ecritureTable.getField("ID_COMPTE_PCE"), "=", compteTable.getKey()));
314
                sel.setWhere(w);
18 ilm 315
 
142 ilm 316
                String req = sel.asString() + " GROUP BY \"COMPTE_PCE\".\"ID\", \"COMPTE_PCE\".\"NUMERO\", \"COMPTE_PCE\".\"NOM\" ORDER BY \"COMPTE_PCE\".\"NUMERO\"";
317
                System.out.println(req);
18 ilm 318
 
142 ilm 319
                Object ob = CloturePanel.this.base.getDataSource().execute(req, new ArrayListHandler());
18 ilm 320
 
142 ilm 321
                List myList = (List) ob;
18 ilm 322
 
142 ilm 323
                if (myList.size() != 0) {
324
                    GenerationMvtVirement gen = new GenerationMvtVirement(1, id_Compte_Bilan_Cloture, 0, 0, "Fermeture du compte ", CloturePanel.this.rowExercice.getDate("DATE_FIN").getTime(),
325
                            JournalSQLElement.OD, "Fermeture des comptes");
149 ilm 326
                    gen.setFermeture(true);
142 ilm 327
                    for (int i = 0; i < myList.size(); i++) {
18 ilm 328
 
142 ilm 329
                        Object[] objTmp = (Object[]) myList.get(i);
330
                        Compte cptTmp = new Compte(((Number) objTmp[0]).intValue(), objTmp[1].toString(), objTmp[2].toString(), "", ((Number) objTmp[3]).longValue(), ((Number) objTmp[4]).longValue());
331
                        // vecteurCompte.add(cptTmp);
332
 
333
                        long solde = cptTmp.getTotalDebit() - cptTmp.getTotalCredit();
334
 
335
                        if (solde != 0) {
336
                            if (solde > 0) {
337
                                gen.setValues(cptTmp.getId(), id_Compte_Bilan_Cloture, 0, Math.abs(solde), "Fermeture du compte " + cptTmp.getNumero(),
338
                                        CloturePanel.this.rowExercice.getDate("DATE_FIN").getTime(), JournalSQLElement.OD, false);
339
 
340
                            } else {
341
                                gen.setValues(cptTmp.getId(), id_Compte_Bilan_Cloture, Math.abs(solde), 0, "Fermeture du compte " + cptTmp.getNumero(),
342
                                        CloturePanel.this.rowExercice.getDate("DATE_FIN").getTime(), JournalSQLElement.OD, false);
343
                            }
344
                            gen.genereMouvement();
345
                        }
346
                    }
347
                }
348
                /*******************************************************************************************
349
                 * Validation des écritures de clotures
350
                 ******************************************************************************************/
351
                SwingUtilities.invokeLater(new Runnable() {
352
                    public void run() {
353
                        CloturePanel.this.opEnCours.setText("En cours: validation des écritures de l'exercice");
354
                        CloturePanel.this.bar.setValue(3);
355
                    }
356
                });
357
                EcritureSQLElement.validationEcrituresBefore((Date) CloturePanel.this.rowExercice.getObject("DATE_FIN"), true);
358
 
359
                // A nouveaux
360
                Object[] compteAnouveau = CloturePanel.this.mRAN.keySet().toArray();
149 ilm 361
                int journalAN = JournalSQLElement.OD;
362
                if (rowPrefCompte.getObject("ID_JOURNAL_AN") != null && !rowPrefCompte.isForeignEmpty("ID_JOURNAL_AN")) {
363
                    journalAN = rowPrefCompte.getForeignID("ID_JOURNAL_AN");
364
                }
365
                GenerationMvtVirement genAnouveaux = new GenerationMvtVirement(id_Compte_Bilan_Ouverture, 1, 0, 0, "A nouveaux", CloturePanel.this.dateOuv.getValue(), journalAN, "A nouveaux");
366
                genAnouveaux.setOuverture(true);
142 ilm 367
                for (int i = 0; i < CloturePanel.this.mRAN.keySet().size(); i++) {
368
 
369
                    long solde = CloturePanel.this.mRAN.get(compteAnouveau[i]).longValue();
370
 
371
                    // if (solde != 0) {
18 ilm 372
                    if (solde > 0) {
142 ilm 373
                        genAnouveaux.setValues(id_Compte_Bilan_Ouverture, Integer.parseInt(compteAnouveau[i].toString()), 0, Math.abs(solde), "A nouveaux", CloturePanel.this.dateOuv.getValue(),
149 ilm 374
                                journalAN, false);
18 ilm 375
                    } else {
142 ilm 376
                        genAnouveaux.setValues(id_Compte_Bilan_Ouverture, Integer.parseInt(compteAnouveau[i].toString()), Math.abs(solde), 0, "A nouveaux", CloturePanel.this.dateOuv.getValue(),
149 ilm 377
                                journalAN, false);
18 ilm 378
                    }
142 ilm 379
                    genAnouveaux.genereMouvement();
380
                    // }
18 ilm 381
                }
382
 
142 ilm 383
                // Fixé la nouvel date de l'exercice
384
                SQLRowValues valsExercice = new SQLRowValues(CloturePanel.this.exercice);
385
                valsExercice.put("CLOTURE", Boolean.TRUE);
386
                valsExercice.update(CloturePanel.this.rowExercice.getID());
18 ilm 387
 
142 ilm 388
                // Creation d'un nouvel exercice
389
                valsExercice.put("CLOTURE", Boolean.FALSE);
390
                valsExercice.put("DATE_DEB", new java.sql.Date(CloturePanel.this.dateOuv.getValue().getTime()));
391
                valsExercice.put("DATE_FIN", new java.sql.Date(CloturePanel.this.dateFerm.getValue().getTime()));
392
                valsExercice.put("ID_SOCIETE_COMMON", CloturePanel.this.rowSociete.getID());
393
                SQLRow rowNewEx = valsExercice.insert();
18 ilm 394
 
142 ilm 395
                // mise a jour de l'exercice de la societe
396
                SQLRowValues rowValsSociete = new SQLRowValues(CloturePanel.this.societe);
397
                rowValsSociete.put("ID_EXERCICE_COMMON", rowNewEx.getID());
398
                rowValsSociete.update(CloturePanel.this.rowSociete.getID());
399
                // Recharge les informations de la societe pour pointer sur le nouvel exercice
400
                ComptaPropsConfiguration.getInstanceCompta().getRowSociete().fetchValues();
401
                SwingUtilities.invokeLater(new Runnable() {
402
                    public void run() {
403
                        CloturePanel.this.bar.setValue(4);
404
                        CloturePanel.this.opEnCours.setText("Etat: clôture terminée");
405
                    }
406
                });
407
 
408
                return null;
18 ilm 409
            }
142 ilm 410
        });
18 ilm 411
    }
412
 
73 ilm 413
    private void soldeCompte(boolean compteBilan) throws SQLException {
18 ilm 414
 
415
        String typeCompte;
416
        int compteDest;
417
 
418
        SQLRow rowPrefCompte = this.tablePrefCompte.getRow(2);
419
        if (compteBilan) {
420
            typeCompte = "^[^6-8].*$";
421
            compteDest = rowPrefCompte.getInt("ID_COMPTE_PCE_BILAN_F");
422
            if (compteDest <= 1) {
423
                compteDest = ComptePCESQLElement.getId("891", "Bilan de clôture");
424
            }
425
        } else {
426
            SommeCompte s = new SommeCompte();
427
            long solde6 = s.soldeCompte(6, 6, true, this.rowExercice.getDate("DATE_DEB").getTime(), this.rowExercice.getDate("DATE_FIN").getTime());
428
            long solde7 = s.soldeCompte(7, 7, true, this.rowExercice.getDate("DATE_DEB").getTime(), this.rowExercice.getDate("DATE_FIN").getTime());
429
            long resultat = -solde7 - solde6;
430
            System.err.println("Solde Résultat :::: " + solde7 + " __ " + solde6 + "__" + (solde7 - solde6));
431
            typeCompte = "^(6|7).*$";
432
 
433
            if (resultat > 0) {
434
                compteDest = rowPrefCompte.getInt("ID_COMPTE_PCE_RESULTAT");
435
            } else {
436
                compteDest = rowPrefCompte.getInt("ID_COMPTE_PCE_RESULTAT_PERTE");
437
            }
438
 
439
            if (compteDest <= 1) {
440
                if (resultat > 0) {
441
                    compteDest = ComptePCESQLElement.getId("120", "Résultat de l'exercice (bénéfice)");
442
                } else {
443
                    compteDest = ComptePCESQLElement.getId("129", "Résultat de l'exercice (perte)");
444
                }
445
            }
446
        }
447
 
448
        // on récupére les comptes avec leurs totaux
449
        SQLTable ecritureTable = this.base.getTable("ECRITURE");
450
        SQLTable compteTable = this.base.getTable("COMPTE_PCE");
451
        SQLSelect sel = new SQLSelect(this.base);
452
 
453
        sel.addSelect(compteTable.getKey());
454
        sel.addSelect(compteTable.getField("NUMERO"));
455
        sel.addSelect(compteTable.getField("NOM"));
456
        sel.addSelect(ecritureTable.getField("DEBIT"), "SUM");
457
        sel.addSelect(ecritureTable.getField("CREDIT"), "SUM");
458
 
459
        Where w = new Where(ecritureTable.getField("ID_COMPTE_PCE"), "=", compteTable.getKey());
460
 
461
        String function = "REGEXP";
462
        if (Configuration.getInstance().getBase().getServer().getSQLSystem() == SQLSystem.POSTGRESQL) {
28 ilm 463
            // function = "SIMILAR TO";
464
            // typeCompte = typeCompte.replace(".*", "%");
465
            function = "~";
18 ilm 466
        }
467
 
468
        Where w2 = new Where(compteTable.getField("NUMERO"), function, typeCompte);
469
        Where w3 = new Where(ecritureTable.getField("DATE"), "<=", this.rowExercice.getObject("DATE_FIN"));
470
        sel.setWhere(w.and(w2).and(w3));
471
 
472
        String req = sel.asString() + " GROUP BY \"COMPTE_PCE\".\"ID\", \"COMPTE_PCE\".\"NUMERO\", \"COMPTE_PCE\".\"NOM\" ORDER BY \"COMPTE_PCE\".\"NUMERO\"";
473
        System.err.println(req);
474
 
475
        Object ob = this.base.getDataSource().execute(req, new ArrayListHandler());
476
 
477
        List myList = (List) ob;
478
 
479
        if (myList != null && myList.size() != 0) {
149 ilm 480
            boolean genSoldeNul = true;
481
            if (rowPrefCompte.getObject("CREATE_NUL_SOLDE_ECR") != null) {
482
                genSoldeNul = rowPrefCompte.getBoolean("CREATE_NUL_SOLDE_ECR");
483
            }
18 ilm 484
            GenerationMvtVirement genFerm = new GenerationMvtVirement(1, compteDest, 0, 0, "Fermeture du compte ", this.rowExercice.getDate("DATE_FIN").getTime(), JournalSQLElement.OD,
485
                    "Fermeture des comptes");
149 ilm 486
            genFerm.setFermeture(true);
18 ilm 487
            for (int i = 0; i < myList.size(); i++) {
488
 
489
                Object[] objTmp = (Object[]) myList.get(i);
490
                Compte cptTmp = new Compte(((Number) objTmp[0]).intValue(), objTmp[1].toString(), objTmp[2].toString(), "", ((Number) objTmp[3]).longValue(), ((Number) objTmp[4]).longValue());
491
 
492
                long solde = cptTmp.getTotalDebit() - cptTmp.getTotalCredit();
493
 
149 ilm 494
                if (genSoldeNul || solde != 0) {
495
                    if (compteBilan) {
496
                        this.mRAN.put(objTmp[0], Long.valueOf(solde));
497
                    }
18 ilm 498
 
149 ilm 499
                    if (solde > 0) {
500
                        genFerm.setValues(cptTmp.getId(), compteDest, 0, Math.abs(solde), "Fermeture du compte " + cptTmp.getNumero(), this.rowExercice.getDate("DATE_FIN").getTime(),
501
                                JournalSQLElement.OD, false);
502
                    } else {
18 ilm 503
 
149 ilm 504
                        genFerm.setValues(cptTmp.getId(), compteDest, Math.abs(solde), 0, "Fermeture du compte " + cptTmp.getNumero(), this.rowExercice.getDate("DATE_FIN").getTime(),
505
                                JournalSQLElement.OD, false);
506
                    }
507
                    genFerm.genereMouvement();
18 ilm 508
                }
509
            }
510
            // }
511
        }
512
    }
513
}