OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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.report;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.generationDoc.SheetInterface;
19
import org.openconcerto.erp.generationDoc.SheetXml;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.openconcerto.erp.rights.ComptaUserRight;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
26
import org.openconcerto.sql.model.SQLSelect;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.users.UserManager;
30
import org.openconcerto.utils.GestionDevise;
31
import org.openconcerto.utils.Tuple2;
32
import org.openconcerto.utils.cc.ITransformer;
33
 
34
import java.io.File;
35
import java.text.DateFormat;
36
import java.util.ArrayList;
37
import java.util.Calendar;
38
import java.util.Date;
39
import java.util.HashMap;
40
import java.util.List;
41
import java.util.Map;
42
 
43
import org.apache.commons.dbutils.handlers.ArrayListHandler;
44
 
45
public class GrandLivreSheet extends SheetInterface {
46
 
47
    private static int debutFill, endFill;
48
    public static int MODEALL = 1;
49
    public static int MODELETTREE = 2;
50
    public static int MODENONLETTREE = 3;
51
    private final static SQLTable tableEcriture = base.getTable("ECRITURE");
52
    private final static SQLTable tableJournal = base.getTable("JOURNAL");
53
    private final static SQLTable tableMvt = base.getTable("MOUVEMENT");
54
    private final static SQLTable tableCompte = base.getTable("COMPTE_PCE");
55
 
56
    private final static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
57
    private final static DateFormat dateFormatEcr = DateFormat.getDateInstance(DateFormat.SHORT);
58
    private SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
59
 
60
    private Date dateDu, dateAu;
61
    private String compteDeb, compteEnd;
62
    private int lettrage;
63
    private boolean cumul = false;
64
    private boolean excludeCompteSolde = true;
65
    private boolean centralClient = false;
66
    private boolean centralFourn = false;
67
 
68
    public static void setSize(int debut, int fin) {
69
        debutFill = debut;
70
        endFill = fin;
71
    }
72
 
73
    static {
74
        setSize(7, 69);
75
    }
76
 
77
    private static final Tuple2<String, String> tuple = Tuple2.create("LocationGrandLivre", "Grand Livre");
78
 
79
    public static Tuple2<String, String> getTuple2Location() {
80
        return tuple;
81
    }
82
 
83
    public GrandLivreSheet(Date du, Date au, String compteDep, String compteEnd, int lettrage, boolean cumul, boolean excludeCptSolde, boolean centralClient, boolean centralFourn) {
84
        super();
85
        Calendar cal = Calendar.getInstance();
86
        cal.setTime(au);
87
        this.nbRowsPerPage = 72;
88
        this.printer = PrinterNXProps.getInstance().getStringProperty("GrandLivrePrinter");
89
        this.modele = "GrandLivre.ods";
90
        this.locationOO = SheetXml.getLocationForTuple(tuple, false) + File.separator + cal.get(Calendar.YEAR);
91
        this.locationPDF = SheetXml.getLocationForTuple(tuple, true) + File.separator + cal.get(Calendar.YEAR);
92
        this.dateAu = au;
93
        this.dateDu = du;
94
        this.compteDeb = compteDep.trim();
95
        this.compteEnd = compteEnd.trim();
96
        this.lettrage = lettrage;
97
        this.cumul = cumul;
98
        this.excludeCompteSolde = excludeCptSolde;
99
        this.centralClient = centralClient;
100
        this.centralFourn = centralFourn;
101
 
102
        createMap();
103
    }
104
 
105
    private String toDay = dateFormat.format(new Date());
106
 
107
    private void makeEntete(int rowDeb) {
108
 
109
        this.mCell.put("A" + rowDeb, this.rowSociete.getObject("NOM"));
110
        this.mCell.put("G" + rowDeb, "Edition du " + this.toDay);
111
        // this.mCell.put("D" + (rowDeb + 2), "Grand livre");
112
        // System.err.println("MAKE ENTETE");
113
    }
114
 
115
    private void makePiedPage(int row, String comptes) {
116
        this.mCell.put("A" + row, "Compte : " + comptes);
117
        this.mCell.put("E" + row, "Période du " + dateFormatEcr.format(this.dateDu) + " au " + dateFormatEcr.format(this.dateAu));
118
    }
119
 
120
    private void makeSousTotal(int row, long debit, long credit) {
121
        this.mapStyleRow.put(new Integer(row), "Titre 1");
122
 
123
        this.mCell.put("A" + row, "");
124
        this.mCell.put("B" + row, "");
125
        this.mCell.put("C" + row, "");
126
        this.mCell.put("D" + row, "Sous total");
127
        this.mCell.put("E" + row, Double.valueOf(GestionDevise.currencyToString(debit, false)));
128
        this.mCell.put("F" + row, Double.valueOf(GestionDevise.currencyToString(credit, false)));
129
        this.mCell.put("G" + row, Double.valueOf(GestionDevise.currencyToString(debit - credit, false)));
130
    }
131
 
132
    protected void createMap() {
133
        Date d = new Date();
134
        this.mapReplace = new HashMap();
135
        this.mCell = new HashMap<String, Object>();
136
        this.mapStyleRow = new HashMap<Integer, String>();
137
 
138
        final SQLRowValues vals = new SQLRowValues(tableEcriture);
139
        vals.put("ID_COMPTE_PCE", null);
140
        vals.put("COMPTE_NUMERO", null);
141
        vals.put("COMPTE_NOM", null);
142
        vals.put("ID_JOURNAL", null);
143
        vals.put("JOURNAL_CODE", null);
144
        vals.putRowValues("ID_MOUVEMENT").put("NUMERO", null);
145
        vals.put("CREDIT", null);
146
        vals.put("DEBIT", null);
147
        vals.put("DATE", null);
148
        vals.put("NOM", null);
149
 
150
        final List<Integer> lCompteSolde;
151
        if (GrandLivreSheet.this.excludeCompteSolde) {
152
            lCompteSolde = getListeCompteSolde();
153
        } else {
154
            lCompteSolde = null;
155
        }
156
        Map<Integer, Long> mapCumul = getCumulsAnterieur(GrandLivreSheet.this.dateDu, lCompteSolde);
157
 
158
        final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(vals);
159
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
160
            @Override
161
            public SQLSelect transformChecked(SQLSelect sel) {
162
 
163
                Where w = (new Where(tableEcriture.getField("DATE"), GrandLivreSheet.this.dateDu, GrandLivreSheet.this.dateAu));
164
 
165
                if (GrandLivreSheet.this.compteDeb.equals(GrandLivreSheet.this.compteEnd)) {
166
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", GrandLivreSheet.this.compteDeb));
167
                } else {
168
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) GrandLivreSheet.this.compteDeb, (Object) GrandLivreSheet.this.compteEnd));
169
                }
170
 
171
                w = w.and(new Where(tableEcriture.getField("ID_MOUVEMENT"), "=", tableMvt.getField("ID")));
172
 
173
                if (GrandLivreSheet.this.lettrage == MODELETTREE) {
174
                    Object o = null;
175
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
176
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
177
                } else {
178
                    if (GrandLivreSheet.this.lettrage == MODENONLETTREE) {
179
                        Object o = null;
180
                        Where w2 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
181
                        w = w.and(w2.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
182
                    }
183
                }
184
 
185
                if (GrandLivreSheet.this.excludeCompteSolde) {
186
                    System.err.println("Exclude compte");
187
 
188
                    w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), lCompteSolde).not());
189
                }
190
 
191
                if (!UserManager.getInstance().getCurrentUser().getRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
192
                    // TODO Show Restricted acces in UI
193
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "LIKE", "411%"));
194
                }
195
 
196
                sel.setWhere(w);
197
                sel.addRawOrder("\"ECRITURE\".\"COMPTE_NUMERO\"");
198
                sel.addRawOrder("\"ECRITURE\".\"DATE\"");
199
                sel.addRawOrder("\"MOUVEMENT\".\"NUMERO\"");
200
                System.err.println(sel.asString());
201
                return sel;
202
            }
203
        });
204
 
205
        List<SQLRowValues> list = fetcher.fetch();
206
 
207
        int posLine = 1;
208
        int firstLine = 1;
209
        System.err.println("START CREATE Grand livre, NB ecritures  " + list.size());
210
        this.nbPage = 0;
211
        long totalDebit, totalCredit, sousTotalDebit, sousTotalCredit, totalCreditAntC, totalDebitAntC, totalCreditAntF, totalDebitAntF;
212
 
213
        totalDebit = 0;
214
        totalCredit = 0;
215
        sousTotalCredit = 0;
216
        sousTotalDebit = 0;
217
        totalCreditAntC = 0;
218
        totalDebitAntC = 0;
219
        totalCreditAntF = 0;
220
        totalDebitAntF = 0;
221
        SQLRowValues rowFirstEcr = null;
222
        int idCptFirstEcr = 1;
223
 
224
        boolean setTitle = true;
225
        boolean setLine = false;
226
        boolean setCumuls = true;
227
        boolean firstEcrCentC = true;
228
        boolean firstEcrCentF = true;
229
        String numCptFirstEcr = "411";
230
        String numCptClient = "411";
231
        String nomCptClient = "Clients";
232
        String numCptFourn = "401";
233
        String nomCptFourn = "Fournisseurs";
234
        int idCptClient = ComptePCESQLElement.getId(numCptClient, nomCptClient);
235
        int idCptFourn = ComptePCESQLElement.getId(numCptFourn, nomCptFourn);
236
 
237
        final String titre3 = "Titre 3";
238
        final String cumulAntString = "Cumuls antérieurs";
239
        int j = 0;
240
        for (int i = 0; i < list.size();) {
241
 
242
            System.err.println("START NEW PAGE; POS : " + posLine);
243
 
244
            /***************************************************************************************
245
             * ENTETE
246
             **************************************************************************************/
247
            makeEntete(posLine);
248
            posLine += debutFill - 1;
249
 
250
            // Affiche le nom du compte
251
            setTitle = true;
252
            // ligne vide avant de mettre le setTitle
253
            setLine = false;
254
 
255
            // setCumuls = false;
256
            /***************************************************************************************
257
             * CONTENU
258
             **************************************************************************************/
259
            final Double doubleZero = Double.valueOf("0");
260
            for (j = 0; (j < endFill - debutFill + 1) && i < list.size(); j++) {
261
 
262
                SQLRowValues rowEcr = list.get(i);
263
 
264
                int idCpt = rowEcr.getInt("ID_COMPTE_PCE");
265
                String nomCpt = rowEcr.getString("COMPTE_NOM");
266
                String numCpt = rowEcr.getString("COMPTE_NUMERO");
267
 
268
                // Cumuls antérieurs
269
                if (setCumuls && this.cumul && !setTitle) {
270
 
271
                    this.mapStyleRow.put(Integer.valueOf(posLine), titre3);
272
                    this.mCell.put("A" + posLine, "");
273
                    this.mCell.put("B" + posLine, "");
274
                    this.mCell.put("C" + posLine, "");
275
 
276
                    this.mCell.put("D" + posLine, cumulAntString);
277
                    Long longSolde = mapCumul.get(idCpt);
278
 
279
                    if (longSolde == null) {
280
                        longSolde = Long.valueOf(0);
281
                    }
282
                    long debitCumulAnt = 0;
283
                    long creditCumulAnt = 0;
284
 
285
                    if (longSolde > 0) {
286
                        debitCumulAnt = longSolde;
287
                    } else {
288
                        creditCumulAnt = -longSolde;
289
                    }
290
                    this.mCell.put("E" + posLine, (debitCumulAnt == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(debitCumulAnt, false)));
291
                    this.mCell.put("F" + posLine, (creditCumulAnt == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(creditCumulAnt, false)));
292
                    this.mCell.put("G" + posLine, (longSolde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(longSolde, false)));
293
 
294
                    totalCredit += creditCumulAnt;
295
                    totalDebit += debitCumulAnt;
296
 
297
                    sousTotalCredit += creditCumulAnt;
298
                    sousTotalDebit += debitCumulAnt;
299
                    setCumuls = false;
300
                } else {
301
                    // Titre
302
                    if (setTitle) {
303
                        if (!setLine) {
304
                            this.mapStyleRow.put(new Integer(posLine), "Titre 1");
305
 
306
                            // Si on centralise les comptes clients ou fournisseurs on affiche le
307
                            // compte 401 ou 411
308
                            if (this.centralClient && nomCpt.startsWith("411")) {
309
                                nomCpt = nomCptClient;
310
                                numCpt = numCptClient;
311
                                idCpt = idCptClient;
312
                            }
313
                            if (this.centralFourn && nomCpt.startsWith("401")) {
314
                                nomCpt = nomCptFourn;
315
                                numCpt = numCptFourn;
316
                                idCpt = idCptFourn;
317
                            }
318
                            this.mCell.put("A" + posLine, numCpt);
319
                            this.mCell.put("B" + posLine, nomCpt);
320
                            this.mCell.put("C" + posLine, "");
321
                            this.mCell.put("D" + posLine, "");
322
                            this.mCell.put("E" + posLine, "");
323
                            this.mCell.put("F" + posLine, "");
324
                            this.mCell.put("G" + posLine, "");
325
                            setTitle = false;
326
                            setLine = true;
327
 
328
                            if (rowFirstEcr == null) {
329
                                rowFirstEcr = rowEcr;
330
                                idCptFirstEcr = rowEcr.getInt("ID_COMPTE_PCE");
331
                                numCptFirstEcr = rowEcr.getString("COMPTE_NUMERO");
332
                            }
333
 
334
                        } else {
335
                            this.mapStyleRow.put(new Integer(posLine), "Normal");
336
                            setLine = false;
337
                        }
338
                    } else {
339
 
340
                        // si on change de compte alors on applique le style Titre 1
341
                        if (rowFirstEcr != null && idCptFirstEcr != idCpt && (!this.centralFourn || (!(numCptFirstEcr.startsWith("401") && numCpt.startsWith("401"))))
342
                                && (!this.centralClient || (!(numCptFirstEcr.startsWith("411") && numCpt.startsWith("411"))))) {
343
 
344
                            rowFirstEcr = rowEcr;
345
                            idCptFirstEcr = rowFirstEcr.getInt("ID_COMPTE_PCE");
346
                            numCptFirstEcr = rowEcr.getString("COMPTE_NUMERO");
347
                            makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
348
 
349
                            sousTotalCredit = 0;
350
                            sousTotalDebit = 0;
351
                            setTitle = true;
352
                            setCumuls = true;
353
                        } else {
354
                            long cred = rowEcr.getLong("CREDIT");
355
                            long deb = rowEcr.getLong("DEBIT");
356
                            // Centralisation fournisseur
357
                            if (this.centralFourn && numCpt.startsWith("401")) {
358
                                i++;
359
 
360
                                if (firstEcrCentF) {
361
                                    posLine++;
362
                                    this.mCell.put("D" + (posLine - 1), "Centralisation des comptes fournisseurs");
363
                                    this.mapStyleRow.put(new Integer((posLine - 1)), "Normal");
364
                                    firstEcrCentF = false;
365
                                } else {
366
                                    j--;
367
                                }
368
 
369
                                totalCreditAntF += cred;
370
                                totalDebitAntF += deb;
371
                                sousTotalCredit += cred;
372
                                sousTotalDebit += deb;
373
                                long solde = totalDebitAntF - totalCreditAntF;
374
                                this.mCell.put("E" + (posLine - 1), (totalDebitAntF == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebitAntF, false)));
375
                                this.mCell.put("F" + (posLine - 1), (totalCreditAntF == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalCreditAntF, false)));
376
                                this.mCell.put("G" + (posLine - 1), (solde == 0) ? doubleZero : new Double(GestionDevise.currencyToString(solde, false)));
377
 
378
                                continue;
379
                            }
380
                            // Centralisation client
381
                            if (this.centralClient && numCpt.startsWith("411")) {
382
                                i++;
383
                                if (firstEcrCentC) {
384
                                    posLine++;
385
                                    this.mCell.put("D" + (posLine - 1), "Centralisation des comptes clients");
386
                                    this.mapStyleRow.put(new Integer((posLine - 1)), "Normal");
387
                                    firstEcrCentC = false;
388
                                } else {
389
                                    j--;
390
                                }
391
 
392
                                totalCreditAntC += cred;
393
                                totalDebitAntC += deb;
394
                                sousTotalCredit += cred;
395
                                sousTotalDebit += deb;
396
                                long solde = totalDebitAntC - totalCreditAntC;
397
                                this.mCell.put("E" + (posLine - 1), (totalDebitAntC == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(totalDebitAntC, false)));
398
                                this.mCell.put("F" + (posLine - 1), (totalCreditAntC == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(totalCreditAntC, false)));
399
                                this.mCell.put("G" + (posLine - 1), (solde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(solde, false)));
400
 
401
                                continue;
402
                            }
403
 
404
                            this.mCell.put("A" + posLine, dateFormatEcr.format((Date) rowEcr.getObject("DATE")));
405
 
406
                            this.mCell.put("B" + posLine, rowEcr.getString("JOURNAL_CODE"));
407
                            this.mCell.put("C" + posLine, rowEcr.getForeign("ID_MOUVEMENT").getObject("NUMERO"));
408
                            this.mCell.put("D" + posLine, rowEcr.getObject("NOM"));
409
 
410
                            totalCredit += cred;
411
                            totalDebit += deb;
412
 
413
                            sousTotalCredit += cred;
414
                            sousTotalDebit += deb;
415
                            long solde = sousTotalDebit - sousTotalCredit;
416
 
417
                            this.mCell.put("E" + posLine, (deb == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(deb, false)));
418
                            this.mCell.put("F" + posLine, (cred == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(cred, false)));
419
                            this.mCell.put("G" + posLine, (solde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(solde, false)));
420
 
421
                            this.mapStyleRow.put(Integer.valueOf(posLine), "Normal");
422
                            i++;
423
                        }
424
                    }
425
                }
426
                posLine++;
427
            }
428
 
429
            if (i >= list.size() && j < endFill - debutFill + 1) {
430
                makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
431
            }
432
 
433
            posLine = firstLine + endFill;
434
            /*
435
             * if (this.mapStyleRow.get(new Integer(posLine - 1)) != null) {
436
             * this.mapStyleRow.put(new Integer(posLine - 1), "Titre 2"); }
437
             */
438
 
439
            // Total
440
            this.mCell.put("E" + posLine, (totalDebit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebit, false)));
441
            this.mCell.put("F" + posLine, (totalCredit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalCredit, false)));
442
            this.mCell.put("G" + posLine, (totalDebit - totalCredit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
443
 
444
            posLine += 2;
445
 
446
            // bas de page
447
            makePiedPage(posLine, this.compteDeb + " à " + this.compteEnd);
448
 
449
            posLine++;
450
            firstLine = posLine;
451
            this.nbPage++;
452
 
453
            if (i >= list.size() && j >= (endFill - debutFill + 1)) {
454
 
455
                makeEntete(posLine);
456
                posLine += debutFill - 1;
457
                makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
458
                this.nbPage++;
459
            }
460
 
461
        }
462
 
463
        // on conserve la page d'origine du model
464
        if (this.nbPage > 0) {
465
            this.nbPage--;
466
        }
467
 
468
        Date end = new Date();
469
        System.err.println("///////// TAKE " + (end.getTime() - d.getTime()) + " millisecondes TO CREATE MAP");
470
    }
471
 
472
    private List<Integer> getListeCompteSolde() {
473
        SQLSelect sel = new SQLSelect(base);
474
 
475
        sel.addSelect(tableCompte.getField("ID"));
476
        sel.addSelect(tableEcriture.getField("DEBIT"), "SUM");
477
        sel.addSelect(tableEcriture.getField("CREDIT"), "SUM");
478
 
479
        Where w;
480
        if (this.compteDeb.equals(this.compteEnd)) {
481
            w = new Where(tableCompte.getField("NUMERO"), "=", this.compteDeb);
482
        } else {
483
            w = new Where(tableCompte.getField("NUMERO"), (Object) this.compteDeb, (Object) this.compteEnd);
484
        }
485
 
486
        w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), "=", tableCompte.getField("ID")));
487
 
488
        if (this.cumul) {
489
            w = w.and(new Where(tableEcriture.getField("DATE"), "<=", this.dateAu));
490
        } else {
491
            w = w.and(new Where(tableEcriture.getField("DATE"), this.dateDu, this.dateAu));
492
        }
493
 
494
        if (this.lettrage == MODELETTREE) {
495
            Object o = null;
496
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
497
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
498
        } else {
499
            if (this.lettrage == MODENONLETTREE) {
500
                Object o = null;
501
                Where w2 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
502
                w = w.and(w2.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
503
 
504
            }
505
        }
506
 
507
        sel.setWhere(w);
508
 
509
        String req = sel.asString() + " GROUP BY \"COMPTE_PCE\".\"ID\"";
510
        System.err.println(req);
511
        List<Object[]> l = (List) base.getDataSource().execute(req, new ArrayListHandler());
512
        List<Integer> list = new ArrayList<Integer>();
513
        for (Object[] o : l) {
514
            long credit = 0;
515
            if (o[2] != null) {
516
                credit = Long.valueOf(o[2].toString());
517
            }
518
 
519
            long debit = 0;
520
            if (o[1] != null) {
521
                debit = Long.valueOf(o[1].toString());
522
            }
523
 
524
            int id = Integer.valueOf(o[0].toString());
525
            long solde = debit - credit;
526
            if (solde == 0) {
527
                list.add(id);
528
            }
529
        }
530
        return list;
531
    }
532
 
533
    /**
534
     * @param d date limite des cumuls
535
     * @return Map<Integer id compte, Long solde(debit-credit)>
536
     */
537
    private Map<Integer, Long> getCumulsAnterieur(Date d, List<Integer> listCompteSolde) {
538
        SQLSelect sel = new SQLSelect(base);
539
 
540
        sel.addSelect(tableEcriture.getField("ID_COMPTE_PCE"));
541
        sel.addSelect(tableEcriture.getField("DEBIT"), "SUM");
542
        sel.addSelect(tableEcriture.getField("CREDIT"), "SUM");
543
        sel.addSelect(tableEcriture.getField("COMPTE_NUMERO"));
544
        // sel.addSelect(tableEcriture.getField("ID_MOUVEMENT"));
545
        Where w = (new Where(tableEcriture.getField("DATE"), "<", d));
546
        w = w.and(new Where(tableEcriture.getField("ID_MOUVEMENT"), "=", tableMvt.getKey()));
547
 
548
        if (this.compteDeb.equals(this.compteEnd)) {
549
            w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", this.compteDeb));
550
        } else {
551
            w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) this.compteDeb, (Object) this.compteEnd));
552
        }
553
 
554
        if (this.lettrage == MODELETTREE) {
555
            Object o = null;
556
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
557
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
558
        } else {
559
            if (this.lettrage == MODENONLETTREE) {
560
                Object o = null;
561
                Where w2 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
562
                w = w.and(w2.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
563
            }
564
        }
565
 
566
        w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), "=", tableCompte.getField("ID")));
567
 
568
        if (listCompteSolde != null) {
569
            w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), listCompteSolde).not());
570
        }
571
 
572
        sel.setWhere(w);
573
 
574
        String req = sel.asString() + " GROUP BY \"ECRITURE\".\"ID_COMPTE_PCE\", \"ECRITURE\".\"COMPTE_NUMERO\"";
575
        System.err.println(req);
576
        List<Object[]> l = (List) base.getDataSource().execute(req, new ArrayListHandler());
577
        Map<Integer, Long> map = new HashMap<Integer, Long>();
578
 
579
        int idCptFourn = ComptePCESQLElement.getId("401", "Fournisseurs");
580
        int idCptClient = ComptePCESQLElement.getId("411", "Clients");
581
 
582
        for (Object[] o : l) {
583
 
584
            long credit = 0;
585
            if (o[2] != null) {
586
                credit = Long.valueOf(o[2].toString());
587
            }
588
 
589
            long debit = 0;
590
            if (o[1] != null) {
591
                debit = Long.valueOf(o[1].toString());
592
            }
593
 
594
            int id = Integer.valueOf(o[0].toString());
595
            long solde = debit - credit;
596
            map.put(id, solde);
597
            if (o[3] != null) {
598
                String numero = o[3].toString();
599
                if (this.centralFourn && numero.startsWith("401")) {
600
                    Long lS = map.get(idCptFourn);
601
                    if (lS != null) {
602
                        lS += solde;
603
                    } else {
604
                        lS = new Long(solde);
605
                    }
606
                    map.put(idCptFourn, lS);
607
                }
608
                if (this.centralClient && numero.startsWith("411")) {
609
                    Long lS = map.get(idCptClient);
610
                    if (lS != null) {
611
                        lS += solde;
612
                    } else {
613
                        lS = new Long(solde);
614
                    }
615
                    map.put(idCptClient, lS);
616
                }
617
            }
618
        }
619
 
620
        return map;
621
    }
622
}