OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 41 | Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.finance.accounting.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.preferences.PrinterNXProps;
20
import org.openconcerto.erp.rights.ComptaUserRight;
21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
28
import org.openconcerto.sql.users.UserManager;
29
import org.openconcerto.utils.GestionDevise;
30
import org.openconcerto.utils.cc.ITransformer;
31
 
32
import java.text.DateFormat;
33
import java.util.ArrayList;
34
import java.util.Calendar;
35
import java.util.Date;
36
import java.util.HashMap;
37
import java.util.List;
38
import java.util.Map;
39
 
40
import org.apache.commons.dbutils.handlers.ArrayListHandler;
41
 
42
public class GrandLivreSheet extends SheetInterface {
43
 
44
    private static int debutFill, endFill;
45
    public static int MODEALL = 1;
46
    public static int MODELETTREE = 2;
47
    public static int MODENONLETTREE = 3;
48
    private final static SQLTable tableEcriture = base.getTable("ECRITURE");
49
    private final static SQLTable tableJournal = base.getTable("JOURNAL");
50
    private final static SQLTable tableMvt = base.getTable("MOUVEMENT");
51
    private final static SQLTable tableCompte = base.getTable("COMPTE_PCE");
52
 
53
    private final static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
54
    private final static DateFormat dateFormatEcr = DateFormat.getDateInstance(DateFormat.SHORT);
55
    private SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
56
 
57
    private Date dateDu, dateAu;
58
    private String compteDeb, compteEnd;
59
    private int lettrage;
60
    private boolean cumul = false;
61
    private boolean excludeCompteSolde = true;
62
    private boolean centralClient = false;
63
    private boolean centralFourn = false;
19 ilm 64
    int idJrnlExclude = -1;
18 ilm 65
 
25 ilm 66
    public static String TEMPLATE_ID = "Grand Livre";
67
    public static String TEMPLATE_PROPERTY_NAME = "LocationGrandLivre";
68
 
18 ilm 69
    public static void setSize(int debut, int fin) {
70
        debutFill = debut;
71
        endFill = fin;
72
    }
73
 
74
    static {
75
        setSize(7, 69);
76
    }
77
 
25 ilm 78
    @Override
79
    protected String getYear() {
80
        return "";
18 ilm 81
    }
82
 
19 ilm 83
    public GrandLivreSheet(Date du, Date au, String compteDep, String compteEnd, int lettrage, boolean cumul, boolean excludeCptSolde, boolean centralClient, boolean centralFourn, int idJrnlExclude) {
18 ilm 84
        super();
85
        Calendar cal = Calendar.getInstance();
86
        cal.setTime(au);
87
        this.nbRowsPerPage = 72;
19 ilm 88
        this.idJrnlExclude = idJrnlExclude;
18 ilm 89
        this.printer = PrinterNXProps.getInstance().getStringProperty("GrandLivrePrinter");
90
        this.modele = "GrandLivre.ods";
91
        this.dateAu = au;
92
        this.dateDu = du;
93
        this.compteDeb = compteDep.trim();
94
        this.compteEnd = compteEnd.trim();
95
        this.lettrage = lettrage;
96
        this.cumul = cumul;
97
        this.excludeCompteSolde = excludeCptSolde;
98
        this.centralClient = centralClient;
99
        this.centralFourn = centralFourn;
100
 
101
        createMap();
102
    }
103
 
104
    private String toDay = dateFormat.format(new Date());
41 ilm 105
    private int size;
18 ilm 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
                }
19 ilm 170
                w = w.and(new Where(tableEcriture.getField("ID_JOURNAL"), "!=", idJrnlExclude));
18 ilm 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);
65 ilm 197
                sel.addFieldOrder(tableEcriture.getField("COMPTE_NUMERO"));
198
                sel.addFieldOrder(tableEcriture.getField("DATE"));
199
                sel.addFieldOrder(tableMvt.getField("NUMERO"));
18 ilm 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;
41 ilm 209
        size = list.size();
210
        System.err.println("START CREATE Grand livre, NB ecritures  " + size);
18 ilm 211
        this.nbPage = 0;
212
        long totalDebit, totalCredit, sousTotalDebit, sousTotalCredit, totalCreditAntC, totalDebitAntC, totalCreditAntF, totalDebitAntF;
213
 
214
        totalDebit = 0;
215
        totalCredit = 0;
216
        sousTotalCredit = 0;
217
        sousTotalDebit = 0;
218
        totalCreditAntC = 0;
219
        totalDebitAntC = 0;
220
        totalCreditAntF = 0;
221
        totalDebitAntF = 0;
222
        SQLRowValues rowFirstEcr = null;
223
        int idCptFirstEcr = 1;
224
 
225
        boolean setTitle = true;
226
        boolean setLine = false;
227
        boolean setCumuls = true;
228
        boolean firstEcrCentC = true;
229
        boolean firstEcrCentF = true;
230
        String numCptFirstEcr = "411";
231
        String numCptClient = "411";
232
        String nomCptClient = "Clients";
233
        String numCptFourn = "401";
234
        String nomCptFourn = "Fournisseurs";
235
        int idCptClient = ComptePCESQLElement.getId(numCptClient, nomCptClient);
236
        int idCptFourn = ComptePCESQLElement.getId(numCptFourn, nomCptFourn);
237
 
238
        final String titre3 = "Titre 3";
239
        final String cumulAntString = "Cumuls antérieurs";
240
        int j = 0;
41 ilm 241
        for (int i = 0; i < size;) {
18 ilm 242
 
243
            System.err.println("START NEW PAGE; POS : " + posLine);
244
 
245
            /***************************************************************************************
246
             * ENTETE
247
             **************************************************************************************/
248
            makeEntete(posLine);
249
            posLine += debutFill - 1;
250
 
251
            // Affiche le nom du compte
252
            setTitle = true;
253
            // ligne vide avant de mettre le setTitle
254
            setLine = false;
255
 
256
            // setCumuls = false;
257
            /***************************************************************************************
258
             * CONTENU
259
             **************************************************************************************/
260
            final Double doubleZero = Double.valueOf("0");
41 ilm 261
            for (j = 0; (j < endFill - debutFill + 1) && i < size; j++) {
18 ilm 262
 
263
                SQLRowValues rowEcr = list.get(i);
264
 
265
                int idCpt = rowEcr.getInt("ID_COMPTE_PCE");
266
                String nomCpt = rowEcr.getString("COMPTE_NOM");
267
                String numCpt = rowEcr.getString("COMPTE_NUMERO");
268
 
269
                // Cumuls antérieurs
270
                if (setCumuls && this.cumul && !setTitle) {
271
 
272
                    this.mapStyleRow.put(Integer.valueOf(posLine), titre3);
273
                    this.mCell.put("A" + posLine, "");
274
                    this.mCell.put("B" + posLine, "");
275
                    this.mCell.put("C" + posLine, "");
276
 
277
                    this.mCell.put("D" + posLine, cumulAntString);
278
                    Long longSolde = mapCumul.get(idCpt);
279
 
280
                    if (longSolde == null) {
281
                        longSolde = Long.valueOf(0);
282
                    }
283
                    long debitCumulAnt = 0;
284
                    long creditCumulAnt = 0;
285
 
286
                    if (longSolde > 0) {
287
                        debitCumulAnt = longSolde;
288
                    } else {
289
                        creditCumulAnt = -longSolde;
290
                    }
291
                    this.mCell.put("E" + posLine, (debitCumulAnt == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(debitCumulAnt, false)));
292
                    this.mCell.put("F" + posLine, (creditCumulAnt == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(creditCumulAnt, false)));
293
                    this.mCell.put("G" + posLine, (longSolde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(longSolde, false)));
294
 
295
                    totalCredit += creditCumulAnt;
296
                    totalDebit += debitCumulAnt;
297
 
298
                    sousTotalCredit += creditCumulAnt;
299
                    sousTotalDebit += debitCumulAnt;
300
                    setCumuls = false;
301
                } else {
302
                    // Titre
303
                    if (setTitle) {
304
                        if (!setLine) {
305
                            this.mapStyleRow.put(new Integer(posLine), "Titre 1");
306
 
307
                            // Si on centralise les comptes clients ou fournisseurs on affiche le
308
                            // compte 401 ou 411
309
                            if (this.centralClient && nomCpt.startsWith("411")) {
310
                                nomCpt = nomCptClient;
311
                                numCpt = numCptClient;
312
                                idCpt = idCptClient;
313
                            }
314
                            if (this.centralFourn && nomCpt.startsWith("401")) {
315
                                nomCpt = nomCptFourn;
316
                                numCpt = numCptFourn;
317
                                idCpt = idCptFourn;
318
                            }
319
                            this.mCell.put("A" + posLine, numCpt);
320
                            this.mCell.put("B" + posLine, nomCpt);
321
                            this.mCell.put("C" + posLine, "");
322
                            this.mCell.put("D" + posLine, "");
323
                            this.mCell.put("E" + posLine, "");
324
                            this.mCell.put("F" + posLine, "");
325
                            this.mCell.put("G" + posLine, "");
326
                            setTitle = false;
327
                            setLine = true;
328
 
329
                            if (rowFirstEcr == null) {
330
                                rowFirstEcr = rowEcr;
331
                                idCptFirstEcr = rowEcr.getInt("ID_COMPTE_PCE");
332
                                numCptFirstEcr = rowEcr.getString("COMPTE_NUMERO");
333
                            }
334
 
335
                        } else {
336
                            this.mapStyleRow.put(new Integer(posLine), "Normal");
337
                            setLine = false;
338
                        }
339
                    } else {
340
 
341
                        // si on change de compte alors on applique le style Titre 1
342
                        if (rowFirstEcr != null && idCptFirstEcr != idCpt && (!this.centralFourn || (!(numCptFirstEcr.startsWith("401") && numCpt.startsWith("401"))))
343
                                && (!this.centralClient || (!(numCptFirstEcr.startsWith("411") && numCpt.startsWith("411"))))) {
344
 
345
                            rowFirstEcr = rowEcr;
346
                            idCptFirstEcr = rowFirstEcr.getInt("ID_COMPTE_PCE");
347
                            numCptFirstEcr = rowEcr.getString("COMPTE_NUMERO");
348
                            makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
349
 
350
                            sousTotalCredit = 0;
351
                            sousTotalDebit = 0;
352
                            setTitle = true;
353
                            setCumuls = true;
354
                        } else {
355
                            long cred = rowEcr.getLong("CREDIT");
356
                            long deb = rowEcr.getLong("DEBIT");
357
                            // Centralisation fournisseur
358
                            if (this.centralFourn && numCpt.startsWith("401")) {
359
                                i++;
360
 
361
                                if (firstEcrCentF) {
362
                                    posLine++;
363
                                    this.mCell.put("D" + (posLine - 1), "Centralisation des comptes fournisseurs");
364
                                    this.mapStyleRow.put(new Integer((posLine - 1)), "Normal");
365
                                    firstEcrCentF = false;
366
                                } else {
367
                                    j--;
368
                                }
369
 
370
                                totalCreditAntF += cred;
371
                                totalDebitAntF += deb;
372
                                sousTotalCredit += cred;
373
                                sousTotalDebit += deb;
374
                                long solde = totalDebitAntF - totalCreditAntF;
375
                                this.mCell.put("E" + (posLine - 1), (totalDebitAntF == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebitAntF, false)));
376
                                this.mCell.put("F" + (posLine - 1), (totalCreditAntF == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalCreditAntF, false)));
377
                                this.mCell.put("G" + (posLine - 1), (solde == 0) ? doubleZero : new Double(GestionDevise.currencyToString(solde, false)));
378
 
379
                                continue;
380
                            }
381
                            // Centralisation client
382
                            if (this.centralClient && numCpt.startsWith("411")) {
383
                                i++;
384
                                if (firstEcrCentC) {
385
                                    posLine++;
386
                                    this.mCell.put("D" + (posLine - 1), "Centralisation des comptes clients");
387
                                    this.mapStyleRow.put(new Integer((posLine - 1)), "Normal");
388
                                    firstEcrCentC = false;
389
                                } else {
390
                                    j--;
391
                                }
392
 
393
                                totalCreditAntC += cred;
394
                                totalDebitAntC += deb;
395
                                sousTotalCredit += cred;
396
                                sousTotalDebit += deb;
397
                                long solde = totalDebitAntC - totalCreditAntC;
398
                                this.mCell.put("E" + (posLine - 1), (totalDebitAntC == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(totalDebitAntC, false)));
399
                                this.mCell.put("F" + (posLine - 1), (totalCreditAntC == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(totalCreditAntC, false)));
400
                                this.mCell.put("G" + (posLine - 1), (solde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(solde, false)));
401
 
402
                                continue;
403
                            }
404
 
405
                            this.mCell.put("A" + posLine, dateFormatEcr.format((Date) rowEcr.getObject("DATE")));
406
 
407
                            this.mCell.put("B" + posLine, rowEcr.getString("JOURNAL_CODE"));
408
                            this.mCell.put("C" + posLine, rowEcr.getForeign("ID_MOUVEMENT").getObject("NUMERO"));
409
                            this.mCell.put("D" + posLine, rowEcr.getObject("NOM"));
410
 
411
                            totalCredit += cred;
412
                            totalDebit += deb;
413
 
414
                            sousTotalCredit += cred;
415
                            sousTotalDebit += deb;
416
                            long solde = sousTotalDebit - sousTotalCredit;
417
 
418
                            this.mCell.put("E" + posLine, (deb == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(deb, false)));
419
                            this.mCell.put("F" + posLine, (cred == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(cred, false)));
420
                            this.mCell.put("G" + posLine, (solde == 0) ? doubleZero : Double.valueOf(GestionDevise.currencyToString(solde, false)));
421
 
422
                            this.mapStyleRow.put(Integer.valueOf(posLine), "Normal");
423
                            i++;
424
                        }
425
                    }
426
                }
427
                posLine++;
428
            }
429
 
41 ilm 430
            if (i >= size && j < endFill - debutFill + 1) {
18 ilm 431
                makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
432
            }
433
 
434
            posLine = firstLine + endFill;
435
            /*
436
             * if (this.mapStyleRow.get(new Integer(posLine - 1)) != null) {
437
             * this.mapStyleRow.put(new Integer(posLine - 1), "Titre 2"); }
438
             */
439
 
440
            // Total
441
            this.mCell.put("E" + posLine, (totalDebit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebit, false)));
442
            this.mCell.put("F" + posLine, (totalCredit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalCredit, false)));
443
            this.mCell.put("G" + posLine, (totalDebit - totalCredit == 0) ? doubleZero : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
444
 
445
            posLine += 2;
446
 
447
            // bas de page
448
            makePiedPage(posLine, this.compteDeb + " à " + this.compteEnd);
449
 
450
            posLine++;
451
            firstLine = posLine;
452
            this.nbPage++;
453
 
41 ilm 454
            if (i >= size && j >= (endFill - debutFill + 1)) {
18 ilm 455
 
456
                makeEntete(posLine);
457
                posLine += debutFill - 1;
458
                makeSousTotal(posLine, sousTotalDebit, sousTotalCredit);
459
                this.nbPage++;
460
            }
461
 
462
        }
463
 
464
        // on conserve la page d'origine du model
465
        if (this.nbPage > 0) {
466
            this.nbPage--;
467
        }
468
 
469
        Date end = new Date();
470
        System.err.println("///////// TAKE " + (end.getTime() - d.getTime()) + " millisecondes TO CREATE MAP");
471
    }
472
 
473
    private List<Integer> getListeCompteSolde() {
474
        SQLSelect sel = new SQLSelect(base);
475
 
476
        sel.addSelect(tableCompte.getField("ID"));
477
        sel.addSelect(tableEcriture.getField("DEBIT"), "SUM");
478
        sel.addSelect(tableEcriture.getField("CREDIT"), "SUM");
479
 
480
        Where w;
481
        if (this.compteDeb.equals(this.compteEnd)) {
482
            w = new Where(tableCompte.getField("NUMERO"), "=", this.compteDeb);
483
        } else {
484
            w = new Where(tableCompte.getField("NUMERO"), (Object) this.compteDeb, (Object) this.compteEnd);
485
        }
486
 
487
        w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), "=", tableCompte.getField("ID")));
488
 
489
        if (this.cumul) {
490
            w = w.and(new Where(tableEcriture.getField("DATE"), "<=", this.dateAu));
491
        } else {
492
            w = w.and(new Where(tableEcriture.getField("DATE"), this.dateDu, this.dateAu));
493
        }
19 ilm 494
        w = w.and(new Where(tableEcriture.getField("ID_JOURNAL"), "!=", idJrnlExclude));
18 ilm 495
        if (this.lettrage == MODELETTREE) {
496
            Object o = null;
497
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
498
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
499
        } else {
500
            if (this.lettrage == MODENONLETTREE) {
501
                Object o = null;
502
                Where w2 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
503
                w = w.and(w2.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
504
 
505
            }
506
        }
507
 
508
        sel.setWhere(w);
509
 
510
        String req = sel.asString() + " GROUP BY \"COMPTE_PCE\".\"ID\"";
511
        System.err.println(req);
512
        List<Object[]> l = (List) base.getDataSource().execute(req, new ArrayListHandler());
513
        List<Integer> list = new ArrayList<Integer>();
514
        for (Object[] o : l) {
515
            long credit = 0;
516
            if (o[2] != null) {
517
                credit = Long.valueOf(o[2].toString());
518
            }
519
 
520
            long debit = 0;
521
            if (o[1] != null) {
522
                debit = Long.valueOf(o[1].toString());
523
            }
524
 
525
            int id = Integer.valueOf(o[0].toString());
526
            long solde = debit - credit;
527
            if (solde == 0) {
528
                list.add(id);
529
            }
530
        }
531
        return list;
532
    }
533
 
534
    /**
535
     * @param d date limite des cumuls
536
     * @return Map<Integer id compte, Long solde(debit-credit)>
537
     */
538
    private Map<Integer, Long> getCumulsAnterieur(Date d, List<Integer> listCompteSolde) {
539
        SQLSelect sel = new SQLSelect(base);
540
 
541
        sel.addSelect(tableEcriture.getField("ID_COMPTE_PCE"));
542
        sel.addSelect(tableEcriture.getField("DEBIT"), "SUM");
543
        sel.addSelect(tableEcriture.getField("CREDIT"), "SUM");
544
        sel.addSelect(tableEcriture.getField("COMPTE_NUMERO"));
545
        // sel.addSelect(tableEcriture.getField("ID_MOUVEMENT"));
546
        Where w = (new Where(tableEcriture.getField("DATE"), "<", d));
547
        w = w.and(new Where(tableEcriture.getField("ID_MOUVEMENT"), "=", tableMvt.getKey()));
548
 
549
        if (this.compteDeb.equals(this.compteEnd)) {
550
            w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", this.compteDeb));
551
        } else {
552
            w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) this.compteDeb, (Object) this.compteEnd));
553
        }
554
 
555
        if (this.lettrage == MODELETTREE) {
556
            Object o = null;
557
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
558
            w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
559
        } else {
560
            if (this.lettrage == MODENONLETTREE) {
561
                Object o = null;
562
                Where w2 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
563
                w = w.and(w2.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
564
            }
565
        }
566
 
567
        w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), "=", tableCompte.getField("ID")));
19 ilm 568
        w = w.and(new Where(tableEcriture.getField("ID_JOURNAL"), "!=", idJrnlExclude));
18 ilm 569
        if (listCompteSolde != null) {
570
            w = w.and(new Where(tableEcriture.getField("ID_COMPTE_PCE"), listCompteSolde).not());
571
        }
572
 
573
        sel.setWhere(w);
574
 
575
        String req = sel.asString() + " GROUP BY \"ECRITURE\".\"ID_COMPTE_PCE\", \"ECRITURE\".\"COMPTE_NUMERO\"";
576
        System.err.println(req);
577
        List<Object[]> l = (List) base.getDataSource().execute(req, new ArrayListHandler());
578
        Map<Integer, Long> map = new HashMap<Integer, Long>();
579
 
580
        int idCptFourn = ComptePCESQLElement.getId("401", "Fournisseurs");
581
        int idCptClient = ComptePCESQLElement.getId("411", "Clients");
582
 
583
        for (Object[] o : l) {
584
 
585
            long credit = 0;
586
            if (o[2] != null) {
587
                credit = Long.valueOf(o[2].toString());
588
            }
589
 
590
            long debit = 0;
591
            if (o[1] != null) {
592
                debit = Long.valueOf(o[1].toString());
593
            }
594
 
595
            int id = Integer.valueOf(o[0].toString());
596
            long solde = debit - credit;
597
            map.put(id, solde);
598
            if (o[3] != null) {
599
                String numero = o[3].toString();
600
                if (this.centralFourn && numero.startsWith("401")) {
601
                    Long lS = map.get(idCptFourn);
602
                    if (lS != null) {
603
                        lS += solde;
604
                    } else {
605
                        lS = new Long(solde);
606
                    }
607
                    map.put(idCptFourn, lS);
608
                }
609
                if (this.centralClient && numero.startsWith("411")) {
610
                    Long lS = map.get(idCptClient);
611
                    if (lS != null) {
612
                        lS += solde;
613
                    } else {
614
                        lS = new Long(solde);
615
                    }
616
                    map.put(idCptClient, lS);
617
                }
618
            }
619
        }
620
 
621
        return map;
622
    }
25 ilm 623
 
624
    @Override
625
    public String getTemplateId() {
626
        return TEMPLATE_ID;
627
    }
41 ilm 628
 
629
    public int getSize() {
630
        return size;
631
    }
18 ilm 632
}