OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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