OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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