OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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