OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 156 | 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.core.finance.accounting.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.model.SQLBase;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLSelect;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
23
 
24
import java.util.List;
25
 
26
import javax.swing.SwingWorker;
27
import javax.swing.table.AbstractTableModel;
28
 
29
import org.apache.commons.dbutils.handlers.ArrayListHandler;
30
 
31
public class PointageModel extends AbstractTableModel {
32
 
33
    private String[] titresCol;
34
    private String[] titresRow;
35
 
36
    private long debitPointe, creditPointe, debitNonPointe, creditNonPointe, creditSelection, debitSelection;
37
 
38
    private static final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
39
    private static final SQLTable tableEcr = base.getTable("ECRITURE");
40
    int idCpt;
41
 
42
    public PointageModel(int idCpt) {
43
 
44
        this.creditNonPointe = 0;
45
        this.creditPointe = 0;
46
        this.creditSelection = 0;
47
 
48
        this.debitNonPointe = 0;
49
        this.debitPointe = 0;
50
        this.debitSelection = 0;
51
 
52
        this.idCpt = idCpt;
53
        this.titresCol = new String[6];
54
        this.titresCol[0] = "Totaux";
55
        this.titresCol[1] = "Pointé";
56
        this.titresCol[2] = "Non Pointé";
57
        this.titresCol[3] = "Total";
58
 
59
        this.titresCol[4] = "Sélection";
60
        this.titresCol[5] = "Pointéé + sélection";
61
 
62
        this.titresRow = new String[3];
63
        this.titresRow[0] = "Débit";
64
        this.titresRow[1] = "Crédit";
65
        this.titresRow[2] = "Solde";
66
 
67
        updateTotauxCompte();
68
    }
69
 
70
    public void setIdCompte(int id) {
71
        this.idCpt = id;
72
        updateTotauxCompte();
73
        updateSelection(null);
74
    }
75
 
76
    public void updateSelection(int[] rowIndex) {
77
        System.err.println("Update Selection");
78
        this.creditSelection = 0;
79
        this.debitSelection = 0;
80
 
81
        if (rowIndex != null) {
82
            for (int i = 0; i < rowIndex.length; i++) {
83
 
84
                SQLRow row = tableEcr.getRow(rowIndex[i]);
85
 
86
                if (row != null) {
87
 
80 ilm 88
                    // if (row.getString("POINTEE").trim().length() == 0) {
89
                    this.debitSelection += ((Long) row.getObject("DEBIT")).longValue();
90
                    this.creditSelection += ((Long) row.getObject("CREDIT")).longValue();
91
                    // }
92
                    /*
93
                     * else {
94
                     *
95
                     * this.debitSelection -= row.getFloat("DEBIT"); this.creditSelection -=
96
                     * row.getFloat("CREDIT"); }
97
                     */
18 ilm 98
                }
99
            }
100
        }
101
        this.fireTableDataChanged();
102
    }
103
 
104
    public void updateTotauxCompte() {
105
 
106
        new SwingWorker<String, Object>() {
107
 
108
            @Override
109
            protected String doInBackground() throws Exception {
110
 
111
                SQLSelect sel = new SQLSelect(base);
112
                sel.addSelect(tableEcr.getField("CREDIT"), "SUM");
113
                sel.addSelect(tableEcr.getField("DEBIT"), "SUM");
114
 
115
                Where w = new Where(tableEcr.getField("ID_COMPTE_PCE"), "=", PointageModel.this.idCpt);
116
                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "!=", "")));
117
 
118
                String reqPointee = sel.toString();
119
 
120
                Object obPointee = base.getDataSource().execute(reqPointee, new ArrayListHandler());
121
 
122
                List myListPointee = (List) obPointee;
123
 
124
                PointageModel.this.creditPointe = 0;
125
                PointageModel.this.debitPointe = 0;
126
                if (myListPointee.size() != 0) {
127
 
128
                    for (int i = 0; i < myListPointee.size(); i++) {
129
                        Object[] objTmp = (Object[]) myListPointee.get(i);
130
 
131
                        if (objTmp[0] != null) {
132
                            PointageModel.this.creditPointe += ((Number) objTmp[0]).longValue();
133
                        }
134
                        if (objTmp[1] != null) {
135
                            PointageModel.this.debitPointe += ((Number) objTmp[1]).longValue();
136
                        }
137
                    }
138
                }
139
 
140
                sel.setWhere(w.and(new Where(tableEcr.getField("POINTEE"), "=", "")));
141
                String reqNotPointee = sel.toString();
142
 
143
                Object obNotPointee = base.getDataSource().execute(reqNotPointee, new ArrayListHandler());
144
 
145
                List myListNotPointee = (List) obNotPointee;
146
 
147
                PointageModel.this.creditNonPointe = 0;
148
                PointageModel.this.debitNonPointe = 0;
149
                if (myListNotPointee.size() != 0) {
150
 
151
                    for (int i = 0; i < myListNotPointee.size(); i++) {
152
                        Object[] objTmp = (Object[]) myListNotPointee.get(i);
153
 
154
                        if (objTmp[0] != null) {
155
                            PointageModel.this.creditNonPointe += ((Number) objTmp[0]).longValue();
156
                        }
157
 
158
                        if (objTmp[1] != null) {
159
                            PointageModel.this.debitNonPointe += ((Number) objTmp[1]).longValue();
160
                        }
161
                    }
162
                }
163
 
164
                return null;
165
            }
166
 
167
            @Override
168
            protected void done() {
169
 
170
                PointageModel.this.fireTableDataChanged();
171
            }
172
        }.execute();
173
 
174
    }
175
 
176
    public String getColumnName(int column) {
177
 
178
        return this.titresCol[column];
179
    }
180
 
181
    public int getColumnCount() {
182
 
183
        return this.titresCol.length;
184
    }
185
 
186
    public int getRowCount() {
187
 
188
        return this.titresRow.length;
189
    }
190
 
191
    @Override
192
    public Class<?> getColumnClass(int columnIndex) {
193
        if (columnIndex == 0) {
194
            return String.class;
195
        } else {
196
            return Long.class;
197
        }
198
    }
199
 
200
    public Object getValueAt(int rowIndex, int columnIndex) {
201
 
202
        if (columnIndex == 0) {
203
            return this.titresRow[rowIndex];
204
        }
205
 
206
        if (columnIndex == 1) {
207
            if (rowIndex == 0) {
208
                return new Long(this.debitPointe);
209
            }
210
            if (rowIndex == 1) {
211
                return new Long(this.creditPointe);
212
            }
213
            if (rowIndex == 2) {
214
                return new Long(this.debitPointe - this.creditPointe);
215
            }
216
        }
217
 
218
        if (columnIndex == 2) {
219
            if (rowIndex == 0) {
220
                return new Long(this.debitNonPointe);
221
            }
222
            if (rowIndex == 1) {
223
                return new Long(this.creditNonPointe);
224
            }
225
            if (rowIndex == 2) {
226
                return new Long(this.debitNonPointe - this.creditNonPointe);
227
            }
228
        }
229
 
230
        if (columnIndex == 3) {
231
            if (rowIndex == 0) {
232
                return new Long(this.debitNonPointe + this.debitPointe);
233
            }
234
            if (rowIndex == 1) {
235
                return new Long(this.creditNonPointe + this.creditPointe);
236
            }
237
            if (rowIndex == 2) {
238
                return new Long((this.debitNonPointe - this.creditNonPointe) + (this.debitPointe - this.creditPointe));
239
            }
240
        }
241
 
242
        if (columnIndex == 4) {
243
            if (rowIndex == 0) {
244
                return new Long(this.debitSelection);
245
            }
246
            if (rowIndex == 1) {
247
                return new Long(this.creditSelection);
248
            }
249
            if (rowIndex == 2) {
250
                return new Long(this.debitSelection - this.creditSelection);
251
            }
252
        }
253
 
254
        if (columnIndex == 5) {
255
            if (rowIndex == 0) {
256
                return new Long(this.debitSelection + this.debitPointe);
257
            }
258
            if (rowIndex == 1) {
259
                return new Long(this.creditSelection + this.creditPointe);
260
            }
261
            if (rowIndex == 2) {
262
                return new Long(this.debitSelection - this.creditSelection + this.debitPointe - this.creditPointe);
263
            }
264
        }
265
 
266
        return null;
267
    }
268
}