OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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