OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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