OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
174 ilm 18
import org.openconcerto.erp.core.customerrelationship.customer.element.ContactItemTable;
144 ilm 19
import org.openconcerto.erp.core.edm.AttachmentAction;
18 ilm 20
import org.openconcerto.sql.element.BaseSQLComponent;
21
import org.openconcerto.sql.element.ElementSQLObject;
22
import org.openconcerto.sql.element.SQLComponent;
142 ilm 23
import org.openconcerto.sql.element.SQLElement;
18 ilm 24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
174 ilm 29
import org.openconcerto.sql.model.UndefinedRowValuesCache;
18 ilm 30
import org.openconcerto.sql.model.Where;
31
import org.openconcerto.sql.sqlobject.ElementComboBox;
142 ilm 32
import org.openconcerto.sql.view.EditFrame;
174 ilm 33
import org.openconcerto.sql.view.EditPanel.EditMode;
142 ilm 34
import org.openconcerto.sql.view.EditPanelListener;
35
import org.openconcerto.sql.view.list.IListe;
36
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
37
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 38
import org.openconcerto.ui.DefaultGridBagConstraints;
174 ilm 39
import org.openconcerto.ui.FormLayouter;
142 ilm 40
import org.openconcerto.ui.FrameUtil;
18 ilm 41
import org.openconcerto.ui.warning.JLabelWarning;
21 ilm 42
import org.openconcerto.utils.checks.ValidState;
43
import org.openconcerto.utils.text.SimpleDocumentListener;
18 ilm 44
 
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
142 ilm 47
import java.awt.event.ActionEvent;
18 ilm 48
import java.sql.SQLException;
49
import java.util.ArrayList;
28 ilm 50
import java.util.Date;
18 ilm 51
import java.util.List;
52
 
142 ilm 53
import javax.swing.AbstractAction;
18 ilm 54
import javax.swing.BorderFactory;
55
import javax.swing.JLabel;
142 ilm 56
import javax.swing.JOptionPane;
18 ilm 57
import javax.swing.JPanel;
58
import javax.swing.JScrollPane;
59
import javax.swing.JTabbedPane;
60
import javax.swing.JTextField;
61
import javax.swing.event.DocumentEvent;
62
 
63
public class SalarieSQLElement extends ComptaSQLConfElement {
64
 
65
    public SalarieSQLElement() {
66
        super("SALARIE", "un salarié", "salariés");
142 ilm 67
 
144 ilm 68
        PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
69
        actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
70
        getRowActions().add(actionAttachment);
71
 
142 ilm 72
        PredicateRowAction actionNewContrat = new PredicateRowAction(new AbstractAction("Créer un nouveau contrat") {
73
 
74
            @Override
75
            public void actionPerformed(ActionEvent e) {
76
 
77
                final SQLRowAccessor rowSelected = IListe.get(e).getSelectedRow();
78
 
79
                JOptionPane.showMessageDialog(null,
80
                        "Attention la création d'un nouveau contrat n'est pas réversible.\nCréer tous les bulletins de salaire de l'ancien contrat avant la création du nouveau.");
81
                final SQLElement infosPayeElement = getDirectory().getElement("INFOS_SALARIE_PAYE");
82
                EditFrame f = new EditFrame(infosPayeElement, EditMode.CREATION);
83
                final SQLRow rowInfosAncien = rowSelected.asRow().getForeign("ID_INFOS_SALARIE_PAYE");
84
 
85
                SQLRowValues cloneInfos = infosPayeElement.createCopy(rowInfosAncien.getID());
86
                ((SQLRowValues) cloneInfos.getForeign("ID_CONTRAT_SALARIE")).put("DATE_DEBUT", null);
87
                f.getSQLComponent().select(cloneInfos);
88
 
89
                f.addEditPanelListener(new EditPanelListener() {
90
 
91
                    @Override
92
                    public void modified() {
93
                    }
94
 
95
                    @Override
96
                    public void inserted(int id) {
97
                        try {
98
                            rowInfosAncien.createEmptyUpdateRow().put("ID_SALARIE", rowSelected.getID()).commit();
99
                            rowSelected.createEmptyUpdateRow().put("ID_INFOS_SALARIE_PAYE", id).commit();
174 ilm 100
                            infosPayeElement.getTable().getRow(id).createEmptyUpdateRow().put("ID_SALARIE", rowSelected.getID()).commit();
142 ilm 101
                        } catch (SQLException e) {
102
                            e.printStackTrace();
103
                        }
104
                    }
105
 
106
                    @Override
107
                    public void deleted() {
108
                    }
109
 
110
                    @Override
111
                    public void cancelled() {
112
                    }
113
                });
114
                FrameUtil.show(f);
115
            }
116
        }, true);
117
        actionNewContrat.setPredicate(IListeEvent.getSingleSelectionPredicate());
118
        this.getRowActions().add(actionNewContrat);
119
 
182 ilm 120
        PredicateRowAction actionVarEdit = new PredicateRowAction(new AbstractAction("Editer les variables de période") {
121
 
122
            @Override
123
            public void actionPerformed(ActionEvent e) {
124
 
125
                final SQLRowAccessor rowSelected = IListe.get(e).getSelectedRow();
126
                final SQLElement infosPayeElement = getDirectory().getElement("VARIABLE_SALARIE");
127
                EditFrame f = new EditFrame(infosPayeElement, EditMode.MODIFICATION);
128
                final Integer rowVar = rowSelected.asRow().getForeignID("ID_VARIABLE_SALARIE");
129
                f.getSQLComponent().select(rowVar);
130
                FrameUtil.show(f);
131
            }
132
        }, true);
133
        actionVarEdit.setPredicate(IListeEvent.getSingleSelectionPredicate());
134
        this.getRowActions().add(actionVarEdit);
135
 
18 ilm 136
    }
137
 
138
    protected List<String> getListFields() {
139
        final List<String> l = new ArrayList<String>();
140
        l.add("CODE");
141
        l.add("NOM");
142
        l.add("PRENOM");
143
        l.add("ID_FICHE_PAYE");
144
        return l;
145
    }
146
 
147
    protected List<String> getComboFields() {
148
        final List<String> l = new ArrayList<String>();
149
        l.add("CODE");
150
        l.add("NOM");
151
        l.add("PRENOM");
152
        return l;
153
    }
154
 
155
    /*
156
     * (non-Javadoc)
157
     *
158
     * @see org.openconcerto.devis.SQLElement#getComponent()
159
     */
160
    public SQLComponent createComponent() {
161
        return new BaseSQLComponent(this) {
21 ilm 162
            private final JLabel warningCodeSalLabel = new JLabelWarning();
18 ilm 163
            private final JTextField textCode = new JTextField();
174 ilm 164
            ContactItemTable tableContact = new ContactItemTable(UndefinedRowValuesCache.getInstance().getDefaultRowValues(getTable().getTable("CONTACT_SALARIE")));
18 ilm 165
            private JTabbedPane tabbedPane;
166
            private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
167
 
168
            public void addViews() {
169
 
170
                // TODO LIKE EBP --> CALCUL DU BRUT A PARTIR DU NET
171
                this.warningCodeSalLabel.setText("Ce code est déjà affecté à un autre salarié!");
172
 
173
                this.setLayout(new GridBagLayout());
174
                GridBagConstraints c = new DefaultGridBagConstraints();
175
 
176
                // Titre personnel
177
                final JLabel labelTitrePersonnel = new JLabel(getLabelFor("ID_TITRE_PERSONNEL"));
178
                final ElementComboBox comboTitre = new ElementComboBox(false, 5);
179
 
180
                this.add(labelTitrePersonnel, c);
181
                c.gridx++;
182
                c.weightx = 0;
183
                c.fill = GridBagConstraints.NONE;
184
                this.add(comboTitre, c);
185
 
186
                // Nom
187
                final JLabel labelNom = new JLabel(getLabelFor("NOM"));
188
                final JTextField textNom = new JTextField();
189
                c.fill = GridBagConstraints.HORIZONTAL;
190
                c.gridx++;
191
                c.weightx = 0;
192
                this.add(labelNom, c);
193
                c.gridx++;
194
                c.weightx = 1;
195
                this.add(textNom, c);
196
 
197
                // Prénom
198
                final JLabel labelPrenom = new JLabel(getLabelFor("PRENOM"));
199
                final JTextField textPrenom = new JTextField();
200
 
201
                c.gridx++;
202
                c.weightx = 0;
203
                this.add(labelPrenom, c);
204
                c.gridx++;
205
                c.weightx = 1;
206
                this.add(textPrenom, c);
207
 
208
                // Code
209
                final JLabel labelCode = new JLabel(getLabelFor("CODE"));
210
                c.gridx++;
211
                c.weightx = 0;
212
                this.add(labelCode, c);
213
                c.gridx++;
214
                c.weightx = 1;
215
                this.add(this.textCode, c);
216
 
217
                c.weightx = 0;
218
                c.gridx++;
219
                this.add(this.warningCodeSalLabel, c);
220
                this.warningCodeSalLabel.setVisible(false);
221
 
21 ilm 222
                this.textCode.getDocument().addDocumentListener(new SimpleDocumentListener() {
223
                    @Override
224
                    public void update(DocumentEvent e) {
225
                        checkCode();
18 ilm 226
                    }
227
                });
228
 
174 ilm 229
                // User
230
                final JLabel labelUser = new JLabel(getLabelFor("ID_USER_COMMON"));
231
                final ElementComboBox comboUser = new ElementComboBox(false, 50);
232
                c.gridx = 0;
233
                c.gridy++;
234
                this.add(labelUser, c);
235
                c.gridx++;
182 ilm 236
                c.weightx = 1;
174 ilm 237
                c.fill = GridBagConstraints.NONE;
238
                this.add(comboUser, c);
239
                this.addView(comboUser, "ID_USER_COMMON");
240
 
18 ilm 241
                /***********************************************************************************
242
                 * TABBED PANE
243
                 **********************************************************************************/
244
                this.tabbedPane = new JTabbedPane();
245
 
246
                // Etat Civil
247
                this.addView("ID_ETAT_CIVIL", REQ + ";" + DEC + ";" + SEP);
248
                ElementSQLObject eltEtatCivil = (ElementSQLObject) this.getView("ID_ETAT_CIVIL");
249
                JScrollPane scrollEtatCivil = new JScrollPane(eltEtatCivil);
250
                scrollEtatCivil.setBorder(null);
251
                this.tabbedPane.add("Etat Civil", scrollEtatCivil);
252
 
174 ilm 253
                // Contact
254
                JPanel panelContact = new JPanel(new GridBagLayout());
255
                DefaultGridBagConstraints cContact = new DefaultGridBagConstraints();
256
                cContact.weighty = 1;
257
                cContact.weightx = 1;
258
                cContact.fill = GridBagConstraints.BOTH;
259
 
260
                panelContact.add(tableContact, cContact);
261
                JScrollPane scrollContact = new JScrollPane(panelContact);
262
                scrollContact.setBorder(null);
263
                this.tabbedPane.add("Contacts", scrollContact);
264
 
18 ilm 265
                // Règlement de la paye
266
                this.addView("ID_REGLEMENT_PAYE", REQ + ";" + DEC + ";" + SEP);
267
                ElementSQLObject eltReglPaye = (ElementSQLObject) this.getView("ID_REGLEMENT_PAYE");
268
                JScrollPane scrollReglPaye = new JScrollPane(eltReglPaye);
269
                scrollReglPaye.setBorder(null);
270
                this.tabbedPane.add("Règlement", scrollReglPaye);
271
 
156 ilm 272
                // Pas
273
                this.addView("ID_PAS", REQ + ";" + DEC + ";" + SEP);
274
                ElementSQLObject eltPas = (ElementSQLObject) this.getView("ID_PAS");
275
                JScrollPane scrollPas = new JScrollPane(eltPas);
276
                scrollPas.setBorder(null);
277
                this.tabbedPane.add("PAS", scrollPas);
278
 
18 ilm 279
                // Infos salarie-paye
280
                this.addView("ID_INFOS_SALARIE_PAYE", REQ + ";" + DEC + ";" + SEP);
281
                ElementSQLObject eltInfosPaye = (ElementSQLObject) this.getView("ID_INFOS_SALARIE_PAYE");
282
                JScrollPane scrollInfosPaye = new JScrollPane(eltInfosPaye);
283
                scrollInfosPaye.setBorder(null);
284
                this.tabbedPane.add("Informations salarié-paye", scrollInfosPaye);
285
 
286
                // Fiche de paye
287
                this.addView("ID_FICHE_PAYE", REQ + ";" + DEC + ";" + SEP);
288
                ElementSQLObject eltFichePaye = (ElementSQLObject) this.getView("ID_FICHE_PAYE");
289
                JScrollPane scrollFichePaye = new JScrollPane(eltFichePaye);
290
                scrollFichePaye.setBorder(null);
291
                this.tabbedPane.add("Fiche de paye en cours", scrollFichePaye);
292
                // this.tabbedPane.setEnabledAt(this.tabbedPane.getTabCount() - 1, false);
293
 
294
                // Cumuls
295
                this.addView("ID_CUMULS_CONGES", REQ + ";" + DEC + ";" + SEP);
296
                ElementSQLObject eltCumulsConges = (ElementSQLObject) this.getView("ID_CUMULS_CONGES");
297
                JPanel panelCumulsConges = new JPanel();
298
                panelCumulsConges.setBorder(BorderFactory.createTitledBorder("Cumuls congés"));
299
                panelCumulsConges.add(eltCumulsConges);
300
 
301
                this.addView("ID_CUMULS_PAYE", REQ + ";" + DEC + ";" + SEP);
302
                ElementSQLObject eltCumulPaye = (ElementSQLObject) this.getView("ID_CUMULS_PAYE");
303
                JPanel panelCumulsPaye = new JPanel();
304
                panelCumulsPaye.setBorder(BorderFactory.createTitledBorder("Cumuls paye"));
305
                panelCumulsPaye.add(eltCumulPaye);
306
 
307
                this.addView("ID_VARIABLE_SALARIE", REQ + ";" + DEC + ";" + SEP);
308
                ElementSQLObject eltVarSalarie = (ElementSQLObject) this.getView("ID_VARIABLE_SALARIE");
309
                JPanel panelVarSalarie = new JPanel();
310
                panelVarSalarie.setBorder(BorderFactory.createTitledBorder("Variables de la période"));
311
                panelVarSalarie.add(eltVarSalarie);
312
 
313
                JPanel panelAllCumul = new JPanel();
314
                GridBagConstraints cPanel = new DefaultGridBagConstraints();
315
                panelAllCumul.setLayout(new GridBagLayout());
316
                cPanel.fill = GridBagConstraints.NONE;
317
 
318
                panelAllCumul.add(panelCumulsConges, cPanel);
319
                cPanel.gridx++;
320
                cPanel.weightx = 1;
321
                panelAllCumul.add(panelCumulsPaye, cPanel);
322
                cPanel.gridwidth = GridBagConstraints.REMAINDER;
323
                cPanel.gridy++;
324
                cPanel.gridx = 0;
325
                cPanel.weighty = 1;
326
                cPanel.weightx = 1;
327
                cPanel.anchor = GridBagConstraints.NORTHWEST;
328
                panelAllCumul.add(panelVarSalarie, cPanel);
329
 
330
                this.tabbedPane.add("Cumuls et variables de la période", new JScrollPane(panelAllCumul));
331
                // this.tabbedPane.setEnabledAt(this.tabbedPane.getTabCount() - 1, false);
332
 
180 ilm 333
                if (!getElement().getAdditionalFields().isEmpty()) {
174 ilm 334
                    // Champ Module
335
                    c.gridx = 0;
336
                    c.gridy++;
337
                    c.gridwidth = GridBagConstraints.REMAINDER;
338
                    final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
339
                    this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
340
                    this.tabbedPane.add("Compléments", new JScrollPane(addP));
341
                }
342
 
18 ilm 343
                c.gridy++;
344
                c.gridx = 0;
345
                c.weighty = 1;
346
                c.gridwidth = GridBagConstraints.REMAINDER;
347
                c.fill = GridBagConstraints.BOTH;
348
                c.weighty = 1;
349
                c.weightx = 1;
350
                c.anchor = GridBagConstraints.NORTHWEST;
351
                this.add(this.tabbedPane, c);
352
 
353
                this.addRequiredSQLObject(textNom, "NOM");
354
                this.addRequiredSQLObject(textPrenom, "PRENOM");
355
                this.addRequiredSQLObject(this.textCode, "CODE");
356
                this.addRequiredSQLObject(comboTitre, "ID_TITRE_PERSONNEL");
357
                comboTitre.setButtonsVisible(false);
358
                DefaultGridBagConstraints.lockMinimumSize(comboTitre);
359
 
28 ilm 360
                this.textCode.setText(NumerotationAutoSQLElement.getNextNumero(SalarieSQLElement.class, new Date()));
18 ilm 361
            }
362
 
21 ilm 363
            @Override
364
            public synchronized ValidState getValidState() {
365
                return super.getValidState().and(ValidState.createCached(!this.warningCodeSalLabel.isVisible(), this.warningCodeSalLabel.getText()));
18 ilm 366
            }
367
 
21 ilm 368
            private void checkCode() {
18 ilm 369
                SQLSelect selNum = new SQLSelect(getTable().getBase());
21 ilm 370
                selNum.addSelectFunctionStar("count");
18 ilm 371
                selNum.setWhere(new Where(getTable().getField("CODE"), "=", this.textCode.getText().trim()));
372
                selNum.andWhere(new Where(getTable().getField("ID"), "!=", getSelectedID()));
373
 
21 ilm 374
                final Number count = (Number) getTable().getBase().getDataSource().executeScalar(selNum.asString());
375
                final boolean isValid = count.intValue() == 0;
376
                final boolean currentValid = !this.warningCodeSalLabel.isVisible();
377
                if (currentValid != isValid) {
378
                    this.warningCodeSalLabel.setVisible(!isValid);
379
                    this.fireValidChange();
380
                }
18 ilm 381
            }
382
 
383
            @Override
384
            public void select(SQLRowAccessor r) {
385
                super.select(r);
386
                /*
387
                 * if (r.getID() > 1) { this.tabbedPane.setEnabledAt(this.tabbedPane.getTabCount() -
388
                 * 1, true); this.tabbedPane.setEnabledAt(this.tabbedPane.getTabCount() - 2, true);
389
                 * }
390
                 */
21 ilm 391
                checkCode();
174 ilm 392
                if (r != null) {
393
                    this.tableContact.insertFrom("ID_SALARIE", r.asRowValues());
394
                }
18 ilm 395
            }
396
 
397
            public void update() {
398
                super.update();
399
 
400
                SQLTable tableFichePaye = getTable().getBase().getTable("FICHE_PAYE");
401
                SQLRowValues rowVals = new SQLRowValues(tableFichePaye);
174 ilm 402
                rowVals.put("ID_SALARIE",
403
 
404
                        getSelectedID());
18 ilm 405
                SQLRow row = getTable().getRow(getSelectedID());
406
                try {
407
                    rowVals.update(row.getInt("ID_FICHE_PAYE"));
408
                } catch (SQLException e) {
409
                    e.printStackTrace();
410
                }
174 ilm 411
                this.tableContact.updateField("ID_SALARIE", getSelectedID());
18 ilm 412
            }
413
 
414
            public int insert(SQLRow order) {
415
                int id = super.insert(order);
416
                SQLTable tableFichePaye = getTable().getBase().getTable("FICHE_PAYE");
417
                SQLTable tableInfosPaye = getTable().getBase().getTable("INFOS_SALARIE_PAYE");
418
                SQLRow row = getTable().getRow(id);
419
                SQLRow rowInfosPaye = tableInfosPaye.getRow(row.getInt("ID_INFOS_SALARIE_PAYE"));
142 ilm 420
                try {
421
                    rowInfosPaye.createEmptyUpdateRow().put("ID_SALARIE", id).commit();
422
                } catch (SQLException e1) {
423
                    e1.printStackTrace();
424
                }
18 ilm 425
 
426
                SQLRowValues rowVals = new SQLRowValues(tableFichePaye);
427
                rowVals.put("ID_SALARIE", id);
428
                rowVals.put("CONGES_ACQUIS", rowInfosPaye.getObject("CONGES_PAYES"));
429
                try {
430
                    rowVals.update(row.getInt("ID_FICHE_PAYE"));
431
                } catch (SQLException e) {
432
                    e.printStackTrace();
433
                }
434
 
435
                // incrémentation du numéro auto
28 ilm 436
                if (NumerotationAutoSQLElement.getNextNumero(SalarieSQLElement.class, new Date()).equalsIgnoreCase(this.textCode.getText().trim())) {
18 ilm 437
                    SQLRowValues rowValsNum = new SQLRowValues(this.tableNum);
438
                    int val = this.tableNum.getRow(2).getInt("SALARIE_START");
439
                    val++;
440
                    rowValsNum.put("SALARIE_START", new Integer(val));
441
 
442
                    try {
443
                        rowValsNum.update(2);
444
                    } catch (SQLException e) {
445
 
446
                        e.printStackTrace();
447
                    }
448
                }
174 ilm 449
                this.tableContact.updateField("ID_SALARIE", id);
18 ilm 450
                return id;
451
            }
174 ilm 452
 
18 ilm 453
        };
454
    }
57 ilm 455
 
456
    @Override
457
    protected String createCode() {
156 ilm 458
        return createCodeOfPackage() + ".employe";
57 ilm 459
    }
18 ilm 460
}