OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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