OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.product.component;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
63 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
67 ilm 18
import org.openconcerto.erp.core.common.ui.CodeFournisseurItemTable;
21 ilm 19
import org.openconcerto.erp.core.common.ui.TotalPanel;
93 ilm 20
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
18 ilm 21
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
144 ilm 22
import org.openconcerto.erp.core.sales.product.element.ArticleCodeClientTable;
18 ilm 23
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
156 ilm 24
import org.openconcerto.erp.core.sales.product.element.SupplierPriceListTable;
61 ilm 25
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
156 ilm 26
import org.openconcerto.erp.core.sales.product.ui.ArticleCategorieComptableTable;
19 ilm 27
import org.openconcerto.erp.core.sales.product.ui.ArticleDesignationTable;
28
import org.openconcerto.erp.core.sales.product.ui.ArticleTarifTable;
94 ilm 29
import org.openconcerto.erp.core.sales.product.ui.ProductItemListTable;
30
import org.openconcerto.erp.core.sales.product.ui.ProductQtyPriceListTable;
31
import org.openconcerto.erp.core.sales.product.ui.RowValuesTableEditionPanel;
156 ilm 32
import org.openconcerto.erp.core.supplychain.stock.element.ComposedItemStockUpdater;
33
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
34
import org.openconcerto.erp.core.supplychain.stock.element.StockItem;
67 ilm 35
import org.openconcerto.erp.model.ISQLCompteSelector;
18 ilm 36
import org.openconcerto.erp.preferences.DefaultNXProps;
61 ilm 37
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 38
import org.openconcerto.sql.Configuration;
39
import org.openconcerto.sql.element.BaseSQLComponent;
40
import org.openconcerto.sql.element.SQLElement;
41
import org.openconcerto.sql.model.SQLRow;
42
import org.openconcerto.sql.model.SQLRowAccessor;
43
import org.openconcerto.sql.model.SQLRowListRSH;
44
import org.openconcerto.sql.model.SQLRowValues;
45
import org.openconcerto.sql.model.SQLSelect;
156 ilm 46
import org.openconcerto.sql.model.SQLTable;
18 ilm 47
import org.openconcerto.sql.model.Where;
61 ilm 48
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 49
import org.openconcerto.sql.sqlobject.ElementComboBox;
156 ilm 50
import org.openconcerto.sql.sqlobject.SQLTextCombo;
18 ilm 51
import org.openconcerto.ui.DefaultGridBagConstraints;
27 ilm 52
import org.openconcerto.ui.FormLayouter;
142 ilm 53
import org.openconcerto.ui.TitledSeparator;
19 ilm 54
import org.openconcerto.ui.component.ITextArea;
18 ilm 55
import org.openconcerto.ui.preferences.DefaultProps;
90 ilm 56
import org.openconcerto.utils.DecimalUtils;
156 ilm 57
import org.openconcerto.utils.ExceptionHandler;
83 ilm 58
import org.openconcerto.utils.StringUtils;
18 ilm 59
import org.openconcerto.utils.text.SimpleDocumentListener;
60
 
19 ilm 61
import java.awt.Component;
18 ilm 62
import java.awt.GridBagConstraints;
63
import java.awt.GridBagLayout;
80 ilm 64
import java.awt.Insets;
19 ilm 65
import java.awt.event.ActionEvent;
66
import java.awt.event.ActionListener;
18 ilm 67
import java.beans.PropertyChangeEvent;
68
import java.beans.PropertyChangeListener;
19 ilm 69
import java.math.BigDecimal;
67 ilm 70
import java.math.RoundingMode;
18 ilm 71
import java.sql.SQLException;
156 ilm 72
import java.util.ArrayList;
93 ilm 73
import java.util.Date;
18 ilm 74
import java.util.List;
75
 
19 ilm 76
import javax.swing.JButton;
18 ilm 77
import javax.swing.JCheckBox;
78
import javax.swing.JLabel;
19 ilm 79
import javax.swing.JOptionPane;
18 ilm 80
import javax.swing.JPanel;
81
import javax.swing.JSeparator;
19 ilm 82
import javax.swing.JTabbedPane;
18 ilm 83
import javax.swing.JTextField;
84
import javax.swing.SwingConstants;
85
import javax.swing.event.DocumentEvent;
86
import javax.swing.event.DocumentListener;
87
 
88
public class ReferenceArticleSQLComponent extends BaseSQLComponent {
89
 
67 ilm 90
    private JTextField textPVHT, textPVTTC, textPAHT;
91
    private JTextField textMetrique1VT, textMetrique1HA;
18 ilm 92
 
73 ilm 93
    private final JCheckBox boxService = new JCheckBox(getLabelFor("SERVICE"));
94
    private final JCheckBox checkObs = new JCheckBox(getLabelFor("OBSOLETE"));
18 ilm 95
    private JTextField textNom, textCode;
96
    private JTextField textPoids;
97
    private JTextField textValMetrique1, textValMetrique2, textValMetrique3;
98
    private DocumentListener htDocListener, ttcDocListener, detailsListener;
73 ilm 99
    private PropertyChangeListener propertyChangeListener;
18 ilm 100
    private PropertyChangeListener taxeListener;
73 ilm 101
    private final ElementComboBox comboSelTaxe = new ElementComboBox(false, 10);
102
    private final ElementComboBox comboSelModeVente = new ElementComboBox(false, 25);
65 ilm 103
    private JLabel labelMetriqueHA1 = new JLabel(getLabelFor("PRIX_METRIQUE_HA_1"), SwingConstants.RIGHT);
104
    private JLabel labelMetriqueVT1 = new JLabel(getLabelFor("PRIX_METRIQUE_VT_1"), SwingConstants.RIGHT);
19 ilm 105
 
73 ilm 106
    private ArticleDesignationTable tableDes = new ArticleDesignationTable();
144 ilm 107
    private ArticleCodeClientTable tableCodeClient = new ArticleCodeClientTable();
73 ilm 108
    private ArticleTarifTable tableTarifVente = new ArticleTarifTable(this);
156 ilm 109
    private ArticleCategorieComptableTable tableCatComptable = new ArticleCategorieComptableTable();
110
    private SupplierPriceListTable tableFourSec = new SupplierPriceListTable();
111
 
94 ilm 112
    private ProductQtyPriceListTable tableTarifQteVente = new ProductQtyPriceListTable(this);
113
    private ProductItemListTable tableBom;
156 ilm 114
    private final JTextField textMarge = new JTextField(10);
115
    private final JLabel labelMarge = new JLabel("%           ");
116
    private ElementComboBox boxCR;
117
    private JCheckBox boxMargeWithCR;
19 ilm 118
 
93 ilm 119
    private DocumentListener pieceHAArticle = new SimpleDocumentListener() {
18 ilm 120
 
93 ilm 121
        @Override
122
        public void update(DocumentEvent e) {
18 ilm 123
 
93 ilm 124
            if (!ReferenceArticleSQLComponent.this.textPAHT.getText().equalsIgnoreCase(ReferenceArticleSQLComponent.this.textMetrique1HA.getText())) {
125
                ReferenceArticleSQLComponent.this.textMetrique1HA.setText(ReferenceArticleSQLComponent.this.textPAHT.getText());
126
            }
18 ilm 127
        }
128
 
129
    };
93 ilm 130
    private DocumentListener pieceVTArticle = new SimpleDocumentListener() {
18 ilm 131
 
93 ilm 132
        @Override
133
        public void update(DocumentEvent e) {
134
            if (!ReferenceArticleSQLComponent.this.textPVHT.getText().equalsIgnoreCase(ReferenceArticleSQLComponent.this.textMetrique1VT.getText())) {
135
                ReferenceArticleSQLComponent.this.textMetrique1VT.setText(ReferenceArticleSQLComponent.this.textPVHT.getText());
136
            }
18 ilm 137
        }
138
 
139
    };
140
 
141
    private DocumentListener listenerMargeTextMarge = new SimpleDocumentListener() {
142
        @Override
143
        public void update(DocumentEvent e) {
144
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
145
            updateVtFromMarge();
146
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
147
        }
148
 
149
    };
150
 
151
    private DocumentListener listenerMargeTextVT = new SimpleDocumentListener() {
152
        @Override
153
        public void update(DocumentEvent e) {
154
            ReferenceArticleSQLComponent.this.textMarge.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextMarge);
155
            if (ReferenceArticleSQLComponent.this.textPVHT.getText().trim().length() > 0 && ReferenceArticleSQLComponent.this.textPAHT.getText().trim().length() > 0) {
83 ilm 156
                final BigDecimal vt = StringUtils.getBigDecimalFromUserText(ReferenceArticleSQLComponent.this.textPVHT.getText());
156 ilm 157
                BigDecimal ha = StringUtils.getBigDecimalFromUserText(ReferenceArticleSQLComponent.this.textPAHT.getText());
67 ilm 158
 
18 ilm 159
                if (vt != null && ha != null) {
73 ilm 160
                    if (vt.signum() != 0 && ha.signum() != 0) {
156 ilm 161
 
162
                        if (boxMargeWithCR.isSelected() && boxCR != null) {
163
                            SQLRow rowCR = boxCR.getSelectedRow();
164
                            if (rowCR != null && !rowCR.isUndefined()) {
165
                                BigDecimal cr = rowCR.getBigDecimal("POURCENT");
166
                                ha = ha.multiply(cr.movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
167
                            }
168
                        }
169
 
67 ilm 170
                        BigDecimal margeHT = vt.subtract(ha);
21 ilm 171
 
67 ilm 172
                        BigDecimal value;
173
 
21 ilm 174
                        if (DefaultNXProps.getInstance().getBooleanValue(TotalPanel.MARGE_MARQUE, false)) {
67 ilm 175
                            if (vt.compareTo(BigDecimal.ZERO) > 0) {
90 ilm 176
                                value = margeHT.divide(vt, DecimalUtils.HIGH_PRECISION).multiply(BigDecimal.valueOf(100), DecimalUtils.HIGH_PRECISION);
21 ilm 177
                            } else {
67 ilm 178
                                value = BigDecimal.ZERO;
21 ilm 179
                            }
180
                        } else {
90 ilm 181
                            value = margeHT.divide(ha, DecimalUtils.HIGH_PRECISION).multiply(BigDecimal.valueOf(100), DecimalUtils.HIGH_PRECISION);
21 ilm 182
                        }
183
 
67 ilm 184
                        if (value.compareTo(BigDecimal.ZERO) > 0) {
185
                            ReferenceArticleSQLComponent.this.textMarge.setText(value.setScale(6, RoundingMode.HALF_UP).toString());
18 ilm 186
                        } else {
187
                            ReferenceArticleSQLComponent.this.textMarge.setText("0");
188
                        }
156 ilm 189
                        labelMarge.setText("% (" + StringUtils.leftAlign(margeHT.setScale(2, RoundingMode.HALF_UP).toString(), 9) + ")");
18 ilm 190
                    }
191
                }
192
            }
193
            ReferenceArticleSQLComponent.this.textMarge.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextMarge);
194
        }
195
    };
196
 
197
    private DocumentListener listenerMargeTextHA = new SimpleDocumentListener() {
198
        @Override
199
        public void update(DocumentEvent e) {
200
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
201
            updateVtFromMarge();
202
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
203
        }
204
    };
205
 
206
    private void updateVtFromMarge() {
67 ilm 207
        if (this.textPAHT.getText().trim().length() > 0) {
21 ilm 208
 
83 ilm 209
            BigDecimal ha = StringUtils.getBigDecimalFromUserText(this.textPAHT.getText());
18 ilm 210
            if (ha != null && this.textMarge.getText().trim().length() > 0) {
73 ilm 211
 
156 ilm 212
                if (boxMargeWithCR.isSelected() && boxCR != null) {
213
                    SQLRow rowCR = this.boxCR.getSelectedRow();
214
                    if (rowCR != null && !rowCR.isUndefined()) {
215
                        BigDecimal cr = rowCR.getBigDecimal("POURCENT");
216
                        ha = ha.multiply(cr.movePointLeft(2).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
217
                    }
218
                }
219
 
83 ilm 220
                BigDecimal d = StringUtils.getBigDecimalFromUserText(this.textMarge.getText());
156 ilm 221
                if (d == null) {
222
                    d = BigDecimal.ZERO;
223
                }
224
                final BigDecimal vt;
21 ilm 225
                if (DefaultNXProps.getInstance().getBooleanValue(TotalPanel.MARGE_MARQUE, false)) {
90 ilm 226
                    final BigDecimal e = BigDecimal.ONE.subtract(d.divide(BigDecimal.valueOf(100), DecimalUtils.HIGH_PRECISION));
73 ilm 227
                    if (e.signum() == 0) {
156 ilm 228
                        vt = BigDecimal.ZERO;
21 ilm 229
                    } else {
156 ilm 230
                        vt = ha.divide(e, DecimalUtils.HIGH_PRECISION).setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP);
21 ilm 231
                    }
232
                } else {
90 ilm 233
                    BigDecimal result = ha.multiply(d.divide(BigDecimal.valueOf(100), DecimalUtils.HIGH_PRECISION).add(BigDecimal.ONE));
156 ilm 234
                    vt = result.setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP);
235
                    this.textPVHT.setText(vt.toString());
21 ilm 236
                }
156 ilm 237
                this.textPVHT.setText(vt.toString());
238
                BigDecimal margeHT = vt.subtract(ha);
239
                labelMarge.setText("% (" + StringUtils.leftAlign(margeHT.setScale(2, RoundingMode.HALF_UP).toString(), 9) + ")");
18 ilm 240
            }
241
        }
242
    }
243
 
244
    public ReferenceArticleSQLComponent(SQLElement elt) {
245
        super(elt);
94 ilm 246
        if (elt.getTable().getDBRoot().contains("ARTICLE_ELEMENT")) {
247
            this.tableBom = new ProductItemListTable();
248
        }
18 ilm 249
    }
250
 
251
    @Override
252
    public void select(SQLRowAccessor r) {
253
        super.select(r);
254
        if (r != null && r.getID() > getTable().getUndefinedID()) {
255
            this.checkObs.setVisible(true);
19 ilm 256
            this.tableTarifVente.setArticleValues(r);
257
            this.tableTarifVente.insertFrom("ID_ARTICLE", r.getID());
156 ilm 258
            this.tableCatComptable.insertFrom("ID_ARTICLE", r.getID());
259
            this.tableFourSec.insertFrom("ID_ARTICLE", r.getID());
94 ilm 260
            this.tableTarifQteVente.insertFrom("ID_ARTICLE", r.getID());
261
            if (this.tableBom != null) {
262
                this.tableBom.insertFrom("ID_ARTICLE_PARENT", r.getID());
263
            }
19 ilm 264
            this.tableDes.insertFrom("ID_ARTICLE", r.getID());
144 ilm 265
            this.tableCodeClient.insertFrom("ID_ARTICLE", r.getID());
67 ilm 266
            if (this.codeFournisseurTable != null) {
267
                this.codeFournisseurTable.insertFrom("ID_ARTICLE", r.getID());
268
            }
132 ilm 269
            selectModeVente(r.getForeignID("ID_MODE_VENTE_ARTICLE"));
18 ilm 270
        }
271
    }
272
 
273
    public void addViews() {
274
        this.setLayout(new GridBagLayout());
275
        final GridBagConstraints c = new DefaultGridBagConstraints();
276
 
67 ilm 277
        this.textPVHT = new JTextField(15);
278
        this.textPVTTC = new JTextField(15);
279
        this.textPAHT = new JTextField(15);
18 ilm 280
        this.textPVHT.getDocument().addDocumentListener(this.listenerMargeTextVT);
281
 
282
        // Init metrique devise field
67 ilm 283
        this.textMetrique1HA = new JTextField(15);
284
        this.textMetrique1VT = new JTextField(15);
18 ilm 285
 
286
        // init metrique value field
67 ilm 287
        this.textValMetrique1 = new JTextField(15);
288
        this.textValMetrique2 = new JTextField(15);
289
        this.textValMetrique3 = new JTextField(15);
18 ilm 290
 
291
        this.textCode = new JTextField();
292
        this.textNom = new JTextField();
293
        this.textPoids = new JTextField(6);
294
 
295
        // Code
19 ilm 296
        JLabel codelabel = new JLabel(getLabelFor("CODE"));
297
        codelabel.setHorizontalAlignment(SwingConstants.RIGHT);
298
        DefaultGridBagConstraints.lockMinimumSize(codelabel);
299
        this.add(codelabel, c);
18 ilm 300
        c.gridx++;
19 ilm 301
        c.weightx = 1;
302
        DefaultGridBagConstraints.lockMinimumSize(textCode);
18 ilm 303
        this.add(this.textCode, c);
304
 
19 ilm 305
        // Famille
18 ilm 306
        c.gridx++;
19 ilm 307
        c.gridwidth = 1;
18 ilm 308
        c.weightx = 0;
19 ilm 309
        JLabel labelFamille = new JLabel(getLabelFor("ID_FAMILLE_ARTICLE"));
310
        labelFamille.setHorizontalAlignment(SwingConstants.RIGHT);
311
        DefaultGridBagConstraints.lockMinimumSize(labelFamille);
312
        this.add(labelFamille, c);
18 ilm 313
        c.gridx++;
19 ilm 314
        c.weightx = 1;
315
        c.gridwidth = 1;
316
        final ElementComboBox comboSelFamille = new ElementComboBox(false, 25);
67 ilm 317
        this.addSQLObject(comboSelFamille, "ID_FAMILLE_ARTICLE");
19 ilm 318
        DefaultGridBagConstraints.lockMinimumSize(comboSelFamille);
319
        this.add(comboSelFamille, c);
18 ilm 320
 
19 ilm 321
        // Nom
18 ilm 322
        c.gridy++;
323
        c.gridx = 0;
324
        c.weightx = 0;
19 ilm 325
        JLabel labelNom = new JLabel(getLabelFor("NOM"));
326
        labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
327
        DefaultGridBagConstraints.lockMinimumSize(labelNom);
328
        this.add(labelNom, c);
18 ilm 329
        c.gridx++;
19 ilm 330
        c.weightx = 1;
331
        DefaultGridBagConstraints.lockMinimumSize(textNom);
332
        this.add(this.textNom, c);
18 ilm 333
 
19 ilm 334
        // Code barre
18 ilm 335
        c.gridx++;
19 ilm 336
        c.weightx = 0;
337
        JLabel labelCodeBarre = new JLabel(getLabelFor("CODE_BARRE"));
338
        labelCodeBarre.setHorizontalAlignment(SwingConstants.RIGHT);
339
        DefaultGridBagConstraints.lockMinimumSize(labelCodeBarre);
340
        this.add(labelCodeBarre, c);
341
        c.gridx++;
18 ilm 342
        c.weightx = 1;
19 ilm 343
        JTextField fieldCodeBarre = new JTextField();
344
        DefaultGridBagConstraints.lockMinimumSize(fieldCodeBarre);
345
        this.add(fieldCodeBarre, c);
346
        this.addView(fieldCodeBarre, "CODE_BARRE");
18 ilm 347
 
61 ilm 348
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
349
        // Gestion des unités de vente
350
        final boolean gestionUV = prefs.getBoolean(GestionArticleGlobalPreferencePanel.UNITE_VENTE, true);
93 ilm 351
        c.gridy++;
156 ilm 352
        final ElementComboBox boxUnite = new ElementComboBox();
61 ilm 353
        if (gestionUV) {
354
            c.gridx = 0;
65 ilm 355
            c.weightx = 0;
356
            this.add(new JLabel(getLabelFor("ID_UNITE_VENTE"), SwingConstants.RIGHT), c);
61 ilm 357
            c.gridx++;
65 ilm 358
            c.weightx = 1;
80 ilm 359
            c.fill = GridBagConstraints.NONE;
360
            DefaultGridBagConstraints.lockMinimumSize(boxUnite);
61 ilm 361
            this.add(boxUnite, c);
362
            this.addView(boxUnite, "ID_UNITE_VENTE");
80 ilm 363
            c.fill = GridBagConstraints.HORIZONTAL;
61 ilm 364
        }
93 ilm 365
        c.gridx = 2;
366
        c.weightx = 0;
367
        this.add(new JLabel(getLabelFor("SKU"), SwingConstants.RIGHT), c);
368
        c.gridx++;
369
        c.weightx = 1;
370
        JTextField fieldSKU = new JTextField();
371
        DefaultGridBagConstraints.lockMinimumSize(fieldSKU);
372
        this.add(fieldSKU, c);
373
        this.addView(fieldSKU, "SKU");
374
        c.fill = GridBagConstraints.HORIZONTAL;
375
 
156 ilm 376
        c.gridy++;
377
        if (gestionUV) {
378
            c.gridx = 0;
379
            c.weightx = 0;
380
            this.add(new JLabel(getLabelFor("QTE_UNITAIRE"), SwingConstants.RIGHT), c);
381
            c.gridx++;
382
            c.weightx = 1;
383
            c.fill = GridBagConstraints.NONE;
384
            final JTextField qte = new JTextField(10);
385
            qte.setEditable(false);
386
 
387
            this.add(qte, c);
388
            this.addView(qte, "QTE_UNITAIRE", REQ);
389
            boxUnite.addModelListener("wantedID", new PropertyChangeListener() {
390
 
391
                @Override
392
                public void propertyChange(PropertyChangeEvent evt) {
393
                    if (!boxUnite.isEmpty() && boxUnite.getSelectedId() == UniteVenteArticleSQLElement.A_LA_PIECE) {
394
                        qte.setText("1");
395
                        qte.setEditable(false);
396
                    } else {
397
                        qte.setEditable(true);
398
                    }
399
 
400
                }
401
            });
402
            c.fill = GridBagConstraints.HORIZONTAL;
403
        }
404
 
19 ilm 405
        DefaultProps props = DefaultNXProps.getInstance();
18 ilm 406
 
407
        // Article détaillé
408
        String modeVente = props.getStringProperty("ArticleModeVenteAvance");
409
        Boolean bModeVente = Boolean.valueOf(modeVente);
410
        boolean modeVenteAvance = (bModeVente == null || bModeVente.booleanValue());
411
 
412
        if (modeVenteAvance) {
413
            addModeVenteAvance(c);
414
        }
415
 
416
        getMontantPanel(c, props);
417
 
27 ilm 418
        // Champ Module
419
        c.gridx = 0;
420
        c.gridy++;
421
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 422
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
27 ilm 423
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
41 ilm 424
        this.add(addP, c);
27 ilm 425
 
19 ilm 426
        JTabbedPane pane = new JTabbedPane();
18 ilm 427
        c.gridy++;
428
        c.weightx = 1;
67 ilm 429
        c.weighty = 1;
19 ilm 430
 
94 ilm 431
        pane.add("Tarifs de vente spéciaux", createTarifPanel());
432
        pane.add("Tarifs de vente par quantité", createTarifQtePanel());
433
        if (this.tableBom != null) {
434
            pane.add("Nomenclature", createBOMpanel());
435
        }
19 ilm 436
        pane.add("Exportation", createExportationPanel());
437
        pane.add("Achat", createAchatPanel());
438
        pane.add("Stock", createStockPanel());
439
        pane.add("Descriptif", createDescriptifPanel());
440
        pane.add("Désignations multilingues", createDesignationPanel());
144 ilm 441
        pane.add("Désignations clients", createCodeClientPanel());
67 ilm 442
        pane.add("Comptabilité", createComptaPanel());
443
        pane.add(getLabelFor("INFOS"), createInfosPanel());
444
 
18 ilm 445
        c.fill = GridBagConstraints.BOTH;
19 ilm 446
        this.add(pane, c);
447
 
18 ilm 448
        this.addSQLObject(this.textMetrique1HA, "PRIX_METRIQUE_HA_1");
449
        this.addSQLObject(this.textMetrique1VT, "PRIX_METRIQUE_VT_1");
450
        this.addSQLObject(this.textValMetrique1, "VALEUR_METRIQUE_1");
451
        this.addSQLObject(this.textValMetrique2, "VALEUR_METRIQUE_2");
452
        this.addSQLObject(this.textValMetrique3, "VALEUR_METRIQUE_3");
453
        this.addSQLObject(this.comboSelModeVente, "ID_MODE_VENTE_ARTICLE");
454
        this.addSQLObject(this.boxService, "SERVICE");
455
 
456
        this.addRequiredSQLObject(this.textNom, "NOM");
457
        this.addRequiredSQLObject(this.textCode, "CODE");
458
 
459
        this.addSQLObject(this.textPoids, "POIDS");
460
 
461
        this.comboSelTaxe.setButtonsVisible(false);
462
        this.propertyChangeListener = new PropertyChangeListener() {
463
 
464
            public void propertyChange(PropertyChangeEvent evt) {
465
                System.err.println(ReferenceArticleSQLComponent.this.comboSelModeVente.getSelectedId());
466
                selectModeVente(ReferenceArticleSQLComponent.this.comboSelModeVente.getSelectedId());
19 ilm 467
 
18 ilm 468
            }
469
        };
470
        setListenerModeVenteActive(true);
471
        this.comboSelModeVente.setValue(ReferenceArticleSQLElement.A_LA_PIECE);
472
    }
473
 
67 ilm 474
    private Component createInfosPanel() {
475
        JPanel panel = new JPanel(new GridBagLayout());
476
        panel.setOpaque(false);
477
        GridBagConstraints c = new DefaultGridBagConstraints();
144 ilm 478
        c.weightx = 0;
479
        c.gridy++;
480
        c.gridx = 0;
481
        c.weightx = 0;
482
        JLabel labelifco = new JLabel(getLabelFor("IFCO"));
67 ilm 483
 
144 ilm 484
        panel.add(labelifco, c);
485
        c.weightx = 1;
486
        c.gridx++;
487
        JTextField textIfco = new JTextField(40);
488
        panel.add(textIfco, c);
489
        addView(textIfco, "IFCO");
490
 
491
        c.gridx++;
142 ilm 492
        c.weightx = 0;
144 ilm 493
        JLabel labelTare = new JLabel(getLabelFor("TARE"));
494
        panel.add(labelTare, c);
495
        c.weightx = 1;
496
        c.gridx++;
497
        JTextField textTare = new JTextField(40);
498
        panel.add(textTare, c);
499
        addView(textTare, "TARE");
500
 
149 ilm 501
        c.weightx = 0;
144 ilm 502
        c.gridy++;
503
        c.gridx = 0;
149 ilm 504
        JLabel labelMasque = new JLabel(getLabelFor("MASQUE_CAISSE"));
505
        c.fill = GridBagConstraints.BOTH;
506
        panel.add(labelMasque, c);
507
        c.weightx = 1;
508
        c.gridx++;
509
        JCheckBox boxMasqueCaisse = new JCheckBox();
510
        panel.add(boxMasqueCaisse, c);
511
        this.addSQLObject(boxMasqueCaisse, "MASQUE_CAISSE");
512
 
513
        c.gridy++;
514
        c.gridx = 0;
144 ilm 515
        c.weightx = 0;
142 ilm 516
        JLabel labelEco = new JLabel(getLabelFor("ID_ECO_CONTRIBUTION"));
517
        c.fill = GridBagConstraints.BOTH;
518
        panel.add(labelEco, c);
67 ilm 519
        c.weightx = 1;
142 ilm 520
        c.gridx++;
521
        ElementComboBox box = new ElementComboBox();
522
        panel.add(box, c);
523
 
524
        c.weightx = 0;
525
        c.gridy++;
526
        c.gridx = 0;
527
        JLabel labelTaxeCompl = new JLabel(getLabelFor("ID_TAXE_COMPLEMENTAIRE"));
528
        c.fill = GridBagConstraints.BOTH;
529
        panel.add(labelTaxeCompl, c);
530
        c.weightx = 1;
531
        c.gridx++;
532
        ElementComboBox boxTaxeCompl = new ElementComboBox();
533
        panel.add(boxTaxeCompl, c);
534
 
156 ilm 535
        c.weightx = 0;
142 ilm 536
        c.gridy++;
537
        c.gridx = 0;
156 ilm 538
        JLabel labelMatiere = new JLabel(getLabelFor("MATIERE"));
539
        c.fill = GridBagConstraints.BOTH;
540
        panel.add(labelMatiere, c);
541
        c.weightx = 1;
542
        c.gridx++;
543
        SQLTextCombo comboMatiere = new SQLTextCombo();
544
        panel.add(comboMatiere, c);
545
 
546
        c.gridy++;
547
        c.gridx = 0;
142 ilm 548
        c.gridwidth = GridBagConstraints.REMAINDER;
549
        TitledSeparator sep = new TitledSeparator(getLabelFor("INFOS"));
550
        panel.add(sep, c);
551
        c.gridy++;
67 ilm 552
        c.weighty = 1;
553
        ITextArea infos = new ITextArea();
554
        c.fill = GridBagConstraints.BOTH;
555
        panel.add(infos, c);
556
 
557
        this.addSQLObject(infos, "INFOS");
156 ilm 558
        this.addSQLObject(comboMatiere, "MATIERE");
142 ilm 559
        this.addSQLObject(box, "ID_ECO_CONTRIBUTION");
560
        this.addSQLObject(boxTaxeCompl, "ID_TAXE_COMPLEMENTAIRE");
67 ilm 561
        return panel;
562
    }
563
 
19 ilm 564
    private Component createDescriptifPanel() {
565
        JPanel panel = new JPanel(new GridBagLayout());
566
        panel.setOpaque(false);
567
        GridBagConstraints c = new DefaultGridBagConstraints();
568
 
569
        // Obsolete
570
        c.fill = GridBagConstraints.NONE;
41 ilm 571
        c.gridwidth = GridBagConstraints.REMAINDER;
19 ilm 572
        c.weightx = 1;
25 ilm 573
        this.checkObs.setOpaque(false);
19 ilm 574
        panel.add(this.checkObs, c);
575
 
576
        this.checkObs.setVisible(false);
577
        this.addView(this.checkObs, "OBSOLETE");
578
 
41 ilm 579
        if (getTable().getFieldsName().contains("COLORIS")) {
580
            JTextField fieldColoris = new JTextField();
581
            c.gridy++;
582
            c.fill = GridBagConstraints.HORIZONTAL;
583
            c.weightx = 0;
584
            c.gridwidth = 1;
585
            panel.add(new JLabel(getLabelFor("COLORIS")), c);
586
 
587
            c.weightx = 1;
588
            c.gridx++;
589
            panel.add(fieldColoris, c);
590
            this.addView(fieldColoris, "COLORIS");
591
        }
19 ilm 592
        ITextArea area = new ITextArea();
593
        JLabel sep = new JLabel("Descriptif complet");
594
        c.gridy++;
41 ilm 595
        c.gridx = 0;
596
        c.gridwidth = GridBagConstraints.REMAINDER;
19 ilm 597
        c.fill = GridBagConstraints.HORIZONTAL;
598
        panel.add(sep, c);
599
 
600
        c.gridy++;
601
        c.weighty = 1;
602
        c.fill = GridBagConstraints.BOTH;
603
        panel.add(area, c);
604
        this.addView(area, "DESCRIPTIF");
605
        return panel;
606
    }
607
 
608
    private Component createDesignationPanel() {
609
        JPanel panel = new JPanel(new GridBagLayout());
610
        panel.setOpaque(false);
611
        GridBagConstraints c = new DefaultGridBagConstraints();
612
 
613
        // Ajout des
614
        c.gridwidth = 1;
615
        c.weightx = 0;
616
        c.gridy++;
617
        c.gridx = 0;
618
        panel.add(new JLabel("Ajouter une désignation "), c);
619
 
620
        final ElementComboBox boxDes = new ElementComboBox();
621
        boxDes.init(Configuration.getInstance().getDirectory().getElement("LANGUE"));
622
 
623
        c.gridx++;
624
        panel.add(boxDes, c);
625
 
626
        c.fill = GridBagConstraints.NONE;
627
        c.gridx++;
628
        JButton buttonAjouterDes = new JButton("Ajouter");
629
        buttonAjouterDes.setOpaque(false);
630
        panel.add(buttonAjouterDes, c);
631
        c.gridx++;
632
        JButton buttonSupprimerDes = new JButton("Supprimer");
633
        buttonSupprimerDes.setOpaque(false);
634
        panel.add(buttonSupprimerDes, c);
635
 
636
        c.gridy++;
637
        c.gridx = 0;
638
        c.gridwidth = GridBagConstraints.REMAINDER;
639
        c.fill = GridBagConstraints.BOTH;
640
        c.weighty = 1;
641
        c.weightx = 1;
642
        this.tableDes.setOpaque(false);
643
        panel.add(this.tableDes, c);
644
 
645
        // Listerners
646
        buttonAjouterDes.addActionListener(new ActionListener() {
647
 
648
            @Override
649
            public void actionPerformed(ActionEvent e) {
650
 
651
                int id = boxDes.getSelectedId();
652
                if (id <= 1) {
653
                    return;
654
                }
655
                int nbRows = tableDes.getModel().getRowCount();
656
 
657
                for (int i = 0; i < nbRows; i++) {
658
                    SQLRowValues rowVals = tableDes.getModel().getRowValuesAt(i);
659
                    int idLangue = Integer.parseInt(rowVals.getObject("ID_LANGUE").toString());
660
                    if (idLangue == id) {
661
                        JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLa langue est déjà présente dans la liste!");
662
                        return;
663
                    }
664
                }
665
 
666
                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_DESIGNATION"));
667
                if (getSelectedID() > 1) {
668
                    rowVals.put("ID_ARTICLE", getSelectedID());
669
                }
670
                rowVals.put("ID_LANGUE", id);
671
                rowVals.put("NOM", "");
672
                tableDes.getModel().addRow(rowVals);
673
            }
674
        });
675
        buttonSupprimerDes.addActionListener(new ActionListener() {
676
 
677
            @Override
678
            public void actionPerformed(ActionEvent e) {
679
                tableDes.removeSelectedRow();
680
            }
681
        });
682
 
683
        return panel;
684
    }
685
 
144 ilm 686
    private Component createCodeClientPanel() {
687
        JPanel panel = new JPanel(new GridBagLayout());
688
        panel.setOpaque(false);
689
        GridBagConstraints c = new DefaultGridBagConstraints();
690
 
691
        c.gridx = 0;
692
        c.gridwidth = GridBagConstraints.REMAINDER;
693
        c.fill = GridBagConstraints.BOTH;
694
        c.weighty = 1;
695
        c.weightx = 1;
696
        this.tableCodeClient.setOpaque(false);
697
        panel.add(this.tableCodeClient, c);
698
 
699
        return panel;
700
    }
701
 
19 ilm 702
    private Component createStockPanel() {
703
        JPanel panel = new JPanel(new GridBagLayout());
704
        panel.setOpaque(false);
705
        GridBagConstraints c = new DefaultGridBagConstraints();
93 ilm 706
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
142 ilm 707
        boolean gestionStockMin = prefs.getBoolean(GestionArticleGlobalPreferencePanel.WARNING_STOCK_MIN, true);
19 ilm 708
        c.gridx = 0;
709
        c.gridy++;
710
 
711
        final JCheckBox boxStock = new JCheckBox(getLabelFor("GESTION_STOCK"));
712
        boxStock.setOpaque(false);
713
        panel.add(boxStock, c);
714
        this.addView(boxStock, "GESTION_STOCK");
715
 
716
        final JTextField fieldQteMin = new JTextField();
717
        final JTextField fieldQteAchat = new JTextField();
718
        boxStock.addActionListener(new ActionListener() {
719
 
720
            @Override
721
            public void actionPerformed(ActionEvent e) {
722
                fieldQteMin.setEnabled(boxStock.isSelected());
723
                fieldQteAchat.setEnabled(boxStock.isSelected());
724
            }
725
        });
726
 
156 ilm 727
        boolean visibleDepot = (prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false));
728
        c.gridx = 0;
729
        c.gridy++;
730
        c.weightx = 0;
731
        final JLabel labelDepot = new JLabel(getLabelFor("ID_DEPOT_STOCK"));
732
        panel.add(labelDepot, c);
733
        labelDepot.setVisible(visibleDepot);
734
        c.gridx++;
735
        c.weightx = 1;
736
        ElementComboBox boxDepot = new ElementComboBox();
737
        panel.add(boxDepot, c);
738
        boxDepot.setVisible(visibleDepot);
739
        this.addView(boxDepot, "ID_DEPOT_STOCK", REQ);
740
 
19 ilm 741
        c.gridwidth = 1;
742
        if (gestionStockMin) {
156 ilm 743
            // c.gridx = 0;
744
            // c.gridy++;
745
            // c.weightx = 0;
746
            // panel.add(new JLabel(getLabelFor("QTE_MIN")), c);
747
            // c.gridx++;
748
            // c.weightx = 1;
749
            // panel.add(fieldQteMin, c);
750
            // this.addView(fieldQteMin, "QTE_MIN");
19 ilm 751
 
752
            c.gridx = 0;
753
            c.gridy++;
754
            c.weightx = 0;
755
            panel.add(new JLabel(getLabelFor("QTE_ACHAT")), c);
756
            c.gridx++;
757
            c.weightx = 1;
758
            panel.add(fieldQteAchat, c);
759
            this.addView(fieldQteAchat, "QTE_ACHAT");
760
        }
761
 
762
        c.gridy++;
763
        c.weighty = 1;
764
        c.weightx = 1;
765
        c.fill = GridBagConstraints.BOTH;
766
        final JPanel spacer = new JPanel();
767
        spacer.setOpaque(false);
768
        panel.add(spacer, c);
769
        return panel;
770
    }
771
 
67 ilm 772
    private Component createComptaPanel() {
773
        JPanel panel = new JPanel(new GridBagLayout());
774
        panel.setOpaque(false);
775
        GridBagConstraints c = new DefaultGridBagConstraints();
776
        c.gridwidth = 1;
777
        c.weighty = 0;
778
        c.weightx = 0;
779
        ISQLCompteSelector sel = new ISQLCompteSelector();
780
        c.fill = GridBagConstraints.BOTH;
781
        panel.add(new JLabel(getLabelFor("ID_COMPTE_PCE")), c);
782
        c.gridx++;
783
        c.weightx = 1;
784
        panel.add(sel, c);
785
        this.addView(sel, "ID_COMPTE_PCE");
786
 
787
        c.gridwidth = 1;
788
        c.gridy++;
789
        c.weighty = 0;
790
        c.gridx = 0;
791
        c.weightx = 0;
792
        ISQLCompteSelector selAchat = new ISQLCompteSelector();
793
        c.fill = GridBagConstraints.BOTH;
794
        panel.add(new JLabel(getLabelFor("ID_COMPTE_PCE_ACHAT")), c);
795
        c.gridx++;
796
        c.weightx = 1;
797
        panel.add(selAchat, c);
798
        this.addView(selAchat, "ID_COMPTE_PCE_ACHAT");
799
 
800
        c.gridy++;
156 ilm 801
        c.gridx = 0;
67 ilm 802
        c.weighty = 1;
803
        c.weightx = 1;
804
        c.fill = GridBagConstraints.BOTH;
156 ilm 805
        c.gridwidth = GridBagConstraints.REMAINDER;
806
 
807
        panel.add(createCategorieComptablePanel(), c);
808
        // final JPanel spacer = new JPanel();
809
        // spacer.setOpaque(false);
810
        // panel.add(spacer, c);
67 ilm 811
        return panel;
812
    }
813
 
814
    private SQLRowValues rowValuesDefaultCodeFournisseur;
815
    private CodeFournisseurItemTable codeFournisseurTable;
816
 
19 ilm 817
    private Component createAchatPanel() {
818
        JPanel panel = new JPanel(new GridBagLayout());
819
        panel.setOpaque(false);
820
        GridBagConstraints c = new DefaultGridBagConstraints();
821
        // Fournisseur
822
        c.gridy++;
823
        c.gridx = 0;
824
        c.gridwidth = 1;
825
        c.weightx = 0;
826
        JLabel labelFournisseur = new JLabel(getLabelFor("ID_FOURNISSEUR"));
827
        labelFournisseur.setHorizontalAlignment(SwingConstants.RIGHT);
828
        panel.add(labelFournisseur, c);
829
        c.gridx++;
830
        c.weightx = 1;
831
        c.gridwidth = 2;
832
        final ElementComboBox comboSelFournisseur = new ElementComboBox(false, 25);
833
        panel.add(comboSelFournisseur, c);
834
        this.addView(comboSelFournisseur, "ID_FOURNISSEUR");
835
 
67 ilm 836
        SQLPreferences prefs = new SQLPreferences(ComptaPropsConfiguration.getInstanceCompta().getRootSociete());
837
        final boolean supplierCode = prefs.getBoolean(GestionArticleGlobalPreferencePanel.SUPPLIER_PRODUCT_CODE, false);
838
 
839
        if (getTable().getSchema().contains("CODE_FOURNISSEUR") && supplierCode) {
840
            this.rowValuesDefaultCodeFournisseur = new SQLRowValues(getTable().getTable("CODE_FOURNISSEUR"));
841
            this.codeFournisseurTable = new CodeFournisseurItemTable(this.rowValuesDefaultCodeFournisseur);
842
            c.gridy++;
843
            c.gridx = 0;
844
            c.gridwidth = 3;
845
            c.weighty = 1;
846
            c.weightx = 1;
847
            c.fill = GridBagConstraints.BOTH;
848
            panel.add(this.codeFournisseurTable, c);
849
            comboSelFournisseur.addValueListener(new PropertyChangeListener() {
850
 
851
                @Override
852
                public void propertyChange(PropertyChangeEvent evt) {
853
                    rowValuesDefaultCodeFournisseur.put("ID_FOURNISSEUR", comboSelFournisseur.getSelectedId());
854
                }
855
            });
856
        } else {
156 ilm 857
 
858
            // Tarif fournisseur
67 ilm 859
            c.gridy++;
156 ilm 860
            c.gridx = 0;
861
            c.gridwidth = GridBagConstraints.REMAINDER;
862
            TitledSeparator sep = new TitledSeparator("Tarifs fournisseurs");
863
            panel.add(sep, c);
864
 
865
            // Ajout fournisseur
866
            c.gridwidth = 1;
867
            c.weightx = 0;
868
            c.gridy++;
869
            c.gridx = 0;
870
            panel.add(new JLabel("Ajouter le fournisseur "), c);
871
 
872
            final ElementComboBox boxF = new ElementComboBox();
873
            boxF.init(Configuration.getInstance().getDirectory().getElement("FOURNISSEUR"));
874
 
875
            c.gridx++;
876
            panel.add(boxF, c);
877
 
878
            c.fill = GridBagConstraints.NONE;
879
            c.gridx++;
880
            JButton buttonAjouter = new JButton("Ajouter");
881
            buttonAjouter.setOpaque(false);
882
            panel.add(buttonAjouter, c);
883
            c.gridx++;
884
            JButton buttonSupprimer = new JButton("Supprimer");
885
            buttonSupprimer.setOpaque(false);
886
            panel.add(buttonSupprimer, c);
887
 
888
            c.gridwidth = GridBagConstraints.REMAINDER;
889
            c.fill = GridBagConstraints.BOTH;
890
            c.weightx = 1;
67 ilm 891
            c.weighty = 1;
156 ilm 892
            c.gridy++;
893
            c.gridx = 0;
67 ilm 894
            c.fill = GridBagConstraints.BOTH;
156 ilm 895
            this.tableFourSec.setOpaque(false);
896
            panel.add(this.tableFourSec, c);
897
 
898
            // Listeners
899
            buttonAjouter.addActionListener(new ActionListener() {
900
 
901
                @Override
902
                public void actionPerformed(ActionEvent e) {
903
 
904
                    SQLRow rowCat = boxF.getSelectedRow();
905
                    if (rowCat == null || rowCat.isUndefined()) {
906
                        return;
907
                    }
908
                    int nbRows = tableFourSec.getModel().getRowCount();
909
 
910
                    // for (int i = 0; i < nbRows; i++) {
911
                    // SQLRowValues rowVals = tableFourSec.getModel().getRowValuesAt(i);
912
                    // int idTarif =
913
                    // Integer.parseInt(rowVals.getObject("ID_FOURNISSEUR").toString());
914
                    // if (idTarif == rowCat.getID()) {
915
                    // JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLe fournisseur
916
                    // est déjà présent dans la liste!");
917
                    // return;
918
                    // }
919
                    // }
920
 
921
                    SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_TARIF_FOURNISSEUR"));
922
                    if (getSelectedID() > 1) {
923
                        rowVals.put("ID_ARTICLE", getSelectedID());
924
                    }
925
                    rowVals.put("ID_FOURNISSEUR", rowCat.getID());
926
                    tableFourSec.getModel().addRow(rowVals);
927
                }
928
            });
929
            buttonSupprimer.addActionListener(new ActionListener() {
930
 
931
                @Override
932
                public void actionPerformed(ActionEvent e) {
933
                    tableFourSec.removeSelectedRow();
934
                }
935
            });
67 ilm 936
        }
19 ilm 937
        return panel;
938
    }
939
 
940
    private JPanel createExportationPanel() {
941
        JPanel panel = new JPanel(new GridBagLayout());
942
        panel.setOpaque(false);
943
        GridBagConstraints c = new DefaultGridBagConstraints();
944
        // Code douanier
945
        c.gridx = 0;
946
        c.weightx = 0;
947
        c.gridy++;
948
        c.gridwidth = 1;
949
        JLabel labelCodeD = new JLabel(getLabelFor("CODE_DOUANIER"));
950
        labelCodeD.setHorizontalAlignment(SwingConstants.RIGHT);
951
        panel.add(labelCodeD, c);
952
 
953
        c.gridx++;
954
        JTextField fieldCodeDouanier = new JTextField();
955
        c.weightx = 1;
956
        panel.add(fieldCodeDouanier, c);
957
        this.addView(fieldCodeDouanier, "CODE_DOUANIER");
958
 
959
        // Pays d'origine
960
        c.gridx++;
961
        c.weightx = 0;
962
        JLabel labelPays = new JLabel(getLabelFor("ID_PAYS"));
963
        labelPays.setHorizontalAlignment(SwingConstants.RIGHT);
964
        panel.add(labelPays, c);
965
        c.gridx++;
966
        c.weightx = 1;
967
        final ElementComboBox comboSelPays = new ElementComboBox(false);
968
        panel.add(comboSelPays, c);
969
        this.addView(comboSelPays, "ID_PAYS");
970
        c.gridy++;
971
        c.weighty = 1;
972
        c.weightx = 1;
973
        c.fill = GridBagConstraints.BOTH;
974
        final JPanel spacer = new JPanel();
975
        spacer.setOpaque(false);
976
        panel.add(spacer, c);
977
        return panel;
978
    }
979
 
980
    private JPanel createTarifPanel() {
981
        JPanel panel = new JPanel(new GridBagLayout());
982
        panel.setOpaque(false);
983
        GridBagConstraints c = new DefaultGridBagConstraints();
984
 
985
        // Ajout tarif
986
        c.gridwidth = 1;
987
        c.weightx = 0;
988
        c.gridy++;
989
        c.gridx = 0;
990
        panel.add(new JLabel("Ajouter le tarif "), c);
991
 
992
        final ElementComboBox boxTarif = new ElementComboBox();
993
        boxTarif.init(Configuration.getInstance().getDirectory().getElement("TARIF"));
994
 
995
        c.gridx++;
996
        panel.add(boxTarif, c);
997
 
998
        c.fill = GridBagConstraints.NONE;
999
        c.gridx++;
1000
        JButton buttonAjouter = new JButton("Ajouter");
1001
        buttonAjouter.setOpaque(false);
1002
        panel.add(buttonAjouter, c);
1003
        c.gridx++;
1004
        JButton buttonSupprimer = new JButton("Supprimer");
1005
        buttonSupprimer.setOpaque(false);
1006
        panel.add(buttonSupprimer, c);
1007
 
1008
        c.gridwidth = GridBagConstraints.REMAINDER;
1009
        c.fill = GridBagConstraints.BOTH;
1010
        c.weightx = 1;
1011
        c.weighty = 1;
1012
        c.gridy++;
1013
        c.gridx = 0;
1014
        c.fill = GridBagConstraints.BOTH;
1015
        this.tableTarifVente.setOpaque(false);
1016
        panel.add(this.tableTarifVente, c);
1017
 
1018
        // Listeners
1019
        buttonAjouter.addActionListener(new ActionListener() {
1020
 
1021
            @Override
1022
            public void actionPerformed(ActionEvent e) {
1023
 
1024
                SQLRow rowTarif = boxTarif.getSelectedRow();
1025
                if (rowTarif == null || rowTarif.isUndefined()) {
1026
                    return;
1027
                }
94 ilm 1028
                // int nbRows = tableTarifVente.getModel().getRowCount();
19 ilm 1029
 
94 ilm 1030
                // FIXME Check with qty
1031
                // for (int i = 0; i < nbRows; i++) {
1032
                // SQLRowValues rowVals = tableTarifVente.getModel().getRowValuesAt(i);
1033
                // int idTarif = Integer.parseInt(rowVals.getObject("ID_TARIF").toString());
1034
                // if (idTarif == rowTarif.getID()) {
1035
                // JOptionPane.showMessageDialog(null,
1036
                // "Impossible d'ajouter.\nLe tarif est déjà présent dans la liste!");
1037
                // return;
1038
                // }
1039
                // }
19 ilm 1040
 
1041
                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_TARIF"));
1042
                if (getSelectedID() > 1) {
1043
                    rowVals.put("ID_ARTICLE", getSelectedID());
1044
                }
1045
                rowVals.put("ID_TARIF", rowTarif.getID());
1046
                rowVals.put("ID_DEVISE", rowTarif.getInt("ID_DEVISE"));
1047
                rowVals.put("ID_TAXE", rowTarif.getInt("ID_TAXE"));
67 ilm 1048
                rowVals.put("PRIX_METRIQUE_VT_1", BigDecimal.ZERO);
1049
                rowVals.put("PV_HT", BigDecimal.ZERO);
1050
                rowVals.put("PV_TTC", BigDecimal.ZERO);
19 ilm 1051
                tableTarifVente.getModel().addRow(rowVals);
1052
            }
1053
        });
1054
        buttonSupprimer.addActionListener(new ActionListener() {
1055
 
1056
            @Override
1057
            public void actionPerformed(ActionEvent e) {
1058
                tableTarifVente.removeSelectedRow();
1059
            }
1060
        });
1061
        return panel;
1062
    }
1063
 
156 ilm 1064
    private JPanel createCategorieComptablePanel() {
1065
        JPanel panel = new JPanel(new GridBagLayout());
1066
        panel.setOpaque(false);
1067
        GridBagConstraints c = new DefaultGridBagConstraints();
1068
 
1069
        // Ajout catégorie
1070
        c.gridwidth = 1;
1071
        c.weightx = 0;
1072
        c.gridy++;
1073
        c.gridx = 0;
1074
        panel.add(new JLabel("Ajouter la catégorie "), c);
1075
 
1076
        final ElementComboBox boxCat = new ElementComboBox();
1077
        boxCat.init(Configuration.getInstance().getDirectory().getElement("CATEGORIE_COMPTABLE"));
1078
 
1079
        c.gridx++;
1080
        panel.add(boxCat, c);
1081
 
1082
        c.fill = GridBagConstraints.NONE;
1083
        c.gridx++;
1084
        JButton buttonAjouter = new JButton("Ajouter");
1085
        buttonAjouter.setOpaque(false);
1086
        panel.add(buttonAjouter, c);
1087
        c.gridx++;
1088
        JButton buttonSupprimer = new JButton("Supprimer");
1089
        buttonSupprimer.setOpaque(false);
1090
        panel.add(buttonSupprimer, c);
1091
 
1092
        c.gridwidth = GridBagConstraints.REMAINDER;
1093
        c.fill = GridBagConstraints.BOTH;
1094
        c.weightx = 1;
1095
        c.weighty = 1;
1096
        c.gridy++;
1097
        c.gridx = 0;
1098
        c.fill = GridBagConstraints.BOTH;
1099
        this.tableCatComptable.setOpaque(false);
1100
        panel.add(this.tableCatComptable, c);
1101
 
1102
        // Listeners
1103
        buttonAjouter.addActionListener(new ActionListener() {
1104
 
1105
            @Override
1106
            public void actionPerformed(ActionEvent e) {
1107
 
1108
                SQLRow rowCat = boxCat.getSelectedRow();
1109
                if (rowCat == null || rowCat.isUndefined()) {
1110
                    return;
1111
                }
1112
                int nbRows = tableCatComptable.getModel().getRowCount();
1113
 
1114
                for (int i = 0; i < nbRows; i++) {
1115
                    SQLRowValues rowVals = tableCatComptable.getModel().getRowValuesAt(i);
1116
                    int idTarif = Integer.parseInt(rowVals.getObject("ID_CATEGORIE_COMPTABLE").toString());
1117
                    if (idTarif == rowCat.getID()) {
1118
                        JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLa catégorie est déjà présente dans la liste!");
1119
                        return;
1120
                    }
1121
                }
1122
 
1123
                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_CATEGORIE_COMPTABLE"));
1124
                if (getSelectedID() > 1) {
1125
                    rowVals.put("ID_ARTICLE", getSelectedID());
1126
                }
1127
                rowVals.put("ID_CATEGORIE_COMPTABLE", rowCat.getID());
1128
                tableCatComptable.getModel().addRow(rowVals);
1129
            }
1130
        });
1131
        buttonSupprimer.addActionListener(new ActionListener() {
1132
 
1133
            @Override
1134
            public void actionPerformed(ActionEvent e) {
1135
                tableCatComptable.removeSelectedRow();
1136
            }
1137
        });
1138
        return panel;
1139
    }
1140
 
94 ilm 1141
    private JPanel createBOMpanel() {
1142
        JPanel panel = new JPanel(new GridBagLayout());
1143
        panel.setOpaque(false);
1144
        GridBagConstraints c = new DefaultGridBagConstraints();
1145
 
1146
        c.gridwidth = GridBagConstraints.REMAINDER;
1147
        c.fill = GridBagConstraints.BOTH;
1148
        c.weightx = 1;
1149
        c.weighty = 1;
1150
        c.gridx = 0;
1151
        c.fill = GridBagConstraints.BOTH;
1152
        this.tableBom.setOpaque(false);
1153
        panel.add(new RowValuesTableEditionPanel(this.tableBom), c);
1154
        return panel;
1155
    }
1156
 
1157
    private JPanel createTarifQtePanel() {
1158
        JPanel panel = new JPanel(new GridBagLayout());
1159
        panel.setOpaque(false);
1160
        GridBagConstraints c = new DefaultGridBagConstraints();
1161
 
1162
        c.gridwidth = GridBagConstraints.REMAINDER;
1163
        c.fill = GridBagConstraints.BOTH;
1164
        c.weightx = 1;
1165
        c.weighty = 1;
1166
        c.gridx = 0;
1167
        c.fill = GridBagConstraints.BOTH;
1168
        this.tableTarifQteVente.setOpaque(false);
1169
        panel.add(new RowValuesTableEditionPanel(this.tableTarifQteVente), c);
1170
        return panel;
1171
    }
1172
 
18 ilm 1173
    protected void getMontantPanel(final GridBagConstraints c, DefaultProps props) {
80 ilm 1174
        c.gridx = 0;
1175
        c.gridy++;
1176
        c.gridwidth = 1;
19 ilm 1177
        // PA devise
67 ilm 1178
        JPanel pDevise = new JPanel(new GridBagLayout());
1179
        GridBagConstraints cDevise = new DefaultGridBagConstraints();
80 ilm 1180
        cDevise.insets = new Insets(0, 0, 0, 4);
1181
        c.weightx = 0;
1182
        c.fill = GridBagConstraints.HORIZONTAL;
1183
        this.add(new JLabel("Devise du fournisseur"), c);
67 ilm 1184
        final ElementComboBox boxDevise = new ElementComboBox(true, 15);
1185
        cDevise.gridx++;
1186
        cDevise.weightx = 1;
1187
        pDevise.add(boxDevise, cDevise);
19 ilm 1188
        this.addView(boxDevise, "ID_DEVISE_HA");
67 ilm 1189
        DefaultGridBagConstraints.lockMinimumSize(boxDevise);
19 ilm 1190
 
67 ilm 1191
        cDevise.weightx = 0;
1192
        cDevise.gridx++;
1193
        pDevise.add(new JLabel("Prix d'achat devise"), cDevise);
1194
        final JTextField fieldHAD = new JTextField(15);
1195
        cDevise.weightx = 1;
1196
        cDevise.gridx++;
1197
        pDevise.add(fieldHAD, cDevise);
19 ilm 1198
        this.addView(fieldHAD, "PA_DEVISE");
67 ilm 1199
        DefaultGridBagConstraints.lockMinimumSize(fieldHAD);
1200
 
80 ilm 1201
        c.gridx++;
19 ilm 1202
        c.gridwidth = GridBagConstraints.REMAINDER;
1203
        c.anchor = GridBagConstraints.WEST;
1204
        c.weightx = 1;
1205
        c.fill = GridBagConstraints.NONE;
1206
        this.add(pDevise, c);
1207
        fieldHAD.getDocument().addDocumentListener(new SimpleDocumentListener() {
1208
 
1209
            @Override
1210
            public void update(DocumentEvent e) {
1211
 
83 ilm 1212
                if (!isFilling() && boxDevise != null && boxDevise.getSelectedRow() != null && !boxDevise.getSelectedRow().isUndefined()) {
1213
                    BigDecimal ha = StringUtils.getBigDecimalFromUserText(fieldHAD.getText());
1214
                    if (ha == null) {
1215
                        ha = BigDecimal.ZERO;
19 ilm 1216
                    }
93 ilm 1217
                    CurrencyConverter c = new CurrencyConverter();
132 ilm 1218
                    String devCode = boxDevise.getSelectedRow().getString("CODE");
93 ilm 1219
                    textPAHT.setText(c.convert(ha, devCode, c.getCompanyCurrencyCode(), new Date(), true).setScale(getTable().getField("PA_DEVISE").getType().getDecimalDigits(), RoundingMode.HALF_UP)
1220
                            .toString());
67 ilm 1221
 
19 ilm 1222
                }
1223
            }
1224
        });
1225
 
18 ilm 1226
        // PA
67 ilm 1227
        JPanel p = new JPanel(new GridBagLayout());
1228
        GridBagConstraints cAchat = new DefaultGridBagConstraints();
80 ilm 1229
        cAchat.insets = new Insets(0, 0, 0, 4);
1230
        c.gridx = 0;
1231
        c.gridy++;
1232
        c.weightx = 0;
1233
        c.gridwidth = 1;
1234
        c.fill = GridBagConstraints.HORIZONTAL;
1235
        this.add(new JLabel(getLabelFor("PA_HT"), SwingConstants.RIGHT), c);
67 ilm 1236
        cAchat.gridx++;
1237
        cAchat.weightx = 1;
1238
        p.add(this.textPAHT, cAchat);
18 ilm 1239
        this.textPAHT.getDocument().addDocumentListener(this.listenerMargeTextHA);
1240
 
1241
        // Marge
67 ilm 1242
        cAchat.gridx++;
1243
        cAchat.weightx = 0;
1244
        p.add(new JLabel("Marge"), cAchat);
1245
        cAchat.weightx = 1;
1246
        cAchat.gridx++;
1247
        p.add(this.textMarge, cAchat);
18 ilm 1248
        this.textMarge.getDocument().addDocumentListener(this.listenerMargeTextMarge);
67 ilm 1249
        cAchat.gridx++;
1250
        cAchat.weightx = 0;
156 ilm 1251
        p.add(this.labelMarge, cAchat);
18 ilm 1252
 
1253
        // Poids
1254
        JLabel labelPds = new JLabel(getLabelFor("POIDS"));
67 ilm 1255
        cAchat.gridx++;
1256
        cAchat.weightx = 0;
1257
        p.add(labelPds, cAchat);
18 ilm 1258
        labelPds.setHorizontalAlignment(SwingConstants.RIGHT);
67 ilm 1259
        cAchat.weightx = 1;
1260
        cAchat.gridx++;
1261
        p.add(this.textPoids, cAchat);
1262
        DefaultGridBagConstraints.lockMinimumSize(this.textPoids);
18 ilm 1263
 
1264
        // Service
1265
        String sService = props.getStringProperty("ArticleService");
1266
        Boolean bService = Boolean.valueOf(sService);
1267
        if (bService != null && bService.booleanValue()) {
67 ilm 1268
            cAchat.gridx++;
1269
            cAchat.weightx = 0;
1270
            p.add(this.boxService, cAchat);
18 ilm 1271
        }
1272
 
80 ilm 1273
        c.gridx++;
18 ilm 1274
        c.gridwidth = GridBagConstraints.REMAINDER;
1275
        c.anchor = GridBagConstraints.WEST;
1276
        c.weightx = 1;
1277
        c.fill = GridBagConstraints.NONE;
1278
 
1279
        this.add(p, c);
156 ilm 1280
        c.gridx = 0;
1281
        c.gridy++;
1282
        c.gridwidth = 1;
18 ilm 1283
 
156 ilm 1284
        if (getTable().contains("ID_COUT_REVIENT")) {
1285
            // Cout de revient
1286
            c.weightx = 0;
1287
            c.fill = GridBagConstraints.HORIZONTAL;
1288
            this.add(new JLabel(getLabelFor("ID_COUT_REVIENT"), SwingConstants.RIGHT), c);
1289
 
1290
            JPanel pCR = new JPanel(new GridBagLayout());
1291
            GridBagConstraints cCR = new DefaultGridBagConstraints();
1292
            cCR.insets = new Insets(0, 0, 0, 4);
1293
            this.boxCR = new ElementComboBox(true, 15);
1294
 
1295
            pCR.add(boxCR, cCR);
1296
            this.addView(boxCR, "ID_COUT_REVIENT");
1297
            DefaultGridBagConstraints.lockMinimumSize(boxCR);
1298
            boxCR.addModelListener("wantedID", new PropertyChangeListener() {
1299
 
1300
                @Override
1301
                public void propertyChange(PropertyChangeEvent evt) {
1302
                    if (!isFilling()) {
1303
                        ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
1304
                        updateVtFromMarge();
1305
                        ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
1306
                    }
1307
                }
1308
            });
1309
            cCR.gridx++;
1310
            this.boxMargeWithCR = new JCheckBox(getLabelFor("MARGE_WITH_COUT_REVIENT"));
1311
            pCR.add(boxMargeWithCR, cCR);
1312
            addView(boxMargeWithCR, "MARGE_WITH_COUT_REVIENT");
1313
            boxMargeWithCR.addActionListener(new ActionListener() {
1314
 
1315
                @Override
1316
                public void actionPerformed(ActionEvent e) {
1317
                    if (!isFilling()) {
1318
                        ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
1319
                        updateVtFromMarge();
1320
                        ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
1321
                    }
1322
                }
1323
            });
1324
            c.gridx++;
1325
            c.gridwidth = GridBagConstraints.REMAINDER;
1326
            c.anchor = GridBagConstraints.WEST;
1327
            c.weightx = 1;
1328
            c.fill = GridBagConstraints.NONE;
1329
            this.add(pCR, c);
1330
        }
1331
 
18 ilm 1332
        // PV HT
80 ilm 1333
        c.gridx = 0;
1334
        c.gridy++;
1335
 
67 ilm 1336
        JPanel p2 = new JPanel(new GridBagLayout());
1337
        GridBagConstraints cVT = new DefaultGridBagConstraints();
80 ilm 1338
        cVT.insets = new Insets(0, 0, 0, 4);
1339
 
1340
        c.weightx = 0;
1341
        c.fill = GridBagConstraints.HORIZONTAL;
1342
        c.gridwidth = 1;
1343
        this.add(new JLabel(getLabelFor("PV_HT"), SwingConstants.RIGHT), c);
67 ilm 1344
        cVT.gridx++;
1345
        cVT.weightx = 1;
80 ilm 1346
 
67 ilm 1347
        p2.add(this.textPVHT, cVT);
18 ilm 1348
 
1349
        // Taxe
1350
        JLabel labelTaxe = new JLabel(getLabelFor("ID_TAXE"));
67 ilm 1351
        cVT.gridx++;
1352
        cVT.weightx = 0;
1353
        p2.add(labelTaxe, cVT);
18 ilm 1354
        labelTaxe.setHorizontalAlignment(SwingConstants.RIGHT);
67 ilm 1355
        cVT.gridx++;
1356
        // cVT.weightx = 1;
18 ilm 1357
 
67 ilm 1358
        p2.add(this.comboSelTaxe, cVT);
18 ilm 1359
 
1360
        // PV_TTC
67 ilm 1361
        cVT.gridx++;
1362
        cVT.weightx = 0;
1363
        p2.add(new JLabel(getLabelFor("PV_TTC")), cVT);
1364
        cVT.gridx++;
1365
        cVT.weightx = 1;
1366
        p2.add(this.textPVTTC, cVT);
80 ilm 1367
        c.gridx = 1;
1368
 
18 ilm 1369
        c.gridwidth = GridBagConstraints.REMAINDER;
1370
        c.anchor = GridBagConstraints.WEST;
1371
        c.weightx = 1;
1372
        c.fill = GridBagConstraints.NONE;
1373
        this.add(p2, c);
1374
 
1375
        this.addRequiredSQLObject(this.textPAHT, "PA_HT");
1376
        this.addRequiredSQLObject(this.textPVHT, "PV_HT");
67 ilm 1377
        DefaultGridBagConstraints.lockMinimumSize(this.textPVHT);
18 ilm 1378
        this.addRequiredSQLObject(this.comboSelTaxe, "ID_TAXE");
67 ilm 1379
        DefaultGridBagConstraints.lockMinimumSize(this.comboSelTaxe);
80 ilm 1380
        DefaultGridBagConstraints.lockMaximumSize(this.comboSelTaxe);
18 ilm 1381
        this.addRequiredSQLObject(this.textPVTTC, "PV_TTC");
67 ilm 1382
        DefaultGridBagConstraints.lockMinimumSize(this.textPVTTC);
1383
        DefaultGridBagConstraints.lockMinimumSize(this.textPAHT);
1384
        DefaultGridBagConstraints.lockMinimumSize(this.textMarge);
18 ilm 1385
        this.ttcDocListener = new DocumentListener() {
1386
            public void changedUpdate(DocumentEvent e) {
1387
                setTextHT();
1388
            }
1389
 
1390
            public void insertUpdate(DocumentEvent e) {
1391
                setTextHT();
1392
            }
1393
 
1394
            public void removeUpdate(DocumentEvent e) {
1395
                setTextHT();
1396
            }
1397
        };
1398
 
1399
        this.htDocListener = new DocumentListener() {
1400
            public void changedUpdate(DocumentEvent e) {
1401
                setTextTTC();
1402
            }
1403
 
1404
            public void insertUpdate(DocumentEvent e) {
1405
                setTextTTC();
1406
            }
1407
 
1408
            public void removeUpdate(DocumentEvent e) {
1409
                setTextTTC();
1410
            }
1411
 
1412
        };
1413
 
1414
        this.detailsListener = new SimpleDocumentListener() {
1415
 
1416
            @Override
1417
            public void update(DocumentEvent e) {
1418
                updatePiece();
1419
            }
1420
 
1421
        };
1422
 
1423
        this.taxeListener = new PropertyChangeListener() {
1424
 
1425
            public void propertyChange(PropertyChangeEvent evt) {
156 ilm 1426
                if (!isFilling()) {
1427
                    if (ReferenceArticleSQLComponent.this.textPVHT.getText().trim().length() > 0) {
1428
                        setTextTTC();
1429
                    } else {
1430
                        setTextHT();
1431
                    }
18 ilm 1432
                }
19 ilm 1433
                tableTarifVente.fireModification();
18 ilm 1434
            }
1435
        };
1436
        this.textPVHT.getDocument().addDocumentListener(this.htDocListener);
1437
        this.textPVTTC.getDocument().addDocumentListener(this.ttcDocListener);
1438
        this.comboSelTaxe.addValueListener(this.taxeListener);
1439
 
1440
        this.textMetrique1HA.getDocument().addDocumentListener(this.detailsListener);
1441
        this.textMetrique1VT.getDocument().addDocumentListener(this.detailsListener);
1442
 
1443
        this.textValMetrique1.getDocument().addDocumentListener(this.detailsListener);
1444
        this.textValMetrique2.getDocument().addDocumentListener(this.detailsListener);
1445
        this.textValMetrique3.getDocument().addDocumentListener(this.detailsListener);
1446
 
1447
    }
1448
 
1449
    private void setListenerModeVenteActive(boolean b) {
1450
        if (b) {
1451
            this.comboSelModeVente.addValueListener(this.propertyChangeListener);
1452
        } else {
1453
            this.comboSelModeVente.removePropertyChangeListener(this.propertyChangeListener);
1454
        }
1455
    }
1456
 
1457
    /**
1458
     * @param c
1459
     * @param props
1460
     */
1461
    private void addModeVenteAvance(GridBagConstraints c) {
1462
        DefaultProps props = DefaultNXProps.getInstance();
1463
        JSeparator sep = new JSeparator();
65 ilm 1464
        JLabel labelDetails = new JLabel("Article détaillé", SwingConstants.RIGHT);
18 ilm 1465
        c.gridx = 0;
1466
        c.gridy++;
1467
        c.weightx = 0;
1468
        this.add(labelDetails, c);
1469
        c.gridx++;
1470
        c.gridwidth = GridBagConstraints.REMAINDER;
1471
        c.fill = GridBagConstraints.HORIZONTAL;
1472
        c.weightx = 1;
1473
        this.add(sep, c);
1474
 
1475
        // Mode de vente
1476
        c.gridwidth = 1;
1477
        c.weightx = 0;
1478
        c.gridx = 0;
1479
        c.gridy++;
65 ilm 1480
        this.add(new JLabel(getLabelFor("ID_MODE_VENTE_ARTICLE"), SwingConstants.RIGHT), c);
1481
        c.weightx = 1;
18 ilm 1482
        c.gridx++;
1483
        this.add(this.comboSelModeVente, c);
1484
 
1485
        // Prix metrique
1486
        c.gridx = 0;
65 ilm 1487
        c.weightx = 0;
18 ilm 1488
        c.gridy++;
1489
        this.add(this.labelMetriqueHA1, c);
1490
        c.gridx++;
1491
        c.weightx = 1;
1492
        this.add(this.textMetrique1HA, c);
1493
 
1494
        c.gridx++;
1495
        c.weightx = 0;
1496
        this.add(this.labelMetriqueVT1, c);
1497
        c.gridx++;
1498
        c.weightx = 1;
1499
        this.add(this.textMetrique1VT, c);
1500
 
1501
        // Metrique 1
1502
        c.weightx = 0;
65 ilm 1503
        JLabel labelMetrique1 = new JLabel(getLabelFor("VALEUR_METRIQUE_1"), SwingConstants.RIGHT);
18 ilm 1504
        c.gridx = 0;
1505
        c.gridy++;
1506
        this.add(labelMetrique1, c);
1507
        c.gridx++;
1508
        c.weightx = 1;
1509
        this.add(this.textValMetrique1, c);
1510
        c.gridx++;
1511
        c.weightx = 0;
1512
 
1513
        Boolean bMetrique1 = Boolean.valueOf(props.getStringProperty("ArticleLongueur"));
1514
        labelMetrique1.setVisible(bMetrique1 == null || bMetrique1.booleanValue());
1515
        this.textValMetrique1.setVisible(bMetrique1 == null || bMetrique1.booleanValue());
1516
 
1517
        // Metrique 2
65 ilm 1518
        JLabel labelMetrique2 = new JLabel(getLabelFor("VALEUR_METRIQUE_2"), SwingConstants.RIGHT);
18 ilm 1519
        c.gridx = 0;
1520
        c.gridy++;
1521
        c.weightx = 0;
1522
        this.add(labelMetrique2, c);
1523
        c.gridx++;
1524
        c.weightx = 1;
1525
        this.add(this.textValMetrique2, c);
1526
        c.gridx++;
1527
        c.weightx = 0;
1528
 
1529
        Boolean bMetrique2 = Boolean.valueOf(props.getStringProperty("ArticleLargeur"));
1530
        labelMetrique2.setVisible(bMetrique2 == null || bMetrique2.booleanValue());
1531
        this.textValMetrique2.setVisible(bMetrique2 == null || bMetrique2.booleanValue());
1532
 
1533
        // Metrique 3
65 ilm 1534
        JLabel labelMetrique3 = new JLabel(getLabelFor("VALEUR_METRIQUE_3"), SwingConstants.RIGHT);
18 ilm 1535
        c.gridx = 0;
1536
        c.gridy++;
1537
        c.weightx = 0;
1538
        this.add(labelMetrique3, c);
1539
        c.gridx++;
1540
        c.weightx = 1;
1541
        this.add(this.textValMetrique3, c);
1542
        c.gridx++;
1543
 
1544
        Boolean bMetrique3 = Boolean.valueOf(props.getStringProperty("ArticlePoids"));
1545
        labelMetrique3.setVisible(bMetrique3 == null || bMetrique3.booleanValue());
1546
        this.textValMetrique3.setVisible(bMetrique3 == null || bMetrique3.booleanValue());
1547
 
1548
        // Article détaillé
1549
        JSeparator sep2 = new JSeparator();
65 ilm 1550
        JLabel labelPiece = new JLabel("Article pièce", SwingConstants.RIGHT);
18 ilm 1551
        c.gridx = 0;
1552
        c.gridy++;
1553
        c.weightx = 0;
1554
        this.add(labelPiece, c);
1555
        c.gridx++;
1556
        c.gridwidth = GridBagConstraints.REMAINDER;
1557
        c.fill = GridBagConstraints.HORIZONTAL;
1558
        c.weightx = 1;
1559
        this.add(sep2, c);
1560
 
1561
    }
1562
 
19 ilm 1563
    @Override
1564
    public void update() {
156 ilm 1565
        SQLRow row = this.getTable().getRow(this.getSelectedID());
19 ilm 1566
            super.update();
1567
        this.tableTarifVente.updateField("ID_ARTICLE", getSelectedID());
156 ilm 1568
        this.tableCatComptable.updateField("ID_ARTICLE", getSelectedID());
1569
        this.tableFourSec.updateField("ID_ARTICLE", getSelectedID());
94 ilm 1570
        this.tableTarifQteVente.updateField("ID_ARTICLE", getSelectedID());
1571
        if (this.tableBom != null) {
1572
            this.tableBom.updateField("ID_ARTICLE_PARENT", getSelectedID());
1573
        }
1574
 
19 ilm 1575
        this.tableDes.updateField("ID_ARTICLE", getSelectedID());
144 ilm 1576
        this.tableCodeClient.updateField("ID_ARTICLE", getSelectedID());
67 ilm 1577
        if (this.codeFournisseurTable != null) {
1578
            this.codeFournisseurTable.updateField("ID_ARTICLE", getSelectedID());
1579
        }
156 ilm 1580
 
1581
        ((ReferenceArticleSQLElement) getElement()).initStock(getSelectedID());
1582
 
1583
        SQLSelect sel = new SQLSelect();
1584
        SQLTable tableStock = getTable().getTable("STOCK");
1585
        sel.addSelect(tableStock.getKey());
1586
        Where w = new Where(tableStock.getField("ID_ARTICLE"), "=", getSelectedID()).and(new Where(tableStock.getField("ID_DEPOT_STOCK"), "=", row.getForeignID("ID_DEPOT_STOCK")));
1587
        sel.setWhere(w);
1588
 
1589
        List<SQLRow> stock = SQLRowListRSH.execute(sel);
1590
        if (stock != null && stock.size() == 1) {
1591
            try {
1592
                row.createEmptyUpdateRow().put("ID_STOCK", stock.get(0).getID()).update();
1593
            } catch (SQLException e) {
1594
                ExceptionHandler.handle("Erreur lors de la mise à jour du stock principal", e);
1595
            }
1596
        }
1597
 
19 ilm 1598
    }
18 ilm 1599
 
1600
    /**
1601
     * Sélection d'un mode de vente pour l'article. Affiche les prix metriques requis et fixe les
1602
     * valeurs.
1603
     *
1604
     * @param id id du mode de vente
1605
     */
1606
    private void selectModeVente(int id) {
1607
 
1608
        this.labelMetriqueHA1.setEnabled(true);
1609
        this.labelMetriqueVT1.setEnabled(true);
1610
        this.textMetrique1HA.setEnabled(true);
1611
        this.textMetrique1VT.setEnabled(true);
1612
 
1613
        this.textPAHT.getDocument().removeDocumentListener(this.pieceHAArticle);
1614
        this.textPVHT.getDocument().removeDocumentListener(this.pieceVTArticle);
1615
 
1616
        switch (id) {
1617
        case ReferenceArticleSQLElement.AU_METRE_CARRE:
1618
            this.labelMetriqueHA1.setText("Prix d'achat HT au mètre carré");
1619
            this.labelMetriqueVT1.setText("Prix de vente HT au mètre carré");
1620
            break;
1621
        case ReferenceArticleSQLElement.AU_METRE_LARGEUR:
1622
        case ReferenceArticleSQLElement.AU_METRE_LONGUEUR:
1623
            this.labelMetriqueHA1.setText("Prix d'achat HT au mètre");
1624
            this.labelMetriqueVT1.setText("Prix de vente HT au mètre");
1625
            break;
1626
 
1627
        case ReferenceArticleSQLElement.AU_POID_METRECARRE:
1628
            this.labelMetriqueHA1.setText("Prix d'achat HT au kilo");
1629
            this.labelMetriqueVT1.setText("Prix de vente HT au kilo");
1630
            break;
1631
        case -1:
73 ilm 1632
            // No break need to enable the listener
18 ilm 1633
        default:
1634
            this.labelMetriqueHA1.setEnabled(false);
1635
            this.labelMetriqueVT1.setEnabled(false);
1636
            this.textMetrique1HA.setEnabled(false);
1637
            this.textMetrique1VT.setEnabled(false);
1638
 
67 ilm 1639
            this.textMetrique1HA.setText(this.textPAHT.getText().trim());
1640
            this.textMetrique1VT.setText(this.textPVHT.getText().trim());
18 ilm 1641
            this.textPAHT.getDocument().addDocumentListener(this.pieceHAArticle);
1642
            this.textPVHT.getDocument().addDocumentListener(this.pieceVTArticle);
1643
            break;
1644
        }
19 ilm 1645
        this.tableTarifVente.fireModification();
1646
    }
18 ilm 1647
 
19 ilm 1648
    @Override
1649
    public int insert(SQLRow order) {
1650
        int id = super.insert(order);
1651
        this.tableTarifVente.updateField("ID_ARTICLE", id);
156 ilm 1652
        this.tableCatComptable.updateField("ID_ARTICLE", id);
1653
        this.tableFourSec.updateField("ID_ARTICLE", id);
94 ilm 1654
        this.tableTarifQteVente.updateField("ID_ARTICLE", id);
1655
        if (this.tableBom != null) {
1656
            this.tableBom.updateField("ID_ARTICLE_PARENT", id);
1657
        }
19 ilm 1658
        this.tableDes.updateField("ID_ARTICLE", id);
144 ilm 1659
        this.tableCodeClient.updateField("ID_ARTICLE", id);
67 ilm 1660
        if (this.codeFournisseurTable != null) {
1661
            this.codeFournisseurTable.updateField("ID_ARTICLE", id);
1662
        }
156 ilm 1663
        ((ReferenceArticleSQLElement) getElement()).initStock(id);
19 ilm 1664
        return id;
18 ilm 1665
    }
1666
 
1667
    @Override
1668
    protected SQLRowValues createDefaults() {
1669
        SQLRowValues rowVals = new SQLRowValues(getTable());
1670
 
94 ilm 1671
        rowVals.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
156 ilm 1672
        rowVals.put("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID);
61 ilm 1673
        rowVals.put("ID_UNITE_VENTE", UniteVenteArticleSQLElement.A_LA_PIECE);
156 ilm 1674
        rowVals.put("QTE_UNITAIRE", BigDecimal.ONE);
18 ilm 1675
        rowVals.put("ID_MODE_VENTE_ARTICLE", ReferenceArticleSQLElement.A_LA_PIECE);
1676
        selectModeVente(ReferenceArticleSQLElement.A_LA_PIECE);
1677
        rowVals.put("VALEUR_METRIQUE_1", Float.valueOf("1.0"));
67 ilm 1678
        rowVals.put("PA_HT", BigDecimal.ZERO);
156 ilm 1679
        rowVals.put("QTE_UNITAIRE", BigDecimal.ONE);
18 ilm 1680
        rowVals.put("POIDS", Float.valueOf(0));
156 ilm 1681
        rowVals.put("GESTION_STOCK", Boolean.TRUE);
19 ilm 1682
 
18 ilm 1683
        return rowVals;
1684
    }
1685
 
1686
    private void setTextHT() {
149 ilm 1687
        if (!isFilling()) {
1688
            this.textPVHT.getDocument().removeDocumentListener(this.htDocListener);
1689
            final BigDecimal ttc = StringUtils.getBigDecimalFromUserText(this.textPVTTC.getText());
1690
            if (ttc != null) {
1691
                int id = this.comboSelTaxe.getSelectedId();
1692
                if (id > 1) {
1693
                    Float resultTaux = TaxeCache.getCache().getTauxFromId(id);
1694
                    float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue() / 100.0F;
1695
                    this.textPVHT.setText(ttc.divide(BigDecimal.valueOf(taux).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION)
1696
                            .setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1697
                }
67 ilm 1698
            }
149 ilm 1699
            this.textPVHT.getDocument().addDocumentListener(this.htDocListener);
18 ilm 1700
        }
1701
    }
1702
 
1703
    private void setTextTTC() {
149 ilm 1704
        if (!isFilling()) {
1705
            this.textPVTTC.getDocument().removeDocumentListener(this.ttcDocListener);
1706
            final BigDecimal ht = StringUtils.getBigDecimalFromUserText(this.textPVHT.getText());
1707
            if (ht != null) {
1708
                int id = this.comboSelTaxe.getSelectedId();
1709
                if (id > 1) {
1710
                    final Float resultTaux = TaxeCache.getCache().getTauxFromId(id);
1711
                    final float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue() / 100.0F;
1712
                    this.textPVTTC.setText(ht.multiply(BigDecimal.valueOf(taux).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION)
1713
                            .setScale(getTable().getField("PV_TTC").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1714
                }
67 ilm 1715
            }
149 ilm 1716
            this.textPVTTC.getDocument().addDocumentListener(this.ttcDocListener);
18 ilm 1717
        }
1718
    }
1719
 
1720
    /**
1721
     * calcul du prix achat et vente ainsi que le poids total pour la piece
1722
     */
1723
    private void updatePiece() {
1724
        if (this.comboSelModeVente.getSelectedId() > 1 && this.comboSelModeVente.getSelectedId() != ReferenceArticleSQLElement.A_LA_PIECE) {
1725
            SQLRowValues rowVals = getDetailsRowValues();
1726
            float poidsTot = ReferenceArticleSQLElement.getPoidsFromDetails(rowVals);
1727
            this.textPoids.setText(String.valueOf(poidsTot));
67 ilm 1728
            this.textPAHT.setText(ReferenceArticleSQLElement.getPrixHAFromDetails(rowVals).setScale(getTable().getField("PA_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1729
            this.textPVHT.setText(ReferenceArticleSQLElement.getPrixVTFromDetails(rowVals).setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
19 ilm 1730
            this.tableTarifVente.fireModification();
18 ilm 1731
        }
1732
    }
1733
 
19 ilm 1734
    public int getSelectedTaxe() {
1735
        return this.comboSelTaxe.getSelectedId();
1736
    }
1737
 
1738
    public SQLRowValues getDetailsRowValues() {
73 ilm 1739
        final SQLRowValues rowVals = new SQLRowValues(getTable());
18 ilm 1740
 
83 ilm 1741
        BigDecimal pAchat = StringUtils.getBigDecimalFromUserText(this.textMetrique1HA.getText());
1742
        if (pAchat == null) {
1743
            pAchat = BigDecimal.ZERO;
1744
        }
1745
        rowVals.put("PRIX_METRIQUE_HA_1", pAchat);
67 ilm 1746
 
83 ilm 1747
        BigDecimal pVente = StringUtils.getBigDecimalFromUserText(this.textMetrique1VT.getText());
1748
        if (pVente == null) {
1749
            pVente = BigDecimal.ZERO;
1750
        }
1751
        rowVals.put("PRIX_METRIQUE_VT_1", pVente);
18 ilm 1752
        put(rowVals, this.textValMetrique1);
1753
        put(rowVals, this.textValMetrique2);
1754
        put(rowVals, this.textValMetrique3);
1755
        rowVals.put("ID_MODE_VENTE_ARTICLE", this.comboSelModeVente.getSelectedId());
1756
 
1757
        return rowVals;
1758
    }
1759
 
1760
    private void put(SQLRowValues rowVals, JTextField comp) {
1761
        Float f = (comp.getText() == null || comp.getText().trim().length() == 0) ? 0.0F : Float.valueOf(comp.getText());
1762
        rowVals.put(this.getView(comp).getField().getName(), f);
1763
    }
1764
 
1765
}