OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 177
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.finance.accounting.report;
14
 package org.openconcerto.erp.core.finance.accounting.report;
15
 
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
-
 
17
import org.openconcerto.erp.element.objet.Compte;
17
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
18
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
18
import org.openconcerto.erp.generationDoc.SheetInterface;
19
import org.openconcerto.erp.generationDoc.SheetInterface;
19
import org.openconcerto.erp.preferences.PrinterNXProps;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.Configuration;
-
 
22
import org.openconcerto.sql.model.SQLBase;
21
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowListRSH;
24
import org.openconcerto.sql.model.SQLRowListRSH;
23
import org.openconcerto.sql.model.SQLSelect;
25
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.Where;
27
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.utils.GestionDevise;
28
import org.openconcerto.utils.GestionDevise;
27
 
29
 
28
import java.text.DateFormat;
30
import java.text.DateFormat;
-
 
31
import java.util.ArrayList;
29
import java.util.Calendar;
32
import java.util.Calendar;
30
import java.util.Date;
33
import java.util.Date;
31
import java.util.HashMap;
34
import java.util.HashMap;
32
import java.util.List;
35
import java.util.List;
33
import java.util.Map;
36
import java.util.Map;
Line 65... Line 68...
65
    @Override
68
    @Override
66
    public String getTemplateId() {
69
    public String getTemplateId() {
67
        return TEMPLATE_ID;
70
        return TEMPLATE_ID;
68
    }
71
    }
69
 
72
 
70
    public BalanceSheet(Date du, Date au, String compteDeb, String compteEnd, boolean centralClient, boolean centralFourn, boolean centralFournImmo) {
73
    public BalanceSheet(Date du, Date au, String compteDeb, String compteEnd, boolean centralClient, boolean centralFourn, boolean centralFournImmo, boolean displayAll) {
71
        super();
74
        super();
72
 
75
 
73
        Calendar cal = Calendar.getInstance();
76
        Calendar cal = Calendar.getInstance();
74
        cal.setTime(au);
77
        cal.setTime(au);
75
 
78
 
Line 82... Line 85...
82
 
85
 
83
        // this.locationOO = storage.getDocumentOutputDirectory(TEMPLATE_ID);
86
        // this.locationOO = storage.getDocumentOutputDirectory(TEMPLATE_ID);
84
        // this.locationPDF = storage.getPDFOutputDirectory(TEMPLATE_ID);
87
        // this.locationPDF = storage.getPDFOutputDirectory(TEMPLATE_ID);
85
        this.dateAu = au;
88
        this.dateAu = au;
86
        this.dateDu = du;
89
        this.dateDu = du;
87
 
-
 
-
 
90
        this.viewMode = displayAll;
88
        this.compteDeb = compteDeb;
91
        this.compteDeb = compteDeb;
89
        this.compteEnd = compteEnd;
92
        this.compteEnd = compteEnd;
90
        this.centralClient = centralClient;
93
        this.centralClient = centralClient;
91
        this.centralFourn = centralFourn;
94
        this.centralFourn = centralFourn;
92
        this.centralFournImmo = centralFournImmo;
95
        this.centralFournImmo = centralFournImmo;
Line 119... Line 122...
119
        this.mCell.put("E" + row, new Double(GestionDevise.currencyToString(debit - credit, false)));
122
        this.mCell.put("E" + row, new Double(GestionDevise.currencyToString(debit - credit, false)));
120
 
123
 
121
        this.mapStyleRow.put(new Integer(row), "Titre 1");
124
        this.mapStyleRow.put(new Integer(row), "Titre 1");
122
    }
125
    }
123
 
126
 
-
 
127
    private boolean viewMode = true;
-
 
128
 
124
    protected void createMap() {
129
    protected void createMap() {
-
 
130
        int posLine = 1;
-
 
131
        int firstLine = 1;
-
 
132
        this.nbPage = 0;
-
 
133
        long totalDebit, totalCredit, sousTotalDebit, sousTotalCredit;
-
 
134
 
-
 
135
        totalDebit = 0;
-
 
136
        totalCredit = 0;
-
 
137
        sousTotalDebit = 0;
-
 
138
        sousTotalCredit = 0;
-
 
139
 
-
 
140
        long totalDebitClient = 0;
-
 
141
        long totalCreditClient = 0;
-
 
142
 
-
 
143
        long totalDebitFourn = 0;
-
 
144
        long totalCreditFourn = 0;
-
 
145
 
-
 
146
        long totalDebitFournImmo = 0;
-
 
147
        long totalCreditFournImmo = 0;
-
 
148
 
-
 
149
        String numCptClient = "411";
-
 
150
        String nomCptClient = "Clients";
-
 
151
        String numCptFourn = "401";
-
 
152
        String nomCptFourn = "Fournisseurs";
-
 
153
        String numCptFournImmo = "404";
-
 
154
        String nomCptFournImmo = "Fournisseurs d'immobilisations";
-
 
155
        boolean addedLine = false;
-
 
156
        boolean addedLineImmo = false;
-
 
157
        boolean addedLineFourn = false;
-
 
158
        int j = 0;
-
 
159
        String classe = "";
-
 
160
 
-
 
161
        if (viewMode) {
-
 
162
            getBalance();
-
 
163
 
-
 
164
            for (int i = 0; i < this.vecteurCompte.size();) {
-
 
165
 
-
 
166
                System.err.println("START NEW PAGE; POS : " + posLine);
-
 
167
 
-
 
168
                /***************************************************************************************
-
 
169
                 * ENTETE
-
 
170
                 **************************************************************************************/
-
 
171
                makeEntete(posLine);
-
 
172
                posLine += debutFill - 1;
-
 
173
 
-
 
174
                /***************************************************************************************
-
 
175
                 * CONTENU
-
 
176
                 **************************************************************************************/
-
 
177
                for (j = 0; (j < endFill - debutFill + 1) && i < this.vecteurCompte.size(); j++) {
-
 
178
                    Compte compte = this.vecteurCompte.get(i);
-
 
179
 
-
 
180
                    String numeroCpt = compte.getNumero();
-
 
181
                    String nomCpt = compte.getNom();
-
 
182
 
-
 
183
                    long deb = compte.getTotalDebit();
-
 
184
                    long cred = compte.getTotalCredit();
-
 
185
 
-
 
186
                    totalCredit += cred;
-
 
187
                    sousTotalCredit += cred;
-
 
188
                    totalDebit += deb;
-
 
189
                    sousTotalDebit += deb;
-
 
190
 
-
 
191
                    this.mCell.put("A" + posLine, numeroCpt);
-
 
192
                    this.mCell.put("B" + posLine, nomCpt);
-
 
193
                    this.mCell.put("C" + posLine, new Double(GestionDevise.currencyToString(deb, false)));
-
 
194
                    this.mCell.put("D" + posLine, new Double(GestionDevise.currencyToString(cred, false)));
-
 
195
                    this.mCell.put("E" + posLine, new Double(GestionDevise.currencyToString(deb - cred, false)));
-
 
196
 
-
 
197
                    if (compte.getSousCompte().isEmpty()) {
-
 
198
                        this.mapStyleRow.put(new Integer(posLine), "Normal");
-
 
199
                    } else {
-
 
200
                        this.mapStyleRow.put(new Integer(posLine), "Titre 1");
-
 
201
                    }
-
 
202
                    i++;
-
 
203
 
-
 
204
                    posLine++;
-
 
205
                }
-
 
206
 
-
 
207
                if (i >= this.vecteurCompte.size() && j < endFill - debutFill + 1) {
-
 
208
 
-
 
209
                    makeSousTotalClasse(posLine, sousTotalDebit, sousTotalCredit, classe);
-
 
210
                }
-
 
211
 
-
 
212
                posLine = firstLine + endFill;
-
 
213
                /*
-
 
214
                 * if (this.mapStyleRow.get(new Integer(posLine - 1)) != null) {
-
 
215
                 * this.mapStyleRow.put(new Integer(posLine - 1), "Titre 2"); }
-
 
216
                 */
-
 
217
 
-
 
218
                // Total
-
 
219
                this.mCell.put("C" + posLine, ((totalDebit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit, false))));
-
 
220
                this.mCell.put("D" + posLine, ((totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalCredit, false))));
-
 
221
                this.mCell.put("E" + posLine, (totalDebit - totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
-
 
222
 
-
 
223
                posLine += 2;
-
 
224
 
-
 
225
                // bas de page
-
 
226
                makePiedPage(posLine);
-
 
227
 
-
 
228
                posLine++;
-
 
229
                firstLine = posLine;
-
 
230
                this.nbPage++;
-
 
231
 
-
 
232
                // if (i >= this.vecteurCompte.size() && j >= (endFill - debutFill + 1)) {
-
 
233
                //
-
 
234
                // makeEntete(posLine);
-
 
235
                // posLine += debutFill - 1;
-
 
236
                //
-
 
237
                // makeSousTotalClasse(posLine, sousTotalDebit, sousTotalCredit, classe);
-
 
238
                //
-
 
239
                // this.nbPage++;
-
 
240
                // }
-
 
241
 
-
 
242
            }
-
 
243
        } else {
125
 
244
 
126
        this.mapReplace = new HashMap();
245
            this.mapReplace = new HashMap();
127
        this.mCell = new HashMap();
246
            this.mCell = new HashMap();
128
        this.mapStyleRow = new HashMap();
247
            this.mapStyleRow = new HashMap();
129
 
248
 
Line 155... Line 274...
155
 
274
 
156
        System.err.println(req);
275
            System.err.println(req);
157
 
276
 
158
        List l = (List) base.getDataSource().execute(req, new ArrayListHandler());
277
            List l = (List) base.getDataSource().execute(req, new ArrayListHandler());
159
 
278
 
160
        int posLine = 1;
-
 
161
        int firstLine = 1;
-
 
162
        System.err.println("START CREATE Grand livre, NB ecritures  " + l.size());
-
 
163
        this.nbPage = 0;
-
 
164
        long totalDebit, totalCredit, sousTotalDebit, sousTotalCredit;
-
 
165
 
-
 
166
        totalDebit = 0;
-
 
167
        totalCredit = 0;
-
 
168
        sousTotalDebit = 0;
-
 
169
        sousTotalCredit = 0;
-
 
170
 
-
 
171
        long totalDebitClient = 0;
-
 
172
        long totalCreditClient = 0;
-
 
173
 
-
 
174
        long totalDebitFourn = 0;
-
 
175
        long totalCreditFourn = 0;
-
 
176
 
-
 
177
        long totalDebitFournImmo = 0;
-
 
178
        long totalCreditFournImmo = 0;
-
 
179
 
-
 
180
        String numCptClient = "411";
-
 
181
        String nomCptClient = "Clients";
-
 
182
        String numCptFourn = "401";
-
 
183
        String nomCptFourn = "Fournisseurs";
-
 
184
        String numCptFournImmo = "404";
-
 
185
        String nomCptFournImmo = "Fournisseurs d'immobilisations";
-
 
186
        boolean addedLine = false;
-
 
187
        boolean addedLineImmo = false;
-
 
188
        boolean addedLineFourn = false;
-
 
189
        int j = 0;
-
 
190
        String classe = "";
-
 
191
 
-
 
192
        SQLSelect selCompte = new SQLSelect();
279
            SQLSelect selCompte = new SQLSelect();
193
        selCompte.addSelectStar(tableCompte);
280
            selCompte.addSelectStar(tableCompte);
194
        List<SQLRow> compteRows = SQLRowListRSH.execute(selCompte);
281
            List<SQLRow> compteRows = SQLRowListRSH.execute(selCompte);
195
        Map<Integer, SQLRow> mapCompte = new HashMap<Integer, SQLRow>();
282
            Map<Integer, SQLRow> mapCompte = new HashMap<Integer, SQLRow>();
196
        for (SQLRow sqlRow : compteRows) {
283
            for (SQLRow sqlRow : compteRows) {
Line 342... Line 429...
342
 
429
 
343
                this.nbPage++;
430
                    this.nbPage++;
344
            }
431
                }
345
 
432
 
346
        }
433
            }
-
 
434
        }
347
 
435
 
348
        // on conserve la page d'origine du model
436
        // on conserve la page d'origine du model
349
        if (this.nbPage > 0) {
437
        if (this.nbPage > 0) {
350
            this.nbPage--;
438
            this.nbPage--;
351
        }
439
        }
352
    }
440
    }
-
 
441
 
-
 
442
    private long totalDebitBalance = 0;
-
 
443
    private long totalCreditBalance = 0;
-
 
444
    private List<Compte> vecteurCompte = new ArrayList<Compte>();
-
 
445
 
-
 
446
    public void getBalance() {
-
 
447
 
-
 
448
        // Compte numero -- totalDebit
-
 
449
        Map<Number, Long> mapCompteDebit = new HashMap<Number, Long>();
-
 
450
        Map<Number, Long> mapCompteCredit = new HashMap<Number, Long>();
-
 
451
        List<Compte> comptes = new ArrayList<Compte>();
-
 
452
        this.totalDebitBalance = 0;
-
 
453
        this.totalCreditBalance = 0;
-
 
454
 
-
 
455
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
-
 
456
        SQLTable compteTable = base.getTable("COMPTE_PCE");
-
 
457
        SQLTable ecritureTable = base.getTable("ECRITURE");
-
 
458
 
-
 
459
        SQLSelect sel = new SQLSelect();
-
 
460
 
-
 
461
        // On recupere le solde des comptes
-
 
462
        sel.addSelect(compteTable.getField("ID"));
-
 
463
        sel.addSelect(ecritureTable.getField("DEBIT"), "SUM");
-
 
464
        sel.addSelect(ecritureTable.getField("CREDIT"), "SUM");
-
 
465
        sel.addSelect(compteTable.getField("NUMERO"));
-
 
466
        sel.setDistinct(true);
-
 
467
        Where w = (new Where(tableEcriture.getField("DATE"), this.dateDu, this.dateAu));
-
 
468
        if (dateDu == null) {
-
 
469
            w = (new Where(tableEcriture.getField("DATE"), "<=", this.dateAu));
-
 
470
        }
-
 
471
        sel.setWhere(w.and(new Where(compteTable.getField("ID"), "=", ecritureTable.getField("ID_COMPTE_PCE"))));
-
 
472
 
-
 
473
        String req = sel.asString() + " GROUP BY \"COMPTE_PCE\".\"ID\",\"COMPTE_PCE\".\"NUMERO\"  ORDER BY \"COMPTE_PCE\".\"NUMERO\"";
-
 
474
 
-
 
475
        System.out.println(req);
-
 
476
 
-
 
477
        Object ob = base.getDataSource().execute(req, new ArrayListHandler());
-
 
478
 
-
 
479
        List myList = (List) ob;
-
 
480
 
-
 
481
        if (myList.size() != 0) {
-
 
482
 
-
 
483
            for (int i = 0; i < myList.size(); i++) {
-
 
484
 
-
 
485
                Object[] tmp = (Object[]) myList.get(i);
-
 
486
 
-
 
487
                mapCompteDebit.put((Number) tmp[0], Long.parseLong(tmp[1].toString()));
-
 
488
                mapCompteCredit.put((Number) tmp[0], Long.parseLong(tmp[2].toString()));
-
 
489
            }
-
 
490
        }
-
 
491
 
-
 
492
        // Création du vecteur balance
-
 
493
        sel = new SQLSelect();
-
 
494
 
-
 
495
        sel.addSelect(compteTable.getKey());
-
 
496
        sel.addSelect(compteTable.getField("NUMERO"));
-
 
497
        sel.addSelect(compteTable.getField("NOM"));
-
 
498
 
-
 
499
        sel.addRawOrder("\"COMPTE_PCE\".\"NUMERO\"");
-
 
500
 
-
 
501
        String reqCompte = sel.asString();
-
 
502
        System.out.println(req);
-
 
503
 
-
 
504
        Object obCompte = base.getDataSource().execute(reqCompte, new ArrayListHandler());
-
 
505
 
-
 
506
        List myListCompte = (List) obCompte;
-
 
507
 
-
 
508
        if (myListCompte.size() != 0) {
-
 
509
 
-
 
510
            for (int i = 0; i < myListCompte.size(); i++) {
-
 
511
 
-
 
512
                Object[] tmp = (Object[]) myListCompte.get(i);
-
 
513
                System.err.println("Compte " + tmp[1].toString().trim());
-
 
514
 
-
 
515
                long totalDebit = 0;
-
 
516
                long totalCredit = 0;
-
 
517
                if (mapCompteDebit.get(tmp[0]) != null) {
-
 
518
                    totalDebit = Long.parseLong(mapCompteDebit.get(tmp[0]).toString());
-
 
519
                }
-
 
520
 
-
 
521
                if (mapCompteCredit.get(tmp[0]) != null) {
-
 
522
                    totalCredit = Long.parseLong(mapCompteCredit.get(tmp[0]).toString());
-
 
523
                }
-
 
524
 
-
 
525
                this.totalDebitBalance += totalDebit;
-
 
526
                this.totalCreditBalance += totalCredit;
-
 
527
 
-
 
528
                List<String> sousCompte = new ArrayList<>();
-
 
529
                for (int j = i + 1; j < (myListCompte.size() - 1); j++) {
-
 
530
                    Object[] tmpNext = (Object[]) myListCompte.get(j);
-
 
531
                    if (tmpNext[1].toString().trim().startsWith(tmp[1].toString().trim())) {
-
 
532
                        System.err.println("Sous Compte " + tmpNext[1].toString().trim());
-
 
533
                        sousCompte.add(tmpNext[1].toString().trim());
-
 
534
                        if (mapCompteDebit.get(tmpNext[0]) != null) {
-
 
535
                            totalDebit += Long.parseLong(mapCompteDebit.get(tmpNext[0]).toString());
-
 
536
                        }
-
 
537
 
-
 
538
                        if (mapCompteCredit.get(tmpNext[0]) != null) {
-
 
539
                            totalCredit += Long.parseLong(mapCompteCredit.get(tmpNext[0]).toString());
-
 
540
                        }
-
 
541
                    } else {
-
 
542
                        break;
-
 
543
                    }
-
 
544
                }
-
 
545
                if ((totalDebit != 0.0) || (totalCredit != 0.0)) {
-
 
546
                    Compte cpt = new Compte(((Number) tmp[0]).intValue(), tmp[1].toString(), tmp[2].toString(), "", totalDebit, totalCredit);
-
 
547
                    cpt.addSousCompte(sousCompte);
-
 
548
                    comptes.add(cpt);
-
 
549
                }
-
 
550
            }
-
 
551
        }
-
 
552
 
-
 
553
        this.vecteurCompte = comptes;
-
 
554
    }
353
}
555
}