OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
91 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
 
144 ilm 16
import org.openconcerto.erp.core.common.element.StyleSQLElement;
91 ilm 17
import org.openconcerto.erp.generationDoc.AbstractSheetXMLWithDate;
149 ilm 18
import org.openconcerto.erp.preferences.PayeGlobalPreferencePanel;
91 ilm 19
import org.openconcerto.erp.preferences.PrinterNXProps;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.SQLRow;
144 ilm 22
import org.openconcerto.sql.model.SQLRowAccessor;
23
import org.openconcerto.sql.model.SQLRowListRSH;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
149 ilm 27
import org.openconcerto.sql.preferences.SQLPreferences;
144 ilm 28
import org.openconcerto.utils.cc.ITransformer;
91 ilm 29
 
144 ilm 30
import java.math.BigDecimal;
31
import java.util.ArrayList;
142 ilm 32
import java.util.Calendar;
144 ilm 33
import java.util.HashMap;
34
import java.util.HashSet;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Set;
142 ilm 38
 
144 ilm 39
import javax.swing.JOptionPane;
40
import javax.swing.SwingUtilities;
41
 
91 ilm 42
public class FichePayeSheetXML extends AbstractSheetXMLWithDate {
43
 
149 ilm 44
    public static final String TEMPLATE_ID = "FichePayeSimplifiee";
91 ilm 45
    public static final String TEMPLATE_PROPERTY_NAME = "LocationFichePaye";
144 ilm 46
    final Map<Integer, String> cotisationSimplifieeLink = new HashMap<>();
91 ilm 47
 
144 ilm 48
    public FichePayeSheetXML(final SQLRow row) {
177 ilm 49
        this(row, !new SQLPreferences(row.getTable().getDBRoot()).getBoolean(PayeGlobalPreferencePanel.NOT_PAYE_SIMPL, Boolean.FALSE));
50
    }
51
 
52
    public FichePayeSheetXML(final SQLRow row, boolean simplifie) {
91 ilm 53
        super(row);
54
        this.printer = PrinterNXProps.getInstance().getStringProperty("FichePayePrinter");
55
        this.elt = Configuration.getInstance().getDirectory().getElement("FICHE_PAYE");
149 ilm 56
 
177 ilm 57
        if (simplifie) {
58
 
144 ilm 59
            SQLSelect sel = new SQLSelect();
60
            sel.addSelect(row.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getField("LIGNE_PAYE_SIMPLIFIEE"));
61
            sel.addSelect(row.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getKey());
62
            List<SQLRow> rubCotis = SQLRowListRSH.execute(sel);
91 ilm 63
 
144 ilm 64
            for (SQLRow sqlRow : rubCotis) {
65
                cotisationSimplifieeLink.put(sqlRow.getID(), sqlRow.getString("LIGNE_PAYE_SIMPLIFIEE"));
66
            }
67
 
68
            final String codeStatu = row.getForeign("ID_SALARIE").getForeign("ID_INFOS_SALARIE_PAYE").getForeign("ID_CONTRAT_SALARIE").getForeign("ID_CODE_STATUT_CATEGORIEL").getString("CODE");
69
            final boolean cadre = codeStatu.equals("01") || codeStatu.equals("02");
70
 
71
            final Set<Integer> ignoredRubriqueComm = new HashSet<Integer>();
72
            // Total nat
73
            ignoredRubriqueComm.add(2);
74
            // Total brut
75
            // ignoredRubriqueComm.add(3);
76
            // Ligne vide
77
            ignoredRubriqueComm.add(7);
78
            // Cotisations sal
79
            ignoredRubriqueComm.add(4);
80
            // Cotisation pat
81
            ignoredRubriqueComm.add(5);
82
 
83
            this.setPostProcess(new ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>>() {
84
 
85
                @Override
86
                public List<SQLRowAccessor> transformChecked(List<SQLRowAccessor> input) {
87
 
88
                    List<SQLRowAccessor> result = new ArrayList<SQLRowAccessor>(input.size());
89
                    List<SQLRowAccessor> resultCom = new ArrayList<SQLRowAccessor>(input.size());
90
                    List<SQLRowAccessor> resultNet = new ArrayList<SQLRowAccessor>(input.size());
91
 
92
                    List<SQLRowAccessor> resultCotis = new ArrayList<SQLRowAccessor>(input.size());
93
                    Map<String, SQLRowValues> mapLigneSimplifiee = initCotisations(cadre, resultCotis);
94
                    for (SQLRowAccessor sqlRowAccessor : input) {
95
                        if (sqlRowAccessor.getBoolean("IN_PERIODE")) {
96
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_BRUT")) {
97
                                if (result.size() == 0) {
98
                                    SQLRowValues rowValsTitle = new SQLRowValues(sqlRowAccessor.getTable());
99
                                    rowValsTitle.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 1"));
100
                                    rowValsTitle.put("NOM", "Rubrique de brut");
101
                                    rowValsTitle.put("IMPRESSION", Boolean.TRUE);
102
                                    rowValsTitle.put("IN_PERIODE", Boolean.TRUE);
103
                                    result.add(rowValsTitle);
104
                                }
105
                                SQLRowValues rowValsToAdd = sqlRowAccessor.asRowValues();
106
                                rowValsToAdd.put("ID_STYLE", 2);
107
                                result.add(rowValsToAdd);
108
                            }
109
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_NET") && sqlRowAccessor.getBoolean("IMPRESSION") && sqlRowAccessor.getBoolean("IN_PERIODE")) {
110
                                if (resultNet.size() == 0) {
111
                                    SQLRowValues rowValsTitle = new SQLRowValues(sqlRowAccessor.getTable());
112
                                    rowValsTitle.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 1"));
113
                                    rowValsTitle.put("NOM", "Rubrique de net");
114
                                    rowValsTitle.put("IMPRESSION", Boolean.TRUE);
115
                                    rowValsTitle.put("IN_PERIODE", Boolean.TRUE);
116
                                    resultNet.add(rowValsTitle);
117
                                }
118
                                SQLRowValues rowValsToAdd = sqlRowAccessor.asRowValues();
119
                                rowValsToAdd.put("ID_STYLE", 2);
120
                                resultNet.add(rowValsToAdd);
121
                            }
122
 
123
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_COMM") && !ignoredRubriqueComm.contains(sqlRowAccessor.getInt("IDSOURCE"))) {
124
                                if (resultNet.size() == 0) {
125
                                    result.add(sqlRowAccessor);
126
                                } else {
127
                                    resultNet.add(sqlRowAccessor);
128
                                }
129
 
130
                            }
131
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_COTISATION")) {
132
                                cumuls(sqlRowAccessor, mapLigneSimplifiee, cadre);
133
                            }
134
                        }
135
                    }
136
                    result.addAll(resultNet);
137
                    result.addAll(resultCom);
138
                    result.addAll(resultCotis);
139
                    return result;
140
                }
141
            });
142
        }
91 ilm 143
    }
144
 
144 ilm 145
    private void cumuls(final SQLRowAccessor sqlRowAccessor, Map<String, SQLRowValues> mapLigneSimplifiee, final boolean cadre) {
146
        // "TAUX_PAT" numeric(16,6) DEFAULT 0,
147
        // "TAUX_SAL" numeric(16,6) DEFAULT 0,
148
        // "NB_BASE" numeric(16,6) DEFAULT 0,
149
        // "MONTANT_SAL_AJ" numeric(16,2) DEFAULT 0,
150
        // "MONTANT_PAT" numeric(16,2) DEFAULT 0,
151
        // "MONTANT_SAL_DED" numeric(16,2) DEFAULT 0,
152
        String idLigneSimplifiee = this.cotisationSimplifieeLink.get(sqlRowAccessor.getInt("IDSOURCE"));
153
        if (idLigneSimplifiee == null || idLigneSimplifiee.trim().length() == 0) {
154
            final SQLRow rowRub = sqlRowAccessor.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getRow(sqlRowAccessor.getInt("IDSOURCE"));
155
 
156
            SwingUtilities.invokeLater(new Runnable() {
157
 
158
                @Override
159
                public void run() {
160
                    JOptionPane.showMessageDialog(null, "Aucune liaison définie vers une ligne de bulletin simplifié pour la cotisation " + rowRub.getString("CODE") + " - " + rowRub.getString("NOM")
161
                            + ".\n La liaison sera faite sur autres contributions dues par l'employeur!");
162
                }
163
            });
164
            idLigneSimplifiee = LignePayeSimplifiee.AUTRES_CONTRIBUTIONS.getId();
165
        }
166
 
167
        if (!idLigneSimplifiee.equals(LignePayeSimplifiee.IGNORE.getId())) {
168
            SQLRowValues ligneSimplifiee = mapLigneSimplifiee.get(idLigneSimplifiee);
169
            if (ligneSimplifiee == null) {
170
 
171
                final LignePayeSimplifiee fromID = LignePayeSimplifiee.fromID(idLigneSimplifiee);
172
                SwingUtilities.invokeLater(new Runnable() {
173
 
174
                    @Override
175
                    public void run() {
176
                        JOptionPane.showMessageDialog(null,
177
                                "Aucune ligne simplifiée " + fromID + " dans le bulletin " + (cadre ? "cadre!" : "non cadre!") + "\n La ligne " + sqlRowAccessor.getString("NOM") + " a été ignoré!");
178
                    }
179
                });
180
 
181
            } else {
182
                BigDecimal montantPatBulletinSimpl = BigDecimal.ZERO;
183
                BigDecimal montantSalBulletinSimpl = BigDecimal.ZERO;
184
                if (ligneSimplifiee.getBigDecimal("MONTANT_SAL_DED") != null) {
185
                    montantSalBulletinSimpl = ligneSimplifiee.getBigDecimal("MONTANT_SAL_DED");
186
                }
187
                if (ligneSimplifiee.getBigDecimal("MONTANT_PAT") != null) {
188
                    montantPatBulletinSimpl = ligneSimplifiee.getBigDecimal("MONTANT_PAT");
189
                }
190
 
191
                BigDecimal montantPatLigne = BigDecimal.ZERO;
192
                BigDecimal montantSalLigne = BigDecimal.ZERO;
193
                if (sqlRowAccessor.getBigDecimal("MONTANT_SAL_DED") != null) {
194
                    montantSalLigne = sqlRowAccessor.getBigDecimal("MONTANT_SAL_DED");
195
                }
196
                if (sqlRowAccessor.getBigDecimal("MONTANT_PAT") != null) {
197
                    montantPatLigne = sqlRowAccessor.getBigDecimal("MONTANT_PAT");
198
                }
149 ilm 199
                if (montantPatLigne.add(montantSalLigne).signum() != 0) {
200
                    ligneSimplifiee.put("NB_BASE", sqlRowAccessor.getBigDecimal("NB_BASE"));
201
                    ligneSimplifiee.put("MONTANT_SAL_DED", montantSalBulletinSimpl.add(montantSalLigne));
202
                    ligneSimplifiee.put("MONTANT_PAT", montantPatBulletinSimpl.add(montantPatLigne));
203
                }
174 ilm 204
 
205
                BigDecimal tauxPatBulletinSimpl = BigDecimal.ZERO;
206
                BigDecimal tauxSalBulletinSimpl = BigDecimal.ZERO;
207
                if (ligneSimplifiee.getBigDecimal("TAUX_SAL") != null) {
208
                    tauxSalBulletinSimpl = ligneSimplifiee.getBigDecimal("TAUX_SAL");
209
                }
210
                if (ligneSimplifiee.getBigDecimal("TAUX_PAT") != null) {
211
                    tauxPatBulletinSimpl = ligneSimplifiee.getBigDecimal("TAUX_PAT");
212
                }
213
 
214
                BigDecimal tauxPatLigne = BigDecimal.ZERO;
215
                BigDecimal tauxSalLigne = BigDecimal.ZERO;
216
                if (sqlRowAccessor.getBigDecimal("TAUX_SAL") != null) {
217
                    tauxSalLigne = sqlRowAccessor.getBigDecimal("TAUX_SAL");
218
                }
219
                if (sqlRowAccessor.getBigDecimal("TAUX_PAT") != null) {
220
                    tauxPatLigne = sqlRowAccessor.getBigDecimal("TAUX_PAT");
221
                }
222
 
223
                if (tauxSalLigne.signum() > 0) {
224
                    ligneSimplifiee.put("TAUX_SAL", tauxSalBulletinSimpl.add(tauxSalLigne));
225
                }
226
 
227
                if (tauxPatLigne.signum() > 0) {
228
                    ligneSimplifiee.put("TAUX_PAT", tauxPatBulletinSimpl.add(tauxPatLigne));
229
                }
144 ilm 230
            }
231
        }
232
    }
233
 
234
    private StyleSQLElement eltStyle = Configuration.getInstance().getDirectory().getElement(StyleSQLElement.class);
235
 
236
    private Map<String, SQLRowValues> initCotisations(boolean cadre, List<SQLRowAccessor> resultCotis) {
237
 
238
        SQLTable tableFichePayeElt = this.row.getTable().getDBRoot().findTable("FICHE_PAYE_ELEMENT");
239
        SQLRowValues rowValsCotis = new SQLRowValues(tableFichePayeElt);
240
 
241
        rowValsCotis.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 2"));
156 ilm 242
        rowValsCotis.put("NOM", "Cotisations et contributions sociales");
144 ilm 243
        // rowValsCotis.put("NB_BASE", BigDecimal.ZERO);
244
        // rowValsCotis.put("TAUX_SAL", BigDecimal.ZERO);
245
        // rowValsCotis.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
246
        // rowValsCotis.put("MONTANT_SAL_DED", BigDecimal.ZERO);
247
        // rowValsCotis.put("MONTANT_PAT", BigDecimal.ZERO);
248
        rowValsCotis.put("IMPRESSION", Boolean.TRUE);
249
        rowValsCotis.put("IN_PERIODE", Boolean.TRUE);
250
        resultCotis.add(rowValsCotis);
251
 
252
        Map<String, SQLRowValues> mapPayeSimplifiee = new HashMap<String, SQLRowValues>();
253
        List<GroupePayeSimplifiee> listGroupe = cadre ? StructurePayeSimplifiee.getStructureCadre() : StructurePayeSimplifiee.getStructureNonCadre();
254
        for (GroupePayeSimplifiee groupe : listGroupe) {
255
            SQLRowValues rowValsFichePaye = new SQLRowValues(tableFichePayeElt);
256
            rowValsFichePaye.put("NOM", groupe.getTranslation());
257
            rowValsFichePaye.put("IMPRESSION", Boolean.TRUE);
258
            rowValsFichePaye.put("IN_PERIODE", Boolean.TRUE);
259
            rowValsFichePaye.put("ID_STYLE", eltStyle.getAllStyleByName().get("Normal"));
260
            // rowValsFichePaye.put("NB_BASE", BigDecimal.ZERO);
261
            // rowValsFichePaye.put("TAUX_SAL", BigDecimal.ZERO);
262
            // rowValsFichePaye.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
263
            // rowValsFichePaye.put("MONTANT_SAL_DED", BigDecimal.ZERO);
264
            // rowValsFichePaye.put("MONTANT_PAT", BigDecimal.ZERO);
265
            resultCotis.add(rowValsFichePaye);
266
            mapPayeSimplifiee.put(groupe.getId(), rowValsFichePaye);
267
            for (LignePayeSimplifiee lignePayeSimplifiee : groupe.getChildren()) {
268
 
269
                if (groupe.isShowChildren()) {
270
                    SQLRowValues rowValsLPaye = new SQLRowValues(tableFichePayeElt);
271
                    rowValsLPaye.put("NOM", lignePayeSimplifiee.getTranslation());
272
                    rowValsLPaye.put("IMPRESSION", Boolean.TRUE);
273
                    rowValsLPaye.put("IN_PERIODE", Boolean.TRUE);
274
                    rowValsLPaye.put("ID_STYLE", eltStyle.getAllStyleByName().get("Normal"));
275
                    // rowValsLPaye.put("NB_BASE", BigDecimal.ZERO);
276
                    // rowValsLPaye.put("TAUX_SAL", BigDecimal.ZERO);
277
                    // rowValsLPaye.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
278
                    // rowValsLPaye.put("MONTANT_SAL_DED", BigDecimal.ZERO);
279
                    // rowValsLPaye.put("MONTANT_PAT", BigDecimal.ZERO);
280
                    mapPayeSimplifiee.put(lignePayeSimplifiee.getId(), rowValsLPaye);
281
                    resultCotis.add(rowValsLPaye);
282
                } else {
283
                    mapPayeSimplifiee.put(lignePayeSimplifiee.getId(), rowValsFichePaye);
284
                }
285
            }
286
        }
287
        return mapPayeSimplifiee;
288
    }
289
 
91 ilm 290
    @Override
291
    public String getDefaultTemplateId() {
292
        return TEMPLATE_ID;
293
    }
294
 
295
    @Override
296
    public String getName() {
149 ilm 297
        SQLPreferences prefs = new SQLPreferences(elt.getTable().getDBRoot());
298
        boolean prefNomSal = prefs.getBoolean(PayeGlobalPreferencePanel.SALARIE_NOM_FICHIER, Boolean.FALSE);
91 ilm 299
        SQLRow rowSal = row.getForeign("ID_SALARIE");
300
        SQLRow rowMois = row.getForeign("ID_MOIS");
142 ilm 301
        Calendar du = row.getDate("DU");
302
        String suffix = "";
149 ilm 303
        if (prefNomSal) {
304
            suffix = "_" + rowSal.getString("NOM");
305
        }
142 ilm 306
        if (du != null && du.get(Calendar.DAY_OF_MONTH) != 1) {
307
            suffix = "_" + du.get(Calendar.DAY_OF_MONTH);
308
        }
309
        return ("FichePaye_" + rowSal.getString("CODE") + suffix + "_" + rowMois.getString("NOM") + "_" + row.getString("ANNEE"));
91 ilm 310
    }
311
 
312
}