OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | 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.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.ui.SQLJavaEditor;
18
import org.openconcerto.erp.core.humanresources.payroll.element.PeriodeValiditeSQLElement;
19
import org.openconcerto.erp.core.humanresources.payroll.element.VariablePayeSQLElement;
73 ilm 20
import org.openconcerto.erp.preferences.PayeGlobalPreferencePanel;
18 ilm 21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.model.SQLBase;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
73 ilm 28
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 29
 
132 ilm 30
import java.math.BigDecimal;
31
import java.math.RoundingMode;
18 ilm 32
import java.sql.SQLException;
33
import java.util.Date;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Vector;
38
 
132 ilm 39
import javax.swing.JOptionPane;
40
import javax.swing.SwingUtilities;
18 ilm 41
import javax.swing.table.AbstractTableModel;
42
 
43
import org.apache.commons.dbutils.handlers.ArrayListHandler;
44
 
45
// TODO gestion de la place des rubriques dans l'ordre brut - cot - net et comm everywhere
46
// FIXME Thread pour le calcul
47
public class FichePayeModel extends AbstractTableModel {
48
 
49
    // Rubrique
50
    private Vector<SQLRowValues> vectRubrique;
51
    private Vector<SQLRowValues> vectRowValsToDelete;
52
 
53
    // table des rubriques
54
    private final Map<String, SQLTable> mapTableSource = new HashMap<String, SQLTable>();
55
 
56
    // Id de la fiche de paye concernee
57
    private int idFiche;
58
 
59
    private String[] title;
60
 
61
    private final static SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
62
    private final static SQLTable tableProfilElt = Configuration.getInstance().getBase().getTable("PROFIL_PAYE_ELEMENT");
63
    private final static SQLTable tableFichePayeElt = base.getTable("FICHE_PAYE_ELEMENT");
64
    private final static SQLTable tableFichePaye = base.getTable("FICHE_PAYE");
65
    private final static SQLTable tableValidite = Configuration.getInstance().getBase().getTable("PERIODE_VALIDITE");
66
 
67
    private SQLJavaEditor javaEdit = new SQLJavaEditor(VariablePayeSQLElement.getMapTree());
68
 
69
    // liste des variable de paye à calculer
132 ilm 70
    private BigDecimal salBrut, cotPat, cotSal, netImp, netAPayer, csg, csgSansAbattement;
71
 
18 ilm 72
    private Map<Integer, String> mapField;
73
 
132 ilm 74
    private final BigDecimal tauxCSG;
73 ilm 75
 
18 ilm 76
    public FichePayeModel(int idFiche) {
77
 
78
        System.err.println("NEW FICHE PAYE MODEL");
79
 
80
        this.idFiche = idFiche;
81
        this.vectRubrique = new Vector<SQLRowValues>();
82
        this.vectRowValsToDelete = new Vector<SQLRowValues>();
83
 
84
        // Titres des colonnes
85
        this.title = new String[9];
86
        this.title[0] = "Libellé";
87
        this.title[1] = "Base";
88
        this.title[2] = "Taux sal.";
89
        this.title[3] = "Montant sal. à ajouter";
90
        this.title[4] = "Montant sal. à déduire";
91
        this.title[5] = "Taux pat.";
92
        this.title[6] = "Montant pat.";
93
        this.title[7] = "Impression";
94
        this.title[8] = "Dans la Période";
95
 
96
        SQLTable tableNet = Configuration.getInstance().getBase().getTable("RUBRIQUE_NET");
97
        SQLTable tableBrut = Configuration.getInstance().getBase().getTable("RUBRIQUE_BRUT");
98
        SQLTable tableCotis = Configuration.getInstance().getBase().getTable("RUBRIQUE_COTISATION");
99
        SQLTable tableComm = Configuration.getInstance().getBase().getTable("RUBRIQUE_COMM");
100
        this.mapTableSource.put(tableNet.getName(), tableNet);
101
        this.mapTableSource.put(tableBrut.getName(), tableBrut);
102
        this.mapTableSource.put(tableCotis.getName(), tableCotis);
103
        this.mapTableSource.put(tableComm.getName(), tableComm);
104
 
105
        this.mapField = new HashMap<Integer, String>();
106
        this.mapField.put(new Integer(0), "NOM");
107
        this.mapField.put(new Integer(1), "NB_BASE");
108
        this.mapField.put(new Integer(2), "TAUX_SAL");
109
        this.mapField.put(new Integer(3), "MONTANT_SAL_AJ");
110
        this.mapField.put(new Integer(4), "MONTANT_SAL_DED");
111
        this.mapField.put(new Integer(5), "TAUX_PAT");
112
        this.mapField.put(new Integer(6), "MONTANT_PAT");
113
        this.mapField.put(new Integer(7), "IMPRESSION");
114
        this.mapField.put(new Integer(8), "IN_PERIODE");
115
 
73 ilm 116
        SQLPreferences prefs = new SQLPreferences(tableFichePaye.getTable().getDBRoot());
132 ilm 117
        this.tauxCSG = new BigDecimal(prefs.getDouble(PayeGlobalPreferencePanel.ASSIETTE_CSG, 0.9825D));
73 ilm 118
 
18 ilm 119
        // loadElement();
120
        // methodeTmp();
121
    }
122
 
123
    private void resetValueFiche() {
124
 
125
        /*
126
         * if (this.threadUpdate != null && this.threadUpdate.isAlive()) { this.threadUpdate.stop();
127
         * }
128
         */
132 ilm 129
        this.salBrut = BigDecimal.ZERO;
130
        this.cotPat = BigDecimal.ZERO;
131
        this.cotSal = BigDecimal.ZERO;
132
        this.netAPayer = BigDecimal.ZERO;
133
        this.netImp = BigDecimal.ZERO;
134
        this.csg = BigDecimal.ZERO;
135
        this.csgSansAbattement = BigDecimal.ZERO;
18 ilm 136
    }
137
 
138
    public void loadAllElements() {
139
 
140
        System.err.println("Start At " + new Date());
141
        if (this.idFiche <= 1) {
142
            System.err.println("Aucune fiche associée");
143
            return;
144
        }
145
 
146
        // RAZ
147
        resetValueFiche();
148
 
149
        /*
150
         * this.threadUpdate = new Thread("Update Fiche Paye") { public void run() {
151
         */
152
        this.vectRubrique = new Vector<SQLRowValues>();
153
 
154
        SQLRow rowFiche = tableFichePaye.getRow(this.idFiche);
155
 
156
        this.javaEdit.setSalarieID(rowFiche.getInt("ID_SALARIE"));
157
 
158
        // éléments de la fiche de paye
73 ilm 159
        SQLSelect selAllIDFicheElt = new SQLSelect();
18 ilm 160
 
161
        selAllIDFicheElt.addSelect(tableFichePayeElt.getField("ID"));
162
        selAllIDFicheElt.addSelect(tableFichePayeElt.getField("POSITION"));
163
        selAllIDFicheElt.setWhere(new Where(tableFichePayeElt.getField("ID_FICHE_PAYE"), "=", this.idFiche));
164
 
165
        selAllIDFicheElt.setDistinct(true);
166
 
167
        selAllIDFicheElt.addRawOrder("\"FICHE_PAYE_ELEMENT\".\"POSITION\"");
168
        String reqAllIDFichelElt = selAllIDFicheElt.asString();
169
 
170
        System.err.println("Request " + reqAllIDFichelElt);
171
 
172
        Object[] objIDFicheElt = ((List) base.getDataSource().execute(reqAllIDFichelElt, new ArrayListHandler())).toArray();
173
 
174
        System.err.println(objIDFicheElt.length + " elements to load");
175
 
176
        for (int i = 0; i < objIDFicheElt.length; i++) {
177
            SQLRow row = tableFichePayeElt.getRow(Integer.parseInt(((Object[]) objIDFicheElt[i])[0].toString()));
178
 
179
            String source = row.getString("SOURCE");
180
            int idSource = row.getInt("IDSOURCE");
181
 
182
            if (source.trim().length() != 0) {
183
 
184
                // System.err.println("Source != null");
185
 
186
                if (this.mapTableSource.get(source) != null) {
187
                    SQLRow rowSource = this.mapTableSource.get(source).getRow(idSource);
188
 
189
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_BRUT")) {
190
                        loadElementBrut(rowSource, row);
191
                    }
192
 
193
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COTISATION")) {
194
                        loadElementCotisation(rowSource, row);
195
                    }
196
 
197
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_NET")) {
198
                        loadElementNet(rowSource, row);
199
                    }
200
 
201
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
202
                        loadElementComm(rowSource, row);
203
                    }
204
 
205
                } else {
206
                    System.err.println("Table " + source + " non référencée");
207
                }
208
            }
209
        }
210
        System.err.println(this.vectRubrique.size() + " elements ADDed ");
211
 
212
        fireTableDataChanged();
213
        /*
214
         * } }; this.threadUpdate.start();
215
         */
216
 
217
        System.err.println("End At " + new Date());
218
    }
219
 
220
    public String getColumnName(int column) {
221
 
222
        return this.title[column];
223
    }
224
 
225
    public int getRowCount() {
226
 
227
        return this.vectRubrique.size();
228
    }
229
 
230
    public int getColumnCount() {
231
 
232
        return this.title.length;
233
    }
234
 
235
    public Object getValueAt(int rowIndex, int columnIndex) {
236
 
237
        SQLRowValues row = this.vectRubrique.get(rowIndex);
238
        Object o = null;
239
 
240
        if (row != null) {
241
            o = row.getObject(this.mapField.get(new Integer(columnIndex)).toString());
242
        }
243
 
244
        return o;
245
    }
246
 
247
    public Class<?> getColumnClass(int columnIndex) {
248
        Class<?> cl = tableFichePayeElt.getField(this.mapField.get(new Integer(columnIndex))).getType().getJavaType();
249
        return cl;
250
    }
251
 
252
    /*
253
     * public boolean isCellEditable(int rowIndex, int columnIndex) {
254
     *
255
     * if (columnIndex == 0) { return true; }
256
     *
257
     * SQLRowValues rowVals = (SQLRowValues) this.vectRubrique.get(rowIndex);
258
     *
259
     * Object ob = rowVals.getObject("SOURCE"); String source = (ob == null) ? "" : ob.toString();
260
     *
261
     * if ((source.trim().length() != 0) && (!source.equalsIgnoreCase("RUBRIQUE_COTISATION"))) {
262
     *
263
     * if (!source.equalsIgnoreCase("RUBRIQUE_COMM")) { if (columnIndex > 5) { return false; } else
264
     * {
265
     *
266
     * if (columnIndex == 1 || columnIndex == 2) { return true; }
267
     *
268
     * if (source.equalsIgnoreCase("RUBRIQUE_COT") && (columnIndex == 5)) {
269
     *
270
     * return true; } } } } return false; }
271
     *
272
     * public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
273
     *
274
     * System.err.println("_______***$$$$ " + this.mapField.get(new
275
     * Integer(columnIndex)).toString()); SQLRowValues rowVals = (SQLRowValues)
276
     * this.vectRubrique.get(rowIndex); rowVals.put(this.mapField.get(new
277
     * Integer(columnIndex)).toString(), aValue); rowVals.put("VALIDE", Boolean.TRUE);
278
     *
279
     * try { rowVals.update(); } catch (SQLException e) { e.printStackTrace(); }
280
     *
281
     * calculValue(); }
282
     */
283
    public boolean containValueAt(int rowIndex, int columnIndex) {
284
 
285
        if (columnIndex == 0) {
286
            return true;
287
        }
288
 
289
        SQLRowValues rowVals = this.vectRubrique.get(rowIndex);
290
 
291
        Object ob = rowVals.getObject("SOURCE");
292
        String source = (ob == null) ? "" : ob.toString();
293
 
294
        Object obId = rowVals.getObject("IDSOURCE");
295
        int idSource = (obId == null) ? 1 : rowVals.getInt("IDSOURCE");
296
 
297
        if ((source.trim().length() != 0) && (!source.equalsIgnoreCase("RUBRIQUE_COTISATION"))) {
298
            /*
299
             * if (source.equalsIgnoreCase("RUBRIQUE_COMM")) { return true; } else {
300
             */
301
 
302
            if (columnIndex > 4) {
303
                return false;
304
            } else {
305
                SQLRow row = this.mapTableSource.get(source).getRow(idSource);
306
                if (source.equalsIgnoreCase("RUBRIQUE_BRUT")) {
307
                    if ((row.getInt("ID_TYPE_RUBRIQUE_BRUT") == 2) && (columnIndex == 4)) {
308
                        return false;
309
                    }
310
                    if ((row.getInt("ID_TYPE_RUBRIQUE_BRUT") == 3) && (columnIndex == 3)) {
311
                        return false;
312
                    }
313
                } else {
314
                    if (source.equalsIgnoreCase("RUBRIQUE_NET")) {
315
                        if ((row.getInt("ID_TYPE_RUBRIQUE_NET") == 2) && (columnIndex == 4)) {
316
                            return false;
317
                        }
318
                        if ((row.getInt("ID_TYPE_RUBRIQUE_NET") == 3) && (columnIndex == 3)) {
319
                            return false;
320
                        }
321
                    } else {
322
                        return false;
323
                    }
324
                }
325
                // }
326
            }
327
        } else {
328
            if (columnIndex == 3) {
329
                return false;
330
            }
331
        }
332
 
333
        return true;
334
    }
335
 
336
    public void loadFromProfil(final int idProfil) {
337
 
338
        System.err.println("Load from profil");
339
 
340
        resetValueFiche();
341
 
342
        /*
343
         * this.threadUpdate = new Thread("Update Fiche Paye") { public void run() {
344
         */
345
        // On supprime les anciennes lignes de la fiche
346
        while (this.vectRubrique.size() > 0) {
347
 
348
            this.vectRowValsToDelete.add(this.vectRubrique.remove(0));
349
        }
350
 
351
        // this.vectRubrique = new Vector();
352
 
353
        // Listes des rubriques du profil
73 ilm 354
        SQLSelect selAllIDProfilElt = new SQLSelect();
18 ilm 355
 
356
        selAllIDProfilElt.addSelect(tableProfilElt.getField("ID"));
357
        selAllIDProfilElt.addSelect(tableProfilElt.getField("POSITION"));
358
        selAllIDProfilElt.setWhere(new Where(tableProfilElt.getField("ID_PROFIL_PAYE"), "=", idProfil));
359
        selAllIDProfilElt.addRawOrder("\"PROFIL_PAYE_ELEMENT\".\"POSITION\"");
360
 
361
        String reqAllIDProfilElt = selAllIDProfilElt.asString();
362
 
363
        Object[] objIDProfilElt = ((List) Configuration.getInstance().getBase().getDataSource().execute(reqAllIDProfilElt, new ArrayListHandler())).toArray();
364
 
365
        for (int i = 0; i < objIDProfilElt.length; i++) {
366
            SQLRow rowTmp = tableProfilElt.getRow(Integer.parseInt(((Object[]) objIDProfilElt[i])[0].toString()));
367
 
368
            String source = rowTmp.getString("SOURCE");
369
            int idSource = rowTmp.getInt("IDSOURCE");
370
 
371
            if (this.mapTableSource.get(source) != null) {
372
                SQLRow row = this.mapTableSource.get(source).getRow(idSource);
373
 
374
                if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_BRUT")) {
375
                    loadElementBrut(row, null);
376
                }
377
                if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_COTISATION")) {
378
                    loadElementCotisation(row, null);
379
                }
380
                if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_NET")) {
381
                    loadElementNet(row, null);
382
                }
383
                if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
384
                    loadElementComm(row, null);
385
                }
386
 
387
            } else {
388
                System.err.println("FichePayeModel.java --> Table non référencée dans la Map. Table name : " + source);
389
            }
390
        }
391
 
392
        // this.vectRubrique = new Vector();
393
 
394
        // updateFields(this.idFiche);
395
        fireTableDataChanged();
396
 
397
        /*
398
         * } }; this.threadUpdate.start();
399
         */
400
    }
401
 
402
    public String getSourceAt(int rowIndex) {
403
        return this.vectRubrique.get(rowIndex).getString("SOURCE");
404
    }
405
 
406
    public int upRow(int rowIndex) {
407
        // On vérifie qu'il est possible de remonter la ligne
408
        if ((this.vectRubrique.size() > 1) && (rowIndex > 0)) {
409
            System.err.println("UP");
410
            SQLRowValues tmp = this.vectRubrique.get(rowIndex);
411
            this.vectRubrique.set(rowIndex, this.vectRubrique.get(rowIndex - 1));
412
            this.vectRubrique.set(rowIndex - 1, tmp);
413
            this.fireTableDataChanged();
414
            return rowIndex - 1;
415
        }
416
        System.err.println("can't up!!");
417
        return rowIndex;
418
    }
419
 
420
    public int downRow(int rowIndex) {
421
        // On vérifie qu'il est possible de descendre la ligne
422
        if ((rowIndex >= 0) && (this.vectRubrique.size() > 1) && (rowIndex + 1 < this.vectRubrique.size())) {
423
 
424
            System.err.println("DOWN");
425
            SQLRowValues tmp = this.vectRubrique.get(rowIndex);
426
            this.vectRubrique.set(rowIndex, this.vectRubrique.get(rowIndex + 1));
427
            this.vectRubrique.set(rowIndex + 1, tmp);
428
            this.fireTableDataChanged();
429
            return rowIndex + 1;
430
        }
431
 
432
        System.err.println("can't down!!!");
433
        return rowIndex;
434
    }
435
 
436
    public void setLastRowAT(int rowIndex) {
437
 
438
        // On vérifie qu'il est possible de descendre la ligne
439
        if ((rowIndex > 0) && (rowIndex < this.vectRubrique.size())) {
440
            this.vectRubrique.add(rowIndex, this.vectRubrique.remove(this.vectRubrique.size() - 1));
441
        }
442
        this.fireTableDataChanged();
443
    }
444
 
445
    public void setFicheID(int id) {
446
        this.idFiche = id;
447
 
448
        // this.javaEdit.setSalarieID(this.tableFichePaye.getRow(this.idFiche).getInt("ID_SALARIE"));
449
        this.loadAllElements();
450
    }
451
 
452
    /***********************************************************************************************
453
     * Ajouter une ligne
454
     *
455
     * @param row SQLRow RUBRIQUE_BRUT, RUBRIQUE_COTISATION, RUBRIQUE_NET, RUBRIQUE_COMM
456
     * @param index index ou doit etre insere la row
457
     */
458
    public void addRowAt(SQLRow row, int index) {
459
 
460
        int size = this.vectRubrique.size();
461
        if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_BRUT")) {
462
            this.loadElementBrut(row, null);
463
        } else {
464
            if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_COTISATION")) {
465
                this.loadElementCotisation(row, null);
466
            } else {
467
                if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_NET")) {
468
                    this.loadElementNet(row, null);
469
                } else {
470
                    if (row.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
471
                        this.loadElementComm(row, null);
472
                    }
473
                }
474
            }
475
        }
476
 
477
        if (size != this.vectRubrique.size()) {
478
            setLastRowAT(index);
479
        }
480
        if (!row.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
481
            calculValue();
482
        }
483
        this.fireTableDataChanged();
484
    }
485
 
486
    public void removeRow(int rowIndex) {
487
        // System.err.println("_________________________________REMOVE");
488
        if (rowIndex >= 0) {
489
 
490
            SQLRowValues rowVals = this.vectRubrique.remove(rowIndex);
491
            this.vectRowValsToDelete.add(rowVals);
492
 
493
            if (!rowVals.getString("SOURCE").equalsIgnoreCase("RUBRIQUE_COMM")) {
494
                calculValue();
495
            }
496
            this.fireTableDataChanged();
497
        }
498
    }
499
 
500
    public void updateFields(int idFiche) {
501
 
502
        // System.err.println("UPDATE FIELDS");
503
 
504
        for (int i = 0; i < this.vectRowValsToDelete.size(); i++) {
505
            SQLRowValues rowVals = this.vectRowValsToDelete.get(i);
506
 
507
            if (rowVals.getID() != SQLRow.NONEXISTANT_ID) {
508
 
509
                rowVals.put("ARCHIVE", 1);
510
                try {
511
                    rowVals.update();
512
                } catch (SQLException e) {
513
 
514
                    e.printStackTrace();
515
                }
516
            }
517
        }
518
 
519
        this.vectRowValsToDelete = new Vector<SQLRowValues>();
520
 
521
        for (int i = 0; i < this.vectRubrique.size(); i++) {
522
            SQLRowValues rowVals = this.vectRubrique.get(i);
523
            rowVals.put("ID_FICHE_PAYE", idFiche);
524
            rowVals.put("POSITION", i);
525
 
526
            try {
527
                rowVals.commit();
528
            } catch (SQLException e) {
529
 
530
                e.printStackTrace();
531
            }
532
        }
533
    }
534
 
535
    public void showData() {
536
 
537
        if (this.vectRubrique.size() == 0) {
538
            System.err.println("Vecteur contains no value.");
539
        }
540
        for (int i = 0; i < this.vectRubrique.size(); i++) {
541
            System.err.println(this.vectRubrique.get(i));
542
        }
543
    }
544
 
545
    public Object getVectorObjectAt(int index) {
546
        return this.vectRubrique.get(index);
547
    }
548
 
549
    private boolean isEltInPeriod(SQLRow rowSource) {
550
        SQLRow rowFiche = tableFichePaye.getRow(this.idFiche);
551
        int mois = rowFiche.getInt("ID_MOIS") - 1;
552
 
553
        Object ob = PeriodeValiditeSQLElement.mapTranslate().get(Integer.valueOf(mois));
554
 
555
        if (ob == null) {
556
            return false;
557
        }
558
        String moisName = ob.toString();
559
 
560
        SQLRow rowPeriodeValid = tableValidite.getRow(rowSource.getInt("ID_PERIODE_VALIDITE"));
561
 
562
        return (rowPeriodeValid.getBoolean(moisName));
563
    }
564
 
565
    private boolean isEltImprimable(SQLRow rowSource, SQLRowValues row) {
566
 
567
        int impression = rowSource.getInt("ID_IMPRESSION_RUBRIQUE");
568
 
569
        if (impression == 3) {
570
            return true;
571
        } else {
572
            if (impression == 4) {
573
                return false;
574
            } else {
575
                if (impression == 2) {
576
 
132 ilm 577
                    BigDecimal montantSalAjOb = row.getBigDecimal("MONTANT_SAL_AJ");
578
                    BigDecimal montantSalAj = (montantSalAjOb == null) ? BigDecimal.ZERO : montantSalAjOb;
18 ilm 579
 
132 ilm 580
                    BigDecimal montantSalDedOb = row.getBigDecimal("MONTANT_SAL_DED");
581
                    BigDecimal montantSalDed = (montantSalDedOb == null) ? BigDecimal.ZERO : montantSalDedOb;
18 ilm 582
 
132 ilm 583
                    BigDecimal montantPatOb = row.getBigDecimal("MONTANT_PAT");
584
                    BigDecimal montantPat = (montantPatOb == null) ? BigDecimal.ZERO : montantPatOb;
18 ilm 585
 
132 ilm 586
                    if (montantSalAj.signum() == 0 && montantSalDed.signum() == 0 && montantPat.signum() == 0) {
18 ilm 587
                        return false;
588
                    }
589
                    return true;
590
 
591
                }
592
            }
593
        }
594
        return true;
595
    }
596
 
597
    /**
598
     * charge un élément de la fiche dans rowVals, dont la rubrique source est rowSource et
599
     * l'élément row si l'élément existe déja
600
     *
601
     * @param rowVals
602
     * @param rowSource
603
     * @param row
604
     * @return true si on doit calculer les valeurs
605
     */
606
    private boolean loadElement(SQLRowValues rowVals, SQLRow rowSource, SQLRow row) {
607
 
608
        if (row != null) {
609
            rowVals.loadAbsolutelyAll(row);
610
        }
611
 
612
        // on vérifie que la rubrique s'applique pour le mois concerné
613
        // if (!isEltInPeriod(rowSource)) {
614
        // System.err.println("Not In periode");
615
        rowVals.put("IN_PERIODE", Boolean.valueOf(isEltInPeriod(rowSource)));
616
        // }
617
 
618
        rowVals.put("SOURCE", rowSource.getTable().getName());
619
        rowVals.put("IDSOURCE", rowSource.getID());
620
 
621
        Object ob = rowVals.getObject("VALIDE");
622
        boolean b = (ob == null) ? false : new Boolean(ob.toString()).booleanValue();
623
 
624
        return b;
625
    }
626
 
627
    private void updateValueFiche() {
628
 
629
        SQLRowValues rowValsFiche = new SQLRowValues(tableFichePaye);
132 ilm 630
        rowValsFiche.put("SAL_BRUT", this.salBrut);
631
        rowValsFiche.put("NET_IMP", this.netImp.add(this.salBrut));
632
        rowValsFiche.put("NET_A_PAYER", this.netAPayer.add(this.salBrut));
633
        rowValsFiche.put("COT_SAL", this.cotSal);
634
        rowValsFiche.put("COT_PAT", this.cotPat);
635
        rowValsFiche.put("CSG", this.salBrut.add(this.csg).multiply(this.tauxCSG).add(this.csgSansAbattement));
18 ilm 636
 
637
        try {
638
            rowValsFiche.update(this.idFiche);
639
        } catch (SQLException e) {
640
            e.printStackTrace();
641
        }
642
    }
643
 
644
    /***********************************************************************************************
645
     * Charge un élément d'une rubrique de brut
646
     *
647
     * @param rowSource row de la rubrique source
648
     * @param row row de l'élément de la fiche
649
     **********************************************************************************************/
650
    private void loadElementBrut(SQLRow rowSource, SQLRow row) {
651
 
652
        // System.err.println("________________________LOAD ELT BRUT");
653
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
654
 
655
        if (!loadElement(rowVals, rowSource, row)) {
656
 
657
            // System.err.println("________________________Recalcul des ELT BRUT ");
658
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
659
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX"), "TAUX");
660
            rowVals.put("NOM", rowSource.getString("NOM"));
661
 
132 ilm 662
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
663
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
18 ilm 664
        }
665
 
666
        calculBrut(rowSource, rowVals);
667
 
668
        boolean b = isEltImprimable(rowSource, rowVals);
669
        // System.err.println("Impression --- > " + b);
670
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
671
 
672
        this.vectRubrique.add(rowVals);
673
    }
674
 
675
    /**
676
     * Calcul le montant d'une rubrique de brut et met à jour les variables du salarié
677
     *
678
     * @param rowSource
679
     * @param rowVals
680
     */
681
    private void calculBrut(SQLRow rowSource, SQLRowValues rowVals) {
682
 
132 ilm 683
        if (rowVals.getBoolean("IN_PERIODE")) {
18 ilm 684
 
132 ilm 685
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
686
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
18 ilm 687
 
132 ilm 688
            BigDecimal base = (baseOb == null) ? BigDecimal.ZERO : baseOb;
689
            BigDecimal tauxSal = (tauxSalOb == null) ? BigDecimal.ZERO : tauxSalOb;
18 ilm 690
 
691
            // Calcul du montant
692
            String formuleMontant = rowSource.getString("MONTANT");
693
 
132 ilm 694
            BigDecimal montant = BigDecimal.ZERO;
18 ilm 695
            if (formuleMontant.trim().length() == 0) {
132 ilm 696
                montant = base.multiply(tauxSal).setScale(2, RoundingMode.HALF_UP);
18 ilm 697
            } else {
698
                Object montantNet = this.javaEdit.checkFormule(rowSource.getString("MONTANT"), "MONTANT");
132 ilm 699
                BigDecimal montantNetS = (montantNet == null) ? BigDecimal.ZERO : new BigDecimal(montantNet.toString());
700
                montant = montantNetS;
18 ilm 701
            }
702
 
703
            // Retenue
704
            if (rowSource.getInt("ID_TYPE_RUBRIQUE_BRUT") == 3) {
705
 
132 ilm 706
                rowVals.put("MONTANT_SAL_DED", montant);
707
                this.salBrut = this.salBrut.subtract(montant);
708
            } // Gain
18 ilm 709
            else {
710
 
132 ilm 711
                rowVals.put("MONTANT_SAL_AJ", montant);
712
                this.salBrut = this.salBrut.add(montant);
18 ilm 713
            }
714
 
715
            // Mis a jour du salaire brut
716
            updateValueFiche();
717
        }
718
    }
719
 
720
    private void calculNet(SQLRow rowSource, SQLRowValues rowVals) {
721
 
132 ilm 722
        if (rowVals.getBoolean("IN_PERIODE")) {
18 ilm 723
 
132 ilm 724
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
725
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
18 ilm 726
 
132 ilm 727
            BigDecimal base = baseOb == null ? BigDecimal.ZERO : baseOb;
728
            BigDecimal tauxSal = tauxSalOb == null ? BigDecimal.ZERO : tauxSalOb;
18 ilm 729
 
730
            // Calcul du montant
731
            String formuleMontant = rowSource.getString("MONTANT");
732
 
132 ilm 733
            BigDecimal montant = BigDecimal.ZERO;
18 ilm 734
            if (formuleMontant.trim().length() == 0) {
132 ilm 735
                montant = base.multiply(tauxSal).setScale(2, RoundingMode.HALF_UP);
18 ilm 736
            } else {
737
                Object montantNet = this.javaEdit.checkFormule(rowSource.getString("MONTANT"), "MONTANT");
738
                if (montantNet != null) {
132 ilm 739
                    montant = new BigDecimal(montantNet.toString());
18 ilm 740
                }
741
            }
742
 
743
            // Retenue
744
            if (rowSource.getInt("ID_TYPE_RUBRIQUE_NET") == 3) {
745
 
132 ilm 746
                rowVals.put("MONTANT_SAL_DED", montant);
18 ilm 747
 
132 ilm 748
                this.netAPayer = this.netAPayer.subtract(montant);
749
                if (!rowSource.getBoolean("IMPOSABLE")) {
750
                    this.netImp = this.netImp.subtract(montant);
18 ilm 751
                }
752
 
132 ilm 753
            } // Gain
18 ilm 754
            else {
755
 
132 ilm 756
                rowVals.put("MONTANT_SAL_AJ", montant);
18 ilm 757
 
132 ilm 758
                this.netAPayer = this.netAPayer.add(montant);
759
                if (!rowSource.getBoolean("IMPOSABLE")) {
760
                    this.netImp = this.netImp.add(montant);
18 ilm 761
                }
762
            }
763
 
764
            // Mis a jour du salaire net
765
            updateValueFiche();
766
        }
767
    }
768
 
769
    private void loadElementNet(SQLRow rowSource, SQLRow row) {
770
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
771
        // System.err.println("________________________LOAD ELT NET");
772
 
773
        if (!loadElement(rowVals, rowSource, row)) {
774
 
775
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
776
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX"), "TAUX");
777
            rowVals.put("NOM", rowSource.getString("NOM"));
132 ilm 778
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
779
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
18 ilm 780
        }
781
 
782
        calculNet(rowSource, rowVals);
783
 
784
        boolean b = isEltImprimable(rowSource, rowVals);
785
        // System.err.println("Impression --- > " + b);
786
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
787
 
788
        this.vectRubrique.add(rowVals);
789
    }
790
 
791
    private void calculCotisation(SQLRow rowSource, SQLRowValues rowVals) {
792
 
793
        if (((Boolean) rowVals.getObject("IN_PERIODE")).booleanValue()) {
794
 
132 ilm 795
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
796
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
797
            BigDecimal tauxPatOb = rowVals.getBigDecimal("TAUX_PAT");
18 ilm 798
 
132 ilm 799
            BigDecimal base = baseOb == null ? BigDecimal.ZERO : baseOb;
800
            BigDecimal tauxSal = tauxSalOb == null ? BigDecimal.ZERO : tauxSalOb;
801
            BigDecimal tauxPat = tauxPatOb == null ? BigDecimal.ZERO : tauxPatOb;
18 ilm 802
 
803
            // Calcul du montant
132 ilm 804
            BigDecimal montantSal = base.multiply(tauxSal).movePointLeft(2).setScale(2, RoundingMode.HALF_UP);
805
            BigDecimal montantPat = base.multiply(tauxPat).movePointLeft(2).setScale(2, RoundingMode.HALF_UP);
18 ilm 806
 
132 ilm 807
            rowVals.put("MONTANT_SAL_DED", montantSal);
808
            rowVals.put("MONTANT_PAT", montantPat);
18 ilm 809
 
132 ilm 810
            this.netAPayer = this.netAPayer.subtract(montantSal);
811
            if (!rowSource.getBoolean("IMPOSABLE")) {
812
                this.netImp = this.netImp.subtract(montantSal);
18 ilm 813
            }
814
 
94 ilm 815
            if (rowSource.getBoolean("PART_PAT_IMPOSABLE")) {
132 ilm 816
                this.netImp = this.netImp.add(montantPat);
94 ilm 817
            }
818
 
132 ilm 819
            if (rowSource.getBoolean("PART_CSG")) {
820
                this.csg = this.csg.add(montantPat);
821
            }
94 ilm 822
            if (rowSource.getBoolean("PART_CSG_SANS_ABATTEMENT")) {
132 ilm 823
                this.csgSansAbattement = this.csgSansAbattement.add(montantPat);
94 ilm 824
            }
825
 
132 ilm 826
            this.cotSal = this.cotSal.add(montantSal);
827
            this.cotPat = this.cotPat.add(montantPat);
18 ilm 828
 
829
            // Mis a jour des cotisations
830
            updateValueFiche();
831
        }
832
    }
833
 
132 ilm 834
    private void loadElementCotisation(final SQLRow rowSource, SQLRow row) {
18 ilm 835
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
836
        // System.err.println("________________________LOAD ELT COTISATION");
837
 
838
        if (!loadElement(rowVals, rowSource, row)) {
839
 
840
            // On calcule les valeurs
841
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
132 ilm 842
            if (!this.javaEdit.isCodeValid()) {
843
                SwingUtilities.invokeLater(new Runnable() {
844
 
845
                    @Override
846
                    public void run() {
847
                        JOptionPane.showMessageDialog(null, "La formule BASE pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
848
                    }
849
                });
850
            }
18 ilm 851
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TX_SAL"), "TX_SAL");
132 ilm 852
            if (!this.javaEdit.isCodeValid()) {
853
                SwingUtilities.invokeLater(new Runnable() {
854
 
855
                    @Override
856
                    public void run() {
857
                        JOptionPane.showMessageDialog(null, "La formule TX_SAL pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
858
                    }
859
                });
860
            }
18 ilm 861
            Object tauxPatOb = this.javaEdit.checkFormule(rowSource.getString("TX_PAT"), "TX_PAT");
132 ilm 862
            if (!this.javaEdit.isCodeValid()) {
863
                SwingUtilities.invokeLater(new Runnable() {
864
 
865
                    @Override
866
                    public void run() {
867
                        JOptionPane.showMessageDialog(null, "La formule TX_PAT pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
868
                    }
869
                });
870
            }
18 ilm 871
            rowVals.put("NOM", rowSource.getString("NOM"));
132 ilm 872
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
873
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
874
            rowVals.put("TAUX_PAT", (tauxPatOb == null) ? null : new BigDecimal(tauxPatOb.toString()));
18 ilm 875
        }
876
 
877
        calculCotisation(rowSource, rowVals);
878
 
879
        boolean b = isEltImprimable(rowSource, rowVals);
880
        // System.err.println("Impression --- > " + b);
881
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
882
 
883
        this.vectRubrique.add(rowVals);
884
    }
885
 
886
    public void loadElementComm(SQLRow rowSource, SQLRow row) {
887
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
888
        // System.err.println("________________________LOAD ELT COMM");
889
 
890
        if (loadElement(rowVals, rowSource, row)) {
891
            this.vectRubrique.add(rowVals);
892
            return;
893
        }
894
 
895
        Object baseOb = this.javaEdit.checkFormule(rowSource.getString("NB_BASE"), "BASE");
896
        Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX_SAL"), "SAL");
897
        Object tauxPatOb = this.javaEdit.checkFormule(rowSource.getString("TAUX_PAT"), "PAT");
898
        Object montantPatOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_PAT"), "MONTANT");
899
        Object montantAdOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_SAL_AJ"), "MONTANT");
900
        Object montantDedOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_SAL_DED"), "MONTANT");
901
        rowVals.put("NOM", rowSource.getBoolean("NOM_VISIBLE") ? rowSource.getString("NOM") : "");
132 ilm 902
        rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
903
        rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
904
        rowVals.put("TAUX_PAT", (tauxPatOb == null) ? null : new BigDecimal(tauxPatOb.toString()));
905
        rowVals.put("MONTANT_PAT", (montantPatOb == null) ? null : new BigDecimal(montantPatOb.toString()));
906
        rowVals.put("MONTANT_SAL_AJ", (montantAdOb == null) ? null : new BigDecimal(montantAdOb.toString()));
907
        rowVals.put("MONTANT_SAL_DED", (montantDedOb == null) ? null : new BigDecimal(montantDedOb.toString()));
18 ilm 908
 
909
        boolean b = isEltImprimable(rowSource, rowVals);
910
        // System.err.println("Impression --- > " + b);
911
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
912
 
913
        this.vectRubrique.add(rowVals);
914
    }
915
 
916
    private void calculValue() {
917
 
918
        System.err.println("Start calculValue At " + new Date());
919
 
920
        resetValueFiche();
921
 
922
        /*
923
         * this.threadUpdate = new Thread("Update Fiche Paye") { public void run() {
924
         */
925
        Vector<SQLRowValues> vectTmp = new Vector<SQLRowValues>(this.vectRubrique);
926
 
927
        this.vectRubrique = new Vector<SQLRowValues>();
928
        for (int i = 0; i < vectTmp.size(); i++) {
929
 
930
            SQLRowValues rowVals = vectTmp.get(i);
931
            String source = rowVals.getString("SOURCE");
932
            int idSource = rowVals.getInt("IDSOURCE");
933
            SQLRow row = tableFichePayeElt.getRow(rowVals.getID());
934
 
935
            if (source.trim().length() != 0) {
936
 
937
                // System.err.println("Source != null");
938
 
939
                if (this.mapTableSource.get(source) != null) {
940
                    SQLRow rowSource = this.mapTableSource.get(source).getRow(idSource);
941
 
942
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_BRUT")) {
943
                        loadElementBrut(rowSource, row);
944
                    }
945
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COTISATION")) {
946
                        loadElementCotisation(rowSource, row);
947
                    }
948
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_NET")) {
949
                        loadElementNet(rowSource, row);
950
                    }
951
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
952
                        loadElementComm(rowSource, row);
953
                    }
954
                } else {
955
                    System.err.println("Table " + source + " non référencée");
956
                }
957
            }
958
        }
959
        System.err.println(this.vectRubrique.size() + " elements ADDed ");
960
 
961
        fireTableDataChanged();
962
        /*
963
         * } }; this.threadUpdate.start();
964
         */
965
 
966
        System.err.println("End calculValue At " + new Date());
967
    }
968
 
969
    public void validElt() {
970
 
971
        System.err.println("Validation des éléments de la fiche.");
972
        for (int i = 0; i < this.vectRubrique.size(); i++) {
973
            SQLRowValues rowVals = this.vectRubrique.get(i);
974
            rowVals.put("VALIDE", Boolean.valueOf(true));
975
 
976
            try {
977
                rowVals.commit();
978
            } catch (SQLException e) {
979
                e.printStackTrace();
980
            }
981
        }
982
        System.err.println("Validation terminée.");
983
    }
984
}