OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 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
 
156 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
83 ilm 17
import org.openconcerto.erp.core.common.ui.DeviseCellEditor;
93 ilm 18
import org.openconcerto.erp.core.common.ui.RowValuesMultiLineEditTable;
83 ilm 19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.SQLElement;
93 ilm 21
import org.openconcerto.sql.model.SQLRow;
83 ilm 22
import org.openconcerto.sql.model.SQLRowAccessor;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLTable;
93 ilm 25
import org.openconcerto.sql.model.UndefinedRowValuesCache;
83 ilm 26
import org.openconcerto.sql.view.list.CellDynamicModifier;
27
import org.openconcerto.sql.view.list.RowValuesTable;
28
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
29
import org.openconcerto.sql.view.list.RowValuesTableModel;
93 ilm 30
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
83 ilm 31
import org.openconcerto.sql.view.list.SQLTableElement;
32
import org.openconcerto.ui.DefaultGridBagConstraints;
93 ilm 33
import org.openconcerto.ui.table.PercentTableCellRenderer;
90 ilm 34
import org.openconcerto.utils.DecimalUtils;
83 ilm 35
 
93 ilm 36
import java.awt.Dimension;
83 ilm 37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
93 ilm 39
import java.awt.event.ActionEvent;
40
import java.awt.event.ActionListener;
83 ilm 41
import java.math.BigDecimal;
42
import java.math.RoundingMode;
93 ilm 43
import java.util.Collection;
83 ilm 44
import java.util.List;
45
import java.util.Vector;
46
 
149 ilm 47
import javax.swing.AbstractAction;
93 ilm 48
import javax.swing.JButton;
83 ilm 49
import javax.swing.JPanel;
50
import javax.swing.JScrollPane;
51
import javax.swing.ToolTipManager;
149 ilm 52
import javax.swing.event.TableModelEvent;
53
import javax.swing.event.TableModelListener;
83 ilm 54
 
55
public class AnalytiqueItemTable extends JPanel {
56
 
57
    private final RowValuesTable table;
58
    private final DeviseKmRowValuesRenderer deviseRenderer = new DeviseKmRowValuesRenderer();
59
    private final DeviseCellEditor deviseCellEditor = new DeviseCellEditor();
93 ilm 60
    private SQLRowAccessor rowEcr;
61
    private static final SQLElement elt = Configuration.getInstance().getDirectory().getElement("ASSOCIATION_ANALYTIQUE");
62
    private final SQLRowValues rowVals = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(elt.getTable()));
83 ilm 63
 
149 ilm 64
    private long totalArepartir;
65
 
93 ilm 66
    public SQLRowValues getDefaultRowValues() {
67
        return this.rowVals;
68
    }
69
 
70
    public AnalytiqueItemTable(boolean multilineEditor) {
149 ilm 71
        this(multilineEditor, null);
72
    }
73
 
74
    public AnalytiqueItemTable(boolean multilineEditor, final AbstractAction actionOnValidate) {
83 ilm 75
        setLayout(new GridBagLayout());
76
        final GridBagConstraints c = new DefaultGridBagConstraints();
77
        c.weightx = 1;
78
 
79
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
80
        final SQLTable tableElement = elt.getTable();
81
 
82
        final SQLTableElement tableElementNomCompte = new SQLTableElement(tableElement.getField("ID_POSTE_ANALYTIQUE"));
83
        list.add(tableElementNomCompte);
84
 
85
        final SQLTableElement tableElementPourcent = new SQLTableElement(tableElement.getField("POURCENT"));
93 ilm 86
        tableElementPourcent.setRenderer(new PercentTableCellRenderer());
83 ilm 87
        list.add(tableElementPourcent);
88
 
149 ilm 89
        final SQLTableElement tableElementMontant = new SQLTableElement(tableElement.getField("MONTANT"), Long.class, this.deviseCellEditor) {
90
            @Override
91
            protected Object getDefaultNullValue() {
92
                return 0L;
93
            }
94
        };
83 ilm 95
        list.add(tableElementMontant);
96
 
93 ilm 97
        rowVals.put("POURCENT", BigDecimal.ONE.movePointRight(2));
98
        final RowValuesTableModel model = new RowValuesTableModel(elt, list, tableElement.getField("ID_POSTE_ANALYTIQUE"), false, rowVals);
83 ilm 99
 
93 ilm 100
        if (multilineEditor) {
101
            this.table = new RowValuesMultiLineEditTable(model, null, "ANALYTIQUE") {
102
                @Override
103
                public String getStringValue(final SQLRowValues rowVals) {
104
                    return getStringAssocs(rowVals);
105
                }
83 ilm 106
 
93 ilm 107
                public void insertFrom(final SQLRowAccessor row) {
108
                    rowEcr = row;
109
                    getDefaultRowValues().put("POURCENT", BigDecimal.TEN.movePointRight(1));
110
                    getDefaultRowValues().put("MONTANT", rowEcr.getLong("DEBIT") - rowEcr.getLong("CREDIT"));
149 ilm 111
                    totalArepartir = rowEcr.getLong("DEBIT") - rowEcr.getLong("CREDIT");
93 ilm 112
                    super.insertFrom(row);
113
                }
149 ilm 114
 
93 ilm 115
            };
83 ilm 116
 
93 ilm 117
            ToolTipManager.sharedInstance().unregisterComponent(this.table);
118
            ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
119
 
120
            final RowValuesTableControlPanel panelControl = new RowValuesTableControlPanel(this.table);
121
            panelControl.setVisibleButtonHaut(false);
122
            panelControl.setVisibleButtonBas(false);
123
            panelControl.setVisibleButtonClone(false);
124
            panelControl.setVisibleButtonInserer(false);
125
            this.add(panelControl, c);
126
 
127
            c.gridy++;
128
            c.fill = GridBagConstraints.BOTH;
129
            c.weightx = 1;
130
            c.weighty = 1;
131
 
132
            this.add(new JScrollPane(this.table), c);
133
            this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
134
 
135
            // Bouton valider et fermer
136
            final JButton buttonValider = new JButton("Valider les modifications");
137
            final JButton buttonFermer = new JButton("Fermer");
138
            c.gridx = 0;
139
            c.gridy++;
140
            c.anchor = GridBagConstraints.EAST;
141
            c.weightx = 1;
142
            c.weighty = 0;
143
            c.fill = GridBagConstraints.NONE;
144
            c.gridwidth = GridBagConstraints.REMAINDER;
145
            final JPanel panelButton = new JPanel();
149 ilm 146
            buttonValider.setEnabled(false);
93 ilm 147
            panelButton.add(buttonValider);
148
            panelButton.add(buttonFermer);
149
            this.add(panelButton, c);
149 ilm 150
            model.addTableModelListener(new TableModelListener() {
93 ilm 151
 
149 ilm 152
                @Override
153
                public void tableChanged(TableModelEvent e) {
154
                    long totalReparti = 0;
156 ilm 155
                        // TODO Gestion répartition multi axe
156
                        if (totalArepartir != 0) {
157
                            for (int i = 0; i < model.getRowCount(); i++) {
158
                                totalReparti += model.getRowValuesAt(i).getLong("MONTANT");
159
                            }
160
 
161
                            getDefaultRowValues().put("POURCENT", new BigDecimal(totalReparti).divide(new BigDecimal(totalArepartir), DecimalUtils.HIGH_PRECISION).setScale(6, RoundingMode.HALF_UP));
162
                            getDefaultRowValues().put("MONTANT", totalArepartir - totalReparti);
163
                            buttonValider.setEnabled(totalReparti == totalArepartir);
164
                        } else {
165
                            buttonValider.setEnabled(true);
149 ilm 166
                        }
167
                }
168
            });
93 ilm 169
            final RowValuesMultiLineEditTable multiTable = (RowValuesMultiLineEditTable) this.table;
170
            buttonValider.addActionListener(new ActionListener() {
171
                public void actionPerformed(final ActionEvent event) {
172
                    multiTable.updateField(multiTable.getForeignField(), multiTable.getRowValuesRoot());
173
                    // buttonValider.setEnabled(false);
174
                    multiTable.closeTable();
149 ilm 175
                    if (actionOnValidate != null) {
176
                        actionOnValidate.actionPerformed(null);
177
                    }
93 ilm 178
                }
179
            });
180
            buttonFermer.addActionListener(new ActionListener() {
181
                public void actionPerformed(final ActionEvent event) {
182
                    multiTable.closeTable();
183
                }
184
            });
185
 
186
            this.setMinimumSize(new Dimension(this.getMinimumSize().width, 200));
187
            this.setPreferredSize(new Dimension(this.getPreferredSize().width, 200));
188
        } else {
189
            this.table = new RowValuesTable(model, null);
190
            ToolTipManager.sharedInstance().unregisterComponent(this.table);
191
            ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
192
            this.add(new RowValuesTableControlPanel(this.table), c);
193
            c.fill = GridBagConstraints.BOTH;
194
            c.weightx = 1;
195
            c.weighty = 1;
196
            c.gridy++;
197
            this.add(new JScrollPane(this.table), c);
198
        }
199
 
83 ilm 200
        tableElementMontant.addModificationListener(tableElementPourcent);
201
        tableElementPourcent.setModifier(new CellDynamicModifier() {
202
            @Override
93 ilm 203
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
83 ilm 204
                long montant = row.getLong("MONTANT");
205
 
93 ilm 206
                SQLRowAccessor foreignEcr = row.getForeign("ID_ECRITURE");
207
                if (rowEcr != null) {
208
                    foreignEcr = rowEcr;
209
                }
210
                long total = foreignEcr.getLong("DEBIT") - foreignEcr.getLong("CREDIT");
211
                BigDecimal pourcent = BigDecimal.ZERO;
83 ilm 212
 
93 ilm 213
                if (total != 0) {
149 ilm 214
                    pourcent = new BigDecimal(montant).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).abs().movePointRight(2).setScale(tableElementPourcent.getDecimalDigits(),
215
                            RoundingMode.HALF_UP);
93 ilm 216
                }
83 ilm 217
                return pourcent;
218
            }
219
 
220
        });
221
 
222
        tableElementPourcent.addModificationListener(tableElementMontant);
223
        tableElementMontant.setModifier(new CellDynamicModifier() {
224
            @Override
93 ilm 225
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
83 ilm 226
                BigDecimal percent = row.getBigDecimal("POURCENT");
227
 
93 ilm 228
                SQLRowAccessor foreignEcr = row.getForeign("ID_ECRITURE");
229
                if (rowEcr != null) {
230
                    foreignEcr = rowEcr;
231
                }
232
                long total = foreignEcr.getLong("DEBIT") - foreignEcr.getLong("CREDIT");
83 ilm 233
 
234
                BigDecimal montant = percent.movePointLeft(2).multiply(new BigDecimal(total)).setScale(0, RoundingMode.HALF_UP);
235
 
236
                return montant.longValue();
237
            }
238
 
239
        });
240
 
241
        c.gridy++;
242
 
243
        tableElementMontant.setRenderer(this.deviseRenderer);
244
 
245
    }
246
 
93 ilm 247
    public RowValuesTable getTable() {
248
        return table;
249
    }
250
 
83 ilm 251
    public void updateField(final String field, final int id) {
252
        this.table.updateField(field, id);
253
    }
254
 
255
    public void insertFrom(final SQLRowAccessor row) {
93 ilm 256
        this.rowEcr = row;
257
        getDefaultRowValues().putEmptyLink("ID_SAISIE_KM_ELEMENT");
258
        if (rowEcr != null && !rowEcr.isForeignEmpty("ID_MOUVEMENT")) {
259
            SQLRowAccessor r = rowEcr.getForeign("ID_MOUVEMENT");
260
            if (r.getString("SOURCE").equalsIgnoreCase("SAISIE_KM")) {
261
                Collection<? extends SQLRowAccessor> rElt = this.rowEcr.getReferentRows(this.rowEcr.getTable().getTable("SAISIE_KM_ELEMENT"));
262
                if (rElt.size() > 0) {
263
                    getDefaultRowValues().put("ID_SAISIE_KM_ELEMENT", rElt.iterator().next().getID());
264
                }
265
            }
266
            getDefaultRowValues().put("POURCENT", BigDecimal.TEN.movePointRight(1));
267
            getDefaultRowValues().put("MONTANT", rowEcr.getLong("DEBIT") - rowEcr.getLong("CREDIT"));
149 ilm 268
            this.totalArepartir = rowEcr.getLong("DEBIT") - rowEcr.getLong("CREDIT");
93 ilm 269
        }
83 ilm 270
        this.table.insertFrom(row);
271
    }
272
 
273
    public RowValuesTableModel getModel() {
274
        return this.table.getRowValuesTableModel();
275
    }
276
 
93 ilm 277
    public static String getStringAssocs(final SQLRowValues rowVals) {
278
        final StringBuffer buf = new StringBuffer();
279
 
280
        final SQLTable tableElement = elt.getTable();
281
        if (rowVals.getID() > 1) {
282
            final SQLRow row = rowVals.getTable().getRow(rowVals.getID());
283
            final List<SQLRow> rowSet = row.getReferentRows(tableElement);
284
 
285
            for (final SQLRow row2 : rowSet) {
286
                buf.append(getStringAssoc(row2) + ", ");
287
            }
288
        } else {
289
            final Collection<SQLRowValues> colRows = rowVals.getReferentRows();
290
            for (final SQLRowValues rowValues : colRows) {
291
                if (rowValues.getTable().getName().equalsIgnoreCase(tableElement.getName())) {
292
                    buf.append(getStringAssoc(rowValues) + ", ");
293
                }
294
            }
295
        }
296
 
297
        // return buf.append("...").toString().trim();
298
 
299
        String string = buf.toString();
300
        if (string.length() > 2) {
301
            string = string.substring(0, string.length() - 2);
302
        }
303
        return string.trim();
304
    }
305
 
306
    private static String getStringAssoc(final SQLRowAccessor row) {
307
        final StringBuffer buf = new StringBuffer();
308
        final SQLRowAccessor rowVerif = row.getForeign("ID_POSTE_ANALYTIQUE");
309
        if (rowVerif != null) {
310
            buf.append(rowVerif.getString("NOM"));
311
        }
312
        return buf.toString();
313
    }
314
 
83 ilm 315
}