OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 149 | 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.model;
15
 
16
import org.openconcerto.erp.core.humanresources.payroll.element.FichePayeSQLElement;
91 ilm 17
import org.openconcerto.erp.core.humanresources.payroll.report.FichePayeSheetXML;
18 ilm 18
import org.openconcerto.erp.core.humanresources.payroll.ui.VisualisationPayeFrame;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.SQLTableListener;
91 ilm 26
import org.openconcerto.sql.model.Where;
18 ilm 27
import org.openconcerto.utils.ExceptionHandler;
28
 
29
import java.sql.Date;
30
import java.sql.SQLException;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Vector;
35
import java.util.concurrent.Semaphore;
36
 
37
import javax.swing.JLabel;
38
import javax.swing.JOptionPane;
39
import javax.swing.JProgressBar;
40
import javax.swing.SwingUtilities;
41
import javax.swing.table.AbstractTableModel;
42
 
43
import org.apache.commons.dbutils.handlers.ArrayListHandler;
44
 
45
public class EditionFichePayeModel extends AbstractTableModel {
46
 
47
    private Vector<Map<String, Object>> vData = new Vector<Map<String, Object>>();
48
    private String[] columnsName = { "A créer", "Salarié", "Brut", "Net", "Visualiser", "Imprimer" };
49
    private Map<Integer, String> mapColumn = new HashMap<Integer, String>();
50
    private JProgressBar bar;
51
    JLabel labelEtat;
52
 
53
    public EditionFichePayeModel(JProgressBar bar, JLabel labelEtat) {
54
 
55
        this.bar = bar;
56
        this.labelEtat = labelEtat;
57
        this.mapColumn.put(Integer.valueOf(0), "A_CREER");
58
        this.mapColumn.put(Integer.valueOf(1), "NOM");
59
        this.mapColumn.put(Integer.valueOf(2), "BRUT");
60
        this.mapColumn.put(Integer.valueOf(3), "NET");
61
        this.mapColumn.put(Integer.valueOf(4), "VISU");
62
        this.mapColumn.put(Integer.valueOf(5), "IMPRESSION");
63
 
64
        fill();
65
        updateAll();
66
 
67
        // FIXME Update
68
        SQLElement eltSal = Configuration.getInstance().getDirectory().getElement("SALARIE");
69
        eltSal.getTable().addTableListener(new SQLTableListener() {
70
 
71
            public void rowAdded(SQLTable table, int id) {
72
                updateAll();
73
            }
74
 
75
            public void rowDeleted(SQLTable table, int id) {
76
                updateAll();
77
            }
78
 
79
            public void rowModified(SQLTable table, int id) {
80
                updateAll();
81
            }
82
        });
83
    }
84
 
85
    public int getColumnCount() {
86
        return this.columnsName.length;
87
    }
88
 
89
    public int getRowCount() {
90
        return this.vData.size();
91
    }
92
 
93
    public Object getValueAt(int rowIndex, int columnIndex) {
94
 
95
        String s = this.mapColumn.get(Integer.valueOf(columnIndex)).toString();
96
        Map<String, Object> m = this.vData.get(rowIndex);
97
        return m.get(s);
98
    }
99
 
100
    @Override
101
    public String getColumnName(int column) {
102
 
103
        return this.columnsName[column];
104
    }
105
 
106
    @Override
107
    public Class<?> getColumnClass(int columnIndex) {
108
 
109
        String s = this.mapColumn.get(columnIndex).toString();
110
        if (s.equalsIgnoreCase("A_CREER") || s.equalsIgnoreCase("IMPRESSION") || s.equalsIgnoreCase("VISU")) {
111
            return Boolean.class;
112
        } else {
113
            if (s.equalsIgnoreCase("NOM")) {
114
                return String.class;
115
            } else {
116
                return Float.class;
117
            }
118
 
119
        }
120
    }
121
 
122
    @Override
123
    public boolean isCellEditable(int rowIndex, int columnIndex) {
124
 
125
        String s = this.mapColumn.get(columnIndex).toString();
91 ilm 126
        if (s.equalsIgnoreCase("A_CREER") || s.equalsIgnoreCase("IMPRESSION")) {
18 ilm 127
            return true;
128
        }
129
        return false;
130
 
131
    }
132
 
133
    @Override
134
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
135
        Map<String, Object> m = this.vData.get(rowIndex);
136
        String s = this.mapColumn.get(columnIndex).toString();
137
        Object o = m.get(s);
138
        if (o instanceof Boolean) {
139
            m.put(s, aValue);
140
        }
141
    }
142
 
143
    /**
144
     * Charge les données du model
145
     *
146
     */
147
    private void fill() {
148
 
149
        // on recupere la lsite des salaries
150
        SQLElement eltSal = Configuration.getInstance().getDirectory().getElement("SALARIE");
151
        SQLSelect sel = new SQLSelect(eltSal.getTable().getBase());
152
        sel.addSelect(eltSal.getTable().getField("ID"));
91 ilm 153
        final SQLTable tableInfos = eltSal.getTable().getTable("INFOS_SALARIE_PAYE");
154
        Where w = new Where(tableInfos.getKey(), "=", eltSal.getTable().getField("ID_INFOS_SALARIE_PAYE"));
155
        Where w2 = new Where(tableInfos.getField("DATE_SORTIE"), "=", (Object) null);
156
        w2 = w2.or(new Where(tableInfos.getField("DATE_SORTIE"), ">", new java.util.Date()));
157
        sel.setWhere(w.and(w2));
18 ilm 158
        this.vData.removeAllElements();
159
        List l = (List) eltSal.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
160
        if (l != null) {
161
            for (int i = 0; i < l.size(); i++) {
162
                int idSal = ((Number) ((Object[]) l.get(i))[0]).intValue();
163
 
164
                Map<String, Object> m = new HashMap<String, Object>();
165
                m.put("A_CREER", Boolean.TRUE);
166
                m.put("NOM", new Integer(i));
167
                m.put("BRUT", new Integer(i));
168
                m.put("NET", new Integer(i));
91 ilm 169
                m.put("VISU", Boolean.TRUE);
170
                m.put("IMPRESSION", Boolean.FALSE);
18 ilm 171
                m.put("ID_SALARIE", new Integer(idSal));
172
 
173
                this.vData.add(m);
174
            }
175
        }
176
    }
177
 
178
    /**
179
     * Mise à jour du brut et du net pour chacun des salariés
180
     *
181
     */
182
    private void updateAll() {
183
 
184
        SQLElement eltSal = Configuration.getInstance().getDirectory().getElement("SALARIE");
185
        SQLElement eltFichePaye = Configuration.getInstance().getDirectory().getElement("FICHE_PAYE");
186
 
187
        for (int i = 0; i < this.vData.size(); i++) {
188
            Map<String, Object> m = this.vData.get(i);
189
            Integer idSal = Integer.parseInt(m.get("ID_SALARIE").toString());
190
 
191
            SQLRow rowSal = eltSal.getTable().getRow(idSal.intValue());
192
            int idFiche = rowSal.getInt("ID_FICHE_PAYE");
193
            SQLRow rowFiche = eltFichePaye.getTable().getRow(idFiche);
194
            m.put("BRUT", Float.valueOf(rowFiche.getFloat("SAL_BRUT")));
195
            m.put("NET", Float.valueOf(rowFiche.getFloat("NET_A_PAYER")));
196
 
197
            String nom = rowSal.getString("CODE") + " " + rowSal.getString("NOM") + " " + rowSal.getString("PRENOM");
198
            m.put("NOM", nom);
199
        }
200
 
201
        SwingUtilities.invokeLater(new Runnable() {
202
            public void run() {
203
                fireTableDataChanged();
204
                System.err.println("Update all");
205
            }
206
        });
207
 
208
    }
209
 
210
    public int getIdSalAtRow(int row) {
211
        Map<String, Object> m = this.vData.get(row);
212
        return ((Number) m.get("ID_SALARIE")).intValue();
213
    }
214
 
215
    /**
216
     * Validation des fiches selectionnées
217
     *
218
     * @param annee
219
     * @param idMois
220
     * @param du
221
     * @param au
222
     */
223
    public void validationFiche(final String annee, final int idMois, final Date du, final Date au) {
224
 
225
        final Thread t = new Thread() {
226
            @Override
227
            public void run() {
228
                try {
229
                    EditionFichePayeModel.this.bar.setMaximum(EditionFichePayeModel.this.vData.size() * 4 - 1);
230
                    EditionFichePayeModel.this.bar.setString(null);
231
                    EditionFichePayeModel.this.bar.setStringPainted(false);
232
                    int tmp = 0;
233
                    EditionFichePayeModel.this.bar.setValue(tmp);
234
 
235
                    final SQLElement eltSal = Configuration.getInstance().getDirectory().getElement("SALARIE");
236
                    final SQLElement eltFichePaye = Configuration.getInstance().getDirectory().getElement("FICHE_PAYE");
237
 
238
                    // On crée la fiche de paye pour chacun des salariés sélectionnés
239
                    for (int i = 0; i < EditionFichePayeModel.this.vData.size(); i++) {
240
                        Map<String, Object> m = EditionFichePayeModel.this.vData.get(i);
241
                        Boolean bCreate = (Boolean) m.get("A_CREER");
242
 
243
                        if (bCreate.booleanValue()) {
244
                            final int idSal = ((Number) m.get("ID_SALARIE")).intValue();
245
                            SQLRow rowSalarie = eltSal.getTable().getRow(idSal);
246
                            final String salName = rowSalarie.getString("CODE") + " " + rowSalarie.getString("NOM");
247
                            final SQLRow row = eltSal.getTable().getRow(idSal);
248
                            final int idFiche = row.getInt("ID_FICHE_PAYE");
249
 
250
                            // Update de la periode
251
                            SQLRowValues rowVals = new SQLRowValues(eltFichePaye.getTable());
252
                            rowVals.put("ANNEE", Integer.valueOf(annee));
253
                            rowVals.put("ID_MOIS", idMois);
254
                            rowVals.put("DU", du);
255
                            rowVals.put("AU", au);
256
                            try {
257
                                rowVals.update(idFiche);
258
                            } catch (SQLException e) {
259
                                e.printStackTrace();
260
                            }
261
                            SwingUtilities.invokeLater(new Runnable() {
262
                                public void run() {
263
                                    EditionFichePayeModel.this.labelEtat.setText(salName + " - Mise à jour de la période");
264
                                }
265
                            });
266
                            EditionFichePayeModel.this.bar.setValue(tmp++);
267
 
268
                            // Visualisation
269
                            Boolean bVisu = (Boolean) m.get("VISU");
270
                            boolean resume = true;
271
                            if (bVisu.booleanValue()) {
272
 
273
                                final Semaphore semaphore = new Semaphore(1);
274
                                try {
275
                                    semaphore.acquire();
276
                                    // on demande le sémaphore
277
 
278
                                    final VisualisationPayeFrame frame = new VisualisationPayeFrame(semaphore);
279
                                    SwingUtilities.invokeLater(new Runnable() {
280
                                        public void run() {
281
 
282
                                            frame.pack();
283
                                            frame.setSelectedFichePaye(idFiche);
284
                                            frame.setVisible(true);
285
                                        }
286
                                    });
287
 
288
                                    // synchronized (this) {
289
                                    // try {
290
                                    // System.err.println("Wait ");
291
                                    // this.wait();
292
                                    // System.err.println("WakeUp");
293
                                    //
294
                                    // } catch (InterruptedException e) {
295
                                    // e.printStackTrace();
296
                                    // }
297
                                    // }
298
                                    semaphore.acquire();
299
                                    System.err.println("Etat --> " + frame.getAnswer());
300
                                    resume = frame.getAnswer();
301
                                } catch (InterruptedException e1) {
302
                                    // TODO Auto-generated catch block
303
                                    e1.printStackTrace();
304
                                }
305
                            }
306
 
307
                            EditionFichePayeModel.this.bar.setValue(tmp++);
308
 
309
                            // test si l'utilisateur n'a pas annulé l'action
310
                            if (resume) {
311
                                SwingUtilities.invokeLater(new Runnable() {
312
                                    public void run() {
313
                                        EditionFichePayeModel.this.labelEtat.setText(salName + " - Validation de la fiche");
314
                                    }
315
                                });
316
                                // Validation de la fiche
317
                                FichePayeSQLElement.validationFiche(idFiche);
318
 
319
                                // Update des rubriques
320
                                SQLRow rowSalNew = eltSal.getTable().getRow(idSal);
321
                                final int idFicheNew = rowSalNew.getInt("ID_FICHE_PAYE");
322
                                FichePayeModel ficheModel = new FichePayeModel(idFicheNew);
323
                                ficheModel.loadAllElements();
324
 
325
                                EditionFichePayeModel.this.bar.setValue(tmp++);
326
 
327
                                // Impression
144 ilm 328
                                try {
329
                                    SQLRow rowFiche = eltFichePaye.getTable().getRow(idFiche);
330
                                    FichePayeSheetXML sheet = new FichePayeSheetXML(rowFiche);
331
                                    sheet.createDocumentAsynchronous();
332
                                    Boolean bPrint = (Boolean) m.get("IMPRESSION");
333
                                    sheet.showPrintAndExportAsynchronous(false, bPrint.booleanValue(), true);
334
                                } catch (Exception e) {
335
                                    ExceptionHandler.handle("Erreur lors de la création du document!", e);
336
                                }
18 ilm 337
                                EditionFichePayeModel.this.bar.setValue(tmp++);
338
 
339
                            } else {
340
 
341
                                SwingUtilities.invokeLater(new Runnable() {
342
                                    public void run() {
343
                                        EditionFichePayeModel.this.labelEtat.setText(salName + " - Création annulée");
344
                                    }
345
                                });
346
                                tmp += 2;
347
                                EditionFichePayeModel.this.bar.setValue(tmp);
348
 
349
                                SwingUtilities.invokeLater(new Runnable() {
350
                                    public void run() {
351
                                        String msg = "Création annulée pour " + row.getString("CODE") + " " + row.getString("NOM") + " " + row.getString("PRENOM");
352
                                        JOptionPane.showMessageDialog(null, msg, "Création des payes", JOptionPane.INFORMATION_MESSAGE);
353
                                    }
354
                                });
355
                            }
356
                        } else {
357
 
358
                            tmp += 4;
359
                            EditionFichePayeModel.this.bar.setValue(tmp);
360
 
361
                        }
362
                    }
363
                } catch (Exception e) {
364
                    ExceptionHandler.handle("Erreur pendant la création des fiches de paye", e);
365
                }
366
                // Fin de l'edition
367
                SwingUtilities.invokeLater(new Runnable() {
368
                    public void run() {
369
                        updateAll();
370
                        JOptionPane.showMessageDialog(null, "Création des payes terminée", "Création paye", JOptionPane.INFORMATION_MESSAGE);
371
                    }
372
                });
373
                EditionFichePayeModel.this.labelEtat.setText("Traitement terminé");
374
                EditionFichePayeModel.this.bar.setString("Terminé");
375
                EditionFichePayeModel.this.bar.setStringPainted(true);
376
            }
377
        };
378
        t.start();
379
    }
380
}