OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | 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.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.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.Where;
25
 
132 ilm 26
import java.math.BigDecimal;
18 ilm 27
import java.text.DateFormat;
28
import java.util.Date;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32
 
33
import org.apache.commons.dbutils.handlers.ArrayListHandler;
34
 
35
public class EtatChargesPayeSheet extends SheetInterface {
36
 
37
    private static int debutFill, endFill;
38
    private final static SQLTable tableFichePaye = base.getTable("FICHE_PAYE");
39
    private final static SQLTable tableFichePayeElement = base.getTable("FICHE_PAYE_ELEMENT");
40
    private final static SQLTable tableMois = base.getTable("MOIS");
41
    private final static SQLTable tableCaisse = Configuration.getInstance().getBase().getTable("CAISSE_COTISATION");
42
    private final static SQLTable tableRubCot = Configuration.getInstance().getBase().getTable("RUBRIQUE_COTISATION");
43
 
142 ilm 44
    private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
18 ilm 45
    private int moisDu, moisAu;
46
    private String annee;
47
 
48
    public static void setSize(int debut, int fin) {
49
        debutFill = debut;
50
        endFill = fin;
51
 
52
    }
53
 
54
    static {
55
        setSize(7, 66);
56
    }
57
 
25 ilm 58
    public static String TEMPLATE_ID = "Etat des charges";
59
    public static String TEMPLATE_PROPERTY_NAME = "LocationEtatChargesPaye";
18 ilm 60
 
25 ilm 61
    @Override
62
    public String getTemplateId() {
63
        return TEMPLATE_ID;
18 ilm 64
    }
65
 
25 ilm 66
    @Override
67
    protected String getYear() {
68
        return "";
69
    }
70
 
18 ilm 71
    public EtatChargesPayeSheet(int moisDu, int moisAu, String annee) {
72
        super();
73
 
74
        this.printer = PrinterNXProps.getInstance().getStringProperty("EtatChargesPayePrinter");
75
        this.modele = "EtatChargesPaye.ods";
76
        this.moisAu = moisAu;
77
        this.moisDu = moisDu;
78
        this.annee = annee;
79
 
80
        this.nbRowsPerPage = 68;
81
 
82
        createMap();
83
    }
84
 
85
    private void makeEntete(int row) {
86
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
87
        this.mCell.put("A" + row, rowSociete.getObject("NOM"));
88
        this.mCell.put("F" + row, "Edition du " + dateFormat.format(new Date()));
89
        // this.mCell.put("D" + (row + 2), "Impression Journaux");
90
        System.err.println("MAKE ENTETE");
91
    }
92
 
93
    private void makeBasPage(int row) {
94
        SQLRow rowMoisDu = tableMois.getRow(this.moisDu);
95
        SQLRow rowMoisAu = tableMois.getRow(this.moisAu);
96
 
97
        this.mCell.put("A" + row, "Période de " + rowMoisDu.getString("NOM") + " à " + rowMoisAu.getString("NOM") + " " + this.annee);
98
    }
99
 
100
    protected void createMap() {
101
 
102
        this.mapReplace = new HashMap();
103
        this.mCell = new HashMap();
104
        this.mapStyleRow = new HashMap();
105
 
106
        SQLSelect sel = new SQLSelect(base);
107
        sel.addSelect(tableFichePaye.getField("ID"));
108
        sel.addSelect(tableFichePayeElement.getField("ID"));
109
        // sel.addSelect(tableSalarie.getField("ID"));
110
 
111
        Where w = (new Where(tableFichePayeElement.getField("ID_FICHE_PAYE"), "=", tableFichePaye.getField("ID")));
112
        Where w6 = (new Where(tableFichePayeElement.getField("SOURCE"), "=", "RUBRIQUE_COTISATION"));
113
        // Where w2 = (new Where(tableFichePaye.getField("ID_SALARIE"), "=",
114
        // tableSalarie.getField("ID")));
115
        Where w3 = (new Where(tableFichePaye.getField("ID_MOIS"), new Integer(this.moisDu), new Integer(this.moisAu)));
116
        Where w4 = (new Where(tableFichePaye.getField("ANNEE"), "=", new Integer(this.annee)));
117
        Where w5 = (new Where(tableFichePaye.getField("VALIDE"), "=", Boolean.TRUE));
118
 
119
        sel.setWhere(w);
120
        // sel.andWhere(w2);
121
        sel.andWhere(w3);
122
        sel.andWhere(w4);
123
        sel.andWhere(w5);
124
        sel.andWhere(w6);
125
        sel.setDistinct(true);
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 idCaisse, Map rowValsRubCotCumulé
134
        Map mapCaisse = new HashMap();
135
        Map mapFiche = new HashMap();
136
 
137
        // Cumuls des rubriques de cotisations par caisse
138
        for (int i = 0; i < l.size(); i++) {
139
            Object[] tmp = (Object[]) l.get(i);
140
            mapFiche.put(tmp[0], "");
141
            int idFicheElt = Integer.parseInt(tmp[1].toString());
142
 
143
            SQLRow rowFicheElt = tableFichePayeElement.getRow(idFicheElt);
144
            SQLRow rowRub = tableRubCot.getRow(rowFicheElt.getInt("IDSOURCE"));
145
 
146
            // On recupere la map de la caisse
147
            Map mapValueRub;
148
            if (mapCaisse.containsKey(new Integer(rowRub.getInt("ID_CAISSE_COTISATION")))) {
149
                mapValueRub = (Map) mapCaisse.get(new Integer(rowRub.getInt("ID_CAISSE_COTISATION")));
150
            } else {
151
                mapValueRub = new HashMap();
152
                mapCaisse.put(new Integer(rowRub.getInt("ID_CAISSE_COTISATION")), mapValueRub);
153
            }
154
 
155
            // on recupere la rowvalues de la rubrique
156
            SQLRowValues rowVals;
157
            if (mapValueRub.containsKey(rowFicheElt.getObject("IDSOURCE"))) {
158
                rowVals = (SQLRowValues) mapValueRub.get(rowFicheElt.getObject("IDSOURCE"));
159
                // on cumule les données
160
                if (rowFicheElt.getObject("NB_BASE") != null) {
161
                    Object o = rowVals.getObject("NB_BASE");
132 ilm 162
                    BigDecimal base = (o == null) ? BigDecimal.ZERO : ((BigDecimal) o);
163
                    base = base.add(rowFicheElt.getBigDecimal("NB_BASE"));
164
                    rowVals.put("NB_BASE", base);
18 ilm 165
                }
166
                if (rowFicheElt.getObject("MONTANT_PAT") != null) {
167
                    Object o = rowVals.getObject("MONTANT_PAT");
132 ilm 168
                    BigDecimal montant = (o == null) ? BigDecimal.ZERO : ((BigDecimal) o);
169
                    montant = montant.add(rowFicheElt.getBigDecimal("MONTANT_PAT"));
170
                    rowVals.put("MONTANT_PAT", montant);
18 ilm 171
                }
172
                if (rowFicheElt.getObject("MONTANT_SAL_DED") != null) {
173
                    Object o = rowVals.getObject("MONTANT_SAL_DED");
132 ilm 174
                    BigDecimal montant = (o == null) ? BigDecimal.ZERO : ((BigDecimal) o);
175
                    montant = montant.add(rowFicheElt.getBigDecimal("MONTANT_SAL_DED"));
176
                    rowVals.put("MONTANT_SAL_DED", montant);
18 ilm 177
                }
178
            } else {
179
                rowVals = new SQLRowValues(tableFichePayeElement);
73 ilm 180
                Configuration.getInstance().getDirectory().getElement(tableFichePayeElement).loadAllSafe(rowVals, rowFicheElt);
132 ilm 181
                BigDecimal montantPat, montantSal;
18 ilm 182
 
183
                Object o = rowVals.getObject("MONTANT_PAT");
132 ilm 184
                montantPat = (o == null) ? BigDecimal.ZERO : ((BigDecimal) o);
18 ilm 185
 
186
                o = rowVals.getObject("MONTANT_SAL_DED");
132 ilm 187
                montantSal = (o == null) ? BigDecimal.ZERO : ((BigDecimal) o);
18 ilm 188
 
132 ilm 189
                if (montantPat.signum() != 0 || montantSal.signum() != 0) {
18 ilm 190
                    mapValueRub.put(rowFicheElt.getObject("IDSOURCE"), rowVals);
191
                }
192
            }
193
 
194
        }
195
 
196
        // Fill
197
        int posLine = 1;
198
        int firstLine = 1;
199
 
200
        System.err.println("Dump fiche " + mapFiche);
201
        System.err.println("NB Pages = " + mapCaisse.keySet().size());
202
 
203
        for (int n = 0; n < mapCaisse.keySet().size(); n++) {
204
 
205
            // entete
206
            makeEntete(posLine);
207
            posLine += (debutFill - 1);
208
 
209
            Map mapValue = (Map) mapCaisse.get(mapCaisse.keySet().toArray()[n]);
132 ilm 210
            BigDecimal totalMontantSal = BigDecimal.ZERO;
211
            BigDecimal totalMontantPat = BigDecimal.ZERO;
18 ilm 212
 
213
            SQLRow rowCaisse = tableCaisse.getRow(Integer.parseInt(mapCaisse.keySet().toArray()[n].toString()));
214
            this.mCell.put("A" + posLine, "Caisse " + rowCaisse.getObject("NOM"));
215
            this.mCell.put("B" + posLine, "");
216
            this.mCell.put("C" + posLine, "");
217
            this.mCell.put("D" + posLine, "");
218
            this.mCell.put("E" + posLine, "");
219
            this.mCell.put("F" + posLine, "");
220
            this.mapStyleRow.put(new Integer(posLine), "Titre 1");
221
 
222
            posLine++;
223
 
224
            for (int i = 0; i < mapValue.keySet().size(); i++) {
225
                SQLRowValues rowVals = (SQLRowValues) mapValue.get(mapValue.keySet().toArray()[i]);
226
 
227
                this.mCell.put("A" + posLine, rowVals.getObject("NOM"));
228
                this.mCell.put("B" + posLine, rowVals.getObject("NB_BASE"));
229
 
132 ilm 230
                BigDecimal txSal = rowVals.getBigDecimal("TAUX_SAL");
231
                txSal = (txSal == null) ? BigDecimal.ZERO : txSal;
232
                BigDecimal txPat = rowVals.getBigDecimal("TAUX_PAT");
233
                txPat = (txPat == null) ? BigDecimal.ZERO : txPat;
234
                this.mCell.put("C" + posLine, txSal.add(txPat));
18 ilm 235
 
132 ilm 236
                // System.err.println(rowVals.getObject("MONTANT_SAL_DED").getClass());
237
                BigDecimal montantSal = rowVals.getBigDecimal("MONTANT_SAL_DED");
238
                montantSal = (montantSal == null) ? BigDecimal.ZERO : montantSal;
239
                BigDecimal montantPat = rowVals.getBigDecimal("MONTANT_PAT");
240
                montantPat = (montantPat == null) ? BigDecimal.ZERO : montantPat;
241
                this.mCell.put("D" + posLine, montantPat);
242
                this.mCell.put("E" + posLine, montantSal);
243
                this.mCell.put("F" + posLine, montantSal.add(montantPat));
244
                totalMontantPat = totalMontantPat.add(montantPat);
245
                totalMontantSal = totalMontantSal.add(montantSal);
18 ilm 246
 
247
                this.mapStyleRow.put(new Integer(posLine), "Normal");
248
                posLine++;
249
            }
250
 
251
            this.mCell.put("A" + posLine, "Total");
252
            this.mCell.put("B" + posLine, "");
253
            this.mCell.put("C" + posLine, "");
132 ilm 254
            this.mCell.put("D" + posLine, totalMontantPat);
255
            this.mCell.put("E" + posLine, totalMontantSal);
256
            this.mCell.put("F" + posLine, totalMontantPat.add(totalMontantSal));
18 ilm 257
            this.mapStyleRow.put(new Integer(posLine), "Titre 1");
258
 
259
            // pied de page
260
            posLine = firstLine + endFill - 1;
261
            posLine += 2;
262
            makeBasPage(posLine);
263
 
264
            posLine++;
265
            firstLine = posLine;
266
        }
267
 
268
        this.nbPage = mapCaisse.size();
269
 
182 ilm 270
        if (this.nbPage < 1) {
271
            this.nbPage = 1;
272
        }
18 ilm 273
        System.err.println("Nombre de page " + this.nbPage);
274
 
275
    }
276
}