OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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