OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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