OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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