OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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