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
18 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.
18 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.common.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
93 ilm 17
import org.openconcerto.erp.core.common.element.StyleSQLElement;
18
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 19
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
61 ilm 20
import org.openconcerto.erp.preferences.DefaultNXProps;
21
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
93 ilm 22
import org.openconcerto.erp.utils.TM;
18 ilm 23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.SQLElement;
25
import org.openconcerto.sql.model.SQLField;
26
import org.openconcerto.sql.model.SQLRow;
19 ilm 27
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 28
import org.openconcerto.sql.model.SQLRowValues;
156 ilm 29
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
18 ilm 30
import org.openconcerto.sql.model.SQLTable;
156 ilm 31
import org.openconcerto.sql.model.Where;
61 ilm 32
import org.openconcerto.sql.preferences.SQLPreferences;
93 ilm 33
import org.openconcerto.sql.view.list.AutoCompletionManager;
18 ilm 34
import org.openconcerto.sql.view.list.RowValuesTable;
35
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
36
import org.openconcerto.sql.view.list.RowValuesTableModel;
37
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
38
import org.openconcerto.sql.view.list.SQLTableElement;
39
import org.openconcerto.ui.DefaultGridBagConstraints;
28 ilm 40
import org.openconcerto.ui.table.XTableColumnModel;
93 ilm 41
import org.openconcerto.utils.DecimalUtils;
83 ilm 42
import org.openconcerto.utils.ExceptionHandler;
18 ilm 43
 
44
import java.awt.GridBagConstraints;
45
import java.awt.GridBagLayout;
46
import java.io.File;
83 ilm 47
import java.math.BigDecimal;
48
import java.sql.SQLException;
49
import java.util.ArrayList;
93 ilm 50
import java.util.Collection;
142 ilm 51
import java.util.Collections;
18 ilm 52
import java.util.Date;
53
import java.util.List;
93 ilm 54
import java.util.Map;
18 ilm 55
import java.util.Set;
56
 
142 ilm 57
import javax.swing.AbstractAction;
18 ilm 58
import javax.swing.JButton;
93 ilm 59
import javax.swing.JOptionPane;
18 ilm 60
import javax.swing.JPanel;
61
import javax.swing.JScrollPane;
65 ilm 62
import javax.swing.ScrollPaneConstants;
18 ilm 63
 
64
public abstract class AbstractArticleItemTable extends JPanel {
65
    protected RowValuesTable table;
67 ilm 66
    protected SQLTableElement totalHT, totalHA;
61 ilm 67
    protected SQLTableElement tableElementTVA;
18 ilm 68
    protected SQLTableElement tableElementTotalTTC;
19 ilm 69
    protected SQLTableElement tableElementTotalDevise;
18 ilm 70
    protected SQLTableElement service, qte, ha;
71
    protected SQLTableElement tableElementPoidsTotal;
142 ilm 72
    protected SQLTableElement tableElementEcoID, tableElementEco, tableElementEcoTotal;
61 ilm 73
    protected SQLTableElement prebilan;
156 ilm 74
    protected SQLRowAccessor rowCatComptable;
93 ilm 75
    private RowValuesTableModel model;
18 ilm 76
    protected SQLRowValues defaultRowVals;
182 ilm 77
    protected List<JButton> buttons = new ArrayList<>();
18 ilm 78
    protected RowValuesTableControlPanel control = null;
19 ilm 79
    private SQLRowAccessor tarif = null;
18 ilm 80
 
142 ilm 81
    public static String SHOW_TOTAL_ECO_CONTRIBUTION = "SHOW_TOTAL_ECO_CONTRIBUTION";
82
    public static String SHOW_ECO_CONTRIBUTION_COLUMNS = "SHOW_ECO_CONTRIBUTION_COLUMNS";
83
 
93 ilm 84
    private Date dateDevise = new Date();
85
    private boolean usedBiasedDevise = true;
86
 
18 ilm 87
    public AbstractArticleItemTable() {
93 ilm 88
        this(null);
18 ilm 89
    }
90
 
91
    public AbstractArticleItemTable(List<JButton> buttons) {
92
        this.buttons = buttons;
93
        init();
94
        uiInit();
95
    }
96
 
156 ilm 97
    public void setRowCatComptable(SQLRowAccessor rowCatComptable) {
98
        this.rowCatComptable = rowCatComptable;
99
    }
100
 
18 ilm 101
    /**
102
     *
103
     */
104
    abstract protected void init();
105
 
93 ilm 106
    protected void setModel(RowValuesTableModel model) {
107
        this.model = model;
108
 
109
    }
110
 
111
    public boolean isUsedBiasedDevise() {
112
        return usedBiasedDevise;
113
    }
114
 
115
    public void setUsedBiasedDevise(boolean usedBiasedDevise) {
116
        this.usedBiasedDevise = usedBiasedDevise;
117
    }
118
 
119
    public void setDateDevise(Date dateDevise) {
120
        if (dateDevise != null) {
121
            this.dateDevise = dateDevise;
122
            refreshDeviseAmount();
123
        }
124
    }
125
 
126
    public Date getDateDevise() {
127
        return dateDevise;
128
    }
129
 
130
    protected abstract void refreshDeviseAmount();
131
 
18 ilm 132
    protected File getConfigurationFile() {
133
        return new File(Configuration.getInstance().getConfDir(), "Table/" + getConfigurationFileName());
134
    }
135
 
136
    /**
137
     *
138
     */
139
    protected void uiInit() {
140
        // Ui init
141
        setLayout(new GridBagLayout());
61 ilm 142
        this.setOpaque(false);
18 ilm 143
        final GridBagConstraints c = new DefaultGridBagConstraints();
144
 
145
        c.weightx = 1;
146
 
182 ilm 147
        this.buttons.add(1, new AjoutDeclinaisonButton(this));
18 ilm 148
        control = new RowValuesTableControlPanel(this.table, this.buttons);
61 ilm 149
        control.setOpaque(false);
18 ilm 150
        this.add(control, c);
151
 
152
        c.gridy++;
153
        c.fill = GridBagConstraints.BOTH;
154
        c.weightx = 1;
155
        c.weighty = 1;
156
        final JScrollPane comp = new JScrollPane(this.table);
65 ilm 157
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
18 ilm 158
        this.add(comp, c);
159
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
160
    }
161
 
162
    /**
163
     * @return the coniguration file to store pref
164
     */
165
    protected abstract String getConfigurationFileName();
166
 
167
    public abstract SQLElement getSQLElement();
168
 
169
    public void updateField(final String field, final int id) {
170
        this.table.updateField(field, id);
171
    }
172
 
173
    public RowValuesTable getRowValuesTable() {
174
        return this.table;
175
    }
176
 
177
    public void insertFrom(final String field, final int id) {
178
        this.table.insertFrom(field, id);
179
 
180
    }
181
 
182
    public RowValuesTableModel getModel() {
183
        return this.table.getRowValuesTableModel();
184
    }
185
 
61 ilm 186
    public SQLTableElement getPrebilanElement() {
187
        return this.prebilan;
188
    }
189
 
18 ilm 190
    public SQLTableElement getPrixTotalHTElement() {
191
        return this.totalHT;
192
    }
193
 
194
    public SQLTableElement getPoidsTotalElement() {
195
        return this.tableElementPoidsTotal;
196
    }
197
 
198
    public SQLTableElement getPrixTotalTTCElement() {
199
        return this.tableElementTotalTTC;
200
    }
201
 
202
    public SQLTableElement getPrixServiceElement() {
203
        return this.service;
204
    }
205
 
206
    public SQLTableElement getQteElement() {
207
        return this.qte;
208
    }
209
 
210
    public SQLTableElement getHaElement() {
211
        return this.ha;
212
    }
213
 
67 ilm 214
    public SQLTableElement getTotalHaElement() {
215
        return this.totalHA;
216
    }
217
 
61 ilm 218
    public SQLTableElement getTVAElement() {
219
        return this.tableElementTVA;
220
    }
221
 
19 ilm 222
    public SQLTableElement getTableElementTotalDevise() {
223
        return this.tableElementTotalDevise;
224
    }
225
 
142 ilm 226
    public SQLTableElement getTableElementTotalEco() {
227
        return this.tableElementEcoTotal;
228
    }
229
 
18 ilm 230
    public void deplacerDe(final int inc) {
231
        final int rowIndex = this.table.getSelectedRow();
232
 
233
        final int dest = this.model.moveBy(rowIndex, inc);
234
        this.table.getSelectionModel().setSelectionInterval(dest, dest);
235
    }
236
 
237
    /**
83 ilm 238
     * @return le poids total de tous les éléments (niveau 1) du tableau
18 ilm 239
     */
240
    public float getPoidsTotal() {
241
 
242
        float poids = 0.0F;
243
        final int poidsTColIndex = this.model.getColumnIndexForElement(this.tableElementPoidsTotal);
244
        if (poidsTColIndex >= 0) {
245
            for (int i = 0; i < this.table.getRowCount(); i++) {
246
                final Number tmp = (Number) this.model.getValueAt(i, poidsTColIndex);
90 ilm 247
                int level = 1;
248
                if (this.model.getRowValuesAt(i).getObject("NIVEAU") != null) {
249
                    level = this.model.getRowValuesAt(i).getInt("NIVEAU");
250
                }
251
                if (tmp != null && level == 1) {
18 ilm 252
                    poids += tmp.floatValue();
253
                }
254
            }
255
        }
256
        return poids;
257
    }
258
 
259
    public void refreshTable() {
260
        this.table.repaint();
261
    }
262
 
263
    public void createArticle(final int id, final SQLElement eltSource) {
264
 
265
        final SQLElement eltArticleTable = getSQLElement();
266
 
267
        final SQLTable tableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
268
 
61 ilm 269
        final boolean modeAvance = DefaultNXProps.getInstance().getBooleanValue("ArticleModeVenteAvance", false);
67 ilm 270
        SQLPreferences prefs = SQLPreferences.getMemCached(tableArticle.getDBRoot());
61 ilm 271
        final boolean createArticle = prefs.getBoolean(GestionArticleGlobalPreferencePanel.CREATE_ARTICLE_AUTO, true);
272
 
156 ilm 273
        if (createArticle) {
18 ilm 274
 
156 ilm 275
            SQLRowValues rowValsToFetch = new SQLRowValues(eltArticleTable.getTable());
276
            rowValsToFetch.putNulls(eltArticleTable.getTable().getFieldsName());
277
            SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsToFetch);
278
            Where w = new Where(eltArticleTable.getTable().getField("CODE"), "!=", "");
279
            w = w.and(new Where(eltArticleTable.getTable().getField("NOM"), "!=", ""));
280
            Where w2 = new Where(eltArticleTable.getTable().getField("ID_ARTICLE"), "!=", tableArticle.getUndefinedID());
281
            w2 = w2.or(new Where(eltArticleTable.getTable().getField("ID_ARTICLE"), "!=", (Object) null));
282
            final List<SQLRowValues> resultNonAssigned = fetcher.fetch(w.and(w2).and(new Where(eltArticleTable.getTable().getForeignKeys(eltSource.getTable()).iterator().next(), "=", id)));
18 ilm 283
 
156 ilm 284
            // On récupére les articles qui composent la table
285
            // final List<SQLRow> listElts =
286
            // eltSource.getTable().getRow(id).getReferentRows(eltArticleTable.getTable());
287
            final Set<SQLField> fields = tableArticle.getFields();
18 ilm 288
 
182 ilm 289
            SQLRow rowCmd = null;
156 ilm 290
            for (final SQLRowAccessor rowElt : resultNonAssigned) {
182 ilm 291
                final SQLRowValues rowArticle = new SQLRowValues(tableArticle);
156 ilm 292
                // final SQLRow foreignRow = rowElt.getForeignRow("ID_ARTICLE");
293
                // if (foreignRow == null || foreignRow.isUndefined()) {
294
                final Set<String> fieldsName = rowElt.getTable().getFieldsName();
295
                // on récupére l'article qui lui correspond
296
 
297
                for (final SQLField field : fields) {
298
 
299
                    final String name = field.getName();
300
                    if (fieldsName.contains(name) && !field.isPrimaryKey()) {
301
                        rowArticle.put(name, rowElt.getObject(name));
302
                    }
18 ilm 303
                }
182 ilm 304
                if (rowElt.getTable().getName().equals("COMMANDE_ELEMENT")) {
305
                    if (rowCmd == null) {
306
                        rowCmd = rowElt.getTable().getForeignTable("ID_COMMANDE").getRow(id);
307
                    }
308
                    rowArticle.put("ID_FOURNISSEUR", rowCmd.getForeignIDNumber("ID_FOURNISSEUR"));
309
 
310
                    SQLRowValues rowValsTarifF = new SQLRowValues(rowCmd.getTable().getTable("ARTICLE_TARIF_FOURNISSEUR"));
311
                    rowValsTarifF.put("ID_FOURNISSEUR", rowCmd.getForeignIDNumber("ID_FOURNISSEUR"));
312
                    rowValsTarifF.put("ID_ARTICLE", rowArticle);
313
                    rowValsTarifF.put("PRIX_ACHAT", rowArticle.getObject("PRIX_METRIQUE_HA_1"));
314
                    rowValsTarifF.put("PRIX_ACHAT_DEVISE_F", rowArticle.getObject("PRIX_METRIQUE_HA_1"));
315
                    rowValsTarifF.put("QTE", 1);
316
                    rowValsTarifF.put("DATE_PRIX", rowCmd.getDate("DATE").getTime());
317
                }
318
 
156 ilm 319
                // crée les articles si il n'existe pas
61 ilm 320
 
156 ilm 321
                int idArt = -1;
322
                if (modeAvance)
323
                    idArt = ReferenceArticleSQLElement.getIdForCNM(rowArticle, createArticle);
324
                else {
325
                    idArt = ReferenceArticleSQLElement.getIdForCN(rowArticle, createArticle);
83 ilm 326
                }
156 ilm 327
                if (createArticle && idArt > 1 && rowElt.isForeignEmpty("ID_ARTICLE")) {
328
                    try {
329
                        rowElt.createEmptyUpdateRow().put("ID_ARTICLE", idArt).update();
330
                    } catch (SQLException e) {
331
                        ExceptionHandler.handle("Erreur lors de l'affectation de l'article crée!", e);
332
                    }
333
                }
334
                // ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
83 ilm 335
            }
18 ilm 336
        }
28 ilm 337
        // }
18 ilm 338
    }
339
 
340
 
341
    public SQLRowValues getDefaultRowValues() {
342
        return this.defaultRowVals;
343
    }
19 ilm 344
 
345
    public SQLRowAccessor getTarif() {
346
        return tarif;
347
    }
348
 
349
    public void setTarif(SQLRowAccessor idTarif, boolean ask) {
350
        this.tarif = idTarif;
132 ilm 351
        // Test si ID_DEVISE est dans la table pour KD
352
        if (this.tarif != null && this.tarif.getTable().contains("ID_DEVISE") && !this.tarif.isForeignEmpty("ID_DEVISE") && this.defaultRowVals != null) {
94 ilm 353
            this.defaultRowVals.put("ID_DEVISE", this.tarif.getForeignID("ID_DEVISE"));
354
        }
19 ilm 355
    }
28 ilm 356
 
357
    protected void setColumnVisible(int col, boolean visible) {
358
        if (col >= 0) {
359
            XTableColumnModel columnModel = this.table.getColumnModel();
360
            columnModel.setColumnVisible(columnModel.getColumnByModelIndex(col), visible);
361
        }
362
    }
83 ilm 363
 
174 ilm 364
    private SQLRowAccessor rowClient = null;
365
 
366
    public SQLRowAccessor getRowClient() {
367
        return rowClient;
368
    }
369
 
370
    public void setClient(SQLRowAccessor rowClient, boolean ask) {
371
        this.rowClient = rowClient;
372
    }
373
 
93 ilm 374
    protected void calculTarifNomenclature() {
83 ilm 375
 
93 ilm 376
        if (this.model.getRowCount() == 0) {
377
            return;
90 ilm 378
        }
93 ilm 379
        final int columnForField = this.model.getColumnForField("NIVEAU");
380
        if (columnForField >= 0) {
381
            checkNiveau();
382
 
383
            int rowCount = this.model.getRowCount();
384
 
385
            for (int niveau = 4; niveau > 1; niveau--) {
386
                int index = rowCount - 1;
387
 
388
                while (index > 0) {
389
 
390
                    BigDecimal prixUnitHT = BigDecimal.ZERO;
391
                    BigDecimal prixUnitHA = BigDecimal.ZERO;
177 ilm 392
                    BigDecimal prixUnitEco = BigDecimal.ZERO;
93 ilm 393
 
394
                    boolean update = false;
395
                    int indexToUpdate = index;
396
                    // Calcul du sous total
397
                    for (int i = index; i >= 0; i--) {
398
                        indexToUpdate = i;
399
                        SQLRowValues rowVals = this.getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
400
                        int niveauCourant = niveau;
401
                        if (rowVals.getObject("NIVEAU") != null) {
402
                            niveauCourant = rowVals.getInt("NIVEAU");
403
                        }
132 ilm 404
                        if (niveauCourant > 0) {
405
                            if (niveauCourant < niveau || niveauCourant == 1) {
406
                                break;
407
                            } else if (niveauCourant == niveau) {
408
                                update = true;
409
                                // Cumul des valeurs
410
                                prixUnitHT = prixUnitHT.add(rowVals.getBigDecimal("PV_HT").multiply(new BigDecimal(rowVals.getInt("QTE"))).multiply(rowVals.getBigDecimal("QTE_UNITAIRE")));
411
                                prixUnitHA = prixUnitHA.add(rowVals.getBigDecimal("PA_HT").multiply(new BigDecimal(rowVals.getInt("QTE"))).multiply(rowVals.getBigDecimal("QTE_UNITAIRE")));
177 ilm 412
                                BigDecimal eco = rowVals.getBigDecimal("ECO_CONTRIBUTION");
413
                                if (eco != null) {
414
                                    prixUnitEco = prixUnitEco.add(eco.multiply(new BigDecimal(rowVals.getInt("QTE"))).multiply(rowVals.getBigDecimal("QTE_UNITAIRE")));
415
                                }
132 ilm 416
                            }
93 ilm 417
                        }
418
                    }
419
                    if (update) {
420
                        final int columnForFieldHA = this.model.getColumnForField("PRIX_METRIQUE_HA_1");
421
                        if (columnForFieldHA >= 0) {
422
                            this.model.setValueAt(prixUnitHA, indexToUpdate, columnForFieldHA);
423
                        }
94 ilm 424
 
425
                        final int columnForFieldPVht1 = this.model.getColumnForField("PRIX_METRIQUE_VT_1");
426
                        if (columnForFieldPVht1 >= 0) {
427
                            this.model.setValueAt(prixUnitHT, indexToUpdate, columnForFieldPVht1);
93 ilm 428
                        }
177 ilm 429
                        final int columnForFieldEco = this.model.getColumnForField("ECO_CONTRIBUTION");
430
                        if (columnForFieldEco >= 0) {
431
                            this.model.setValueAt(prixUnitEco, indexToUpdate, columnForFieldEco);
432
                        }
93 ilm 433
                    }
434
                    index = indexToUpdate - 1;
83 ilm 435
                }
436
            }
93 ilm 437
        }
438
    }
83 ilm 439
 
93 ilm 440
    private void checkNiveau() {
83 ilm 441
 
93 ilm 442
        int n = this.model.getRowCount();
443
        final int columnForField = this.model.getColumnForField("NIVEAU");
444
        if (n > 0 && columnForField >= 0) {
94 ilm 445
            int start = 0;
446
            for (int i = 0; i < n; i++) {
447
                start = i;
448
                SQLRowValues rowVals = this.model.getRowValuesAt(i);
449
                if (rowVals.getObject("NIVEAU") == null || rowVals.getInt("NIVEAU") >= 1) {
450
                    this.model.setValueAt(1, i, columnForField);
451
                    break;
452
                }
93 ilm 453
            }
90 ilm 454
 
93 ilm 455
            // Dernier niveau correct autre que -1
94 ilm 456
            int lastGoodPrevious = this.model.getRowValuesAt(start).getInt("NIVEAU");
457
            for (int i = start + 1; i < n; i++) {
93 ilm 458
                // SQLRowValues rowValsPrev = this.model.getRowValuesAt(i - 1);
459
                SQLRowValues rowVals = this.model.getRowValuesAt(i);
460
                if (rowVals.getObject("NIVEAU") == null) {
461
                    this.model.setValueAt(1, i, columnForField);
462
                }
463
                // int niveauPrev = rowValsPrev.getInt("NIVEAU");
464
                int niveau = rowVals.getInt("NIVEAU");
465
                if (niveau != -1) {
90 ilm 466
 
93 ilm 467
                    if (niveau - lastGoodPrevious > 1) {
468
                        this.model.setValueAt(lastGoodPrevious, i, columnForField);
90 ilm 469
                    }
93 ilm 470
                    lastGoodPrevious = niveau;
83 ilm 471
                }
472
            }
473
        }
474
    }
475
 
476
    public List<SQLRowValues> getRowValuesAtLevel(int level) {
477
        final int rowCount = this.model.getRowCount();
478
        final List<SQLRowValues> result = new ArrayList<SQLRowValues>(rowCount);
479
        for (int i = 0; i < rowCount; i++) {
480
            final SQLRowValues row = this.model.getRowValuesAt(i);
481
            if (row.getObject("NIVEAU") == null || row.getInt("NIVEAU") == level) {
482
                result.add(row);
483
            }
484
        }
485
        return result;
486
    }
93 ilm 487
 
94 ilm 488
    public static enum EXPAND_TYPE {
489
        VIEW_ONLY, EXPAND, FLAT
490
    };
491
 
182 ilm 492
    public AutoCompletionManager getCodeCompletionManager() {
493
        return null;
494
    }
495
 
94 ilm 496
    public void expandNomenclature(int index, AutoCompletionManager m, final EXPAND_TYPE type) {
93 ilm 497
        SQLRowValues rowValsLineFather = this.model.getRowValuesAt(index);
498
        if (!rowValsLineFather.isForeignEmpty("ID_ARTICLE")) {
499
 
94 ilm 500
            if (type == EXPAND_TYPE.EXPAND) {
93 ilm 501
                int a1 = JOptionPane.showConfirmDialog(this.table, TM.tr("product.bom.expand.warning"), "Warning", JOptionPane.OK_CANCEL_OPTION);
502
                if (a1 != JOptionPane.YES_OPTION) {
503
                    return;
504
                }
94 ilm 505
            } else if (type == EXPAND_TYPE.FLAT) {
506
                int a1 = JOptionPane.showConfirmDialog(this.table, TM.tr("product.bom.flatexpand.warning"), "Warning", JOptionPane.OK_CANCEL_OPTION);
507
                if (a1 != JOptionPane.YES_OPTION) {
508
                    return;
509
                }
93 ilm 510
            }
94 ilm 511
 
93 ilm 512
            final int fatherLevel = rowValsLineFather.getInt("NIVEAU");
513
            // Test si il existe déjà des éléments d'un niveau inférieur dans le tableau
514
            if (index < table.getRowCount() - 1) {
515
                SQLRowValues rowValsLineNext = this.model.getRowValuesAt(index + 1);
516
                if (fatherLevel < rowValsLineNext.getInt("NIVEAU")) {
94 ilm 517
                    int a = JOptionPane.showConfirmDialog(this.table, "Cette ligne contient déjà des éléments d'un niveau inférieur. Êtes vous sûr de vouloir éclater la nomenclature?", "Nomenclature",
518
                            JOptionPane.YES_NO_OPTION);
93 ilm 519
                    if (a == JOptionPane.NO_OPTION) {
520
                        return;
521
                    }
522
                }
523
            }
524
 
525
            SQLRowAccessor rowValsArticleFather = rowValsLineFather.getForeign("ID_ARTICLE");
526
            // Elements composant la nomenclature
527
            Collection<? extends SQLRowAccessor> elts = rowValsArticleFather.getReferentRows(rowValsArticleFather.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"));
528
 
529
            if (elts.size() == 0) {
530
                JOptionPane.showMessageDialog(this.table, "Cet article ne contient aucun élément.");
531
            }
532
 
533
            List<? extends SQLRowAccessor> eltsList = new ArrayList<SQLRowAccessor>(elts);
94 ilm 534
            if (type == EXPAND_TYPE.FLAT) {
535
                this.model.putValue(-1, index, "NIVEAU");
536
                // this.model.putValue(allStyleByName.get("Composant"), index, "ID_STYLE");
537
            }
142 ilm 538
            Set<String> fieldsFrom = m.getFieldsFrom();
539
            fieldsFrom.remove("POURCENT_REMISE");
93 ilm 540
            for (int i = eltsList.size() - 1; i >= 0; i--) {
541
                SQLRowAccessor sqlRowArticleChildElement = eltsList.get(i);
542
                final SQLRowAccessor foreignArticleChild = sqlRowArticleChildElement.getForeign("ID_ARTICLE");
156 ilm 543
                if (foreignArticleChild != null && !foreignArticleChild.isUndefined()) {
544
                    final SQLRowValues row2Insert = new SQLRowValues(this.model.getDefaultRowValues());
93 ilm 545
 
156 ilm 546
                    m.fillRowValues(foreignArticleChild, fieldsFrom, row2Insert);
93 ilm 547
 
156 ilm 548
                    // Fill prix total
549
                    row2Insert.put("ID_ARTICLE", foreignArticleChild.getID());
93 ilm 550
 
156 ilm 551
                    row2Insert.put("CODE", foreignArticleChild.getObject("CODE"));
552
                    row2Insert.put("NOM", foreignArticleChild.getObject("NOM"));
93 ilm 553
 
156 ilm 554
                    if (type == EXPAND_TYPE.FLAT) {
555
                        row2Insert.put("QTE", sqlRowArticleChildElement.getInt("QTE") * rowValsLineFather.getInt("QTE"));
556
                    } else {
557
                        row2Insert.put("QTE", sqlRowArticleChildElement.getInt("QTE"));
558
                    }
559
                    if (row2Insert.getTable().contains("POURCENT_REMISE")) {
560
                        row2Insert.put("POURCENT_REMISE", BigDecimal.ZERO);
561
                        row2Insert.put("MONTANT_REMISE", BigDecimal.ZERO);
562
                    }
94 ilm 563
 
156 ilm 564
                    if (type == EXPAND_TYPE.EXPAND) {
565
                        row2Insert.put("NIVEAU", fatherLevel + 1);
566
                    } else if (type == EXPAND_TYPE.VIEW_ONLY) {
567
                        row2Insert.put("NIVEAU", -1);
568
                    } else if (type == EXPAND_TYPE.FLAT) {
569
                        row2Insert.put("NIVEAU", 1);
570
                    }
93 ilm 571
 
156 ilm 572
                    if (type != EXPAND_TYPE.VIEW_ONLY) {
93 ilm 573
 
156 ilm 574
                        if (row2Insert.getTable().contains("T_PA_TTC")) {
575
                            row2Insert.put("PA_HT", row2Insert.getObject("PRIX_METRIQUE_HA_1"));
93 ilm 576
 
156 ilm 577
                            final BigDecimal resultTotalHT = row2Insert.getBigDecimal("PA_HT").multiply(new BigDecimal(row2Insert.getInt("QTE")));
578
                            row2Insert.put("T_PA_HT", resultTotalHT);
93 ilm 579
 
156 ilm 580
                            Float resultTaux = TaxeCache.getCache().getTauxFromId(row2Insert.getForeignID("ID_TAXE"));
93 ilm 581
 
156 ilm 582
                            if (resultTaux == null) {
583
                                SQLRow rowTax = TaxeCache.getCache().getFirstTaxe();
584
                                resultTaux = rowTax.getFloat("TAUX");
585
                            }
93 ilm 586
 
156 ilm 587
                            float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
93 ilm 588
 
156 ilm 589
                            BigDecimal r = resultTotalHT.multiply(BigDecimal.valueOf(taux).movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
94 ilm 590
 
156 ilm 591
                            row2Insert.put("T_PA_TTC", r);
592
                        } else {
593
                            row2Insert.put("PV_HT", row2Insert.getObject("PRIX_METRIQUE_VT_1"));
94 ilm 594
 
156 ilm 595
                            final BigDecimal resultTotalHT = row2Insert.getBigDecimal("PV_HT").multiply(new BigDecimal(row2Insert.getInt("QTE")));
596
                            row2Insert.put("T_PV_HT", resultTotalHT);
94 ilm 597
 
156 ilm 598
                            Float resultTaux = TaxeCache.getCache().getTauxFromId(row2Insert.getForeignID("ID_TAXE"));
94 ilm 599
 
156 ilm 600
                            if (resultTaux == null) {
601
                                SQLRow rowTax = TaxeCache.getCache().getFirstTaxe();
602
                                resultTaux = rowTax.getFloat("TAUX");
603
                            }
94 ilm 604
 
156 ilm 605
                            float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
94 ilm 606
 
156 ilm 607
                            BigDecimal r = resultTotalHT.multiply(BigDecimal.valueOf(taux).movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
94 ilm 608
 
156 ilm 609
                            row2Insert.put("T_PV_TTC", r);
94 ilm 610
 
156 ilm 611
                        }
94 ilm 612
                    }
156 ilm 613
                    Map<String, Integer> allStyleByName = getSQLElement().getDirectory().getElement(StyleSQLElement.class).getAllStyleByName();
614
                    row2Insert.put("ID_STYLE", allStyleByName.get("Composant"));
615
                    this.model.addRowAt(index + 1, row2Insert);
93 ilm 616
                }
617
            }
618
        }
619
    }
142 ilm 620
 
621
    protected List<AbstractAction> getAdditionnalMouseAction(final int rowIndex) {
622
        return Collections.emptyList();
623
    }
624
 
625
    public void insertFromReliquat(List<SQLRowValues> reliquats) {
626
 
627
        for (SQLRowValues reliquat : reliquats) {
628
 
629
            final SQLRowValues row2Insert = new SQLRowValues(getRowValuesTable().getRowValuesTableModel().getDefaultRowValues());
630
 
631
            // Completion depuis l'article trouvé
632
            final SQLRowAccessor article = reliquat.getForeign("ID_ARTICLE").asRow();
633
 
634
            row2Insert.put("ID_ARTICLE", article.getID());
635
            row2Insert.put("CODE", article.getObject("CODE"));
636
            row2Insert.put("NOM", article.getObject("NOM"));
637
 
638
            row2Insert.put("QTE", reliquat.getObject("QTE"));
639
            row2Insert.put("QTE_UNITAIRE", reliquat.getObject("QTE_UNITAIRE"));
640
            row2Insert.put("ID_UNITE_VENTE", reliquat.getForeignID("ID_UNITE_VENTE"));
641
            getRowValuesTable().getRowValuesTableModel().addRowAt(0, row2Insert);
642
 
643
        }
644
    }
18 ilm 645
}