OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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