OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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