OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Rev 151 | 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();
149 ilm 639
        final SQLTable tableValidite = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("PERIODE_VALIDITE");
640
        final int idPeriode = rowSource.getInt("ID_PERIODE_VALIDITE");
641
        SQLRow rowPeriodeValid = SQLBackgroundTableCache.getInstance().getCacheForTable(tableValidite).getRowFromId(idPeriode);
642
        if (rowPeriodeValid == null) {
643
            rowPeriodeValid = tableValidite.getRow(idPeriode);
644
            if (rowPeriodeValid == null) {
645
                System.err.println("FichePayeModel.isEltInPeriod() impossible de vérifier la validité de l'id " + idPeriode + " depuis la table PERIODE_VALIDITE");
646
                return true;
647
            }
648
        }
18 ilm 649
 
650
        return (rowPeriodeValid.getBoolean(moisName));
651
    }
652
 
653
    private boolean isEltImprimable(SQLRow rowSource, SQLRowValues row) {
654
 
655
        int impression = rowSource.getInt("ID_IMPRESSION_RUBRIQUE");
656
 
657
        if (impression == 3) {
658
            return true;
659
        } else {
660
            if (impression == 4) {
661
                return false;
662
            } else {
663
                if (impression == 2) {
664
 
132 ilm 665
                    BigDecimal montantSalAjOb = row.getBigDecimal("MONTANT_SAL_AJ");
666
                    BigDecimal montantSalAj = (montantSalAjOb == null) ? BigDecimal.ZERO : montantSalAjOb;
18 ilm 667
 
132 ilm 668
                    BigDecimal montantSalDedOb = row.getBigDecimal("MONTANT_SAL_DED");
669
                    BigDecimal montantSalDed = (montantSalDedOb == null) ? BigDecimal.ZERO : montantSalDedOb;
18 ilm 670
 
132 ilm 671
                    BigDecimal montantPatOb = row.getBigDecimal("MONTANT_PAT");
672
                    BigDecimal montantPat = (montantPatOb == null) ? BigDecimal.ZERO : montantPatOb;
18 ilm 673
 
132 ilm 674
                    if (montantSalAj.signum() == 0 && montantSalDed.signum() == 0 && montantPat.signum() == 0) {
18 ilm 675
                        return false;
676
                    }
677
                    return true;
678
 
679
                }
680
            }
681
        }
682
        return true;
683
    }
684
 
685
    /**
686
     * charge un élément de la fiche dans rowVals, dont la rubrique source est rowSource et
687
     * l'élément row si l'élément existe déja
688
     *
689
     * @param rowVals
690
     * @param rowSource
691
     * @param row
692
     * @return true si on doit calculer les valeurs
693
     */
694
    private boolean loadElement(SQLRowValues rowVals, SQLRow rowSource, SQLRow row) {
695
 
696
        if (row != null) {
697
            rowVals.loadAbsolutelyAll(row);
698
        }
699
 
700
        // on vérifie que la rubrique s'applique pour le mois concerné
701
        // if (!isEltInPeriod(rowSource)) {
702
        // System.err.println("Not In periode");
703
        rowVals.put("IN_PERIODE", Boolean.valueOf(isEltInPeriod(rowSource)));
704
        // }
705
 
706
        rowVals.put("SOURCE", rowSource.getTable().getName());
707
        rowVals.put("IDSOURCE", rowSource.getID());
708
 
709
        Object ob = rowVals.getObject("VALIDE");
710
        boolean b = (ob == null) ? false : new Boolean(ob.toString()).booleanValue();
711
 
144 ilm 712
        if (rowVals.getObject("ID_FICHE_PAYE") != null && !rowVals.isForeignEmpty("ID_FICHE_PAYE") && isValidated) {
142 ilm 713
            b = true;
714
        }
715
 
18 ilm 716
        return b;
717
    }
718
 
144 ilm 719
    private float smicHoraire = 0;
720
 
721
    private float getSmicHoraire(DBRoot root) {
722
        if (smicHoraire != 0) {
723
            return smicHoraire;
724
        }
725
        SQLTable table = root.findTable("VARIABLE_PAYE");
726
        SQLSelect sel = new SQLSelect();
727
        sel.addSelectStar(table);
728
        sel.setWhere(new Where(table.getField("NOM"), "=", "SMIC"));
729
        List<SQLRow> result = SQLRowListRSH.execute(sel);
730
        if (result.size() > 0) {
731
            smicHoraire = result.get(0).getFloat("VALEUR");
732
        } else {
733
            smicHoraire = 0;
734
            SwingUtilities.invokeLater(new Runnable() {
735
 
736
                @Override
737
                public void run() {
738
                    JOptionPane.showMessageDialog(null,
739
                            "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!");
740
                }
741
            });
742
        }
743
        return smicHoraire;
744
    }
745
 
18 ilm 746
    private void updateValueFiche() {
747
 
144 ilm 748
        if (!isValidated && this.idFiche > tableFichePaye.getUndefinedID()) {
142 ilm 749
            SQLRowValues rowValsFiche = new SQLRowValues(tableFichePaye);
144 ilm 750
            rowValsFiche.put(tableFichePaye.getKey().getName(), this.idFiche);
142 ilm 751
            rowValsFiche.put("SAL_BRUT", this.salBrut);
144 ilm 752
            rowValsFiche.put("SAL_BASE_BRUT", this.salBrutBase);
753
            rowValsFiche.put("SAL_BRUT_COTISABLE", this.salBrutCotis);
754
            rowValsFiche.put("SAL_BRUT_TAXABLE", this.salBrutTaxable);
755
            rowValsFiche.put("NET_IMP", getNetImpTotal());
756
            rowValsFiche.put("NET_A_PAYER", getNetAPayerTotal());
142 ilm 757
            rowValsFiche.put("COT_SAL", this.cotSal);
758
            rowValsFiche.put("COT_PAT", this.cotPat);
144 ilm 759
            rowValsFiche.put("TAXE_CM_PAT", this.taxCmPat);
760
            rowValsFiche.put("TAXE_CM_SAL", this.taxCmSal);
761
            rowValsFiche.put("CSG", getCsgTotal());
762
            rowValsFiche.put("HEURE_TRAV", getHeureTrav());
763
            if (this.cice == null) {
764
                if (salBrut.signum() > 0
765
                        && salBrut.compareTo(new BigDecimal(getSmicHoraire(tableFichePaye.getDBRoot())).multiply(getHeureTrav()).multiply(new BigDecimal(2.5), DecimalUtils.HIGH_PRECISION)) <= 0) {
766
                    this.cice = salBrut;
767
                } else {
768
                    this.cice = BigDecimal.ZERO;
769
                }
770
            }
771
            rowValsFiche.put("CICE", this.cice);
772
            rowValsFiche.put("ALLEGEMENT_COTISATION", this.allegmentCotisation);
773
            rowValsFiche.put("AVANTAGE_NATURE", this.avantage);
142 ilm 774
            try {
775
                rowValsFiche.update(this.idFiche);
144 ilm 776
                // rowValsFiche.getGraph().store(StoreMode.COMMIT, false);
142 ilm 777
            } catch (SQLException e) {
778
                e.printStackTrace();
779
            }
18 ilm 780
        }
144 ilm 781
 
18 ilm 782
    }
783
 
144 ilm 784
    private BigDecimal getHeureTrav() {
785
        final SQLRow rowFiche = tableFichePaye.getRow(idFiche);
786
        return new BigDecimal(rowFiche.getForeign("ID_SALARIE").getForeign("ID_VARIABLE_SALARIE").getFloat("HEURE_TRAV"));
787
 
788
    }
789
 
18 ilm 790
    /***********************************************************************************************
791
     * Charge un élément d'une rubrique de brut
792
     *
793
     * @param rowSource row de la rubrique source
794
     * @param row row de l'élément de la fiche
795
     **********************************************************************************************/
796
    private void loadElementBrut(SQLRow rowSource, SQLRow row) {
797
 
798
        // System.err.println("________________________LOAD ELT BRUT");
799
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
800
 
801
        if (!loadElement(rowVals, rowSource, row)) {
802
 
803
            // System.err.println("________________________Recalcul des ELT BRUT ");
804
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
805
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX"), "TAUX");
806
            rowVals.put("NOM", rowSource.getString("NOM"));
807
 
132 ilm 808
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
809
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
18 ilm 810
        }
811
 
812
        calculBrut(rowSource, rowVals);
813
 
814
        boolean b = isEltImprimable(rowSource, rowVals);
815
        // System.err.println("Impression --- > " + b);
816
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
817
 
818
        this.vectRubrique.add(rowVals);
819
    }
820
 
821
    /**
822
     * Calcul le montant d'une rubrique de brut et met à jour les variables du salarié
823
     *
824
     * @param rowSource
825
     * @param rowVals
826
     */
827
    private void calculBrut(SQLRow rowSource, SQLRowValues rowVals) {
828
 
132 ilm 829
        if (rowVals.getBoolean("IN_PERIODE")) {
18 ilm 830
 
132 ilm 831
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
832
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
18 ilm 833
 
132 ilm 834
            BigDecimal base = (baseOb == null) ? BigDecimal.ZERO : baseOb;
835
            BigDecimal tauxSal = (tauxSalOb == null) ? BigDecimal.ZERO : tauxSalOb;
18 ilm 836
 
837
            // Calcul du montant
838
            String formuleMontant = rowSource.getString("MONTANT");
839
 
144 ilm 840
            if (!isValidated) {
841
                BigDecimal montant = BigDecimal.ZERO;
842
                if (formuleMontant.trim().length() == 0) {
843
                    montant = base.multiply(tauxSal).setScale(2, RoundingMode.HALF_UP);
844
                } else {
845
                    Object montantNet = this.javaEdit.checkFormule(rowSource.getString("MONTANT"), "MONTANT");
846
                    BigDecimal montantNetS = (montantNet == null) ? BigDecimal.ZERO : new BigDecimal(montantNet.toString());
847
                    montant = montantNetS;
848
                }
18 ilm 849
 
144 ilm 850
                // Retenue
851
                if (rowSource.getInt("ID_TYPE_RUBRIQUE_BRUT") == 3) {
18 ilm 852
 
144 ilm 853
                    rowVals.put("MONTANT_SAL_DED", montant);
854
                    this.salBrut = this.salBrut.subtract(montant);
855
                    if (rowSource.getBoolean("TAXABLE_CM")) {
856
                        this.salBrutTaxable = this.salBrutTaxable.subtract(montant);
857
                    }
858
                    if (rowSource.getBoolean("COTISABLE")) {
859
                        this.salBrutCotis = this.salBrutCotis.subtract(montant);
860
                    }
861
                    if (rowSource.getBoolean("PART_BRUT")) {
862
                        this.salBrutBase = this.salBrutBase.subtract(montant);
863
                    }
864
                } // Gain
865
                else {
18 ilm 866
 
144 ilm 867
                    rowVals.put("MONTANT_SAL_AJ", montant);
868
                    this.salBrut = this.salBrut.add(montant);
869
                    if (rowSource.getBoolean("TAXABLE_CM")) {
870
                        this.salBrutTaxable = this.salBrutTaxable.add(montant);
871
                    }
872
                    if (rowSource.getBoolean("COTISABLE")) {
873
                        this.salBrutCotis = this.salBrutCotis.add(montant);
874
                    }
875
                    if (rowSource.getBoolean("PART_BRUT")) {
876
                        this.salBrutBase = this.salBrutBase.add(montant);
877
                    }
878
                    if (rowSource.getBoolean("AVANTAGE_NATURE")) {
879
                        this.avantage = this.avantage.add(montant);
880
                    }
881
 
882
                }
883
 
884
                // Mis a jour du salaire brut
885
                // updateValueFiche();
886
            } else {
887
                BigDecimal ded = rowVals.getBigDecimal("MONTANT_SAL_DED");
888
                if (ded != null) {
889
                    this.salBrut = this.salBrut.subtract(ded);
890
                    if (rowSource.getBoolean("TAXABLE_CM")) {
891
                        this.salBrutTaxable = this.salBrutTaxable.subtract(ded);
892
                    }
893
                    if (rowSource.getBoolean("COTISABLE")) {
894
                        this.salBrutCotis = this.salBrutCotis.subtract(ded);
895
                    }
896
                    if (rowSource.getBoolean("PART_BRUT")) {
897
                        this.salBrutBase = this.salBrutBase.subtract(ded);
898
                    }
899
                }
900
                BigDecimal add = rowVals.getBigDecimal("MONTANT_SAL_AJ");
901
                if (add != null) {
902
                    this.salBrut = this.salBrut.add(add);
903
                    if (rowSource.getBoolean("AVANTAGE_NATURE")) {
904
                        this.avantage = this.avantage.add(add);
905
                    }
906
                    if (rowSource.getBoolean("TAXABLE_CM")) {
907
                        this.salBrutTaxable = this.salBrutTaxable.add(add);
908
                    }
909
                    if (rowSource.getBoolean("COTISABLE")) {
910
                        this.salBrutCotis = this.salBrutCotis.add(add);
911
                    }
912
                    if (rowSource.getBoolean("PART_BRUT")) {
913
                        this.salBrutBase = this.salBrutBase.add(add);
914
                    }
915
                }
18 ilm 916
            }
917
        }
918
    }
919
 
920
    private void calculNet(SQLRow rowSource, SQLRowValues rowVals) {
921
 
132 ilm 922
        if (rowVals.getBoolean("IN_PERIODE")) {
18 ilm 923
 
132 ilm 924
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
925
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
18 ilm 926
 
132 ilm 927
            BigDecimal base = baseOb == null ? BigDecimal.ZERO : baseOb;
928
            BigDecimal tauxSal = tauxSalOb == null ? BigDecimal.ZERO : tauxSalOb;
18 ilm 929
 
144 ilm 930
            if (!isValidated) {
931
                // Calcul du montant
932
                String formuleMontant = rowSource.getString("MONTANT");
18 ilm 933
 
144 ilm 934
                BigDecimal montant = BigDecimal.ZERO;
935
                if (formuleMontant.trim().length() == 0) {
936
                    montant = base.multiply(tauxSal).setScale(2, RoundingMode.HALF_UP);
937
                } else {
938
                    Object montantNet = this.javaEdit.checkFormule(rowSource.getString("MONTANT"), "MONTANT");
939
                    if (montantNet != null) {
940
                        montant = new BigDecimal(montantNet.toString());
941
                    }
18 ilm 942
                }
943
 
144 ilm 944
                // Retenue
945
                if (rowSource.getInt("ID_TYPE_RUBRIQUE_NET") == 3) {
18 ilm 946
 
144 ilm 947
                    rowVals.put("MONTANT_SAL_DED", montant);
18 ilm 948
 
144 ilm 949
                    this.netAPayer = this.netAPayer.subtract(montant);
950
                    if (rowSource.getBoolean("IMPOSABLE")) {
951
                        this.netImp = this.netImp.subtract(montant);
952
                    }
18 ilm 953
 
144 ilm 954
                } // Gain
955
                else {
18 ilm 956
 
144 ilm 957
                    rowVals.put("MONTANT_SAL_AJ", montant);
18 ilm 958
 
144 ilm 959
                    this.netAPayer = this.netAPayer.add(montant);
960
                    if (rowSource.getBoolean("IMPOSABLE")) {
961
                        this.netImp = this.netImp.add(montant);
962
                    }
18 ilm 963
                }
144 ilm 964
 
965
                // Mis a jour du salaire net
966
                // updateValueFiche();
967
            } else {
968
                BigDecimal ded = rowVals.getBigDecimal("MONTANT_SAL_DED");
969
                if (ded != null) {
970
                    this.netAPayer = this.netAPayer.subtract(ded);
971
                    if (rowSource.getBoolean("IMPOSABLE")) {
972
                        this.netImp = this.netImp.subtract(ded);
973
                    }
974
                }
975
                BigDecimal add = rowVals.getBigDecimal("MONTANT_SAL_AJ");
976
                if (add != null) {
977
                    this.netAPayer = this.netAPayer.add(add);
978
                    if (rowSource.getBoolean("IMPOSABLE")) {
979
                        this.netImp = this.netImp.add(add);
980
                    }
981
                }
18 ilm 982
            }
983
        }
984
    }
985
 
986
    private void loadElementNet(SQLRow rowSource, SQLRow row) {
987
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
988
        // System.err.println("________________________LOAD ELT NET");
989
 
990
        if (!loadElement(rowVals, rowSource, row)) {
991
 
992
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
993
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX"), "TAUX");
994
            rowVals.put("NOM", rowSource.getString("NOM"));
132 ilm 995
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
996
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
18 ilm 997
        }
998
 
999
        calculNet(rowSource, rowVals);
1000
 
1001
        boolean b = isEltImprimable(rowSource, rowVals);
1002
        // System.err.println("Impression --- > " + b);
1003
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
1004
 
1005
        this.vectRubrique.add(rowVals);
1006
    }
1007
 
1008
    private void calculCotisation(SQLRow rowSource, SQLRowValues rowVals) {
1009
 
1010
        if (((Boolean) rowVals.getObject("IN_PERIODE")).booleanValue()) {
1011
 
132 ilm 1012
            BigDecimal baseOb = rowVals.getBigDecimal("NB_BASE");
1013
            BigDecimal tauxSalOb = rowVals.getBigDecimal("TAUX_SAL");
1014
            BigDecimal tauxPatOb = rowVals.getBigDecimal("TAUX_PAT");
18 ilm 1015
 
132 ilm 1016
            BigDecimal base = baseOb == null ? BigDecimal.ZERO : baseOb;
1017
            BigDecimal tauxSal = tauxSalOb == null ? BigDecimal.ZERO : tauxSalOb;
1018
            BigDecimal tauxPat = tauxPatOb == null ? BigDecimal.ZERO : tauxPatOb;
18 ilm 1019
 
1020
            // Calcul du montant
132 ilm 1021
            BigDecimal montantSal = base.multiply(tauxSal).movePointLeft(2).setScale(2, RoundingMode.HALF_UP);
1022
            BigDecimal montantPat = base.multiply(tauxPat).movePointLeft(2).setScale(2, RoundingMode.HALF_UP);
18 ilm 1023
 
132 ilm 1024
            rowVals.put("MONTANT_SAL_DED", montantSal);
1025
            rowVals.put("MONTANT_PAT", montantPat);
18 ilm 1026
 
132 ilm 1027
            this.netAPayer = this.netAPayer.subtract(montantSal);
1028
            if (!rowSource.getBoolean("IMPOSABLE")) {
1029
                this.netImp = this.netImp.subtract(montantSal);
18 ilm 1030
            }
1031
 
144 ilm 1032
            if (rowSource.getBoolean("ALLEGEMENT_COTISATION")) {
1033
                this.allegmentCotisation = this.allegmentCotisation.add(montantPat);
1034
            }
1035
 
94 ilm 1036
            if (rowSource.getBoolean("PART_PAT_IMPOSABLE")) {
132 ilm 1037
                this.netImp = this.netImp.add(montantPat);
94 ilm 1038
            }
1039
 
132 ilm 1040
            if (rowSource.getBoolean("PART_CSG")) {
1041
                this.csg = this.csg.add(montantPat);
1042
            }
94 ilm 1043
            if (rowSource.getBoolean("PART_CSG_SANS_ABATTEMENT")) {
132 ilm 1044
                this.csgSansAbattement = this.csgSansAbattement.add(montantPat);
94 ilm 1045
            }
144 ilm 1046
            if (rowSource.getBoolean("TAXABLE_CM")) {
1047
                this.taxCmSal = this.taxCmSal.add(montantSal);
1048
                this.taxCmPat = this.taxCmPat.add(montantPat);
1049
            } else {
1050
                this.cotSal = this.cotSal.add(montantSal);
1051
                this.cotPat = this.cotPat.add(montantPat);
1052
            }
94 ilm 1053
 
144 ilm 1054
            // CICE
1055
            int codeTP = rowSource.getForeignID("ID_CODE_CAISSE_TYPE_RUBRIQUE");
1056
            if (codeTP == 298) {
1057
                this.cice = rowVals.getBigDecimal("NB_BASE");
1058
            }
18 ilm 1059
 
1060
            // Mis a jour des cotisations
144 ilm 1061
            // updateValueFiche();
18 ilm 1062
        }
1063
    }
1064
 
132 ilm 1065
    private void loadElementCotisation(final SQLRow rowSource, SQLRow row) {
18 ilm 1066
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
1067
        // System.err.println("________________________LOAD ELT COTISATION");
1068
 
1069
        if (!loadElement(rowVals, rowSource, row)) {
1070
 
1071
            // On calcule les valeurs
1072
            Object baseOb = this.javaEdit.checkFormule(rowSource.getString("BASE"), "BASE");
132 ilm 1073
            if (!this.javaEdit.isCodeValid()) {
1074
                SwingUtilities.invokeLater(new Runnable() {
1075
 
1076
                    @Override
1077
                    public void run() {
1078
                        JOptionPane.showMessageDialog(null, "La formule BASE pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
1079
                    }
1080
                });
1081
            }
18 ilm 1082
            Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TX_SAL"), "TX_SAL");
132 ilm 1083
            if (!this.javaEdit.isCodeValid()) {
1084
                SwingUtilities.invokeLater(new Runnable() {
1085
 
1086
                    @Override
1087
                    public void run() {
1088
                        JOptionPane.showMessageDialog(null, "La formule TX_SAL pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
1089
                    }
1090
                });
1091
            }
18 ilm 1092
            Object tauxPatOb = this.javaEdit.checkFormule(rowSource.getString("TX_PAT"), "TX_PAT");
132 ilm 1093
            if (!this.javaEdit.isCodeValid()) {
1094
                SwingUtilities.invokeLater(new Runnable() {
1095
 
1096
                    @Override
1097
                    public void run() {
1098
                        JOptionPane.showMessageDialog(null, "La formule TX_PAT pour la rubrique " + rowSource.getString("CODE") + " n'est pas correcte!");
1099
                    }
1100
                });
1101
            }
18 ilm 1102
            rowVals.put("NOM", rowSource.getString("NOM"));
132 ilm 1103
            rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
1104
            rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
1105
            rowVals.put("TAUX_PAT", (tauxPatOb == null) ? null : new BigDecimal(tauxPatOb.toString()));
18 ilm 1106
        }
1107
 
1108
        calculCotisation(rowSource, rowVals);
1109
 
1110
        boolean b = isEltImprimable(rowSource, rowVals);
1111
        // System.err.println("Impression --- > " + b);
1112
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
1113
 
1114
        this.vectRubrique.add(rowVals);
1115
    }
1116
 
1117
    public void loadElementComm(SQLRow rowSource, SQLRow row) {
1118
        SQLRowValues rowVals = new SQLRowValues(tableFichePayeElt);
1119
        // System.err.println("________________________LOAD ELT COMM");
1120
 
1121
        if (loadElement(rowVals, rowSource, row)) {
1122
            this.vectRubrique.add(rowVals);
1123
            return;
1124
        }
1125
 
1126
        Object baseOb = this.javaEdit.checkFormule(rowSource.getString("NB_BASE"), "BASE");
1127
        Object tauxSalOb = this.javaEdit.checkFormule(rowSource.getString("TAUX_SAL"), "SAL");
1128
        Object tauxPatOb = this.javaEdit.checkFormule(rowSource.getString("TAUX_PAT"), "PAT");
1129
        Object montantPatOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_PAT"), "MONTANT");
1130
        Object montantAdOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_SAL_AJ"), "MONTANT");
1131
        Object montantDedOb = this.javaEdit.checkFormule(rowSource.getString("MONTANT_SAL_DED"), "MONTANT");
1132
        rowVals.put("NOM", rowSource.getBoolean("NOM_VISIBLE") ? rowSource.getString("NOM") : "");
132 ilm 1133
        rowVals.put("NB_BASE", (baseOb == null) ? null : new BigDecimal(baseOb.toString()));
1134
        rowVals.put("TAUX_SAL", (tauxSalOb == null) ? null : new BigDecimal(tauxSalOb.toString()));
1135
        rowVals.put("TAUX_PAT", (tauxPatOb == null) ? null : new BigDecimal(tauxPatOb.toString()));
1136
        rowVals.put("MONTANT_PAT", (montantPatOb == null) ? null : new BigDecimal(montantPatOb.toString()));
1137
        rowVals.put("MONTANT_SAL_AJ", (montantAdOb == null) ? null : new BigDecimal(montantAdOb.toString()));
1138
        rowVals.put("MONTANT_SAL_DED", (montantDedOb == null) ? null : new BigDecimal(montantDedOb.toString()));
18 ilm 1139
 
1140
        boolean b = isEltImprimable(rowSource, rowVals);
1141
        // System.err.println("Impression --- > " + b);
1142
        rowVals.put("IMPRESSION", Boolean.valueOf(b));
1143
 
1144
        this.vectRubrique.add(rowVals);
1145
    }
1146
 
1147
    private void calculValue() {
1148
 
1149
        System.err.println("Start calculValue At " + new Date());
1150
 
1151
        resetValueFiche();
1152
 
1153
        /*
1154
         * this.threadUpdate = new Thread("Update Fiche Paye") { public void run() {
1155
         */
1156
        Vector<SQLRowValues> vectTmp = new Vector<SQLRowValues>(this.vectRubrique);
1157
 
1158
        this.vectRubrique = new Vector<SQLRowValues>();
1159
        for (int i = 0; i < vectTmp.size(); i++) {
1160
 
1161
            SQLRowValues rowVals = vectTmp.get(i);
1162
            String source = rowVals.getString("SOURCE");
1163
            int idSource = rowVals.getInt("IDSOURCE");
1164
            SQLRow row = tableFichePayeElt.getRow(rowVals.getID());
1165
 
1166
            if (source.trim().length() != 0) {
1167
 
1168
                // System.err.println("Source != null");
1169
 
1170
                if (this.mapTableSource.get(source) != null) {
1171
                    SQLRow rowSource = this.mapTableSource.get(source).getRow(idSource);
1172
 
1173
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_BRUT")) {
1174
                        loadElementBrut(rowSource, row);
1175
                    }
1176
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COTISATION")) {
1177
                        loadElementCotisation(rowSource, row);
1178
                    }
1179
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_NET")) {
1180
                        loadElementNet(rowSource, row);
1181
                    }
1182
                    if (rowSource.getTable().getName().equalsIgnoreCase("RUBRIQUE_COMM")) {
1183
                        loadElementComm(rowSource, row);
1184
                    }
1185
                } else {
1186
                    System.err.println("Table " + source + " non référencée");
1187
                }
1188
            }
1189
        }
1190
        System.err.println(this.vectRubrique.size() + " elements ADDed ");
144 ilm 1191
        updateValueFiche();
18 ilm 1192
        fireTableDataChanged();
1193
        /*
1194
         * } }; this.threadUpdate.start();
1195
         */
1196
 
1197
        System.err.println("End calculValue At " + new Date());
1198
    }
1199
 
1200
    public void validElt() {
1201
 
1202
        System.err.println("Validation des éléments de la fiche.");
1203
        for (int i = 0; i < this.vectRubrique.size(); i++) {
1204
            SQLRowValues rowVals = this.vectRubrique.get(i);
1205
            rowVals.put("VALIDE", Boolean.valueOf(true));
1206
 
1207
            try {
1208
                rowVals.commit();
1209
            } catch (SQLException e) {
1210
                e.printStackTrace();
1211
            }
1212
        }
1213
        System.err.println("Validation terminée.");
1214
    }
1215
}