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
18 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.
18 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.SheetInterface;
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;
18 ilm 29
import org.openconcerto.utils.GestionDevise;
30
import org.openconcerto.utils.cc.ITransformer;
31
 
32
import java.text.DateFormat;
33
import java.util.Calendar;
34
import java.util.Date;
35
import java.util.HashMap;
36
import java.util.List;
37
 
38
public class JournauxSheet extends SheetInterface {
39
 
40
    protected static int debutFill, endFill;
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);
18 ilm 52
    protected Date dateDu, dateAu;
53
    protected int[] idS;
54
    protected int lettrage;
55
    private String compteDeb, compteEnd;
56
 
144 ilm 57
    public static String TEMPLATE_ID = "JournauxAncien";
25 ilm 58
    public static String TEMPLATE_PROPERTY_NAME = "LocationJournaux";
59
 
18 ilm 60
    public static void setSize(int debut, int fin) {
61
        debutFill = debut;
62
        endFill = fin;
63
    }
64
 
65
    static {
66
        setSize(7, 68);
67
    }
68
 
25 ilm 69
    @Override
70
    public String getTemplateId() {
71
        return TEMPLATE_ID;
72
    }
18 ilm 73
 
25 ilm 74
    @Override
75
    protected String getYear() {
76
        return "";
18 ilm 77
    }
78
 
79
    public JournauxSheet(int[] id, Date du, Date au, int lettrage, String compteDeb, String compteEnd) {
80
        super();
81
        Calendar cal = Calendar.getInstance();
82
        cal.setTime(au);
83
        this.printer = PrinterNXProps.getInstance().getStringProperty("JournauxPrinter");
144 ilm 84
        this.modele = getTemplateId() + ".ods";
18 ilm 85
        this.dateAu = au;
86
        this.dateDu = du;
87
        this.idS = id;
88
        this.lettrage = lettrage;
89
        this.nbRowsPerPage = 71;
90
        this.compteDeb = compteDeb;
91
        this.compteEnd = compteEnd;
92
 
93
        System.err.println("Init ids with values ");
94
        for (int i = 0; i < id.length; i++) {
95
            System.err.println(id[i]);
96
        }
97
        createMap();
98
    }
99
 
100
    protected void makeEntete(int row, String nomJournal) {
101
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
102
        this.mCell.put("A" + row, rowSociete.getObject("NOM"));
103
        this.mCell.put("F" + row, "Edition du " + dateFormat.format(new Date()));
104
        // this.mCell.put("D" + (row + 2), "Impression Journaux");
105
        System.err.println("MAKE ENTETE");
106
    }
107
 
108
    protected void makeBasPage(int row, String nomJournal) {
109
        this.mCell.put("A" + row, "Journal : " + nomJournal);
110
        this.mCell.put("E" + row, "Période du " + dateFormatEcr.format(this.dateDu) + " au " + dateFormatEcr.format(this.dateAu));
111
    }
112
 
113
    protected void createMap() {
114
 
115
        this.mapReplace = new HashMap();
116
        this.mCell = new HashMap();
117
        this.mapStyleRow = new HashMap();
118
 
119
        final SQLRowValues vals = new SQLRowValues(tableEcriture);
120
 
121
        vals.put("ID_JOURNAL", null);
122
        vals.put("ID_COMPTE_PCE", null);
123
        vals.put("COMPTE_NUMERO", null);
124
        vals.put("COMPTE_NOM", null);
125
        vals.put("JOURNAL_CODE", null);
126
        vals.put("JOURNAL_NOM", null);
127
        vals.putRowValues("ID_MOUVEMENT").put("NUMERO", null);
128
        vals.put("CREDIT", null);
129
        vals.put("DEBIT", null);
130
        vals.put("DATE", null);
131
        vals.put("NOM", null);
132
 
133
        final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(vals);
134
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
135
            @Override
136
            public SQLSelect transformChecked(SQLSelect sel) {
137
 
138
                Where w = (new Where(tableEcriture.getField("DATE"), JournauxSheet.this.dateDu, JournauxSheet.this.dateAu));
139
 
140
                Where w2 = null;
141
                for (int i = 0; i < JournauxSheet.this.idS.length; i++) {
142
                    if (w2 == null) {
143
                        w2 = new Where(tableEcriture.getField("ID_JOURNAL"), "=", JournauxSheet.this.idS[i]);
144
                    } else {
145
                        w2 = w2.or(new Where(tableEcriture.getField("ID_JOURNAL"), "=", JournauxSheet.this.idS[i]));
146
                    }
147
                }
148
 
149
                // w.and(new Where(tableEcriture.getField("ID_MOUVEMENT"), "=",
150
                // tableMvt.getField("ID")));
151
 
152
                if (JournauxSheet.this.lettrage == MODELETTREE) {
153
                    Object o = null;
154
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "<>", o));
155
                    w = w.and(new Where(tableEcriture.getField("LETTRAGE"), "!=", ""));
156
                } else {
157
                    if (JournauxSheet.this.lettrage == MODENONLETTREE) {
158
                        Object o = null;
159
                        Where w3 = new Where(tableEcriture.getField("LETTRAGE"), "=", o);
160
                        w = w.and(w3.or(new Where(tableEcriture.getField("LETTRAGE"), "=", "")));
161
                    }
162
                }
163
 
164
                if (JournauxSheet.this.compteDeb.equals(JournauxSheet.this.compteEnd)) {
165
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "=", JournauxSheet.this.compteDeb));
166
                } else {
167
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), (Object) JournauxSheet.this.compteDeb, (Object) JournauxSheet.this.compteEnd));
168
                }
169
 
144 ilm 170
                if (!UserRightsManager.getCurrentUserRights().haveRight(ComptaUserRight.ACCES_NOT_RESCTRICTED_TO_411)) {
18 ilm 171
                    // TODO Show Restricted acces in UI
172
                    w = w.and(new Where(tableEcriture.getField("COMPTE_NUMERO"), "LIKE", "411%"));
173
                }
174
 
175
                sel.setWhere(w.and(w2));
176
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("ID_JOURNAL")));
177
                sel.addFieldOrder(sel.getAlias(tableEcriture.getField("DATE")));
178
                sel.addFieldOrder(sel.getAlias(tableMvt.getField("NUMERO")));
179
 
180
                return sel;
181
            }
182
        });
183
 
184
        List<SQLRowValues> list = fetcher.fetch();
185
 
186
        int posLine = 1;
187
        int firstLine = 1;
188
        System.err.println("START CREATE JOURNAUX, NB ecritures  " + list.size());
189
        this.nbPage = 0;
190
        long totalDebit, totalCredit;
191
 
192
        totalDebit = 0;
193
        totalCredit = 0;
194
        int prevIdMvt = 0;
195
        SQLRowValues rowFirstEcr = null;
196
        String firstJournal = null;
197
 
198
        for (int i = 0; i < list.size();) {
199
 
200
            rowFirstEcr = list.get(i);
201
 
202
            if (firstJournal == null || !firstJournal.equalsIgnoreCase(rowFirstEcr.getString("JOURNAL_NOM"))) {
203
                totalDebit = 0;
204
                totalCredit = 0;
205
            }
206
 
207
            firstJournal = rowFirstEcr.getString("JOURNAL_NOM");
208
            System.err.println("START NEW PAGE --> Journal : " + firstJournal + "; POS : " + posLine);
209
 
210
            /***************************************************************************************
211
             * ENTETE
212
             **************************************************************************************/
213
            makeEntete(posLine, firstJournal);
214
            posLine += debutFill - 1;
215
 
216
            /***************************************************************************************
217
             * CONTENU
218
             **************************************************************************************/
219
            // && (posLine % endFill !=0)
220
            for (int j = 0; (j < endFill - debutFill + 1) && i < list.size(); j++) {
221
 
222
                SQLRowValues rowEcr = list.get(i);
223
                String journal = rowEcr.getString("JOURNAL_NOM");
224
 
225
                if (journal.equalsIgnoreCase(firstJournal)) {
226
 
227
                    SQLRowAccessor rowMvt = rowEcr.getForeign("ID_MOUVEMENT");
228
 
229
                    // si on change de mouvement alors on applique le style Titre 1
230
                    if (prevIdMvt != rowMvt.getID()) {
231
                        prevIdMvt = rowMvt.getID();
232
                        this.mapStyleRow.put(new Integer(posLine), "Titre 1");
233
                    } else {
234
                        this.mapStyleRow.put(new Integer(posLine), "Normal");
235
                    }
236
                    this.mCell.put("A" + posLine, dateFormatEcr.format(rowEcr.getDate("DATE").getTime()));
237
 
238
                    this.mCell.put("B" + posLine, rowEcr.getString("COMPTE_NUMERO"));
239
 
240
                    this.mCell.put("C" + posLine, rowMvt.getObject("NUMERO"));
241
                    this.mCell.put("D" + posLine, rowEcr.getObject("NOM"));
242
                    long deb = ((Long) rowEcr.getObject("DEBIT")).longValue();
243
                    long cred = ((Long) rowEcr.getObject("CREDIT")).longValue();
244
 
245
                    long solde = deb - cred;
246
 
247
                    totalCredit += cred;
248
                    totalDebit += deb;
249
 
250
                    this.mCell.put("E" + posLine, (deb == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(deb, false)));
251
                    this.mCell.put("F" + posLine, (cred == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(cred, false)));
252
                    this.mCell.put("G" + posLine, (solde == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(solde, false)));
253
 
254
                } else {
255
                    break;
256
                }
257
 
258
                i++;
259
                posLine++;
260
            }
261
 
262
            posLine = firstLine + endFill;
263
            /*
264
             * if (this.mapStyleRow.get(new Integer(posLine - 1)) != null) {
265
             * this.mapStyleRow.put(new Integer(posLine - 1), "Titre 2"); }
266
             */
267
 
268
            // Bas de page
269
            this.mCell.put("E" + posLine, (totalDebit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit, false)));
270
            this.mCell.put("F" + posLine, (totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalCredit, false)));
271
            this.mCell.put("G" + posLine, (totalDebit - totalCredit == 0) ? new Double(0) : new Double(GestionDevise.currencyToString(totalDebit - totalCredit, false)));
272
 
273
            posLine += 2;
274
 
275
            makeBasPage(posLine, firstJournal);
276
 
277
            posLine++;
278
            firstLine = posLine;
279
            this.nbPage++;
280
        }
281
 
182 ilm 282
 
283
        if (this.nbPage < 1) {
284
            this.nbPage = 1;
18 ilm 285
        }
286
    }
287
}