OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 67 | Rev 90 | Go to most recent revision | 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.common.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
61 ilm 18
import org.openconcerto.erp.preferences.DefaultNXProps;
19
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.element.SQLElement;
22
import org.openconcerto.sql.model.SQLField;
23
import org.openconcerto.sql.model.SQLRow;
19 ilm 24
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 25
import org.openconcerto.sql.model.SQLRowValues;
26
import org.openconcerto.sql.model.SQLTable;
61 ilm 27
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 28
import org.openconcerto.sql.view.list.RowValuesTable;
29
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
30
import org.openconcerto.sql.view.list.RowValuesTableModel;
31
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
32
import org.openconcerto.sql.view.list.SQLTableElement;
33
import org.openconcerto.ui.DefaultGridBagConstraints;
28 ilm 34
import org.openconcerto.ui.table.XTableColumnModel;
83 ilm 35
import org.openconcerto.utils.ExceptionHandler;
18 ilm 36
 
37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
39
import java.io.File;
83 ilm 40
import java.math.BigDecimal;
41
import java.sql.SQLException;
42
import java.util.ArrayList;
18 ilm 43
import java.util.Date;
44
import java.util.List;
45
import java.util.Set;
46
 
47
import javax.swing.JButton;
48
import javax.swing.JPanel;
49
import javax.swing.JScrollPane;
65 ilm 50
import javax.swing.ScrollPaneConstants;
18 ilm 51
 
52
public abstract class AbstractArticleItemTable extends JPanel {
53
    protected RowValuesTable table;
67 ilm 54
    protected SQLTableElement totalHT, totalHA;
61 ilm 55
    protected SQLTableElement tableElementTVA;
18 ilm 56
    protected SQLTableElement tableElementTotalTTC;
19 ilm 57
    protected SQLTableElement tableElementTotalDevise;
18 ilm 58
    protected SQLTableElement service, qte, ha;
59
    protected SQLTableElement tableElementPoidsTotal;
61 ilm 60
    protected SQLTableElement prebilan;
18 ilm 61
    protected RowValuesTableModel model;
62
    protected SQLRowValues defaultRowVals;
63
    private List<JButton> buttons = null;
64
    protected RowValuesTableControlPanel control = null;
19 ilm 65
    private SQLRowAccessor tarif = null;
18 ilm 66
 
67
    public AbstractArticleItemTable() {
68
        init();
69
        uiInit();
70
    }
71
 
72
    public AbstractArticleItemTable(List<JButton> buttons) {
73
        this.buttons = buttons;
74
        init();
75
        uiInit();
76
    }
77
 
78
    /**
79
     *
80
     */
81
    abstract protected void init();
82
 
83
    protected File getConfigurationFile() {
84
        return new File(Configuration.getInstance().getConfDir(), "Table/" + getConfigurationFileName());
85
    }
86
 
87
    /**
88
     *
89
     */
90
    protected void uiInit() {
91
        // Ui init
92
        setLayout(new GridBagLayout());
61 ilm 93
        this.setOpaque(false);
18 ilm 94
        final GridBagConstraints c = new DefaultGridBagConstraints();
95
 
96
        c.weightx = 1;
97
 
98
        control = new RowValuesTableControlPanel(this.table, this.buttons);
61 ilm 99
        control.setOpaque(false);
18 ilm 100
        this.add(control, c);
101
 
102
        c.gridy++;
103
        c.fill = GridBagConstraints.BOTH;
104
        c.weightx = 1;
105
        c.weighty = 1;
106
        final JScrollPane comp = new JScrollPane(this.table);
65 ilm 107
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
18 ilm 108
        this.add(comp, c);
109
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
110
    }
111
 
112
    /**
113
     * @return the coniguration file to store pref
114
     */
115
    protected abstract String getConfigurationFileName();
116
 
117
    public abstract SQLElement getSQLElement();
118
 
119
    public void updateField(final String field, final int id) {
120
        this.table.updateField(field, id);
121
    }
122
 
123
    public RowValuesTable getRowValuesTable() {
124
        return this.table;
125
    }
126
 
127
    public void insertFrom(final String field, final int id) {
128
        this.table.insertFrom(field, id);
129
 
130
    }
131
 
132
    public RowValuesTableModel getModel() {
133
        return this.table.getRowValuesTableModel();
134
    }
135
 
61 ilm 136
    public SQLTableElement getPrebilanElement() {
137
        return this.prebilan;
138
    }
139
 
18 ilm 140
    public SQLTableElement getPrixTotalHTElement() {
141
        return this.totalHT;
142
    }
143
 
144
    public SQLTableElement getPoidsTotalElement() {
145
        return this.tableElementPoidsTotal;
146
    }
147
 
148
    public SQLTableElement getPrixTotalTTCElement() {
149
        return this.tableElementTotalTTC;
150
    }
151
 
152
    public SQLTableElement getPrixServiceElement() {
153
        return this.service;
154
    }
155
 
156
    public SQLTableElement getQteElement() {
157
        return this.qte;
158
    }
159
 
160
    public SQLTableElement getHaElement() {
161
        return this.ha;
162
    }
163
 
67 ilm 164
    public SQLTableElement getTotalHaElement() {
165
        return this.totalHA;
166
    }
167
 
61 ilm 168
    public SQLTableElement getTVAElement() {
169
        return this.tableElementTVA;
170
    }
171
 
19 ilm 172
    public SQLTableElement getTableElementTotalDevise() {
173
        return this.tableElementTotalDevise;
174
    }
175
 
18 ilm 176
    public void deplacerDe(final int inc) {
177
        final int rowIndex = this.table.getSelectedRow();
178
 
179
        final int dest = this.model.moveBy(rowIndex, inc);
180
        this.table.getSelectionModel().setSelectionInterval(dest, dest);
181
    }
182
 
183
    /**
83 ilm 184
     * @return le poids total de tous les éléments (niveau 1) du tableau
18 ilm 185
     */
186
    public float getPoidsTotal() {
187
 
188
        float poids = 0.0F;
189
        final int poidsTColIndex = this.model.getColumnIndexForElement(this.tableElementPoidsTotal);
190
        if (poidsTColIndex >= 0) {
191
            for (int i = 0; i < this.table.getRowCount(); i++) {
192
                final Number tmp = (Number) this.model.getValueAt(i, poidsTColIndex);
83 ilm 193
                if (tmp != null && this.model.getRowValuesAt(i).getInt("NIVEAU") == 1) {
18 ilm 194
                    poids += tmp.floatValue();
195
                }
196
            }
197
        }
198
        return poids;
199
    }
200
 
201
    public void refreshTable() {
202
        this.table.repaint();
203
    }
204
 
205
    public void createArticle(final int id, final SQLElement eltSource) {
206
 
207
        final SQLElement eltArticleTable = getSQLElement();
208
 
209
        final SQLTable tableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
210
 
61 ilm 211
        final boolean modeAvance = DefaultNXProps.getInstance().getBooleanValue("ArticleModeVenteAvance", false);
67 ilm 212
        SQLPreferences prefs = SQLPreferences.getMemCached(tableArticle.getDBRoot());
61 ilm 213
        final boolean createArticle = prefs.getBoolean(GestionArticleGlobalPreferencePanel.CREATE_ARTICLE_AUTO, true);
214
 
18 ilm 215
        // On récupére les articles qui composent la table
216
        final List<SQLRow> listElts = eltSource.getTable().getRow(id).getReferentRows(eltArticleTable.getTable());
217
        final SQLRowValues rowArticle = new SQLRowValues(tableArticle);
218
        final Set<SQLField> fields = tableArticle.getFields();
219
 
220
        for (final SQLRow rowElt : listElts) {
28 ilm 221
            // final SQLRow foreignRow = rowElt.getForeignRow("ID_ARTICLE");
222
            // if (foreignRow == null || foreignRow.isUndefined()) {
18 ilm 223
            final Set<String> fieldsName = rowElt.getTable().getFieldsName();
224
            // on récupére l'article qui lui correspond
225
 
226
            for (final SQLField field : fields) {
227
 
228
                final String name = field.getName();
229
                if (fieldsName.contains(name) && !field.isPrimaryKey()) {
230
                    rowArticle.put(name, rowElt.getObject(name));
231
                }
232
            }
233
            // crée les articles si il n'existe pas
61 ilm 234
 
83 ilm 235
            int idArt = -1;
61 ilm 236
            if (modeAvance)
83 ilm 237
                idArt = ReferenceArticleSQLElement.getIdForCNM(rowArticle, createArticle);
61 ilm 238
            else {
83 ilm 239
                idArt = ReferenceArticleSQLElement.getIdForCN(rowArticle, createArticle);
61 ilm 240
            }
83 ilm 241
            if (createArticle && idArt > 1 && rowElt.isForeignEmpty("ID_ARTICLE")) {
242
                try {
243
                    rowElt.createEmptyUpdateRow().put("ID_ARTICLE", idArt).update();
244
                } catch (SQLException e) {
245
                    ExceptionHandler.handle("Erreur lors de l'affectation de l'article crée!", e);
246
                }
247
            }
61 ilm 248
            // ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
18 ilm 249
        }
28 ilm 250
        // }
18 ilm 251
    }
252
 
253
 
254
    public SQLRowValues getDefaultRowValues() {
255
        return this.defaultRowVals;
256
    }
19 ilm 257
 
258
    public SQLRowAccessor getTarif() {
259
        return tarif;
260
    }
261
 
262
    public void setTarif(SQLRowAccessor idTarif, boolean ask) {
263
        this.tarif = idTarif;
264
    }
28 ilm 265
 
266
    protected void setColumnVisible(int col, boolean visible) {
267
        if (col >= 0) {
268
            XTableColumnModel columnModel = this.table.getColumnModel();
269
            columnModel.setColumnVisible(columnModel.getColumnByModelIndex(col), visible);
270
        }
271
    }
83 ilm 272
 
273
    protected void calculTarifNomenclature(int index) {
274
 
275
        int rowCount = this.model.getRowCount();
276
        SQLRowValues rowValsSource = this.getRowValuesTable().getRowValuesTableModel().getRowValuesAt(index);
277
        int niveauSource = rowValsSource.getInt("NIVEAU");
278
        if (niveauSource > 1) {
279
            int startFrom = index;
280
            for (int i = index + 1; i < rowCount; i++) {
281
                SQLRowValues rowVals = this.getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
282
                if (rowVals.getInt("NIVEAU") == niveauSource) {
283
                    startFrom = i;
284
                } else {
285
                    break;
286
                }
287
            }
288
 
289
            if (startFrom == index) {
290
 
291
                int indexToUpdate = index;
292
                BigDecimal prixUnitHT = BigDecimal.ZERO;
293
                BigDecimal prixUnitHA = BigDecimal.ZERO;
294
                for (int i = index; i >= 0; i--) {
295
                    indexToUpdate = i;
296
                    SQLRowValues rowVals = this.getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
297
                    final int niveauCourant = rowVals.getInt("NIVEAU");
298
                    if (niveauCourant < niveauSource) {
299
 
300
                        break;
301
                    } else if (niveauCourant == niveauSource) {
302
                        // Cumul des valeurs
303
                        prixUnitHT = prixUnitHT.add(rowVals.getBigDecimal("PV_HT").multiply(new BigDecimal(rowVals.getInt("QTE"))).multiply(rowVals.getBigDecimal("QTE_UNITAIRE")));
304
                        prixUnitHA = prixUnitHA.add(rowVals.getBigDecimal("PA_HT").multiply(new BigDecimal(rowVals.getInt("QTE"))).multiply(rowVals.getBigDecimal("QTE_UNITAIRE")));
305
                    }
306
                }
307
 
308
                this.model.putValue(prixUnitHA, indexToUpdate, "PRIX_METRIQUE_HA_1");
309
                // this.model.putValue(prixUnitHA, indexToUpdate, "PA_HT");
310
                this.model.putValue(ComptaPropsConfiguration.getInstanceCompta().getRowSociete().getForeignID("ID_DEVISE"), indexToUpdate, "ID_DEVISE");
311
                this.model.putValue(prixUnitHT, indexToUpdate, "PV_U_DEVISE");
312
                this.model.putValue(prixUnitHT, indexToUpdate, "PRIX_METRIQUE_VT_1");
313
                // this.model.putValue(prixUnitHT, indexToUpdate, "PV_HT");
314
                // if (indexToUpdate < rowCount) {
315
                // calculTarifNomenclature(indexToUpdate);
316
                // }
317
            } else {
318
                calculTarifNomenclature(startFrom);
319
            }
320
        }
321
    }
322
 
323
    public List<SQLRowValues> getRowValuesAtLevel(int level) {
324
        final int rowCount = this.model.getRowCount();
325
        final List<SQLRowValues> result = new ArrayList<SQLRowValues>(rowCount);
326
        for (int i = 0; i < rowCount; i++) {
327
            final SQLRowValues row = this.model.getRowValuesAt(i);
328
            if (row.getObject("NIVEAU") == null || row.getInt("NIVEAU") == level) {
329
                result.add(row);
330
            }
331
        }
332
        return result;
333
    }
18 ilm 334
}