OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 90 | 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;
18 ilm 20
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
21
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
61 ilm 22
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
19 ilm 23
import org.openconcerto.erp.core.sales.product.ui.ArticleDesignationTable;
24
import org.openconcerto.erp.core.sales.product.ui.ArticleTarifTable;
67 ilm 25
import org.openconcerto.erp.model.ISQLCompteSelector;
18 ilm 26
import org.openconcerto.erp.preferences.DefaultNXProps;
61 ilm 27
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 28
import org.openconcerto.sql.Configuration;
29
import org.openconcerto.sql.element.BaseSQLComponent;
30
import org.openconcerto.sql.element.SQLElement;
31
import org.openconcerto.sql.model.SQLRow;
32
import org.openconcerto.sql.model.SQLRowAccessor;
33
import org.openconcerto.sql.model.SQLRowListRSH;
34
import org.openconcerto.sql.model.SQLRowValues;
35
import org.openconcerto.sql.model.SQLSelect;
36
import org.openconcerto.sql.model.Where;
61 ilm 37
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 38
import org.openconcerto.sql.sqlobject.ElementComboBox;
39
import org.openconcerto.ui.DefaultGridBagConstraints;
27 ilm 40
import org.openconcerto.ui.FormLayouter;
19 ilm 41
import org.openconcerto.ui.component.ITextArea;
18 ilm 42
import org.openconcerto.ui.preferences.DefaultProps;
83 ilm 43
import org.openconcerto.utils.StringUtils;
18 ilm 44
import org.openconcerto.utils.text.SimpleDocumentListener;
45
 
19 ilm 46
import java.awt.Component;
18 ilm 47
import java.awt.GridBagConstraints;
48
import java.awt.GridBagLayout;
80 ilm 49
import java.awt.Insets;
19 ilm 50
import java.awt.event.ActionEvent;
51
import java.awt.event.ActionListener;
18 ilm 52
import java.beans.PropertyChangeEvent;
53
import java.beans.PropertyChangeListener;
19 ilm 54
import java.math.BigDecimal;
67 ilm 55
import java.math.MathContext;
56
import java.math.RoundingMode;
18 ilm 57
import java.sql.SQLException;
58
import java.util.List;
59
 
19 ilm 60
import javax.swing.JButton;
18 ilm 61
import javax.swing.JCheckBox;
62
import javax.swing.JLabel;
19 ilm 63
import javax.swing.JOptionPane;
18 ilm 64
import javax.swing.JPanel;
65
import javax.swing.JSeparator;
19 ilm 66
import javax.swing.JTabbedPane;
18 ilm 67
import javax.swing.JTextField;
68
import javax.swing.SwingConstants;
69
import javax.swing.event.DocumentEvent;
70
import javax.swing.event.DocumentListener;
71
 
72
public class ReferenceArticleSQLComponent extends BaseSQLComponent {
73
 
67 ilm 74
    private JTextField textPVHT, textPVTTC, textPAHT;
75
    private JTextField textMetrique1VT, textMetrique1HA;
18 ilm 76
 
73 ilm 77
    private final JCheckBox boxService = new JCheckBox(getLabelFor("SERVICE"));
78
    private final JCheckBox checkObs = new JCheckBox(getLabelFor("OBSOLETE"));
18 ilm 79
    private JTextField textNom, textCode;
80
    private JTextField textPoids;
81
    private JTextField textValMetrique1, textValMetrique2, textValMetrique3;
82
    private DocumentListener htDocListener, ttcDocListener, detailsListener;
73 ilm 83
    private PropertyChangeListener propertyChangeListener;
18 ilm 84
    private PropertyChangeListener taxeListener;
73 ilm 85
    private final ElementComboBox comboSelTaxe = new ElementComboBox(false, 10);
86
    private final ElementComboBox comboSelModeVente = new ElementComboBox(false, 25);
65 ilm 87
    private JLabel labelMetriqueHA1 = new JLabel(getLabelFor("PRIX_METRIQUE_HA_1"), SwingConstants.RIGHT);
88
    private JLabel labelMetriqueVT1 = new JLabel(getLabelFor("PRIX_METRIQUE_VT_1"), SwingConstants.RIGHT);
19 ilm 89
 
73 ilm 90
    private ArticleDesignationTable tableDes = new ArticleDesignationTable();
91
    private ArticleTarifTable tableTarifVente = new ArticleTarifTable(this);
92
    private final JTextField textMarge = new JTextField(15);
19 ilm 93
 
18 ilm 94
    private DocumentListener pieceHAArticle = new DocumentListener() {
95
 
96
        public void changedUpdate(DocumentEvent arg0) {
97
            ReferenceArticleSQLComponent.this.textMetrique1HA.setText(ReferenceArticleSQLComponent.this.textPAHT.getText());
98
        }
99
 
100
        public void insertUpdate(DocumentEvent arg0) {
101
            ReferenceArticleSQLComponent.this.textMetrique1HA.setText(ReferenceArticleSQLComponent.this.textPAHT.getText());
102
        }
103
 
104
        public void removeUpdate(DocumentEvent arg0) {
105
            ReferenceArticleSQLComponent.this.textMetrique1HA.setText(ReferenceArticleSQLComponent.this.textPAHT.getText());
106
        }
107
 
108
    };
109
    private DocumentListener pieceVTArticle = new DocumentListener() {
110
 
111
        public void changedUpdate(DocumentEvent arg0) {
112
            ReferenceArticleSQLComponent.this.textMetrique1VT.setText(ReferenceArticleSQLComponent.this.textPVHT.getText());
113
        }
114
 
115
        public void insertUpdate(DocumentEvent arg0) {
116
            ReferenceArticleSQLComponent.this.textMetrique1VT.setText(ReferenceArticleSQLComponent.this.textPVHT.getText());
117
        }
118
 
119
        public void removeUpdate(DocumentEvent arg0) {
120
            ReferenceArticleSQLComponent.this.textMetrique1VT.setText(ReferenceArticleSQLComponent.this.textPVHT.getText());
121
        }
122
 
123
    };
124
 
125
    private DocumentListener listenerMargeTextMarge = new SimpleDocumentListener() {
126
        @Override
127
        public void update(DocumentEvent e) {
128
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
129
            updateVtFromMarge();
130
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
131
        }
132
 
133
    };
134
 
135
    private DocumentListener listenerMargeTextVT = new SimpleDocumentListener() {
136
        @Override
137
        public void update(DocumentEvent e) {
138
            ReferenceArticleSQLComponent.this.textMarge.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextMarge);
139
            if (ReferenceArticleSQLComponent.this.textPVHT.getText().trim().length() > 0 && ReferenceArticleSQLComponent.this.textPAHT.getText().trim().length() > 0) {
83 ilm 140
                final BigDecimal vt = StringUtils.getBigDecimalFromUserText(ReferenceArticleSQLComponent.this.textPVHT.getText());
141
                final BigDecimal ha = StringUtils.getBigDecimalFromUserText(ReferenceArticleSQLComponent.this.textPAHT.getText());
67 ilm 142
 
18 ilm 143
                if (vt != null && ha != null) {
73 ilm 144
                    if (vt.signum() != 0 && ha.signum() != 0) {
67 ilm 145
                        BigDecimal margeHT = vt.subtract(ha);
21 ilm 146
 
67 ilm 147
                        BigDecimal value;
148
 
21 ilm 149
                        if (DefaultNXProps.getInstance().getBooleanValue(TotalPanel.MARGE_MARQUE, false)) {
67 ilm 150
                            if (vt.compareTo(BigDecimal.ZERO) > 0) {
151
                                value = margeHT.divide(vt, MathContext.DECIMAL128).multiply(BigDecimal.valueOf(100), MathContext.DECIMAL128);
21 ilm 152
                            } else {
67 ilm 153
                                value = BigDecimal.ZERO;
21 ilm 154
                            }
155
                        } else {
67 ilm 156
                            value = margeHT.divide(ha, MathContext.DECIMAL128).multiply(BigDecimal.valueOf(100), MathContext.DECIMAL128);
21 ilm 157
                        }
158
 
67 ilm 159
                        if (value.compareTo(BigDecimal.ZERO) > 0) {
160
                            ReferenceArticleSQLComponent.this.textMarge.setText(value.setScale(6, RoundingMode.HALF_UP).toString());
18 ilm 161
                        } else {
162
                            ReferenceArticleSQLComponent.this.textMarge.setText("0");
163
                        }
164
                    }
165
                }
166
            }
167
            ReferenceArticleSQLComponent.this.textMarge.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextMarge);
168
        }
169
    };
170
 
171
    private DocumentListener listenerMargeTextHA = new SimpleDocumentListener() {
172
        @Override
173
        public void update(DocumentEvent e) {
174
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().removeDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
175
            updateVtFromMarge();
176
            ReferenceArticleSQLComponent.this.textPVHT.getDocument().addDocumentListener(ReferenceArticleSQLComponent.this.listenerMargeTextVT);
177
        }
178
    };
179
 
180
    private void updateVtFromMarge() {
67 ilm 181
        if (this.textPAHT.getText().trim().length() > 0) {
21 ilm 182
 
83 ilm 183
            BigDecimal ha = StringUtils.getBigDecimalFromUserText(this.textPAHT.getText());
18 ilm 184
            if (ha != null && this.textMarge.getText().trim().length() > 0) {
73 ilm 185
 
83 ilm 186
                BigDecimal d = StringUtils.getBigDecimalFromUserText(this.textMarge.getText());
21 ilm 187
                if (DefaultNXProps.getInstance().getBooleanValue(TotalPanel.MARGE_MARQUE, false)) {
67 ilm 188
                    final BigDecimal e = BigDecimal.ONE.subtract(d.divide(BigDecimal.valueOf(100), MathContext.DECIMAL128));
73 ilm 189
                    if (e.signum() == 0) {
21 ilm 190
                        this.textPVHT.setText("0");
191
                    } else {
67 ilm 192
                        this.textPVHT.setText(ha.divide(e, MathContext.DECIMAL128).setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
21 ilm 193
                    }
194
                } else {
67 ilm 195
                    BigDecimal result = ha.multiply(d.divide(BigDecimal.valueOf(100), MathContext.DECIMAL128).add(BigDecimal.ONE));
196
                    this.textPVHT.setText(result.setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
21 ilm 197
                }
18 ilm 198
            }
199
        }
200
    }
201
 
202
    public ReferenceArticleSQLComponent(SQLElement elt) {
203
        super(elt);
204
    }
205
 
206
    @Override
207
    public void select(SQLRowAccessor r) {
208
        super.select(r);
209
        if (r != null && r.getID() > getTable().getUndefinedID()) {
210
            this.checkObs.setVisible(true);
19 ilm 211
            this.tableTarifVente.setArticleValues(r);
212
            this.tableTarifVente.insertFrom("ID_ARTICLE", r.getID());
213
            this.tableDes.insertFrom("ID_ARTICLE", r.getID());
67 ilm 214
            if (this.codeFournisseurTable != null) {
215
                this.codeFournisseurTable.insertFrom("ID_ARTICLE", r.getID());
216
            }
18 ilm 217
        }
218
    }
219
 
220
    public void addViews() {
221
        this.setLayout(new GridBagLayout());
222
        final GridBagConstraints c = new DefaultGridBagConstraints();
223
 
67 ilm 224
        this.textPVHT = new JTextField(15);
225
        this.textPVTTC = new JTextField(15);
226
        this.textPAHT = new JTextField(15);
18 ilm 227
        this.textPVHT.getDocument().addDocumentListener(this.listenerMargeTextVT);
228
 
229
        // Init metrique devise field
67 ilm 230
        this.textMetrique1HA = new JTextField(15);
231
        this.textMetrique1VT = new JTextField(15);
18 ilm 232
 
233
        // init metrique value field
67 ilm 234
        this.textValMetrique1 = new JTextField(15);
235
        this.textValMetrique2 = new JTextField(15);
236
        this.textValMetrique3 = new JTextField(15);
18 ilm 237
 
238
        this.textCode = new JTextField();
239
        this.textNom = new JTextField();
240
        this.textPoids = new JTextField(6);
241
 
242
        // Code
19 ilm 243
        JLabel codelabel = new JLabel(getLabelFor("CODE"));
244
        codelabel.setHorizontalAlignment(SwingConstants.RIGHT);
245
        DefaultGridBagConstraints.lockMinimumSize(codelabel);
246
        this.add(codelabel, c);
18 ilm 247
        c.gridx++;
19 ilm 248
        c.weightx = 1;
249
        DefaultGridBagConstraints.lockMinimumSize(textCode);
18 ilm 250
        this.add(this.textCode, c);
251
 
19 ilm 252
        // Famille
18 ilm 253
        c.gridx++;
19 ilm 254
        c.gridwidth = 1;
18 ilm 255
        c.weightx = 0;
19 ilm 256
        JLabel labelFamille = new JLabel(getLabelFor("ID_FAMILLE_ARTICLE"));
257
        labelFamille.setHorizontalAlignment(SwingConstants.RIGHT);
258
        DefaultGridBagConstraints.lockMinimumSize(labelFamille);
259
        this.add(labelFamille, c);
18 ilm 260
        c.gridx++;
19 ilm 261
        c.weightx = 1;
262
        c.gridwidth = 1;
263
        final ElementComboBox comboSelFamille = new ElementComboBox(false, 25);
67 ilm 264
        this.addSQLObject(comboSelFamille, "ID_FAMILLE_ARTICLE");
19 ilm 265
        DefaultGridBagConstraints.lockMinimumSize(comboSelFamille);
266
        this.add(comboSelFamille, c);
18 ilm 267
 
19 ilm 268
        // Nom
18 ilm 269
        c.gridy++;
270
        c.gridx = 0;
271
        c.weightx = 0;
19 ilm 272
        JLabel labelNom = new JLabel(getLabelFor("NOM"));
273
        labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
274
        DefaultGridBagConstraints.lockMinimumSize(labelNom);
275
        this.add(labelNom, c);
18 ilm 276
        c.gridx++;
19 ilm 277
        c.weightx = 1;
278
        DefaultGridBagConstraints.lockMinimumSize(textNom);
279
        this.add(this.textNom, c);
18 ilm 280
 
19 ilm 281
        // Code barre
18 ilm 282
        c.gridx++;
19 ilm 283
        c.weightx = 0;
284
        JLabel labelCodeBarre = new JLabel(getLabelFor("CODE_BARRE"));
285
        labelCodeBarre.setHorizontalAlignment(SwingConstants.RIGHT);
286
        DefaultGridBagConstraints.lockMinimumSize(labelCodeBarre);
287
        this.add(labelCodeBarre, c);
288
        c.gridx++;
18 ilm 289
        c.weightx = 1;
19 ilm 290
        JTextField fieldCodeBarre = new JTextField();
291
        DefaultGridBagConstraints.lockMinimumSize(fieldCodeBarre);
292
        this.add(fieldCodeBarre, c);
293
        this.addView(fieldCodeBarre, "CODE_BARRE");
18 ilm 294
 
61 ilm 295
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
296
        // Gestion des unités de vente
297
        final boolean gestionUV = prefs.getBoolean(GestionArticleGlobalPreferencePanel.UNITE_VENTE, true);
298
        if (gestionUV) {
299
            c.gridy++;
300
            c.gridx = 0;
65 ilm 301
            c.weightx = 0;
302
            this.add(new JLabel(getLabelFor("ID_UNITE_VENTE"), SwingConstants.RIGHT), c);
61 ilm 303
            c.gridx++;
65 ilm 304
            c.weightx = 1;
80 ilm 305
            c.fill = GridBagConstraints.NONE;
61 ilm 306
            ElementComboBox boxUnite = new ElementComboBox();
80 ilm 307
            DefaultGridBagConstraints.lockMinimumSize(boxUnite);
61 ilm 308
            this.add(boxUnite, c);
309
            this.addView(boxUnite, "ID_UNITE_VENTE");
80 ilm 310
            c.fill = GridBagConstraints.HORIZONTAL;
61 ilm 311
        }
19 ilm 312
        DefaultProps props = DefaultNXProps.getInstance();
18 ilm 313
 
314
        // Article détaillé
315
        String modeVente = props.getStringProperty("ArticleModeVenteAvance");
316
        Boolean bModeVente = Boolean.valueOf(modeVente);
317
        boolean modeVenteAvance = (bModeVente == null || bModeVente.booleanValue());
318
 
319
        if (modeVenteAvance) {
320
            addModeVenteAvance(c);
321
        }
322
 
323
        getMontantPanel(c, props);
324
 
27 ilm 325
        // Champ Module
326
        c.gridx = 0;
327
        c.gridy++;
328
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 329
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
27 ilm 330
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
41 ilm 331
        this.add(addP, c);
27 ilm 332
 
19 ilm 333
        JTabbedPane pane = new JTabbedPane();
18 ilm 334
        c.gridy++;
335
        c.weightx = 1;
67 ilm 336
        c.weighty = 1;
19 ilm 337
 
338
        pane.add("Tarifs de vente", createTarifPanel());
339
        pane.add("Exportation", createExportationPanel());
340
        pane.add("Achat", createAchatPanel());
341
        pane.add("Stock", createStockPanel());
342
        pane.add("Descriptif", createDescriptifPanel());
343
        pane.add("Désignations multilingues", createDesignationPanel());
67 ilm 344
        pane.add("Comptabilité", createComptaPanel());
345
        pane.add(getLabelFor("INFOS"), createInfosPanel());
346
 
18 ilm 347
        c.fill = GridBagConstraints.BOTH;
19 ilm 348
        this.add(pane, c);
349
 
18 ilm 350
        this.addSQLObject(this.textMetrique1HA, "PRIX_METRIQUE_HA_1");
351
        this.addSQLObject(this.textMetrique1VT, "PRIX_METRIQUE_VT_1");
352
        this.addSQLObject(this.textValMetrique1, "VALEUR_METRIQUE_1");
353
        this.addSQLObject(this.textValMetrique2, "VALEUR_METRIQUE_2");
354
        this.addSQLObject(this.textValMetrique3, "VALEUR_METRIQUE_3");
355
        this.addSQLObject(this.comboSelModeVente, "ID_MODE_VENTE_ARTICLE");
356
        this.addSQLObject(this.boxService, "SERVICE");
357
 
358
        this.addRequiredSQLObject(this.textNom, "NOM");
359
        this.addRequiredSQLObject(this.textCode, "CODE");
360
 
361
        this.addSQLObject(this.textPoids, "POIDS");
362
 
363
        this.comboSelTaxe.setButtonsVisible(false);
364
        this.propertyChangeListener = new PropertyChangeListener() {
365
 
366
            public void propertyChange(PropertyChangeEvent evt) {
367
                System.err.println(ReferenceArticleSQLComponent.this.comboSelModeVente.getSelectedId());
368
                selectModeVente(ReferenceArticleSQLComponent.this.comboSelModeVente.getSelectedId());
19 ilm 369
 
18 ilm 370
            }
371
        };
372
        setListenerModeVenteActive(true);
373
        this.comboSelModeVente.setValue(ReferenceArticleSQLElement.A_LA_PIECE);
374
    }
375
 
67 ilm 376
    private Component createInfosPanel() {
377
        JPanel panel = new JPanel(new GridBagLayout());
378
        panel.setOpaque(false);
379
        GridBagConstraints c = new DefaultGridBagConstraints();
380
 
381
        c.weightx = 1;
382
        c.weighty = 1;
383
        ITextArea infos = new ITextArea();
384
        c.fill = GridBagConstraints.BOTH;
385
        panel.add(infos, c);
386
 
387
        this.addSQLObject(infos, "INFOS");
388
 
389
        return panel;
390
    }
391
 
19 ilm 392
    private Component createDescriptifPanel() {
393
        JPanel panel = new JPanel(new GridBagLayout());
394
        panel.setOpaque(false);
395
        GridBagConstraints c = new DefaultGridBagConstraints();
396
 
397
        // Obsolete
398
        c.fill = GridBagConstraints.NONE;
41 ilm 399
        c.gridwidth = GridBagConstraints.REMAINDER;
19 ilm 400
        c.weightx = 1;
25 ilm 401
        this.checkObs.setOpaque(false);
19 ilm 402
        panel.add(this.checkObs, c);
403
 
404
        this.checkObs.setVisible(false);
405
        this.addView(this.checkObs, "OBSOLETE");
406
 
41 ilm 407
        if (getTable().getFieldsName().contains("COLORIS")) {
408
            JTextField fieldColoris = new JTextField();
409
            c.gridy++;
410
            c.fill = GridBagConstraints.HORIZONTAL;
411
            c.weightx = 0;
412
            c.gridwidth = 1;
413
            panel.add(new JLabel(getLabelFor("COLORIS")), c);
414
 
415
            c.weightx = 1;
416
            c.gridx++;
417
            panel.add(fieldColoris, c);
418
            this.addView(fieldColoris, "COLORIS");
419
        }
19 ilm 420
        ITextArea area = new ITextArea();
421
        JLabel sep = new JLabel("Descriptif complet");
422
        c.gridy++;
41 ilm 423
        c.gridx = 0;
424
        c.gridwidth = GridBagConstraints.REMAINDER;
19 ilm 425
        c.fill = GridBagConstraints.HORIZONTAL;
426
        panel.add(sep, c);
427
 
428
        c.gridy++;
429
        c.weighty = 1;
430
        c.fill = GridBagConstraints.BOTH;
431
        panel.add(area, c);
432
        this.addView(area, "DESCRIPTIF");
433
        return panel;
434
    }
435
 
436
    private Component createDesignationPanel() {
437
        JPanel panel = new JPanel(new GridBagLayout());
438
        panel.setOpaque(false);
439
        GridBagConstraints c = new DefaultGridBagConstraints();
440
 
441
        // Ajout des
442
        c.gridwidth = 1;
443
        c.weightx = 0;
444
        c.gridy++;
445
        c.gridx = 0;
446
        panel.add(new JLabel("Ajouter une désignation "), c);
447
 
448
        final ElementComboBox boxDes = new ElementComboBox();
449
        boxDes.init(Configuration.getInstance().getDirectory().getElement("LANGUE"));
450
 
451
        c.gridx++;
452
        panel.add(boxDes, c);
453
 
454
        c.fill = GridBagConstraints.NONE;
455
        c.gridx++;
456
        JButton buttonAjouterDes = new JButton("Ajouter");
457
        buttonAjouterDes.setOpaque(false);
458
        panel.add(buttonAjouterDes, c);
459
        c.gridx++;
460
        JButton buttonSupprimerDes = new JButton("Supprimer");
461
        buttonSupprimerDes.setOpaque(false);
462
        panel.add(buttonSupprimerDes, c);
463
 
464
        c.gridy++;
465
        c.gridx = 0;
466
        c.gridwidth = GridBagConstraints.REMAINDER;
467
        c.fill = GridBagConstraints.BOTH;
468
        c.weighty = 1;
469
        c.weightx = 1;
470
        this.tableDes.setOpaque(false);
471
        panel.add(this.tableDes, c);
472
 
473
        // Listerners
474
        buttonAjouterDes.addActionListener(new ActionListener() {
475
 
476
            @Override
477
            public void actionPerformed(ActionEvent e) {
478
 
479
                int id = boxDes.getSelectedId();
480
                if (id <= 1) {
481
                    return;
482
                }
483
                int nbRows = tableDes.getModel().getRowCount();
484
 
485
                for (int i = 0; i < nbRows; i++) {
486
                    SQLRowValues rowVals = tableDes.getModel().getRowValuesAt(i);
487
                    int idLangue = Integer.parseInt(rowVals.getObject("ID_LANGUE").toString());
488
                    if (idLangue == id) {
489
                        JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLa langue est déjà présente dans la liste!");
490
                        return;
491
                    }
492
                }
493
 
494
                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_DESIGNATION"));
495
                if (getSelectedID() > 1) {
496
                    rowVals.put("ID_ARTICLE", getSelectedID());
497
                }
498
                rowVals.put("ID_LANGUE", id);
499
                rowVals.put("NOM", "");
500
                tableDes.getModel().addRow(rowVals);
501
            }
502
        });
503
        buttonSupprimerDes.addActionListener(new ActionListener() {
504
 
505
            @Override
506
            public void actionPerformed(ActionEvent e) {
507
                tableDes.removeSelectedRow();
508
            }
509
        });
510
 
511
        return panel;
512
    }
513
 
514
    private Component createStockPanel() {
515
        JPanel panel = new JPanel(new GridBagLayout());
516
        panel.setOpaque(false);
517
        GridBagConstraints c = new DefaultGridBagConstraints();
518
        DefaultProps props = DefaultNXProps.getInstance();
519
        String stockMin = props.getStringProperty("ArticleStockMin");
520
        Boolean bStockMin = !stockMin.equalsIgnoreCase("false");
521
        boolean gestionStockMin = (bStockMin == null || bStockMin.booleanValue());
522
        c.gridx = 0;
523
        c.gridy++;
524
 
525
        final JCheckBox boxStock = new JCheckBox(getLabelFor("GESTION_STOCK"));
526
        boxStock.setOpaque(false);
527
        panel.add(boxStock, c);
528
        this.addView(boxStock, "GESTION_STOCK");
529
 
530
        final JTextField fieldQteMin = new JTextField();
531
        final JTextField fieldQteAchat = new JTextField();
532
        boxStock.addActionListener(new ActionListener() {
533
 
534
            @Override
535
            public void actionPerformed(ActionEvent e) {
536
                fieldQteMin.setEnabled(boxStock.isSelected());
537
                fieldQteAchat.setEnabled(boxStock.isSelected());
538
            }
539
        });
540
 
541
        c.gridwidth = 1;
542
        if (gestionStockMin) {
543
            c.gridx = 0;
544
            c.gridy++;
545
            c.weightx = 0;
546
            panel.add(new JLabel(getLabelFor("QTE_MIN")), c);
547
            c.gridx++;
548
            c.weightx = 1;
549
            panel.add(fieldQteMin, c);
550
            this.addView(fieldQteMin, "QTE_MIN");
551
 
552
            c.gridx = 0;
553
            c.gridy++;
554
            c.weightx = 0;
555
            panel.add(new JLabel(getLabelFor("QTE_ACHAT")), c);
556
            c.gridx++;
557
            c.weightx = 1;
558
            panel.add(fieldQteAchat, c);
559
            this.addView(fieldQteAchat, "QTE_ACHAT");
560
        }
561
 
562
        c.gridy++;
563
        c.weighty = 1;
564
        c.weightx = 1;
565
        c.fill = GridBagConstraints.BOTH;
566
        final JPanel spacer = new JPanel();
567
        spacer.setOpaque(false);
568
        panel.add(spacer, c);
569
        return panel;
570
    }
571
 
67 ilm 572
    private Component createComptaPanel() {
573
        JPanel panel = new JPanel(new GridBagLayout());
574
        panel.setOpaque(false);
575
        GridBagConstraints c = new DefaultGridBagConstraints();
576
        c.gridwidth = 1;
577
        c.weighty = 0;
578
        c.weightx = 0;
579
        ISQLCompteSelector sel = new ISQLCompteSelector();
580
        c.fill = GridBagConstraints.BOTH;
581
        panel.add(new JLabel(getLabelFor("ID_COMPTE_PCE")), c);
582
        c.gridx++;
583
        c.weightx = 1;
584
        panel.add(sel, c);
585
        this.addView(sel, "ID_COMPTE_PCE");
586
 
587
        c.gridwidth = 1;
588
        c.gridy++;
589
        c.weighty = 0;
590
        c.gridx = 0;
591
        c.weightx = 0;
592
        ISQLCompteSelector selAchat = new ISQLCompteSelector();
593
        c.fill = GridBagConstraints.BOTH;
594
        panel.add(new JLabel(getLabelFor("ID_COMPTE_PCE_ACHAT")), c);
595
        c.gridx++;
596
        c.weightx = 1;
597
        panel.add(selAchat, c);
598
        this.addView(selAchat, "ID_COMPTE_PCE_ACHAT");
599
 
600
        c.gridy++;
601
        c.weighty = 1;
602
        c.weightx = 1;
603
        c.fill = GridBagConstraints.BOTH;
604
        final JPanel spacer = new JPanel();
605
        spacer.setOpaque(false);
606
        panel.add(spacer, c);
607
        return panel;
608
    }
609
 
610
    private SQLRowValues rowValuesDefaultCodeFournisseur;
611
    private CodeFournisseurItemTable codeFournisseurTable;
612
 
19 ilm 613
    private Component createAchatPanel() {
614
        JPanel panel = new JPanel(new GridBagLayout());
615
        panel.setOpaque(false);
616
        GridBagConstraints c = new DefaultGridBagConstraints();
617
        // Fournisseur
618
        c.gridy++;
619
        c.gridx = 0;
620
        c.gridwidth = 1;
621
        c.weightx = 0;
622
        JLabel labelFournisseur = new JLabel(getLabelFor("ID_FOURNISSEUR"));
623
        labelFournisseur.setHorizontalAlignment(SwingConstants.RIGHT);
624
        panel.add(labelFournisseur, c);
625
        c.gridx++;
626
        c.weightx = 1;
627
        c.gridwidth = 2;
628
        final ElementComboBox comboSelFournisseur = new ElementComboBox(false, 25);
629
        panel.add(comboSelFournisseur, c);
630
        this.addView(comboSelFournisseur, "ID_FOURNISSEUR");
631
 
67 ilm 632
        SQLPreferences prefs = new SQLPreferences(ComptaPropsConfiguration.getInstanceCompta().getRootSociete());
633
        final boolean supplierCode = prefs.getBoolean(GestionArticleGlobalPreferencePanel.SUPPLIER_PRODUCT_CODE, false);
634
 
635
        if (getTable().getSchema().contains("CODE_FOURNISSEUR") && supplierCode) {
636
            this.rowValuesDefaultCodeFournisseur = new SQLRowValues(getTable().getTable("CODE_FOURNISSEUR"));
637
            this.codeFournisseurTable = new CodeFournisseurItemTable(this.rowValuesDefaultCodeFournisseur);
638
            c.gridy++;
639
            c.gridx = 0;
640
            c.gridwidth = 3;
641
            c.weighty = 1;
642
            c.weightx = 1;
643
            c.fill = GridBagConstraints.BOTH;
644
            panel.add(this.codeFournisseurTable, c);
645
            comboSelFournisseur.addValueListener(new PropertyChangeListener() {
646
 
647
                @Override
648
                public void propertyChange(PropertyChangeEvent evt) {
649
                    rowValuesDefaultCodeFournisseur.put("ID_FOURNISSEUR", comboSelFournisseur.getSelectedId());
650
                }
651
            });
652
        } else {
653
            c.gridy++;
654
            c.weighty = 1;
655
            c.weightx = 1;
656
            c.fill = GridBagConstraints.BOTH;
657
            final JPanel spacer = new JPanel();
658
            spacer.setOpaque(false);
659
            panel.add(spacer, c);
660
        }
19 ilm 661
        return panel;
662
    }
663
 
664
    private JPanel createExportationPanel() {
665
        JPanel panel = new JPanel(new GridBagLayout());
666
        panel.setOpaque(false);
667
        GridBagConstraints c = new DefaultGridBagConstraints();
668
        // Code douanier
669
        c.gridx = 0;
670
        c.weightx = 0;
671
        c.gridy++;
672
        c.gridwidth = 1;
673
        JLabel labelCodeD = new JLabel(getLabelFor("CODE_DOUANIER"));
674
        labelCodeD.setHorizontalAlignment(SwingConstants.RIGHT);
675
        panel.add(labelCodeD, c);
676
 
677
        c.gridx++;
678
        JTextField fieldCodeDouanier = new JTextField();
679
        c.weightx = 1;
680
        panel.add(fieldCodeDouanier, c);
681
        this.addView(fieldCodeDouanier, "CODE_DOUANIER");
682
 
683
        // Pays d'origine
684
        c.gridx++;
685
        c.weightx = 0;
686
        JLabel labelPays = new JLabel(getLabelFor("ID_PAYS"));
687
        labelPays.setHorizontalAlignment(SwingConstants.RIGHT);
688
        panel.add(labelPays, c);
689
        c.gridx++;
690
        c.weightx = 1;
691
        final ElementComboBox comboSelPays = new ElementComboBox(false);
692
        panel.add(comboSelPays, c);
693
        this.addView(comboSelPays, "ID_PAYS");
694
        c.gridy++;
695
        c.weighty = 1;
696
        c.weightx = 1;
697
        c.fill = GridBagConstraints.BOTH;
698
        final JPanel spacer = new JPanel();
699
        spacer.setOpaque(false);
700
        panel.add(spacer, c);
701
        return panel;
702
    }
703
 
704
    private JPanel createTarifPanel() {
705
        JPanel panel = new JPanel(new GridBagLayout());
706
        panel.setOpaque(false);
707
        GridBagConstraints c = new DefaultGridBagConstraints();
708
 
709
        // Ajout tarif
710
        c.gridwidth = 1;
711
        c.weightx = 0;
712
        c.gridy++;
713
        c.gridx = 0;
714
        panel.add(new JLabel("Ajouter le tarif "), c);
715
 
716
        final ElementComboBox boxTarif = new ElementComboBox();
717
        boxTarif.init(Configuration.getInstance().getDirectory().getElement("TARIF"));
718
 
719
        c.gridx++;
720
        panel.add(boxTarif, c);
721
 
722
        c.fill = GridBagConstraints.NONE;
723
        c.gridx++;
724
        JButton buttonAjouter = new JButton("Ajouter");
725
        buttonAjouter.setOpaque(false);
726
        panel.add(buttonAjouter, c);
727
        c.gridx++;
728
        JButton buttonSupprimer = new JButton("Supprimer");
729
        buttonSupprimer.setOpaque(false);
730
        panel.add(buttonSupprimer, c);
731
 
732
        c.gridwidth = GridBagConstraints.REMAINDER;
733
        c.fill = GridBagConstraints.BOTH;
734
        c.weightx = 1;
735
        c.weighty = 1;
736
        c.gridy++;
737
        c.gridx = 0;
738
        c.fill = GridBagConstraints.BOTH;
739
        this.tableTarifVente.setOpaque(false);
740
        panel.add(this.tableTarifVente, c);
741
 
742
        // Listeners
743
        buttonAjouter.addActionListener(new ActionListener() {
744
 
745
            @Override
746
            public void actionPerformed(ActionEvent e) {
747
 
748
                SQLRow rowTarif = boxTarif.getSelectedRow();
749
                if (rowTarif == null || rowTarif.isUndefined()) {
750
                    return;
751
                }
752
                int nbRows = tableTarifVente.getModel().getRowCount();
753
 
754
                for (int i = 0; i < nbRows; i++) {
755
                    SQLRowValues rowVals = tableTarifVente.getModel().getRowValuesAt(i);
756
                    int idTarif = Integer.parseInt(rowVals.getObject("ID_TARIF").toString());
757
                    if (idTarif == rowTarif.getID()) {
758
                        JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLe tarif est déjà présent dans la liste!");
759
                        return;
760
                    }
761
                }
762
 
763
                SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("ARTICLE_TARIF"));
764
                if (getSelectedID() > 1) {
765
                    rowVals.put("ID_ARTICLE", getSelectedID());
766
                }
767
                rowVals.put("ID_TARIF", rowTarif.getID());
768
                rowVals.put("ID_DEVISE", rowTarif.getInt("ID_DEVISE"));
769
                rowVals.put("ID_TAXE", rowTarif.getInt("ID_TAXE"));
67 ilm 770
                rowVals.put("PRIX_METRIQUE_VT_1", BigDecimal.ZERO);
771
                rowVals.put("PV_HT", BigDecimal.ZERO);
772
                rowVals.put("PV_TTC", BigDecimal.ZERO);
19 ilm 773
                tableTarifVente.getModel().addRow(rowVals);
774
            }
775
        });
776
        buttonSupprimer.addActionListener(new ActionListener() {
777
 
778
            @Override
779
            public void actionPerformed(ActionEvent e) {
780
                tableTarifVente.removeSelectedRow();
781
            }
782
        });
783
        return panel;
784
    }
785
 
18 ilm 786
    protected void getMontantPanel(final GridBagConstraints c, DefaultProps props) {
80 ilm 787
        c.gridx = 0;
788
        c.gridy++;
789
        c.gridwidth = 1;
19 ilm 790
        // PA devise
67 ilm 791
        JPanel pDevise = new JPanel(new GridBagLayout());
792
        GridBagConstraints cDevise = new DefaultGridBagConstraints();
80 ilm 793
        cDevise.insets = new Insets(0, 0, 0, 4);
794
        c.weightx = 0;
795
        c.fill = GridBagConstraints.HORIZONTAL;
796
        this.add(new JLabel("Devise du fournisseur"), c);
67 ilm 797
        final ElementComboBox boxDevise = new ElementComboBox(true, 15);
798
        cDevise.gridx++;
799
        cDevise.weightx = 1;
800
        pDevise.add(boxDevise, cDevise);
19 ilm 801
        this.addView(boxDevise, "ID_DEVISE_HA");
67 ilm 802
        DefaultGridBagConstraints.lockMinimumSize(boxDevise);
19 ilm 803
 
67 ilm 804
        cDevise.weightx = 0;
805
        cDevise.gridx++;
806
        pDevise.add(new JLabel("Prix d'achat devise"), cDevise);
807
        final JTextField fieldHAD = new JTextField(15);
808
        cDevise.weightx = 1;
809
        cDevise.gridx++;
810
        pDevise.add(fieldHAD, cDevise);
19 ilm 811
        this.addView(fieldHAD, "PA_DEVISE");
67 ilm 812
        DefaultGridBagConstraints.lockMinimumSize(fieldHAD);
813
 
80 ilm 814
        c.gridx++;
19 ilm 815
        c.gridwidth = GridBagConstraints.REMAINDER;
816
        c.anchor = GridBagConstraints.WEST;
817
        c.weightx = 1;
818
        c.fill = GridBagConstraints.NONE;
819
        this.add(pDevise, c);
820
        fieldHAD.getDocument().addDocumentListener(new SimpleDocumentListener() {
821
 
822
            @Override
823
            public void update(DocumentEvent e) {
824
 
83 ilm 825
                if (!isFilling() && boxDevise != null && boxDevise.getSelectedRow() != null && !boxDevise.getSelectedRow().isUndefined()) {
826
                    BigDecimal ha = StringUtils.getBigDecimalFromUserText(fieldHAD.getText());
827
                    if (ha == null) {
828
                        ha = BigDecimal.ZERO;
19 ilm 829
                    }
83 ilm 830
                    final BigDecimal taux = (BigDecimal) boxDevise.getSelectedRow().getObject("TAUX");
831
                    textPAHT.setText(taux.multiply(ha, MathContext.DECIMAL128).setScale(getTable().getField("PA_DEVISE").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
67 ilm 832
 
19 ilm 833
                }
834
            }
835
        });
836
 
18 ilm 837
        // PA
67 ilm 838
        JPanel p = new JPanel(new GridBagLayout());
839
        GridBagConstraints cAchat = new DefaultGridBagConstraints();
80 ilm 840
        cAchat.insets = new Insets(0, 0, 0, 4);
841
        c.gridx = 0;
842
        c.gridy++;
843
        c.weightx = 0;
844
        c.gridwidth = 1;
845
        c.fill = GridBagConstraints.HORIZONTAL;
846
        this.add(new JLabel(getLabelFor("PA_HT"), SwingConstants.RIGHT), c);
67 ilm 847
        cAchat.gridx++;
848
        cAchat.weightx = 1;
849
        p.add(this.textPAHT, cAchat);
18 ilm 850
        this.textPAHT.getDocument().addDocumentListener(this.listenerMargeTextHA);
851
 
852
        // Marge
67 ilm 853
        cAchat.gridx++;
854
        cAchat.weightx = 0;
855
        p.add(new JLabel("Marge"), cAchat);
856
        cAchat.weightx = 1;
857
        cAchat.gridx++;
858
        p.add(this.textMarge, cAchat);
18 ilm 859
        this.textMarge.getDocument().addDocumentListener(this.listenerMargeTextMarge);
67 ilm 860
        cAchat.gridx++;
861
        cAchat.weightx = 0;
862
        p.add(new JLabel("%           "), cAchat);
18 ilm 863
 
864
        // Poids
865
        JLabel labelPds = new JLabel(getLabelFor("POIDS"));
67 ilm 866
        cAchat.gridx++;
867
        cAchat.weightx = 0;
868
        p.add(labelPds, cAchat);
18 ilm 869
        labelPds.setHorizontalAlignment(SwingConstants.RIGHT);
67 ilm 870
        cAchat.weightx = 1;
871
        cAchat.gridx++;
872
        p.add(this.textPoids, cAchat);
873
        DefaultGridBagConstraints.lockMinimumSize(this.textPoids);
18 ilm 874
 
875
        // Service
876
        String sService = props.getStringProperty("ArticleService");
877
        Boolean bService = Boolean.valueOf(sService);
878
        if (bService != null && bService.booleanValue()) {
67 ilm 879
            cAchat.gridx++;
880
            cAchat.weightx = 0;
881
            p.add(this.boxService, cAchat);
18 ilm 882
        }
883
 
80 ilm 884
        c.gridx++;
18 ilm 885
        c.gridwidth = GridBagConstraints.REMAINDER;
886
        c.anchor = GridBagConstraints.WEST;
887
        c.weightx = 1;
888
        c.fill = GridBagConstraints.NONE;
889
 
890
        this.add(p, c);
891
 
892
        // PV HT
80 ilm 893
        c.gridx = 0;
894
        c.gridy++;
895
 
67 ilm 896
        JPanel p2 = new JPanel(new GridBagLayout());
897
        GridBagConstraints cVT = new DefaultGridBagConstraints();
80 ilm 898
        cVT.insets = new Insets(0, 0, 0, 4);
899
 
900
        c.weightx = 0;
901
        c.fill = GridBagConstraints.HORIZONTAL;
902
        c.gridwidth = 1;
903
        this.add(new JLabel(getLabelFor("PV_HT"), SwingConstants.RIGHT), c);
67 ilm 904
        cVT.gridx++;
905
        cVT.weightx = 1;
80 ilm 906
 
67 ilm 907
        p2.add(this.textPVHT, cVT);
18 ilm 908
 
909
        // Taxe
910
        JLabel labelTaxe = new JLabel(getLabelFor("ID_TAXE"));
67 ilm 911
        cVT.gridx++;
912
        cVT.weightx = 0;
913
        p2.add(labelTaxe, cVT);
18 ilm 914
        labelTaxe.setHorizontalAlignment(SwingConstants.RIGHT);
67 ilm 915
        cVT.gridx++;
916
        // cVT.weightx = 1;
18 ilm 917
 
67 ilm 918
        p2.add(this.comboSelTaxe, cVT);
18 ilm 919
 
920
        // PV_TTC
67 ilm 921
        cVT.gridx++;
922
        cVT.weightx = 0;
923
        p2.add(new JLabel(getLabelFor("PV_TTC")), cVT);
924
        cVT.gridx++;
925
        cVT.weightx = 1;
926
        p2.add(this.textPVTTC, cVT);
80 ilm 927
        c.gridx = 1;
928
 
18 ilm 929
        c.gridwidth = GridBagConstraints.REMAINDER;
930
        c.anchor = GridBagConstraints.WEST;
931
        c.weightx = 1;
932
        c.fill = GridBagConstraints.NONE;
933
        this.add(p2, c);
934
 
935
        this.addRequiredSQLObject(this.textPAHT, "PA_HT");
936
        this.addRequiredSQLObject(this.textPVHT, "PV_HT");
67 ilm 937
        DefaultGridBagConstraints.lockMinimumSize(this.textPVHT);
18 ilm 938
        this.addRequiredSQLObject(this.comboSelTaxe, "ID_TAXE");
67 ilm 939
        DefaultGridBagConstraints.lockMinimumSize(this.comboSelTaxe);
80 ilm 940
        DefaultGridBagConstraints.lockMaximumSize(this.comboSelTaxe);
18 ilm 941
        this.addRequiredSQLObject(this.textPVTTC, "PV_TTC");
67 ilm 942
        DefaultGridBagConstraints.lockMinimumSize(this.textPVTTC);
943
        DefaultGridBagConstraints.lockMinimumSize(this.textPAHT);
944
        DefaultGridBagConstraints.lockMinimumSize(this.textMarge);
18 ilm 945
        this.ttcDocListener = new DocumentListener() {
946
            public void changedUpdate(DocumentEvent e) {
947
                setTextHT();
948
            }
949
 
950
            public void insertUpdate(DocumentEvent e) {
951
                setTextHT();
952
            }
953
 
954
            public void removeUpdate(DocumentEvent e) {
955
                setTextHT();
956
            }
957
        };
958
 
959
        this.htDocListener = new DocumentListener() {
960
            public void changedUpdate(DocumentEvent e) {
961
                setTextTTC();
962
            }
963
 
964
            public void insertUpdate(DocumentEvent e) {
965
                setTextTTC();
966
            }
967
 
968
            public void removeUpdate(DocumentEvent e) {
969
                setTextTTC();
970
            }
971
 
972
        };
973
 
974
        this.detailsListener = new SimpleDocumentListener() {
975
 
976
            @Override
977
            public void update(DocumentEvent e) {
978
                updatePiece();
979
            }
980
 
981
        };
982
 
983
        this.taxeListener = new PropertyChangeListener() {
984
 
985
            public void propertyChange(PropertyChangeEvent evt) {
986
                if (ReferenceArticleSQLComponent.this.textPVHT.getText().trim().length() > 0) {
987
                    setTextTTC();
988
                } else {
989
                    setTextHT();
990
                }
19 ilm 991
                tableTarifVente.fireModification();
18 ilm 992
            }
993
        };
994
        this.textPVHT.getDocument().addDocumentListener(this.htDocListener);
995
        this.textPVTTC.getDocument().addDocumentListener(this.ttcDocListener);
996
        this.comboSelTaxe.addValueListener(this.taxeListener);
997
 
998
        this.textMetrique1HA.getDocument().addDocumentListener(this.detailsListener);
999
        this.textMetrique1VT.getDocument().addDocumentListener(this.detailsListener);
1000
 
1001
        this.textValMetrique1.getDocument().addDocumentListener(this.detailsListener);
1002
        this.textValMetrique2.getDocument().addDocumentListener(this.detailsListener);
1003
        this.textValMetrique3.getDocument().addDocumentListener(this.detailsListener);
1004
 
1005
    }
1006
 
1007
    private void setListenerModeVenteActive(boolean b) {
1008
        if (b) {
1009
            this.comboSelModeVente.addValueListener(this.propertyChangeListener);
1010
        } else {
1011
            this.comboSelModeVente.removePropertyChangeListener(this.propertyChangeListener);
1012
        }
1013
    }
1014
 
1015
    /**
1016
     * @param c
1017
     * @param props
1018
     */
1019
    private void addModeVenteAvance(GridBagConstraints c) {
1020
        DefaultProps props = DefaultNXProps.getInstance();
1021
        JSeparator sep = new JSeparator();
65 ilm 1022
        JLabel labelDetails = new JLabel("Article détaillé", SwingConstants.RIGHT);
18 ilm 1023
        c.gridx = 0;
1024
        c.gridy++;
1025
        c.weightx = 0;
1026
        this.add(labelDetails, c);
1027
        c.gridx++;
1028
        c.gridwidth = GridBagConstraints.REMAINDER;
1029
        c.fill = GridBagConstraints.HORIZONTAL;
1030
        c.weightx = 1;
1031
        this.add(sep, c);
1032
 
1033
        // Mode de vente
1034
        c.gridwidth = 1;
1035
        c.weightx = 0;
1036
        c.gridx = 0;
1037
        c.gridy++;
65 ilm 1038
        this.add(new JLabel(getLabelFor("ID_MODE_VENTE_ARTICLE"), SwingConstants.RIGHT), c);
1039
        c.weightx = 1;
18 ilm 1040
        c.gridx++;
1041
        this.add(this.comboSelModeVente, c);
1042
 
1043
        // Prix metrique
1044
        c.gridx = 0;
65 ilm 1045
        c.weightx = 0;
18 ilm 1046
        c.gridy++;
1047
        this.add(this.labelMetriqueHA1, c);
1048
        c.gridx++;
1049
        c.weightx = 1;
1050
        this.add(this.textMetrique1HA, c);
1051
 
1052
        c.gridx++;
1053
        c.weightx = 0;
1054
        this.add(this.labelMetriqueVT1, c);
1055
        c.gridx++;
1056
        c.weightx = 1;
1057
        this.add(this.textMetrique1VT, c);
1058
 
1059
        // Metrique 1
1060
        c.weightx = 0;
65 ilm 1061
        JLabel labelMetrique1 = new JLabel(getLabelFor("VALEUR_METRIQUE_1"), SwingConstants.RIGHT);
18 ilm 1062
        c.gridx = 0;
1063
        c.gridy++;
1064
        this.add(labelMetrique1, c);
1065
        c.gridx++;
1066
        c.weightx = 1;
1067
        this.add(this.textValMetrique1, c);
1068
        c.gridx++;
1069
        c.weightx = 0;
1070
 
1071
        Boolean bMetrique1 = Boolean.valueOf(props.getStringProperty("ArticleLongueur"));
1072
        labelMetrique1.setVisible(bMetrique1 == null || bMetrique1.booleanValue());
1073
        this.textValMetrique1.setVisible(bMetrique1 == null || bMetrique1.booleanValue());
1074
 
1075
        // Metrique 2
65 ilm 1076
        JLabel labelMetrique2 = new JLabel(getLabelFor("VALEUR_METRIQUE_2"), SwingConstants.RIGHT);
18 ilm 1077
        c.gridx = 0;
1078
        c.gridy++;
1079
        c.weightx = 0;
1080
        this.add(labelMetrique2, c);
1081
        c.gridx++;
1082
        c.weightx = 1;
1083
        this.add(this.textValMetrique2, c);
1084
        c.gridx++;
1085
        c.weightx = 0;
1086
 
1087
        Boolean bMetrique2 = Boolean.valueOf(props.getStringProperty("ArticleLargeur"));
1088
        labelMetrique2.setVisible(bMetrique2 == null || bMetrique2.booleanValue());
1089
        this.textValMetrique2.setVisible(bMetrique2 == null || bMetrique2.booleanValue());
1090
 
1091
        // Metrique 3
65 ilm 1092
        JLabel labelMetrique3 = new JLabel(getLabelFor("VALEUR_METRIQUE_3"), SwingConstants.RIGHT);
18 ilm 1093
        c.gridx = 0;
1094
        c.gridy++;
1095
        c.weightx = 0;
1096
        this.add(labelMetrique3, c);
1097
        c.gridx++;
1098
        c.weightx = 1;
1099
        this.add(this.textValMetrique3, c);
1100
        c.gridx++;
1101
 
1102
        Boolean bMetrique3 = Boolean.valueOf(props.getStringProperty("ArticlePoids"));
1103
        labelMetrique3.setVisible(bMetrique3 == null || bMetrique3.booleanValue());
1104
        this.textValMetrique3.setVisible(bMetrique3 == null || bMetrique3.booleanValue());
1105
 
1106
        // Article détaillé
1107
        JSeparator sep2 = new JSeparator();
65 ilm 1108
        JLabel labelPiece = new JLabel("Article pièce", SwingConstants.RIGHT);
18 ilm 1109
        c.gridx = 0;
1110
        c.gridy++;
1111
        c.weightx = 0;
1112
        this.add(labelPiece, c);
1113
        c.gridx++;
1114
        c.gridwidth = GridBagConstraints.REMAINDER;
1115
        c.fill = GridBagConstraints.HORIZONTAL;
1116
        c.weightx = 1;
1117
        this.add(sep2, c);
1118
 
1119
    }
1120
 
19 ilm 1121
    @Override
1122
    public void update() {
1123
            super.update();
1124
        this.tableTarifVente.updateField("ID_ARTICLE", getSelectedID());
1125
        this.tableDes.updateField("ID_ARTICLE", getSelectedID());
67 ilm 1126
        if (this.codeFournisseurTable != null) {
1127
            this.codeFournisseurTable.updateField("ID_ARTICLE", getSelectedID());
1128
        }
19 ilm 1129
    }
18 ilm 1130
 
1131
    /**
1132
     * Sélection d'un mode de vente pour l'article. Affiche les prix metriques requis et fixe les
1133
     * valeurs.
1134
     *
1135
     * @param id id du mode de vente
1136
     */
1137
    private void selectModeVente(int id) {
1138
 
1139
        this.labelMetriqueHA1.setEnabled(true);
1140
        this.labelMetriqueVT1.setEnabled(true);
1141
        this.textMetrique1HA.setEnabled(true);
1142
        this.textMetrique1VT.setEnabled(true);
1143
 
1144
        this.textPAHT.getDocument().removeDocumentListener(this.pieceHAArticle);
1145
        this.textPVHT.getDocument().removeDocumentListener(this.pieceVTArticle);
1146
 
1147
        switch (id) {
1148
        case ReferenceArticleSQLElement.AU_METRE_CARRE:
1149
            this.labelMetriqueHA1.setText("Prix d'achat HT au mètre carré");
1150
            this.labelMetriqueVT1.setText("Prix de vente HT au mètre carré");
1151
            break;
1152
        case ReferenceArticleSQLElement.AU_METRE_LARGEUR:
1153
        case ReferenceArticleSQLElement.AU_METRE_LONGUEUR:
1154
            this.labelMetriqueHA1.setText("Prix d'achat HT au mètre");
1155
            this.labelMetriqueVT1.setText("Prix de vente HT au mètre");
1156
            break;
1157
 
1158
        case ReferenceArticleSQLElement.AU_POID_METRECARRE:
1159
            this.labelMetriqueHA1.setText("Prix d'achat HT au kilo");
1160
            this.labelMetriqueVT1.setText("Prix de vente HT au kilo");
1161
            break;
1162
        case -1:
73 ilm 1163
            // No break need to enable the listener
18 ilm 1164
        default:
1165
            this.labelMetriqueHA1.setEnabled(false);
1166
            this.labelMetriqueVT1.setEnabled(false);
1167
            this.textMetrique1HA.setEnabled(false);
1168
            this.textMetrique1VT.setEnabled(false);
1169
 
67 ilm 1170
            this.textMetrique1HA.setText(this.textPAHT.getText().trim());
1171
            this.textMetrique1VT.setText(this.textPVHT.getText().trim());
18 ilm 1172
            this.textPAHT.getDocument().addDocumentListener(this.pieceHAArticle);
1173
            this.textPVHT.getDocument().addDocumentListener(this.pieceVTArticle);
1174
            break;
1175
        }
19 ilm 1176
        this.tableTarifVente.fireModification();
1177
    }
18 ilm 1178
 
19 ilm 1179
    @Override
1180
    public int insert(SQLRow order) {
1181
        int id = super.insert(order);
1182
        this.tableTarifVente.updateField("ID_ARTICLE", id);
1183
        this.tableDes.updateField("ID_ARTICLE", id);
67 ilm 1184
        if (this.codeFournisseurTable != null) {
1185
            this.codeFournisseurTable.updateField("ID_ARTICLE", id);
1186
        }
19 ilm 1187
        return id;
18 ilm 1188
    }
1189
 
1190
    @Override
1191
    protected SQLRowValues createDefaults() {
1192
        SQLRowValues rowVals = new SQLRowValues(getTable());
1193
 
1194
        SQLRow row = getTable().getRow(getTable().getUndefinedID());
1195
 
1196
        rowVals.put("ID_TAXE", row.getInt("ID_TAXE"));
61 ilm 1197
        rowVals.put("ID_UNITE_VENTE", UniteVenteArticleSQLElement.A_LA_PIECE);
18 ilm 1198
        rowVals.put("ID_MODE_VENTE_ARTICLE", ReferenceArticleSQLElement.A_LA_PIECE);
1199
        selectModeVente(ReferenceArticleSQLElement.A_LA_PIECE);
1200
        rowVals.put("VALEUR_METRIQUE_1", Float.valueOf("1.0"));
67 ilm 1201
        rowVals.put("PA_HT", BigDecimal.ZERO);
18 ilm 1202
        rowVals.put("POIDS", Float.valueOf(0));
19 ilm 1203
 
18 ilm 1204
        return rowVals;
1205
    }
1206
 
1207
    private void setTextHT() {
1208
        this.textPVHT.getDocument().removeDocumentListener(this.htDocListener);
83 ilm 1209
        final BigDecimal ttc = StringUtils.getBigDecimalFromUserText(this.textPVTTC.getText());
1210
        if (ttc != null) {
67 ilm 1211
            int id = this.comboSelTaxe.getSelectedId();
1212
            if (id > 1) {
1213
                Float resultTaux = TaxeCache.getCache().getTauxFromId(id);
1214
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue() / 100.0F;
1215
                this.textPVHT.setText(ttc.divide(BigDecimal.valueOf(taux).add(BigDecimal.ONE), MathContext.DECIMAL128)
1216
                        .setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1217
            }
18 ilm 1218
        }
1219
        this.textPVHT.getDocument().addDocumentListener(this.htDocListener);
1220
    }
1221
 
1222
    private void setTextTTC() {
1223
        this.textPVTTC.getDocument().removeDocumentListener(this.ttcDocListener);
83 ilm 1224
        final BigDecimal ht = StringUtils.getBigDecimalFromUserText(this.textPVHT.getText());
1225
        if (ht != null) {
67 ilm 1226
            int id = this.comboSelTaxe.getSelectedId();
1227
            if (id > 1) {
83 ilm 1228
                final Float resultTaux = TaxeCache.getCache().getTauxFromId(id);
1229
                final float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue() / 100.0F;
67 ilm 1230
                this.textPVTTC.setText(ht.multiply(BigDecimal.valueOf(taux).add(BigDecimal.ONE), MathContext.DECIMAL128)
1231
                        .setScale(getTable().getField("PV_TTC").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1232
            }
18 ilm 1233
        }
1234
        this.textPVTTC.getDocument().addDocumentListener(this.ttcDocListener);
1235
    }
1236
 
1237
    /**
1238
     * calcul du prix achat et vente ainsi que le poids total pour la piece
1239
     */
1240
    private void updatePiece() {
1241
        if (this.comboSelModeVente.getSelectedId() > 1 && this.comboSelModeVente.getSelectedId() != ReferenceArticleSQLElement.A_LA_PIECE) {
1242
            SQLRowValues rowVals = getDetailsRowValues();
1243
            float poidsTot = ReferenceArticleSQLElement.getPoidsFromDetails(rowVals);
1244
            this.textPoids.setText(String.valueOf(poidsTot));
67 ilm 1245
            this.textPAHT.setText(ReferenceArticleSQLElement.getPrixHAFromDetails(rowVals).setScale(getTable().getField("PA_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
1246
            this.textPVHT.setText(ReferenceArticleSQLElement.getPrixVTFromDetails(rowVals).setScale(getTable().getField("PV_HT").getType().getDecimalDigits(), RoundingMode.HALF_UP).toString());
19 ilm 1247
            this.tableTarifVente.fireModification();
18 ilm 1248
        }
1249
    }
1250
 
19 ilm 1251
    public int getSelectedTaxe() {
1252
        return this.comboSelTaxe.getSelectedId();
1253
    }
1254
 
1255
    public SQLRowValues getDetailsRowValues() {
73 ilm 1256
        final SQLRowValues rowVals = new SQLRowValues(getTable());
18 ilm 1257
 
83 ilm 1258
        BigDecimal pAchat = StringUtils.getBigDecimalFromUserText(this.textMetrique1HA.getText());
1259
        if (pAchat == null) {
1260
            pAchat = BigDecimal.ZERO;
1261
        }
1262
        rowVals.put("PRIX_METRIQUE_HA_1", pAchat);
67 ilm 1263
 
83 ilm 1264
        BigDecimal pVente = StringUtils.getBigDecimalFromUserText(this.textMetrique1VT.getText());
1265
        if (pVente == null) {
1266
            pVente = BigDecimal.ZERO;
1267
        }
1268
        rowVals.put("PRIX_METRIQUE_VT_1", pVente);
18 ilm 1269
        put(rowVals, this.textValMetrique1);
1270
        put(rowVals, this.textValMetrique2);
1271
        put(rowVals, this.textValMetrique3);
1272
        rowVals.put("ID_MODE_VENTE_ARTICLE", this.comboSelModeVente.getSelectedId());
1273
 
1274
        return rowVals;
1275
    }
1276
 
1277
    private void put(SQLRowValues rowVals, JTextField comp) {
1278
        Float f = (comp.getText() == null || comp.getText().trim().length() == 0) ? 0.0F : Float.valueOf(comp.getText());
1279
        rowVals.put(this.getView(comp).getField().getName(), f);
1280
    }
1281
 
1282
}