OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | 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.ui;
15
 
16
import org.openconcerto.erp.model.EditionFichePayeModel;
17
import org.openconcerto.erp.preferences.DefaultNXProps;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.element.SQLElement;
20
import org.openconcerto.sql.sqlobject.ElementComboBox;
21
import org.openconcerto.sql.view.EditFrame;
22
import org.openconcerto.sql.view.EditPanel;
23
import org.openconcerto.ui.JDate;
24
import org.openconcerto.ui.preferences.DefaultProps;
25
import org.openconcerto.utils.ExceptionHandler;
149 ilm 26
import org.openconcerto.utils.TableSorter;
18 ilm 27
 
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.Insets;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.awt.event.MouseEvent;
34
import java.awt.event.MouseListener;
35
import java.beans.PropertyChangeEvent;
36
import java.beans.PropertyChangeListener;
149 ilm 37
import java.util.ArrayList;
18 ilm 38
import java.util.Calendar;
94 ilm 39
import java.util.Date;
149 ilm 40
import java.util.List;
18 ilm 41
 
42
import javax.swing.AbstractAction;
43
import javax.swing.BorderFactory;
44
import javax.swing.JButton;
45
import javax.swing.JFrame;
46
import javax.swing.JLabel;
94 ilm 47
import javax.swing.JOptionPane;
18 ilm 48
import javax.swing.JPanel;
49
import javax.swing.JPopupMenu;
50
import javax.swing.JProgressBar;
51
import javax.swing.JScrollPane;
52
import javax.swing.JSpinner;
53
import javax.swing.JTable;
54
import javax.swing.SpinnerModel;
55
import javax.swing.SpinnerNumberModel;
56
import javax.swing.SwingConstants;
57
import javax.swing.SwingUtilities;
58
import javax.swing.event.ChangeEvent;
59
import javax.swing.event.ChangeListener;
60
 
61
public class EditionFichePayePanel extends JPanel implements MouseListener {
62
 
63
    private final DefaultProps props = DefaultNXProps.getInstance();
64
    private final JProgressBar bar = new JProgressBar();
65
    private final JLabel labelEtatEdition = new JLabel();
66
    private final EditionFichePayeModel model = new EditionFichePayeModel(bar, labelEtatEdition);
67
    private final JTable table;
68
    private EditFrame frameModifySal = null;
69
 
70
    public EditionFichePayePanel() {
71
        super(new GridBagLayout());
72
 
73
        GridBagConstraints c = new GridBagConstraints();
74
        c.gridx = 0;
75
        c.gridy = 0;
76
        c.gridwidth = 1;
77
        c.gridheight = 1;
78
        c.weightx = 0;
79
        c.weighty = 0;
80
        c.anchor = GridBagConstraints.WEST;
81
        c.fill = GridBagConstraints.HORIZONTAL;
82
        c.insets = new Insets(2, 2, 1, 2);
83
 
84
        // Période de travail
85
        JPanel panelPeriode = new JPanel();
86
        panelPeriode.setBorder(BorderFactory.createTitledBorder("Période de travail"));
87
 
88
        SQLElement eltMois = Configuration.getInstance().getDirectory().getElement("MOIS");
89
        JLabel labelMois = new JLabel("Mois");
90
        final ElementComboBox selMois = new ElementComboBox(false, 25);
91
        selMois.init(eltMois);
92
        selMois.setButtonsVisible(false);
93
 
94
        // on remet les anciennes valeurs
95
        int valMois = props.getIntProperty("MoisEditionPaye");
96
        if (valMois > 1) {
97
            selMois.setValue(valMois);
98
        }
99
 
100
        JLabel labelAnnee = new JLabel("Année");
101
        // final JTextField textAnnee = new JTextField(4);
102
        Calendar cal = Calendar.getInstance();
103
        SpinnerModel modelSpinner = new SpinnerNumberModel(cal.get(Calendar.YEAR), // initial value
104
                cal.get(Calendar.YEAR) - 100, // min
105
                cal.get(Calendar.YEAR) + 100, // max
106
                1);
107
        final JSpinner textAnnee = new JSpinner(modelSpinner);
108
 
109
        String valYear = props.getStringProperty("AnneeEditionPaye");
110
        if (valYear != null && valYear.trim().length() > 0) {
111
            textAnnee.setValue(Integer.valueOf(valYear));
112
        }
113
        panelPeriode.add(labelMois);
114
        panelPeriode.add(selMois);
115
        panelPeriode.add(labelAnnee);
116
        panelPeriode.add(textAnnee);
117
 
118
        JLabel periodeDeb = new JLabel("Correspondant à la période du");
119
        JLabel periodeFin = new JLabel("au");
120
        final JDate dateDeb = new JDate();
121
        final JDate dateFin = new JDate();
122
 
123
        panelPeriode.add(periodeDeb);
124
        panelPeriode.add(dateDeb);
125
        panelPeriode.add(periodeFin);
126
        panelPeriode.add(dateFin);
127
 
128
        c.gridwidth = GridBagConstraints.REMAINDER;
129
        c.weightx = 1;
130
        this.add(panelPeriode, c);
131
 
132
        // Liste des salariés
133
        c.gridy++;
149 ilm 134
        TableSorter s = new TableSorter(this.model);
135
        this.table = new JTable(s);
136
        s.setTableHeader(this.table.getTableHeader());
137
        // this.table = new JTable(this.model);
18 ilm 138
        this.table.addMouseListener(this);
139
        c.fill = GridBagConstraints.BOTH;
140
        c.weighty = 1;
141
        this.add(new JScrollPane(this.table), c);
142
 
143
        // Label Etat
144
        c.gridx = 0;
145
        c.gridy++;
146
        c.gridwidth = GridBagConstraints.REMAINDER;
147
        c.fill = GridBagConstraints.HORIZONTAL;
148
        c.weighty = 0;
149
        c.weightx = 1;
150
        this.labelEtatEdition.setHorizontalAlignment(SwingConstants.CENTER);
151
        this.add(this.labelEtatEdition, c);
152
 
153
        // Progress Bar
154
        c.gridwidth = GridBagConstraints.REMAINDER;
155
        c.fill = GridBagConstraints.HORIZONTAL;
156
        c.gridy++;
157
        c.weighty = 0;
158
        this.add(this.bar, c);
159
 
160
        // Button
83 ilm 161
        final JButton buttonValid = new JButton("Valider");
162
        final JButton buttonFermer = new JButton("Fermer");
18 ilm 163
 
83 ilm 164
        PropertyChangeListener dateListener = new PropertyChangeListener() {
165
 
166
            @Override
167
            public void propertyChange(PropertyChangeEvent evt) {
174 ilm 168
                buttonValid.setEnabled(dateDeb.getValue() != null && dateFin.getValue() != null && dateDeb.getValue().compareTo(dateFin.getValue()) <= 0);
149 ilm 169
                model.setDateLimit(dateDeb.getValue());
83 ilm 170
            }
171
        };
172
        dateDeb.addValueListener(dateListener);
173
        dateFin.addValueListener(dateListener);
174
 
18 ilm 175
        c.gridy++;
176
        c.gridwidth = 1;
177
        c.anchor = GridBagConstraints.EAST;
178
        c.weightx = 1;
179
        c.fill = GridBagConstraints.NONE;
180
        this.add(buttonValid, c);
181
        c.weightx = 0;
182
        c.gridx++;
183
        this.add(buttonFermer, c);
184
 
185
        buttonFermer.addActionListener(new ActionListener() {
186
            public void actionPerformed(ActionEvent e) {
187
                ((JFrame) SwingUtilities.getRoot(EditionFichePayePanel.this)).dispose();
188
            }
189
        });
190
 
191
        buttonValid.addActionListener(new ActionListener() {
192
            public void actionPerformed(ActionEvent e) {
193
                try {
194
                    // Sauvegarde des valeurs
195
                    props.setProperty("MoisEditionPaye", String.valueOf(selMois.getSelectedId()));
196
                    // props.setProperty("AnneeEditionPaye", textAnnee.getText());
197
                    props.store();
198
 
94 ilm 199
                    final Date dateStart = dateDeb.getDate();
18 ilm 200
 
94 ilm 201
                    if (dateStart == null) {
202
                        JOptionPane.showMessageDialog(EditionFichePayePanel.this, "Date de début manquante.");
203
                        return;
204
                    }
205
                    java.sql.Date du = new java.sql.Date(dateStart.getTime());
206
                    final Date dateEnd = dateFin.getDate();
207
                    if (dateEnd == null) {
208
                        JOptionPane.showMessageDialog(EditionFichePayePanel.this, "Date de fin manquante.");
209
                        return;
210
                    }
211
                    java.sql.Date au = new java.sql.Date(dateEnd.getTime());
212
 
18 ilm 213
                    // model.validationFiche(textAnnee.getText(), selMois.getSelectedId(), du, au);
214
                    model.validationFiche(textAnnee.getValue().toString(), selMois.getSelectedId(), du, au);
215
                } catch (Exception ex) {
216
                    ExceptionHandler.handle("Impossible de valider la paye", ex);
217
                }
218
            }
219
        });
220
 
221
        selMois.addValueListener(new PropertyChangeListener() {
222
 
223
            public void propertyChange(PropertyChangeEvent evt) {
224
                if (selMois.getSelectedId() > 1 && textAnnee.getValue() != null) {
225
                    Calendar cal = Calendar.getInstance();
226
                    cal.set(Calendar.DAY_OF_MONTH, 1);
227
                    cal.set(Calendar.MONTH, selMois.getSelectedId() - 2);
228
                    cal.set(Calendar.YEAR, Integer.valueOf(textAnnee.getValue().toString()).intValue());
229
                    dateDeb.setValue(cal.getTime());
230
                    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
231
                    dateFin.setValue(cal.getTime());
232
                }
233
            }
234
        });
235
 
236
        textAnnee.addChangeListener(new ChangeListener() {
237
            public void stateChanged(ChangeEvent e) {
238
 
239
                Calendar cal = Calendar.getInstance();
240
                cal.set(Calendar.DAY_OF_MONTH, 1);
241
                cal.set(Calendar.MONTH, selMois.getSelectedId() - 2);
242
                cal.set(Calendar.YEAR, Integer.valueOf(textAnnee.getValue().toString()).intValue());
243
                dateDeb.setValue(cal.getTime());
244
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
245
                dateFin.setValue(cal.getTime());
246
            }
247
        });
248
 
249
    }
250
 
251
    public void mouseClicked(MouseEvent e) {
252
    }
253
 
254
    public void mouseEntered(MouseEvent e) {
255
    }
256
 
257
    public void mouseExited(MouseEvent e) {
258
    }
259
 
260
    public void mousePressed(final MouseEvent e) {
261
 
262
        int row = table.rowAtPoint(e.getPoint());
263
        final int idSal = model.getIdSalAtRow(row);
264
 
265
        if (e.getButton() == MouseEvent.BUTTON3) {
266
            JPopupMenu menuDroit = new JPopupMenu();
267
            menuDroit.add(new AbstractAction("Modifier") {
268
                public void actionPerformed(ActionEvent e) {
269
                    if (frameModifySal == null) {
270
                        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SALARIE");
271
                        frameModifySal = new EditFrame(elt, EditPanel.MODIFICATION);
272
                    }
273
 
274
                    frameModifySal.selectionId(idSal, 1);
275
                    frameModifySal.pack();
276
                    frameModifySal.setVisible(true);
277
                }
278
            });
149 ilm 279
 
280
            menuDroit.add(new AbstractAction("Marquer tout \"A créer\"") {
281
 
282
                public void actionPerformed(ActionEvent e) {
283
                    int rows = table.getModel().getRowCount();
284
                    for (int i = 0; i < rows; i++) {
285
                        table.getModel().setValueAt(Boolean.TRUE, i, 0);
286
                    }
287
                    model.fireTableDataChanged();
288
                }
289
            });
290
 
291
            menuDroit.add(new AbstractAction("Désélectionner tout \"A créer\"") {
292
 
293
                public void actionPerformed(ActionEvent e) {
294
                    int rows = table.getModel().getRowCount();
295
                    for (int i = 0; i < rows; i++) {
296
                        table.getModel().setValueAt(Boolean.FALSE, i, 0);
297
                    }
298
                    model.fireTableDataChanged();
299
                }
300
            });
301
 
302
            menuDroit.add(new AbstractAction("Marquer la sélection \"A créer\"") {
303
 
304
                public void actionPerformed(ActionEvent e) {
305
                    int[] selectedRows = table.getSelectedRows();
306
 
307
                    List<Integer> l = new ArrayList<>(selectedRows.length);
308
                    for (int i : selectedRows) {
309
                        l.add(i);
310
                    }
311
                    int rows = table.getModel().getRowCount();
312
                    for (int i = 0; i < rows; i++) {
313
                        table.setValueAt(l.contains(i), i, 0);
314
                    }
315
                    model.fireTableDataChanged();
316
                }
317
            });
318
 
18 ilm 319
            menuDroit.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
320
        }
321
    }
322
 
323
    public void mouseReleased(MouseEvent e) {
324
    }
325
}