OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 180 | 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;
31
import org.openconcerto.utils.DecimalUtils;
32
 
33
import java.awt.GridBagConstraints;
34
import java.awt.GridBagLayout;
149 ilm 35
import java.awt.event.ActionEvent;
144 ilm 36
import java.math.BigDecimal;
37
import java.util.ArrayList;
149 ilm 38
import java.util.Arrays;
144 ilm 39
import java.util.List;
40
import java.util.Vector;
41
 
149 ilm 42
import javax.swing.AbstractAction;
43
import javax.swing.JButton;
144 ilm 44
import javax.swing.JPanel;
45
import javax.swing.JScrollPane;
46
import javax.swing.ScrollPaneConstants;
47
import javax.swing.ToolTipManager;
48
 
49
public class ChiffrageCommandeTable extends JPanel {
50
    private RowValuesTable table;
51
    private RowValuesTableModel model;
52
    private SQLComponent comp;
53
 
54
    public ChiffrageCommandeTable(SQLComponent comp) {
55
        this.comp = comp;
56
        init();
57
        uiInit();
58
    }
59
 
60
    protected void init() {
61
 
62
        final SQLElement e = getSQLElement();
63
 
64
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
65
 
66
        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM"));
67
        list.add(tableElementNom);
177 ilm 68
        if (e.getTable().contains("ID_TYPE_CMD")) {
69
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_TYPE_CMD"));
70
            list.add(cat);
71
        }
72
        if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
73
            final SQLTableElement cat = new SQLTableElement(e.getTable().getField("ID_CATEGORIE_HEURE"));
74
            list.add(cat);
75
        }
76
 
144 ilm 77
        final SQLTableElement fam = new SQLTableElement(e.getTable().getField("ID_FAMILLE_ARTICLE"));
78
        list.add(fam);
177 ilm 79
 
144 ilm 80
        final SQLField fieldHA = e.getTable().getField("PA_HT");
81
        final DeviseNumericCellEditor editorPAHT = new DeviseNumericCellEditor(fieldHA);
82
        final SQLTableElement pa = new SQLTableElement(fieldHA, BigDecimal.class, editorPAHT);
83
        pa.setRenderer(new DeviseTableCellRenderer());
177 ilm 84
        if (e.getTable().contains("ID_CATEGORIE_HEURE")) {
85
            pa.setEditable(false);
86
        }
144 ilm 87
        list.add(pa);
88
 
89
        final SQLField fieldPV = e.getTable().getField("PV_HT");
90
        final DeviseNumericCellEditor editorPVHT = new DeviseNumericCellEditor(fieldPV);
91
        final SQLTableElement pv = new SQLTableElement(fieldPV, BigDecimal.class, editorPVHT);
92
        pv.setRenderer(new DeviseTableCellRenderer());
93
        list.add(pv);
94
 
95
        SQLTableElement qteU = new SQLTableElement(e.getTable().getField("QTE"), BigDecimal.class) {
96
 
97
            protected Object getDefaultNullValue() {
98
                return BigDecimal.ZERO;
99
            }
100
        };
101
        qteU.setRenderer(new DeviseTableCellRenderer());
102
        list.add(qteU);
177 ilm 103
 
104
        if (e.getTable().contains("ANT")) {
105
            SQLTableElement ant = new SQLTableElement(e.getTable().getField("ANT"), BigDecimal.class) {
144 ilm 106
 
177 ilm 107
                protected Object getDefaultNullValue() {
108
                    return BigDecimal.ZERO;
109
                }
110
            };
111
            ant.setRenderer(new DeviseTableCellRenderer());
112
            list.add(ant);
113
        }
114
 
115
        if (e.getTable().contains("RESTANT")) {
116
            SQLTableElement restant = new SQLTableElement(e.getTable().getField("RESTANT"), BigDecimal.class) {
117
 
118
                protected Object getDefaultNullValue() {
119
                    return BigDecimal.ZERO;
120
                }
121
            };
122
            restant.setRenderer(new DeviseTableCellRenderer());
123
            list.add(restant);
124
        }
125
 
144 ilm 126
        final SQLTableElement unit = new SQLTableElement(e.getTable().getField("ID_UNITE_VENTE"));
127
        list.add(unit);
128
 
129
        final SQLField fieldTotalHA = e.getTable().getField("T_PA_HT");
130
        final DeviseNumericCellEditor editorTotalPAHT = new DeviseNumericCellEditor(fieldTotalHA);
131
        final SQLTableElement totalpa = new SQLTableElement(fieldTotalHA, BigDecimal.class, editorTotalPAHT);
132
        totalpa.setRenderer(new DeviseTableCellRenderer());
133
        totalpa.setEditable(false);
134
        list.add(totalpa);
135
 
136
        final SQLField fieldTotalPV = e.getTable().getField("T_PV_HT");
137
        final DeviseNumericCellEditor editorTotalPVHT = new DeviseNumericCellEditor(fieldTotalPV);
138
        final SQLTableElement totalpv = new SQLTableElement(fieldTotalPV, BigDecimal.class, editorTotalPVHT);
139
        totalpv.setEditable(false);
140
        totalpv.setRenderer(new DeviseTableCellRenderer());
141
        list.add(totalpv);
142
 
143
        final SQLField fieldMarge = e.getTable().getField("MARGE");
144
        final DeviseNumericCellEditor editorMarge = new DeviseNumericCellEditor(fieldMarge);
145
        final SQLTableElement marge = new SQLTableElement(fieldMarge, BigDecimal.class, editorMarge);
146
        marge.setEditable(false);
147
        marge.setRenderer(new DeviseTableCellRenderer());
148
        list.add(marge);
149
 
150
        SQLRowValues defautRow = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
151
        this.model = new RowValuesTableModel(e, list, e.getTable().getField("NOM"), false, defautRow);
152
 
153
        this.table = new RowValuesTable(this.model, null, true);
154
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
155
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
156
 
157
        // Autocompletion
158
        // AutoCompletionManager m = new AutoCompletionManager(tableElementNom,
159
        // ((ComptaPropsConfiguration)
160
        // Configuration.getInstance()).getSQLBaseSociete().getField("ECHANTILLON.NOM"), this.table,
161
        // this.table.getRowValuesTableModel());
162
        // m.fill("NOM", "NOM");
163
        // m.fill("PV_HT", "PV_HT");
164
 
165
        // // Calcul automatique du total HT
166
        qteU.addModificationListener(totalpv);
167
        pv.addModificationListener(totalpv);
168
        totalpv.setModifier(new CellDynamicModifier() {
169
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
170
                System.out.println("Compute totalHT");
171
 
172
                BigDecimal qte = row.getBigDecimal("QTE");
173
                BigDecimal f = row.getBigDecimal("PV_HT");
174
                if (f == null) {
175
                    return null;
176
                }
177
                BigDecimal r = f.multiply(qte, DecimalUtils.HIGH_PRECISION);
178
                return r;
179
            }
180
        });
181
 
182
        qteU.addModificationListener(totalpa);
183
        pa.addModificationListener(totalpa);
184
        totalpa.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("PA_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
        totalpv.addModificationListener(marge);
199
        totalpa.addModificationListener(marge);
200
        marge.setModifier(new CellDynamicModifier() {
201
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
202
 
203
                BigDecimal f = row.getBigDecimal("T_PV_HT");
204
                if (f == null) {
205
                    return null;
206
                }
207
                BigDecimal f2 = row.getBigDecimal("T_PA_HT");
208
                if (f2 == null) {
209
                    return null;
210
                }
211
                BigDecimal r = f.subtract(f2);
212
                return r;
213
            }
214
        });
215
    }
216
 
217
    /**
218
     *
219
     */
220
    protected void uiInit() {
221
        // Ui init
222
        this.setLayout(new GridBagLayout());
223
        this.setOpaque(false);
224
        final GridBagConstraints c = new DefaultGridBagConstraints();
225
        c.weightx = 1;
226
 
227
        c.gridy++;
228
        c.gridx = 0;
149 ilm 229
        JButton loadDefaultButton = new JButton(new AbstractAction("Charger les valeurs par défaut") {
230
 
231
            @Override
232
            public void actionPerformed(ActionEvent e) {
233
                for (SQLRowValues sqlRowValues : lists) {
234
                    getModel().addRow(sqlRowValues);
235
                }
236
            }
237
        });
238
        final JPanel control = new RowValuesTableControlPanel(this.table, Arrays.asList(loadDefaultButton));
144 ilm 239
        control.setOpaque(false);
240
        this.add(control, c);
241
 
242
        c.gridy++;
243
        c.fill = GridBagConstraints.BOTH;
244
        c.weighty = 1;
245
        c.weightx = 1;
246
        JScrollPane comp = new JScrollPane(this.table);
247
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
248
        this.add(comp, c);
249
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
250
 
251
        for (SQLRowValues sqlRowValues : lists) {
252
            getModel().addRow(sqlRowValues);
253
        }
254
    }
255
 
256
    private static List<SQLRowValues> lists = new ArrayList<SQLRowValues>();
257
 
258
    public static void addDefaultSQLRowValues(SQLRowValues rowVals) {
259
        lists.add(rowVals);
260
    }
261
 
262
    public SQLElement getSQLElement() {
263
        return Configuration.getInstance().getDirectory().getElement("CHIFFRAGE_COMMANDE_CLIENT");
264
    }
265
 
266
    public void updateField(String field, int id) {
267
        this.table.updateField(field, id);
268
    }
269
 
270
    public RowValuesTable getRowValuesTable() {
271
        return this.table;
272
    }
273
 
274
    public void insertFrom(String field, int id) {
275
        this.table.insertFrom(field, id);
276
    }
277
 
278
    public RowValuesTableModel getModel() {
279
        return this.table.getRowValuesTableModel();
280
    }
281
 
282
    public void refreshTable() {
283
        this.table.repaint();
284
    }
285
}