OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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.core.humanresources.payroll.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
156 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18 ilm 18
import org.openconcerto.erp.core.common.ui.SQLJavaEditor;
19
import org.openconcerto.erp.core.humanresources.payroll.component.FormuleTreeNode;
20
import org.openconcerto.erp.core.humanresources.payroll.component.VariableRowTreeNode;
21
import org.openconcerto.erp.core.humanresources.payroll.component.VariableTree;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.element.BaseSQLComponent;
24
import org.openconcerto.sql.element.SQLComponent;
132 ilm 25
import org.openconcerto.sql.element.TreesOfSQLRows;
156 ilm 26
import org.openconcerto.sql.model.DBRoot;
18 ilm 27
import org.openconcerto.sql.model.SQLBase;
28
import org.openconcerto.sql.model.SQLField;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowAccessor;
31
import org.openconcerto.sql.model.SQLSelect;
32
import org.openconcerto.sql.model.SQLTable;
33
import org.openconcerto.sql.model.Where;
34
import org.openconcerto.sql.sqlobject.ElementComboBox;
35
import org.openconcerto.sql.view.EditFrame;
36
import org.openconcerto.ui.DefaultGridBagConstraints;
37
import org.openconcerto.ui.component.ITextArea;
38
import org.openconcerto.ui.warning.JLabelWarning;
39
import org.openconcerto.utils.ExceptionHandler;
21 ilm 40
import org.openconcerto.utils.checks.ValidState;
41
import org.openconcerto.utils.text.SimpleDocumentListener;
18 ilm 42
 
43
import java.awt.Component;
44
import java.awt.Dimension;
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.awt.event.MouseAdapter;
50
import java.awt.event.MouseEvent;
51
import java.beans.PropertyChangeEvent;
52
import java.beans.PropertyChangeListener;
53
import java.sql.SQLException;
54
import java.util.ArrayList;
156 ilm 55
import java.util.Arrays;
18 ilm 56
import java.util.HashMap;
57
import java.util.HashSet;
58
import java.util.List;
59
import java.util.Map;
60
import java.util.Set;
61
 
62
import javax.swing.AbstractAction;
63
import javax.swing.ButtonGroup;
64
import javax.swing.JLabel;
156 ilm 65
import javax.swing.JOptionPane;
18 ilm 66
import javax.swing.JPanel;
67
import javax.swing.JPopupMenu;
68
import javax.swing.JRadioButton;
69
import javax.swing.JScrollPane;
70
import javax.swing.JSplitPane;
71
import javax.swing.JTextField;
72
import javax.swing.event.DocumentEvent;
73
import javax.swing.tree.TreePath;
74
 
75
import org.apache.commons.dbutils.handlers.ArrayListHandler;
76
import org.jedit.CTokenMarker;
77
 
78
// FIXME retirer le scrolling de l'edit frame pour scroller uniquement sur l'arbre des variables
156 ilm 79
public class VariablePayeSQLElement extends ComptaSQLConfElement {
18 ilm 80
 
21 ilm 81
    private final static ValidState VAR_ALREADY_EXIST = ValidState.createInvalid("Cette variable existe déjà !");
82
    private final static ValidState VAR_NAME_NOT_CORRECT = ValidState.createInvalid("Nom de variable incorrect !");
83
    private final static ValidState VAR_NO_NAME = ValidState.createInvalid("Aucun nom attribué !");
84
 
18 ilm 85
    private static SQLTable tableVarSal = null;
86
 
87
    private static SQLTable getTableVarSal() {
88
        if (tableVarSal == null)
89
            tableVarSal = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("VARIABLE_SALARIE");
90
        return tableVarSal;
91
    }
92
 
156 ilm 93
    public VariablePayeSQLElement(final DBRoot root) {
94
        super(root.getTable("VARIABLE_PAYE"), "une variable de paye", "variables de paye");
18 ilm 95
    }
96
 
97
    protected List<String> getListFields() {
98
        final List<String> l = new ArrayList<String>();
99
        l.add("NOM");
100
        l.add("CATEGORIE");
101
        l.add("VALEUR");
102
        l.add("FORMULE");
103
        return l;
104
    }
105
 
106
    @Override
107
    public Set<String> getInsertOnlyFields() {
108
        Set<String> s = new HashSet<String>();
109
        s.add("NOM");
110
        return s;
111
    }
112
 
113
    protected List<String> getComboFields() {
114
        final List<String> l = new ArrayList<String>();
115
        l.add("NOM");
116
        return l;
117
    }
118
 
57 ilm 119
    public static final boolean isForbidden(final String code) {
21 ilm 120
        final List<String> l = getForbiddenVarName();
121
        for (int i = 0; i < l.size(); i++) {
122
            if (l.get(i).trim().equalsIgnoreCase(code)) {
123
                return true;
124
            }
125
        }
126
        return false;
127
    }
128
 
18 ilm 129
    // Liste des variables deja definit
130
    // L'utilisateur ne peut pas utiliser ces noms
131
    public static final List<String> getForbiddenVarName() {
132
 
133
        List<String> l = new ArrayList<String>();
134
 
135
        for (SQLField field : getTableVarSal().getContentFields()) {
136
 
137
            // String field = i.next().toString().trim();
138
            // field = field.substring(field.indexOf('.') + 1, field.length() - 1);
139
            l.add(field.getName());
140
        }
141
 
142
        l.add("PAT");
143
        l.add("SAL");
144
        l.add("BASE");
145
        l.add("MONTANT");
146
        l.add("TAUX");
147
 
148
        l.add("ACOMPTE");
149
        l.add("DUREE_HEBDO");
150
        l.add("DUREE_MOIS");
151
        l.add("SALAIRE_MOIS");
152
        l.add("TAUX_AT");
153
        l.add("CONGES_PAYES");
154
        l.add("NB_ENFANTS");
155
        l.add("NB_PERS_A_CHARGE");
156
        l.add("PAT");
157
        l.add("SAL");
158
        l.add("MONTANT");
159
 
160
        l.add("SAL_BRUT");
161
        l.add("COT_PAT");
162
        l.add("COT_SAL");
163
        l.add("NET_IMP");
164
        l.add("NET_A_PAYER");
165
        l.add("CSG");
166
 
167
        l.add("CONGES_ACQUIS");
168
        l.add("CSG_C");
169
        l.add("CONGES_PRIS");
170
        l.add("RESTANT");
171
        l.add("COT_PAT_C");
172
        l.add("COT_SAL_C");
173
        l.add("HEURE_ABS");
174
        l.add("HEURE_TRAV");
175
        l.add("HEURE_110");
176
        l.add("HEURE_125");
177
        l.add("HEURE_150");
178
        l.add("HEURE_200");
179
        l.add("SAL_BRUT_C");
144 ilm 180
        l.add("HEURE_TRAV_C");
18 ilm 181
        l.add("NET_A_PAYER_C");
182
        l.add("NET_IMP_C");
132 ilm 183
 
144 ilm 184
        l.add("ANCIENNETE_JOUR");
185
 
132 ilm 186
        l.add("COEFF");
187
        l.add("ECHELON");
188
        l.add("NIVEAU");
189
        l.add("POSITION");
190
        l.add("INDICE");
191
 
18 ilm 192
        return l;
193
    }
194
 
195
    // Map qui contient la structure de l'arbre
196
    public static final Map<String, List<?>> getMapTree() {
197
 
198
        SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
199
        SQLBase baseCommon = Configuration.getInstance().getBase();
200
        SQLTable tableVar = baseCommon.getTable("VARIABLE_PAYE");
201
        SQLTable tableCumulsConges = base.getTable("CUMULS_CONGES");
202
        SQLTable tableCumulsPaye = base.getTable("CUMULS_PAYE");
156 ilm 203
        SQLTable tablePAS = base.getTable("PAS");
18 ilm 204
 
205
        Map<String, List<?>> mapTree = new HashMap<String, List<?>>();
206
 
207
        // Variables propre au salarié
208
        List<SQLField> varSal = new ArrayList<SQLField>();
209
 
210
        for (SQLField field : getTableVarSal().getContentFields()) {
211
 
212
            varSal.add(field);
213
        }
214
        for (SQLField field : tableCumulsConges.getContentFields()) {
215
 
216
            varSal.add(field);
217
        }
218
 
219
        for (SQLField field : tableCumulsPaye.getContentFields()) {
220
 
221
            varSal.add(field);
222
        }
223
 
156 ilm 224
        for (SQLField field : tablePAS.getContentFields()) {
225
 
226
            varSal.add(field);
227
        }
228
 
18 ilm 229
        mapTree.put("Infos période", varSal);
230
 
231
        List<SQLField> l = new ArrayList<SQLField>();
232
        SQLTable tableInfos = base.getTable("INFOS_SALARIE_PAYE");
233
        l.add(tableInfos.getField("DUREE_HEBDO"));
234
        l.add(tableInfos.getField("DUREE_MOIS"));
235
        l.add(tableInfos.getField("SALAIRE_MOIS"));
236
        l.add(tableInfos.getField("TAUX_AT"));
237
        l.add(tableInfos.getField("CONGES_PAYES"));
132 ilm 238
        if (tableInfos.contains("BASE_FILLON_ANNUELLE")) {
239
            l.add(tableInfos.getField("BASE_FILLON_ANNUELLE"));
240
        }
241
 
91 ilm 242
        if (tableInfos.contains("PRIME_TRANSPORT")) {
243
            l.add(tableInfos.getField("PRIME_LOGEMENT"));
244
            l.add(tableInfos.getField("PRIME_PANIER"));
245
            l.add(tableInfos.getField("PRIME_TRANSPORT"));
246
        }
18 ilm 247
        mapTree.put("Contrat salarié", l);
248
 
249
        List<SQLField> l2 = new ArrayList<SQLField>();
250
        SQLTable tableFichePaye = base.getTable("FICHE_PAYE");
251
        l2.add(tableFichePaye.getField("CONGES_ACQUIS"));
252
        l2.add(tableFichePaye.getField("ACOMPTE"));
253
        l2.add(tableFichePaye.getField("SAL_BRUT"));
144 ilm 254
        l2.add(tableFichePaye.getField("SAL_BRUT_TAXABLE"));
255
        l2.add(tableFichePaye.getField("SAL_BASE_BRUT"));
256
        l2.add(tableFichePaye.getField("TAXE_CM_SAL"));
257
        l2.add(tableFichePaye.getField("TAXE_CM_PAT"));
258
        l2.add(tableFichePaye.getField("SAL_BRUT_COTISABLE"));
18 ilm 259
        l2.add(tableFichePaye.getField("COT_PAT"));
260
        l2.add(tableFichePaye.getField("COT_SAL"));
261
        l2.add(tableFichePaye.getField("NET_IMP"));
262
        l2.add(tableFichePaye.getField("NET_A_PAYER"));
263
        l2.add(tableFichePaye.getField("CSG"));
177 ilm 264
        l2.add(tableFichePaye.getField("CSG_REDUITE"));
132 ilm 265
 
18 ilm 266
        mapTree.put("Contenu paye", l2);
267
 
132 ilm 268
        List<SQLField> l3 = new ArrayList<SQLField>();
269
        SQLTable tableClassement = base.getTable("CLASSEMENT_CONVENTIONNEL");
270
        l3.add(tableClassement.getField("COEFF"));
271
        l3.add(tableClassement.getField("ECHELON"));
272
        l3.add(tableClassement.getField("NIVEAU"));
273
        l3.add(tableClassement.getField("POSITION"));
274
        l3.add(tableClassement.getField("INDICE"));
275
        mapTree.put("Classement conventionnel", l3);
276
 
277
        List<SQLField> l4 = new ArrayList<SQLField>();
278
        SQLTable tableCoeff = base.getTable("COEFF_PRIME");
279
        if (tableCoeff != null) {
280
            l4.add(tableCoeff.getField("PRIME_PERSO"));
281
            l4.add(tableCoeff.getField("PRIME_RECONSTRUCTION"));
282
            l4.add(tableCoeff.getField("PRIME_ANCIENNETE"));
283
            l4.add(tableCoeff.getField("PRIME_DEROULEMENT"));
284
            mapTree.put("Coefficient de prime", l4);
285
        }
18 ilm 286
        /*
287
         * List lEtat = new ArrayList(); SQLTable tableEtat =
288
         * Configuration.getInstance().getBase().getTable("ETAT_CIVIL");
289
         * lEtat.add(tableEtat.getField("NB_ENFANTS"));
290
         * lEtat.add(tableEtat.getField("NB_PERS_A_CHARGE")); mapTree.put("Salarie.etat_civil",
291
         * lEtat);
292
         */
293
 
294
        SQLSelect selAllVarID = new SQLSelect(tableVar.getBase());
295
 
296
        selAllVarID.addSelect(tableVar.getField("ID"));
297
        selAllVarID.addRawOrder("LENGTH(\"VARIABLE_PAYE\".\"NOM\") DESC");
298
 
299
        String reqAllVarID = selAllVarID.asString();
300
        Object[] objKeysRowVar = ((List) tableVar.getBase().getDataSource().execute(reqAllVarID, new ArrayListHandler())).toArray();
301
 
302
        List<SQLRow> lVar = new ArrayList<SQLRow>();
303
        for (int i = 0; i < objKeysRowVar.length; i++) {
304
            Object[] tmp = (Object[]) objKeysRowVar[i];
305
            lVar.add(tableVar.getRow(Integer.parseInt(tmp[0].toString())));
306
        }
307
 
308
        if (lVar.size() > 0) {
309
            mapTree.put("Variables", lVar);
310
        }
311
 
312
        List<Map<String, String>> fonction = new ArrayList<Map<String, String>>();
313
        Map<String, String> listFonction = new HashMap<String, String>();
314
        listFonction.put("Minimum", "Math.min()");
315
        listFonction.put("Maximum", "Math.max()");
316
        listFonction.put("Valeur absolue", "Math.abs()");
317
        listFonction.put("Arrondi", "Math.round()");
318
 
319
        fonction.add(listFonction);
320
 
321
        mapTree.put("Fonctions", fonction);
322
        // Chargement des variables dans l'éditeur
323
        // System.out.println(mapTree);
324
 
325
        return mapTree;
326
    }
327
 
328
    /*
329
     * (non-Javadoc)
330
     *
331
     * @see org.openconcerto.devis.SQLElement#getComponent()
332
     */
333
    public SQLComponent createComponent() {
334
 
335
        return new BaseSQLComponent(this) {
336
 
21 ilm 337
            private ValidState validVarName;
18 ilm 338
            private JRadioButton radioVal = new JRadioButton("Valeur");
339
            private JRadioButton radioFormule = new JRadioButton("Formule");
340
 
341
            private final JTextField textValeur = new JTextField();
342
            // private final ITextArea textFormule = new ITextArea();
343
            private final VariableTree treeVariable = new VariableTree();
344
            private final JTextField textNom = new JTextField();
21 ilm 345
            private final JLabel labelWarningBadVar = new JLabelWarning();
18 ilm 346
            private ElementComboBox comboSelSal;
347
            private EditFrame edit = null;
348
            private final SQLJavaEditor textFormule = new SQLJavaEditor(getMapTree());
349
 
350
            public void addViews() {
351
                this.setLayout(new GridBagLayout());
352
                final GridBagConstraints c = new DefaultGridBagConstraints();
353
 
21 ilm 354
                this.validVarName = null;
18 ilm 355
                this.textFormule.setEditable(false);
356
 
357
                // Arbre des variables
358
                JScrollPane sc = new JScrollPane(this.treeVariable);
359
                sc.setPreferredSize(new Dimension(150, sc.getPreferredSize().height));
360
 
361
                this.treeVariable.addMouseListener(new MouseAdapter() {
362
                    public void mousePressed(final MouseEvent mE) {
363
                        if (mE.getButton() == MouseEvent.BUTTON3) {
364
                            JPopupMenu menuDroit = new JPopupMenu();
365
 
366
                            TreePath path = treeVariable.getClosestPathForLocation(mE.getPoint().x, mE.getPoint().y);
367
 
368
                            final Object obj = path.getLastPathComponent();
369
 
370
                            if ((obj == null) || !(obj instanceof VariableRowTreeNode)) {
371
                                return;
372
                            }
373
 
374
                            menuDroit.add(new AbstractAction("Editer") {
375
                                public void actionPerformed(ActionEvent e) {
376
                                    if (edit == null) {
80 ilm 377
                                        edit = new EditFrame(getElement(), EditFrame.MODIFICATION);
18 ilm 378
                                    }
379
 
380
                                    System.err.println("Action performed");
381
 
382
                                    if (obj != null) {
383
                                        System.err.println("Object not null --> " + obj.toString());
384
                                        if (obj instanceof VariableRowTreeNode) {
385
                                            System.err.println("Object VariableRowTreeNode");
386
                                            VariableRowTreeNode varNode = (VariableRowTreeNode) obj;
387
 
388
                                            edit.selectionId(varNode.getID(), 1);
389
                                            edit.setVisible(true);
390
                                        }
391
                                    }
392
                                }
393
                            });
394
                            menuDroit.show((Component) mE.getSource(), mE.getPoint().x, mE.getPoint().y);
395
                        } else {
396
                            if (mE.getClickCount() == 2) {
397
                                TreePath path = treeVariable.getClosestPathForLocation(mE.getPoint().x, mE.getPoint().y);
398
                                Object obj = path.getLastPathComponent();
399
 
400
                                if (obj != null) {
401
                                    if (obj instanceof FormuleTreeNode) {
402
                                        final FormuleTreeNode n = (FormuleTreeNode) obj;
403
 
404
                                        int start = textFormule.getSelectionStart();
405
                                        String tmp = textFormule.getText();
406
                                        textFormule.setText(tmp.substring(0, start) + n.getTextValue() + tmp.substring(start, tmp.length()));
407
                                    }
408
                                }
409
                            }
410
                        }
411
                    }
412
                });
413
 
414
                JPanel panelDroite = new JPanel();
415
                panelDroite.setLayout(new GridBagLayout());
416
 
417
                // Categorie
418
                JTextField textCategorie = new JTextField();
419
                c.fill = GridBagConstraints.HORIZONTAL;
420
                c.gridheight = 1;
421
                c.gridx = 1;
422
                c.gridy = 0;
423
                JLabel labelCategorie = new JLabel("Catégorie");
424
                panelDroite.add(labelCategorie, c);
425
                c.gridx++;
21 ilm 426
                c.gridwidth = GridBagConstraints.REMAINDER;
18 ilm 427
                panelDroite.add(textCategorie, c);
21 ilm 428
                c.gridwidth = 1;
18 ilm 429
 
430
                // Nom
431
                c.fill = GridBagConstraints.HORIZONTAL;
432
                c.gridheight = 1;
433
                c.gridx = 1;
434
                c.gridy++;
435
                JLabel labelNom = new JLabel("Nom");
436
                panelDroite.add(labelNom, c);
437
 
438
                c.gridx++;
21 ilm 439
                c.weightx = 1;
18 ilm 440
                panelDroite.add(this.textNom, c);
441
 
21 ilm 442
                this.textNom.getDocument().addDocumentListener(new SimpleDocumentListener() {
443
                    @Override
444
                    public void update(DocumentEvent e) {
445
                        updateValidVarName();
18 ilm 446
                    }
447
                });
448
 
449
                c.gridx++;
21 ilm 450
                c.weightx = 0;
18 ilm 451
                panelDroite.add(this.labelWarningBadVar, c);
452
 
453
                // Description
454
                JLabel labelInfos = new JLabel(getLabelFor("INFOS"));
455
                ITextArea textInfos = new ITextArea();
456
                c.gridy++;
457
                c.gridx = 1;
458
                c.gridwidth = 1;
21 ilm 459
                c.weightx = 0;
18 ilm 460
                panelDroite.add(labelInfos, c);
461
                c.gridx++;
462
                c.gridwidth = GridBagConstraints.REMAINDER;
21 ilm 463
                c.weightx = 1;
18 ilm 464
                c.weighty = 0;
465
                panelDroite.add(textInfos, c);
466
 
467
                // Valeur
468
                c.gridx = 1;
469
                c.gridy++;
470
                c.gridwidth = 1;
21 ilm 471
                c.weightx = 0;
18 ilm 472
                panelDroite.add(this.radioVal, c);
473
 
474
                c.gridx++;
21 ilm 475
                c.weightx = 1;
18 ilm 476
                c.gridwidth = GridBagConstraints.REMAINDER;
477
                panelDroite.add(this.textValeur, c);
478
 
479
                c.gridwidth = 1;
480
                c.gridx = 1;
481
                c.gridy++;
482
                panelDroite.add(this.radioFormule, c);
483
 
484
                c.gridx++;
485
                c.weightx = 1;
486
                c.weighty = 1;
487
                c.fill = GridBagConstraints.BOTH;
488
                c.gridwidth = GridBagConstraints.REMAINDER;
489
                panelDroite.add(this.textFormule, c);
490
                c.gridwidth = 1;
491
 
492
                ButtonGroup group = new ButtonGroup();
493
                group.add(this.radioVal);
494
                group.add(this.radioFormule);
495
 
496
                this.radioVal.setSelected(true);
497
                setFormuleEnabled(false);
498
 
499
                this.radioVal.addActionListener(new ActionListener() {
500
                    public void actionPerformed(ActionEvent e) {
501
 
502
                        setFormuleEnabled(false);
503
                    }
504
                });
505
                this.radioFormule.addActionListener(new ActionListener() {
506
                    public void actionPerformed(ActionEvent e) {
507
 
508
                        setFormuleEnabled(true);
509
                    }
510
                });
511
 
512
                c.gridy++;
513
                c.gridx = 1;
514
                c.weighty = 0;
515
                c.weightx = 0;
516
                c.fill = GridBagConstraints.HORIZONTAL;
517
 
518
                this.comboSelSal = new ElementComboBox(false);
80 ilm 519
                this.comboSelSal.init(getDirectory().getElement(SalarieSQLElement.class));
18 ilm 520
 
521
                c.gridx++;
21 ilm 522
                c.gridwidth = GridBagConstraints.REMAINDER;
523
                c.weightx = 0;
18 ilm 524
                panelDroite.add(this.comboSelSal, c);
21 ilm 525
                c.gridwidth = 1;
18 ilm 526
 
527
                JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sc, panelDroite);
528
 
529
                c.fill = GridBagConstraints.BOTH;
530
                c.gridx = 0;
531
                c.gridy = 0;
532
                c.weightx = 1;
533
                c.weighty = 1;
534
                this.add(split, c);
535
 
536
                this.addRequiredSQLObject(this.textNom, "NOM");
537
                this.addSQLObject(this.textValeur, "VALEUR");
538
                this.addSQLObject(this.textFormule, "FORMULE");
539
                this.addSQLObject(textCategorie, "CATEGORIE");
540
                this.addSQLObject(textInfos, "INFOS");
541
 
542
                this.comboSelSal.addValueListener(new PropertyChangeListener() {
543
                    public void propertyChange(PropertyChangeEvent evt) {
544
                        // TODO Auto-generated method stub
545
                        textFormule.setSalarieID(comboSelSal.getSelectedId());
546
                    }
547
                });
548
            }
549
 
21 ilm 550
            @Override
551
            public synchronized ValidState getValidState() {
552
                return super.getValidState().and(this.validVarName);
18 ilm 553
            }
554
 
555
            private void setFormuleEnabled(boolean b) {
556
 
557
                if (b) {
558
                    this.textValeur.setText("");
559
                } else {
560
                    this.textFormule.setText("");
561
                }
562
 
563
                this.textValeur.setEditable(!b);
564
                this.textValeur.setEnabled(!b);
565
                this.textFormule.setEditable(b);
566
                this.textFormule.setEnabled(b);
567
                this.treeVariable.setEnabled(b);
568
                this.treeVariable.setEditable(b);
569
            }
570
 
21 ilm 571
            private void setValidVarName(ValidState s) {
572
                if (!s.equals(this.validVarName)) {
573
                    this.validVarName = s;
574
                    final boolean warningVisible = !s.isValid();
575
                    if (warningVisible)
576
                        this.labelWarningBadVar.setText(s.getValidationText());
577
                    this.labelWarningBadVar.setVisible(warningVisible);
578
                    this.fireValidChange();
579
                }
580
            }
18 ilm 581
 
21 ilm 582
            private void updateValidVarName() {
583
                this.setValidVarName(this.computeValidVarName());
584
            }
585
 
586
            private ValidState computeValidVarName() {
18 ilm 587
                // on vérifie si la syntaxe de la variable est correct (chiffre lettre et _)
21 ilm 588
                final String varName = this.textNom.getText().trim();
18 ilm 589
 
590
                System.err.println("Verification de la validité du nom de la variable.");
591
 
592
                if (varName.length() == 0) {
21 ilm 593
                    return VAR_NO_NAME;
18 ilm 594
                }
595
 
596
                // ne contient que des chiffre lettre et _ et ne commence pas par un chiffre
597
                if (!isJavaVar(varName)) {
21 ilm 598
                    return VAR_NAME_NOT_CORRECT;
18 ilm 599
                }
600
 
601
                // on vérifie que la variable n'existe pas déja
602
                SQLSelect selAllVarName = new SQLSelect(getTable().getBase());
603
 
604
                selAllVarName.addSelect(VariablePayeSQLElement.this.getTable().getField("ID"));
21 ilm 605
                Where w = new Where(VariablePayeSQLElement.this.getTable().getField("NOM"), "=", varName);
18 ilm 606
                w = w.and(new Where(VariablePayeSQLElement.this.getTable().getKey(), "!=", getSelectedID()));
607
                selAllVarName.setWhere(w);
608
 
609
                String reqAllVarName = selAllVarName.asString();// + " AND '" + varName.trim() + "'
610
                // REGEXP VARIABLE_PAYE.NOM";
611
                Object[] objKeysRowName = ((List) getTable().getBase().getDataSource().execute(reqAllVarName, new ArrayListHandler())).toArray();
612
 
613
                if (objKeysRowName.length > 0) {
21 ilm 614
                    return VAR_ALREADY_EXIST;
18 ilm 615
                } else {
616
 
617
                    // Impossible de créer une variable du meme nom qu'un champ du salarie
21 ilm 618
                    if (isForbidden(varName))
619
                        return VAR_ALREADY_EXIST;
18 ilm 620
 
21 ilm 621
                    this.textFormule.setVarAssign(varName);
622
                    return ValidState.getTrueInstance();
18 ilm 623
                }
624
            }
625
 
626
            private boolean isJavaVar(String s) {
627
                if ((s.charAt(0) >= '0') && ((s.charAt(0) <= '9'))) {
628
                    System.err.println("Erreur la variable commence par un chiffre!!");
629
                    return false;
630
                } else {
631
                    for (int i = 0; i < s.length(); i++) {
632
 
633
                        if (!(((s.charAt(i) >= '0') && (s.charAt(i) <= '9')) || (s.charAt(i) >= 'a') && (s.charAt(i) <= 'z') || (s.charAt(i) >= 'A') && (s.charAt(i) <= 'Z') || (s.charAt(i) == '_'))) {
634
                            System.err.println("Erreur la variable contient un caractere incorrect!!");
635
                            return false;
636
                        }
637
                    }
638
 
639
                    return (!CTokenMarker.getKeywords().isExisting(s));
640
                }
641
            }
642
 
643
            @Override
644
            public void select(SQLRowAccessor r) {
645
 
646
                super.select(r);
647
                // System.err.println("Select RowAccess -------> " + r.getID() + " For Object " +
648
                // this.hashCode());
649
                if (r != null) {
650
                    if (r.getString("FORMULE").trim().length() == 0) {
651
                        this.radioVal.setSelected(true);
652
                        setFormuleEnabled(false);
653
                    } else {
654
                        this.radioFormule.setSelected(true);
655
                        setFormuleEnabled(true);
656
                    }
657
 
658
                    this.textFormule.setVarAssign(r.getString("NOM"));
659
                }
660
 
21 ilm 661
                this.updateValidVarName();
18 ilm 662
            }
663
        };
664
    }
665
 
666
    @Override
132 ilm 667
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
668
        for (SQLRow row : trees.getRows()) {
669
            secureArchiveVariable(row, cutLinks);
670
        }
18 ilm 671
    }
672
 
673
    // Archive la variable si elle n'est pas utilisé dans une formule
132 ilm 674
    private void secureArchiveVariable(SQLRow row, boolean cutLinks) throws SQLException {
18 ilm 675
 
676
        if (row != null) {
156 ilm 677
            // Test si la variable n'est pas utilisée dans une autre variable
678
            SQLSelect selCheckVar = new SQLSelect();
679
            selCheckVar.addSelect(getTable().getField("ID"));
680
            final String patternMatching = "%" + row.getString("NOM") + "%";
681
            selCheckVar.setWhere(new Where(getTable().getField("FORMULE"), "LIKE", patternMatching));
682
            selCheckVar.andWhere(new Where(getTable().getField("ID"), "!=", row.getID()));
683
            String reqCheckVar = selCheckVar.asString();
684
            List<Object> l = getTable().getBase().getDataSource().executeA(reqCheckVar);
685
            if (l.isEmpty()) {
686
                l = checkVariableInRubrique(patternMatching, "RUBRIQUE_BRUT", Arrays.asList("BASE", "TAUX", "MONTANT"));
687
                if (l.isEmpty()) {
688
                    l = checkVariableInRubrique(patternMatching, "RUBRIQUE_COTISATION", Arrays.asList("BASE", "TX_SAL", "TX_PAT"));
689
                    if (l.isEmpty()) {
690
                        l = checkVariableInRubrique(patternMatching, "RUBRIQUE_NET", Arrays.asList("BASE", "TAUX", "MONTANT"));
691
                        if (l.isEmpty()) {
692
                            super.archive(new TreesOfSQLRows(this, row), true);
693
                        } else {
694
                            JOptionPane.showMessageDialog(null, "Suppression impossible, cette variable est référencée dans une rubrique de net!");
695
                        }
18 ilm 696
 
156 ilm 697
                    } else {
698
                        JOptionPane.showMessageDialog(null, "Suppression impossible, cette variable est référencée dans une rubrique de cotisation!");
699
                    }
700
 
701
                } else {
702
                    JOptionPane.showMessageDialog(null, "Suppression impossible, cette variable est référencée dans une rubrique de brut!");
703
                }
18 ilm 704
            } else {
156 ilm 705
                JOptionPane.showMessageDialog(null, "Suppression impossible, cette variable est référencée par une autre.");
18 ilm 706
            }
707
 
708
        } else {
132 ilm 709
            super.archive(new TreesOfSQLRows(this, row), true);
18 ilm 710
        }
711
    }
57 ilm 712
 
156 ilm 713
    private List checkVariableInRubrique(final String patternMatching, final String tableRub, List<String> fieldsToCheck) {
714
 
715
        SQLSelect selBrut = new SQLSelect();
716
        final SQLTable tableRubBrut = getTable().getDBRoot().findTable(tableRub);
717
        selBrut.addSelect(tableRubBrut.getField("ID"));
718
        for (String fieldToCheck : fieldsToCheck) {
719
            selBrut.setWhere(new Where(tableRubBrut.getField(fieldToCheck), "LIKE", patternMatching));
720
        }
721
 
722
        String reqCheckBrut = selBrut.asString();
723
        return getTable().getBase().getDataSource().executeA(reqCheckBrut);
724
 
725
    }
726
 
57 ilm 727
    @Override
728
    protected String createCode() {
729
        return "humanresources.payroll.payment.variable";
730
    }
18 ilm 731
}