OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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