OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
65 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
65 ilm 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.generationDoc.AbstractListeSheetXml;
18
import org.openconcerto.erp.preferences.PrinterNXProps;
19
import org.openconcerto.erp.rights.ComptaUserRight;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowAccessor;
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;
65 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
public class JournauxSheetXML extends AbstractListeSheetXml {
41
 
42
    private final static SQLTable tableEcriture = base.getTable("ECRITURE");
43
    protected final static SQLTable tableJournal = base.getTable("JOURNAL");
44
    private final static SQLTable tableMvt = base.getTable("MOUVEMENT");
45
    protected final static SQLTable tableCompte = base.getTable("COMPTE_PCE");
46
    public final static int MODEALL = 1;
47
    public final static int MODELETTREE = 2;
48
    public final static int MODENONLETTREE = 3;
49
 
144 ilm 50
    private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
51
    private final DateFormat dateFormatEcr = DateFormat.getDateInstance(DateFormat.SHORT);
65 ilm 52
    protected Date dateDu, dateAu;
53
    protected int id;
54
    protected int lettrage;
55
    private String compteDeb, compteEnd;
56
 
57
    public static String TEMPLATE_ID = "Journaux";
58
    public static String TEMPLATE_PROPERTY_NAME = "LocationJournaux";
59
 
60
    private SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
61
 
62
    @Override
63
    public String getDefaultTemplateId() {
64
        return TEMPLATE_ID;
65
    }
66
 
67 ilm 67
    @Override
68
    public String getStoragePathP() {
69
        return "Journaux";
70
    }
71
 
65 ilm 72
    Date date;
73
 
74
    @Override
75
    public String getName() {
76
        if (this.date == null) {
77
            this.date = new Date();
78
        }
79
        return "Journal" + date.getTime();
80
    }
81
 
82
    public JournauxSheetXML(int id, Date du, Date au, int lettrage, String compteDeb, String compteEnd) {
83
        super();
84
        Calendar cal = Calendar.getInstance();
85
        cal.setTime(au);
86
        this.printer = PrinterNXProps.getInstance().getStringProperty("JournauxPrinter");
87
        this.dateAu = au;
88
        this.dateDu = du;
89
        this.id = id;
90
        this.lettrage = lettrage;
91
        this.compteDeb = compteDeb;
92
        this.compteEnd = compteEnd;
93
    }
94
 
95
    protected void makeEntete(Map<String, Object> line, String nomJournal) {
96
        line.put("TITRE_1", "Journal " + nomJournal + " - " + rowSociete.getObject("TYPE") + " " + rowSociete.getObject("NOM"));
97
        line.put("TITRE_2", "Edition du " + dateFormat.format(new Date()) + " Période du " + dateFormatEcr.format(this.dateDu) + " au " + dateFormatEcr.format(this.dateAu));
98
    }
99
 
100
    protected void createListeValues() {
101
 
102
        final SQLRowValues vals = new SQLRowValues(tableEcriture);
103
 
104
        vals.put("ID_JOURNAL", null);
105
        vals.put("ID_COMPTE_PCE", null);
106
        vals.put("COMPTE_NUMERO", null);
107
        vals.put("COMPTE_NOM", null);
108
        vals.put("JOURNAL_CODE", null);
109
        vals.put("JOURNAL_NOM", null);
110
        vals.putRowValues("ID_MOUVEMENT").put("NUMERO", null);
111
        vals.put("CREDIT", null);
112
        vals.put("DEBIT", null);
113
        vals.put("DATE", null);
114
        vals.put("NOM", null);
182 ilm 115
        if (tableEcriture.contains("NOM_PIECE")) {
116
            vals.put("NOM_PIECE", null);
117
        }
65 ilm 118
 
119
        final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(vals);
120
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
121
            @Override
122
            public SQLSelect transformChecked(SQLSelect sel) {
123
 
124
                Where w = (new Where(tableEcriture.getField("DATE"), JournauxSheetXML.this.dateDu, JournauxSheetXML.this.dateAu));
125
 
126
                Where w2 = new Where(tableEcriture.getField("ID_JOURNAL"), "=", JournauxSheetXML.this.id);
127
 
128
                if (JournauxSheetXML.this.lettrage == MODELETTREE) {
129
                    Object o = null;
130
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
131
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
132
                } else {
133
                    if (JournauxSheetXML.this.lettrage == MODENONLETTREE) {
134
                        Object o = null;
135
                        Where w3 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
136
                        w = w.and(w3.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
137
                    }
138
                }
139
 
140
                if (JournauxSheetXML.this.compteDeb.equals(JournauxSheetXML.this.compteEnd)) {
141
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", JournauxSheetXML.this.compteDeb));
142
                } else {
143
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) JournauxSheetXML.this.compteDeb, (Object) JournauxSheetXML.this.compteEnd));
144
                }
145
 
144 ilm 146
                if (!UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
65 ilm 147
                    // TODO Show Restricted acces in UI
148
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "LIKE", "411%"));
149
                }
150
 
151
                sel.setWhere(w.and(w2));
152
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("ID_JOURNAL")));
153
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("DATE")));
154
                sel.addFieldOrder(sel.getAlias(tableMvt.getField("NUMERO")));
155
 
156
                return sel;
157
            }
158
        });
159
 
160
        List<SQLRowValues> list = fetcher.fetch();
161
 
162
        System.err.println("START CREATE JOURNAUX, NB ecritures  " + list.size());
163
 
164
        long totalDebit, totalCredit;
165
 
166
        totalDebit = 0;
167
        totalCredit = 0;
168
        int prevIdMvt = 0;
169
 
170
        String firstJournal = tableJournal.getRow(this.id).getString("NOM");
171
 
172
        List<Map<String, Object>> tableauVals = new ArrayList<Map<String, Object>>();
173
        this.listAllSheetValues.put(0, tableauVals);
174
 
175
        Map<Integer, String> style = new HashMap<Integer, String>();
176
        this.styleAllSheetValues.put(0, style);
177
 
178
        for (int i = 0; i < list.size(); i++) {
179
 
180
            Map<String, Object> values = new HashMap<String, Object>();
181
 
182
            SQLRowValues rowEcr = list.get(i);
183
 
184
            SQLRowAccessor rowMvt = rowEcr.getForeign("ID_MOUVEMENT");
185
 
186
            // si on change de mouvement alors on applique le style Titre 1
187
            if (prevIdMvt != rowMvt.getID()) {
188
                prevIdMvt = rowMvt.getID();
189
                style.put(tableauVals.size(), "Titre 1");
190
            } else {
191
                style.put(tableauVals.size(), "Normal");
192
            }
193
            values.put("DATE", dateFormatEcr.format(rowEcr.getDate("DATE").getTime()));
194
 
195
            values.put("NUMERO_COMPTE", rowEcr.getString("COMPTE_NUMERO"));
196
 
177 ilm 197
            if (tableEcriture.contains("NOM_PIECE")) {
198
                values.put("NOM_PIECE", rowEcr.getObject("NOM_PIECE"));
199
            }
200
 
65 ilm 201
            values.put("NUMERO_MOUVEMENT", rowMvt.getObject("NUMERO"));
202
            Object libelle = rowEcr.getObject("NOM");
203
            values.put("LIBELLE", libelle);
204
            long deb = ((Long) rowEcr.getObject("DEBIT")).longValue();
205
            long cred = ((Long) rowEcr.getObject("CREDIT")).longValue();
206
 
207
            long solde = deb - cred;
208
 
209
            totalCredit += cred;
210
            totalDebit += deb;
211
 
212
            values.put("DEBIT", (deb == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(deb, false)));
213
            values.put("CREDIT", (cred == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(cred, false)));
214
            values.put("SOLDE", (solde == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(solde, false)));
215
 
216
            tableauVals.add(values);
217
 
218
        }
219
 
220
        Map<String, Object> sheetVals = new HashMap<String, Object>();
221
        this.mapAllSheetValues.put(0, sheetVals);
222
 
223
        makeEntete(sheetVals, firstJournal);
224
 
225
        sheetVals.put("TOTAL_DEBIT", (totalDebit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit, false)));
226
        sheetVals.put("TOTAL_CREDIT", (totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalCredit, false)));
227
        sheetVals.put("TOTAL_SOLDE", (totalDebit - totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
228
 
229
    }
230
}