OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.common.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
93 ilm 17
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
18 ilm 18
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
142 ilm 19
import org.openconcerto.erp.core.sales.pos.io.BarcodeReader;
20
import org.openconcerto.erp.core.sales.pos.ui.BarcodeListener;
18 ilm 21
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
94 ilm 22
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
23
import org.openconcerto.erp.core.sales.product.ui.CurrencyWithSymbolRenderer;
174 ilm 24
import org.openconcerto.erp.core.sales.product.ui.DeliveredQtyRowValuesRenderer;
61 ilm 25
import org.openconcerto.erp.core.sales.product.ui.QteUnitRowValuesRenderer;
177 ilm 26
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
27
import org.openconcerto.erp.core.supplychain.stock.element.StockSQLElement;
18 ilm 28
import org.openconcerto.erp.preferences.DefaultNXProps;
61 ilm 29
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 30
import org.openconcerto.sql.Configuration;
31
import org.openconcerto.sql.element.SQLElement;
94 ilm 32
import org.openconcerto.sql.model.FieldPath;
19 ilm 33
import org.openconcerto.sql.model.SQLRow;
34
import org.openconcerto.sql.model.SQLRowAccessor;
142 ilm 35
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 36
import org.openconcerto.sql.model.SQLRowValues;
142 ilm 37
import org.openconcerto.sql.model.SQLSelect;
41 ilm 38
import org.openconcerto.sql.model.SQLTable;
80 ilm 39
import org.openconcerto.sql.model.UndefinedRowValuesCache;
41 ilm 40
import org.openconcerto.sql.model.Where;
94 ilm 41
import org.openconcerto.sql.model.graph.Path;
61 ilm 42
import org.openconcerto.sql.preferences.SQLPreferences;
83 ilm 43
import org.openconcerto.sql.sqlobject.ITextArticleWithCompletionCellEditor;
61 ilm 44
import org.openconcerto.sql.sqlobject.ITextWithCompletion;
177 ilm 45
import org.openconcerto.sql.view.EditFrame;
46
import org.openconcerto.sql.view.EditPanel.EditMode;
18 ilm 47
import org.openconcerto.sql.view.list.AutoCompletionManager;
48
import org.openconcerto.sql.view.list.CellDynamicModifier;
49
import org.openconcerto.sql.view.list.RowValuesTable;
50
import org.openconcerto.sql.view.list.RowValuesTableModel;
51
import org.openconcerto.sql.view.list.SQLTableElement;
65 ilm 52
import org.openconcerto.sql.view.list.ValidStateChecker;
177 ilm 53
import org.openconcerto.ui.preferences.DefaultProps;
90 ilm 54
import org.openconcerto.utils.DecimalUtils;
142 ilm 55
import org.openconcerto.utils.Tuple3;
94 ilm 56
import org.openconcerto.utils.i18n.TranslationManager;
18 ilm 57
 
174 ilm 58
import java.awt.Component;
132 ilm 59
import java.awt.event.ActionEvent;
177 ilm 60
import java.awt.event.ActionListener;
142 ilm 61
import java.awt.event.HierarchyEvent;
62
import java.awt.event.HierarchyListener;
63
import java.awt.event.KeyEvent;
132 ilm 64
import java.awt.event.MouseAdapter;
65
import java.awt.event.MouseEvent;
66
import java.math.BigDecimal;
67
import java.math.RoundingMode;
68
import java.util.ArrayList;
69
import java.util.Arrays;
70
import java.util.Calendar;
71
import java.util.HashMap;
72
import java.util.HashSet;
73
import java.util.List;
74
import java.util.Map;
75
import java.util.Set;
76
import java.util.Vector;
77
 
78
import javax.swing.AbstractAction;
177 ilm 79
import javax.swing.JButton;
142 ilm 80
import javax.swing.JFrame;
174 ilm 81
import javax.swing.JLabel;
132 ilm 82
import javax.swing.JOptionPane;
83
import javax.swing.JPopupMenu;
84
import javax.swing.JTable;
85
import javax.swing.SwingUtilities;
86
import javax.swing.ToolTipManager;
87
import javax.swing.event.TableModelEvent;
88
import javax.swing.event.TableModelListener;
174 ilm 89
import javax.swing.table.DefaultTableCellRenderer;
132 ilm 90
import javax.swing.table.TableCellRenderer;
91
 
18 ilm 92
public abstract class AbstractAchatArticleItemTable extends AbstractArticleItemTable {
93
 
73 ilm 94
    private AutoCompletionManager m;
95
    private AutoCompletionManager m2, m3;
96
    private AutoCompletionManager m4;
97
    private final SQLTable tableArticle = getSQLElement().getTable().getTable("ARTICLE");
98
    private SQLRowAccessor rowDevise;
99
    private boolean supplierCode;
100
 
18 ilm 101
    public AbstractAchatArticleItemTable() {
102
        super();
103
    }
104
 
94 ilm 105
    @Override
106
    protected void setModel(RowValuesTableModel model) {
107
        super.setModel(model);
108
        model.addTableModelListener(new TableModelListener() {
109
 
110
            @Override
111
            public void tableChanged(TableModelEvent e) {
112
 
113
                calculTarifNomenclature();
114
 
115
            }
116
        });
117
    }
118
 
18 ilm 119
    protected void init() {
120
 
121
        final SQLElement e = getSQLElement();
132 ilm 122
        final SQLPreferences prefs = SQLPreferences.getMemCached(getSQLElement().getTable().getDBRoot());
61 ilm 123
        final boolean selectArticle = prefs.getBoolean(GestionArticleGlobalPreferencePanel.USE_CREATED_ARTICLE, false);
124
        final boolean createAuto = prefs.getBoolean(GestionArticleGlobalPreferencePanel.CREATE_ARTICLE_AUTO, true);
142 ilm 125
        final boolean showEco = prefs.getBoolean(AbstractVenteArticleItemTable.SHOW_ECO_CONTRIBUTION_COLUMNS, false);
149 ilm 126
        final boolean showDmdAchat = prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVE_DEMANDE_ACHAT, false);
156 ilm 127
        final boolean showDevise = prefs.getBoolean(AbstractVenteArticleItemTable.ARTICLE_SHOW_DEVISE, false);
67 ilm 128
        this.supplierCode = prefs.getBoolean(GestionArticleGlobalPreferencePanel.SUPPLIER_PRODUCT_CODE, false);
61 ilm 129
 
18 ilm 130
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
94 ilm 131
        final SQLTableElement eNiveau = new SQLTableElement(e.getTable().getField("NIVEAU")) {
132
            @Override
133
            public void setValueFrom(SQLRowValues row, Object value) {
134
                super.setValueFrom(row, value);
135
            }
136
        };
137
        eNiveau.setRenderer(new NiveauTableCellRender());
138
        eNiveau.setEditor(new NiveauTableCellEditor());
139
        list.add(eNiveau);
18 ilm 140
        list.add(new SQLTableElement(e.getTable().getField("ID_STYLE")));
28 ilm 141
 
149 ilm 142
        SQLTableElement dmdElt = null;
143
        if (showDmdAchat && e.getTable().contains("ID_DEMANDE_ACHAT_ELEMENT")) {
144
            dmdElt = new SQLTableElement(e.getTable().getField("ID_DEMANDE_ACHAT_ELEMENT"));
145
            list.add(dmdElt);
146
        }
147
 
174 ilm 148
        if (e.getTable().contains("ID_AFFAIRE")) {
149
            list.add(new SQLTableElement(e.getTable().getField("ID_AFFAIRE")));
150
        }
151
 
67 ilm 152
        SQLTableElement tableElementCodeFournisseur = null;
153
 
154
        if (e.getTable().contains("ID_CODE_FOURNISSEUR") && supplierCode) {
155
            tableElementCodeFournisseur = new SQLTableElement(e.getTable().getField("ID_CODE_FOURNISSEUR"), true, true, true);
156
            list.add(tableElementCodeFournisseur);
157
        }
158
 
174 ilm 159
        if (e.getTable().contains("ID_COMPTE_PCE")) {
160
            list.add(new SQLTableElement(e.getTable().getField("ID_COMPTE_PCE")));
161
        }
162
 
142 ilm 163
        SQLTableElement tableElementArticle = new SQLTableElement(e.getTable().getField("ID_ARTICLE"), true, true, true) {
164
            @Override
165
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
166
                boolean b = super.isCellEditable(vals, rowIndex, columnIndex);
167
                if (vals.getTable().contains("ID_COMMANDE_ELEMENT")) {
168
                    boolean noCmdElt = vals.getObject("ID_COMMANDE_ELEMENT") == null || vals.isForeignEmpty("ID_COMMANDE_ELEMENT");
169
                    return b && noCmdElt;
170
                } else {
171
                    return b;
172
                }
173
 
174
            }
175
        };
61 ilm 176
        list.add(tableElementArticle);
28 ilm 177
 
156 ilm 178
        // Article
179
        SQLTableElement tableElementDepot = new SQLTableElement(e.getTable().getField("ID_DEPOT_STOCK"), true, true, true);
180
        list.add(tableElementDepot);
181
 
28 ilm 182
        if (e.getTable().getFieldsName().contains("ID_FAMILLE_ARTICLE")) {
183
            final SQLTableElement tableFamille = new SQLTableElement(e.getTable().getField("ID_FAMILLE_ARTICLE"));
184
            list.add(tableFamille);
185
        }
186
 
142 ilm 187
        if (e.getTable().getFieldsName().contains("ID_ECO_CONTRIBUTION")) {
188
            this.tableElementEcoID = new SQLTableElement(e.getTable().getField("ID_ECO_CONTRIBUTION"));
189
            list.add(this.tableElementEcoID);
190
        }
191
 
94 ilm 192
        if (e.getTable().getFieldsName().contains("INCOTERM")) {
193
            final SQLTableElement tableElementInco = new SQLTableElement(e.getTable().getField("INCOTERM"));
194
            tableElementInco.setEditable(false);
195
            list.add(tableElementInco);
196
        }
197
 
28 ilm 198
        if (e.getTable().getFieldsName().contains("PREBILAN")) {
93 ilm 199
            final SQLTableElement tableElementPre = new SQLTableElement(e.getTable().getField("PREBILAN"), BigDecimal.class);
200
            tableElementPre.setRenderer(new DeviseTableCellRenderer());
28 ilm 201
            list.add(tableElementPre);
202
        }
203
 
18 ilm 204
        // Code article
94 ilm 205
        final SQLTableElement tableElementCode = new SQLTableElement(e.getTable().getField("CODE"), String.class,
142 ilm 206
                new ITextArticleWithCompletionCellEditor(e.getTable().getTable("ARTICLE"), e.getTable().getTable("ARTICLE_FOURNISSEUR"))) {
207
            @Override
208
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
209
                boolean b = super.isCellEditable(vals, rowIndex, columnIndex);
210
                if (vals.getTable().contains("ID_COMMANDE_ELEMENT")) {
211
                    boolean noCmdElt = vals.getObject("ID_COMMANDE_ELEMENT") == null || vals.isForeignEmpty("ID_COMMANDE_ELEMENT");
212
                    return b && noCmdElt;
213
                } else {
214
                    return b;
215
                }
216
 
217
            }
218
        };
18 ilm 219
        list.add(tableElementCode);
220
        // Désignation de l'article
142 ilm 221
        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM")) {
222
            @Override
223
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
224
                boolean b = super.isCellEditable(vals, rowIndex, columnIndex);
225
                if (vals.getTable().contains("ID_COMMANDE_ELEMENT")) {
226
                    boolean noCmdElt = vals.getObject("ID_COMMANDE_ELEMENT") == null || vals.isForeignEmpty("ID_COMMANDE_ELEMENT");
227
                    return b && noCmdElt;
228
                } else {
229
                    return b;
230
                }
231
 
232
            }
233
        };
18 ilm 234
        list.add(tableElementNom);
142 ilm 235
 
73 ilm 236
        if (e.getTable().getFieldsName().contains("DESCRIPTIF")) {
237
            final SQLTableElement tableElementDesc = new SQLTableElement(e.getTable().getField("DESCRIPTIF"));
238
            list.add(tableElementDesc);
239
        }
41 ilm 240
        if (e.getTable().getFieldsName().contains("COLORIS")) {
241
            final SQLTableElement tableElementColoris = new SQLTableElement(e.getTable().getField("COLORIS"));
242
            list.add(tableElementColoris);
243
        }
18 ilm 244
        // Valeur des métriques
245
        final SQLTableElement tableElement_ValeurMetrique2 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_2"), Float.class);
246
        list.add(tableElement_ValeurMetrique2);
247
        final SQLTableElement tableElement_ValeurMetrique3 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_3"), Float.class);
248
        list.add(tableElement_ValeurMetrique3);
249
        final SQLTableElement tableElement_ValeurMetrique1 = new SQLTableElement(e.getTable().getField("VALEUR_METRIQUE_1"), Float.class);
250
        list.add(tableElement_ValeurMetrique1);
251
        // Prix d'achat HT de la métrique 1
156 ilm 252
        final DeviseNumericHTConvertorCellEditor editorPAHT = new DeviseNumericHTConvertorCellEditor(e.getTable().getField("PRIX_METRIQUE_HA_1"));
253
        final SQLTableElement tableElement_PrixMetrique1_AchatHT = new SQLTableElement(e.getTable().getField("PRIX_METRIQUE_HA_1"), BigDecimal.class, editorPAHT);
94 ilm 254
        tableElement_PrixMetrique1_AchatHT.setRenderer(new CurrencyWithSymbolRenderer());
18 ilm 255
        list.add(tableElement_PrixMetrique1_AchatHT);
19 ilm 256
 
142 ilm 257
        if (e.getTable().getFieldsName().contains("ECO_CONTRIBUTION")) {
258
            this.tableElementEco = new SQLTableElement(e.getTable().getField("ECO_CONTRIBUTION"));
259
            list.add(this.tableElementEco);
260
        }
261
 
19 ilm 262
        final SQLTableElement tableElement_Devise = new SQLTableElement(e.getTable().getField("ID_DEVISE"));
94 ilm 263
        tableElement_Devise.setEditable(false);
67 ilm 264
        final SQLTableElement tableElement_PA_Devise = new SQLTableElement(e.getTable().getField("PA_DEVISE"), BigDecimal.class);
94 ilm 265
        Path p = new Path(getSQLElement().getTable()).addForeignField("ID_DEVISE");
266
        tableElement_PA_Devise.setRenderer(new CurrencyWithSymbolRenderer(new FieldPath(p, "CODE")));
156 ilm 267
        if (showDevise) {
28 ilm 268
            // Devise
269
            list.add(tableElement_Devise);
19 ilm 270
 
28 ilm 271
            // Prix d'achat HT devise
272
            list.add(tableElement_PA_Devise);
273
        }
18 ilm 274
        // Mode de vente
275
        final SQLTableElement tableElement_ModeVente = new SQLTableElement(e.getTable().getField("ID_MODE_VENTE_ARTICLE"));
276
        list.add(tableElement_ModeVente);
19 ilm 277
 
18 ilm 278
        // Prix d'achat unitaire HT
67 ilm 279
        this.ha = new SQLTableElement(e.getTable().getField("PA_HT"), BigDecimal.class);
94 ilm 280
        this.ha.setRenderer(new CurrencyWithSymbolRenderer());
18 ilm 281
        list.add(this.ha);
19 ilm 282
 
61 ilm 283
        SQLTableElement qteU = new SQLTableElement(e.getTable().getField("QTE_UNITAIRE"), BigDecimal.class) {
284
            @Override
132 ilm 285
            public boolean isCellEditable(SQLRowValues vals, int rowIndex, int columnIndex) {
61 ilm 286
 
287
                SQLRowAccessor row = vals.getForeign("ID_UNITE_VENTE");
288
                if (row != null && !row.isUndefined() && row.getBoolean("A_LA_PIECE")) {
289
                    return false;
290
                } else {
132 ilm 291
                    return super.isCellEditable(vals, rowIndex, columnIndex);
61 ilm 292
                }
293
            }
294
 
295
            @Override
296
            public TableCellRenderer getTableCellRenderer() {
297
                return new QteUnitRowValuesRenderer();
298
            }
67 ilm 299
 
300
            protected Object getDefaultNullValue() {
301
                return BigDecimal.ZERO;
302
            }
61 ilm 303
        };
304
        list.add(qteU);
305
 
306
        SQLTableElement uniteVente = new SQLTableElement(e.getTable().getField("ID_UNITE_VENTE"));
307
        list.add(uniteVente);
308
 
174 ilm 309
        SQLTableElement tableCmdElt = null;
310
        if (e.getTable().contains("ID_COMMANDE_ELEMENT")) {
311
            tableCmdElt = new SQLTableElement(e.getTable().getField("ID_COMMANDE_ELEMENT"));
312
            tableCmdElt.setRenderer(new DefaultTableCellRenderer() {
313
                @Override
314
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
315
                    SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(row);
316
 
317
                    JLabel tableCellRendererComponent = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
318
                    tableCellRendererComponent.setText("");
319
                    if (rowVals.getObject("ID_COMMANDE_ELEMENT") != null && !rowVals.isForeignEmpty("ID_COMMANDE_ELEMENT")) {
320
                        SQLRowAccessor rowCmdItem = rowVals.getForeign("ID_COMMANDE_ELEMENT");
321
                        int qte = rowCmdItem.getInt("QTE");
322
                        BigDecimal qteU = rowCmdItem.getBigDecimal("QTE_UNITAIRE");
323
                        BigDecimal qteR = rowCmdItem.getBigDecimal("QTE_RECUE");
324
                        qteU = qteU.multiply(new BigDecimal(qte));
325
                        if (qteR != null) {
326
                            tableCellRendererComponent.setText(qteU.subtract(qteR).setScale(2, RoundingMode.HALF_UP).toString());
327
                        } else {
328
                            tableCellRendererComponent.setText(qteU.setScale(2, RoundingMode.HALF_UP).toString());
329
                        }
330
                    }
331
 
332
                    return tableCellRendererComponent;
333
                }
334
            });
335
            tableCmdElt.setEditable(false);
336
            list.add(tableCmdElt);
337
        }
338
 
149 ilm 339
        if (e.getTable().getFieldsName().contains("QTE_ORIGINE")) {
340
            final SQLTableElement tableQteO = new SQLTableElement(e.getTable().getField("QTE_ORIGINE"));
341
            tableQteO.setEditable(false);
342
            list.add(tableQteO);
343
        }
344
 
19 ilm 345
        // Quantité
346
        final SQLTableElement qteElement = new SQLTableElement(e.getTable().getField("QTE"), Integer.class) {
347
            protected Object getDefaultNullValue() {
348
                return Integer.valueOf(0);
349
            }
350
        };
174 ilm 351
        if (e.getTable().getFieldsName().contains("QTE_ORIGINE")) {
352
            qteElement.setRenderer(new DeliveredQtyRowValuesRenderer(false));
353
        }
19 ilm 354
        list.add(qteElement);
18 ilm 355
        // TVA
61 ilm 356
        this.tableElementTVA = new SQLTableElement(e.getTable().getField("ID_TAXE"));
357
        list.add(this.tableElementTVA);
18 ilm 358
        // Poids piece
359
        SQLTableElement tableElementPoids = new SQLTableElement(e.getTable().getField("POIDS"), Float.class);
360
        list.add(tableElementPoids);
361
 
362
        // Poids total
363
        this.tableElementPoidsTotal = new SQLTableElement(e.getTable().getField("T_POIDS"), Float.class);
364
        list.add(this.tableElementPoidsTotal);
177 ilm 365
        // Packaging
366
        if (e.getTable().contains("POIDS_COLIS_NET") && prefs.getBoolean(GestionArticleGlobalPreferencePanel.ITEM_PACKAGING, false)) {
18 ilm 367
 
177 ilm 368
            SQLTableElement tareColis = new SQLTableElement(e.getTable().getField("TARE"), BigDecimal.class) {
369
                @Override
370
                public TableCellRenderer getTableCellRenderer() {
371
                    return new QteUnitRowValuesRenderer();
372
                }
373
 
374
            };
375
            list.add(tareColis);
376
 
377
            SQLTableElement poidsColis = new SQLTableElement(e.getTable().getField("POIDS_COLIS_NET"), BigDecimal.class) {
378
                @Override
379
                public TableCellRenderer getTableCellRenderer() {
380
                    return new QteUnitRowValuesRenderer();
381
                }
382
 
383
            };
384
            list.add(poidsColis);
385
 
386
            SQLTableElement nbColis = new SQLTableElement(e.getTable().getField("NB_COLIS"), Integer.class);
387
            list.add(nbColis);
388
 
389
            final SQLTableElement totalPoidsColis = new SQLTableElement(e.getTable().getField("T_POIDS_COLIS_NET"), BigDecimal.class) {
390
                @Override
391
                public TableCellRenderer getTableCellRenderer() {
392
                    return new QteUnitRowValuesRenderer();
393
                }
394
 
395
            };
396
            list.add(totalPoidsColis);
397
 
398
            poidsColis.addModificationListener(totalPoidsColis);
399
            nbColis.addModificationListener(totalPoidsColis);
400
            totalPoidsColis.setModifier(new CellDynamicModifier() {
401
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
402
                    final BigDecimal pdsColis = row.getBigDecimal("POIDS_COLIS_NET");
403
                    final Object o3 = row.getObject("NB_COLIS");
404
 
405
                    BigDecimal pdsColisTotal = BigDecimal.ZERO;
406
 
407
                    if (pdsColis != null && o3 != null) {
408
                        int nb = (Integer) o3;
409
                        pdsColisTotal = pdsColis.multiply(new BigDecimal(nb), DecimalUtils.HIGH_PRECISION);
410
                    }
411
                    return pdsColisTotal.setScale(totalPoidsColis.getDecimalDigits(), RoundingMode.HALF_UP);
412
                }
413
            });
414
 
415
            final SQLTableElement totalPoidsBrut = new SQLTableElement(e.getTable().getField("T_POIDS_BRUT"), BigDecimal.class) {
416
                @Override
417
                public TableCellRenderer getTableCellRenderer() {
418
                    return new QteUnitRowValuesRenderer();
419
                }
420
 
421
            };
422
            list.add(totalPoidsBrut);
423
 
424
            tareColis.addModificationListener(totalPoidsBrut);
425
            poidsColis.addModificationListener(totalPoidsBrut);
426
            nbColis.addModificationListener(totalPoidsBrut);
427
            this.tableElementPoidsTotal.addModificationListener(totalPoidsBrut);
428
            totalPoidsBrut.setModifier(new CellDynamicModifier() {
429
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
430
                    final BigDecimal tare = row.getBigDecimal("TARE");
431
                    final int qte = row.getInt("QTE");
432
                    final BigDecimal pdsColis = row.getBigDecimal("POIDS_COLIS_NET");
433
                    final Object o3 = row.getObject("NB_COLIS");
434
 
435
                    BigDecimal pdsBrutTotal = BigDecimal.ZERO;
436
 
437
                    if (row.getObject("T_POIDS") != null) {
438
                        pdsBrutTotal = new BigDecimal(row.getFloat("T_POIDS"));
439
                    }
440
 
441
                    if (tare != null) {
442
                        pdsBrutTotal = pdsBrutTotal.add(tare.multiply(new BigDecimal(qte)));
443
                    }
444
 
445
                    if (pdsColis != null && o3 != null) {
446
                        int nb = (Integer) o3;
447
                        pdsBrutTotal = pdsBrutTotal.add(pdsColis.multiply(new BigDecimal(nb), DecimalUtils.HIGH_PRECISION));
448
                    }
449
                    // return row.getObject("T_POIDS_COLIS_NET");
450
                    return pdsBrutTotal.setScale(totalPoidsBrut.getDecimalDigits(), RoundingMode.HALF_UP);
451
                }
452
            });
453
 
454
        }
18 ilm 455
        // Service
456
        String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
457
        Boolean b = Boolean.valueOf(val);
458
        if (b != null && b.booleanValue()) {
459
            this.service = new SQLTableElement(e.getTable().getField("SERVICE"), Boolean.class);
460
            list.add(this.service);
461
        }
462
 
156 ilm 463
        if (showDevise) {
28 ilm 464
            // Prix d'achat HT devise
67 ilm 465
            this.tableElementTotalDevise = new SQLTableElement(e.getTable().getField("PA_DEVISE_T"), BigDecimal.class);
174 ilm 466
            CurrencyWithSymbolRenderer deviseTotalRenderer = new CurrencyWithSymbolRenderer(new FieldPath(p, "CODE"));
467
            deviseTotalRenderer.setHideZeroValue(true);
468
            this.tableElementTotalDevise.setRenderer(deviseTotalRenderer);
28 ilm 469
            list.add(tableElementTotalDevise);
470
        }
67 ilm 471
 
472
        SQLTableElement tableElementRemise = null;
473
        if (e.getTable().contains("POURCENT_REMISE")) {
474
            tableElementRemise = new SQLTableElement(e.getTable().getField("POURCENT_REMISE"));
475
            list.add(tableElementRemise);
476
        }
477
 
142 ilm 478
        if (e.getTable().getFieldsName().contains("T_ECO_CONTRIBUTION")) {
479
            this.tableElementEcoTotal = new SQLTableElement(e.getTable().getField("T_ECO_CONTRIBUTION"));
480
            list.add(this.tableElementEcoTotal);
481
        }
482
 
18 ilm 483
        // Total HT
67 ilm 484
        this.totalHT = new SQLTableElement(e.getTable().getField("T_PA_HT"), BigDecimal.class);
174 ilm 485
        DeviseTableCellRenderer totalRenderer = new DeviseTableCellRenderer();
486
        totalRenderer.setHideZeroValue(true);
487
        this.totalHT.setRenderer(totalRenderer);
65 ilm 488
        this.totalHT.setEditable(false);
73 ilm 489
        if (e.getTable().contains("POURCENT_REMISE") && tableElementRemise != null) {
67 ilm 490
            tableElementRemise.addModificationListener(this.totalHT);
491
        }
18 ilm 492
        list.add(this.totalHT);
67 ilm 493
        this.totalHA = this.totalHT;
18 ilm 494
        // Total TTC
67 ilm 495
        this.tableElementTotalTTC = new SQLTableElement(e.getTable().getField("T_PA_TTC"), BigDecimal.class);
174 ilm 496
        this.tableElementTotalTTC.setRenderer(totalRenderer);
18 ilm 497
        list.add(this.tableElementTotalTTC);
498
 
94 ilm 499
        this.defaultRowVals = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
177 ilm 500
        this.defaultRowVals.put("ID_TAXE", TaxeCache.getCache().getFirstTaxeAchat().getID());
94 ilm 501
        this.defaultRowVals.put("CODE", "");
502
        this.defaultRowVals.put("NOM", "");
503
        this.defaultRowVals.put("QTE", 1);
504
        this.defaultRowVals.put("QTE_UNITAIRE", BigDecimal.ONE);
505
        this.defaultRowVals.put("ID_UNITE_VENTE", UniteVenteArticleSQLElement.A_LA_PIECE);
506
        this.defaultRowVals.put("ID_MODE_VENTE_ARTICLE", ReferenceArticleSQLElement.A_LA_PIECE);
177 ilm 507
 
508
        if (e.getTable().contains("ID_DEPOT_STOCK")) {
509
            DefaultProps props = DefaultNXProps.getInstance();
510
            Integer depotDefault = props.getIntProperty("DepotStockDefault", DepotStockSQLElement.DEFAULT_ID);
511
            this.defaultRowVals.put("ID_DEPOT_STOCK", depotDefault);
512
        }
513
 
514
        final RowValuesTableModel model = new RowValuesTableModel(e, list, e.getTable().getField("NOM"), false, this.defaultRowVals) {
515
            @Override
516
            public void commitData() {
517
                super.commitData(true);
518
            }
519
        };
520
 
93 ilm 521
        setModel(model);
80 ilm 522
 
93 ilm 523
        this.table = new RowValuesTable(model, getConfigurationFile());
18 ilm 524
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
525
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
177 ilm 526
        if (getSQLElement().getTable().getName().equals("COMMANDE_ELEMENT")) {
527
            this.table.getClearCloneTableElement().add("QTE_RECUE");
528
            this.table.getClearCloneTableElement().add("RECU");
529
            this.table.getClearCloneTableElement().add("RECU_FORCED");
530
        } else if (getSQLElement().getTable().getName().equals("BON_RECEPTION_ELEMENT")) {
531
            this.table.getClearCloneTableElement().add("ID_COMMANDE_ELEMENT");
532
        }
18 ilm 533
 
94 ilm 534
        table.addMouseListener(new MouseAdapter() {
535
            @Override
536
            public void mouseReleased(MouseEvent e) {
132 ilm 537
                handlePopup(e);
538
            }
539
 
540
            @Override
541
            public void mousePressed(MouseEvent e) {
542
                handlePopup(e);
543
            }
544
 
545
            public void handlePopup(MouseEvent e) {
94 ilm 546
                final int rowindex = table.getSelectedRow();
547
                if (rowindex < 0)
548
                    return;
549
                if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
550
                    JPopupMenu popup = new JPopupMenu();
132 ilm 551
                    if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.CAN_EXPAND_NOMENCLATURE_HA, true)) {
552
                        popup.add(new AbstractAction(TranslationManager.getInstance().getTranslationForItem("product.bom.expand")) {
94 ilm 553
 
132 ilm 554
                            @Override
555
                            public void actionPerformed(ActionEvent arg0) {
556
                                expandNomenclature(rowindex, m, EXPAND_TYPE.EXPAND);
557
                            }
558
                        });
559
                        popup.add(new AbstractAction(TranslationManager.getInstance().getTranslationForItem("product.bom.expose")) {
94 ilm 560
 
132 ilm 561
                            @Override
562
                            public void actionPerformed(ActionEvent arg0) {
563
                                expandNomenclature(rowindex, m, EXPAND_TYPE.VIEW_ONLY);
564
                            }
565
                        });
566
                        popup.add(new AbstractAction(TranslationManager.getInstance().getTranslationForItem("product.bom.flat")) {
94 ilm 567
 
132 ilm 568
                            @Override
569
                            public void actionPerformed(ActionEvent arg0) {
570
                                expandNomenclature(rowindex, m, EXPAND_TYPE.FLAT);
571
                            }
572
                        });
573
                    }
94 ilm 574
                    for (AbstractAction action : getAdditionnalMouseAction(rowindex)) {
575
                        popup.add(action);
576
                    }
577
 
578
                    popup.show(e.getComponent(), e.getX(), e.getY());
579
                }
580
            }
581
        });
582
 
18 ilm 583
        // Autocompletion
149 ilm 584
        if (dmdElt != null) {
585
            AutoCompletionManager dmd = new AutoCompletionManager(dmdElt, ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("DEMANDE_ACHAT_ELEMENT.ID"), this.table,
586
                    this.table.getRowValuesTableModel(), ITextWithCompletion.MODE_CONTAINS, true, true, new ValidStateChecker());
587
            dmd.fill("ID_FAMILLE_ARTICLE", "ID_FAMILLE_ARTICLE");
588
        }
28 ilm 589
        List<String> completionFields = new ArrayList<String>();
132 ilm 590
        if (e.getTable().getFieldsName().contains("INCOTERM")) {
591
            completionFields.add("INCOTERM");
592
        }
142 ilm 593
        if (e.getTable().getFieldsName().contains("ID_ECO_CONTRIBUTION")) {
594
            completionFields.add("ID_ECO_CONTRIBUTION");
595
        }
61 ilm 596
        completionFields.add("ID_UNITE_VENTE");
156 ilm 597
        completionFields.add("QTE_UNITAIRE");
28 ilm 598
        completionFields.add("PA_HT");
599
        completionFields.add("PV_HT");
600
        completionFields.add("POIDS");
601
        completionFields.add("ID_TAXE");
602
        completionFields.add("PRIX_METRIQUE_HA_1");
603
        completionFields.add("PRIX_METRIQUE_HA_2");
604
        completionFields.add("PRIX_METRIQUE_HA_3");
605
        completionFields.add("VALEUR_METRIQUE_1");
606
        completionFields.add("VALEUR_METRIQUE_2");
607
        completionFields.add("VALEUR_METRIQUE_3");
608
        completionFields.add("ID_MODE_VENTE_ARTICLE");
609
        completionFields.add("PRIX_METRIQUE_VT_1");
610
        completionFields.add("PRIX_METRIQUE_VT_2");
611
        completionFields.add("PRIX_METRIQUE_VT_3");
612
        completionFields.add("SERVICE");
613
        completionFields.add("ID_DEVISE");
614
        completionFields.add("PA_DEVISE");
41 ilm 615
        if (e.getTable().getFieldsName().contains("COLORIS")) {
616
            completionFields.add("COLORIS");
617
        }
132 ilm 618
 
73 ilm 619
        if (e.getTable().getFieldsName().contains("DESCRIPTIF")) {
620
            completionFields.add("DESCRIPTIF");
621
        }
28 ilm 622
        if (e.getTable().getFieldsName().contains("ID_FAMILLE_ARTICLE")) {
623
            completionFields.add("ID_FAMILLE_ARTICLE");
624
        }
177 ilm 625
        final SQLTable sqlTableArticle = e.getTable().getTable("ARTICLE");
626
        if (getSQLElement().getTable().getFieldsName().contains("POIDS_COLIS_NET") && sqlTableArticle.getTable().getFieldsName().contains("POIDS_COLIS_NET")) {
627
            completionFields.add("POIDS_COLIS_NET");
628
        }
629
        if (getSQLElement().getTable().getFieldsName().contains("TARE") && sqlTableArticle.getTable().getFieldsName().contains("TARE")) {
630
            completionFields.add("TARE");
631
        }
632
        this.m = new AutoCompletionManager(tableElementCode, sqlTableArticle.getField("CODE"), this.table, this.table.getRowValuesTableModel()) {
19 ilm 633
            @Override
93 ilm 634
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
19 ilm 635
                Object res = tarifCompletion(row, field);
636
                if (res == null) {
93 ilm 637
                    return super.getValueFrom(row, field, rowDest);
19 ilm 638
                } else {
639
                    return res;
640
                }
641
            }
642
        };
18 ilm 643
        m.fill("NOM", "NOM");
61 ilm 644
        m.fill("ID", "ID_ARTICLE");
177 ilm 645
        if (e.getTable().contains("ID_CODE_FOURNISSEUR") && supplierCode) {
646
            m.fill("ID_CODE_FOURNISSEUR", "ID_CODE_FOURNISSEUR");
647
        }
28 ilm 648
        for (String string : completionFields) {
649
            m.fill(string, string);
650
        }
93 ilm 651
        final Where w = new Where(sqlTableArticle.getField("OBSOLETE"), "=", Boolean.FALSE);
652
        m.setWhere(w);
653
 
41 ilm 654
        this.m2 = new AutoCompletionManager(tableElementNom, ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("ARTICLE.NOM"), this.table,
19 ilm 655
                this.table.getRowValuesTableModel()) {
656
            @Override
93 ilm 657
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
19 ilm 658
                Object res = tarifCompletion(row, field);
659
                if (res == null) {
93 ilm 660
                    return super.getValueFrom(row, field, rowDest);
19 ilm 661
                } else {
662
                    return res;
663
                }
664
            }
665
        };
18 ilm 666
        m2.fill("CODE", "CODE");
61 ilm 667
        m2.fill("ID", "ID_ARTICLE");
28 ilm 668
        for (String string : completionFields) {
669
            m2.fill(string, string);
670
        }
93 ilm 671
        m2.setWhere(w);
28 ilm 672
 
61 ilm 673
        this.m3 = new AutoCompletionManager(tableElementArticle, ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("ARTICLE.NOM"), this.table,
65 ilm 674
                this.table.getRowValuesTableModel(), ITextWithCompletion.MODE_CONTAINS, true, true, new ValidStateChecker()) {
61 ilm 675
            @Override
93 ilm 676
            protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
61 ilm 677
                Object res = tarifCompletion(row, field);
678
                if (res == null) {
93 ilm 679
                    return super.getValueFrom(row, field, rowDest);
61 ilm 680
                } else {
681
                    return res;
682
                }
683
            }
684
        };
685
        m3.fill("CODE", "CODE");
686
        m3.fill("NOM", "NOM");
687
        for (String string : completionFields) {
688
            m3.fill(string, string);
689
        }
93 ilm 690
        m3.setWhere(w);
28 ilm 691
 
67 ilm 692
        if (e.getTable().contains("ID_CODE_FOURNISSEUR") && supplierCode) {
693
            this.m4 = new AutoCompletionManager(tableElementCodeFournisseur, ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getField("ARTICLE.NOM"), this.table,
694
                    this.table.getRowValuesTableModel(), ITextWithCompletion.MODE_CONTAINS, true, true, new ValidStateChecker()) {
695
                @Override
93 ilm 696
                protected Object getValueFrom(SQLRow row, String field, SQLRowAccessor rowDest) {
67 ilm 697
                    Object res = tarifCompletion(row, field);
698
                    if (res == null) {
93 ilm 699
                        return super.getValueFrom(row, field, rowDest);
67 ilm 700
                    } else {
701
                        return res;
702
                    }
703
                }
704
            };
177 ilm 705
            m4.fill("ID", "ID_ARTICLE");
67 ilm 706
            m4.fill("CODE", "CODE");
707
            m4.fill("NOM", "NOM");
708
            for (String string : completionFields) {
709
                m4.fill(string, string);
710
            }
711
        }
712
 
713
        tableElementCode.addModificationListener(tableElementArticle);
714
        tableElementArticle.setModifier(new CellDynamicModifier() {
715
            @Override
93 ilm 716
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
83 ilm 717
                try {
718
                    SQLRowAccessor foreign = row.getForeign("ID_ARTICLE");
719
                    if (foreign != null && !foreign.isUndefined() && foreign.getObject("CODE") != null && foreign.getString("CODE").equals(row.getString("CODE"))) {
720
                        return foreign.getID();
721
                    } else {
722
                        return tableArticle.getUndefinedID();
723
                    }
724
                } catch (Exception e) {
67 ilm 725
                    return tableArticle.getUndefinedID();
726
                }
727
            }
728
        });
729
 
142 ilm 730
        // ECO Contribution
731
        if (this.tableElementEco != null && this.tableElementEcoTotal != null && this.tableElementEcoID != null) {
732
            qteElement.addModificationListener(this.tableElementEcoTotal);
733
            this.tableElementEco.addModificationListener(this.tableElementEcoTotal);
734
            this.tableElementEcoTotal.setModifier(new CellDynamicModifier() {
735
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
736
 
737
                    int qte = Integer.parseInt(row.getObject("QTE").toString());
738
                    BigDecimal f = (row.getObject("ECO_CONTRIBUTION") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("ECO_CONTRIBUTION");
739
                    return f.multiply(new BigDecimal(qte));
740
                }
741
 
742
            });
743
            this.tableElementEcoID.addModificationListener(this.tableElementEco);
744
            this.tableElementEco.setModifier(new CellDynamicModifier() {
745
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
746
 
747
                    if (source.equals(tableElementEcoID)) {
748
                        return row.getForeign("ID_ECO_CONTRIBUTION").getBigDecimal("TAUX");
749
                    } else {
750
                        return row.getObject("ECO_CONTRIBUTION");
751
                    }
752
                }
753
            });
754
        }
755
 
18 ilm 756
        // Calcul automatique du total HT
757
        qteElement.addModificationListener(this.totalHT);
61 ilm 758
        qteU.addModificationListener(this.totalHT);
18 ilm 759
        this.ha.addModificationListener(this.totalHT);
760
        this.totalHT.setModifier(new CellDynamicModifier() {
93 ilm 761
            public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
18 ilm 762
 
763
                int qte = Integer.parseInt(row.getObject("QTE").toString());
83 ilm 764
                BigDecimal f = (row.getObject("PA_HT") == null) ? BigDecimal.ZERO : (BigDecimal) row.getObject("PA_HT");
61 ilm 765
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
90 ilm 766
                BigDecimal r = b.multiply(f.multiply(BigDecimal.valueOf(qte)), DecimalUtils.HIGH_PRECISION).setScale(totalHT.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
67 ilm 767
 
768
                if (row.getTable().contains("POURCENT_REMISE")) {
769
                    final Object o2 = row.getObject("POURCENT_REMISE");
770
 
771
                    BigDecimal lremise = (o2 == null) ? BigDecimal.ZERO : ((BigDecimal) o2);
772
                    if (lremise.compareTo(BigDecimal.ZERO) >= 0 && lremise.compareTo(BigDecimal.valueOf(100)) < 0) {
773
 
774
                        r = r.multiply(new BigDecimal(100).subtract(lremise).movePointLeft(2)).setScale(totalHT.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
775
                    }
776
                }
777
 
778
                return r;
18 ilm 779
            }
780
 
781
        });
156 ilm 782
        if (showDevise) {
93 ilm 783
 
784
            if (tableElement_PA_Devise != null) {
785
                tableElement_PA_Devise.addModificationListener(tableElement_PrixMetrique1_AchatHT);
786
            }
787
 
788
            if (tableElement_PA_Devise != null) {
789
                tableElement_PrixMetrique1_AchatHT.addModificationListener(tableElement_PA_Devise);
790
 
791
                tableElement_PA_Devise.setModifier(new CellDynamicModifier() {
792
                    public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
793
                        if (source != null && source.getField().getName().equals("PA_DEVISE")) {
794
                            return row.getObject("PA_DEVISE");
795
                        } else {
142 ilm 796
                            if (!row.isForeignEmpty("ID_DEVISE") && row.getForeign("ID_DEVISE") != null) {
93 ilm 797
                                String devCode = row.getForeign("ID_DEVISE").getString("CODE");
798
                                BigDecimal bigDecimal = (BigDecimal) row.getObject("PRIX_METRIQUE_HA_1");
799
 
800
                                CurrencyConverter c = new CurrencyConverter();
94 ilm 801
                                BigDecimal result = convert(bigDecimal, devCode, true);
93 ilm 802
                                if (result == null) {
803
                                    JOptionPane.showMessageDialog(AbstractAchatArticleItemTable.this, "Unable to convert " + bigDecimal + " from " + c.getCompanyCurrencyCode() + " to " + devCode);
804
                                    return BigDecimal.ZERO;
805
                                }
806
                                return result;
807
                            } else if (source != null && source.getField().getName().equalsIgnoreCase("PRIX_METRIQUE_HA_1")) {
808
                                return row.getObject("PRIX_METRIQUE_HA_1");
809
                            }
810
                            return row.getObject("PA_DEVISE");
811
                        }
812
                    }
813
 
814
                });
815
            }
816
 
28 ilm 817
            qteElement.addModificationListener(this.tableElementTotalDevise);
61 ilm 818
            qteU.addModificationListener(this.tableElementTotalDevise);
28 ilm 819
            tableElement_PA_Devise.addModificationListener(this.tableElementTotalDevise);
73 ilm 820
            if (e.getTable().contains("POURCENT_REMISE") && tableElementRemise != null) {
67 ilm 821
                tableElementRemise.addModificationListener(this.tableElementTotalDevise);
822
            }
28 ilm 823
            this.tableElementTotalDevise.setModifier(new CellDynamicModifier() {
93 ilm 824
                public Object computeValueFrom(final SQLRowValues row, SQLTableElement source) {
28 ilm 825
                    int qte = Integer.parseInt(row.getObject("QTE").toString());
67 ilm 826
                    BigDecimal f = (BigDecimal) row.getObject("PA_DEVISE");
61 ilm 827
                    BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
90 ilm 828
                    BigDecimal r = b.multiply(f.multiply(BigDecimal.valueOf(qte)), DecimalUtils.HIGH_PRECISION).setScale(tableElementTotalDevise.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
67 ilm 829
 
830
                    if (row.getTable().contains("POURCENT_REMISE")) {
831
                        final Object o2 = row.getObject("POURCENT_REMISE");
832
                        BigDecimal lremise = (o2 == null) ? BigDecimal.ZERO : ((BigDecimal) o2);
833
                        if (lremise.compareTo(BigDecimal.ZERO) >= 0 && lremise.compareTo(BigDecimal.valueOf(100)) < 0) {
834
 
835
                            r = r.multiply(new BigDecimal(100).subtract(lremise).movePointLeft(2)).setScale(tableElementTotalDevise.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
836
                        }
837
                    }
61 ilm 838
                    return r;
28 ilm 839
                }
19 ilm 840
 
28 ilm 841
            });
842
        }
18 ilm 843
        // Calcul automatique du total TTC
844
        qteElement.addModificationListener(this.tableElementTotalTTC);
61 ilm 845
        qteU.addModificationListener(this.tableElementTotalTTC);
18 ilm 846
        this.ha.addModificationListener(this.tableElementTotalTTC);
61 ilm 847
        this.tableElementTVA.addModificationListener(this.tableElementTotalTTC);
18 ilm 848
        this.tableElementTotalTTC.setModifier(new CellDynamicModifier() {
849
            @Override
93 ilm 850
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 851
                int qte = Integer.parseInt(row.getObject("QTE").toString());
67 ilm 852
                BigDecimal f = (BigDecimal) row.getObject("PA_HT");
18 ilm 853
                int idTaux = Integer.parseInt(row.getObject("ID_TAXE").toString());
177 ilm 854
                Float resultTaux = TaxeCache.getCache().getTauxFromId(idTaux);
855
                if (resultTaux == null) {
856
                    SQLRow rowTax = TaxeCache.getCache().getFirstTaxe();
857
                    row.put("ID_TAXE", rowTax.getID());
858
                    resultTaux = rowTax.getFloat("TAUX");
18 ilm 859
                }
156 ilm 860
                editorPAHT.setTaxe(resultTaux);
61 ilm 861
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
90 ilm 862
                BigDecimal r = b.multiply(f.multiply(BigDecimal.valueOf(qte), DecimalUtils.HIGH_PRECISION), DecimalUtils.HIGH_PRECISION).setScale(tableElementTotalTTC.getDecimalDigits(),
67 ilm 863
                        BigDecimal.ROUND_HALF_UP);
73 ilm 864
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
67 ilm 865
 
866
                BigDecimal total = r.multiply(BigDecimal.ONE.add(new BigDecimal(taux / 100f))).setScale(tableElementTotalTTC.getDecimalDigits(), RoundingMode.HALF_UP);
61 ilm 867
                return total;
18 ilm 868
            }
869
 
870
        });
871
 
872
        this.table.readState();
873
 
874
        // Mode Gestion article avancé
875
        String valModeAvanceVt = DefaultNXProps.getInstance().getStringProperty("ArticleModeVenteAvance");
876
        Boolean bModeAvance = Boolean.valueOf(valModeAvanceVt);
28 ilm 877
        boolean view = !(bModeAvance != null && !bModeAvance.booleanValue());
93 ilm 878
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_1"), view);
879
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_2"), view);
880
        setColumnVisible(model.getColumnForField("VALEUR_METRIQUE_3"), view);
881
        setColumnVisible(model.getColumnForField("PRIX_METRIQUE_VT_1"), view);
882
        setColumnVisible(model.getColumnForField("ID_MODE_VENTE_ARTICLE"), view);
883
        setColumnVisible(model.getColumnForField("PA_HT"), view);
18 ilm 884
 
142 ilm 885
        if (e.getTable().contains("ID_COMMANDE_ELEMENT")) {
886
            setColumnVisible(model.getColumnForField("ID_COMMANDE_ELEMENT"), false);
887
        }
888
 
61 ilm 889
        // Gestion des unités de vente
890
        final boolean gestionUV = prefs.getBoolean(GestionArticleGlobalPreferencePanel.UNITE_VENTE, true);
93 ilm 891
        setColumnVisible(model.getColumnForField("QTE_UNITAIRE"), gestionUV);
892
        setColumnVisible(model.getColumnForField("ID_UNITE_VENTE"), gestionUV);
61 ilm 893
 
93 ilm 894
        setColumnVisible(model.getColumnForField("ID_STYLE"), DefaultNXProps.getInstance().getBooleanValue("ArticleShowStyle", true));
61 ilm 895
 
142 ilm 896
        if (this.tableElementEco != null && this.tableElementEcoTotal != null && this.tableElementEcoID != null) {
897
            setColumnVisible(model.getColumnForField("ID_ECO_CONTRIBUTION"), showEco);
898
            setColumnVisible(model.getColumnForField("ECO_CONTRIBUTION"), showEco);
899
            setColumnVisible(model.getColumnForField("T_ECO_CONTRIBUTION"), showEco);
900
        }
901
 
93 ilm 902
        setColumnVisible(model.getColumnForField("ID_ARTICLE"), selectArticle);
903
        setColumnVisible(model.getColumnForField("CODE"), !selectArticle || (selectArticle && createAuto));
904
        setColumnVisible(model.getColumnForField("NOM"), !selectArticle || (selectArticle && createAuto));
61 ilm 905
 
156 ilm 906
        setColumnVisible(model.getColumnForField("ID_DEPOT_STOCK"), prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false));
907
 
177 ilm 908
        setColumnVisible(model.getColumnForField("T_POIDS_COLIS_NET"), false);
909
        setColumnVisible(model.getColumnForField("T_POIDS_BRUT"), false);
910
 
18 ilm 911
        // Calcul automatique du poids unitaire
912
        tableElement_ValeurMetrique1.addModificationListener(tableElementPoids);
913
        tableElement_ValeurMetrique2.addModificationListener(tableElementPoids);
914
        tableElement_ValeurMetrique3.addModificationListener(tableElementPoids);
915
        tableElementPoids.setModifier(new CellDynamicModifier() {
93 ilm 916
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 917
                return new Float(ReferenceArticleSQLElement.getPoidsFromDetails(row));
918
            }
919
 
920
        });
921
        // Calcul automatique du poids total
922
        tableElementPoids.addModificationListener(this.tableElementPoidsTotal);
923
        qteElement.addModificationListener(this.tableElementPoidsTotal);
61 ilm 924
        qteU.addModificationListener(this.tableElementPoidsTotal);
18 ilm 925
        this.tableElementPoidsTotal.setModifier(new CellDynamicModifier() {
93 ilm 926
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
73 ilm 927
 
65 ilm 928
                Number f = (row.getObject("POIDS") == null) ? 0 : (Number) row.getObject("POIDS");
18 ilm 929
                int qte = Integer.parseInt(row.getObject("QTE").toString());
61 ilm 930
 
931
                BigDecimal b = (row.getObject("QTE_UNITAIRE") == null) ? BigDecimal.ONE : (BigDecimal) row.getObject("QTE_UNITAIRE");
932
                // FIXME convertir en float autrement pour éviter une valeur non transposable
933
                // avec floatValue ou passer POIDS en bigDecimal
934
                return b.multiply(new BigDecimal(f.floatValue() * qte)).floatValue();
18 ilm 935
            }
936
 
937
        });
938
 
93 ilm 939
        tableElement_PrixMetrique1_AchatHT.setModifier(new CellDynamicModifier() {
940
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
941
                if (source != null && source.getField().getName().equals("PRIX_METRIQUE_HA_1")) {
942
                    return row.getObject("PRIX_METRIQUE_HA_1");
943
                } else {
944
                    if (source != null && source.getField().getName().equals("PA_DEVISE")) {
142 ilm 945
                        if (!row.isForeignEmpty("ID_DEVISE") && row.getForeign("ID_DEVISE") != null) {
93 ilm 946
                            String devCode = row.getForeign("ID_DEVISE").getString("CODE");
947
                            BigDecimal bigDecimal = (BigDecimal) row.getObject("PA_DEVISE");
948
 
94 ilm 949
                            return convert(bigDecimal, devCode, false);
93 ilm 950
                        } else {
951
                            return row.getObject("PRIX_METRIQUE_HA_1");
952
                        }
953
                    }
954
                    return row.getObject("PRIX_METRIQUE_HA_1");
955
                }
956
            }
957
 
958
        });
959
 
18 ilm 960
        // Calcul automatique du prix d'achat unitaire HT
961
        tableElement_ValeurMetrique1.addModificationListener(this.ha);
962
        tableElement_ValeurMetrique2.addModificationListener(this.ha);
963
        tableElement_ValeurMetrique3.addModificationListener(this.ha);
964
        tableElement_PrixMetrique1_AchatHT.addModificationListener(this.ha);
965
        this.ha.setModifier(new CellDynamicModifier() {
93 ilm 966
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 967
 
93 ilm 968
                if (row.isForeignEmpty("ID_MODE_VENTE_ARTICLE") || row.getInt("ID_MODE_VENTE_ARTICLE") == ReferenceArticleSQLElement.A_LA_PIECE) {
67 ilm 969
                    return row.getObject("PRIX_METRIQUE_HA_1");
18 ilm 970
                } else {
971
 
67 ilm 972
                    final BigDecimal prixHAFromDetails = ReferenceArticleSQLElement.getPrixHAFromDetails(row);
973
                    return prixHAFromDetails.setScale(ha.getDecimalDigits(), RoundingMode.HALF_UP);
18 ilm 974
                }
975
 
976
            }
977
 
978
        });
979
 
61 ilm 980
        uniteVente.addModificationListener(qteU);
981
        qteU.setModifier(new CellDynamicModifier() {
93 ilm 982
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
61 ilm 983
                SQLRowAccessor rowUnite = row.getForeign("ID_UNITE_VENTE");
984
                if (rowUnite != null && !rowUnite.isUndefined() && rowUnite.getBoolean("A_LA_PIECE")) {
985
                    return BigDecimal.ONE;
986
                } else {
987
                    return row.getObject("QTE_UNITAIRE");
988
                }
989
            }
990
 
991
        });
132 ilm 992
        // La devise est renseignée globalement dans la commande et est reportée automatiquement sur
993
        // les lignes
994
        setColumnVisible(model.getColumnIndexForElement(tableElement_Devise), false);
28 ilm 995
        for (String string : visibilityMap.keySet()) {
93 ilm 996
            setColumnVisible(model.getColumnForField(string), visibilityMap.get(string));
28 ilm 997
        }
998
 
142 ilm 999
        // Barcode reader
1000
        final BarcodeReader barcodeReader = ComptaPropsConfiguration.getInstanceCompta().getBarcodeReader();
1001
        if (barcodeReader != null) {
1002
 
1003
            final BarcodeListener l = new BarcodeListener() {
1004
 
1005
                @Override
1006
                public void keyReceived(KeyEvent ee) {
1007
                    // TODO Auto-generated method stub
1008
                }
1009
 
1010
                @Override
1011
                public void barcodeRead(String code) {
1012
                    if (((JFrame) SwingUtilities.getRoot(getRowValuesTable())).isActive()) {
1013
                        final SQLSelect selArticle = new SQLSelect();
1014
                        final SQLTable tableArticle = getSQLElement().getForeignElement("ID_ARTICLE").getTable();
1015
                        selArticle.addSelectStar(tableArticle);
1016
                        Where w = new Where(tableArticle.getField("OBSOLETE"), "=", Boolean.FALSE);
1017
                        w = w.and(new Where(tableArticle.getField("CODE_BARRE"), "=", code));
1018
                        selArticle.setWhere(w);
1019
                        List<SQLRow> l2 = SQLRowListRSH.execute(selArticle);
1020
                        if (l2.size() > 0) {
1021
                            System.err.println("ARTICLE " + l2.get(0).getString("NOM"));
1022
                            Tuple3<Double, String, String> art = Tuple3.create(1.0D, l2.get(0).getString("CODE"), l2.get(0).getString("NOM"));
1023
                            List<Tuple3<Double, String, String>> l = new ArrayList<Tuple3<Double, String, String>>();
1024
                            l.add(art);
1025
                            insertFromDrop(l, m);
1026
                        } else {
1027
                            System.err.println("ARTICLE NOT FOUND !");
1028
                        }
1029
                    }
1030
 
1031
                }
1032
            };
1033
            getRowValuesTable().addHierarchyListener(new HierarchyListener() {
1034
                public void hierarchyChanged(HierarchyEvent e) {
1035
                    if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0)
1036
                        if (getRowValuesTable().isDisplayable()) {
1037
                            barcodeReader.addBarcodeListener(l);
1038
                        } else {
1039
                            barcodeReader.removeBarcodeListener(l);
1040
                        }
1041
                }
1042
            });
1043
 
1044
        }
1045
 
19 ilm 1046
        // On réécrit la configuration au cas ou les preferences aurait changé
18 ilm 1047
        this.table.writeState();
177 ilm 1048
 
1049
        if (this.table.getRowValuesTableModel().getColumnForField("ID_DEPOT_STOCK") >= 0 && this.table.getRowValuesTableModel().getColumnForField("ID_ARTICLE") >= 0) {
1050
            if (this.buttons == null) {
1051
                this.buttons = new ArrayList<>();
1052
            }
1053
 
1054
            JButton buttonStock = new JButton("Consulter le stock");
1055
            buttonStock.addActionListener(new ActionListener() {
1056
                public void actionPerformed(ActionEvent event) {
1057
                    SQLRowValues rowValsSel = table.getSelectedRowValues();
1058
                    if (rowValsSel != null) {
1059
                        SQLRowAccessor foreignArt = rowValsSel.getForeign("ID_ARTICLE");
1060
                        if (foreignArt != null && !foreignArt.isUndefined()) {
1061
                            SQLRowAccessor rowValsStock = StockSQLElement.getStock(rowValsSel);
1062
                            if (rowValsStock != null && !rowValsStock.isUndefined()) {
1063
                                EditFrame frame = new EditFrame(table.getRowValuesTableModel().getSQLElement().getDirectory().getElement("STOCK"), EditMode.READONLY);
1064
                                frame.selectionId(rowValsStock.getID());
1065
                                frame.setVisible(true);
1066
                            }
1067
                        }
1068
                    }
1069
 
1070
                }
1071
            });
1072
 
1073
            this.buttons.add(buttonStock);
1074
 
1075
        }
18 ilm 1076
    }
1077
 
28 ilm 1078
    private static Map<String, Boolean> visibilityMap = new HashMap<String, Boolean>();
1079
 
1080
    public static Map<String, Boolean> getVisibilityMap() {
1081
        return visibilityMap;
1082
    }
1083
 
132 ilm 1084
    private String incoterm = "";
1085
 
1086
    public void setIncoterms(String incoterm) {
1087
        if (incoterm == null) {
1088
            incoterm = "";
1089
        }
1090
        this.incoterm = incoterm;
1091
    }
1092
 
1093
    private SQLRow rowFournisseur = null;
1094
 
67 ilm 1095
    public void setFournisseur(SQLRow rowFournisseur) {
132 ilm 1096
        this.rowFournisseur = rowFournisseur;
67 ilm 1097
        if (getSQLElement().getTable().contains("ID_CODE_FOURNISSEUR") && this.supplierCode) {
1098
 
1099
            if (rowFournisseur != null && !rowFournisseur.isUndefined()) {
1100
                Where w = new Where(getSQLElement().getTable().getTable("CODE_FOURNISSEUR").getField("ID_FOURNISSEUR"), "=", rowFournisseur.getID());
1101
                this.m4.setWhere(w);
1102
            } else {
1103
                this.m4.setWhere(null);
1104
            }
1105
        }
1106
    }
1107
 
93 ilm 1108
    private BigDecimal getPrice(final SQLRowAccessor r, List<String> list) {
1109
        BigDecimal result = r.getBigDecimal(list.get(0));
1110
 
1111
        for (int i = 1; i < list.size(); i++) {
1112
            BigDecimal m0 = r.getBigDecimal(list.get(i));
1113
            if (m0 != null && m0.floatValue() > 0) {
1114
                result = result.divide(m0, 2, RoundingMode.HALF_UP);
1115
            }
1116
        }
1117
        return result;
1118
    }
1119
 
149 ilm 1120
    public Object tarifCompletion(SQLRow row, String field) {
132 ilm 1121
        final SQLTable tTarifFournisseur = this.getSQLElement().getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
94 ilm 1122
 
177 ilm 1123
        if (row != null && !row.isUndefined() && (field.equalsIgnoreCase("ID_CODE_FOURNISSEUR")) && this.rowFournisseur != null && !this.rowFournisseur.isUndefined()) {
1124
            final SQLTable foreignTableCodeF = getSQLElement().getTable().getForeignTable("ID_CODE_FOURNISSEUR");
1125
            List<SQLRow> resultCode = row.getReferentRows(foreignTableCodeF);
1126
            int idCode = foreignTableCodeF.getUndefinedID();
1127
            for (SQLRow sqlRow : resultCode) {
1128
                if (sqlRow.getForeignID("ID_FOURNISSEUR") == this.rowFournisseur.getID()) {
1129
                    return sqlRow.getID();
1130
                }
1131
 
1132
            }
1133
            return idCode;
1134
        }
1135
        if (field.equalsIgnoreCase("ID_CODE_FOURNISSEUR")) {
1136
            final SQLTable foreignTableCodeF = getSQLElement().getTable().getForeignTable("ID_CODE_FOURNISSEUR");
1137
            return foreignTableCodeF.getUndefinedID();
1138
        }
1139
 
174 ilm 1140
        if (row != null && !row.isUndefined() && (field.equalsIgnoreCase("PRIX_METRIQUE_HA_1") || field.equalsIgnoreCase("PA_HT")) && tTarifFournisseur != null) {
132 ilm 1141
            List<String> incoTerms;
1142
 
1143
            if (this.incoterm != null && this.incoterm.equalsIgnoreCase("CPT")) {
1144
                incoTerms = Arrays.asList("PRIX_ACHAT", "COEF_TRANSPORT_PORT");
1145
            } else if (this.incoterm != null && this.incoterm.equalsIgnoreCase("DDP")) {
1146
                incoTerms = Arrays.asList("PRIX_ACHAT", "COEF_TRANSPORT_PORT", "COEF_TAXE_D");
1147
            } else {
1148
                incoTerms = Arrays.asList("PRIX_ACHAT");
1149
            }
93 ilm 1150
            List<SQLRow> rows = row.getReferentRows(tTarifFournisseur);
177 ilm 1151
            if (row.getBoolean("AUTO_PRIX_ACHAT_NOMENCLATURE") && row.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
19 ilm 1152
 
132 ilm 1153
                List<SQLRow> rowsElt = row.getReferentRows(row.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"));
1154
                BigDecimal price = BigDecimal.ZERO;
1155
                final Set<String> tarifNotFound = new HashSet<String>();
1156
                for (SQLRow sqlRow : rowsElt) {
1157
                    List<SQLRow> rowsT = sqlRow.getForeign("ID_ARTICLE").getReferentRows(tTarifFournisseur);
94 ilm 1158
 
132 ilm 1159
                    boolean priceFound = false;
1160
                    boolean tarifFound = false;
1161
                    if (rowsT.size() > 0) {
1162
                        BigDecimal min = BigDecimal.ZERO;
1163
 
174 ilm 1164
                        BigDecimal defaultPrice = sqlRow.getForeign("ID_ARTICLE").getBigDecimal("PRIX_METRIQUE_HA_1");
132 ilm 1165
                        Calendar c = null;
1166
                        for (SQLRow sqlRowT : rowsT) {
1167
                            if (this.rowFournisseur != null && this.rowFournisseur.getID() == sqlRowT.getForeignID("ID_FOURNISSEUR")) {
1168
                                BigDecimal priceT = getPrice(sqlRowT, incoTerms);
1169
                                defaultPrice = priceT;
1170
                                final Calendar datePrice = sqlRowT.getDate("DATE_PRIX");
142 ilm 1171
                                datePrice.set(Calendar.HOUR, 0);
1172
                                datePrice.set(Calendar.MINUTE, 0);
1173
                                datePrice.set(Calendar.SECOND, 0);
1174
                                datePrice.set(Calendar.MILLISECOND, 0);
132 ilm 1175
                                if (datePrice == null || (this.getDateDevise() != null && !this.getDateDevise().before(datePrice.getTime()))) {
1176
                                    if (c == null || c.before(datePrice)) {
1177
 
1178
                                        min = priceT;
1179
                                        c = datePrice;
1180
                                        priceFound = true;
1181
                                    } else if (c != null) {
1182
                                        defaultPrice = priceT;
1183
                                    }
1184
                                }
1185
                                tarifFound = true;
1186
                            }
1187
                        }
1188
 
1189
                        if (priceFound) {
1190
                            price = price.add(min.multiply(sqlRow.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRow.getInt("QTE"), DecimalUtils.HIGH_PRECISION))));
1191
                        } else {
1192
                            price = price.add(defaultPrice.multiply(sqlRow.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRow.getInt("QTE"), DecimalUtils.HIGH_PRECISION))));
1193
                        }
1194
                    }
1195
                    if (!tarifFound) {
1196
                        tarifNotFound.add(sqlRow.getForeign("ID_ARTICLE").getString("CODE"));
1197
                    }
93 ilm 1198
                }
132 ilm 1199
                if (!tarifNotFound.isEmpty()) {
1200
                    SwingUtilities.invokeLater(new Runnable() {
93 ilm 1201
 
132 ilm 1202
                        @Override
1203
                        public void run() {
1204
                            JOptionPane.showMessageDialog(AbstractAchatArticleItemTable.this.table,
1205
                                    "Attention, impossible de calculer le tarif.\nLes articles suivants n'ont pas de tarif associé :\n" + tarifNotFound);
1206
                        }
1207
                    });
1208
                }
1209
                return price;
1210
            } else if (!rows.isEmpty()) {
174 ilm 1211
 
1212
                BigDecimal min = row.getBigDecimal("PRIX_METRIQUE_HA_1");
1213
 
132 ilm 1214
                Calendar c = null;
93 ilm 1215
                for (SQLRow sqlRow : rows) {
132 ilm 1216
                    if (this.rowFournisseur != null && this.rowFournisseur.getID() == sqlRow.getForeignID("ID_FOURNISSEUR")) {
1217
                        BigDecimal price = getPrice(sqlRow, incoTerms);
1218
                        final Calendar datePrice = sqlRow.getDate("DATE_PRIX");
1219
                        if (datePrice == null || (this.getDateDevise() != null && !this.getDateDevise().before(datePrice.getTime()))) {
1220
                            if (c == null || c.before(datePrice)) {
1221
                                min = price;
1222
                                c = datePrice;
1223
                            }
1224
                        }
93 ilm 1225
                    }
1226
                }
94 ilm 1227
                return min.setScale(2, RoundingMode.HALF_UP);
93 ilm 1228
            }
1229
        }
94 ilm 1230
        if (field.equalsIgnoreCase("INCOTERM")) {
132 ilm 1231
            // if (tTarifFournisseur != null) {
1232
            // List<SQLRow> rows = row.getReferentRows(tTarifFournisseur);
1233
            // if (!rows.isEmpty()) {
1234
            // final SQLRow sqlRow0 = rows.get(0);
1235
            // return sqlRow0.getString("CONDITIONS");
1236
            // }
1237
            // }
1238
            return this.incoterm;
94 ilm 1239
        }
93 ilm 1240
 
19 ilm 1241
        if (getDevise() != null && !getDevise().isUndefined()) {
1242
            if ((field.equalsIgnoreCase("ID_DEVISE") || field.equalsIgnoreCase("ID_DEVISE_HA"))) {
1243
                return getDevise().getID();
1244
            } else if ((field.equalsIgnoreCase("PA_DEVISE"))) {
132 ilm 1245
                if (row.getBigDecimal("PA_DEVISE") != null && row.getBigDecimal("PA_DEVISE").signum() != 0 && this.incoterm.length() == 0) {
93 ilm 1246
                    return row.getBigDecimal("PA_DEVISE");
1247
                } else {
132 ilm 1248
                    String devCode = getDevise().getString("CODE");
93 ilm 1249
 
132 ilm 1250
                    BigDecimal tarifCompletion = (BigDecimal) tarifCompletion(row, "PRIX_METRIQUE_HA_1");
1251
                    if (tarifCompletion == null) {
1252
                        tarifCompletion = row.getBigDecimal("PRIX_METRIQUE_HA_1");
93 ilm 1253
                    }
132 ilm 1254
                    if (tarifCompletion == null) {
1255
                        return null;
1256
                    } else {
1257
                        // Remove set scale 2 --> if scale ex : PA = 95.74MAD --> 0.76592€
1258
                        // with Scale 0.77€ so put 96.25MAD in PA
1259
                        return convert(tarifCompletion, devCode, true);
1260
                    }
93 ilm 1261
                }
132 ilm 1262
 
19 ilm 1263
            }
1264
        } else {
1265
            if ((field.equalsIgnoreCase("ID_DEVISE") || field.equalsIgnoreCase("ID_DEVISE_HA"))) {
1266
                return Configuration.getInstance().getDirectory().getElement("DEVISE").getTable().getUndefinedID();
1267
            } else if ((field.equalsIgnoreCase("PA_DEVISE"))) {
1268
 
67 ilm 1269
                return BigDecimal.ZERO;
19 ilm 1270
            }
156 ilm 1271
 
19 ilm 1272
        }
156 ilm 1273
        if (field.equalsIgnoreCase("ID_TAXE") && this.rowCatComptable != null) {
1274
            return this.rowCatComptable.getForeignID("ID_TAXE_VENTE");
1275
        }
19 ilm 1276
        return null;
132 ilm 1277
 
19 ilm 1278
    }
1279
 
93 ilm 1280
    @Override
1281
    protected void refreshDeviseAmount() {
1282
        int count = getRowValuesTable().getRowCount();
1283
        final int columnForField = getRowValuesTable().getRowValuesTableModel().getColumnForField("PA_DEVISE");
1284
        if (columnForField >= 0) {
1285
            SQLTableElement eltDevise = getRowValuesTable().getRowValuesTableModel().getSQLTableElementAt(columnForField);
1286
            for (int i = 0; i < count; i++) {
1287
                SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
1288
                // getRowValuesTable().getRowValuesTableModel().putValue(rowVals.getObject("PV_U_DEVISE"),
1289
                // i, "PV_U_DEVISE", true);
1290
                eltDevise.setValueFrom(rowVals, rowVals.getObject("PA_DEVISE"));
1291
                getRowValuesTable().getRowValuesTableModel().fireTableChanged(new TableModelEvent(getRowValuesTable().getRowValuesTableModel(), i, i, columnForField));
1292
            }
1293
        }
1294
    }
1295
 
19 ilm 1296
    public SQLRowAccessor getDevise() {
1297
        return this.rowDevise;
1298
    }
1299
 
1300
    public void setDevise(SQLRowAccessor deviseRow) {
1301
        this.rowDevise = deviseRow;
94 ilm 1302
        if (deviseRow == null) {
1303
            getDefaultRowValues().put("ID_DEVISE", null);
1304
        } else {
1305
            getDefaultRowValues().put("ID_DEVISE", this.rowDevise.getID());
1306
        }
19 ilm 1307
    }
41 ilm 1308
 
94 ilm 1309
    private BigDecimal tauxConversion = null;
1310
 
1311
    public void setTauxConversion(BigDecimal tauxConversion) {
1312
        if (tauxConversion != null && tauxConversion.signum() == 0) {
1313
            tauxConversion = null;
1314
        }
1315
        this.tauxConversion = tauxConversion;
1316
        refreshDeviseAmount();
1317
    }
1318
 
1319
    public BigDecimal convert(BigDecimal val, String devCode, boolean fromCompanyCurrency) {
1320
        if (val == null) {
1321
            return val;
1322
        }
1323
        if (this.tauxConversion == null) {
1324
            CurrencyConverter c = new CurrencyConverter();
1325
            if (fromCompanyCurrency) {
1326
                return c.convert(val, c.getCompanyCurrencyCode(), devCode, getDateDevise(), isUsedBiasedDevise());
1327
            } else {
1328
                return c.convert(val, devCode, c.getCompanyCurrencyCode(), getDateDevise(), isUsedBiasedDevise());
1329
            }
1330
        } else {
1331
            if (fromCompanyCurrency) {
1332
                return val.divide(this.tauxConversion, DecimalUtils.HIGH_PRECISION).setScale(val.scale(), RoundingMode.HALF_UP);
1333
            } else {
1334
                return val.multiply(this.tauxConversion, DecimalUtils.HIGH_PRECISION).setScale(val.scale(), RoundingMode.HALF_UP);
1335
            }
1336
        }
1337
    }
1338
 
41 ilm 1339
    public void setFournisseurFilterOnCompletion(SQLRow row) {
93 ilm 1340
        final SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
1341
        Where w = new Where(sqlTableArticle.getField("OBSOLETE"), "=", Boolean.FALSE);
41 ilm 1342
        if (row != null && !row.isUndefined()) {
93 ilm 1343
            w = w.and(new Where(this.tableArticle.getField("ID_FOURNISSEUR"), "=", row.getID()));
41 ilm 1344
        }
93 ilm 1345
        this.m.setWhere(w);
1346
        this.m2.setWhere(w);
1347
        this.m3.setWhere(w);
41 ilm 1348
    }
1349
 
142 ilm 1350
    private void insertFromDrop(List<Tuple3<Double, String, String>> articles, AutoCompletionManager m) {
94 ilm 1351
 
142 ilm 1352
        List<String> code = new ArrayList<String>(articles.size());
1353
        for (int i = articles.size() - 1; i >= 0; i--) {
94 ilm 1354
 
142 ilm 1355
            Tuple3<Double, String, String> tuple = articles.get(i);
1356
            code.add(tuple.get1());
1357
        }
94 ilm 1358
 
142 ilm 1359
        int rowCount = getRowValuesTable().getRowValuesTableModel().getRowCount();
1360
        Map<Integer, Integer> mapRows = new HashMap<Integer, Integer>();
1361
        for (int i = 0; i < rowCount; i++) {
1362
            SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
1363
            if (rowVals.getObject("ID_ARTICLE") != null && !rowVals.isForeignEmpty("ID_ARTICLE")) {
1364
                mapRows.put(rowVals.getForeignID("ID_ARTICLE"), i);
1365
            }
1366
        }
94 ilm 1367
 
142 ilm 1368
        SQLSelect sel = new SQLSelect();
1369
        final SQLTable articleTable = getSQLElement().getTable().getForeignTable("ID_ARTICLE");
1370
        sel.addSelectStar(articleTable);
1371
        sel.setWhere(new Where(articleTable.getField("CODE"), code));
1372
        List<SQLRow> matchCode = SQLRowListRSH.execute(sel);
1373
        Map<String, SQLRow> mapCode = new HashMap<String, SQLRow>();
1374
        for (SQLRow sqlRow : matchCode) {
1375
            mapCode.put(sqlRow.getString("CODE"), sqlRow);
1376
        }
1377
        Set<String> fieldsFrom = m.getFieldsFrom();
1378
        fieldsFrom.remove("POURCENT_REMISE");
1379
        for (int i = articles.size() - 1; i >= 0; i--) {
94 ilm 1380
 
142 ilm 1381
            Tuple3<Double, String, String> tuple = articles.get(i);
94 ilm 1382
 
142 ilm 1383
            SQLRow article = mapCode.get(tuple.get1());
1384
            if (article != null && mapRows.containsKey(article.getID())) {
1385
                Integer index = mapRows.get(article.getID());
1386
                SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(index);
1387
                getRowValuesTable().getRowValuesTableModel().putValue(rowVals.getInt("QTE") + 1, index, "QTE");
1388
            } else {
1389
                final SQLRowValues row2Insert = new SQLRowValues(getRowValuesTable().getRowValuesTableModel().getDefaultRowValues());
1390
 
1391
                // Completion depuis l'article trouvé
1392
                if (article != null) {
1393
                    m.fillRowValues(article, fieldsFrom, row2Insert);
1394
                    // Fill prix total
1395
                    row2Insert.put("ID_ARTICLE", article.getID());
1396
                    row2Insert.put("CODE", article.getObject("CODE"));
1397
                    row2Insert.put("NOM", article.getObject("NOM"));
1398
                } else {
1399
                    row2Insert.put("CODE", tuple.get1());
1400
                    row2Insert.put("NOM", tuple.get2());
1401
                }
1402
 
1403
                row2Insert.put("QTE", Math.round(tuple.get0().floatValue()));
1404
 
1405
                row2Insert.put("PA_HT", row2Insert.getObject("PRIX_METRIQUE_HA_1"));
1406
                //
1407
                final BigDecimal resultTotalHT = row2Insert.getBigDecimal("PA_HT").multiply(new BigDecimal(row2Insert.getInt("QTE")));
1408
                row2Insert.put("T_PA_HT", resultTotalHT);
1409
 
1410
                Float resultTaux = TaxeCache.getCache().getTauxFromId(row2Insert.getForeignID("ID_TAXE"));
1411
 
1412
                if (resultTaux == null) {
177 ilm 1413
                    SQLRow rowTax = TaxeCache.getCache().getFirstTaxeAchat();
142 ilm 1414
                    resultTaux = rowTax.getFloat("TAUX");
1415
                }
1416
 
1417
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
1418
 
1419
                BigDecimal r = resultTotalHT.multiply(BigDecimal.valueOf(taux).movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
1420
 
1421
                row2Insert.put("T_PA_TTC", r);
1422
                // row2Insert.put("ID_STYLE", allStyleByName.get("Composant"));
1423
                getRowValuesTable().getRowValuesTableModel().addRowAt(0, row2Insert);
1424
            }
94 ilm 1425
        }
1426
    }
1427
 
18 ilm 1428
}