OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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