OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 80 | Go to most recent revision | 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
 *
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.humanresources.payroll.report;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.generationDoc.SheetInterface;
18
import org.openconcerto.erp.generationDoc.SheetXml;
19
import org.openconcerto.erp.preferences.PrinterNXProps;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.utils.Tuple2;
26
 
27
import java.io.File;
28
import java.text.DateFormat;
29
import java.util.Date;
30
import java.util.HashMap;
31
import java.util.List;
32
import java.util.Map;
33
 
34
import org.apache.commons.dbutils.handlers.ArrayListHandler;
35
 
36
public class LivrePayeSheet extends SheetInterface {
37
 
38
    // TODO Incorrect si aucune fiche valider
39
    private static int debutFill, endFill;
40
    private static int nbCol;
41
    private final static SQLTable tableSalarie = base.getTable("SALARIE");
42
    private final static SQLTable tableFichePaye = base.getTable("FICHE_PAYE");
43
    private final static SQLTable tableFichePayeElement = base.getTable("FICHE_PAYE_ELEMENT");
44
    private final static SQLTable tableMois = base.getTable("MOIS");
45
    private final static SQLTable tableRubCot = Configuration.getInstance().getBase().getTable("RUBRIQUE_COTISATION");
46
    private final static SQLTable tableRubNet = Configuration.getInstance().getBase().getTable("RUBRIQUE_NET");
47
    private final static SQLTable tableRubBrut = Configuration.getInstance().getBase().getTable("RUBRIQUE_BRUT");
48
 
49
    private final static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
50
    private int moisDu, moisAu;
51
    private String annee;
52
 
53
    public static void setSize(int debut, int fin, int col) {
54
        debutFill = debut;
55
        endFill = fin;
56
        nbCol = col;
57
    }
58
 
59
    static {
60
        setSize(8, 65, 3);
61
    }
62
 
25 ilm 63
    @Override
64
    protected String getYear() {
65
        return "";
18 ilm 66
    }
67
 
25 ilm 68
    public static final String TEMPLATE_ID = "Livre de paye";
69
    public static final String TEMPLATE_PROPERTY_NAME = "LocationLivrePaye";
70
 
18 ilm 71
    public LivrePayeSheet(int moisDu, int moisAu, String annee) {
72
        super();
73
        this.printer = PrinterNXProps.getInstance().getStringProperty("LivrePayePrinter");
74
        this.modele = "LivrePaye.ods";
75
        this.moisAu = moisAu;
76
        this.moisDu = moisDu;
77
        this.annee = annee;
78
 
79
        this.nbRowsPerPage = 67;
80
 
81
        createMap();
82
    }
83
 
84
    private void makeEntete(int row) {
85
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
86
        this.mCell.put("A" + row, rowSociete.getObject("NOM"));
87
        this.mCell.put("D" + row, "Edition du " + dateFormat.format(new Date()));
88
        // this.mCell.put("D" + (row + 2), "Impression Journaux");
89
        System.err.println("MAKE ENTETE");
90
    }
91
 
92
    private void makeBasPage(int row) {
93
        SQLRow rowMoisDu = tableMois.getRow(this.moisDu);
94
        SQLRow rowMoisAu = tableMois.getRow(this.moisAu);
95
 
96
        this.mCell.put("A" + row, "Période de " + rowMoisDu.getString("NOM") + " à " + rowMoisAu.getString("NOM") + " " + this.annee);
97
    }
98
 
25 ilm 99
    @Override
100
    public String getTemplateId() {
101
        return TEMPLATE_ID;
102
    }
103
 
18 ilm 104
    protected void createMap() {
105
 
106
        this.mapReplace = new HashMap();
107
        this.mCell = new HashMap();
108
        this.mapStyleRow = new HashMap();
109
 
110
        SQLSelect sel = new SQLSelect(base);
111
        sel.addSelect(tableFichePaye.getField("ID"));
112
        sel.addSelect(tableFichePayeElement.getField("ID"));
113
        sel.addSelect(tableSalarie.getField("ID"));
114
 
115
        Where w = (new Where(tableFichePayeElement.getField("ID_FICHE_PAYE"), "=", tableFichePaye.getField("ID")));
116
        Where w2 = (new Where(tableFichePaye.getField("ID_SALARIE"), "=", tableSalarie.getField("ID")));
117
        Where w3 = (new Where(tableFichePaye.getField("ID_MOIS"), new Integer(this.moisDu), new Integer(this.moisAu)));
118
        Where w4 = (new Where(tableFichePaye.getField("ANNEE"), "=", new Integer(this.annee)));
119
        Where w5 = (new Where(tableFichePaye.getField("VALIDE"), "=", Boolean.TRUE));
120
 
121
        sel.setWhere(w);
122
        sel.andWhere(w2);
123
        sel.andWhere(w3);
124
        sel.andWhere(w4);
125
        sel.andWhere(w5);
126
        String req = sel.asString();
127
 
128
        System.err.println(req);
129
 
130
        // Liste des rubriques de chaque salaries
131
        List l = (List) base.getDataSource().execute(req, new ArrayListHandler());
132
 
133
        // Association idSal, map Value(idRub, val)
134
        Map mapSalarieBrut = new HashMap();
135
        Map mapSalarieNet = new HashMap();
136
        Map mapSalarieCot = new HashMap();
137
 
138
        Map mapTotalCot = new HashMap();
139
        Map mapTotalNet = new HashMap();
140
        Map mapTotalbrut = new HashMap();
141
 
142
        Map mapRubriqueBrut = new HashMap();
143
        Map mapRubriqueNet = new HashMap();
144
        Map mapRubriqueCot = new HashMap();
145
        Map mapSal = new HashMap();
146
 
147
        // Cumuls des rubriques par salaries
148
        for (int i = 0; i < l.size(); i++) {
149
            Object[] tmp = (Object[]) l.get(i);
150
            // int idFiche = new Integer(tmp[0].toString()).intValue();
151
            int idFicheElt = Integer.parseInt(tmp[1].toString());
152
            int idSal = Integer.parseInt(tmp[2].toString());
153
 
154
            Map mapValue = new HashMap();
155
            Map mapTotal = new HashMap();
156
 
157
            // SQLRow rowFiche = tableFichePaye.getRow(idFiche);
158
            SQLRow rowFicheElt = tableFichePayeElement.getRow(idFicheElt);
159
 
160
            mapSal.put(new Integer(idSal), "");
161
 
162
            if (rowFicheElt.getString("SOURCE").equalsIgnoreCase("RUBRIQUE_BRUT")) {
163
 
164
                mapRubriqueBrut.put(new Integer(rowFicheElt.getInt("IDSOURCE")), "");
165
                mapTotal = mapTotalbrut;
166
                if (mapSalarieBrut.get(new Integer(idSal)) == null) {
167
                    mapSalarieBrut.put(new Integer(idSal), mapValue);
168
                } else {
169
                    mapValue = (Map) mapSalarieBrut.get(new Integer(idSal));
170
                }
171
            } else {
172
                if (rowFicheElt.getString("SOURCE").equalsIgnoreCase("RUBRIQUE_COTISATION")) {
173
                    mapRubriqueCot.put(new Integer(rowFicheElt.getInt("IDSOURCE")), "");
174
                    mapTotal = mapTotalCot;
175
                    if (mapSalarieCot.get(new Integer(idSal)) == null) {
176
                        mapSalarieCot.put(new Integer(idSal), mapValue);
177
                    } else {
178
                        mapValue = (Map) mapSalarieCot.get(new Integer(idSal));
179
                    }
180
                } else {
181
                    if (rowFicheElt.getString("SOURCE").equalsIgnoreCase("RUBRIQUE_NET")) {
182
                        mapRubriqueNet.put(new Integer(rowFicheElt.getInt("IDSOURCE")), "");
183
                        mapTotal = mapTotalNet;
184
                        if (mapSalarieNet.get(new Integer(idSal)) == null) {
185
                            mapSalarieNet.put(new Integer(idSal), mapValue);
186
                        } else {
187
                            mapValue = (Map) mapSalarieNet.get(new Integer(idSal));
188
                        }
189
                    }
190
                }
191
            }
192
 
193
            if (rowFicheElt.getObject("MONTANT_SAL_AJ") != null) {
194
                Object o = mapValue.get(new Integer(rowFicheElt.getInt("IDSOURCE")));
195
                Object oTot = mapTotal.get(new Integer(rowFicheElt.getInt("IDSOURCE")));
196
 
197
                float montant = (o == null) ? 0.0F : ((Float) o).floatValue();
198
                float montantTotal = (oTot == null) ? 0.0F : ((Float) oTot).floatValue();
199
                montant += rowFicheElt.getFloat("MONTANT_SAL_AJ");
200
                montantTotal += rowFicheElt.getFloat("MONTANT_SAL_AJ");
201
 
202
                mapValue.put(new Integer(rowFicheElt.getInt("IDSOURCE")), new Float(montant));
203
                mapTotal.put(new Integer(rowFicheElt.getInt("IDSOURCE")), new Float(montantTotal));
204
            }
205
            if (rowFicheElt.getObject("MONTANT_SAL_DED") != null) {
206
                Object o = mapValue.get(new Integer(rowFicheElt.getInt("IDSOURCE")));
207
                Object oTot = mapTotal.get(new Integer(rowFicheElt.getInt("IDSOURCE")));
208
 
209
                float montant = (o == null) ? 0.0F : ((Float) o).floatValue();
210
                float montantTot = (oTot == null) ? 0.0F : ((Float) oTot).floatValue();
211
                montant -= rowFicheElt.getFloat("MONTANT_SAL_DED");
212
                montantTot -= rowFicheElt.getFloat("MONTANT_SAL_DED");
213
 
214
                mapValue.put(new Integer(rowFicheElt.getInt("IDSOURCE")), new Float(montant));
215
                mapTotal.put(new Integer(rowFicheElt.getInt("IDSOURCE")), new Float(montantTot));
216
            }
217
        }
218
 
219
        // Dump
220
        /*
221
         * for (int j = 0; j < mapSalarieBrut.keySet().size(); j++) {
222
         * System.err.println(mapSalarieBrut.get(mapSalarieBrut.keySet().toArray()[j])); }
223
         */
224
 
225
        // Fill
226
        int posLine = 1;
227
        int firstLine = 1;
228
 
229
        System.err.println("NB Sal = " + mapSal.keySet().size());
230
        System.err.println("NB Pages = " + Math.ceil((double) (mapSal.keySet().size() + 1) / nbCol));
231
        for (int n = 0; n < Math.ceil((double) (mapSal.keySet().size() + 1) / nbCol); n++) {
232
 
233
            // entete
234
            makeEntete(posLine);
235
            posLine += (debutFill - 1);
236
 
237
            int numFirstSal = (n * nbCol);
238
 
239
            if (numFirstSal < mapSal.keySet().size()) {
240
                SQLRow rowSal = tableSalarie.getRow(((Integer) mapSal.keySet().toArray()[numFirstSal]).intValue());
241
                this.mCell.put("B" + (posLine - 2), rowSal.getObject("NOM"));
242
                this.mCell.put("B" + (posLine - 1), rowSal.getObject("PRENOM"));
243
            } else {
244
                if (numFirstSal == mapSal.keySet().size()) {
245
                    System.err.println("Cumuls B");
246
                    this.mCell.put("B" + (posLine - 2), "Cumuls");
247
                    this.mCell.put("B" + (posLine - 1), "");
248
                }
249
            }
250
            if (numFirstSal + 1 < mapSal.keySet().size()) {
251
                SQLRow rowSal = tableSalarie.getRow(((Integer) mapSal.keySet().toArray()[numFirstSal + 1]).intValue());
252
                this.mCell.put("C" + (posLine - 2), rowSal.getObject("NOM"));
253
                this.mCell.put("C" + (posLine - 1), rowSal.getObject("PRENOM"));
254
            } else {
255
                if (numFirstSal + 1 == mapSal.keySet().size()) {
256
                    System.err.println("Cumuls C");
257
                    this.mCell.put("C" + (posLine - 2), "Cumuls");
258
                    this.mCell.put("C" + (posLine - 1), "");
259
                }
260
            }
261
            if (numFirstSal + 2 < mapSal.keySet().size()) {
262
                SQLRow rowSal = tableSalarie.getRow(((Integer) mapSal.keySet().toArray()[numFirstSal + 2]).intValue());
263
                this.mCell.put("D" + (posLine - 2), rowSal.getObject("NOM"));
264
                this.mCell.put("D" + (posLine - 1), rowSal.getObject("PRENOM"));
265
            } else {
266
                if (numFirstSal + 2 == mapSal.keySet().size()) {
267
                    System.err.println("Cumuls D");
268
                    this.mCell.put("D" + (posLine - 2), "Cumuls");
269
                    this.mCell.put("D" + (posLine - 1), "");
270
                }
271
            }
272
            for (int i = 0; i < mapRubriqueBrut.keySet().size(); i++) {
273
 
274
                int idRub = ((Number) mapRubriqueBrut.keySet().toArray()[i]).intValue();
275
                SQLRow rowRub = tableRubBrut.getRow(idRub);
276
 
277
                this.mCell.put("A" + posLine, rowRub.getObject("NOM"));
278
 
279
                this.mCell.put("B" + posLine, fillLine(mapSalarieBrut, idRub, mapSal, numFirstSal, mapTotalbrut));
280
                this.mCell.put("C" + posLine, fillLine(mapSalarieBrut, idRub, mapSal, numFirstSal + 1, mapTotalbrut));
281
                this.mCell.put("D" + posLine, fillLine(mapSalarieBrut, idRub, mapSal, numFirstSal + 2, mapTotalbrut));
282
 
283
                posLine++;
284
            }
285
 
286
            for (int i = 0; i < mapRubriqueCot.keySet().size(); i++) {
287
 
288
                int idRub = ((Number) mapRubriqueCot.keySet().toArray()[i]).intValue();
289
                SQLRow rowRub = tableRubCot.getRow(idRub);
290
 
291
                this.mCell.put("A" + posLine, rowRub.getObject("NOM"));
292
 
293
                this.mCell.put("B" + posLine, fillLine(mapSalarieCot, idRub, mapSal, numFirstSal, mapTotalCot));
294
                this.mCell.put("C" + posLine, fillLine(mapSalarieCot, idRub, mapSal, numFirstSal + 1, mapTotalCot));
295
                this.mCell.put("D" + posLine, fillLine(mapSalarieCot, idRub, mapSal, numFirstSal + 2, mapTotalCot));
296
 
297
                posLine++;
298
            }
299
 
300
            for (int i = 0; i < mapRubriqueNet.keySet().size(); i++) {
301
 
302
                int idRub = ((Number) mapRubriqueNet.keySet().toArray()[i]).intValue();
303
                SQLRow rowRub = tableRubNet.getRow(idRub);
304
 
305
                this.mCell.put("A" + posLine, rowRub.getObject("NOM"));
306
 
307
                this.mCell.put("B" + posLine, fillLine(mapSalarieNet, idRub, mapSal, numFirstSal, mapTotalNet));
308
                this.mCell.put("C" + posLine, fillLine(mapSalarieNet, idRub, mapSal, numFirstSal + 1, mapTotalNet));
309
                this.mCell.put("D" + posLine, fillLine(mapSalarieNet, idRub, mapSal, numFirstSal + 2, mapTotalNet));
310
 
311
                posLine++;
312
            }
313
 
314
            // pied de page
315
            posLine = firstLine + endFill - 1;
316
            posLine += 2;
317
            makeBasPage(posLine);
318
 
319
            posLine++;
320
            firstLine = posLine;
321
        }
322
 
323
        this.nbPage = new Double(Math.ceil((double) (mapSal.keySet().size() + 1) / (nbCol))).intValue();
324
 
325
        System.err.println("Nombre de page " + this.nbPage);
326
 
327
        // on conserve la page d'origine du model
328
 
329
        if (this.nbPage > 0) {
330
            this.nbPage--;
331
        }
332
    }
333
 
334
    private Object fillLine(Map mapSalRub, int idRub, Map mapSal, int numSal, Map mapTotal) {
335
 
336
        Object value = null;
337
        if (numSal < mapSal.keySet().size()) {
338
            Map m = (Map) mapSalRub.get(mapSal.keySet().toArray()[numSal]);
339
 
340
            value = new Float(0);
341
            if (m != null && m.get(new Integer(idRub)) != null) {
342
                value = m.get(new Integer(idRub));
343
            }
344
        } else {
345
 
346
            if (numSal == mapSal.keySet().size()) {
347
                value = mapTotal.get(new Integer(idRub));
348
            }
349
        }
350
        return value;
351
    }
352
}