OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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