OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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.core.finance.accounting.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
180 ilm 17
import org.openconcerto.erp.core.common.ui.DeviseField;
156 ilm 18
import org.openconcerto.erp.core.finance.accounting.ui.PointagePanel;
18 ilm 19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.model.SQLBase;
21
import org.openconcerto.sql.model.SQLRow;
156 ilm 22
import org.openconcerto.sql.model.SQLRowValues;
18 ilm 23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.Where;
156 ilm 26
import org.openconcerto.sql.view.list.IListe;
27
import org.openconcerto.sql.view.list.ITableModel;
18 ilm 28
 
156 ilm 29
import java.util.Date;
18 ilm 30
import java.util.List;
31
 
32
import javax.swing.SwingWorker;
33
import javax.swing.table.AbstractTableModel;
34
 
35
import org.apache.commons.dbutils.handlers.ArrayListHandler;
36
 
37
public class PointageModel extends AbstractTableModel {
38
 
39
    private String[] titresCol;
40
    private String[] titresRow;
156 ilm 41
    private PointagePanel panel;
42
    private long debitListe, creditListe, debitTotal, creditTotal, debitPointe, creditPointe, debitNonPointe, creditNonPointe, creditSelection, debitSelection;
18 ilm 43
 
44
    private static final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
45
    private static final SQLTable tableEcr = base.getTable("ECRITURE");
46
    int idCpt;
47
 
156 ilm 48
    public PointageModel(int idCpt, PointagePanel panel) {
18 ilm 49
        this.creditNonPointe = 0;
156 ilm 50
        this.creditTotal = 0;
18 ilm 51
        this.creditPointe = 0;
52
        this.creditSelection = 0;
53
 
156 ilm 54
        this.panel = panel;
55
 
18 ilm 56
        this.debitNonPointe = 0;
57
        this.debitPointe = 0;
156 ilm 58
        this.debitTotal = 0;
18 ilm 59
        this.debitSelection = 0;
60
 
61
        this.idCpt = idCpt;
62
        this.titresCol = new String[6];
156 ilm 63
        this.titresCol[0] = "Totaux période";
18 ilm 64
        this.titresCol[1] = "Pointé";
156 ilm 65
        this.titresCol[2] = "Relevé courant";
18 ilm 66
        this.titresCol[3] = "Total";
67
 
68
        this.titresCol[4] = "Sélection";
156 ilm 69
        this.titresCol[5] = "Liste";
18 ilm 70
 
71
        this.titresRow = new String[3];
72
        this.titresRow[0] = "Débit";
73
        this.titresRow[1] = "Crédit";
74
        this.titresRow[2] = "Solde";
75
 
76
        updateTotauxCompte();
77
    }
78
 
79
    public void setIdCompte(int id) {
80
        this.idCpt = id;
81
        updateTotauxCompte();
82
        updateSelection(null);
83
    }
84
 
85
    public void updateSelection(int[] rowIndex) {
86
        this.creditSelection = 0;
87
        this.debitSelection = 0;
88
 
89
        if (rowIndex != null) {
90
            for (int i = 0; i < rowIndex.length; i++) {
91
 
92
                SQLRow row = tableEcr.getRow(rowIndex[i]);
93
 
94
                if (row != null) {
95
 
80 ilm 96
                    // if (row.getString("POINTEE").trim().length() == 0) {
97
                    this.debitSelection += ((Long) row.getObject("DEBIT")).longValue();
98
                    this.creditSelection += ((Long) row.getObject("CREDIT")).longValue();
99
                    // }
100
                    /*
101
                     * else {
102
                     *
103
                     * this.debitSelection -= row.getFloat("DEBIT"); this.creditSelection -=
104
                     * row.getFloat("CREDIT"); }
105
                     */
18 ilm 106
                }
107
            }
108
        }
109
        this.fireTableDataChanged();
110
    }
111
 
156 ilm 112
    private SwingWorker<String, Object> workerUpdater;
113
 
18 ilm 114
    public void updateTotauxCompte() {
180 ilm 115
        updateTotauxCompte(null, null, null);
116
    }
117
 
118
    public void updateTotauxCompte(final DeviseField depart, final DeviseField arrive, final DeviseField ecart) {
119
 
156 ilm 120
        final Date deb = panel.getDateDeb();
121
        final Date fin = panel.getDateFin();
18 ilm 122
 
156 ilm 123
        if (workerUpdater != null) {
124
            workerUpdater.cancel(true);
125
        }
18 ilm 126
 
156 ilm 127
        workerUpdater = new SwingWorker<String, Object>() {
128
 
18 ilm 129
            @Override
130
            protected String doInBackground() throws Exception {
131
 
156 ilm 132
                SQLSelect sel = new SQLSelect();
18 ilm 133
                sel.addSelect(tableEcr.getField("CREDIT"), "SUM");
134
                sel.addSelect(tableEcr.getField("DEBIT"), "SUM");
135
 
136
                Where w = new Where(tableEcr.getField("ID_COMPTE_PCE"), "=", PointageModel.this.idCpt);
156 ilm 137
 
138
                if (deb != null && fin != null) {
139
                    w = w.and(new Where(tableEcr.getField("DATE"), deb, fin));
140
                } else if (deb != null) {
141
                    w = w.and(new Where(tableEcr.getField("DATE"), ">=", deb));
142
                } else if (fin != null) {
143
                    w = w.and(new Where(tableEcr.getField("DATE"), "<=", fin));
144
                }
18 ilm 145
                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "!=", "")));
146
 
147
                String reqPointee = sel.toString();
148
 
149
                Object obPointee = base.getDataSource().execute(reqPointee, new ArrayListHandler());
150
 
151
                List myListPointee = (List) obPointee;
152
 
153
                PointageModel.this.creditPointe = 0;
154
                PointageModel.this.debitPointe = 0;
156 ilm 155
                if (!myListPointee.isEmpty()) {
18 ilm 156
 
157
                    for (int i = 0; i < myListPointee.size(); i++) {
158
                        Object[] objTmp = (Object[]) myListPointee.get(i);
159
 
160
                        if (objTmp[0] != null) {
161
                            PointageModel.this.creditPointe += ((Number) objTmp[0]).longValue();
162
                        }
163
                        if (objTmp[1] != null) {
164
                            PointageModel.this.debitPointe += ((Number) objTmp[1]).longValue();
165
                        }
166
                    }
167
                }
168
 
156 ilm 169
                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "=", panel.getCodePointage())));
170
                String reqReleveCourant = sel.toString();
18 ilm 171
 
156 ilm 172
                Object obReleveCourant = base.getDataSource().execute(reqReleveCourant, new ArrayListHandler());
18 ilm 173
 
156 ilm 174
                List myListReleveCourant = (List) obReleveCourant;
18 ilm 175
 
176
                PointageModel.this.creditNonPointe = 0;
177
                PointageModel.this.debitNonPointe = 0;
156 ilm 178
                if (!myListReleveCourant.isEmpty()) {
18 ilm 179
 
156 ilm 180
                    for (int i = 0; i < myListReleveCourant.size(); i++) {
181
                        Object[] objTmp = (Object[]) myListReleveCourant.get(i);
18 ilm 182
 
183
                        if (objTmp[0] != null) {
184
                            PointageModel.this.creditNonPointe += ((Number) objTmp[0]).longValue();
185
                        }
186
 
187
                        if (objTmp[1] != null) {
188
                            PointageModel.this.debitNonPointe += ((Number) objTmp[1]).longValue();
189
                        }
190
                    }
191
                }
192
 
156 ilm 193
                sel.setWhere(w);
194
                String reqReleveTotal = sel.toString();
195
 
196
                Object obReleveTotal = base.getDataSource().execute(reqReleveTotal, new ArrayListHandler());
197
 
198
                List myListReleveTotal = (List) obReleveTotal;
199
 
200
                PointageModel.this.creditTotal = 0;
201
                PointageModel.this.debitTotal = 0;
202
                if (!myListReleveTotal.isEmpty()) {
203
 
204
                    for (int i = 0; i < myListReleveTotal.size(); i++) {
205
                        Object[] objTmp = (Object[]) myListReleveTotal.get(i);
206
 
207
                        if (objTmp[0] != null) {
208
                            PointageModel.this.creditTotal += ((Number) objTmp[0]).longValue();
209
                        }
210
 
211
                        if (objTmp[1] != null) {
212
                            PointageModel.this.debitTotal += ((Number) objTmp[1]).longValue();
213
                        }
214
                    }
215
                }
216
 
217
                PointageModel.this.creditListe = 0;
218
                PointageModel.this.debitListe = 0;
219
                IListe listeEcr = panel.getEcriturePanel().getListe();
220
                for (int i = 0; i < listeEcr.getRowCount(); i++) {
221
 
222
                    final SQLRowValues rowAt = ITableModel.getLine(listeEcr.getModel(), i).getRow();
223
                    if (rowAt.getObject("CREDIT") != null) {
224
                        PointageModel.this.creditListe += rowAt.getLong("CREDIT");
225
                    }
226
 
227
                    if (rowAt.getObject("DEBIT") != null) {
228
                        PointageModel.this.debitListe += rowAt.getLong("DEBIT");
229
                    }
230
                }
231
 
18 ilm 232
                return null;
233
            }
234
 
235
            @Override
236
            protected void done() {
237
 
156 ilm 238
                if (isCancelled()) {
239
                    return;
240
                }
180 ilm 241
                if (arrive != null && depart != null && ecart != null) {
242
                    final Long valueArrive = arrive.getValue();
243
                    final Long valueDepart = depart.getValue();
244
                    if (valueArrive != null && valueDepart != null) {
182 ilm 245
                        ecart.setValue(valueArrive - valueDepart - PointageModel.this.debitNonPointe + PointageModel.this.creditNonPointe);
180 ilm 246
                    } else {
247
                        ecart.setValue(null);
248
                    }
249
                }
18 ilm 250
                PointageModel.this.fireTableDataChanged();
251
            }
156 ilm 252
        };
253
        workerUpdater.execute();
18 ilm 254
 
255
    }
256
 
156 ilm 257
    @Override
18 ilm 258
    public String getColumnName(int column) {
259
 
260
        return this.titresCol[column];
261
    }
262
 
263
    public int getColumnCount() {
264
 
265
        return this.titresCol.length;
266
    }
267
 
268
    public int getRowCount() {
269
 
270
        return this.titresRow.length;
271
    }
272
 
273
    @Override
274
    public Class<?> getColumnClass(int columnIndex) {
275
        if (columnIndex == 0) {
276
            return String.class;
277
        } else {
278
            return Long.class;
279
        }
280
    }
281
 
282
    public Object getValueAt(int rowIndex, int columnIndex) {
283
 
284
        if (columnIndex == 0) {
285
            return this.titresRow[rowIndex];
286
        }
287
 
288
        if (columnIndex == 1) {
289
            if (rowIndex == 0) {
156 ilm 290
                return Long.valueOf(this.debitPointe);
18 ilm 291
            }
292
            if (rowIndex == 1) {
156 ilm 293
                return Long.valueOf(this.creditPointe);
18 ilm 294
            }
295
            if (rowIndex == 2) {
156 ilm 296
                return Long.valueOf(this.debitPointe - this.creditPointe);
18 ilm 297
            }
298
        }
299
 
300
        if (columnIndex == 2) {
301
            if (rowIndex == 0) {
156 ilm 302
                return Long.valueOf(this.debitNonPointe);
18 ilm 303
            }
304
            if (rowIndex == 1) {
156 ilm 305
                return Long.valueOf(this.creditNonPointe);
18 ilm 306
            }
307
            if (rowIndex == 2) {
156 ilm 308
                return Long.valueOf(this.debitNonPointe - this.creditNonPointe);
18 ilm 309
            }
310
        }
311
 
312
        if (columnIndex == 3) {
313
            if (rowIndex == 0) {
156 ilm 314
                return Long.valueOf(this.debitTotal);
18 ilm 315
            }
316
            if (rowIndex == 1) {
156 ilm 317
                return Long.valueOf(this.creditTotal);
18 ilm 318
            }
319
            if (rowIndex == 2) {
156 ilm 320
                return Long.valueOf(this.debitTotal - this.creditTotal);
18 ilm 321
            }
322
        }
323
 
324
        if (columnIndex == 4) {
325
            if (rowIndex == 0) {
156 ilm 326
                return Long.valueOf(this.debitSelection);
18 ilm 327
            }
328
            if (rowIndex == 1) {
156 ilm 329
                return Long.valueOf(this.creditSelection);
18 ilm 330
            }
331
            if (rowIndex == 2) {
156 ilm 332
                return Long.valueOf(this.debitSelection - this.creditSelection);
18 ilm 333
            }
334
        }
335
 
336
        if (columnIndex == 5) {
337
            if (rowIndex == 0) {
156 ilm 338
                return Long.valueOf(this.debitListe);
18 ilm 339
            }
340
            if (rowIndex == 1) {
156 ilm 341
                return Long.valueOf(this.creditListe);
18 ilm 342
            }
343
            if (rowIndex == 2) {
156 ilm 344
                return Long.valueOf(this.debitListe - this.creditListe);
18 ilm 345
            }
346
        }
347
 
348
        return null;
349
    }
350
}