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
144 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.
144 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.supplychain.order.table;
15
 
16
import org.openconcerto.erp.core.common.ui.DeviseNumericCellEditor;
17
import org.openconcerto.erp.core.common.ui.DeviseTableCellRenderer;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.element.SQLComponent;
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLField;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.UndefinedRowValuesCache;
24
import org.openconcerto.sql.view.list.CellDynamicModifier;
25
import org.openconcerto.sql.view.list.RowValuesTable;
26
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
27
import org.openconcerto.sql.view.list.RowValuesTableModel;
28
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
29
import org.openconcerto.sql.view.list.SQLTableElement;
30
import org.openconcerto.ui.DefaultGridBagConstraints;
180 ilm 31
import org.openconcerto.ui.table.XTableColumnModel;
144 ilm 32
import org.openconcerto.utils.DecimalUtils;
33
 
34
import java.awt.GridBagConstraints;
35
import java.awt.GridBagLayout;
149 ilm 36
import java.awt.event.ActionEvent;
144 ilm 37
import java.math.BigDecimal;
38
import java.util.ArrayList;
149 ilm 39
import java.util.Arrays;
180 ilm 40
import java.util.HashMap;
144 ilm 41
import java.util.List;
180 ilm 42
import java.util.Map;
144 ilm 43
import java.util.Vector;
44
 
149 ilm 45
import javax.swing.AbstractAction;
46
import javax.swing.JButton;
144 ilm 47
import javax.swing.JPanel;
48
import javax.swing.JScrollPane;
49
import javax.swing.ScrollPaneConstants;
50
import javax.swing.ToolTipManager;
51
 
52
public class ChiffrageCommandeTable extends JPanel {
53
    private RowValuesTable table;
54
    private RowValuesTableModel model;
55
    private SQLComponent comp;
56
 
57
    public ChiffrageCommandeTable(SQLComponent comp) {
58
        this.comp = comp;
59
        init();
60
        uiInit();
61
    }
62
 
63
    protected void init() {
64
 
65
        final SQLElement e = getSQLElement();
66
 
67
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
68
 
182 ilm 69
        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM")) {
70
            @Override
71
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
72
                if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
73
                    return false;
74
                } else {
75
                    return super.isCellEditable(vals, rowIndex, columnIndex);
76
                }
77
            }
78
        };
144 ilm 79
        list.add(tableElementNom);
177 ilm 80
        if (e.getTable().contains("ID_TYPE_CMD")) {
81
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_TYPE_CMD"));
82
            list.add(cat);
83
        }
84
        if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
85
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_CATEGORIE_HEURE"));
86
            list.add(cat);
87
        }
88
 
144 ilm 89
        final SQLTableElement fam = new SQLTableElement(e.getTable().getField("ID_FAMILLE_ARTICLE"));
90
        list.add(fam);
177 ilm 91
 
144 ilm 92
        final SQLField fieldHA = e.getTable().getField("PA_HT");
93
        final DeviseNumericCellEditor editorPAHT = new DeviseNumericCellEditor(fieldHA);
182 ilm 94
        final SQLTableElement pa = new SQLTableElement(fieldHA, BigDecimal.class, editorPAHT) {
95
            @Override
96
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
97
                if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
98
                    return false;
99
                } else {
100
                    return super.isCellEditable(vals, rowIndex, columnIndex);
101
                }
102
            }
103
        };
180 ilm 104
        DeviseTableCellRenderer renderer = new DeviseTableCellRenderer();
105
        renderer.setHideZeroValue(true);
182 ilm 106
        pa.setRenderer(renderer);
144 ilm 107
        list.add(pa);
108
 
109
        final SQLField fieldPV = e.getTable().getField("PV_HT");
110
        final DeviseNumericCellEditor editorPVHT = new DeviseNumericCellEditor(fieldPV);
111
        final SQLTableElement pv = new SQLTableElement(fieldPV, BigDecimal.class, editorPVHT);
180 ilm 112
        pv.setRenderer(renderer);
144 ilm 113
        list.add(pv);
114
 
115
        SQLTableElement qteU = new SQLTableElement(e.getTable().getField("QTE"), BigDecimal.class) {
116
 
117
            protected Object getDefaultNullValue() {
118
                return BigDecimal.ZERO;
119
            }
182 ilm 120
 
121
            @Override
122
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
123
                if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
124
                    return vals.getObject("ID_CATEGORIE_HEURE") == null || vals.isForeignEmpty("ID_CATEGORIE_HEURE");
125
                } else {
126
                    return super.isCellEditable(vals, rowIndex, columnIndex);
127
                }
128
            }
144 ilm 129
        };
180 ilm 130
        qteU.setRenderer(renderer);
144 ilm 131
        list.add(qteU);
180 ilm 132
 
177 ilm 133
        if (e.getTable().contains("ANT")) {
134
            SQLTableElement ant = new SQLTableElement(e.getTable().getField("ANT"), BigDecimal.class) {
144 ilm 135
 
177 ilm 136
                protected Object getDefaultNullValue() {
137
                    return BigDecimal.ZERO;
138
                }
139
            };
180 ilm 140
            ant.setRenderer(renderer);
177 ilm 141
            list.add(ant);
142
        }
180 ilm 143
 
177 ilm 144
        if (e.getTable().contains("RESTANT")) {
145
            SQLTableElement restant = new SQLTableElement(e.getTable().getField("RESTANT"), BigDecimal.class) {
146
 
147
                protected Object getDefaultNullValue() {
148
                    return BigDecimal.ZERO;
149
                }
150
            };
180 ilm 151
            restant.setRenderer(renderer);
177 ilm 152
            list.add(restant);
153
        }
154
 
144 ilm 155
        final SQLTableElement unit = new SQLTableElement(e.getTable().getField("ID_UNITE_VENTE"));
156
        list.add(unit);
157
 
158
        final SQLField fieldTotalHA = e.getTable().getField("T_PA_HT");
159
        final DeviseNumericCellEditor editorTotalPAHT = new DeviseNumericCellEditor(fieldTotalHA);
160
        final SQLTableElement totalpa = new SQLTableElement(fieldTotalHA, BigDecimal.class, editorTotalPAHT);
180 ilm 161
        totalpa.setRenderer(renderer);
144 ilm 162
        totalpa.setEditable(false);
163
        list.add(totalpa);
164
 
165
        final SQLField fieldTotalPV = e.getTable().getField("T_PV_HT");
166
        final DeviseNumericCellEditor editorTotalPVHT = new DeviseNumericCellEditor(fieldTotalPV);
167
        final SQLTableElement totalpv = new SQLTableElement(fieldTotalPV, BigDecimal.class, editorTotalPVHT);
168
        totalpv.setEditable(false);
180 ilm 169
        totalpv.setRenderer(renderer);
144 ilm 170
        list.add(totalpv);
171
 
172
        final SQLField fieldMarge = e.getTable().getField("MARGE");
173
        final DeviseNumericCellEditor editorMarge = new DeviseNumericCellEditor(fieldMarge);
174
        final SQLTableElement marge = new SQLTableElement(fieldMarge, BigDecimal.class, editorMarge);
175
        marge.setEditable(false);
180 ilm 176
        marge.setRenderer(renderer);
144 ilm 177
        list.add(marge);
178
 
179
        SQLRowValues defautRow = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
180
        this.model = new RowValuesTableModel(e, list, e.getTable().getField("NOM"), false, defautRow);
181
 
182
        this.table = new RowValuesTable(this.model, null, true);
183
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
184
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
185
 
180 ilm 186
        this.table.readState();
187
 
188
        Map<String, Boolean> mapCustom = getCustomVisibilityMap();
189
        if (mapCustom != null) {
190
            for (String string : mapCustom.keySet()) {
191
                setColumnVisible(model.getColumnForField(string), mapCustom.get(string));
192
            }
193
        }
194
 
195
        this.table.writeState();
196
 
144 ilm 197
        // Autocompletion
198
        // AutoCompletionManager m = new AutoCompletionManager(tableElementNom,
199
        // ((ComptaPropsConfiguration)
200
        // Configuration.getInstance()).getSQLBaseSociete().getField("ECHANTILLON.NOM"), this.table,
201
        // this.table.getRowValuesTableModel());
202
        // m.fill("NOM", "NOM");
203
        // m.fill("PV_HT", "PV_HT");
204
 
205
        // // Calcul automatique du total HT
206
        qteU.addModificationListener(totalpv);
207
        pv.addModificationListener(totalpv);
208
        totalpv.setModifier(new CellDynamicModifier() {
209
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
210
                System.out.println("Compute totalHT");
211
 
212
                BigDecimal qte = row.getBigDecimal("QTE");
213
                BigDecimal f = row.getBigDecimal("PV_HT");
214
                if (f == null) {
215
                    return null;
216
                }
217
                BigDecimal r = f.multiply(qte, DecimalUtils.HIGH_PRECISION);
218
                return r;
219
            }
220
        });
221
 
222
        qteU.addModificationListener(totalpa);
223
        pa.addModificationListener(totalpa);
224
        totalpa.setModifier(new CellDynamicModifier() {
225
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
226
                System.out.println("Compute totalHT");
227
 
228
                BigDecimal qte = row.getBigDecimal("QTE");
229
                BigDecimal f = row.getBigDecimal("PA_HT");
230
                if (f == null) {
231
                    return null;
232
                }
233
                BigDecimal r = f.multiply(qte, DecimalUtils.HIGH_PRECISION);
234
                return r;
235
            }
236
 
237
        });
238
        totalpv.addModificationListener(marge);
239
        totalpa.addModificationListener(marge);
240
        marge.setModifier(new CellDynamicModifier() {
241
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
242
 
243
                BigDecimal f = row.getBigDecimal("T_PV_HT");
244
                if (f == null) {
245
                    return null;
246
                }
247
                BigDecimal f2 = row.getBigDecimal("T_PA_HT");
248
                if (f2 == null) {
249
                    return null;
250
                }
251
                BigDecimal r = f.subtract(f2);
252
                return r;
253
            }
254
        });
255
    }
256
 
180 ilm 257
    protected void setColumnVisible(int col, boolean visible) {
258
        if (col >= 0) {
259
            XTableColumnModel columnModel = this.table.getColumnModel();
260
            columnModel.setColumnVisible(columnModel.getColumnByModelIndex(col), visible);
261
        }
262
    }
263
 
264
    public static Map<String, Boolean> map = new HashMap<String, Boolean>();
265
 
266
    protected Map<String, Boolean> getCustomVisibilityMap() {
267
        return map;
268
    }
269
 
144 ilm 270
    /**
271
     *
272
     */
273
    protected void uiInit() {
274
        // Ui init
275
        this.setLayout(new GridBagLayout());
276
        this.setOpaque(false);
277
        final GridBagConstraints c = new DefaultGridBagConstraints();
278
        c.weightx = 1;
279
 
280
        c.gridy++;
281
        c.gridx = 0;
149 ilm 282
        JButton loadDefaultButton = new JButton(new AbstractAction("Charger les valeurs par défaut") {
283
 
284
            @Override
285
            public void actionPerformed(ActionEvent e) {
286
                for (SQLRowValues sqlRowValues : lists) {
287
                    getModel().addRow(sqlRowValues);
288
                }
289
            }
290
        });
291
        final JPanel control = new RowValuesTableControlPanel(this.table, Arrays.asList(loadDefaultButton));
144 ilm 292
        control.setOpaque(false);
293
        this.add(control, c);
294
 
295
        c.gridy++;
296
        c.fill = GridBagConstraints.BOTH;
297
        c.weighty = 1;
298
        c.weightx = 1;
299
        JScrollPane comp = new JScrollPane(this.table);
300
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
301
        this.add(comp, c);
302
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
303
 
304
        for (SQLRowValues sqlRowValues : lists) {
305
            getModel().addRow(sqlRowValues);
306
        }
307
    }
308
 
309
    private static List<SQLRowValues> lists = new ArrayList<SQLRowValues>();
310
 
311
    public static void addDefaultSQLRowValues(SQLRowValues rowVals) {
312
        lists.add(rowVals);
313
    }
314
 
315
    public SQLElement getSQLElement() {
316
        return Configuration.getInstance().getDirectory().getElement("CHIFFRAGE_COMMANDE_CLIENT");
317
    }
318
 
319
    public void updateField(String field, int id) {
320
        this.table.updateField(field, id);
321
    }
322
 
323
    public RowValuesTable getRowValuesTable() {
324
        return this.table;
325
    }
326
 
327
    public void insertFrom(String field, int id) {
328
        this.table.insertFrom(field, id);
329
    }
330
 
331
    public RowValuesTableModel getModel() {
332
        return this.table.getRowValuesTableModel();
333
    }
334
 
335
    public void refreshTable() {
336
        this.table.repaint();
337
    }
338
}