OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
80 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.supplychain.order.component;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
180 ilm 19
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
80 ilm 20
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
21
import org.openconcerto.erp.core.common.ui.DeviseField;
22
import org.openconcerto.erp.core.common.ui.TotalPanel;
23
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
132 ilm 24
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
177 ilm 25
import org.openconcerto.erp.core.finance.payment.component.ModeDeReglementSQLComponent;
80 ilm 26
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
27
import org.openconcerto.erp.core.supplychain.order.ui.FactureFournisseurItemTable;
94 ilm 28
import org.openconcerto.erp.generationDoc.gestcomm.FactureFournisseurXmlSheet;
80 ilm 29
import org.openconcerto.erp.generationEcritures.GenerationMvtFactureFournisseur;
94 ilm 30
import org.openconcerto.erp.panel.PanelOOSQLComponent;
80 ilm 31
import org.openconcerto.erp.preferences.DefaultNXProps;
32
import org.openconcerto.sql.Configuration;
33
import org.openconcerto.sql.element.ElementSQLObject;
34
import org.openconcerto.sql.element.SQLElement;
35
import org.openconcerto.sql.model.SQLBackgroundTableCache;
36
import org.openconcerto.sql.model.SQLRow;
93 ilm 37
import org.openconcerto.sql.model.SQLRowAccessor;
80 ilm 38
import org.openconcerto.sql.model.SQLRowValues;
156 ilm 39
import org.openconcerto.sql.model.SQLTable;
93 ilm 40
import org.openconcerto.sql.model.Where;
156 ilm 41
import org.openconcerto.sql.preferences.SQLPreferences;
93 ilm 42
import org.openconcerto.sql.request.ComboSQLRequest;
80 ilm 43
import org.openconcerto.sql.sqlobject.ElementComboBox;
180 ilm 44
import org.openconcerto.sql.sqlobject.JUniqueTextField;
45
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
80 ilm 46
import org.openconcerto.sql.users.UserManager;
47
import org.openconcerto.sql.view.list.RowValuesTable;
180 ilm 48
import org.openconcerto.sql.view.list.RowValuesTableModel;
80 ilm 49
import org.openconcerto.ui.AutoHideListener;
50
import org.openconcerto.ui.DefaultGridBagConstraints;
51
import org.openconcerto.ui.FormLayouter;
52
import org.openconcerto.ui.JDate;
53
import org.openconcerto.ui.component.ITextArea;
54
import org.openconcerto.ui.preferences.DefaultProps;
156 ilm 55
import org.openconcerto.utils.ExceptionHandler;
180 ilm 56
import org.openconcerto.utils.checks.ValidState;
94 ilm 57
import org.openconcerto.utils.text.SimpleDocumentListener;
80 ilm 58
 
132 ilm 59
import java.awt.Color;
60
import java.awt.GridBagConstraints;
61
import java.awt.GridBagLayout;
62
import java.beans.PropertyChangeEvent;
63
import java.beans.PropertyChangeListener;
64
import java.math.BigDecimal;
156 ilm 65
import java.sql.SQLException;
180 ilm 66
import java.util.Arrays;
67
import java.util.Date;
174 ilm 68
import java.util.HashSet;
69
import java.util.List;
70
import java.util.Set;
132 ilm 71
 
72
import javax.swing.JLabel;
177 ilm 73
import javax.swing.JOptionPane;
132 ilm 74
import javax.swing.JPanel;
75
import javax.swing.JScrollPane;
180 ilm 76
import javax.swing.JTabbedPane;
132 ilm 77
import javax.swing.JTextField;
78
import javax.swing.SwingConstants;
174 ilm 79
import javax.swing.SwingUtilities;
132 ilm 80
import javax.swing.event.DocumentEvent;
81
import javax.swing.event.DocumentListener;
82
import javax.swing.event.TableModelEvent;
83
import javax.swing.event.TableModelListener;
84
 
80 ilm 85
public class FactureFournisseurSQLComponent extends TransfertBaseSQLComponent {
86
 
87
    private FactureFournisseurItemTable table = new FactureFournisseurItemTable();
88
    private final ITextArea infos = new ITextArea(3, 3);
89
    private ElementComboBox fourn = new ElementComboBox();
90
    private ElementComboBox comptePCE = new ElementComboBox();
156 ilm 91
    private ElementComboBox avoirFourn = new ElementComboBox();
180 ilm 92
    private ElementComboBox avoirFourn2 = new ElementComboBox();
93
 
94 ilm 94
    private PanelOOSQLComponent panelOO;
80 ilm 95
    final JPanel panelAdrSpec = new JPanel(new GridBagLayout());
93 ilm 96
    private JDate dateCommande = new JDate();
132 ilm 97
    private final JTextField fieldTaux = new JTextField(15);
156 ilm 98
    private final DeviseField textNetAPayer = new DeviseField(15);
132 ilm 99
    private final JLabel labelTaux = new JLabel();
142 ilm 100
    private ElementSQLObject eltModeRegl;
156 ilm 101
    DeviseField fieldTTC = new DeviseField();
102
    DeviseField avoirTTC = new DeviseField();
142 ilm 103
    private PropertyChangeListener listenerModeReglDefaut = new PropertyChangeListener() {
80 ilm 104
 
142 ilm 105
        public void propertyChange(PropertyChangeEvent evt) {
106
 
180 ilm 107
            final SQLRow rowFourn = FactureFournisseurSQLComponent.this.fourn.getRequest().getPrimaryTable().getRow(FactureFournisseurSQLComponent.this.fourn.getWantedID());
108
            if ((getMode() == Mode.INSERTION || !isFilling()) && rowFourn != null && !rowFourn.isUndefined()) {
142 ilm 109
 
110
                int idModeRegl = rowFourn.getInt("ID_MODE_REGLEMENT");
111
                if (idModeRegl > 1 && FactureFournisseurSQLComponent.this.eltModeRegl != null && getMode() == Mode.INSERTION) {
174 ilm 112
                    SQLElement sqlEltModeRegl = getElement().getDirectory().getElement("MODE_REGLEMENT");
142 ilm 113
                    SQLRow rowModeRegl = sqlEltModeRegl.getTable().getRow(idModeRegl);
114
                    SQLRowValues rowVals = rowModeRegl.createUpdateRow();
115
                    rowVals.clearPrimaryKeys();
116
                    FactureFournisseurSQLComponent.this.eltModeRegl.setValue(rowVals);
117
                }
180 ilm 118
 
142 ilm 119
            }
120
 
121
        }
122
 
123
    };
124
 
80 ilm 125
    public FactureFournisseurSQLComponent() {
126
        super(Configuration.getInstance().getDirectory().getElement("FACTURE_FOURNISSEUR"));
127
    }
128
 
129
    public ElementComboBox getBoxFournisseur() {
130
        return this.fourn;
131
    }
132
 
174 ilm 133
    @Override
134
    protected Set<String> createRequiredNames() {
135
        final Set<String> s = new HashSet<>(1);
136
        if (getTable().contains("ID_TYPE_CMD")) {
137
            s.add("ID_TYPE_CMD");
138
        }
139
        return s;
140
    }
141
 
180 ilm 142
    @Override
143
    public synchronized ValidState getValidState() {
144
        if (getTable().contains("ID_COMPTE_PCE") && getTable().contains("ID_TYPE_CMD") && getTable().getTable("FACTURE_FOURNISSEUR_ELEMENT").contains("ID_COMPTE_PCE")) {
145
            SQLRequestComboBox boxCpt = (SQLRequestComboBox) getView("ID_COMPTE_PCE").getComp();
146
            final SQLRow selectedCpt = boxCpt.getSelectedRow();
147
            if (selectedCpt == null || selectedCpt.isUndefined()) {
148
                RowValuesTableModel tableRow = this.table.getRowValuesTable().getRowValuesTableModel();
149
 
150
                for (int i = 0; i < tableRow.getRowCount(); i++) {
151
                    SQLRowValues rowVals = tableRow.getRowValuesAt(i);
152
                    if (rowVals.getInt("QTE") != 0 && (rowVals.getObject("ID_COMPTE_PCE") == null || rowVals.isForeignEmpty("ID_COMPTE_PCE"))) {
153
                        return ValidState.create(false, "Aucun compte global sélectionné et une ligne avec une quantité > 0 n'est pas affectée à un compte!");
154
                    }
155
                }
156
            }
157
 
158
        }
159
        return super.getValidState();
160
    }
161
 
162
    private JUniqueTextField numero;
163
 
80 ilm 164
    public void addViews() {
165
        this.setLayout(new GridBagLayout());
166
        final GridBagConstraints c = new DefaultGridBagConstraints();
167
 
180 ilm 168
        if (getTable().getTable("NUMEROTATION_AUTO").contains("FACTURE_FOURNISSEUR_START")) {
169
            this.numero = new JUniqueTextField(35) {
170
                @Override
171
                public String getAutoRefreshNumber() {
172
                    if (getMode() == Mode.INSERTION) {
173
                        final Date date = FactureFournisseurSQLComponent.this.dateCommande.getDate();
174
                        return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), date == null ? new Date() : date);
175
                    } else {
176
                        return null;
177
                    }
178
                }
179
            };
180
            this.dateCommande.addValueListener(new PropertyChangeListener() {
181
 
182
                @Override
183
                public void propertyChange(PropertyChangeEvent evt) {
184
                    if (!isFilling() && FactureFournisseurSQLComponent.this.dateCommande.getValue() != null) {
185
 
186
                        final String nextNumero = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), FactureFournisseurSQLComponent.this.dateCommande.getValue());
187
 
188
                        if (FactureFournisseurSQLComponent.this.numero.getText().trim().length() > 0 && !nextNumero.equalsIgnoreCase(FactureFournisseurSQLComponent.this.numero.getText())) {
189
 
190
                            int answer = JOptionPane.showConfirmDialog(FactureFournisseurSQLComponent.this, "Voulez vous actualiser le numéro de la facture?", "Changement du numéro de facture",
191
                                    JOptionPane.YES_NO_OPTION);
192
                            if (answer == JOptionPane.NO_OPTION) {
193
                                return;
194
                            }
195
                        }
196
 
197
                        FactureFournisseurSQLComponent.this.numero.setText(nextNumero);
198
 
199
                    }
200
                }
201
            });
202
        } else {
203
            this.numero = new JUniqueTextField(35);
204
        }
80 ilm 205
        // Numero du commande
206
        c.gridx = 0;
207
        c.weightx = 0;
208
        this.add(new JLabel(getLabelFor("NUMERO"), SwingConstants.RIGHT), c);
209
 
210
        c.gridx++;
211
        c.weightx = 1;
212
        c.fill = GridBagConstraints.NONE;
180 ilm 213
        DefaultGridBagConstraints.lockMinimumSize(this.numero);
214
        this.add(this.numero, c);
80 ilm 215
 
216
        // Date
217
        JLabel labelDate = new JLabel(getLabelFor("DATE"));
218
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
219
        c.gridx = 2;
220
        c.weightx = 0;
221
        c.fill = GridBagConstraints.HORIZONTAL;
222
        this.add(labelDate, c);
223
 
224
        c.gridx++;
225
        c.fill = GridBagConstraints.NONE;
180 ilm 226
        this.add(this.dateCommande, c);
80 ilm 227
 
93 ilm 228
        this.dateCommande.addValueListener(new PropertyChangeListener() {
229
 
230
            @Override
231
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 232
                if (!isFilling() && FactureFournisseurSQLComponent.this.dateCommande.getValue() != null) {
233
                    FactureFournisseurSQLComponent.this.table.setDateDevise(FactureFournisseurSQLComponent.this.dateCommande.getValue());
132 ilm 234
                    updateLabelTauxConversion();
93 ilm 235
                }
236
            }
237
        });
238
 
80 ilm 239
        // Fournisseur
240
        c.gridx = 0;
241
        c.gridy++;
242
        c.weightx = 0;
243
        c.fill = GridBagConstraints.HORIZONTAL;
244
        this.add(new JLabel(getLabelFor("ID_FOURNISSEUR"), SwingConstants.RIGHT), c);
245
 
246
        c.gridx = GridBagConstraints.RELATIVE;
247
        c.gridwidth = 1;
248
        c.weightx = 1;
249
        c.weighty = 0;
250
        c.fill = GridBagConstraints.NONE;
251
        this.add(this.fourn, c);
252
        addRequiredSQLObject(this.fourn, "ID_FOURNISSEUR");
253
 
180 ilm 254
        this.fourn.addModelListener("wantedID", new PropertyChangeListener() {
156 ilm 255
 
256
            @Override
257
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 258
                int wantedID = FactureFournisseurSQLComponent.this.fourn.getWantedID();
156 ilm 259
                if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
260
 
261
                    final SQLRow rowF = getTable().getForeignTable("ID_FOURNISSEUR").getRow(wantedID);
262
 
263
                    if (rowF.getObject("ID_CATEGORIE_COMPTABLE") != null && !rowF.isForeignEmpty("ID_CATEGORIE_COMPTABLE")) {
180 ilm 264
                        FactureFournisseurSQLComponent.this.table.setRowCatComptable(rowF.getForeign("ID_CATEGORIE_COMPTABLE"));
156 ilm 265
                    } else {
180 ilm 266
                        FactureFournisseurSQLComponent.this.table.setRowCatComptable(null);
156 ilm 267
                    }
268
 
269
                } else {
180 ilm 270
                    FactureFournisseurSQLComponent.this.table.setRowCatComptable(null);
156 ilm 271
                }
272
            }
273
        });
274
        this.fourn.addModelListener("wantedID", this.listenerModeReglDefaut);
275
 
80 ilm 276
        // Champ Module
277
        c.gridx = 0;
278
        c.gridy++;
279
        c.gridwidth = GridBagConstraints.REMAINDER;
280
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
281
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
282
        this.add(addP, c);
283
 
284
        c.gridy++;
285
        c.gridwidth = 1;
286
 
156 ilm 287
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
288
        final boolean showDevise = prefs.getBoolean(AbstractVenteArticleItemTable.ARTICLE_SHOW_DEVISE, false);
289
 
80 ilm 290
        final ElementComboBox boxDevise = new ElementComboBox();
156 ilm 291
        if (showDevise) {
80 ilm 292
            // Devise
293
            c.gridx = 0;
294
            c.gridy++;
295
            c.weightx = 0;
296
            c.fill = GridBagConstraints.HORIZONTAL;
297
            this.add(new JLabel(getLabelFor("ID_DEVISE"), SwingConstants.RIGHT), c);
298
 
94 ilm 299
            c.gridx++;
80 ilm 300
            c.gridwidth = 1;
301
            c.weightx = 1;
302
            c.weighty = 0;
303
            c.fill = GridBagConstraints.NONE;
304
            this.add(boxDevise, c);
305
            this.addView(boxDevise, "ID_DEVISE");
94 ilm 306
 
132 ilm 307
            this.fourn.addModelListener("wantedID", new PropertyChangeListener() {
308
 
309
                @Override
310
                public void propertyChange(PropertyChangeEvent evt) {
180 ilm 311
                    FactureFournisseurSQLComponent.this.table.setFournisseur(FactureFournisseurSQLComponent.this.fourn.getSelectedRow());
312
                    SQLRow row = FactureFournisseurSQLComponent.this.fourn.getSelectedRow();
132 ilm 313
                    if (!isFilling()) {
314
                        if (row != null && !row.isUndefined() && !row.isForeignEmpty("ID_DEVISE")) {
315
                            boxDevise.setValue(row.getForeignID("ID_DEVISE"));
316
                        }
317
                    }
156 ilm 318
 
319
                    if (row != null && !row.isUndefined()) {
180 ilm 320
                        FactureFournisseurSQLComponent.this.avoirFourn.getRequest()
321
                                .setWhere(new Where(FactureFournisseurSQLComponent.this.avoirFourn.getRequest().getPrimaryTable().getField("ID_FOURNISSEUR"), "=", row.getID()));
156 ilm 322
                    } else {
180 ilm 323
                        FactureFournisseurSQLComponent.this.avoirFourn.getRequest().setWhere(null);
156 ilm 324
                    }
132 ilm 325
                }
326
            });
327
 
94 ilm 328
            if (getTable().contains("TAUX_APPLIQUE")) {
329
                // Devise
330
                c.gridx++;
331
                c.weightx = 0;
332
                c.fill = GridBagConstraints.HORIZONTAL;
333
                this.add(new JLabel(getLabelFor("TAUX_APPLIQUE"), SwingConstants.RIGHT), c);
334
 
335
                c.gridx++;
336
                c.gridwidth = 1;
337
                c.weightx = 1;
338
                c.weighty = 0;
339
                c.fill = GridBagConstraints.NONE;
340
 
132 ilm 341
                DefaultGridBagConstraints.lockMinimumSize(this.fieldTaux);
342
                JPanel panelTaux = new JPanel();
343
                panelTaux.add(this.fieldTaux);
344
                panelTaux.add(this.labelTaux);
345
                this.add(panelTaux, c);
346
                this.addView(this.fieldTaux, "TAUX_APPLIQUE");
347
 
94 ilm 348
            }
80 ilm 349
        }
350
 
351
        // Compte pce
352
        c.gridx = 0;
353
        c.gridy++;
354
        c.weightx = 0;
355
        c.fill = GridBagConstraints.HORIZONTAL;
356
        this.add(new JLabel(getLabelFor("ID_COMPTE_PCE"), SwingConstants.RIGHT), c);
357
 
358
        c.gridx = GridBagConstraints.RELATIVE;
359
        c.gridwidth = 1;
360
        c.weightx = 1;
361
        c.weighty = 0;
362
        c.fill = GridBagConstraints.NONE;
93 ilm 363
 
80 ilm 364
        this.add(this.comptePCE, c);
93 ilm 365
 
366
        final SQLElement foreignElement = getElement().getForeignElement("ID_COMPTE_PCE");
367
        final ComboSQLRequest comboRequest = foreignElement.getComboRequest(true);
368
        comboRequest.setWhere(new Where(foreignElement.getTable().getField("NUMERO"), "LIKE", "6%"));
369
        this.comptePCE.init(foreignElement, comboRequest);
80 ilm 370
        this.addView(this.comptePCE, "ID_COMPTE_PCE");
371
 
372
        // Reference
373
        c.gridx = 0;
374
        c.gridy++;
375
        c.gridwidth = 1;
376
        c.weightx = 0;
377
        c.fill = GridBagConstraints.HORIZONTAL;
378
        c.anchor = GridBagConstraints.EAST;
379
        this.add(new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT), c);
380
 
381
        final JTextField textNom = new JTextField();
382
        c.gridx++;
383
        c.weightx = 1;
384
        this.add(textNom, c);
385
 
386
        String field;
387
            field = "ID_COMMERCIAL";
388
        // Commercial
389
        c.weightx = 0;
390
        c.gridx++;
391
        this.add(new JLabel(getLabelFor(field), SwingConstants.RIGHT), c);
392
 
393
        ElementComboBox commSel = new ElementComboBox(false, 25);
394
        c.gridx = GridBagConstraints.RELATIVE;
395
        c.gridwidth = 1;
396
        c.weightx = 1;
397
        c.weighty = 0;
398
        c.fill = GridBagConstraints.NONE;
399
        c.anchor = GridBagConstraints.WEST;
400
        this.add(commSel, c);
401
        addRequiredSQLObject(commSel, field);
402
 
403
        // Table d'élément
404
        c.fill = GridBagConstraints.BOTH;
405
        c.gridy++;
406
        c.gridx = 0;
407
        c.weightx = 0;
408
        c.weighty = 1;
409
        c.gridwidth = 4;
410
        this.add(this.table, c);
156 ilm 411
        if (showDevise) {
80 ilm 412
 
413
            boxDevise.addValueListener(new PropertyChangeListener() {
414
 
415
                @Override
416
                public void propertyChange(PropertyChangeEvent evt) {
180 ilm 417
                    FactureFournisseurSQLComponent.this.table.setDevise(boxDevise.getSelectedRow());
132 ilm 418
                    updateLabelTauxConversion();
80 ilm 419
 
420
                }
421
            });
94 ilm 422
 
180 ilm 423
            this.fieldTaux.getDocument().addDocumentListener(new SimpleDocumentListener() {
94 ilm 424
 
425
                @Override
426
                public void update(DocumentEvent e) {
427
                    BigDecimal tauxConversion = null;
180 ilm 428
                    if (FactureFournisseurSQLComponent.this.fieldTaux.getText().trim().length() > 0) {
429
                        tauxConversion = new BigDecimal(FactureFournisseurSQLComponent.this.fieldTaux.getText());
94 ilm 430
                    }
180 ilm 431
                    FactureFournisseurSQLComponent.this.table.setTauxConversion(tauxConversion);
132 ilm 432
                    updateLabelTauxConversion();
94 ilm 433
                }
434
            });
80 ilm 435
        }
436
 
437
        this.fourn.addModelListener("wantedID", new PropertyChangeListener() {
438
 
439
            @Override
440
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 441
                FactureFournisseurSQLComponent.this.table.setFournisseur(FactureFournisseurSQLComponent.this.fourn.getSelectedRow());
80 ilm 442
                if (!isFilling()) {
180 ilm 443
                    SQLRow row = FactureFournisseurSQLComponent.this.fourn.getSelectedRow();
156 ilm 444
                    if (row != null && !row.isUndefined()) {
445
                        row.fetchValues();
446
                        if (!row.isForeignEmpty("ID_COMPTE_PCE_CHARGE")) {
180 ilm 447
                            FactureFournisseurSQLComponent.this.comptePCE.setValue(row.getForeign("ID_COMPTE_PCE_CHARGE"));
156 ilm 448
                        }
80 ilm 449
                    }
450
                }
451
            }
452
        });
453
        // Bottom
454
        c.gridy++;
455
        c.weighty = 0;
456
        this.add(getBottomPanel(), c);
457
 
180 ilm 458
        ModeDeReglementSQLComponent modeReglComp = (ModeDeReglementSQLComponent) this.eltModeRegl.getSQLChild();
177 ilm 459
        modeReglComp.addDateCompListener(this.dateCommande);
460
 
180 ilm 461
        // Modèle, visualiser, imprimer..
80 ilm 462
        c.gridx = 0;
463
        c.gridy++;
94 ilm 464
        c.fill = GridBagConstraints.NONE;
465
        c.anchor = GridBagConstraints.SOUTHEAST;
80 ilm 466
        c.gridwidth = GridBagConstraints.REMAINDER;
467
 
94 ilm 468
        this.panelOO = new PanelOOSQLComponent(this);
469
        this.add(this.panelOO, c);
470
 
80 ilm 471
        addSQLObject(textNom, "NOM");
180 ilm 472
        addRequiredSQLObject(this.dateCommande, "DATE");
473
        addRequiredSQLObject(this.numero, "NUMERO");
80 ilm 474
        addSQLObject(this.infos, "INFOS");
475
 
476
        DefaultGridBagConstraints.lockMinimumSize(this.fourn);
477
        DefaultGridBagConstraints.lockMinimumSize(commSel);
478
    }
479
 
132 ilm 480
    private void updateLabelTauxConversion() {
481
        String result = "";
482
        if (this.fieldTaux.getText().trim().length() == 0 && this.table.getDevise() != null && !this.table.getDevise().isUndefined()) {
483
            CurrencyConverter converter = new CurrencyConverter();
484
            BigDecimal taux = converter.convert(BigDecimal.ONE, converter.getCompanyCurrencyCode(), this.table.getDevise().getString("CODE"), this.table.getDateDevise(), false);
485
            result = "(" + taux + ")";
486
        } else {
487
            result = "(" + this.fieldTaux.getText() + ")";
488
        }
489
        this.labelTaux.setText(result);
490
    }
491
 
80 ilm 492
    private JPanel getBottomPanel() {
493
        final JPanel panel = new JPanel(new GridBagLayout());
494
        final GridBagConstraints c = new DefaultGridBagConstraints();
495
 
180 ilm 496
        JTabbedPane tabs = new JTabbedPane();
497
 
498
        JPanel mainBottomPanel = new JPanel();
499
        mainBottomPanel.setOpaque(false);
500
        tabs.add(mainBottomPanel, "Règlements et caractéristiques");
501
        final JScrollPane scrollPane = new JScrollPane(this.infos);
502
        scrollPane.setBorder(null);
503
 
504
        tabs.add(scrollPane, getLabelFor("INFOS"));
80 ilm 505
        c.gridx = 0;
506
        c.weightx = 1;
507
        c.anchor = GridBagConstraints.WEST;
180 ilm 508
        c.fill = GridBagConstraints.BOTH;
509
        panel.add(tabs, c);
80 ilm 510
 
180 ilm 511
        mainBottomPanel.setLayout(new GridBagLayout());
512
        GridBagConstraints cMainBottomPanel = new DefaultGridBagConstraints();
80 ilm 513
 
180 ilm 514
        // Colonne 1 : Infos
80 ilm 515
 
180 ilm 516
        cMainBottomPanel.weighty = 0;
517
        cMainBottomPanel.weightx = 0;
518
        cMainBottomPanel.fill = GridBagConstraints.BOTH;
519
 
80 ilm 520
        this.addView("ID_MODE_REGLEMENT", REQ + ";" + DEC + ";" + SEP);
142 ilm 521
        this.eltModeRegl = (ElementSQLObject) this.getView("ID_MODE_REGLEMENT");
80 ilm 522
 
523
        c.gridx++;
524
        c.fill = GridBagConstraints.NONE;
180 ilm 525
        c.weightx = 1;
80 ilm 526
        c.weighty = 1;
180 ilm 527
        this.eltModeRegl.setOpaque(false);
528
        mainBottomPanel.add(this.eltModeRegl, cMainBottomPanel);
80 ilm 529
 
530
        // Colonne 2 : Poids & autres
180 ilm 531
        JPanel column2 = new JPanel();
532
        column2.setOpaque(false);
533
        cMainBottomPanel.gridx++;
534
        cMainBottomPanel.weightx = 0;
535
        mainBottomPanel.add(column2, cMainBottomPanel);
80 ilm 536
 
180 ilm 537
        column2.setLayout(new GridBagLayout());
538
        GridBagConstraints c2 = new DefaultGridBagConstraints();
80 ilm 539
 
180 ilm 540
        c2.weightx = 0;
541
        c2.weighty = 0;
542
        c2.gridwidth = 1;
543
        c2.gridheight = 1;
544
        c2.fill = GridBagConstraints.HORIZONTAL;
545
        c2.anchor = GridBagConstraints.NORTHEAST;
546
        // Poids
547
        DefaultProps props = DefaultNXProps.getInstance();
548
        Boolean poidsVisible = props.getBooleanValue("ArticleShowPoids");
549
        if (poidsVisible) {
550
            column2.add(new JLabel(getLabelFor("T_POIDS"), SwingConstants.RIGHT), c2);
551
            final JTextField textPoidsTotal = new JTextField(8);
80 ilm 552
            textPoidsTotal.setEnabled(false);
553
            textPoidsTotal.setHorizontalAlignment(JTextField.RIGHT);
554
            textPoidsTotal.setDisabledTextColor(Color.BLACK);
180 ilm 555
            c2.weightx = 1;
556
            c2.gridx++;
557
            column2.add(textPoidsTotal, c2);
558
            DefaultGridBagConstraints.lockMinimumSize(textPoidsTotal);
559
            addSQLObject(textPoidsTotal, "T_POIDS");
80 ilm 560
 
180 ilm 561
            this.table.getModel().addTableModelListener(new TableModelListener() {
80 ilm 562
 
180 ilm 563
                public void tableChanged(TableModelEvent e) {
564
                    textPoidsTotal.setText(String.valueOf(FactureFournisseurSQLComponent.this.table.getPoidsTotal()));
565
                }
566
            });
567
 
80 ilm 568
        } else {
180 ilm 569
            addSQLObject(new JTextField(), "T_POIDS");
80 ilm 570
        }
571
 
572
        DeviseField textPortHT = new DeviseField();
573
        ElementComboBox comboTaxePort = new ElementComboBox();
149 ilm 574
        DeviseField textRemiseHT = new DeviseField();
80 ilm 575
 
576
        if (getTable().contains("PORT_HT")) {
180 ilm 577
            // Port HT
149 ilm 578
            addSQLObject(textPortHT, "PORT_HT");
180 ilm 579
            c2.gridx = 0;
580
            c2.gridy++;
581
            c2.weightx = 0;
582
            column2.add(new JLabel(getLabelFor("PORT_HT"), SwingConstants.RIGHT), c2);
80 ilm 583
            textPortHT.setHorizontalAlignment(JTextField.RIGHT);
180 ilm 584
            c2.gridx++;
585
            c2.weightx = 1;
586
            column2.add(textPortHT, c2);
587
            // TVA sur port
588
            c2.gridy++;
589
            c2.gridx = 0;
590
            c2.weightx = 0;
80 ilm 591
            addRequiredSQLObject(comboTaxePort, "ID_TAXE_PORT");
180 ilm 592
            column2.add(new JLabel(getLabelFor("ID_TAXE_PORT"), SwingConstants.RIGHT), c2);
593
            c2.gridx++;
594
            c2.weightx = 0;
595
            column2.add(comboTaxePort, c2);
596
            // Remise HT
149 ilm 597
            addSQLObject(textRemiseHT, "REMISE_HT");
180 ilm 598
            c2.gridx = 0;
599
            c2.gridy++;
600
            c2.weightx = 0;
601
            column2.add(new JLabel(getLabelFor("REMISE_HT"), SwingConstants.RIGHT), c2);
149 ilm 602
            textRemiseHT.setHorizontalAlignment(JTextField.RIGHT);
180 ilm 603
            c2.gridx++;
604
            c2.weightx = 1;
605
            column2.add(textRemiseHT, c2);
149 ilm 606
 
80 ilm 607
        }
180 ilm 608
        final JTextField textTvaAdujs;
609
        if (getTable().contains("TVA_ADJUSTMENT")) {
610
            textTvaAdujs = new JTextField(15);
611
            final JLabel labelTvaAdujst = new JLabel(getLabelFor("TVA_ADJUSTMENT"), SwingConstants.RIGHT);
612
            c2.gridx = 0;
613
            c2.gridy++;
614
            c2.weightx = 0;
615
            column2.add(labelTvaAdujst, c2);
616
            c2.gridx++;
617
            c2.weightx = 1;
618
            column2.add(textTvaAdujs, c2);
619
            addView(textTvaAdujs, "TVA_ADJUSTMENT");
80 ilm 620
 
180 ilm 621
            DefaultGridBagConstraints.lockMinimumSize(textTvaAdujs);
622
        } else {
623
            textTvaAdujs = null;
624
        }
625
 
626
        c2.gridy++;
627
        c2.gridx = 0;
628
        column2.add(new JLabel("Avoirs", SwingConstants.RIGHT), c2);
629
 
630
        c2.gridx++;
631
        c2.fill = GridBagConstraints.NONE;
632
        c2.anchor = GridBagConstraints.EAST;
633
        column2.add(createPanelAvoir(), c2);
634
 
635
        c2.gridy++;
636
        column2.add(getModuleTotalPanel(), c2);
637
        c2.gridy++;
638
        JPanel filler = new JPanel();
639
        filler.setOpaque(false);
640
        c2.weighty = 1;
641
        column2.add(filler, c2);
642
 
643
        // Colonne 4 Total
644
 
80 ilm 645
        DeviseField fieldHT = new DeviseField();
142 ilm 646
        DeviseField fieldEco = new DeviseField();
80 ilm 647
        DeviseField fieldTVA = new DeviseField();
648
        DeviseField fieldDevise = new DeviseField();
649
        DeviseField fieldService = new DeviseField();
650
        fieldHT.setOpaque(false);
651
        fieldTVA.setOpaque(false);
180 ilm 652
        this.fieldTTC.setOpaque(false);
80 ilm 653
        fieldService.setOpaque(false);
654
        addRequiredSQLObject(fieldDevise, "T_DEVISE");
142 ilm 655
        addRequiredSQLObject(fieldEco, "T_ECO_CONTRIBUTION");
80 ilm 656
        addRequiredSQLObject(fieldHT, "T_HT");
657
        addRequiredSQLObject(fieldTVA, "T_TVA");
658
 
180 ilm 659
        addRequiredSQLObject(this.fieldTTC, "T_TTC");
80 ilm 660
        addRequiredSQLObject(fieldService, "T_SERVICE");
94 ilm 661
 
180 ilm 662
        this.fieldTTC.getDocument().addDocumentListener(new SimpleDocumentListener() {
156 ilm 663
            @Override
664
            public void update(DocumentEvent e) {
665
                refreshText();
666
            }
667
 
668
        });
669
 
94 ilm 670
        // Disable
142 ilm 671
        this.allowEditable("T_ECO_CONTRIBUTION", false);
94 ilm 672
        this.allowEditable("T_HT", false);
673
        this.allowEditable("T_TVA", false);
674
        this.allowEditable("T_TTC", false);
675
        this.allowEditable("T_SERVICE", false);
676
        this.allowEditable("T_POIDS", false);
677
 
180 ilm 678
        final TotalPanel totalTTC = new TotalPanel(this.table, fieldEco, fieldHT, fieldTVA, this.fieldTTC, textPortHT, textRemiseHT, fieldService, null, fieldDevise, null, null,
142 ilm 679
                (getTable().contains("ID_TAXE_PORT") ? comboTaxePort : null), null);
180 ilm 680
        if (textTvaAdujs != null) {
174 ilm 681
            totalTTC.setTextFixTVA(textTvaAdujs);
682
            textTvaAdujs.getDocument().addDocumentListener(new SimpleDocumentListener() {
683
                @Override
684
                public void update(final DocumentEvent e) {
685
                    final String text = textTvaAdujs.getText();
686
                    if (text != null && text.trim().length() > 0) {
687
                        if (!text.trim().equals("-")) {
688
                            BigDecimal tvaFix = new BigDecimal(text);
689
                            if (tvaFix.abs().compareTo(new BigDecimal(0.05)) > 0) {
690
                                final String limitedFix;
691
                                if (tvaFix.signum() > 0) {
692
                                    limitedFix = tvaFix.min(new BigDecimal(0.05)).toString();
693
                                } else {
694
                                    limitedFix = tvaFix.max(new BigDecimal(-0.05)).toString();
695
                                }
696
                                SwingUtilities.invokeLater(new Runnable() {
697
                                    @Override
698
                                    public void run() {
699
 
700
                                        textTvaAdujs.setText(limitedFix);
701
                                    }
702
                                });
703
                            }
704
                        }
705
                    }
706
                    totalTTC.updateTotal();
707
                }
708
            });
709
        }
80 ilm 710
        c.gridx++;
180 ilm 711
        c.gridy = 0;
712
        c.gridwidth = 1;
713
        c.gridheight = 1;
80 ilm 714
        c.anchor = GridBagConstraints.NORTHEAST;
180 ilm 715
        c.fill = GridBagConstraints.HORIZONTAL;
80 ilm 716
        c.weighty = 0;
717
        DefaultGridBagConstraints.lockMinimumSize(totalTTC);
718
 
719
        panel.add(totalTTC, c);
720
 
142 ilm 721
        this.fourn.addModelListener("wantedID", new PropertyChangeListener() {
722
 
723
            @Override
724
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 725
                final SQLRow selectedRow2 = FactureFournisseurSQLComponent.this.fourn.getSelectedRow();
142 ilm 726
                if (selectedRow2 != null && !selectedRow2.isUndefined()) {
727
                    totalTTC.setIntraComm(selectedRow2.getBoolean("UE"));
728
                } else {
729
                    totalTTC.setIntraComm(false);
730
                }
731
 
732
            }
733
        });
734
 
80 ilm 735
        textPortHT.getDocument().addDocumentListener(new DocumentListener() {
736
            public void changedUpdate(DocumentEvent e) {
737
                totalTTC.updateTotal();
738
            }
739
 
740
            public void removeUpdate(DocumentEvent e) {
741
                totalTTC.updateTotal();
742
            }
743
 
744
            public void insertUpdate(DocumentEvent e) {
745
                totalTTC.updateTotal();
746
            }
747
        });
748
 
749
        comboTaxePort.addValueListener(new PropertyChangeListener() {
750
 
751
            @Override
752
            public void propertyChange(PropertyChangeEvent evt) {
753
                totalTTC.updateTotal();
754
            }
755
        });
756
 
757
        textRemiseHT.getDocument().addDocumentListener(new DocumentListener() {
758
            public void changedUpdate(DocumentEvent e) {
759
                totalTTC.updateTotal();
760
            }
761
 
762
            public void removeUpdate(DocumentEvent e) {
763
                totalTTC.updateTotal();
764
            }
765
 
766
            public void insertUpdate(DocumentEvent e) {
767
                totalTTC.updateTotal();
768
            }
769
        });
177 ilm 770
 
771
        ModeDeReglementSQLComponent modeReglComp = (ModeDeReglementSQLComponent) this.eltModeRegl.getSQLChild();
772
        modeReglComp.addDateCompListener(this.dateCommande);
80 ilm 773
        return panel;
774
    }
775
 
776
    protected JPanel getModuleTotalPanel() {
777
 
778
        return AutoHideListener.listen(new JPanel());
779
    }
780
 
781
    public int insert(SQLRow order) {
782
 
180 ilm 783
        final SQLTable tableNum = getTable().getTable("NUMEROTATION_AUTO");
784
        // incrémentation du numéro auto
785
        if (tableNum.contains("FACTURE_FOURNISSEUR_START") && NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), new Date()).equalsIgnoreCase(this.numero.getText().trim())) {
786
            SQLRowValues rowVals = new SQLRowValues(tableNum);
787
 
788
            final SQLRow rowNumAuto = tableNum.getRow(2);
789
            if (rowNumAuto.getObject("FACTURE_FOURNISSEUR_START") != null) {
790
                int val = rowNumAuto.getInt("FACTURE_FOURNISSEUR_START");
791
                val++;
792
                rowVals.put("FACTURE_FOURNISSEUR_START", new Integer(val));
793
 
794
                try {
795
                    rowVals.update(2);
796
                } catch (SQLException e) {
797
                    e.printStackTrace();
798
                }
799
            }
800
        }
80 ilm 801
        int idFacture = getSelectedID();
802
        idFacture = super.insert(order);
803
        this.table.updateField("ID_FACTURE_FOURNISSEUR", idFacture);
804
 
805
        // Création des articles
806
        this.table.createArticle(idFacture, this.getElement());
807
 
156 ilm 808
        final SQLRow rowA = getTable().getRow(idFacture);
809
        new GenerationMvtFactureFournisseur(rowA);
80 ilm 810
 
156 ilm 811
        final FactureFournisseurXmlSheet sheet = new FactureFournisseurXmlSheet(rowA);
94 ilm 812
        sheet.createDocumentAsynchronous();
813
        sheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
814
 
156 ilm 815
        commitAvoir(null, rowA);
816
 
80 ilm 817
        return idFacture;
818
    }
819
 
820
    @Override
821
    public void update() {
822
 
156 ilm 823
        SQLRow rowFactureOld = getTable().getRow(getSelectedID());
80 ilm 824
        super.update();
825
        SQLRow row = getTable().getRow(getSelectedID());
826
        this.table.updateField("ID_FACTURE_FOURNISSEUR", row.getID());
827
        this.table.createArticle(getSelectedID(), this.getElement());
828
 
177 ilm 829
        boolean createCompte = (rowFactureOld.getForeignID("ID_FOURNISSEUR") != row.getForeignID("ID_FOURNISSEUR"));
830
        createCompte = createCompte || (rowFactureOld.getForeignID("ID_COMPTE_PCE") != row.getForeignID("ID_COMPTE_PCE"));
831
        createCompte = createCompte || rowFactureOld.getLong("T_TTC") != row.getLong("T_TTC");
832
        createCompte = createCompte || rowFactureOld.getLong("T_TVA") != row.getLong("T_TVA");
833
        createCompte = createCompte || rowFactureOld.getLong("T_HT") != row.getLong("T_HT");
834
        if (!createCompte) {
835
            int a = JOptionPane.showConfirmDialog(null, "Voulez vous recréer la comptabilité ?", "Comptabilité", JOptionPane.YES_NO_OPTION);
836
            createCompte = a == JOptionPane.YES_OPTION;
837
        }
838
        if (createCompte) {
839
            int idMvt = (row.getObject("ID_MOUVEMENT") == null ? 1 : row.getInt("ID_MOUVEMENT"));
840
            System.err.println("__________***************** UPDATE" + idMvt);
80 ilm 841
 
177 ilm 842
            // on supprime tout ce qui est lié à la facture
843
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
844
            eltEcr.archiveMouvementProfondeur(idMvt, false);
845
            if (idMvt > 1) {
846
                new GenerationMvtFactureFournisseur(row, idMvt);
847
            } else {
848
                new GenerationMvtFactureFournisseur(row);
849
            }
80 ilm 850
        }
94 ilm 851
        final FactureFournisseurXmlSheet sheet = new FactureFournisseurXmlSheet(row);
852
        sheet.createDocumentAsynchronous();
853
        sheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
156 ilm 854
        commitAvoir(rowFactureOld, row);
80 ilm 855
    }
856
 
156 ilm 857
    public void commitAvoir(SQLRow rowFactureOld, SQLRow rowFacture) {
180 ilm 858
 
156 ilm 859
        try {
180 ilm 860
            List<String> fieldsAvoir = Arrays.asList("ID_AVOIR_FOURNISSEUR", "ID_AVOIR_FOURNISSEUR_2");
861
            for (String field : fieldsAvoir) {
156 ilm 862
 
180 ilm 863
                if (rowFactureOld != null && rowFactureOld.getObject(field) != null && !rowFactureOld.isForeignEmpty(field)
864
                        && (rowFacture.getObject(field) == null || rowFacture.isForeignEmpty(field))) {
156 ilm 865
 
180 ilm 866
                    SQLRow rowAvoir = rowFactureOld.getForeignRow(field);
156 ilm 867
 
180 ilm 868
                    SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
156 ilm 869
 
180 ilm 870
                    // Soldé
871
                    rowVals.put("SOLDE", Boolean.FALSE);
156 ilm 872
 
180 ilm 873
                    rowVals.update();
156 ilm 874
 
180 ilm 875
                }
156 ilm 876
 
180 ilm 877
                // on solde l'avoir
878
                if (rowFacture.getObject(field) != null && !rowFacture.isForeignEmpty(field)) {
156 ilm 879
 
180 ilm 880
                    SQLRow rowAvoir = rowFacture.getForeignRow(field);
156 ilm 881
 
180 ilm 882
                    SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
883
                    rowVals.put("SOLDE", Boolean.TRUE);
156 ilm 884
 
180 ilm 885
                    rowVals.update();
886
 
887
                }
156 ilm 888
            }
889
        } catch (SQLException e) {
890
            ExceptionHandler.handle("Erreur lors la mise à jour de l'avoir associée!", e);
891
        }
892
    }
893
 
80 ilm 894
    public void setDefaults() {
895
        this.resetValue();
896
        this.table.getModel().clearRows();
897
    }
898
 
899
    @Override
900
    protected SQLRowValues createDefaults() {
901
        SQLRowValues rowVals = new SQLRowValues(getTable());
902
        rowVals.put("T_POIDS", 0.0F);
903
        // User
904
        // SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
905
        SQLElement eltComm = Configuration.getInstance().getDirectory().getElement("COMMERCIAL");
906
        int idUser = UserManager.getInstance().getCurrentUser().getId();
907
 
908
        // sel.addSelect(eltComm.getTable().getKey());
909
        // sel.setWhere(new Where(eltComm.getTable().getField("ID_USER_COMMON"), "=", idUser));
910
        // List<SQLRow> rowsComm = (List<SQLRow>)
911
        // Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new
912
        // SQLRowListRSH(eltComm.getTable()));
913
        SQLRow rowsComm = SQLBackgroundTableCache.getInstance().getCacheForTable(eltComm.getTable()).getFirstRowContains(idUser, eltComm.getTable().getField("ID_USER_COMMON"));
914
 
915
        if (rowsComm != null) {
916
            rowVals.put("ID_COMMERCIAL", rowsComm.getID());
917
        }
180 ilm 918
 
919
        if (getTable().getTable("NUMEROTATION_AUTO").contains("FACTURE_FOURNISSEUR_START")) {
920
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), new Date()));
921
        }
922
 
80 ilm 923
        rowVals.put("T_HT", Long.valueOf(0));
924
        rowVals.put("T_SERVICE", Long.valueOf(0));
925
        rowVals.put("T_DEVISE", Long.valueOf(0));
926
        rowVals.put("T_TVA", Long.valueOf(0));
927
        rowVals.put("T_TTC", Long.valueOf(0));
928
        // rowVals.put("NUMERO",
929
        // NumerotationAutoSQLElement.getNextNumero(CommandeSQLElement.class));
930
 
931
        if (getTable().contains("ID_TAXE_PORT")) {
932
            rowVals.put("ID_TAXE_PORT", TaxeCache.getCache().getFirstTaxe().getID());
933
        }
934
 
935
        return rowVals;
936
    }
937
 
938
    @Override
939
    public RowValuesTable getRowValuesTable() {
940
        return this.table.getRowValuesTable();
941
    }
942
 
156 ilm 943
    private JPanel createPanelAvoir() {
944
        JPanel panelAvoir = new JPanel(new GridBagLayout());
945
        panelAvoir.setOpaque(false);
946
        GridBagConstraints cA = new DefaultGridBagConstraints();
947
        JLabel labelAvoir = new JLabel(getLabelFor("ID_AVOIR_FOURNISSEUR"));
948
        labelAvoir.setHorizontalAlignment(SwingConstants.RIGHT);
949
        cA.weightx = 1;
950
        labelAvoir.setHorizontalAlignment(SwingConstants.RIGHT);
951
        panelAvoir.add(labelAvoir, cA);
952
        cA.weightx = 0;
953
        cA.gridx++;
954
        this.avoirFourn = new ElementComboBox();
955
        this.avoirFourn.setAddIconVisible(false);
956
        panelAvoir.add(this.avoirFourn, cA);
957
        final JLabel labelTotalAvoir = new JLabel("Total à régler");
958
        this.textNetAPayer.setEditable(false);
959
        cA.gridx++;
960
        cA.weightx = 0;
961
        panelAvoir.add(labelTotalAvoir, cA);
962
        cA.gridx++;
963
        cA.weightx = 0;
964
        panelAvoir.add(this.textNetAPayer, cA);
180 ilm 965
        addView(this.textNetAPayer, "NET_A_PAYER");
156 ilm 966
        this.textNetAPayer.setHorizontalAlignment(SwingConstants.RIGHT);
967
 
180 ilm 968
        JLabel labelAvoir2 = new JLabel(getLabelFor("ID_AVOIR_FOURNISSEUR_2"));
969
        labelAvoir.setHorizontalAlignment(SwingConstants.RIGHT);
970
        cA.gridx = 0;
971
        cA.gridy++;
972
        cA.weightx = 1;
973
        labelAvoir.setHorizontalAlignment(SwingConstants.RIGHT);
974
        panelAvoir.add(labelAvoir2, cA);
975
        cA.weightx = 0;
976
        cA.gridx++;
977
        this.avoirFourn2 = new ElementComboBox();
978
        this.avoirFourn2.setAddIconVisible(false);
979
        panelAvoir.add(this.avoirFourn2, cA);
980
 
981
        addView(this.avoirFourn2, "ID_AVOIR_FOURNISSEUR_2");
156 ilm 982
        addView(this.avoirFourn, "ID_AVOIR_FOURNISSEUR");
180 ilm 983
        addView(this.avoirTTC, "AVOIR_TTC");
984
        this.avoirFourn.addModelListener("wantedID", new PropertyChangeListener() {
156 ilm 985
 
986
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 987
                if (!isFilling()) {
988
                    refreshText();
989
                }
156 ilm 990
            }
991
 
992
        });
180 ilm 993
        this.avoirFourn2.addModelListener("wantedID", new PropertyChangeListener() {
156 ilm 994
 
180 ilm 995
            public void propertyChange(PropertyChangeEvent evt) {
996
                if (!isFilling()) {
997
                    refreshText();
998
                }
999
            }
1000
 
1001
        });
156 ilm 1002
        return panelAvoir;
1003
    }
1004
 
1005
    private void refreshText() {
1006
        Number n = this.fieldTTC.getValue();
1007
        long totalAvoirTTC = 0;
1008
        long netAPayer = 0;
1009
        if (n != null) {
1010
            netAPayer = n.longValue();
1011
        }
1012
 
180 ilm 1013
        List<ElementComboBox> boxAvoir = Arrays.asList(this.avoirFourn, this.avoirFourn2);
1014
        for (ElementComboBox elementComboBox : boxAvoir) {
1015
            if (elementComboBox.getWantedID() > 1) {
1016
                SQLTable tableAvoir = getTable().getForeignTable("ID_AVOIR_FOURNISSEUR");
1017
                if (n != null) {
1018
                    SQLRow rowAvoir = tableAvoir.getRow(elementComboBox.getWantedID());
1019
                    long totalAvoir = ((Number) rowAvoir.getObject("MONTANT_TTC")).longValue();
1020
                    if (getSelectedID() > 1) {
1021
                        SQLRow row = getTable().getRow(getSelectedID());
1022
                        int idAvoirOld = row.getInt("ID_AVOIR_FOURNISSEUR");
1023
                        if (idAvoirOld == rowAvoir.getID()) {
1024
                            totalAvoir += Long.valueOf(row.getObject("AVOIR_TTC").toString());
1025
                        }
156 ilm 1026
                    }
1027
 
180 ilm 1028
                    long l = netAPayer - totalAvoir;
1029
                    if (l < 0) {
1030
                        l = 0;
1031
                        totalAvoirTTC = netAPayer;
1032
                    } else {
1033
                        totalAvoirTTC = totalAvoir;
1034
                    }
1035
                    netAPayer = l;
156 ilm 1036
                }
1037
            }
1038
        }
1039
 
1040
        this.textNetAPayer.setValue(netAPayer);
1041
        this.avoirTTC.setValue(totalAvoirTTC);
1042
    }
1043
 
93 ilm 1044
    @Override
1045
    protected void refreshAfterSelect(SQLRowAccessor rSource) {
1046
        if (this.dateCommande.getValue() != null) {
1047
            this.table.setDateDevise(this.dateCommande.getValue());
132 ilm 1048
            updateLabelTauxConversion();
93 ilm 1049
        }
1050
 
1051
    }
1052
 
174 ilm 1053
    public void loadFactureExistante(int idFacture) {
1054
 
1055
        SQLElement fact = getElement();
1056
        SQLElement factElt = getElement().getDirectory().getElement("FACTURE_FOURNISSEUR_ELEMENT");
1057
 
1058
        // On duplique la facture
1059
        if (idFacture > 1) {
1060
            SQLRow row = fact.getTable().getRow(idFacture);
1061
            SQLRowValues rowVals = new SQLRowValues(fact.getTable());
177 ilm 1062
            rowVals.put("ID_FOURNISSEUR", row.getForeignID("ID_FOURNISSEUR"));
1063
            if (!row.isForeignEmpty("ID_COMPTE_PCE")) {
1064
                rowVals.put("ID_COMPTE_PCE", row.getForeignID("ID_COMPTE_PCE"));
1065
            }
180 ilm 1066
 
174 ilm 1067
            rowVals.put("NOM", row.getObject("NOM"));
1068
            this.select(rowVals);
1069
        }
1070
 
1071
        // On duplique les elements de facture
1072
        List<SQLRow> myListItem = fact.getTable().getRow(idFacture).getReferentRows(factElt.getTable());
1073
 
1074
        if (myListItem.size() != 0) {
1075
            this.table.getModel().clearRows();
1076
 
1077
            for (SQLRow rowElt : myListItem) {
1078
 
1079
                SQLRowValues rowVals = rowElt.createUpdateRow();
1080
                rowVals.clearPrimaryKeys();
180 ilm 1081
                if (rowVals.getTable().contains("ID_COMMANDE_ELEMENT")) {
1082
                    rowVals.putEmptyLink("ID_COMMANDE_ELEMENT");
1083
                }
1084
                if (rowVals.getTable().contains("ID_BON_RECEPTION_ELEMENT")) {
1085
                    rowVals.putEmptyLink("ID_BON_RECEPTION_ELEMENT");
1086
                }
174 ilm 1087
                this.table.getModel().addRow(rowVals);
1088
                int rowIndex = this.table.getModel().getRowCount() - 1;
1089
                this.table.getModel().fireTableModelModified(rowIndex);
1090
            }
1091
        } else {
1092
            this.table.getModel().clearRows();
1093
        }
1094
        this.table.getModel().fireTableDataChanged();
1095
        this.table.repaint();
1096
    }
1097
 
80 ilm 1098
}