OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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