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;
25 ilm 17
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
18 ilm 18
import org.openconcerto.erp.generationDoc.SheetInterface;
19
import org.openconcerto.erp.generationDoc.SpreadSheetGeneratorGestComm;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.jopendocument.link.Component;
22
import org.jopendocument.link.OOConnexion;
23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.utils.ExceptionHandler;
28
 
29
import java.io.File;
132 ilm 30
import java.math.BigDecimal;
18 ilm 31
import java.text.DateFormat;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.HashMap;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Map;
38
 
83 ilm 39
import javax.swing.JFrame;
18 ilm 40
import javax.swing.JOptionPane;
41
 
42
import org.apache.commons.dbutils.handlers.ArrayListHandler;
43
 
44
public class FichePayeSheet extends SheetInterface {
45
    // TODO Ajouter un champ DIF dans le modele pour le remplir a la main --> Droit individuel à la
46
    // formation
47
    private final static SQLTable tableFiche = base.getTable("FICHE_PAYE");
48
    private final static SQLTable tableFicheElt = base.getTable("FICHE_PAYE_ELEMENT");
49
    private final static SQLTable tableMois = base.getTable("MOIS");
50
    private final static SQLTable tableAdresse = base.getTable("ADRESSE");
51
    private final static SQLTable tableAdresseCommon = Configuration.getInstance().getBase().getTable("ADRESSE_COMMON");
52
    private final static SQLTable tableSalarie = base.getTable("SALARIE");
53
    private final static SQLTable tableEtatCivil = base.getTable("ETAT_CIVIL");
54
    private final static SQLTable tableInfosPaye = base.getTable("INFOS_SALARIE_PAYE");
55
    private final static SQLTable tableReglementPaye = base.getTable("REGLEMENT_PAYE");
56
    private final static SQLTable tableContrat = base.getTable("CONTRAT_SALARIE");
57
    private final static SQLTable tableModeRegl = base.getTable("MODE_REGLEMENT_PAYE");
58
    private final static SQLTable tableCumulsConges = base.getTable("CUMULS_CONGES");
59
    private final static SQLTable tableCumulsPaye = base.getTable("CUMULS_PAYE");
60
    private final static SQLTable tableVarPeriode = base.getTable("VARIABLE_SALARIE");
61
    private final static SQLTable tableConventionC = base.getTable("IDCC");
62
 
63
    private Map styleMapRow;
64
 
65
    public Map getStyleMapRow() {
66
        return this.styleMapRow;
67
    }
68
 
69
    public FichePayeSheet(int idFiche) {
70
        super(idFiche, tableFiche);
71
        init();
72
    }
73
 
74
    public FichePayeSheet(SQLRow rowFiche) {
75
        super(rowFiche);
76
        init();
77
    }
78
 
79
    // Nom du fichier généré
80
    public static String getFileName(int id, int type) {
81
        return getFileName(tableFiche.getRow(id), type);
82
    }
83
 
84
    // génération d'une fiche de paye
85
    public static void generation(int id) {
86
        generation(tableFiche.getRow(id));
87
    }
88
 
89
    public static void generation(SQLRow row) {
90
        generation(row, true);
91
    }
92
 
93
    public static void generation(SQLRow row, boolean visu) {
94
        FichePayeSheet fSheet = new FichePayeSheet(row.getID());
95
        new SpreadSheetGeneratorGestComm(fSheet, FichePayeSheet.getFileName(row, FichePayeSheet.typeNoExtension), false, visu);
96
    }
97
 
98
    // impression d'une fiche de paye
99
    public static void impression(int id) {
100
        impression(tableFiche.getRow(id));
101
    }
102
 
103
    public static void impression(SQLRow row) {
104
        final File f = getFile(row, typeOO);
105
        if (f.exists()) {
106
            try {
107
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
108
                if (ooConnexion == null) {
109
                    return;
110
                }
111
                final Component doc = ooConnexion.loadDocument(f, true);
112
                Map<String, Object> map = new HashMap<String, Object>();
113
                map.put("Name", PrinterNXProps.getInstance().getStringProperty("FichePayePrinter"));
114
                doc.printDocument(map);
115
                doc.close();
83 ilm 116
            } catch (LinkageError e) {
117
                JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
18 ilm 118
            } catch (Exception e) {
119
                e.printStackTrace();
120
                ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
121
            }
122
        }
123
    }
124
 
125
    // visualisation d'une fiche
126
    public static void visualisation(SQLRow r, int type) {
127
        final File f = getFile(r, type);
128
        if (f.exists()) {
129
            try {
130
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
131
                if (ooConnexion == null) {
132
                    return;
133
                }
134
                ooConnexion.loadDocument(f, false);
83 ilm 135
            } catch (LinkageError e) {
136
                JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
18 ilm 137
            } catch (Exception e) {
138
                // TODO Auto-generated catch block
139
                e.printStackTrace();
140
                ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
141
            }
142
        }
143
    }
144
 
145
    public static File getFile(SQLRow r, int type) {
146
        return new File(getLocation(r, type), getFileName(r.getID(), type));
147
    }
148
 
149
    public static String getFileName(SQLRow r, int type) {
150
 
151
        SQLRow rowSal = tableSalarie.getRow(r.getInt("ID_SALARIE"));
152
        SQLRow rowMois = tableMois.getRow(r.getInt("ID_MOIS"));
153
        if (type == FichePayeSheet.typeOO) {
154
            return ("FichePaye_" + rowSal.getString("CODE") + "_" + rowMois.getString("NOM") + "_" + r.getString("ANNEE") + ".ods");
155
        } else {
156
            if (type == FichePayeSheet.typePDF) {
157
                return ("FichePaye_" + rowSal.getString("CODE") + "_" + rowMois.getString("NOM") + "_" + r.getString("ANNEE") + ".pdf");
158
            } else {
159
                return ("FichePaye_" + rowSal.getString("CODE") + "_" + rowMois.getString("NOM") + "_" + r.getString("ANNEE"));
160
            }
161
        }
162
    }
163
 
164
    // Emplacement des fichiers générés
165
    public static String getLocation(int id, int type) {
166
        return getLocation(tableFiche.getRow(id), type);
167
    }
168
 
25 ilm 169
    public static final String TEMPLATE_ID = "Fiche de paye";
170
    public static final String TEMPLATE_PROPERTY_NAME = "LocationFichePaye";
18 ilm 171
 
25 ilm 172
    @Override
173
    public String getTemplateId() {
174
        return TEMPLATE_ID;
18 ilm 175
    }
176
 
25 ilm 177
    @Override
178
    protected String getYear() {
179
        // TODO Auto-generated method stub
180
        return this.row.getString("ANNEE");
181
    }
182
 
18 ilm 183
    public static String getLocation(SQLRow r, int type) {
25 ilm 184
        DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
185
        String path;
186
        if (type == FichePayeSheet.typeOO) {
187
            path = storage.getDocumentOutputDirectory(TEMPLATE_ID).getAbsolutePath();
188
        } else {
189
            path = storage.getPDFOutputDirectory(TEMPLATE_ID).getAbsolutePath();
190
        }
18 ilm 191
 
25 ilm 192
        return path + File.separator + r.getString("ANNEE");
18 ilm 193
    }
194
 
195
    private void init() {
196
        this.modele = "FichePaye.ods";
197
        this.printer = PrinterNXProps.getInstance().getStringProperty("FichePayePrinter");
198
    }
199
 
182 ilm 200
    @Override
201
    public int getNbPage() {
202
        return 1;
203
    }
204
 
18 ilm 205
    protected void createMap() {
206
 
142 ilm 207
        final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
208
 
18 ilm 209
        this.styleMapRow = new HashMap();
210
        this.mapReplace = new HashMap();
211
 
212
        this.mCell = new HashMap();
213
 
214
        // Infos societe
215
        SQLRow rowSociete = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
216
        this.mCell.put("B1", rowSociete.getObject("TYPE") + " " + rowSociete.getObject("NOM"));
217
        SQLRow rowAdrSociete = tableAdresseCommon.getRow(rowSociete.getInt("ID_ADRESSE_COMMON"));
218
        this.mCell.put("B2", rowAdrSociete.getObject("RUE"));
73 ilm 219
        this.mCell.put("B3", rowAdrSociete.getString("CODE_POSTAL") + " " + rowAdrSociete.getString("VILLE"));
18 ilm 220
 
221
        this.mCell.put("D5", rowSociete.getObject("NUM_SIRET"));
222
        this.mCell.put("D6", rowSociete.getObject("NUM_APE"));
223
        this.mapReplace.put("D8", rowSociete.getObject("NUMERO_URSSAF"));
224
 
225
        // Infos Salarie
226
        SQLRow rowSal = tableSalarie.getRow(this.row.getInt("ID_SALARIE"));
227
        SQLRow rowEtatCivil = tableEtatCivil.getRow(rowSal.getInt("ID_ETAT_CIVIL"));
228
        this.mCell.put("G8", rowSal.getObject("NOM") + " " + rowSal.getObject("PRENOM"));
229
        SQLRow rowAdrSal = tableAdresse.getRow(rowEtatCivil.getInt("ID_ADRESSE"));
230
        this.mCell.put("G9", rowAdrSal.getObject("RUE"));
73 ilm 231
        this.mCell.put("G11", rowAdrSal.getString("CODE_POSTAL") + " " + rowAdrSal.getString("VILLE"));
18 ilm 232
 
233
        this.mCell.put("D13", rowEtatCivil.getObject("NUMERO_SS"));
234
 
235
        SQLRow rowInfosPaye = tableInfosPaye.getRow(rowSal.getInt("ID_INFOS_SALARIE_PAYE"));
236
        SQLRow rowContrat = tableContrat.getRow(rowInfosPaye.getInt("ID_CONTRAT_SALARIE"));
237
 
238
        if (this.row.getString("NATURE_EMPLOI").trim().length() == 0) {
239
            this.mCell.put("D14", rowContrat.getObject("NATURE"));
240
        } else {
241
            this.mCell.put("D14", this.row.getString("NATURE_EMPLOI"));
242
        }
243
 
244
        SQLRow rowCC;
245
        if (this.row.getInt("ID_IDCC") > 1) {
246
            rowCC = tableConventionC.getRow(this.row.getInt("ID_IDCC"));
247
        } else {
248
            rowCC = tableConventionC.getRow(rowInfosPaye.getInt("ID_IDCC"));
249
        }
250
        this.mCell.put("D15", rowCC.getString("NOM"));
251
 
252
        // Bulletin du
253
        // Bulletin de paie du
254
        Date du = (Date) this.row.getObject("DU");
255
        Date au = (Date) this.row.getObject("AU");
256
        this.mCell.put("F1", "Bulletin de paie du " + dateFormat.format(du) + " au " + dateFormat.format(au));
257
 
258
        // Paiement le
259
        SQLRow rowRegl;
260
        if (this.row.getInt("ID_REGLEMENT_PAYE") <= 1) {
261
            rowRegl = tableReglementPaye.getRow(rowSal.getInt("ID_REGLEMENT_PAYE"));
262
        } else {
263
            rowRegl = tableReglementPaye.getRow(this.row.getInt("ID_REGLEMENT_PAYE"));
264
        }
265
        SQLRow rowModeRegl = tableModeRegl.getRow(rowRegl.getInt("ID_MODE_REGLEMENT_PAYE"));
266
 
267
        Calendar c = Calendar.getInstance();
268
 
269
        c.set(Calendar.MONTH, this.row.getInt("ID_MOIS") - 2);
270
        c.set(Calendar.YEAR, Integer.parseInt(this.row.getString("ANNEE")));
271
 
272
        if (rowRegl.getInt("LE") != 31) {
273
 
274
            c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
275
        }
276
 
277
        int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
278
        int day = Math.min(rowRegl.getInt("LE"), max);
279
 
280
        c.set(Calendar.DAY_OF_MONTH, day);
281
 
282
        this.mCell.put("H3", dateFormat.format(c.getTime()));
283
        this.mCell.put("I3", "Par " + rowModeRegl.getObject("NOM"));
284
 
285
        // Congés
286
        // "G3";
287
        SQLRow rowConges;
288
        if (this.row.getInt("ID_CUMULS_CONGES") <= 1) {
289
            rowConges = tableCumulsConges.getRow(rowSal.getInt("ID_CUMULS_CONGES"));
290
        } else {
291
            rowConges = tableCumulsConges.getRow(this.row.getInt("ID_CUMULS_CONGES"));
292
        }
293
 
294
        SQLRow rowVarSal;
295
        if (this.row.getInt("ID_VARIABLE_SALARIE") <= 1) {
296
            rowVarSal = tableVarPeriode.getRow(rowSal.getInt("ID_VARIABLE_SALARIE"));
297
        } else {
298
            rowVarSal = tableVarPeriode.getRow(this.row.getInt("ID_VARIABLE_SALARIE"));
299
        }
300
 
301
        float congesPris = rowVarSal.getFloat("CONGES_PRIS");
302
        float congesRestant = rowConges.getFloat("RESTANT") - congesPris;
303
        float congesAcquis = rowConges.getFloat("ACQUIS") + this.row.getFloat("CONGES_ACQUIS");
304
        this.mCell.put("G14", new Float(congesPris));
305
        this.mCell.put("H14", new Float(congesRestant));
306
        this.mCell.put("I14", new Float(congesAcquis));
307
 
308
        // Element Devis
309
        SQLSelect selElt = new SQLSelect(base);
310
        selElt.addSelect(tableFicheElt.getField("ID"));
65 ilm 311
        selElt.setWhere(tableFicheElt.getField("ID_FICHE_PAYE"), "=", this.row.getID());
18 ilm 312
 
313
        String req = selElt.asString() + " ORDER BY \"FICHE_PAYE_ELEMENT\".\"POSITION\"";
314
        List l = (List) base.getDataSource().execute(req, new ArrayListHandler());
315
        int pos = 20;
316
        for (Iterator i = l.iterator(); i.hasNext();) {
317
            Object[] o = (Object[]) i.next();
318
            SQLRow rowTmp = tableFicheElt.getRow(Integer.parseInt(o[0].toString()));
319
 
320
            if (rowTmp.getBoolean("IMPRESSION") && rowTmp.getBoolean("IN_PERIODE")) {
321
 
322
                Object nomTmp = rowTmp.getObject("NOM");
323
                this.mCell.put("B" + pos, nomTmp);
324
 
325
                // Base
132 ilm 326
                BigDecimal baseTmp = rowTmp.getBigDecimal("NB_BASE");
18 ilm 327
 
328
                if (baseTmp != null) {
132 ilm 329
                    if (baseTmp.signum() != 0) {
18 ilm 330
                        this.mCell.put("E" + pos, baseTmp);
331
                    } else {
332
                        this.mCell.put("E" + pos, "");
333
                    }
334
                } else {
335
                    this.mCell.put("E" + pos, baseTmp);
336
                }
337
 
338
                // Taux Sal
132 ilm 339
                BigDecimal tauxSalTmp = rowTmp.getBigDecimal("TAUX_SAL");
18 ilm 340
 
341
                if (tauxSalTmp != null) {
132 ilm 342
                    if (tauxSalTmp.signum() != 0) {
18 ilm 343
                        this.mCell.put("F" + pos, tauxSalTmp);
344
                    } else {
345
                        this.mCell.put("F" + pos, "");
346
                    }
347
                } else {
348
                    this.mCell.put("F" + pos, tauxSalTmp);
349
                }
350
 
351
                // Montant Sal Aj
132 ilm 352
                BigDecimal montantSalAjTmp = rowTmp.getBigDecimal("MONTANT_SAL_AJ");
18 ilm 353
                if (montantSalAjTmp != null) {
132 ilm 354
                    if (montantSalAjTmp.signum() != 0) {
18 ilm 355
                        this.mCell.put("G" + pos, montantSalAjTmp);
356
                    } else {
357
                        this.mCell.put("G" + pos, "");
358
                    }
359
                } else {
360
                    this.mCell.put("G" + pos, montantSalAjTmp);
361
                }
362
 
363
                // Montant Sal ded
132 ilm 364
                BigDecimal montantSalDedTmp = rowTmp.getBigDecimal("MONTANT_SAL_DED");
18 ilm 365
                if (montantSalDedTmp != null) {
132 ilm 366
                    if (montantSalDedTmp.signum() != 0) {
18 ilm 367
                        this.mCell.put("H" + pos, montantSalDedTmp);
368
                    } else {
369
                        this.mCell.put("H" + pos, "");
370
                    }
371
                } else {
372
                    this.mCell.put("H" + pos, montantSalDedTmp);
373
                }
374
 
375
                // Taux Pat
132 ilm 376
                BigDecimal tauxPatTmp = rowTmp.getBigDecimal("TAUX_PAT");
18 ilm 377
                if (tauxPatTmp != null) {
132 ilm 378
                    if (tauxPatTmp.signum() != 0) {
18 ilm 379
                        this.mCell.put("I" + pos, tauxPatTmp);
380
                    } else {
381
                        this.mCell.put("I" + pos, "");
382
                    }
383
                } else {
384
                    this.mCell.put("I" + pos, tauxPatTmp);
385
                }
386
 
387
                // Montant Pat
132 ilm 388
                BigDecimal montantPatTmp = rowTmp.getBigDecimal("MONTANT_PAT");
18 ilm 389
                if (montantPatTmp != null) {
132 ilm 390
 
391
                    if (montantPatTmp.signum() != 0) {
18 ilm 392
                        this.mCell.put("J" + pos, montantPatTmp);
393
                    } else {
394
                        this.mCell.put("J" + pos, "");
395
                    }
396
                } else {
397
                    this.mCell.put("J" + pos, montantPatTmp);
398
                }
399
 
400
                if (rowTmp.getString("SOURCE").equalsIgnoreCase("RUBRIQUE_COMM")) {
401
                    this.mapStyleRow.put(new Integer(pos), "Titre 1");
402
                } else {
403
                    this.mapStyleRow.put(new Integer(pos), "Normal");
404
                }
405
 
406
                pos++;
407
            }
408
        }
409
 
410
        // Totaux
132 ilm 411
        BigDecimal netApayerCumul = this.row.getBigDecimal("NET_A_PAYER");
412
        BigDecimal salBrutCumul = this.row.getBigDecimal("SAL_BRUT");
413
        BigDecimal cotSalCumul = this.row.getBigDecimal("COT_SAL");
414
        BigDecimal cotPatCumul = this.row.getBigDecimal("COT_PAT");
415
        BigDecimal netImpCumul = this.row.getBigDecimal("NET_IMP");
18 ilm 416
        this.mCell.put("I61", this.row.getObject("NET_A_PAYER"));
417
        this.mCell.put("D61", this.row.getObject("SAL_BRUT"));
418
        this.mCell.put("E61", this.row.getObject("COT_SAL"));
419
        this.mCell.put("F61", this.row.getObject("COT_PAT"));
420
        this.mCell.put("H61", this.row.getObject("NET_IMP"));
421
 
422
        SQLRow rowCumulsPaye;
423
 
424
        if (this.row.getInt("ID_CUMULS_PAYE") == 1) {
425
            rowCumulsPaye = tableCumulsPaye.getRow(rowSal.getInt("ID_CUMULS_PAYE"));
426
        } else {
427
            rowCumulsPaye = tableCumulsPaye.getRow(this.row.getInt("ID_CUMULS_PAYE"));
428
        }
429
 
132 ilm 430
        netApayerCumul = netApayerCumul.add(rowCumulsPaye.getBigDecimal("NET_A_PAYER_C"));
431
        cotSalCumul = cotSalCumul.add(rowCumulsPaye.getBigDecimal("COT_SAL_C"));
432
        cotPatCumul = cotPatCumul.add(rowCumulsPaye.getBigDecimal("COT_PAT_C"));
433
        netImpCumul = netImpCumul.add(rowCumulsPaye.getBigDecimal("NET_IMP_C"));
18 ilm 434
 
132 ilm 435
        this.mCell.put("D62", salBrutCumul);
436
        this.mCell.put("E62", cotSalCumul);
437
        this.mCell.put("F62", cotPatCumul);
438
        this.mCell.put("H62", netImpCumul);
18 ilm 439
    }
440
 
441
}