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