OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
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;
93 ilm 17
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
18 ilm 18
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
142 ilm 19
import org.openconcerto.erp.core.sales.pos.io.BarcodeReader;
20
import org.openconcerto.erp.core.sales.pos.ui.BarcodeListener;
18 ilm 21
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
94 ilm 22
import org.openconcerto.erp.core.sales.product.model.ProductComponent;
132 ilm 23
import org.openconcerto.erp.core.sales.product.model.ProductHelper;
24
import org.openconcerto.erp.core.sales.product.model.ProductHelper.TypePrice;
18 ilm 25
import org.openconcerto.erp.core.sales.product.ui.ArticleRowValuesRenderer;
94 ilm 26
import org.openconcerto.erp.core.sales.product.ui.CurrencyWithSymbolRenderer;
19 ilm 27
import org.openconcerto.erp.core.sales.product.ui.QteMultipleRowValuesRenderer;
61 ilm 28
import org.openconcerto.erp.core.sales.product.ui.QteUnitRowValuesRenderer;
174 ilm 29
import org.openconcerto.erp.core.supplychain.stock.element.StockSQLElement;
94 ilm 30
import org.openconcerto.erp.importer.ArrayTableModel;
31
import org.openconcerto.erp.importer.DataImporter;
18 ilm 32
import org.openconcerto.erp.preferences.DefaultNXProps;
61 ilm 33
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 34
import org.openconcerto.sql.Configuration;
35
import org.openconcerto.sql.element.SQLElement;
94 ilm 36
import org.openconcerto.sql.model.FieldPath;
19 ilm 37
import org.openconcerto.sql.model.SQLField;
38
import org.openconcerto.sql.model.SQLRow;
39
import org.openconcerto.sql.model.SQLRowAccessor;
94 ilm 40
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 41
import org.openconcerto.sql.model.SQLRowValues;
94 ilm 42
import org.openconcerto.sql.model.SQLSelect;
18 ilm 43
import org.openconcerto.sql.model.SQLTable;
80 ilm 44
import org.openconcerto.sql.model.UndefinedRowValuesCache;
18 ilm 45
import org.openconcerto.sql.model.Where;
94 ilm 46
import org.openconcerto.sql.model.graph.Path;
61 ilm 47
import org.openconcerto.sql.preferences.SQLPreferences;
83 ilm 48
import org.openconcerto.sql.sqlobject.ITextArticleWithCompletionCellEditor;
61 ilm 49
import org.openconcerto.sql.sqlobject.ITextWithCompletion;
94 ilm 50
import org.openconcerto.sql.users.rights.UserRights;
142 ilm 51
import org.openconcerto.sql.users.rights.UserRightsManager;
174 ilm 52
import org.openconcerto.sql.view.EditFrame;
53
import org.openconcerto.sql.view.EditPanel.EditMode;
18 ilm 54
import org.openconcerto.sql.view.list.AutoCompletionManager;
55
import org.openconcerto.sql.view.list.CellDynamicModifier;
56
import org.openconcerto.sql.view.list.RowValuesTable;
57
import org.openconcerto.sql.view.list.RowValuesTableModel;
58
import org.openconcerto.sql.view.list.SQLTableElement;
93 ilm 59
import org.openconcerto.sql.view.list.SQLTextComboTableCellEditor;
65 ilm 60
import org.openconcerto.sql.view.list.ValidStateChecker;
93 ilm 61
import org.openconcerto.utils.CompareUtils;
90 ilm 62
import org.openconcerto.utils.DecimalUtils;
94 ilm 63
import org.openconcerto.utils.StringUtils;
64
import org.openconcerto.utils.Tuple3;
132 ilm 65
import org.openconcerto.utils.cc.ITransformer;
93 ilm 66
import org.openconcerto.utils.i18n.TranslationManager;
18 ilm 67
 
80 ilm 68
import java.awt.Component;
94 ilm 69
import java.awt.datatransfer.DataFlavor;
70
import java.awt.datatransfer.Transferable;
71
import java.awt.datatransfer.UnsupportedFlavorException;
72
import java.awt.dnd.DnDConstants;
73
import java.awt.dnd.DropTarget;
74
import java.awt.dnd.DropTargetDropEvent;
93 ilm 75
import java.awt.event.ActionEvent;
174 ilm 76
import java.awt.event.ActionListener;
142 ilm 77
import java.awt.event.HierarchyEvent;
78
import java.awt.event.HierarchyListener;
79
import java.awt.event.KeyEvent;
93 ilm 80
import java.awt.event.MouseAdapter;
81
import java.awt.event.MouseEvent;
94 ilm 82
import java.io.File;
83
import java.io.IOException;
19 ilm 84
import java.math.BigDecimal;
67 ilm 85
import java.math.RoundingMode;
18 ilm 86
import java.util.ArrayList;
19 ilm 87
import java.util.Collection;
93 ilm 88
import java.util.Date;
28 ilm 89
import java.util.HashMap;
18 ilm 90
import java.util.List;
28 ilm 91
import java.util.Map;
142 ilm 92
import java.util.Set;
18 ilm 93
import java.util.Vector;
94
 
93 ilm 95
import javax.swing.AbstractAction;
18 ilm 96
import javax.swing.JButton;
142 ilm 97
import javax.swing.JFrame;
80 ilm 98
import javax.swing.JLabel;
174 ilm 99
import javax.swing.JMenu;
100
import javax.swing.JMenuItem;
19 ilm 101
import javax.swing.JOptionPane;
93 ilm 102
import javax.swing.JPopupMenu;
80 ilm 103
import javax.swing.JTable;
104
import javax.swing.SwingUtilities;
18 ilm 105
import javax.swing.ToolTipManager;
93 ilm 106
import javax.swing.event.TableModelEvent;
107
import javax.swing.event.TableModelListener;
80 ilm 108
import javax.swing.table.DefaultTableCellRenderer;
18 ilm 109
import javax.swing.table.TableCellRenderer;
110
 
111
public abstract class AbstractVenteArticleItemTable extends AbstractArticleItemTable {
112
 
21 ilm 113
    public static final String ARTICLE_SHOW_DEVISE = "ArticleShowDevise";
28 ilm 114
    public static final String ARTICLE_SERVICE = "ArticleService";
19 ilm 115
 
94 ilm 116
    public static final String EDIT_PRIX_VENTE_CODE = "CORPS_EDITER_PRIX_VENTE";
117
    public static final String SHOW_PRIX_ACHAT_CODE = "CORPS_VOIR_PRIX_ACHAT";
156 ilm 118
    public static final String SHOW_PRIX_VENTE_CODE = "CORPS_VOIR_PRIX_VENTE";
94 ilm 119
    public static final String LOCK_PRIX_MIN_VENTE_CODE = "CORPS_VERROU_PRIX_MIN_VENTE";
120
 
18 ilm 121
    public AbstractVenteArticleItemTable() {
122
        super();
123
    }
124
 
125
    public AbstractVenteArticleItemTable(List<JButton> buttons) {
126
        super(buttons);
127
    }
128
 
132 ilm 129
    protected boolean isCellNiveauEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
93 ilm 130
        RowValuesTableModel model = getModel();
83 ilm 131
        int niveau = vals.getInt("NIVEAU");
132 ilm 132
        return rowIndex + 1 == model.getRowCount() || niveau >= model.getRowValuesAt(rowIndex + 1).getInt("NIVEAU");
83 ilm 133
    }
134
 
80 ilm 135
    SQLTableElement tableElementFacturable;
94 ilm 136
    protected SQLTableElement tableElementRemise;
80 ilm 137
 
83 ilm 138
    public enum TypeCalcul {
139
        CALCUL_FACTURABLE("MONTANT_FACTURABLE", "POURCENT_FACTURABLE"), CALCUL_REMISE("MONTANT_REMISE", "POURCENT_REMISE");
140
 
141
        String fieldMontant, fieldPourcent;
142
 
143
        TypeCalcul(String fieldMontant, String fieldPourcent) {
144
            this.fieldMontant = fieldMontant;
145
            this.fieldPourcent = fieldPourcent;
146
        }
147
 
148
        public String getFieldMontant() {
149
            return fieldMontant;
150
        }
151
 
152
        public String getFieldPourcent() {
153
            return fieldPourcent;
154
        }
155
    };
156
 
157
    public void calculPourcentage(final Acompte a, final TypeCalcul type) {
80 ilm 158
        Runnable r = new Runnable() {
159
 
160
            @Override
161
            public void run() {
162
                SwingUtilities.invokeLater(new Runnable() {
163
 
164
                    @Override
165
                    public void run() {
83 ilm 166
                        SQLTableElement tableElement = (type == TypeCalcul.CALCUL_FACTURABLE ? tableElementFacturable : tableElementRemise);
93 ilm 167
                        RowValuesTableModel model = getModel();
80 ilm 168
                        if (a == null) {
169
                            for (int i = 0; i < model.getRowCount(); i++) {
83 ilm 170
                                model.putValue(null, i, type.getFieldMontant());
171
                                model.putValue(null, i, type.getFieldPourcent());
172
                                tableElement.fireModification(model.getRowValuesAt(i));
80 ilm 173
                            }
174
                        } else if (a.getPercent() != null) {
175
                            for (int i = 0; i < model.getRowCount(); i++) {
83 ilm 176
                                model.putValue(a.getPercent(), i, type.getFieldPourcent());
177
                                model.putValue(null, i, type.getFieldMontant());
178
                                tableElement.fireModification(model.getRowValuesAt(i));
80 ilm 179
                            }
180
                        } else {
181
                            // FIXME repartition du montant sur chaque ligne
182
                            BigDecimal totalHT = BigDecimal.ZERO;
83 ilm 183
                            for (SQLRowValues rowVals : getRowValuesAtLevel(1)) {
80 ilm 184
                                int qte = rowVals.getInt("QTE");
185
                                BigDecimal qteU = rowVals.getBigDecimal("QTE_UNITAIRE");
186
                                BigDecimal pU = rowVals.getBigDecimal("PV_HT");
187
 
90 ilm 188
                                BigDecimal totalLine = pU.multiply(qteU, DecimalUtils.HIGH_PRECISION).multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION).setScale(2, RoundingMode.HALF_UP);
80 ilm 189
 
83 ilm 190
                                // BigDecimal lremise = (type == TypeCalcul.CALCUL_FACTURABLE ?
191
                                // rowVals.getBigDecimal("POURCENT_REMISE") : BigDecimal.ZERO);
192
                                //
193
                                // if (lremise.compareTo(BigDecimal.ZERO) > 0 &&
194
                                // lremise.compareTo(BigDecimal.valueOf(100)) < 100) {
195
                                // totalLine =
196
                                // totalLine.multiply(BigDecimal.valueOf(100).subtract(lremise),
90 ilm 197
                                // DecimalUtils.HIGH_PRECISION).movePointLeft(2);
83 ilm 198
                                // }
199
                                if (type == TypeCalcul.CALCUL_FACTURABLE) {
200
                                    if (rowVals.getTable().getFieldsName().contains("MONTANT_REMISE")) {
201
                                        final BigDecimal acomptePercent = rowVals.getBigDecimal("POURCENT_REMISE");
202
                                        final BigDecimal acompteMontant = rowVals.getBigDecimal("MONTANT_REMISE");
203
                                        Remise remise = new Remise(acomptePercent, acompteMontant);
204
                                        totalLine = remise.getResultFrom(totalLine);
205
                                    }
80 ilm 206
                                }
207
                                totalHT = totalHT.add(totalLine);
208
                            }
209
 
142 ilm 210
                            // BigDecimal percent = (totalHT.signum() != 0 ?
211
                            // a.getMontant().divide(totalHT, DecimalUtils.HIGH_PRECISION) :
212
                            // BigDecimal.ZERO);
213
 
80 ilm 214
                            for (int i = 0; i < model.getRowCount(); i++) {
83 ilm 215
                                // Restrict to level 1
216
                                if (model.getRowValuesAt(i).getInt("NIVEAU") != 1) {
217
                                    continue;
218
                                }
219
                                model.putValue(null, i, type.getFieldPourcent());
80 ilm 220
                                SQLRowValues rowVals = model.getRowValuesAt(i);
221
                                int qte = rowVals.getInt("QTE");
222
                                BigDecimal qteU = rowVals.getBigDecimal("QTE_UNITAIRE");
223
                                BigDecimal pU = rowVals.getBigDecimal("PV_HT");
224
 
90 ilm 225
                                BigDecimal totalLine = pU.multiply(qteU, DecimalUtils.HIGH_PRECISION).multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION).setScale(2, RoundingMode.HALF_UP);
80 ilm 226
 
83 ilm 227
                                // BigDecimal lremise = (type == TypeCalcul.CALCUL_FACTURABLE ?
228
                                // rowVals.getBigDecimal("POURCENT_REMISE") : BigDecimal.ZERO);
229
                                //
230
                                // if (lremise.compareTo(BigDecimal.ZERO) > 0 &&
231
                                // lremise.compareTo(BigDecimal.valueOf(100)) < 100) {
232
                                // totalLine =
233
                                // totalLine.multiply(BigDecimal.valueOf(100).subtract(lremise),
90 ilm 234
                                // DecimalUtils.HIGH_PRECISION).movePointLeft(2);
83 ilm 235
                                // }
236
                                if (rowVals.getTable().getFieldsName().contains("MONTANT_REMISE")) {
237
                                    final BigDecimal acomptePercent = rowVals.getBigDecimal("POURCENT_REMISE");
238
                                    final BigDecimal acompteMontant = rowVals.getBigDecimal("MONTANT_REMISE");
239
                                    Remise remise = new Remise(acomptePercent, acompteMontant);
240
                                    totalLine = remise.getResultFrom(totalLine);
80 ilm 241
                                }
242
 
90 ilm 243
                                BigDecimal percent = (totalHT.signum() != 0 ? totalLine.divide(totalHT, DecimalUtils.HIGH_PRECISION) : BigDecimal.ZERO);
80 ilm 244
 
90 ilm 245
                                model.putValue(a.getMontant().multiply(percent, DecimalUtils.HIGH_PRECISION).setScale(6, RoundingMode.HALF_UP), i, type.getFieldMontant());
83 ilm 246
                                tableElement.fireModification(model.getRowValuesAt(i));
80 ilm 247
                            }
248
                        }
249
                        model.fireTableDataChanged();
250
                    }
251
                });
252
            }
253
        };
93 ilm 254
        getModel().submit(r);
80 ilm 255
 
256
    }
257
 
28 ilm 258
    private static Map<String, Boolean> visibilityMap = new HashMap<String, Boolean>();
259
 
260
    public static Map<String, Boolean> getVisibilityMap() {
261
        return visibilityMap;
262
    }
263
 
19 ilm 264
    private SQLTable tableArticleTarif = Configuration.getInstance().getBase().getTable("ARTICLE_TARIF");
61 ilm 265
    private SQLTable tableArticle = Configuration.getInstance().getBase().getTable("ARTICLE");
19 ilm 266
 
18 ilm 267
    protected void init() {
268
 
67 ilm 269
        SQLPreferences prefs = SQLPreferences.getMemCached(getSQLElement().getTable().getDBRoot());
61 ilm 270
        final boolean selectArticle = prefs.getBoolean(GestionArticleGlobalPreferencePanel.USE_CREATED_ARTICLE, false);
93 ilm 271
        final boolean filterFamilleArticle = prefs.getBoolean(GestionArticleGlobalPreferencePanel.FILTER_BY_FAMILY, false);
61 ilm 272
        final boolean createAuto = prefs.getBoolean(GestionArticleGlobalPreferencePanel.CREATE_ARTICLE_AUTO, true);
142 ilm 273
        final boolean showEco = prefs.getBoolean(AbstractVenteArticleItemTable.SHOW_ECO_CONTRIBUTION_COLUMNS, false);
156 ilm 274
        final boolean showDevise = prefs.getBoolean(AbstractVenteArticleItemTable.ARTICLE_SHOW_DEVISE, false);
61 ilm 275
 
142 ilm 276
        final UserRights rights = UserRightsManager.getCurrentUserRights();
94 ilm 277
        final boolean editVTPrice = rights.haveRight(EDIT_PRIX_VENTE_CODE);
278
        final boolean showHAPrice = rights.haveRight(SHOW_PRIX_ACHAT_CODE);
279
        final boolean lockVTMinPrice = rights.haveRight(LOCK_PRIX_MIN_VENTE_CODE);
280
 
18 ilm 281
        final SQLElement e = getSQLElement();
282
 
283
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
83 ilm 284
        final SQLTableElement eNiveau = new SQLTableElement(e.getTable().getField("NIVEAU")) {
285
            @Override
286
            public void setValueFrom(SQLRowValues row, Object value) {
287
                super.setValueFrom(row, value);
288
            }
289
        };
290
        eNiveau.setRenderer(new NiveauTableCellRender());
291
        eNiveau.setEditor(new NiveauTableCellEditor());
292
        list.add(eNiveau);
293
 
18 ilm 294
        list.add(new SQLTableElement(e.getTable().getField("ID_STYLE")));
28 ilm 295
 
174 ilm 296
        if (e.getTable().contains("ID_COMPTE_PCE")) {
297
            list.add(new SQLTableElement(e.getTable().getField("ID_COMPTE_PCE")));
298
        }
299
 
93 ilm 300
        final SQLTableElement tableFamille = new SQLTableElement(e.getTable().getField("ID_FAMILLE_ARTICLE"));
301
        list.add(tableFamille);
302
 
61 ilm 303
        // Article
156 ilm 304
        SQLTableElement tableElementDepot = new SQLTableElement(e.getTable().getField("ID_DEPOT_STOCK"), true, true, true);
305
        list.add(tableElementDepot);
306
 
307
        // Article
67 ilm 308
        SQLTableElement tableElementArticle = new SQLTableElement(e.getTable().getField("ID_ARTICLE"), true, true, true);
61 ilm 309
        list.add(tableElementArticle);
28 ilm 310
 
142 ilm 311
        if (e.getTable().getFieldsName().contains("ID_ECO_CONTRIBUTION")) {
312
            this.tableElementEcoID = new SQLTableElement(e.getTable().getField("ID_ECO_CONTRIBUTION"));
313
            list.add(this.tableElementEcoID);
314
        }
315
 
28 ilm 316
        // Code article
94 ilm 317
        final SQLTableElement tableElementCode = new SQLTableElement(e.getTable().getField("CODE"), String.class,
318
                new ITextArticleWithCompletionCellEditor(e.getTable().getTable("ARTICLE"), e.getTable().getTable("ARTICLE_FOURNISSEUR")));
18 ilm 319
        list.add(tableElementCode);
83 ilm 320
 
18 ilm 321
        // Désignation de l'article
322
        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM"));
323
        list.add(tableElementNom);
324
 
41 ilm 325
        if (e.getTable().getFieldsName().contains("COLORIS")) {
326
            final SQLTableElement tableElementColoris = new SQLTableElement(e.getTable().getField("COLORIS"));
327
            list.add(tableElementColoris);
328
        }
329
 
21 ilm 330
        if (e.getTable().getFieldsName().contains("DESCRIPTIF")) {
331
            final SQLTableElement tableElementDesc = new SQLTableElement(e.getTable().getField("DESCRIPTIF"));
332
            list.add(tableElementDesc);
333
        }
334
 
149 ilm 335
        if (e.getTable().getFieldsName().contains("DELAI")) {
336
            final SQLTableElement tableElementDelai = new SQLTableElement(e.getTable().getField("DELAI"));
337
            list.add(tableElementDelai);
338
        }
339
 
156 ilm 340
        if (showDevise) {
19 ilm 341
            // Code Douanier
342
            final SQLTableElement tableElementCodeDouane = new SQLTableElement(e.getTable().getField("CODE_DOUANIER"));
343
            list.add(tableElementCodeDouane);
344
            final SQLTableElement tableElementPays = new SQLTableElement(e.getTable().getField("ID_PAYS"));
345
            list.add(tableElementPays);
346
        }
347
 
18 ilm 348
        // Valeur des métriques
349
        final SQLTableElement tableElement_ValeurMetrique2 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_2"), Float.class) {
350
            @Override
132 ilm 351
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
18 ilm 352
                Number modeNumber = (Number) vals.getObject("ID_MODE_VENTE_ARTICLE");
353
                // int mode = vals.getInt("ID_MODE_VENTE_ARTICLE");
94 ilm 354
                if (modeNumber != null && (modeNumber.intValue() == ReferenceArticleSQLElement.A_LA_PIECE || modeNumber.intValue() == ReferenceArticleSQLElement.AU_POID_METRECARRE
355
                        || modeNumber.intValue() == ReferenceArticleSQLElement.AU_METRE_LONGUEUR)) {
18 ilm 356
                    return false;
357
                } else {
132 ilm 358
                    return super.isCellEditable(vals, rowIndex, columnIndex);
18 ilm 359
                }
360
            }
361
 
362
            @Override
363
            public TableCellRenderer getTableCellRenderer() {
364
 
365
                return new ArticleRowValuesRenderer(null);
366
            }
367
        };
368
        list.add(tableElement_ValeurMetrique2);
369
        final SQLTableElement tableElement_ValeurMetrique3 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_3"), Float.class) {
370
            @Override
132 ilm 371
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
18 ilm 372
 
373
                Number modeNumber = (Number) vals.getObject("ID_MODE_VENTE_ARTICLE");
374
                if (modeNumber != null && (!(modeNumber.intValue() == ReferenceArticleSQLElement.AU_POID_METRECARRE))) {
375
                    return false;
376
                } else {
132 ilm 377
                    return super.isCellEditable(vals, rowIndex, columnIndex);
18 ilm 378
                }
379
            }
380
 
381
            @Override
382
            public TableCellRenderer getTableCellRenderer() {
383
 
384
                return new ArticleRowValuesRenderer(null);
385
            }
386
        };
387
        list.add(tableElement_ValeurMetrique3);
388
        final SQLTableElement tableElement_ValeurMetrique1 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_1"), Float.class) {
389
            @Override
132 ilm 390
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
18 ilm 391
 
392
                Number modeNumber = (Number) vals.getObject("ID_MODE_VENTE_ARTICLE");
94 ilm 393
                if (modeNumber != null && (modeNumber.intValue() == ReferenceArticleSQLElement.A_LA_PIECE || modeNumber.intValue() == ReferenceArticleSQLElement.AU_POID_METRECARRE
394
                        || modeNumber.intValue() == ReferenceArticleSQLElement.AU_METRE_LARGEUR)) {
18 ilm 395
                    return false;
396
                } else {
132 ilm 397
                    return super.isCellEditable(vals, rowIndex, columnIndex);
18 ilm 398
                }
399
            }
400
 
401
            @Override
402
            public TableCellRenderer getTableCellRenderer() {
403
 
404
                return new ArticleRowValuesRenderer(null);
405
            }
406
        };
407
        list.add(tableElement_ValeurMetrique1);
408
 
41 ilm 409
        // Prébilan
61 ilm 410
 
41 ilm 411
        if (e.getTable().getFieldsName().contains("PREBILAN")) {
90 ilm 412
            prebilan = new SQLTableElement(e.getTable().getField("PREBILAN"), BigDecimal.class) {
413
                protected Object getDefaultNullValue() {
414
                    return BigDecimal.ZERO;
415
                }
416
 
417
                @Override
132 ilm 418
                public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
419
                    return isCellNiveauEditable(vals, rowIndex, columnIndex);
90 ilm 420
                }
421
 
422
            };
423
            prebilan.setRenderer(new DeviseTableCellRenderer());
41 ilm 424
            list.add(prebilan);
425
        }
426
 
18 ilm 427
        // Prix d'achat HT de la métrique 1
73 ilm 428
        final SQLTableElement tableElement_PrixMetrique1_AchatHT = new SQLTableElement(e.getTable().getField("PRIX_METRIQUE_HA_1"), BigDecimal.class) {
429
            protected Object getDefaultNullValue() {
430
                return BigDecimal.ZERO;
431
            }
83 ilm 432
 
433
            @Override
132 ilm 434
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
435
                return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 436
            }
437
 
73 ilm 438
        };
94 ilm 439
        tableElement_PrixMetrique1_AchatHT.setRenderer(new CurrencyWithSymbolRenderer());
18 ilm 440
        list.add(tableElement_PrixMetrique1_AchatHT);
441
 
19 ilm 442
        SQLTableElement eltDevise = null;
443
        SQLTableElement eltUnitDevise = null;
156 ilm 444
        if (showDevise) {
19 ilm 445
            // Devise
446
            eltDevise = new SQLTableElement(e.getTable().getField("ID_DEVISE"));
447
            list.add(eltDevise);
448
 
449
            // Prix vente devise
83 ilm 450
            eltUnitDevise = new SQLTableElement(e.getTable().getField("PV_U_DEVISE"), BigDecimal.class) {
451
                @Override
132 ilm 452
                public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
453
                    return editVTPrice && isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 454
                }
132 ilm 455
 
456
                protected Object getDefaultNullValue() {
457
                    return BigDecimal.ZERO;
458
                }
459
 
83 ilm 460
            };
94 ilm 461
            Path p = new Path(getSQLElement().getTable()).addForeignField("ID_DEVISE");
462
            eltUnitDevise.setRenderer(new CurrencyWithSymbolRenderer(new FieldPath(p, "CODE")));
19 ilm 463
            list.add(eltUnitDevise);
464
        }
18 ilm 465
        // Prix de vente HT de la métrique 1
73 ilm 466
 
67 ilm 467
        SQLField field = e.getTable().getField("PRIX_METRIQUE_VT_1");
73 ilm 468
        final DeviseNumericHTConvertorCellEditor editorPVHT = new DeviseNumericHTConvertorCellEditor(field);
18 ilm 469
 
83 ilm 470
        final SQLTableElement tableElement_PrixMetrique1_VenteHT = new SQLTableElement(field, BigDecimal.class, editorPVHT) {
471
            @Override
132 ilm 472
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
473
                return editVTPrice && isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 474
            }
475
        };
94 ilm 476
        tableElement_PrixMetrique1_VenteHT.setRenderer(new CurrencyWithSymbolRenderer());
18 ilm 477
        list.add(tableElement_PrixMetrique1_VenteHT);
19 ilm 478
 
142 ilm 479
        if (e.getTable().getFieldsName().contains("ECO_CONTRIBUTION")) {
480
            this.tableElementEco = new SQLTableElement(e.getTable().getField("ECO_CONTRIBUTION"));
481
            list.add(this.tableElementEco);
482
        }
483
 
18 ilm 484
        // // Prix d'achat HT de la métrique 1
61 ilm 485
 
486
        SQLTableElement qteU = new SQLTableElement(e.getTable().getField("QTE_UNITAIRE"), BigDecimal.class) {
487
            @Override
132 ilm 488
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
61 ilm 489
 
490
                SQLRowAccessor row = vals.getForeign("ID_UNITE_VENTE");
491
                if (row != null && !row.isUndefined() && row.getBoolean("A_LA_PIECE")) {
492
                    return false;
493
                } else {
132 ilm 494
                    return super.isCellEditable(vals, rowIndex, columnIndex);
61 ilm 495
                }
496
            }
497
 
498
            @Override
499
            public TableCellRenderer getTableCellRenderer() {
500
                return new QteUnitRowValuesRenderer();
501
            }
67 ilm 502
 
503
            protected Object getDefaultNullValue() {
504
                return BigDecimal.ZERO;
505
            }
61 ilm 506
        };
507
        list.add(qteU);
508
 
509
        SQLTableElement uniteVente = new SQLTableElement(e.getTable().getField("ID_UNITE_VENTE"));
510
        list.add(uniteVente);
511
 
18 ilm 512
        // Quantité
21 ilm 513
        this.qte = new SQLTableElement(e.getTable().getField("QTE"), Integer.class, new QteCellEditor()) {
19 ilm 514
            protected Object getDefaultNullValue() {
515
                return Integer.valueOf(0);
516
            }
517
 
518
            public TableCellRenderer getTableCellRenderer() {
519
                if (getSQLElement().getTable().getFieldsName().contains("QTE_ACHAT")) {
520
                    return new QteMultipleRowValuesRenderer();
521
                } else {
522
                    return super.getTableCellRenderer();
523
                }
524
            }
525
        };
18 ilm 526
        this.qte.setPreferredSize(20);
527
        list.add(this.qte);
528
 
132 ilm 529
        if (e.getTable().contains("RETOUR_STOCK")) {
530
            list.add(new SQLTableElement(e.getTable().getField("RETOUR_STOCK")));
531
        }
532
 
18 ilm 533
        // Mode de vente
534
        final SQLTableElement tableElement_ModeVente = new SQLTableElement(e.getTable().getField("ID_MODE_VENTE_ARTICLE"));
535
        list.add(tableElement_ModeVente);
536
 
537
        // // Prix d'achat unitaire HT
73 ilm 538
 
539
        final SQLField prixAchatHTField = e.getTable().getField("PA_HT");
540
        final DeviseNumericCellEditor editorPAchatHT = new DeviseNumericCellEditor(prixAchatHTField);
541
        this.ha = new SQLTableElement(e.getTable().getField("PA_HT"), BigDecimal.class, editorPAchatHT) {
542
            protected Object getDefaultNullValue() {
543
                return BigDecimal.ZERO;
544
            }
83 ilm 545
 
546
            @Override
132 ilm 547
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
548
                return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 549
            }
73 ilm 550
        };
83 ilm 551
        this.ha = new SQLTableElement(prixAchatHTField, BigDecimal.class, editorPAchatHT) {
552
            @Override
132 ilm 553
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
554
                return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 555
            }
556
        };
94 ilm 557
        this.ha.setRenderer(new CurrencyWithSymbolRenderer());
73 ilm 558
 
18 ilm 559
        list.add(this.ha);
560
 
561
        // Prix de vente unitaire HT
73 ilm 562
        final SQLField prixVenteHTField = e.getTable().getField("PV_HT");
563
        final DeviseNumericCellEditor editorPVenteHT = new DeviseNumericCellEditor(prixAchatHTField);
83 ilm 564
        final SQLTableElement tableElement_PrixVente_HT = new SQLTableElement(prixVenteHTField, BigDecimal.class, editorPVenteHT) {
565
            @Override
132 ilm 566
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
567
                return editVTPrice && isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 568
            }
569
        };
94 ilm 570
        tableElement_PrixVente_HT.setRenderer(new CurrencyWithSymbolRenderer());
18 ilm 571
        list.add(tableElement_PrixVente_HT);
572
 
573
        // TVA
61 ilm 574
        this.tableElementTVA = new SQLTableElement(e.getTable().getField("ID_TAXE"));
575
        this.tableElementTVA.setPreferredSize(20);
576
        list.add(this.tableElementTVA);
18 ilm 577
        // Poids piece
80 ilm 578
        SQLTableElement tableElementPoids = new SQLTableElement(e.getTable().getField("POIDS"), Float.class) {
579
            protected Object getDefaultNullValue() {
580
                return 0F;
581
            }
582
 
583
            @Override
584
            public TableCellRenderer getTableCellRenderer() {
585
                return new QteUnitRowValuesRenderer();
586
            }
587
 
588
        };
18 ilm 589
        tableElementPoids.setPreferredSize(20);
590
        list.add(tableElementPoids);
591
 
592
        // Poids total
80 ilm 593
        this.tableElementPoidsTotal = new SQLTableElement(e.getTable().getField("T_POIDS"), Float.class) {
594
            @Override
595
            public TableCellRenderer getTableCellRenderer() {
596
                return new QteUnitRowValuesRenderer();
597
            }
598
        };
599
        this.tableElementPoidsTotal.setEditable(false);
18 ilm 600
        list.add(this.tableElementPoidsTotal);
601
 
80 ilm 602
        // Packaging
603
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.ITEM_PACKAGING, false)) {
604
 
605
            SQLTableElement poidsColis = new SQLTableElement(e.getTable().getField("POIDS_COLIS_NET"), BigDecimal.class) {
606
                @Override
607
                public TableCellRenderer getTableCellRenderer() {
608
                    return new QteUnitRowValuesRenderer();
609
                }
610
 
611
            };
612
            list.add(poidsColis);
613
 
614
            SQLTableElement nbColis = new SQLTableElement(e.getTable().getField("NB_COLIS"), Integer.class);
615
            list.add(nbColis);
616
 
617
            final SQLTableElement totalPoidsColis = new SQLTableElement(e.getTable().getField("T_POIDS_COLIS_NET"), BigDecimal.class) {
618
                @Override
619
                public TableCellRenderer getTableCellRenderer() {
620
                    return new QteUnitRowValuesRenderer();
621
                }
622
 
623
            };
624
            list.add(totalPoidsColis);
625
 
626
            poidsColis.addModificationListener(totalPoidsColis);
627
            nbColis.addModificationListener(totalPoidsColis);
628
            totalPoidsColis.setModifier(new CellDynamicModifier() {
93 ilm 629
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
80 ilm 630
                    final Object o2 = row.getObject("POIDS_COLIS_NET");
631
                    final Object o3 = row.getObject("NB_COLIS");
632
                    if (o2 != null && o3 != null) {
633
                        BigDecimal poids = (BigDecimal) o2;
634
                        int nb = (Integer) o3;
90 ilm 635
                        return poids.multiply(new BigDecimal(nb), DecimalUtils.HIGH_PRECISION).setScale(totalPoidsColis.getDecimalDigits(), RoundingMode.HALF_UP);
80 ilm 636
                    } else {
637
                        return row.getObject("T_POIDS_COLIS_NET");
638
                    }
639
                }
640
            });
641
 
642
        }
643
 
18 ilm 644
        // Service
19 ilm 645
        if (DefaultNXProps.getInstance().getBooleanValue(ARTICLE_SERVICE, false)) {
18 ilm 646
            this.service = new SQLTableElement(e.getTable().getField("SERVICE"), Boolean.class);
647
            list.add(this.service);
648
        }
649
 
83 ilm 650
        this.totalHT = new SQLTableElement(e.getTable().getField("T_PV_HT"), BigDecimal.class) {
651
            @Override
132 ilm 652
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
653
                return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 654
            }
655
        };
174 ilm 656
        CurrencyWithSymbolRenderer totalRenderer = new CurrencyWithSymbolRenderer();
657
        totalRenderer.setHideZeroValue(true);
658
        this.totalHT.setRenderer(totalRenderer);
61 ilm 659
        this.totalHT.setEditable(false);
80 ilm 660
        if (e.getTable().getFieldsName().contains("MONTANT_FACTURABLE")) {
661
            // SQLTableElement tableElementAcompte = new
662
            // SQLTableElement(e.getTable().getField("POURCENT_ACOMPTE"));
663
            // list.add(tableElementAcompte);
664
 
665
            this.tableElementFacturable = new SQLTableElement(e.getTable().getField("POURCENT_FACTURABLE"), Acompte.class, new AcompteCellEditor("POURCENT_FACTURABLE", "MONTANT_FACTURABLE")) {
666
                @Override
667
                public void setValueFrom(SQLRowValues row, Object value) {
668
 
669
                    if (value != null) {
670
                        Acompte a = (Acompte) value;
671
                        row.put("MONTANT_FACTURABLE", a.getMontant());
672
                        row.put("POURCENT_FACTURABLE", a.getPercent());
673
                    } else {
674
                        row.put("MONTANT_FACTURABLE", null);
675
                        row.put("POURCENT_FACTURABLE", BigDecimal.ONE.movePointRight(2));
676
                    }
677
                    fireModification(row);
678
                }
679
            };
680
            tableElementFacturable.setRenderer(new DefaultTableCellRenderer() {
681
                @Override
682
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
683
                    SQLRowValues rowVals = ((RowValuesTable) table).getRowValuesTableModel().getRowValuesAt(row);
684
                    JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
685
                    BigDecimal percent = rowVals.getBigDecimal("POURCENT_FACTURABLE");
686
                    BigDecimal amount = rowVals.getBigDecimal("MONTANT_FACTURABLE");
687
                    Acompte a = new Acompte(percent, amount);
132 ilm 688
                    label.setText(a.toPlainString(true));
80 ilm 689
                    return label;
690
                }
691
            });
692
            tableElementFacturable.addModificationListener(this.totalHT);
693
            list.add(tableElementFacturable);
28 ilm 694
        }
73 ilm 695
 
83 ilm 696
        final SQLField fieldRemise = e.getTable().getField("POURCENT_REMISE");
697
 
698
        if (e.getTable().getFieldsName().contains("MONTANT_REMISE")) {
699
            tableElementRemise = new SQLTableElement(e.getTable().getField("POURCENT_REMISE"), Acompte.class, new AcompteCellEditor("POURCENT_REMISE", "MONTANT_REMISE")) {
700
                @Override
701
                public void setValueFrom(SQLRowValues row, Object value) {
702
 
703
                    if (value != null) {
704
                        Acompte a = (Acompte) value;
705
                        row.put("MONTANT_REMISE", a.getMontant());
706
                        row.put("POURCENT_REMISE", a.getPercent());
707
                    } else {
708
                        row.put("MONTANT_REMISE", null);
709
                        row.put("POURCENT_REMISE", BigDecimal.ZERO);
710
                    }
711
                    fireModification(row);
712
                }
713
            };
714
            tableElementRemise.setRenderer(new DefaultTableCellRenderer() {
715
                @Override
716
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
717
                    SQLRowValues rowVals = ((RowValuesTable) table).getRowValuesTableModel().getRowValuesAt(row);
718
                    JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
719
                    BigDecimal percent = rowVals.getBigDecimal("POURCENT_REMISE");
720
                    BigDecimal amount = rowVals.getBigDecimal("MONTANT_REMISE");
721
                    Remise a = new Remise(percent, amount);
132 ilm 722
                    label.setText(a.toPlainString(true));
83 ilm 723
                    return label;
724
                }
725
            });
726
        } else {
727
            tableElementRemise = new SQLTableElement(fieldRemise) {
728
                protected Object getDefaultNullValue() {
729
                    return BigDecimal.ZERO;
730
                }
731
            };
732
        }
19 ilm 733
        list.add(tableElementRemise);
28 ilm 734
        SQLTableElement tableElementRG = null;
735
        if (e.getTable().getFieldsName().contains("POURCENT_RG")) {
736
            tableElementRG = new SQLTableElement(e.getTable().getField("POURCENT_RG"));
737
            list.add(tableElementRG);
738
        }
739
 
18 ilm 740
        // Total HT
67 ilm 741
        this.totalHA = new SQLTableElement(e.getTable().getField("T_PA_HT"), BigDecimal.class);
174 ilm 742
        this.totalHA.setRenderer(totalRenderer);
67 ilm 743
        this.totalHA.setEditable(false);
744
        list.add(this.totalHA);
18 ilm 745
 
156 ilm 746
        if (showDevise) {
19 ilm 747
            // Total HT
83 ilm 748
            this.tableElementTotalDevise = new SQLTableElement(e.getTable().getField("PV_T_DEVISE"), BigDecimal.class) {
749
                @Override
132 ilm 750
                public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
751
                    return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 752
                }
753
            };
94 ilm 754
            Path p = new Path(getSQLElement().getTable()).addForeignField("ID_DEVISE");
174 ilm 755
            CurrencyWithSymbolRenderer currencyRenderer = new CurrencyWithSymbolRenderer(new FieldPath(p, "CODE"));
756
            currencyRenderer.setHideZeroValue(true);
757
            this.tableElementTotalDevise.setRenderer(currencyRenderer);
758
            list.add(this.tableElementTotalDevise);
19 ilm 759
        }
21 ilm 760
 
761
        // Marge HT
762
        if (e.getTable().getFieldsName().contains("MARGE_HT")) {
763
 
90 ilm 764
            final SQLTableElement marge = new SQLTableElement(e.getTable().getField("MARGE_HT"), BigDecimal.class) {
765
                protected Object getDefaultNullValue() {
766
                    return BigDecimal.ZERO;
767
                }
768
 
769
                @Override
132 ilm 770
                public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
771
                    return isCellNiveauEditable(vals, rowIndex, columnIndex);
90 ilm 772
                }
773
 
774
            };
174 ilm 775
            marge.setRenderer(totalRenderer);
21 ilm 776
            marge.setEditable(false);
777
            list.add(marge);
778
            this.totalHT.addModificationListener(marge);
67 ilm 779
            this.totalHA.addModificationListener(marge);
21 ilm 780
            marge.setModifier(new CellDynamicModifier() {
781
                @Override
93 ilm 782
                public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
21 ilm 783
 
67 ilm 784
                    BigDecimal vt = (BigDecimal) row.getObject("T_PV_HT");
21 ilm 785
 
67 ilm 786
                    BigDecimal ha = (BigDecimal) row.getObject("T_PA_HT");
21 ilm 787
 
80 ilm 788
                    final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_FACTURABLE");
789
                    final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_FACTURABLE");
790
                    Acompte acompte = new Acompte(acomptePercent, acompteMontant);
791
                    ha = acompte.getResultFrom(ha);
792
                    vt = acompte.getResultFrom(vt);
28 ilm 793
 
67 ilm 794
                    return vt.subtract(ha).setScale(marge.getDecimalDigits(), RoundingMode.HALF_UP);
21 ilm 795
                }
796
 
797
            });
798
 
799
        }
800
 
61 ilm 801
        if (e.getTable().getFieldsName().contains("MARGE_PREBILAN_HT")) {
802
 
90 ilm 803
            final SQLTableElement marge = new SQLTableElement(e.getTable().getField("MARGE_PREBILAN_HT"), BigDecimal.class) {
804
                protected Object getDefaultNullValue() {
805
                    return BigDecimal.ZERO;
806
                }
807
            };
808
            marge.setRenderer(new DeviseTableCellRenderer());
61 ilm 809
            marge.setEditable(false);
810
            list.add(marge);
811
            this.totalHT.addModificationListener(marge);
812
            prebilan.addModificationListener(marge);
813
            marge.setModifier(new CellDynamicModifier() {
814
                @Override
93 ilm 815
                public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
61 ilm 816
 
67 ilm 817
                    BigDecimal vt = (BigDecimal) row.getObject("T_PV_HT");
61 ilm 818
 
67 ilm 819
                    BigDecimal ha = row.getObject("PREBILAN") == null ? BigDecimal.ZERO : (BigDecimal) row.getObject("PREBILAN");
61 ilm 820
 
80 ilm 821
                    final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_FACTURABLE");
822
                    final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_FACTURABLE");
823
                    Acompte acompte = new Acompte(acomptePercent, acompteMontant);
824
                    ha = acompte.getResultFrom(ha);
825
                    vt = acompte.getResultFrom(vt);
67 ilm 826
                    return vt.subtract(ha).setScale(marge.getDecimalDigits(), RoundingMode.HALF_UP);
61 ilm 827
                }
828
 
829
            });
830
 
831
        }
832
 
142 ilm 833
        if (e.getTable().getFieldsName().contains("T_ECO_CONTRIBUTION")) {
834
            this.tableElementEcoTotal = new SQLTableElement(e.getTable().getField("T_ECO_CONTRIBUTION"));
835
            list.add(this.tableElementEcoTotal);
836
        }
837
 
18 ilm 838
        // Total HT
73 ilm 839
 
67 ilm 840
        this.totalHT.setEditable(false);
18 ilm 841
        list.add(this.totalHT);
842
        // Total TTC
65 ilm 843
        // FIXME add a modifier -> T_TTC modify P_VT_METRIQUE_1 + fix CellDynamicModifier not fire
844
        // if value not changed
83 ilm 845
        this.tableElementTotalTTC = new SQLTableElement(e.getTable().getField("T_PV_TTC"), BigDecimal.class) {
846
            @Override
132 ilm 847
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
848
                return isCellNiveauEditable(vals, rowIndex, columnIndex);
83 ilm 849
            }
850
        };
174 ilm 851
        this.tableElementTotalTTC.setRenderer(totalRenderer);
67 ilm 852
        this.tableElementTotalTTC.setEditable(false);
18 ilm 853
        list.add(this.tableElementTotalTTC);
854
 
94 ilm 855
        this.defaultRowVals = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
856
        defaultRowVals.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
857
        defaultRowVals.put("CODE", "");
858
        defaultRowVals.put("NOM", "");
156 ilm 859
        final RowValuesTableModel model = new RowValuesTableModel(e, list, e.getTable().getField("ID_TAXE"), false, defaultRowVals) {
860
            @Override
861
            public void commitData() {
862
                int size = getRowCount();
863
                for (int i = 0; i < size; i++) {
864
                    SQLRowValues rowVals = getRowValuesAt(i);
865
                    if (rowVals.getObject("PV_U_DEVISE") == null) {
866
                        rowVals.put("PV_U_DEVISE", rowVals.getObject("PRIX_METRIQUE_VT_1"));
867
                        final BigDecimal globalQty = rowVals.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(rowVals.getInt("QTE")));
868
                        rowVals.put("PV_T_DEVISE", rowVals.getBigDecimal("PRIX_METRIQUE_VT_1").multiply(globalQty));
869
                    }
870
                }
871
                super.commitData();
872
            }
873
        };
93 ilm 874
        setModel(model);
18 ilm 875
 
93 ilm 876
        this.table = new RowValuesTable(model, getConfigurationFile());
18 ilm 877
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
878
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
879
 
174 ilm 880
        this.table.getTableHeader().addMouseListener(new MouseAdapter() {
881
            @Override
882
            public void mousePressed(MouseEvent e) {
883
                if (e.isPopupTrigger()) {
884
                    displayPopupMenu(e);
885
                }
886
            }
887
 
888
            @Override
889
            public void mouseReleased(MouseEvent e) {
890
                if (e.isPopupTrigger()) {
891
                    displayPopupMenu(e);
892
                }
893
            }
894
 
895
            private void displayPopupMenu(MouseEvent e) {
896
                JPopupMenu menu = new JPopupMenu();
897
 
898
                TaxeCache cache = TaxeCache.getCache();
899
                Set<SQLRowAccessor> taxes = cache.getAllTaxe();
900
                JMenu subMenuTVA = new JMenu("Appliquer une TVA spécifique sur toutes les lignes");
901
                menu.add(subMenuTVA);
902
                for (SQLRowAccessor taxe : taxes) {
903
 
904
                    subMenuTVA.add(new JMenuItem(new AbstractAction(taxe.getFloat("TAUX") + "%") {
905
 
906
                        @Override
907
                        public void actionPerformed(ActionEvent e) {
908
                            for (int i = 0; i < AbstractVenteArticleItemTable.this.table.getRowCount(); i++) {
909
                                AbstractVenteArticleItemTable.this.table.getRowValuesTableModel().putValue(taxe.getID(), i, "ID_TAXE");
910
                            }
911
                        }
912
                    }));
913
                }
914
                menu.show(e.getComponent(), e.getX(), e.getY());
915
            }
916
        });
917
 
93 ilm 918
        if (filterFamilleArticle) {
919
            ((SQLTextComboTableCellEditor) tableElementArticle.getTableCellEditor(this.table)).setDynamicWhere(e.getTable().getTable("ARTICLE").getField("ID_FAMILLE_ARTICLE"));
920
        }
921
 
18 ilm 922
        // Autocompletion
93 ilm 923
        final SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
28 ilm 924
        List<String> completionField = new ArrayList<String>();
142 ilm 925
        if (e.getTable().getFieldsName().contains("ID_ECO_CONTRIBUTION")) {
926
            completionField.add("ID_ECO_CONTRIBUTION");
927
        }
156 ilm 928
        if (showDevise) {
28 ilm 929
            completionField.add("CODE_DOUANIER");
930
            completionField.add("ID_PAYS");
19 ilm 931
        }
156 ilm 932
        completionField.add("POURCENT_REMISE");
61 ilm 933
        completionField.add("ID_UNITE_VENTE");
156 ilm 934
        completionField.add("QTE_UNITAIRE");
28 ilm 935
        completionField.add("PA_HT");
936
        completionField.add("PV_HT");
937
        completionField.add("ID_TAXE");
938
        completionField.add("POIDS");
939
        completionField.add("PRIX_METRIQUE_HA_1");
940
        completionField.add("PRIX_METRIQUE_HA_2");
941
        completionField.add("PRIX_METRIQUE_HA_3");
942
        completionField.add("VALEUR_METRIQUE_1");
943
        completionField.add("VALEUR_METRIQUE_2");
944
        completionField.add("VALEUR_METRIQUE_3");
945
        completionField.add("ID_MODE_VENTE_ARTICLE");
946
        completionField.add("PRIX_METRIQUE_VT_1");
947
        completionField.add("PRIX_METRIQUE_VT_2");
948
        completionField.add("PRIX_METRIQUE_VT_3");
949
        completionField.add("SERVICE");
93 ilm 950
        completionField.add("ID_FAMILLE_ARTICLE");
21 ilm 951
        if (getSQLElement().getTable().getFieldsName().contains("DESCRIPTIF")) {
28 ilm 952
            completionField.add("DESCRIPTIF");
21 ilm 953
        }
156 ilm 954
        if (showDevise) {
28 ilm 955
            completionField.add("ID_DEVISE");
956
            completionField.add("PV_U_DEVISE");
19 ilm 957
        }
958
        if (getSQLElement().getTable().getFieldsName().contains("QTE_ACHAT") && sqlTableArticle.getTable().getFieldsName().contains("QTE_ACHAT")) {
28 ilm 959
            completionField.add("QTE_ACHAT");
19 ilm 960
        }
18 ilm 961
 
28 ilm 962
        final AutoCompletionManager m = new AutoCompletionManager(tableElementCode, sqlTableArticle.getField("CODE"), this.table, this.table.getRowValuesTableModel()) {
93 ilm 963
 
28 ilm 964
            @Override
93 ilm 965
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
966
                Object res = tarifCompletion(row, field, rowDest, true);
28 ilm 967
                if (res == null) {
174 ilm 968
                    res = super.getValueFrom(row, field, rowDest);
28 ilm 969
                }
174 ilm 970
                if (field.equals("POURCENT_REMISE")) {
971
                    return getRemiseClient(row);
972
                }
973
                return res;
28 ilm 974
            }
142 ilm 975
 
28 ilm 976
        };
977
        m.fill("NOM", "NOM");
61 ilm 978
        m.fill("ID", "ID_ARTICLE");
28 ilm 979
        for (String string : completionField) {
980
            m.fill(string, string);
981
        }
982
 
132 ilm 983
        ITransformer<SQLSelect, SQLSelect> selTrans = new ITransformer<SQLSelect, SQLSelect>() {
984
            @Override
985
            public SQLSelect transformChecked(SQLSelect input) {
986
 
149 ilm 987
                // FIXME utiliser le stock sélectionné sur la ligne et non le stock par défaut de
988
                // l'article
132 ilm 989
                final SQLTable tableStock = sqlTableArticle.getTable("STOCK");
990
                input.andWhere(new Where(tableStock.getKey(), "=", sqlTableArticle.getField("ID_STOCK")));
991
                input.setExcludeUndefined(false, tableStock);
156 ilm 992
                Where w = new Where(sqlTableArticle.getField("OBSOLETE"), "=", Boolean.FALSE)
993
                        .or(new Where(input.getAlias(tableStock.getKey()), "!=", tableStock.getUndefinedID()).and(new Where(input.getAlias(tableStock.getField("QTE_REEL")), ">", 0)));
132 ilm 994
 
995
                if (input.getWhere() != null) {
996
                    input.setWhere(input.getWhere().and(w));
997
                } else {
998
                    input.setWhere(w);
999
                }
1000
                input.asString();
1001
                return input;
1002
            }
1003
        };
1004
 
1005
        m.setSelectTransformer(selTrans);
1006
 
94 ilm 1007
        this.table.setDropTarget(new DropTarget() {
1008
            @Override
1009
            public synchronized void drop(DropTargetDropEvent dtde) {
1010
                dropInTable(dtde, m);
1011
                // super.drop(dtde);
1012
            }
1013
        });
1014
 
132 ilm 1015
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.CAN_EXPAND_NOMENCLATURE_VT, true)) {
93 ilm 1016
 
132 ilm 1017
            table.addMouseListener(new MouseAdapter() {
93 ilm 1018
 
132 ilm 1019
                @Override
1020
                public void mousePressed(MouseEvent e) {
1021
 
1022
                    handlePopup(e);
93 ilm 1023
                }
1024
 
132 ilm 1025
                @Override
1026
                public void mouseReleased(MouseEvent e) {
1027
 
1028
                    handlePopup(e);
1029
                }
1030
 
1031
                public void handlePopup(MouseEvent e) {
1032
                    final int rowindex = table.getSelectedRow();
1033
                    if (rowindex < 0)
1034
                        return;
1035
                    if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
1036
                        JPopupMenu popup = new JPopupMenu();
1037
                        popup.add(new AbstractAction(TranslationManager.getInstance().getTranslationForItem("product.bom.expand")) {
1038
 
1039
                            @Override
1040
                            public void actionPerformed(ActionEvent arg0) {
1041
                                expandNomenclature(rowindex, m, EXPAND_TYPE.EXPAND);
1042
                            }
1043
                        });
1044
                        popup.add(new AbstractAction(TranslationManager.getInstance().getTranslationForItem("product.bom.expose")) {
1045
 
1046
                            @Override
1047
                            public void actionPerformed(ActionEvent arg0) {
1048
                                expandNomenclature(rowindex, m, EXPAND_TYPE.VIEW_ONLY);
1049
                            }
1050
                        });
142 ilm 1051
 
1052
                        for (AbstractAction action : getAdditionnalMouseAction(rowindex)) {
1053
                            popup.add(action);
1054
                        }
1055
 
132 ilm 1056
                        popup.show(e.getComponent(), e.getX(), e.getY());
1057
                    }
1058
                }
1059
            });
1060
        }
19 ilm 1061
        final AutoCompletionManager m2 = new AutoCompletionManager(tableElementNom, sqlTableArticle.getField("NOM"), this.table, this.table.getRowValuesTableModel()) {
1062
            @Override
93 ilm 1063
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
1064
                Object res = tarifCompletion(row, field, rowDest, true);
19 ilm 1065
                if (res == null) {
174 ilm 1066
                    res = super.getValueFrom(row, field, rowDest);
19 ilm 1067
                }
174 ilm 1068
                if (field.equals("POURCENT_REMISE")) {
1069
                    return getRemiseClient(row);
1070
                }
1071
                return res;
19 ilm 1072
            }
1073
 
1074
        };
18 ilm 1075
        m2.fill("CODE", "CODE");
61 ilm 1076
        m2.fill("ID", "ID_ARTICLE");
28 ilm 1077
        for (String string : completionField) {
1078
            m2.fill(string, string);
19 ilm 1079
        }
1080
 
132 ilm 1081
        m2.setSelectTransformer(selTrans);
18 ilm 1082
 
61 ilm 1083
        final AutoCompletionManager m3 = new AutoCompletionManager(tableElementArticle, sqlTableArticle.getField("NOM"), this.table, this.table.getRowValuesTableModel(),
65 ilm 1084
                ITextWithCompletion.MODE_CONTAINS, true, true, new ValidStateChecker()) {
61 ilm 1085
            @Override
93 ilm 1086
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
1087
                Object res = tarifCompletion(row, field, rowDest, true);
61 ilm 1088
                if (res == null) {
174 ilm 1089
                    res = super.getValueFrom(row, field, rowDest);
61 ilm 1090
                }
174 ilm 1091
                if (field.equals("POURCENT_REMISE")) {
1092
                    return getRemiseClient(row);
1093
                }
1094
                return res;
61 ilm 1095
            }
28 ilm 1096
 
61 ilm 1097
        };
1098
        m3.fill("CODE", "CODE");
1099
        m3.fill("NOM", "NOM");
1100
        for (String string : completionField) {
1101
            m3.fill(string, string);
1102
        }
28 ilm 1103
 
132 ilm 1104
        m3.setSelectTransformer(selTrans);
61 ilm 1105
 
67 ilm 1106
        // Deselection de l'article si le code est modifié
93 ilm 1107
        tableFamille.addModificationListener(tableElementArticle);
61 ilm 1108
        tableElementCode.addModificationListener(tableElementArticle);
1109
        tableElementArticle.setModifier(new CellDynamicModifier() {
1110
            @Override
93 ilm 1111
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
83 ilm 1112
                try {
93 ilm 1113
                    if (filterFamilleArticle) {
1114
                        if (row.isForeignEmpty("ID_FAMILLE_ARTICLE")) {
132 ilm 1115
                            m.setWhere(null);
1116
                            m2.setWhere(null);
93 ilm 1117
                        } else {
132 ilm 1118
                            m.setWhere(new Where(sqlTableArticle.getField("ID_FAMILLE_ARTICLE"), "=", row.getForeignID("ID_FAMILLE_ARTICLE")));
1119
                            m2.setWhere(new Where(sqlTableArticle.getField("ID_FAMILLE_ARTICLE"), "=", row.getForeignID("ID_FAMILLE_ARTICLE")));
93 ilm 1120
                        }
1121
                    }
83 ilm 1122
                    SQLRowAccessor foreign = row.getForeign("ID_ARTICLE");
1123
                    if (foreign != null && !foreign.isUndefined() && foreign.getObject("CODE") != null && foreign.getString("CODE").equals(row.getString("CODE"))) {
1124
                        return foreign.getID();
1125
                    } else {
1126
                        return tableArticle.getUndefinedID();
1127
                    }
1128
                } catch (Exception e) {
61 ilm 1129
                    return tableArticle.getUndefinedID();
1130
                }
1131
            }
1132
        });
1133
 
142 ilm 1134
        // ECO Contribution
1135
        if (this.tableElementEco != null && this.tableElementEcoTotal != null && this.tableElementEcoID != null) {
1136
            this.qte.addModificationListener(this.tableElementEcoTotal);
1137
            this.tableElementEco.addModificationListener(this.tableElementEcoTotal);
1138
            this.tableElementEcoTotal.setModifier(new CellDynamicModifier() {
1139
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
1140
 
1141
                    int qte = Integer.parseInt(row.getObject("QTE").toString());
1142
                    BigDecimal f = (row.getObject("ECO_CONTRIBUTION") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("ECO_CONTRIBUTION");
1143
                    return f.multiply(new BigDecimal(qte));
1144
                }
1145
 
1146
            });
1147
            this.tableElementEcoID.addModificationListener(this.tableElementEco);
1148
            this.tableElementEco.setModifier(new CellDynamicModifier() {
1149
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
1150
 
1151
                    if (source != null && source.equals(tableElementEcoID)) {
1152
                        return row.getForeign("ID_ECO_CONTRIBUTION").getBigDecimal("TAUX");
1153
                    } else {
1154
                        return row.getObject("ECO_CONTRIBUTION");
1155
                    }
1156
                }
1157
            });
1158
        }
1159
 
18 ilm 1160
        // Calcul automatique du total HT
93 ilm 1161
        this.qte.addModificationListener(tableElement_PrixMetrique1_VenteHT);
18 ilm 1162
        this.qte.addModificationListener(this.totalHT);
67 ilm 1163
        this.qte.addModificationListener(this.totalHA);
61 ilm 1164
        qteU.addModificationListener(this.totalHT);
67 ilm 1165
        qteU.addModificationListener(this.totalHA);
28 ilm 1166
        if (tableElementRG != null) {
1167
            tableElementRG.addModificationListener(this.totalHT);
1168
        }
19 ilm 1169
        tableElementRemise.addModificationListener(this.totalHT);
28 ilm 1170
 
18 ilm 1171
        tableElement_PrixVente_HT.addModificationListener(this.totalHT);
19 ilm 1172
        // tableElement_PrixVente_HT.addModificationListener(tableElement_PrixMetrique1_VenteHT);
67 ilm 1173
        this.ha.addModificationListener(this.totalHA);
18 ilm 1174
 
1175
        this.totalHT.setModifier(new CellDynamicModifier() {
93 ilm 1176
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
19 ilm 1177
 
83 ilm 1178
                BigDecimal lremise = BigDecimal.ZERO;
28 ilm 1179
 
1180
                if (row.getTable().getFieldsName().contains("POURCENT_RG")) {
1181
                    final Object o3 = row.getObject("POURCENT_RG");
93 ilm 1182
                    if (o3 != null) {
1183
                        lremise = lremise.add(((BigDecimal) o3));
1184
                    }
28 ilm 1185
                }
1186
 
19 ilm 1187
                int qte = (row.getObject("QTE") == null) ? 0 : Integer.parseInt(row.getObject("QTE").toString());
61 ilm 1188
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
67 ilm 1189
                BigDecimal f = (BigDecimal) row.getObject("PV_HT");
90 ilm 1190
                BigDecimal r = b.multiply(f.multiply(BigDecimal.valueOf(qte), DecimalUtils.HIGH_PRECISION), DecimalUtils.HIGH_PRECISION);
67 ilm 1191
                if (lremise.compareTo(BigDecimal.ZERO) > 0 && lremise.compareTo(BigDecimal.valueOf(100)) < 100) {
90 ilm 1192
                    r = r.multiply(BigDecimal.valueOf(100).subtract(lremise), DecimalUtils.HIGH_PRECISION).movePointLeft(2);
19 ilm 1193
                }
83 ilm 1194
 
1195
                if (row.getTable().getFieldsName().contains("MONTANT_REMISE")) {
1196
                    final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_REMISE");
1197
                    final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_REMISE");
1198
                    Remise remise = new Remise(acomptePercent, acompteMontant);
1199
                    r = remise.getResultFrom(r);
1200
                }
1201
 
80 ilm 1202
                if (row.getTable().getFieldsName().contains("POURCENT_FACTURABLE")) {
1203
                    final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_FACTURABLE");
1204
                    final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_FACTURABLE");
1205
                    Acompte acompte = new Acompte(acomptePercent, acompteMontant);
1206
                    r = acompte.getResultFrom(r);
1207
                }
1208
 
67 ilm 1209
                return r.setScale(totalHT.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
18 ilm 1210
            }
1211
        });
67 ilm 1212
        this.totalHA.setModifier(new CellDynamicModifier() {
18 ilm 1213
            @Override
93 ilm 1214
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 1215
                int qte = Integer.parseInt(row.getObject("QTE").toString());
61 ilm 1216
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
67 ilm 1217
                BigDecimal f = (BigDecimal) row.getObject("PA_HT");
142 ilm 1218
                BigDecimal rHA = b.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION).multiply(f, DecimalUtils.HIGH_PRECISION).setScale(6, BigDecimal.ROUND_HALF_UP);
1219
                if (row.getTable().getFieldsName().contains("POURCENT_FACTURABLE")) {
1220
                    final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_FACTURABLE");
1221
                    final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_FACTURABLE");
1222
                    if (acomptePercent != null || acompteMontant != null) {
1223
                        if (acomptePercent != null) {
1224
                            rHA = rHA.multiply(acomptePercent.movePointLeft(2), DecimalUtils.HIGH_PRECISION);
1225
                        } else {
1226
                            // Calcul du T_HT vente origin
1227
                            BigDecimal lremise = BigDecimal.ZERO;
1228
 
1229
                            if (row.getTable().getFieldsName().contains("POURCENT_RG")) {
1230
                                final Object o3 = row.getObject("POURCENT_RG");
1231
                                if (o3 != null) {
1232
                                    lremise = lremise.add(((BigDecimal) o3));
1233
                                }
1234
                            }
1235
 
1236
                            BigDecimal fVT = (BigDecimal) row.getObject("PV_HT");
1237
                            BigDecimal r = b.multiply(fVT.multiply(BigDecimal.valueOf(qte), DecimalUtils.HIGH_PRECISION), DecimalUtils.HIGH_PRECISION);
1238
                            if (lremise.compareTo(BigDecimal.ZERO) > 0 && lremise.compareTo(BigDecimal.valueOf(100)) < 100) {
1239
                                r = r.multiply(BigDecimal.valueOf(100).subtract(lremise), DecimalUtils.HIGH_PRECISION).movePointLeft(2);
1240
                            }
1241
 
1242
                            if (row.getTable().getFieldsName().contains("MONTANT_REMISE")) {
1243
                                final BigDecimal acomptePercentR = row.getBigDecimal("POURCENT_REMISE");
1244
                                final BigDecimal acompteMontantR = row.getBigDecimal("MONTANT_REMISE");
1245
                                Remise remise = new Remise(acomptePercentR, acompteMontantR);
1246
                                r = remise.getResultFrom(r);
1247
                            }
1248
                            if (r.signum() != 0) {
1249
                                rHA = rHA.multiply(acompteMontant.divide(r, DecimalUtils.HIGH_PRECISION), DecimalUtils.HIGH_PRECISION);
1250
                            }
1251
                        }
1252
                    }
1253
 
1254
                }
1255
                return rHA.setScale(totalHA.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
18 ilm 1256
            }
83 ilm 1257
 
1258
            @Override
1259
            public void setValueFrom(SQLRowValues row, Object value) {
1260
                super.setValueFrom(row, value);
1261
            }
18 ilm 1262
        });
1263
 
156 ilm 1264
        if (showDevise) {
19 ilm 1265
            this.qte.addModificationListener(tableElementTotalDevise);
61 ilm 1266
            qteU.addModificationListener(tableElementTotalDevise);
80 ilm 1267
            if (eltUnitDevise != null) {
1268
                eltUnitDevise.addModificationListener(tableElementTotalDevise);
1269
            }
21 ilm 1270
            tableElementRemise.addModificationListener(this.tableElementTotalDevise);
19 ilm 1271
            tableElementTotalDevise.setModifier(new CellDynamicModifier() {
1272
                @Override
93 ilm 1273
                public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
1274
                    int qte = row.getInt("QTE");
1275
                    BigDecimal prixDeVenteUnitaireDevise = (row.getObject("PV_U_DEVISE") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("PV_U_DEVISE");
1276
                    BigDecimal qUnitaire = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
1277
                    // r = prixUnitaire x qUnitaire x qte
1278
                    BigDecimal prixVente = qUnitaire.multiply(prixDeVenteUnitaireDevise.multiply(BigDecimal.valueOf(qte), DecimalUtils.HIGH_PRECISION), DecimalUtils.HIGH_PRECISION);
67 ilm 1279
 
83 ilm 1280
                    if (row.getTable().getFieldsName().contains("MONTANT_REMISE")) {
1281
                        final BigDecimal acomptePercent = row.getBigDecimal("POURCENT_REMISE");
1282
                        final BigDecimal acompteMontant = row.getBigDecimal("MONTANT_REMISE");
1283
                        Remise remise = new Remise(acomptePercent, acompteMontant);
93 ilm 1284
                        prixVente = remise.getResultFrom(prixVente);
21 ilm 1285
                    }
83 ilm 1286
 
1287
                    // if (lremise.compareTo(BigDecimal.ZERO) > 0 &&
1288
                    // lremise.compareTo(BigDecimal.valueOf(100)) < 100) {
1289
                    // r = r.multiply(BigDecimal.valueOf(100).subtract(lremise),
90 ilm 1290
                    // DecimalUtils.HIGH_PRECISION).movePointLeft(2);
83 ilm 1291
                    // }
93 ilm 1292
                    return prixVente.setScale(tableElementTotalDevise.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
19 ilm 1293
                }
1294
            });
1295
        }
18 ilm 1296
        // Calcul automatique du total TTC
73 ilm 1297
 
18 ilm 1298
        this.totalHT.addModificationListener(this.tableElementTotalTTC);
61 ilm 1299
        this.tableElementTVA.addModificationListener(this.tableElementTotalTTC);
18 ilm 1300
        this.tableElementTotalTTC.setModifier(new CellDynamicModifier() {
1301
            @Override
93 ilm 1302
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
21 ilm 1303
 
73 ilm 1304
                BigDecimal ht = (BigDecimal) row.getObject("T_PV_HT");
93 ilm 1305
                int idTaux = row.getForeignID("ID_TAXE");
18 ilm 1306
 
1307
                Float resultTaux = TaxeCache.getCache().getTauxFromId(idTaux);
1308
 
1309
                if (resultTaux == null) {
80 ilm 1310
                    SQLRow rowTax = TaxeCache.getCache().getFirstTaxe();
1311
                    row.put("ID_TAXE", rowTax.getID());
1312
                    resultTaux = rowTax.getFloat("TAUX");
18 ilm 1313
                }
1314
 
1315
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
73 ilm 1316
                editorPVHT.setTaxe(taux);
94 ilm 1317
                editorPVHT.setMin(null);
1318
                if (!row.isForeignEmpty("ID_ARTICLE") && getSQLElement().getTable().getDBRoot().contains("ARTICLE_PRIX_MIN_VENTE") && !lockVTMinPrice) {
1319
                    List<SQLRow> minPrices = row.getForeign("ID_ARTICLE").asRow().getReferentRows(row.getTable().getTable("ARTICLE_PRIX_MIN_VENTE"));
1320
                    if (minPrices.size() > 0) {
1321
                        editorPVHT.setMin(minPrices.get(0).getBigDecimal("PRIX"));
1322
                    }
1323
                }
73 ilm 1324
 
90 ilm 1325
                BigDecimal r = ht.multiply(BigDecimal.valueOf(taux).movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
73 ilm 1326
                final BigDecimal resultTTC = r.setScale(tableElementTotalTTC.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
83 ilm 1327
 
73 ilm 1328
                return resultTTC;
18 ilm 1329
            }
1330
 
83 ilm 1331
            @Override
1332
            public void setValueFrom(SQLRowValues row, Object value) {
1333
                super.setValueFrom(row, value);
1334
            }
1335
 
18 ilm 1336
        });
21 ilm 1337
 
18 ilm 1338
        // Calcul automatique du poids unitaire
1339
        tableElement_ValeurMetrique1.addModificationListener(tableElementPoids);
1340
        tableElement_ValeurMetrique2.addModificationListener(tableElementPoids);
1341
        tableElement_ValeurMetrique3.addModificationListener(tableElementPoids);
1342
        tableElementPoids.setModifier(new CellDynamicModifier() {
93 ilm 1343
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 1344
                return new Float(ReferenceArticleSQLElement.getPoidsFromDetails(row));
1345
            }
1346
 
1347
        });
1348
        // Calcul automatique du poids total
1349
        tableElementPoids.addModificationListener(this.tableElementPoidsTotal);
61 ilm 1350
        qteU.addModificationListener(tableElementPoidsTotal);
18 ilm 1351
        this.qte.addModificationListener(this.tableElementPoidsTotal);
1352
        this.tableElementPoidsTotal.setModifier(new CellDynamicModifier() {
93 ilm 1353
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 1354
                Number f = (Number) row.getObject("POIDS");
80 ilm 1355
                if (f == null) {
1356
                    f = 0;
1357
                }
18 ilm 1358
                int qte = Integer.parseInt(row.getObject("QTE").toString());
61 ilm 1359
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
1360
                // FIXME convertir en float autrement pour éviter une valeur non valeur transposable
1361
                // avec floatValue ou passer POIDS en bigDecimal
1362
                return b.multiply(new BigDecimal(f.floatValue() * qte)).floatValue();
18 ilm 1363
            }
1364
 
1365
        });
61 ilm 1366
        uniteVente.addModificationListener(qteU);
1367
        qteU.setModifier(new CellDynamicModifier() {
93 ilm 1368
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
61 ilm 1369
                SQLRowAccessor rowUnite = row.getForeign("ID_UNITE_VENTE");
1370
                if (rowUnite != null && !rowUnite.isUndefined() && rowUnite.getBoolean("A_LA_PIECE")) {
1371
                    return BigDecimal.ONE;
1372
                } else {
1373
                    return row.getObject("QTE_UNITAIRE");
1374
                }
1375
            }
1376
 
1377
        });
93 ilm 1378
 
1379
        tableElement_PrixMetrique1_VenteHT.setModifier(new CellDynamicModifier() {
1380
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
1381
                if (source != null && source.getField().getName().equals("PRIX_METRIQUE_VT_1")) {
1382
                    return row.getObject("PRIX_METRIQUE_VT_1");
1383
                } else {
1384
                    if (source != null && source.getField().getName().equals("PV_U_DEVISE")) {
1385
                        if (!row.isForeignEmpty("ID_DEVISE")) {
1386
                            String devCode = row.getForeign("ID_DEVISE").getString("CODE");
1387
                            BigDecimal prixDeVenteUnitaireDevise = (row.getObject("PV_U_DEVISE") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("PV_U_DEVISE");
1388
 
1389
                            CurrencyConverter c = new CurrencyConverter();
1390
                            BigDecimal result = c.convert(prixDeVenteUnitaireDevise, devCode, c.getCompanyCurrencyCode(), getDateDevise(), isUsedBiasedDevise());
1391
                            if (result == null) {
1392
                                result = prixDeVenteUnitaireDevise;
1393
                            }
1394
                            return result.setScale(row.getTable().getField("PRIX_METRIQUE_VT_1").getType().getDecimalDigits(), RoundingMode.HALF_UP);
1395
                        } else {
1396
                            return row.getObject("PRIX_METRIQUE_VT_1");
1397
                        }
1398
                    }
1399
                    return tarifCompletion(row.getForeign("ID_ARTICLE"), "PRIX_METRIQUE_VT_1", row);
1400
                }
1401
            }
1402
 
1403
        });
1404
 
156 ilm 1405
        if (showDevise) {
80 ilm 1406
            if (eltUnitDevise != null) {
1407
                eltUnitDevise.addModificationListener(tableElement_PrixMetrique1_VenteHT);
1408
            }
83 ilm 1409
 
93 ilm 1410
            if (eltUnitDevise != null) {
1411
                tableElement_PrixMetrique1_VenteHT.addModificationListener(eltUnitDevise);
132 ilm 1412
                eltDevise.addModificationListener(eltUnitDevise);
93 ilm 1413
                eltUnitDevise.setModifier(new CellDynamicModifier() {
1414
                    public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
1415
                        if (source != null && source.getField().getName().equals("PV_U_DEVISE")) {
1416
                            BigDecimal prixDeVenteUnitaireDevise = (row.getObject("PV_U_DEVISE") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("PV_U_DEVISE");
1417
                            return prixDeVenteUnitaireDevise;
1418
                        } else {
1419
                            if (!row.isForeignEmpty("ID_DEVISE")) {
1420
                                String devCode = row.getForeign("ID_DEVISE").getString("CODE");
1421
                                BigDecimal bigDecimal = (BigDecimal) row.getObject("PRIX_METRIQUE_VT_1");
1422
 
1423
                                CurrencyConverter c = new CurrencyConverter();
1424
                                BigDecimal result = c.convert(bigDecimal, c.getCompanyCurrencyCode(), devCode, getDateDevise(), isUsedBiasedDevise());
1425
                                if (result == null) {
1426
                                    result = bigDecimal;
1427
                                }
1428
                                return result.setScale(row.getTable().getField("PRIX_METRIQUE_VT_1").getType().getDecimalDigits(), RoundingMode.HALF_UP);
1429
                            } else if (source != null && source.getField().getName().equalsIgnoreCase("PRIX_METRIQUE_VT_1")) {
1430
                                return row.getObject("PRIX_METRIQUE_VT_1");
1431
                            }
1432
                            BigDecimal prixDeVenteUnitaireDevise = (row.getObject("PV_U_DEVISE") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("PV_U_DEVISE");
1433
                            return prixDeVenteUnitaireDevise;
1434
                        }
19 ilm 1435
                    }
1436
 
93 ilm 1437
                });
1438
            }
19 ilm 1439
        }
1440
 
18 ilm 1441
        // Calcul automatique du prix de vente unitaire HT
1442
        tableElement_ValeurMetrique1.addModificationListener(tableElement_PrixVente_HT);
1443
        tableElement_ValeurMetrique2.addModificationListener(tableElement_PrixVente_HT);
1444
        tableElement_ValeurMetrique3.addModificationListener(tableElement_PrixVente_HT);
1445
        tableElement_PrixMetrique1_VenteHT.addModificationListener(tableElement_PrixVente_HT);
1446
        tableElement_PrixVente_HT.setModifier(new CellDynamicModifier() {
93 ilm 1447
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
1448
                if (row.isForeignEmpty("ID_MODE_VENTE_ARTICLE") || row.getInt("ID_MODE_VENTE_ARTICLE") == ReferenceArticleSQLElement.A_LA_PIECE) {
67 ilm 1449
                    return row.getObject("PRIX_METRIQUE_VT_1");
18 ilm 1450
                } else {
1451
 
67 ilm 1452
                    final BigDecimal prixVTFromDetails = ReferenceArticleSQLElement.getPrixVTFromDetails(row);
1453
                    return prixVTFromDetails.setScale(tableElement_PrixVente_HT.getDecimalDigits(), RoundingMode.HALF_UP);
18 ilm 1454
                }
1455
            }
1456
        });
1457
 
1458
        // Calcul automatique du prix d'achat unitaire HT
1459
        tableElement_ValeurMetrique1.addModificationListener(this.ha);
1460
        tableElement_ValeurMetrique2.addModificationListener(this.ha);
1461
        tableElement_ValeurMetrique3.addModificationListener(this.ha);
1462
        tableElement_PrixMetrique1_AchatHT.addModificationListener(this.ha);
1463
        this.ha.setModifier(new CellDynamicModifier() {
93 ilm 1464
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
1465
                if (row.isForeignEmpty("ID_MODE_VENTE_ARTICLE") || row.getInt("ID_MODE_VENTE_ARTICLE") == ReferenceArticleSQLElement.A_LA_PIECE) {
67 ilm 1466
                    return row.getObject("PRIX_METRIQUE_HA_1");
18 ilm 1467
                } else {
1468
 
67 ilm 1469
                    final BigDecimal prixHAFromDetails = ReferenceArticleSQLElement.getPrixHAFromDetails(row);
1470
                    return prixHAFromDetails.setScale(ha.getDecimalDigits(), RoundingMode.HALF_UP);
18 ilm 1471
                }
1472
            }
1473
 
1474
        });
1475
 
1476
        this.table.readState();
1477
 
93 ilm 1478
        setColumnVisible(model.getColumnForField("T_PA_HT"), true);
18 ilm 1479
 
80 ilm 1480
        // Packaging
1481
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.ITEM_PACKAGING, false)) {
93 ilm 1482
            setColumnVisible(model.getColumnForField("T_POIDS_COLIS_NET"), false);
80 ilm 1483
        }
1484
 
18 ilm 1485
        // Mode Gestion article avancé
21 ilm 1486
        final boolean modeAvance = DefaultNXProps.getInstance().getBooleanValue("ArticleModeVenteAvance", false);
93 ilm 1487
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_1"), modeAvance);
1488
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_2"), modeAvance);
1489
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_3"), modeAvance);
1490
        setColumnVisible(model.getColumnForField("PV_HT"), modeAvance);
1491
        setColumnVisible(model.getColumnForField("PA_HT"), modeAvance);
1492
        setColumnVisible(model.getColumnForField("ID_MODE_VENTE_ARTICLE"), modeAvance);
18 ilm 1493
 
142 ilm 1494
        if (this.tableElementEco != null && this.tableElementEcoTotal != null && this.tableElementEcoID != null) {
1495
            setColumnVisible(model.getColumnForField("ID_ECO_CONTRIBUTION"), showEco);
1496
            setColumnVisible(model.getColumnForField("ECO_CONTRIBUTION"), showEco);
1497
            setColumnVisible(model.getColumnForField("T_ECO_CONTRIBUTION"), showEco);
1498
        }
1499
 
61 ilm 1500
        // Gestion des unités de vente
1501
        final boolean gestionUV = prefs.getBoolean(GestionArticleGlobalPreferencePanel.UNITE_VENTE, true);
93 ilm 1502
        setColumnVisible(model.getColumnForField("QTE_UNITAIRE"), gestionUV);
1503
        setColumnVisible(model.getColumnForField("ID_UNITE_VENTE"), gestionUV);
61 ilm 1504
 
93 ilm 1505
        setColumnVisible(model.getColumnForField("ID_ARTICLE"), selectArticle);
1506
        setColumnVisible(model.getColumnForField("CODE"), !selectArticle || (selectArticle && createAuto));
1507
        setColumnVisible(model.getColumnForField("NOM"), !selectArticle || (selectArticle && createAuto));
61 ilm 1508
 
18 ilm 1509
        // Voir le poids
21 ilm 1510
        final boolean showPoids = DefaultNXProps.getInstance().getBooleanValue("ArticleShowPoids", false);
93 ilm 1511
        setColumnVisible(model.getColumnForField("POIDS"), showPoids);
1512
        setColumnVisible(model.getColumnForField("T_POIDS"), showPoids);
18 ilm 1513
 
1514
        // Voir le style
93 ilm 1515
        setColumnVisible(model.getColumnForField("ID_STYLE"), DefaultNXProps.getInstance().getBooleanValue("ArticleShowStyle", true));
1516
        setColumnVisible(model.getColumnForField("POURCENT_FACTURABLE"), false);
18 ilm 1517
 
93 ilm 1518
        setColumnVisible(getModel().getColumnForField("ID_FAMILLE_ARTICLE"), filterFamilleArticle);
28 ilm 1519
 
94 ilm 1520
        setColumnVisible(model.getColumnForField("PRIX_METRIQUE_HA_1"), showHAPrice);
156 ilm 1521
        setColumnVisible(model.getColumnForField("MARGE_HT"), showHAPrice);
94 ilm 1522
        setColumnVisible(model.getColumnForField("T_PA_HT"), showHAPrice);
93 ilm 1523
 
94 ilm 1524
 
156 ilm 1525
        setColumnVisible(model.getColumnForField("ID_DEPOT_STOCK"), prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false));
1526
 
28 ilm 1527
        for (String string : visibilityMap.keySet()) {
93 ilm 1528
            setColumnVisible(model.getColumnForField(string), visibilityMap.get(string));
28 ilm 1529
        }
1530
 
41 ilm 1531
        Map<String, Boolean> mapCustom = getCustomVisibilityMap();
1532
        if (mapCustom != null) {
1533
            for (String string : mapCustom.keySet()) {
93 ilm 1534
                setColumnVisible(model.getColumnForField(string), mapCustom.get(string));
41 ilm 1535
            }
1536
        }
1537
 
142 ilm 1538
        // Barcode reader
1539
        final BarcodeReader barcodeReader = ComptaPropsConfiguration.getInstanceCompta().getBarcodeReader();
1540
        if (barcodeReader != null) {
1541
 
1542
            final BarcodeListener l = new BarcodeListener() {
1543
 
1544
                @Override
1545
                public void keyReceived(KeyEvent ee) {
1546
                    // TODO Auto-generated method stub
1547
                }
1548
 
1549
                @Override
1550
                public void barcodeRead(String code) {
1551
                    if (((JFrame) SwingUtilities.getRoot(getRowValuesTable())).isActive()) {
1552
                        final SQLSelect selArticle = new SQLSelect();
1553
                        final SQLTable tableArticle = getSQLElement().getForeignElement("ID_ARTICLE").getTable();
1554
                        selArticle.addSelectStar(tableArticle);
1555
                        Where w = new Where(tableArticle.getField("OBSOLETE"), "=", Boolean.FALSE);
1556
                        w = w.and(new Where(tableArticle.getField("CODE_BARRE"), "=", code));
1557
                        selArticle.setWhere(w);
1558
                        List<SQLRow> l2 = SQLRowListRSH.execute(selArticle);
1559
                        if (l2.size() > 0) {
1560
                            System.err.println("ARTICLE " + l2.get(0).getString("NOM"));
1561
                            Tuple3<Double, String, String> art = Tuple3.create(1.0D, l2.get(0).getString("CODE"), l2.get(0).getString("NOM"));
1562
                            List<Tuple3<Double, String, String>> l = new ArrayList<Tuple3<Double, String, String>>();
1563
                            l.add(art);
1564
                            insertFromDrop(l, m);
1565
                        } else {
1566
                            System.err.println("ARTICLE NOT FOUND !");
1567
                        }
1568
                    }
1569
 
1570
                }
1571
            };
1572
            getRowValuesTable().addHierarchyListener(new HierarchyListener() {
1573
                public void hierarchyChanged(HierarchyEvent e) {
1574
                    if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0)
1575
                        if (getRowValuesTable().isDisplayable()) {
1576
                            barcodeReader.addBarcodeListener(l);
1577
                        } else {
1578
                            barcodeReader.removeBarcodeListener(l);
1579
                        }
1580
                }
1581
            });
1582
 
1583
        }
1584
 
18 ilm 1585
        // On réécrit la configuration au cas ou les preferences aurait changé (ajout ou suppression
1586
        // du mode de vente specifique)
1587
        this.table.writeState();
174 ilm 1588
 
1589
        if (this.table.getRowValuesTableModel().getColumnForField("ID_DEPOT_STOCK") >= 0 && this.table.getRowValuesTableModel().getColumnForField("ID_ARTICLE") >= 0) {
1590
            if (this.buttons == null) {
1591
                this.buttons = new ArrayList<>();
1592
            }
1593
 
1594
            JButton buttonStock = new JButton("Consulter le stock");
1595
            buttonStock.addActionListener(new ActionListener() {
1596
                public void actionPerformed(ActionEvent event) {
1597
                    SQLRowValues rowValsSel = table.getSelectedRowValues();
1598
                    if (rowValsSel != null) {
1599
                        SQLRowAccessor foreignArt = rowValsSel.getForeign("ID_ARTICLE");
1600
                        if (foreignArt != null && !foreignArt.isUndefined()) {
1601
                            SQLRowAccessor rowValsStock = StockSQLElement.getStock(rowValsSel);
1602
                            if (rowValsStock != null && !rowValsStock.isUndefined()) {
1603
                                EditFrame frame = new EditFrame(table.getRowValuesTableModel().getSQLElement().getDirectory().getElement("STOCK"), EditMode.READONLY);
1604
                                frame.selectionId(rowValsStock.getID());
1605
                                frame.setVisible(true);
1606
                            }
1607
                        }
1608
                    }
1609
 
1610
                }
1611
            });
1612
 
1613
            this.buttons.add(buttonStock);
1614
 
1615
        }
1616
 
18 ilm 1617
    }
1618
 
174 ilm 1619
    Collection<? extends SQLRowAccessor> cacheRemise = null;
1620
 
1621
    protected BigDecimal getTarifRemiseClient(SQLRowAccessor article, BigDecimal pv) {
1622
        if (cacheRemise != null) {
1623
            if (getRowClient() != null && !getRowClient().isUndefined() && article != null && !article.isUndefined()) {
1624
                for (SQLRowAccessor sqlRowAccessor : cacheRemise) {
1625
                    if (!sqlRowAccessor.isForeignEmpty("ID_ARTICLE") && sqlRowAccessor.getForeignID("ID_ARTICLE") == article.getID()) {
1626
                        BigDecimal remise = sqlRowAccessor.getBigDecimal("POURCENT_REMISE");
1627
                        if (remise != null) {
1628
                            return pv.multiply(BigDecimal.ONE.subtract(remise.movePointLeft(2)), DecimalUtils.HIGH_PRECISION).setScale(pv.scale(), RoundingMode.HALF_UP);
1629
                        }
1630
                    }
1631
                }
1632
            }
1633
        }
1634
        return pv;
1635
    }
1636
 
1637
    protected Acompte getRemiseClient(SQLRowAccessor article) {
1638
        Acompte remise = new Acompte(BigDecimal.ZERO, BigDecimal.ZERO);
1639
        if (this.cacheRemise != null) {
1640
            if (getRowClient() != null && !getRowClient().isUndefined() && article != null && !article.isUndefined()) {
1641
                for (SQLRowAccessor sqlRowAccessor : this.cacheRemise) {
1642
                    if (!sqlRowAccessor.isForeignEmpty("ID_ARTICLE") && sqlRowAccessor.getForeignID("ID_ARTICLE") == article.getID()) {
1643
                        BigDecimal r = sqlRowAccessor.getBigDecimal("POURCENT_REMISE");
1644
                        if (remise != null) {
1645
                            remise = new Acompte(r, null);
1646
                        }
1647
                    }
1648
                }
1649
            }
1650
        }
1651
        return remise;
1652
    }
1653
 
93 ilm 1654
    @Override
1655
    protected void refreshDeviseAmount() {
1656
        int count = getRowValuesTable().getRowCount();
1657
        final int columnForField = getRowValuesTable().getRowValuesTableModel().getColumnForField("PV_U_DEVISE");
1658
        if (columnForField >= 0) {
1659
            SQLTableElement eltDevise = getRowValuesTable().getRowValuesTableModel().getSQLTableElementAt(columnForField);
1660
            for (int i = 0; i < count; i++) {
1661
                SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
1662
                // getRowValuesTable().getRowValuesTableModel().putValue(rowVals.getObject("PV_U_DEVISE"),
1663
                // i, "PV_U_DEVISE", true);
1664
                BigDecimal prixDeVenteUnitaireDevise = (rowVals.getObject("PV_U_DEVISE") == null) ? BigDecimal.ZERO : (BigDecimal) rowVals.getObject("PV_U_DEVISE");
1665
                eltDevise.setValueFrom(rowVals, prixDeVenteUnitaireDevise);
1666
                getRowValuesTable().getRowValuesTableModel().fireTableChanged(new TableModelEvent(getRowValuesTable().getRowValuesTableModel(), i, i, columnForField));
1667
            }
1668
        }
1669
    }
1670
 
41 ilm 1671
    protected Map<String, Boolean> getCustomVisibilityMap() {
1672
        return null;
1673
    }
1674
 
93 ilm 1675
    protected Object tarifCompletion(SQLRowAccessor row, String field, SQLRowAccessor rowDest) {
1676
        return tarifCompletion(row, field, rowDest, false);
1677
    }
19 ilm 1678
 
93 ilm 1679
    protected Object tarifCompletion(SQLRowAccessor row, String field, SQLRowAccessor rowDest, boolean fromCompletion) {
1680
 
156 ilm 1681
        if (row != null && !row.isUndefined() && (field.equalsIgnoreCase("PRIX_METRIQUE_HA_1") || field.equalsIgnoreCase("PA_HT"))) {
132 ilm 1682
 
156 ilm 1683
            if (getSQLElement().getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
1684
                final BigDecimal prc;
1685
                if (row.getBoolean("AUTO_PRIX_REVIENT_NOMENCLATURE")) {
1686
                    ProductHelper helper = new ProductHelper(row.getTable().getDBRoot());
1687
                    prc = helper.getBomPriceForQuantity(1, row.getReferentRows(row.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT")), TypePrice.ARTICLE_PRIX_REVIENT);
1688
                } else {
1689
                    ProductComponent productComp = new ProductComponent(row, BigDecimal.ONE, null, null);
1690
                    prc = productComp.getPRC(new Date());
1691
                }
1692
                if (prc == null) {
1693
                    return BigDecimal.ZERO;
1694
                }
1695
                return prc;
1696
            } else if (getSQLElement().getTable().getDBRoot().contains("COUT_REVIENT") && row.getObject("ID_COUT_REVIENT") != null && !row.isForeignEmpty("ID_COUT_REVIENT")) {
1697
                BigDecimal ha = row.getBigDecimal(field);
1698
                BigDecimal percent = row.getForeign("ID_COUT_REVIENT").getBigDecimal("POURCENT");
1699
                return ha.multiply(percent.movePointLeft(2).add(BigDecimal.ONE)).setScale(ha.precision(), RoundingMode.HALF_UP);
132 ilm 1700
            }
94 ilm 1701
        }
1702
 
73 ilm 1703
        if (getTarif() != null && !getTarif().isUndefined()) {
28 ilm 1704
            Collection<? extends SQLRowAccessor> rows = row.getReferentRows(tableArticleTarif);
19 ilm 1705
 
93 ilm 1706
            // Récupération du tarif associé à la quantité
1707
            int quantite = 0;
1708
            BigDecimal b = rowDest.getBigDecimal("QTE_UNITAIRE");
1709
            int q = rowDest.getInt("QTE");
1710
            BigDecimal qteTotal = b.multiply(new BigDecimal(q), DecimalUtils.HIGH_PRECISION);
28 ilm 1711
            SQLRowAccessor rowTarif = null;
93 ilm 1712
 
1713
            for (SQLRowAccessor sqlRowAccessor : rows) {
1714
 
1715
                if (!sqlRowAccessor.getTable().contains("OBSOLETE") || !sqlRowAccessor.getBoolean("OBSOLETE")) {
1716
                    // FIXME BigDecimal??
1717
                    // BigDecimal bigDecimal = sqlRowAccessor.getBigDecimal("QTE");
1718
                    int qteTarif = sqlRowAccessor.getInt("QTE");
1719
                    if (sqlRowAccessor.getForeignID("ID_TARIF") == getTarif().getID() && CompareUtils.compare(qteTarif, qteTotal) <= 0 && CompareUtils.compare(qteTarif, quantite) > 0) {
1720
                        quantite = qteTarif;
1721
                        rowTarif = sqlRowAccessor;
1722
                        // else {
1723
                        // result = null;
1724
                        // remise = sqlRowAccessor.getBigDecimal("POURCENT_REMISE");
1725
                        // }
1726
                    }
19 ilm 1727
                }
1728
            }
1729
 
1730
            if (rowTarif == null) {
93 ilm 1731
                if (!getTarif().isForeignEmpty("ID_DEVISE")) {
19 ilm 1732
                    if ((field.equalsIgnoreCase("ID_DEVISE"))) {
1733
                        return getTarif().getObject("ID_DEVISE");
1734
                    } else if ((field.equalsIgnoreCase("PV_U_DEVISE"))) {
1735
 
132 ilm 1736
                        return getQtyTarifPvM1(rowDest, fromCompletion);
93 ilm 1737
                    }
1738
                }
1739
                if ((field.equalsIgnoreCase("ID_TAXE"))) {
83 ilm 1740
 
93 ilm 1741
                    if (!getTarif().isForeignEmpty("ID_TAXE")) {
1742
                        return getTarif().getForeignID("ID_TAXE");
19 ilm 1743
                    }
1744
                }
1745
 
93 ilm 1746
            } else {
1747
                if (field.equalsIgnoreCase("PRIX_METRIQUE_VT_1")) {
1748
                    if (rowTarif.isForeignEmpty("ID_DEVISE"))
1749
                        return rowTarif.getObject(field);
1750
                    else {
19 ilm 1751
 
93 ilm 1752
                        String devCode = getTarif().getForeign("ID_DEVISE").getString("CODE");
1753
                        CurrencyConverter c = new CurrencyConverter();
1754
                        BigDecimal result = c.convert(rowTarif.getBigDecimal(field), devCode, c.getCompanyCurrencyCode(), new Date(), true);
1755
                        return result.setScale(row.getTable().getField(field).getType().getDecimalDigits(), RoundingMode.HALF_UP);
1756
                    }
19 ilm 1757
 
93 ilm 1758
                } else if ((field.equalsIgnoreCase("ID_DEVISE"))) {
19 ilm 1759
 
93 ilm 1760
                    return rowTarif.getObject("ID_DEVISE");
1761
                }
19 ilm 1762
 
93 ilm 1763
                else if ((field.equalsIgnoreCase("PV_U_DEVISE"))) {
19 ilm 1764
 
93 ilm 1765
                    return rowTarif.getObject("PRIX_METRIQUE_VT_1");
19 ilm 1766
 
93 ilm 1767
                } else if ((field.equalsIgnoreCase("ID_TAXE"))) {
1768
 
1769
                    if (!rowTarif.isForeignEmpty("ID_TAXE")) {
1770
                        return rowTarif.getObject("ID_TAXE");
1771
 
1772
                    }
94 ilm 1773
                } else if ((field.equalsIgnoreCase("POURCENT_REMISE"))) {
1774
                    Acompte remise = new Acompte(rowTarif.getBigDecimal("POURCENT_REMISE"), BigDecimal.ZERO);
1775
                    return remise;
19 ilm 1776
                }
1777
            }
1778
        }
93 ilm 1779
 
156 ilm 1780
        if (field.equalsIgnoreCase("ID_TAXE_VENTE") && this.rowCatComptable != null) {
1781
            return this.rowCatComptable.getForeignID("ID_TAXE_VENTE");
1782
        }
1783
 
94 ilm 1784
        if ((field.equalsIgnoreCase("POURCENT_REMISE"))) {
1785
            return new Acompte(BigDecimal.ZERO, BigDecimal.ZERO);
1786
        }
1787
 
93 ilm 1788
        if ((field.equalsIgnoreCase("PRIX_METRIQUE_VT_1"))) {
1789
            return getQtyTarifPvM1(rowDest, fromCompletion);
1790
        } else {
1791
            return null;
1792
        }
19 ilm 1793
    }
1794
 
1795
    @Override
174 ilm 1796
    public void setClient(SQLRowAccessor rowClient, boolean ask) {
1797
        if (rowClient == null || getRowClient() == null || rowClient.getID() != getRowClient().getID()) {
1798
            super.setClient(rowClient, ask);
1799
            if (getRowClient() != null && !getRowClient().isUndefined()) {
1800
                this.cacheRemise = getRowClient().getReferentRows(getSQLElement().getTable().getTable("TARIF_ARTICLE_CLIENT"));
1801
                if (ask && getRowValuesTable().getRowCount() > 0
1802
                        && JOptionPane.showConfirmDialog(null, "Appliquer les remises associées au client sur les lignes déjà présentes?") == JOptionPane.YES_OPTION) {
1803
                    int nbRows = this.table.getRowCount();
1804
                    for (int i = 0; i < nbRows; i++) {
1805
                        SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
1806
 
1807
                        if (!rowVals.isForeignEmpty("ID_ARTICLE")) {
1808
                            SQLRowAccessor rowValsArt = rowVals.getForeign("ID_ARTICLE");
1809
                            final Object taxeValue = tarifCompletion(rowValsArt, "ID_TAXE", rowVals);
1810
                            if (taxeValue != null) {
1811
                                getRowValuesTable().getRowValuesTableModel().putValue(taxeValue, i, "ID_TAXE");
1812
                            }
1813
 
1814
                            final Object deviseValue = tarifCompletion(rowValsArt, "ID_DEVISE", rowVals);
1815
                            if (deviseValue != null) {
1816
                                getRowValuesTable().getRowValuesTableModel().putValue(deviseValue, i, "ID_DEVISE");
1817
                            }
1818
                            getRowValuesTable().getRowValuesTableModel().putValue(tarifCompletion(rowValsArt, "PV_U_DEVISE", rowVals), i, "PV_U_DEVISE");
1819
                            getRowValuesTable().getRowValuesTableModel().putValue(tarifCompletion(rowValsArt, "PRIX_METRIQUE_VT_1", rowVals), i, "PRIX_METRIQUE_VT_1");
1820
                        }
1821
                    }
1822
                }
1823
            } else {
1824
                this.cacheRemise = null;
1825
            }
1826
        }
1827
    }
1828
 
1829
    @Override
19 ilm 1830
    public void setTarif(SQLRowAccessor rowValuesTarif, boolean ask) {
1831
        if (rowValuesTarif == null || getTarif() == null || rowValuesTarif.getID() != getTarif().getID()) {
1832
            super.setTarif(rowValuesTarif, ask);
94 ilm 1833
            if (ask && getRowValuesTable().getRowCount() > 0
1834
                    && JOptionPane.showConfirmDialog(null, "Appliquer les tarifs associés au client sur les lignes déjà présentes?") == JOptionPane.YES_OPTION) {
19 ilm 1835
                int nbRows = this.table.getRowCount();
1836
                for (int i = 0; i < nbRows; i++) {
1837
                    SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
1838
 
93 ilm 1839
                    if (!rowVals.isForeignEmpty("ID_ARTICLE")) {
1840
                        SQLRowAccessor rowValsArt = rowVals.getForeign("ID_ARTICLE");
1841
                        final Object taxeValue = tarifCompletion(rowValsArt, "ID_TAXE", rowVals);
1842
                        if (taxeValue != null) {
1843
                            getRowValuesTable().getRowValuesTableModel().putValue(taxeValue, i, "ID_TAXE");
19 ilm 1844
                        }
93 ilm 1845
 
1846
                        final Object deviseValue = tarifCompletion(rowValsArt, "ID_DEVISE", rowVals);
1847
                        if (deviseValue != null) {
1848
                            getRowValuesTable().getRowValuesTableModel().putValue(deviseValue, i, "ID_DEVISE");
1849
                        }
1850
                        getRowValuesTable().getRowValuesTableModel().putValue(tarifCompletion(rowValsArt, "PV_U_DEVISE", rowVals), i, "PV_U_DEVISE");
1851
                        getRowValuesTable().getRowValuesTableModel().putValue(tarifCompletion(rowValsArt, "PRIX_METRIQUE_VT_1", rowVals), i, "PRIX_METRIQUE_VT_1");
19 ilm 1852
                    }
93 ilm 1853
                }
1854
            }
1855
        }
1856
    }
28 ilm 1857
 
93 ilm 1858
    protected Object getQtyTarifPvM1(SQLRowAccessor row, boolean fromCompletion) {
73 ilm 1859
 
93 ilm 1860
        // Test si un tarif spécial est associé à l'article
1861
        // Object o = tarifCompletion(row.getForeign("ID_ARTICLE"), "PRIX_METRIQUE_VT_1", row,
1862
        // false);
1863
        // if (o != null) {
1864
        // return o;
1865
        // }
1866
 
1867
        SQLRowAccessor rowA = row.getForeign("ID_ARTICLE");
132 ilm 1868
        if (rowA != null && !rowA.isUndefined() && rowA.getTable().contains("AUTO_PRIX_MIN_VENTE_NOMENCLATURE") && rowA.getBoolean("AUTO_PRIX_MIN_VENTE_NOMENCLATURE")) {
1869
            BigDecimal b = row.getBigDecimal("QTE_UNITAIRE");
1870
            int q = row.getInt("QTE");
1871
            BigDecimal qteTotal = b.multiply(new BigDecimal(q), DecimalUtils.HIGH_PRECISION);
1872
            ProductHelper helper = new ProductHelper(rowA.getTable().getDBRoot());
1873
 
1874
            return helper.getBomPriceForQuantity(qteTotal.setScale(0, RoundingMode.HALF_UP).intValue(), rowA.getReferentRows(rowA.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT")),
1875
                    TypePrice.ARTICLE_PRIX_PUBLIC);
1876
        }
93 ilm 1877
        BigDecimal result = null;
1878
        // BigDecimal remise = null;
1879
        if (rowA != null && !rowA.isUndefined() && rowA.getTable().getDBRoot().contains("ARTICLE_PRIX_PUBLIC")) {
1880
            Collection<? extends SQLRowAccessor> col = rowA.getReferentRows(rowA.getTable().getTable("ARTICLE_PRIX_PUBLIC"));
1881
            int quantite = 0;
1882
            BigDecimal b = row.getBigDecimal("QTE_UNITAIRE");
1883
            int q = row.getInt("QTE");
1884
            BigDecimal qteTotal = b.multiply(new BigDecimal(q), DecimalUtils.HIGH_PRECISION);
1885
 
1886
            for (SQLRowAccessor sqlRowAccessor : col) {
1887
 
1888
                // FIXME BigDecimal??
1889
                // BigDecimal bigDecimal = sqlRowAccessor.getBigDecimal("QTE");
1890
                int qtePublic = sqlRowAccessor.getInt("QTE");
1891
                if (CompareUtils.compare(qtePublic, qteTotal) <= 0 && CompareUtils.compare(qtePublic, quantite) > 0) {
1892
                    quantite = qtePublic;
1893
                    if (sqlRowAccessor.getBigDecimal("PRIX") != null) {
1894
                        result = sqlRowAccessor.getBigDecimal("PRIX");
1895
                        // remise = null;
19 ilm 1896
                    }
93 ilm 1897
                    // else {
1898
                    // result = null;
1899
                    // remise = sqlRowAccessor.getBigDecimal("POURCENT_REMISE");
1900
                    // }
1901
                }
1902
            }
1903
        }
94 ilm 1904
        if (result == null && rowA != null && !rowA.isUndefined() && rowA.getTable().getDBRoot().contains("ARTICLE_PRIX_MIN_VENTE")) {
93 ilm 1905
            Collection<? extends SQLRowAccessor> col = rowA.getReferentRows(rowA.getTable().getTable("ARTICLE_PRIX_MIN_VENTE"));
1906
            int quantite = 0;
1907
            BigDecimal b = row.getBigDecimal("QTE_UNITAIRE");
1908
            int q = row.getInt("QTE");
1909
            BigDecimal qteTotal = b.multiply(new BigDecimal(q), DecimalUtils.HIGH_PRECISION);
19 ilm 1910
 
93 ilm 1911
            for (SQLRowAccessor sqlRowAccessor : col) {
1912
 
1913
                int qteMinVente = sqlRowAccessor.getInt("QTE");
1914
                if (CompareUtils.compare(qteMinVente, qteTotal) <= 0 && CompareUtils.compare(qteMinVente, quantite) > 0) {
1915
                    quantite = qteMinVente;
1916
                    if (sqlRowAccessor.getBigDecimal("PRIX") != null) {
1917
                        result = sqlRowAccessor.getBigDecimal("PRIX");
19 ilm 1918
                    }
1919
                }
1920
            }
18 ilm 1921
        }
94 ilm 1922
 
1923
        BigDecimal remise = null;
1924
 
1925
        if (rowA != null && !rowA.isUndefined() && rowA.getTable().getDBRoot().contains("TARIF_QUANTITE")) {
1926
            Collection<? extends SQLRowAccessor> col = rowA.getReferentRows(rowA.getTable().getTable("TARIF_QUANTITE"));
1927
            BigDecimal quantite = BigDecimal.ZERO;
1928
            BigDecimal b = row.getBigDecimal("QTE_UNITAIRE");
1929
            int q = row.getInt("QTE");
1930
            BigDecimal qteTotal = b.multiply(new BigDecimal(q), DecimalUtils.HIGH_PRECISION);
1931
 
1932
            for (SQLRowAccessor sqlRowAccessor : col) {
1933
 
1934
                BigDecimal bigDecimal = sqlRowAccessor.getBigDecimal("QUANTITE");
1935
                if (CompareUtils.compare(bigDecimal, qteTotal) <= 0 && CompareUtils.compare(bigDecimal, quantite) > 0) {
1936
                    quantite = bigDecimal;
1937
                    if (sqlRowAccessor.getBigDecimal("PRIX_METRIQUE_VT_1") != null) {
1938
                        result = sqlRowAccessor.getBigDecimal("PRIX_METRIQUE_VT_1");
1939
                        remise = null;
1940
                    } else {
1941
                        result = null;
1942
                        remise = sqlRowAccessor.getBigDecimal("POURCENT_REMISE");
1943
                    }
1944
                }
1945
            }
1946
            if (!col.isEmpty() && result == null && remise == null) {
1947
                result = rowA.getBigDecimal("PRIX_METRIQUE_VT_1");
1948
            }
1949
        }
1950
        int index = getRowValuesTable().getRowValuesTableModel().row2index(row);
1951
        if (result == null && remise == null) {
93 ilm 1952
            // getRowValuesTable().getRowValuesTableModel().putValue(BigDecimal.ZERO, index,
1953
            // "POURCENT_REMISE");
1954
            return (fromCompletion ? null : row.getObject("PRIX_METRIQUE_VT_1"));
1955
        } else {
1956
            if (result != null) {
1957
                // getRowValuesTable().getRowValuesTableModel().putValue(BigDecimal.ZERO, index,
1958
                // "POURCENT_REMISE");
1959
                return result;
1960
            } else {
94 ilm 1961
                getRowValuesTable().getRowValuesTableModel().putValue(remise, index, "POURCENT_REMISE");
93 ilm 1962
                return row.getBigDecimal("PRIX_METRIQUE_VT_1");
1963
            }
1964
        }
1965
 
18 ilm 1966
    }
93 ilm 1967
 
1968
    @Override
1969
    protected void setModel(RowValuesTableModel model) {
1970
        super.setModel(model);
1971
        model.addTableModelListener(new TableModelListener() {
1972
 
1973
            @Override
1974
            public void tableChanged(TableModelEvent e) {
1975
 
1976
                calculTarifNomenclature();
1977
 
1978
            }
1979
        });
1980
    }
94 ilm 1981
 
1982
    private void dropInTable(DropTargetDropEvent dtde, final AutoCompletionManager autoM) {
1983
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
1984
        Transferable t = dtde.getTransferable();
1985
        try {
1986
 
1987
            List<Tuple3<Double, String, String>> articles = new ArrayList<Tuple3<Double, String, String>>();
1988
            if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
1989
                List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
1990
                final DataImporter importer = new DataImporter(getSQLElement().getTable());
1991
                final File file = fileList.get(0);
1992
                if (file.getName().endsWith(".ods") || file.getName().endsWith(".xls")) {
1993
                    ArrayTableModel m = importer.createModelFrom(file);
1994
                    for (int i = 0; i < m.getRowCount(); i++) {
1995
                        List<Object> l = m.getLineValuesAt(i);
1996
                        if (l.size() > 1) {
1997
                            if (l.get(0) == null || l.get(0).toString().length() == 0) {
1998
                                break;
1999
                            }
2000
                            Double qte = ((Number) l.get(1)).doubleValue();
2001
                            String code = "";
2002
                            if (l.get(0) instanceof Number) {
2003
                                code = String.valueOf(((Number) l.get(0)).intValue());
2004
                            } else {
2005
                                code = l.get(0).toString();
2006
                            }
2007
                            String nom = "";
2008
                            if (l.size() > 2) {
2009
                                nom = (String) l.get(2);
2010
                            }
2011
                            if (qte > 0) {
2012
                                articles.add(Tuple3.create(qte, code, nom));
2013
                            }
2014
                        }
2015
                    }
2016
                } else {
2017
                    JOptionPane.showMessageDialog(null, "Les formats de fichiers pris en charge sont ods et xls!");
2018
                }
2019
            } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
2020
                final String transferData = (String) t.getTransferData(DataFlavor.stringFlavor);
2021
                List<String> l = StringUtils.fastSplitTrimmed(transferData, '\n');
2022
                for (String string : l) {
2023
                    List<String> line = StringUtils.fastSplitTrimmed(string, '\t');
2024
                    if (line.size() >= 2) {
2025
                        Double qte = Double.valueOf(line.get(1));
2026
                        String code = (line.get(0) == null ? "" : line.get(0).toString());
2027
                        String nom = "";
2028
                        if (line.size() > 2) {
2029
                            nom = (String) line.get(2);
2030
                        }
2031
                        if (qte > 0) {
2032
                            articles.add(Tuple3.create(qte, code, nom));
2033
                        }
2034
                    } else {
2035
                        break;
2036
                    }
2037
                }
2038
            }
2039
            if (articles.size() > 0) {
2040
                insertFromDrop(articles, autoM);
2041
                for (Tuple3<Double, String, String> tuple3 : articles) {
2042
                    System.err.println("ADD LINE " + tuple3);
2043
                }
2044
            }
2045
        } catch (UnsupportedFlavorException e) {
2046
            // TODO Auto-generated catch block
2047
            e.printStackTrace();
2048
        } catch (IOException e) {
2049
            // TODO Auto-generated catch block
2050
            e.printStackTrace();
2051
        }
2052
 
2053
        // dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
2054
        // Transferable t = dtde.getTransferable();
2055
        // List fileList = (List)t.getTransferData(DataFlavor.javaFileListFlavor);
2056
        // File f = (File)fileList.get(0);
2057
        // table.setValueAt(f.getAbsolutePath(), row, column);
2058
        // table.setValueAt(f.length(), row, column+1);
2059
    }
2060
 
142 ilm 2061
    protected void insertFromDrop(List<Tuple3<Double, String, String>> articles, AutoCompletionManager m) {
94 ilm 2062
 
2063
        List<String> code = new ArrayList<String>(articles.size());
2064
        for (int i = articles.size() - 1; i >= 0; i--) {
2065
 
2066
            Tuple3<Double, String, String> tuple = articles.get(i);
2067
            code.add(tuple.get1());
2068
        }
2069
        SQLSelect sel = new SQLSelect();
2070
        final SQLTable articleTable = getSQLElement().getTable().getForeignTable("ID_ARTICLE");
2071
        sel.addSelectStar(articleTable);
2072
        sel.setWhere(new Where(articleTable.getField("CODE"), code));
2073
        List<SQLRow> matchCode = SQLRowListRSH.execute(sel);
2074
        Map<String, SQLRow> mapCode = new HashMap<String, SQLRow>();
2075
        for (SQLRow sqlRow : matchCode) {
2076
            mapCode.put(sqlRow.getString("CODE"), sqlRow);
2077
        }
2078
 
142 ilm 2079
        int rowCount = getRowValuesTable().getRowValuesTableModel().getRowCount();
2080
        Map<Integer, Integer> mapRows = new HashMap<Integer, Integer>();
2081
        for (int i = 0; i < rowCount; i++) {
2082
            SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
2083
            if (rowVals.getObject("ID_ARTICLE") != null && !rowVals.isForeignEmpty("ID_ARTICLE")) {
2084
                mapRows.put(rowVals.getForeignID("ID_ARTICLE"), i);
2085
            }
2086
        }
2087
 
2088
        Set<String> fieldsFrom = m.getFieldsFrom();
2089
        fieldsFrom.remove("POURCENT_REMISE");
94 ilm 2090
        for (int i = articles.size() - 1; i >= 0; i--) {
2091
 
2092
            Tuple3<Double, String, String> tuple = articles.get(i);
2093
 
2094
            SQLRow article = mapCode.get(tuple.get1());
142 ilm 2095
            String fieldQte = "QTE";
94 ilm 2096
 
142 ilm 2097
            if (article != null && mapRows.containsKey(article.getID())) {
2098
                Integer index = mapRows.get(article.getID());
2099
                SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(index);
2100
                if (rowVals.getTable().getName().equals("BON_DE_LIVRAISON_ELEMENT")) {
2101
                    fieldQte = "QTE_LIVREE";
2102
                }
2103
                getRowValuesTable().getRowValuesTableModel().putValue(rowVals.getInt(fieldQte) + 1, index, fieldQte);
94 ilm 2104
            } else {
142 ilm 2105
                final SQLRowValues row2Insert = new SQLRowValues(getRowValuesTable().getRowValuesTableModel().getDefaultRowValues());
94 ilm 2106
 
142 ilm 2107
                // Completion depuis l'article trouvé
2108
                if (article != null) {
2109
                    m.fillRowValues(article, fieldsFrom, row2Insert);
2110
                    // Fill prix total
2111
                    row2Insert.put("ID_ARTICLE", article.getID());
2112
                    row2Insert.put("CODE", article.getObject("CODE"));
2113
                    row2Insert.put("NOM", article.getObject("NOM"));
2114
                } else {
2115
                    row2Insert.put("CODE", tuple.get1());
2116
                    row2Insert.put("NOM", tuple.get2());
2117
                }
94 ilm 2118
 
142 ilm 2119
                row2Insert.put(fieldQte, Math.round(tuple.get0().floatValue()));
2120
                if (row2Insert.getTable().getName().equals("BON_DE_LIVRAISON_ELEMENT")) {
2121
                    row2Insert.put("QTE_LIVREE", Math.round(tuple.get0().floatValue()));
2122
                }
2123
                row2Insert.put("POURCENT_REMISE", BigDecimal.ZERO);
2124
                row2Insert.put("MONTANT_REMISE", BigDecimal.ZERO);
94 ilm 2125
 
142 ilm 2126
                row2Insert.put("PV_HT", row2Insert.getObject("PRIX_METRIQUE_VT_1"));
2127
                //
2128
                final BigDecimal resultTotalHT = row2Insert.getBigDecimal("PV_HT").multiply(new BigDecimal(row2Insert.getInt(fieldQte)));
2129
                row2Insert.put("T_PV_HT", resultTotalHT);
94 ilm 2130
 
142 ilm 2131
                Float resultTaux = TaxeCache.getCache().getTauxFromId(row2Insert.getForeignID("ID_TAXE"));
94 ilm 2132
 
142 ilm 2133
                if (resultTaux == null) {
2134
                    SQLRow rowTax = TaxeCache.getCache().getFirstTaxe();
2135
                    resultTaux = rowTax.getFloat("TAUX");
2136
                }
94 ilm 2137
 
142 ilm 2138
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
94 ilm 2139
 
142 ilm 2140
                BigDecimal r = resultTotalHT.multiply(BigDecimal.valueOf(taux).movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
94 ilm 2141
 
142 ilm 2142
                row2Insert.put("T_PV_TTC", r);
2143
                // row2Insert.put("ID_STYLE", allStyleByName.get("Composant"));
2144
                getRowValuesTable().getRowValuesTableModel().addRowAt(0, row2Insert);
2145
            }
94 ilm 2146
        }
2147
    }
18 ilm 2148
}