OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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