OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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