OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Rev 174 | 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.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.ui.DeviseCellEditor;
18
import org.openconcerto.erp.core.common.ui.MultiLineTableCellEditor;
93 ilm 19
import org.openconcerto.erp.core.common.ui.RowValuesMultiLineEditTable;
18 ilm 20
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
93 ilm 21
import org.openconcerto.erp.preferences.DefaultNXProps;
18 ilm 22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.element.SQLElement;
24
import org.openconcerto.sql.model.SQLBase;
25
import org.openconcerto.sql.model.SQLRow;
80 ilm 26
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 27
import org.openconcerto.sql.model.SQLRowListRSH;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLSelect;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.sql.model.Where;
32
import org.openconcerto.sql.sqlobject.ITextWithCompletion;
33
import org.openconcerto.sql.view.list.AutoCompletionManager;
34
import org.openconcerto.sql.view.list.RowValuesTable;
35
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
36
import org.openconcerto.sql.view.list.RowValuesTableModel;
37
import org.openconcerto.sql.view.list.SQLTableElement;
65 ilm 38
import org.openconcerto.sql.view.list.TextTableCellEditorWithCompletion;
39
import org.openconcerto.sql.view.list.ValidStateChecker;
18 ilm 40
import org.openconcerto.ui.DefaultGridBagConstraints;
65 ilm 41
import org.openconcerto.utils.checks.ValidState;
18 ilm 42
 
43
import java.awt.GridBagConstraints;
44
import java.awt.GridBagLayout;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.MouseEvent;
47
import java.awt.event.MouseListener;
48
import java.util.HashMap;
49
import java.util.List;
50
import java.util.Map;
51
import java.util.Vector;
52
 
53
import javax.swing.AbstractAction;
54
import javax.swing.JPanel;
55
import javax.swing.JPopupMenu;
56
import javax.swing.JScrollPane;
80 ilm 57
import javax.swing.SwingUtilities;
18 ilm 58
import javax.swing.ToolTipManager;
67 ilm 59
import javax.swing.event.TableModelEvent;
60
import javax.swing.event.TableModelListener;
18 ilm 61
 
62
public class SaisieKmItemTable extends JPanel implements MouseListener {
63
 
64
    private final RowValuesTable table;
65
    private final SQLTableElement debit;
66
    private final SQLTableElement credit;
67
    private final SQLTableElement tableElementNumeroCompte;
68
    private final CompteRowValuesRenderer numeroCompteRenderer = new CompteRowValuesRenderer();
69
    private final DeviseKmRowValuesRenderer deviseRenderer = new DeviseKmRowValuesRenderer();
70
    private final DeviseCellEditor deviseCellEditor = new DeviseCellEditor();
71
 
72
    public SaisieKmItemTable(final SQLRowValues defaultRowVals) {
73
        setLayout(new GridBagLayout());
74
        final GridBagConstraints c = new DefaultGridBagConstraints();
75
        c.weightx = 1;
76
 
77
        final SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_KM_ELEMENT");
78
 
79
        // TODO Obligation de choisir un compte correct
80
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
73 ilm 81
        final SQLTable tableElement = elt.getTable();
82
        this.tableElementNumeroCompte = new SQLTableElement(tableElement.getField("NUMERO"));
18 ilm 83
        list.add(this.tableElementNumeroCompte);
84
 
73 ilm 85
        final SQLTableElement tableElementNomCompte = new SQLTableElement(tableElement.getField("NOM"));
18 ilm 86
        list.add(tableElementNomCompte);
87
 
93 ilm 88
        if (tableElement.getTable().contains("NOM_PIECE")) {
89
            final SQLTableElement tableElementNomPiece = new SQLTableElement(tableElement.getField("NOM_PIECE"));
90
            list.add(tableElementNomPiece);
91
        }
92
 
73 ilm 93
        final SQLTableElement tableElementNomEcriture = new SQLTableElement(tableElement.getField("NOM_ECRITURE"));
18 ilm 94
        list.add(tableElementNomEcriture);
95
 
156 ilm 96
        boolean kd = false;
73 ilm 97
        this.debit = new SQLTableElement(tableElement.getField("DEBIT"), Long.class, this.deviseCellEditor);
18 ilm 98
        list.add(this.debit);
73 ilm 99
        this.credit = new SQLTableElement(tableElement.getField("CREDIT"), Long.class, this.deviseCellEditor);
18 ilm 100
        list.add(this.credit);
156 ilm 101
        if (!kd && !DefaultNXProps.getInstance().getBooleanValue("HideAnalytique")) {
142 ilm 102
            final AnalytiqueItemTable analytiqueAssocTable = new AnalytiqueItemTable(true);
103
            SQLTableElement eltPourcentAnalytique = new SQLTableElement(tableElement.getField("ANALYTIQUE"), String.class,
156 ilm 104
                    new MultiLineTableCellEditor((RowValuesMultiLineEditTable) analytiqueAssocTable.getTable(), analytiqueAssocTable)) {
105
                public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
106
                    return vals.getString("NUMERO") != null && (vals.getString("NUMERO").startsWith("6") || vals.getString("NUMERO").startsWith("7"));
107
                };
108
            };
142 ilm 109
            list.add(eltPourcentAnalytique);
110
        }
18 ilm 111
 
80 ilm 112
        final RowValuesTableModel model = new RowValuesTableModel(elt, list, tableElement.getField("NUMERO"), false, defaultRowVals) {
18 ilm 113
            @Override
114
            public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {
115
                super.setValueAt(aValue, rowIndex, columnIndex);
116
 
117
                final int debitIndex = getColumnIndexForElement(SaisieKmItemTable.this.debit);
118
                final int creditIndex = getColumnIndexForElement(SaisieKmItemTable.this.credit);
119
 
120
                // float debitVal = ((Float) model.getValueAt(rowIndex, debitIndex);
121
 
122
                if (debitIndex == columnIndex && ((Long) aValue).longValue() != 0 && ((Long) getValueAt(rowIndex, creditIndex)).longValue() != 0) {
123
                    setValueAt(Long.valueOf(0), rowIndex, creditIndex);
124
                } else {
125
                    if (creditIndex == columnIndex && ((Long) aValue).longValue() != 0 && ((Long) getValueAt(rowIndex, debitIndex)).longValue() != 0) {
126
                        setValueAt(Long.valueOf(0), rowIndex, debitIndex);
127
                    }
128
                }
129
            }
130
        };
131
 
132
        this.table = new RowValuesTable(model, null);
133
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
134
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
135
 
136
        // Autocompletion
142 ilm 137
        final AutoCompletionManager m = new AutoCompletionManager(this.tableElementNumeroCompte,
138
                ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("COMPTE_PCE.NUMERO"), this.table, this.table.getRowValuesTableModel(),
139
                ITextWithCompletion.MODE_STARTWITH, true, false, new ValidStateChecker() {
65 ilm 140
 
142 ilm 141
                    ComptePCESQLElement elt = Configuration.getInstance().getDirectory().getElement(ComptePCESQLElement.class);
65 ilm 142
 
142 ilm 143
                    @Override
144
                    public ValidState getValidState(Object o) {
145
                        if (o != null) {
146
                            return elt.getCompteNumeroValidState(o.toString());
147
                        }
148
                        return super.getValidState(o);
149
                    }
150
                });
18 ilm 151
        m.fill("NOM", "NOM");
152
        m.setFillWithField("NUMERO");
142 ilm 153
        final Where w = new Where(elt.getTable().getTable("COMPTE_PCE").getField("OBSOLETE"), "=", Boolean.FALSE);
154
        m.setWhere(w);
18 ilm 155
 
156
        // FIXME erreur fill numero
157
        final AutoCompletionManager m2 = new AutoCompletionManager(tableElementNomCompte, ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("COMPTE_PCE.NOM"),
158
                this.table, this.table.getRowValuesTableModel(), ITextWithCompletion.MODE_CONTAINS, true);
159
        m2.fill("NUMERO", "NUMERO");
160
        m2.setFillWithField("NOM");
142 ilm 161
        m2.setWhere(w);
18 ilm 162
 
65 ilm 163
        TextTableCellEditorWithCompletion t = (TextTableCellEditorWithCompletion) this.tableElementNumeroCompte.getTableCellEditor(this.table);
164
 
18 ilm 165
        this.add(new RowValuesTableControlPanel(this.table), c);
166
 
167
        c.gridy++;
168
        c.fill = GridBagConstraints.BOTH;
169
        c.weightx = 1;
170
        c.weighty = 1;
171
        this.add(new JScrollPane(this.table), c);
172
 
173
        this.tableElementNumeroCompte.setRenderer(this.numeroCompteRenderer);
174
        this.debit.setRenderer(this.deviseRenderer);
175
        this.credit.setRenderer(this.deviseRenderer);
176
 
177
        this.table.addMouseListener(this);
67 ilm 178
        this.table.getModel().addTableModelListener(new TableModelListener() {
18 ilm 179
 
67 ilm 180
            @Override
181
            public void tableChanged(TableModelEvent e) {
182
                // Sélectionne automatiquement la ligne ajoutée
183
                if (e.getType() == TableModelEvent.INSERT) {
184
                    editCellAt(e.getFirstRow(), 0);
185
                }
18 ilm 186
 
67 ilm 187
            }
188
        });
189
 
18 ilm 190
    }
191
 
192
    /**
193
     * Charge une ecriture dans une ligne de la RowValuesTable
194
     *
195
     * @param idEcriture id de l'ecriture à charger
196
     * @param contrePasser contrePasser l'ecriture
197
     */
198
    private void loadEcriture(final SQLRow ecrRow, final boolean contrePasser) {
80 ilm 199
        assert SwingUtilities.isEventDispatchThread();
73 ilm 200
        final SQLRow compteRow = ecrRow.getForeignRow("ID_COMPTE_PCE");
18 ilm 201
        final Map<String, Object> m = new HashMap<String, Object>();
202
 
203
        m.put("NUMERO", compteRow.getString("NUMERO"));
204
        m.put("NOM", compteRow.getString("NOM"));
205
 
93 ilm 206
        if (ecrRow.getTable().contains("NOM_PIECE")) {
94 ilm 207
            m.put("NOM_PIECE", ecrRow.getString("NOM_PIECE"));
93 ilm 208
        }
209
 
18 ilm 210
        if (contrePasser) {
211
            m.put("NOM_ECRITURE", "Contrepassation - " + ecrRow.getString("NOM"));
212
            m.put("DEBIT", ecrRow.getObject("CREDIT"));
213
            m.put("CREDIT", ecrRow.getObject("DEBIT"));
214
        } else {
215
            m.put("NOM_ECRITURE", ecrRow.getString("NOM"));
216
            m.put("DEBIT", ecrRow.getObject("DEBIT"));
217
            m.put("CREDIT", ecrRow.getObject("CREDIT"));
218
        }
219
 
220
        final SQLRowValues rowVals = new SQLRowValues(getModel().getSQLElement().getTable(), m);
221
        this.table.getRowValuesTableModel().addRow(rowVals, false);
222
 
223
    }
224
 
225
    /**
226
     * Remplit la RowValuesTable avec les ecritures du mouvement
227
     *
228
     * @param idMvt id du mouvement
229
     * @param contrePasser contrePasser le mouvement
230
     */
231
    public void loadMouvement(final int idMvt, final boolean contrePasser) {
80 ilm 232
        assert SwingUtilities.isEventDispatchThread();
18 ilm 233
        // FIXME load analytique
234
        final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
235
        final SQLTable ecrTable = base.getTable("ECRITURE");
236
 
67 ilm 237
        final SQLSelect selEcriture = new SQLSelect();
18 ilm 238
        selEcriture.addSelectStar(ecrTable);
239
 
240
        final Where w = new Where(ecrTable.getField("ID_MOUVEMENT"), "=", idMvt);
241
        selEcriture.setWhere(w);
242
 
243
        final String reqEcriture = selEcriture.asString();
244
 
245
        final List<SQLRow> myListEcriture = (List<SQLRow>) base.getDataSource().execute(reqEcriture, SQLRowListRSH.createFromSelect(selEcriture, ecrTable));
246
 
247
        this.table.getRowValuesTableModel().clearRows();
248
 
249
        for (SQLRow sqlRow : myListEcriture) {
250
            loadEcriture(sqlRow, contrePasser);
251
        }
252
        this.table.getRowValuesTableModel().fireTableDataChanged();
253
    }
254
 
255
    public void updateField(final String field, final int id) {
256
        this.table.updateField(field, id);
257
    }
258
 
259
    public void insertFrom(final String field, final int id) {
260
        this.table.insertFrom(field, id);
261
    }
262
 
80 ilm 263
    public void insertFrom(final SQLRowAccessor row) {
264
        this.table.insertFrom(row);
265
    }
266
 
18 ilm 267
    public RowValuesTableModel getModel() {
268
        return this.table.getRowValuesTableModel();
269
    }
270
 
271
    public SQLTableElement getCreditElement() {
272
        return this.credit;
273
    }
274
 
275
    public SQLTableElement getDebitElement() {
276
        return this.debit;
277
    }
278
 
279
    public SQLTableElement getNumeroCompteElement() {
280
        return this.tableElementNumeroCompte;
281
    }
282
 
283
    public void setCreateAutoActive(final boolean b) {
284
        this.numeroCompteRenderer.setCreateActive(b);
285
        this.table.revalidate();
286
        this.table.repaint();
287
    }
288
 
289
    public void setRowDeviseValidAt(final boolean b, final int index) {
290
        this.deviseRenderer.setValid(b, index);
291
 
292
    }
293
 
294
    public void editCellAt(final int row, final int column) {
80 ilm 295
        assert SwingUtilities.isEventDispatchThread();
18 ilm 296
        this.table.setColumnSelectionInterval(column, column);
297
        this.table.setRowSelectionInterval(row, row);
298
        this.table.editCellAt(row, column);
299
    }
300
 
301
    private long getContrepartie() {
80 ilm 302
        assert SwingUtilities.isEventDispatchThread();
18 ilm 303
        long totalCred = 0;
304
        long totalDeb = 0;
305
        final RowValuesTableModel model = this.table.getRowValuesTableModel();
306
        final int creditIndex = model.getColumnIndexForElement(getCreditElement());
307
        final int debitIndex = model.getColumnIndexForElement(getDebitElement());
308
        for (int i = 0; i < this.table.getRowCount(); i++) {
73 ilm 309
            if (model.isRowValid(i)) {
310
                final Long fTc = (Long) model.getValueAt(i, creditIndex);
311
                if (fTc != null) {
18 ilm 312
                    totalCred += fTc.longValue();
313
                }
73 ilm 314
                final Long fTd = (Long) model.getValueAt(i, debitIndex);
315
                if (fTd != null) {
18 ilm 316
                    totalDeb += fTd.longValue();
317
                }
318
            }
319
        }
320
        return totalDeb - totalCred;
321
    }
322
 
323
    private long getSoldeRow(final int index) {
80 ilm 324
        assert SwingUtilities.isEventDispatchThread();
18 ilm 325
        if (index >= 0 && index < this.table.getRowCount()) {
326
            final SQLRowValues rowVals = this.table.getRowValuesTableModel().getRowValuesAt(index);
327
            return rowVals.getLong("DEBIT") - rowVals.getLong("CREDIT");
328
        } else {
329
            return 0;
330
        }
331
    }
332
 
73 ilm 333
    public void fillEmptyEntryLabel(String previousText, String text) {
80 ilm 334
        assert SwingUtilities.isEventDispatchThread();
73 ilm 335
        if (text == null)
336
            return;
337
        RowValuesTableModel model = table.getRowValuesTableModel();
338
        int size = model.getRowCount();
339
        for (int i = 0; i < size; i++) {
340
            SQLRowValues r = model.getRowValuesAt(i);
341
            if (r.getString("NOM_ECRITURE") == null || r.getString("NOM_ECRITURE").trim().isEmpty() || r.getString("NOM_ECRITURE").trim().equals(previousText)) {
342
                r.put("NOM_ECRITURE", text);
343
            }
344
        }
345
        model.fireTableDataChanged();
346
    }
347
 
18 ilm 348
    public void mousePressed(final MouseEvent e) {
349
        final int rowSel = this.table.getSelectedRow();
350
        if (e.getButton() == MouseEvent.BUTTON3 && rowSel >= 0 && rowSel < this.table.getRowCount()) {
351
            final JPopupMenu menuDroit = new JPopupMenu();
352
 
353
            menuDroit.add(new AbstractAction("Contrepartie") {
354
                public void actionPerformed(final ActionEvent ev) {
355
 
356
                    long l = getContrepartie();
357
                    if (SaisieKmItemTable.this.table.getRowValuesTableModel().isRowValid(rowSel)) {
358
                        l += getSoldeRow(rowSel);
359
                    }
360
 
361
                    if (l > 0) {
362
                        SaisieKmItemTable.this.table.getRowValuesTableModel().putValue(Long.valueOf(0), rowSel, "DEBIT");
363
                        SaisieKmItemTable.this.table.getRowValuesTableModel().putValue(l, rowSel, "CREDIT");
364
                    } else {
365
                        SaisieKmItemTable.this.table.getRowValuesTableModel().putValue(Long.valueOf(0), rowSel, "CREDIT");
366
                        SaisieKmItemTable.this.table.getRowValuesTableModel().putValue(-l, rowSel, "DEBIT");
367
                    }
368
                }
369
            });
370
            menuDroit.pack();
371
            menuDroit.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
372
            menuDroit.setVisible(true);
373
        }
374
    }
375
 
376
    public void mouseReleased(final MouseEvent e) {
67 ilm 377
    }
18 ilm 378
 
67 ilm 379
    public void mouseClicked(final MouseEvent e) {
18 ilm 380
    }
381
 
67 ilm 382
    public void mouseEntered(final MouseEvent e) {
383
    }
384
 
385
    public void mouseExited(final MouseEvent e) {
386
    }
73 ilm 387
 
18 ilm 388
}