OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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
 *
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.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);
115
 
116
        final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(vals);
117
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
118
            @Override
119
            public SQLSelect transformChecked(SQLSelect sel) {
120
 
121
                Where w = (new Where(tableEcriture.getField("DATE"), JournauxSheetXML.this.dateDu, JournauxSheetXML.this.dateAu));
122
 
123
                Where w2 = new Where(tableEcriture.getField("ID_JOURNAL"), "=", JournauxSheetXML.this.id);
124
 
125
                if (JournauxSheetXML.this.lettrage == MODELETTREE) {
126
                    Object o = null;
127
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
128
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
129
                } else {
130
                    if (JournauxSheetXML.this.lettrage == MODENONLETTREE) {
131
                        Object o = null;
132
                        Where w3 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
133
                        w = w.and(w3.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
134
                    }
135
                }
136
 
137
                if (JournauxSheetXML.this.compteDeb.equals(JournauxSheetXML.this.compteEnd)) {
138
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", JournauxSheetXML.this.compteDeb));
139
                } else {
140
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) JournauxSheetXML.this.compteDeb, (Object) JournauxSheetXML.this.compteEnd));
141
                }
142
 
144 ilm 143
                if (!UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
65 ilm 144
                    // TODO Show Restricted acces in UI
145
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "LIKE", "411%"));
146
                }
147
 
148
                sel.setWhere(w.and(w2));
149
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("ID_JOURNAL")));
150
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("DATE")));
151
                sel.addFieldOrder(sel.getAlias(tableMvt.getField("NUMERO")));
152
 
153
                return sel;
154
            }
155
        });
156
 
157
        List<SQLRowValues> list = fetcher.fetch();
158
 
159
        System.err.println("START CREATE JOURNAUX, NB ecritures  " + list.size());
160
 
161
        long totalDebit, totalCredit;
162
 
163
        totalDebit = 0;
164
        totalCredit = 0;
165
        int prevIdMvt = 0;
166
 
167
        String firstJournal = tableJournal.getRow(this.id).getString("NOM");
168
 
169
        List<Map<String, Object>> tableauVals = new ArrayList<Map<String, Object>>();
170
        this.listAllSheetValues.put(0, tableauVals);
171
 
172
        Map<Integer, String> style = new HashMap<Integer, String>();
173
        this.styleAllSheetValues.put(0, style);
174
 
175
        for (int i = 0; i < list.size(); i++) {
176
 
177
            Map<String, Object> values = new HashMap<String, Object>();
178
 
179
            SQLRowValues rowEcr = list.get(i);
180
 
181
            SQLRowAccessor rowMvt = rowEcr.getForeign("ID_MOUVEMENT");
182
 
183
            // si on change de mouvement alors on applique le style Titre 1
184
            if (prevIdMvt != rowMvt.getID()) {
185
                prevIdMvt = rowMvt.getID();
186
                style.put(tableauVals.size(), "Titre 1");
187
            } else {
188
                style.put(tableauVals.size(), "Normal");
189
            }
190
            values.put("DATE", dateFormatEcr.format(rowEcr.getDate("DATE").getTime()));
191
 
192
            values.put("NUMERO_COMPTE", rowEcr.getString("COMPTE_NUMERO"));
193
 
177 ilm 194
            if (tableEcriture.contains("NOM_PIECE")) {
195
                values.put("NOM_PIECE", rowEcr.getObject("NOM_PIECE"));
196
            }
197
 
65 ilm 198
            values.put("NUMERO_MOUVEMENT", rowMvt.getObject("NUMERO"));
199
            Object libelle = rowEcr.getObject("NOM");
200
            values.put("LIBELLE", libelle);
201
            long deb = ((Long) rowEcr.getObject("DEBIT")).longValue();
202
            long cred = ((Long) rowEcr.getObject("CREDIT")).longValue();
203
 
204
            long solde = deb - cred;
205
 
206
            totalCredit += cred;
207
            totalDebit += deb;
208
 
209
            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)));
211
            values.put("SOLDE", (solde == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(solde, false)));
212
 
213
            tableauVals.add(values);
214
 
215
        }
216
 
217
        Map<String, Object> sheetVals = new HashMap<String, Object>();
218
        this.mapAllSheetValues.put(0, sheetVals);
219
 
220
        makeEntete(sheetVals, firstJournal);
221
 
222
        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)));
224
        sheetVals.put("TOTAL_SOLDE", (totalDebit - totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
225
 
226
    }
227
}