OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.common.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
94 ilm 17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18 ilm 18
import org.openconcerto.erp.preferences.DefaultNXProps;
156 ilm 19
import org.openconcerto.erp.utils.TM;
18 ilm 20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.SQLField;
65 ilm 22
import org.openconcerto.sql.model.SQLRow;
61 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
67 ilm 24
import org.openconcerto.sql.model.SQLRowValues;
94 ilm 25
import org.openconcerto.sql.model.SQLTable;
142 ilm 26
import org.openconcerto.sql.preferences.SQLPreferences;
65 ilm 27
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
94 ilm 28
import org.openconcerto.sql.users.rights.UserRights;
144 ilm 29
import org.openconcerto.sql.users.rights.UserRightsManager;
18 ilm 30
import org.openconcerto.sql.view.list.RowValuesTable;
31
import org.openconcerto.sql.view.list.SQLTableElement;
32
import org.openconcerto.ui.DefaultGridBagConstraints;
33
import org.openconcerto.ui.JLabelBold;
90 ilm 34
import org.openconcerto.utils.DecimalUtils;
67 ilm 35
import org.openconcerto.utils.ExceptionHandler;
18 ilm 36
import org.openconcerto.utils.GestionDevise;
37
 
38
import java.awt.Color;
39
import java.awt.Dimension;
40
import java.awt.GridBagConstraints;
41
import java.awt.GridBagLayout;
42
import java.beans.PropertyChangeListener;
43
import java.beans.PropertyChangeSupport;
61 ilm 44
import java.math.BigDecimal;
67 ilm 45
import java.math.RoundingMode;
46
import java.util.ArrayList;
47
import java.util.List;
18 ilm 48
 
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JSeparator;
52
import javax.swing.JTextField;
144 ilm 53
import javax.swing.SwingWorker;
18 ilm 54
import javax.swing.UIManager;
55
import javax.swing.event.ListSelectionEvent;
56
import javax.swing.event.ListSelectionListener;
57
import javax.swing.event.TableModelEvent;
58
import javax.swing.event.TableModelListener;
59
 
60
public class TotalPanel extends JPanel implements TableModelListener {
21 ilm 61
    public static String MARGE_MARQUE = "MargeMarque";
18 ilm 62
    private RowValuesTable table;
63
    private DeviseField textTotalHT, textTotalHTSel;
64
    private DeviseField textTotalTVA, textTotalTVASel;
65
    private DeviseField textTotalTTC, textTotalTTCSel;
66
    private DeviseField textPortHT;
142 ilm 67
    private DeviseField textRemiseHT, textTotalEco, textTotalEcoSel;
174 ilm 68
    private JTextField textTVAFix;
19 ilm 69
    private JTextField textPoids;
70
    private DeviseField textTotalDevise, textTotalDeviseSel;
18 ilm 71
    private DeviseField textService, textServiceSel;
72
    private DeviseField textHA, textHASel;
73
    private JTextField marge, margeSel;
156 ilm 74
    private DeviseField textFraisDoc, textFraisDocSel;
18 ilm 75
    private boolean gestionHA = false;
76
    private PropertyChangeSupport supp;
77
    private int columnIndexEchHT = -1;
78
    private int columnIndexEchTTC = -1;
73 ilm 79
    private SQLTableElement ha;
65 ilm 80
    private SQLRequestComboBox selPortTVA;
156 ilm 81
    private SQLRequestComboBox selFraisDocTVA = null;
142 ilm 82
    private AcompteField facturable;
83
    private boolean intraComm = false;
18 ilm 84
 
67 ilm 85
    AbstractArticleItemTable articleTable;
86
 
142 ilm 87
    public TotalPanel(AbstractArticleItemTable articleItemTable, DeviseField textTotalEco, DeviseField textTotalHT, DeviseField textTotalTVA, DeviseField textTotalTTC, DeviseField textPortHT,
88
            DeviseField textRemiseHT, DeviseField textService, DeviseField textTotalHA, DeviseField textTotalDevise, JTextField textTotalPoids, JPanel tableEchantillon) {
89
        this(articleItemTable, textTotalEco, textTotalHT, textTotalTVA, textTotalTTC, textPortHT, textRemiseHT, textService, textTotalHA, textTotalDevise, textTotalPoids, tableEchantillon, null,
90
                null);
65 ilm 91
    }
18 ilm 92
 
142 ilm 93
    public TotalPanel(AbstractArticleItemTable articleItemTable, DeviseField textTotalEco, DeviseField textTotalHT, DeviseField textTotalTVA, DeviseField textTotalTTC, DeviseField textPortHT,
94
            DeviseField textRemiseHT, DeviseField textService, DeviseField textTotalHA, DeviseField textTotalDevise, JTextField textTotalPoids, JPanel tableEchantillon, SQLRequestComboBox selPortTva,
95
            AcompteField facturable) {
65 ilm 96
 
18 ilm 97
        super();
93 ilm 98
        this.setOpaque(false);
65 ilm 99
        this.selPortTVA = selPortTva;
142 ilm 100
        this.facturable = facturable;
67 ilm 101
        this.articleTable = articleItemTable;
18 ilm 102
        this.supp = new PropertyChangeSupport(this);
61 ilm 103
        this.table = articleItemTable.getRowValuesTable();
67 ilm 104
 
105
        this.ha = (articleItemTable.getPrebilanElement() == null) ? articleItemTable.getHaElement() : articleItemTable.getPrebilanElement();
61 ilm 106
        this.gestionHA = ha != null && articleItemTable.getQteElement() != null;
18 ilm 107
 
61 ilm 108
        this.textPoids = (textTotalPoids == null ? new JTextField() : textTotalPoids);
18 ilm 109
        this.textTotalHT = textTotalHT;
110
        this.textTotalHT.setBold();
142 ilm 111
        this.textTotalEco = textTotalEco;
18 ilm 112
        this.textTotalTVA = textTotalTVA;
113
        this.textTotalTTC = textTotalTTC;
114
        this.textPortHT = textPortHT;
115
        this.textRemiseHT = textRemiseHT;
116
        this.textService = textService;
156 ilm 117
        this.textFraisDoc = new DeviseField();
174 ilm 118
        this.textTVAFix = new DeviseField();
156 ilm 119
        this.textFraisDocSel = new DeviseField();
18 ilm 120
        this.textTotalHTSel = new DeviseField(true);
121
        this.textServiceSel = new DeviseField();
142 ilm 122
        this.textTotalEcoSel = new DeviseField();
18 ilm 123
        this.textTotalTTCSel = new DeviseField();
124
        this.textTotalTVASel = new DeviseField();
94 ilm 125
        this.textHA = (textTotalHA == null) ? new DeviseField() : textTotalHA;
126
        this.textHASel = new DeviseField();
18 ilm 127
        this.marge = new JTextField();
128
        this.margeSel = new JTextField();
144 ilm 129
        final UserRights rights = UserRightsManager.getCurrentUserRights();
67 ilm 130
 
94 ilm 131
        final boolean showHA = rights.haveRight(AbstractVenteArticleItemTable.SHOW_PRIX_ACHAT_CODE);
132
        this.textHA.setVisible(showHA);
133
        this.textHASel.setVisible(showHA);
134
        this.marge.setVisible(showHA);
135
        this.margeSel.setVisible(showHA);
136
 
61 ilm 137
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 138
            this.textTotalDevise = textTotalDevise;
139
            this.textTotalDeviseSel = new DeviseField();
140
            reconfigure(this.textTotalDevise);
141
            reconfigure(this.textTotalDeviseSel);
142
        }
18 ilm 143
        reconfigure(this.textTotalHT);
142 ilm 144
        reconfigure(this.textTotalEco);
18 ilm 145
        reconfigure(this.textTotalTVA);
146
        reconfigure(this.textTotalTTC);
147
        reconfigure(this.textService);
148
        reconfigure(this.textHA);
149
        reconfigure(this.marge);
150
        reconfigure(this.textTotalHTSel);
156 ilm 151
        reconfigure(this.textFraisDoc);
174 ilm 152
        reconfigure(this.textTVAFix);
156 ilm 153
        reconfigure(this.textFraisDocSel);
142 ilm 154
        reconfigure(this.textTotalEcoSel);
18 ilm 155
        reconfigure(this.textTotalTVASel);
156
        reconfigure(this.textTotalTTCSel);
157
        reconfigure(this.textServiceSel);
158
        reconfigure(this.textHASel);
159
        reconfigure(this.margeSel);
160
 
67 ilm 161
        // String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
162
        // Boolean b = Boolean.valueOf(val);
18 ilm 163
 
67 ilm 164
        // if (this.columnIndexHT < 0 || this.columnIndexTVA < 0 || (b != null && b.booleanValue()
165
        // && this.columnIndexService < 0)) {
166
        // throw new IllegalArgumentException("Impossible de trouver la colonne de " +
167
        // articleItemTable.getPrixTotalHTElement() + " / " + articleItemTable.getTVAElement() +
168
        // " / "
169
        // + articleItemTable.getPrixServiceElement());
170
        // }
18 ilm 171
        this.setLayout(new GridBagLayout());
172
        GridBagConstraints c = new DefaultGridBagConstraints();
173
 
174
        c.gridheight = 1;
175
        c.fill = GridBagConstraints.HORIZONTAL;
176
 
177
        // Collone 1 : Selection
178
        c.gridx++;
156 ilm 179
        this.add(new JLabelBold(TM.tr("TotalPanel.selection")), c); //$NON-NLS-1$
18 ilm 180
        c.gridy++;
181
        c.gridx = 1;
182
        c.gridwidth = 2;
183
        c.weightx = 1;
184
        c.fill = GridBagConstraints.BOTH;
185
        this.add(createSeparator(), c);
186
 
187
        c.gridwidth = 1;
188
        c.weightx = 0;
189
        c.fill = GridBagConstraints.HORIZONTAL;
190
        if (this.gestionHA) {
156 ilm 191
            final JLabel labelMarge = new JLabel(TM.tr("TotalPanel.margin")); //$NON-NLS-1$
174 ilm 192
            final JLabel labelHA = new JLabel((this.ha == articleItemTable.getPrebilanElement()) ? TM.tr("TotalPanel.estimatedBudget") : TM.tr("TotalPanel.purchaseTotal.taxExcluded")); //$NON-NLS-1$//$NON-NLS-2$
18 ilm 193
 
194
            // Total HA HT
195
            c.gridy++;
94 ilm 196
            this.add(labelHA, c);
197
            labelHA.setVisible(showHA);
18 ilm 198
 
199
            c.gridx++;
200
            c.weightx = 1;
201
            this.add(this.textHASel, c);
202
 
203
            // Marge
204
            c.gridy++;
205
            c.gridx = 1;
206
            c.weightx = 0;
94 ilm 207
            this.add(labelMarge, c);
208
            labelMarge.setVisible(showHA);
18 ilm 209
 
210
            c.gridx++;
211
            c.weightx = 1;
212
            this.add(this.margeSel, c);
213
 
214
            c.gridy++;
215
            c.gridx = 1;
216
            c.gridwidth = 2;
217
            c.weightx = 1;
218
 
219
            c.fill = GridBagConstraints.BOTH;
220
            this.add(createSeparator(), c);
221
 
222
            c.gridwidth = 1;
223
            c.weightx = 0;
224
        }
225
 
226
        // Total HT
227
        c.gridy++;
228
        c.gridx = 1;
229
        c.weightx = 0;
230
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 231
        this.add(getLabelBoldFor(textTotalHT.getField(), TM.tr("TotalPanel.total.taxExcluded")), c); //$NON-NLS-1$
18 ilm 232
 
233
        c.gridx++;
234
        c.weightx = 1;
235
        this.add(this.textTotalHTSel, c);
236
 
237
        // Service
28 ilm 238
        if (DefaultNXProps.getInstance().getBooleanValue(AbstractVenteArticleItemTable.ARTICLE_SERVICE, false)) {
239
            c.gridx = 1;
240
            c.gridy++;
241
            c.weightx = 0;
156 ilm 242
            this.add(new JLabel(TM.tr("TotalPanel.service.taxExcluded")), c); //$NON-NLS-1$
28 ilm 243
            c.gridx++;
244
            c.weightx = 1;
245
            this.add(this.textServiceSel, c);
246
        }
18 ilm 247
 
248
        // TVA
249
        c.gridx = 1;
250
        c.gridy++;
251
        c.weightx = 0;
156 ilm 252
        this.add(getLabelFor(textTotalTVA.getField(), TM.tr("TotalPanel.total.VAT")), c); //$NON-NLS-1$
18 ilm 253
        c.gridx++;
254
        c.weightx = 1;
255
        this.add(this.textTotalTVASel, c);
256
 
257
        c.gridx = 1;
258
        c.gridy++;
259
        c.gridwidth = 2;
260
        c.fill = GridBagConstraints.BOTH;
261
        this.add(createSeparator(), c);
262
 
61 ilm 263
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 264
            // Devise
265
            c.gridwidth = 1;
266
            c.gridx = 1;
267
            c.gridy++;
268
            c.weightx = 0;
269
            c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 270
            this.add(getLabelFor(textTotalDevise.getField(), TM.tr("TotalPanel.currencyTotal")), c); //$NON-NLS-1$
19 ilm 271
            c.gridx++;
272
            c.weightx = 1;
273
            this.add(this.textTotalDeviseSel, c);
274
        }
142 ilm 275
 
276
        // Eco-contribution
277
        final SQLPreferences prefs = SQLPreferences.getMemCached(this.table.getRowValuesTableModel().getSQLElement().getTable().getDBRoot());
278
        final boolean showEco = prefs.getBoolean(AbstractVenteArticleItemTable.SHOW_TOTAL_ECO_CONTRIBUTION, false);
279
        if (showEco) {
280
            c.gridx = 1;
281
            c.gridy++;
282
            c.weightx = 0;
156 ilm 283
            this.add(new JLabel(TM.tr("TotalPanel.ecoTax")), c); //$NON-NLS-1$
142 ilm 284
            c.gridx++;
285
            c.weightx = 1;
286
            this.add(this.textTotalEcoSel, c);
287
        }
288
 
18 ilm 289
        // TTC
290
        c.gridwidth = 1;
291
        c.gridx = 1;
292
        c.gridy++;
293
        c.weightx = 0;
294
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 295
        this.add(getLabelFor(textTotalTTC.getField(), TM.tr("TotalPanel.total.taxIncluded")), c); //$NON-NLS-1$
18 ilm 296
        c.gridx++;
297
        c.weightx = 1;
298
        this.add(this.textTotalTTCSel, c);
299
 
300
        // Global
301
        c.gridx = 3;
302
        c.gridy = 0;
303
        c.gridheight = GridBagConstraints.REMAINDER;
304
        c.fill = GridBagConstraints.VERTICAL;
305
        c.weightx = 0;
306
        this.add(createSeparator(), c);
307
 
308
        c.gridheight = 1;
309
        c.fill = GridBagConstraints.HORIZONTAL;
310
        c.gridx++;
156 ilm 311
        this.add(new JLabelBold(TM.tr("TotalPanel.global")), c); //$NON-NLS-1$
18 ilm 312
        c.gridy++;
313
        c.gridx = 4;
314
        c.gridwidth = 2;
315
        c.weightx = 1;
316
        this.add(createSeparator(), c);
317
 
318
        c.gridwidth = 1;
319
        c.weightx = 0;
320
        if (this.gestionHA) {
156 ilm 321
            final JLabel labelMarge = new JLabel(TM.tr("TotalPanel.margin")); //$NON-NLS-1$
174 ilm 322
            final JLabel labelHA = new JLabel((this.ha == articleItemTable.getPrebilanElement()) ? TM.tr("TotalPanel.estimatedBudget") : TM.tr("TotalPanel.purchaseTotal.taxExcluded")); //$NON-NLS-1$//$NON-NLS-2$
18 ilm 323
 
324
            // Total HA HT
325
            c.gridy++;
94 ilm 326
            this.add(labelHA, c);
327
            labelHA.setVisible(showHA);
18 ilm 328
 
329
            c.gridx++;
330
            c.weightx = 1;
331
            this.add(this.textHA, c);
332
 
333
            // Marge
334
            c.gridy++;
335
            c.gridx = 4;
336
            c.weightx = 0;
94 ilm 337
            this.add(labelMarge, c);
338
            labelMarge.setVisible(showHA);
18 ilm 339
 
340
            c.gridx++;
341
            c.weightx = 1;
342
            this.add(this.marge, c);
343
 
344
            c.gridy++;
345
            c.gridx = 4;
346
            c.gridwidth = 2;
347
            c.weightx = 1;
348
            this.add(createSeparator(), c);
349
 
350
            c.gridwidth = 1;
351
            c.weightx = 0;
352
        }
353
 
354
        // Total HT
355
        c.gridy++;
356
        c.gridx = 4;
357
        c.weightx = 0;
156 ilm 358
        this.add(getLabelBoldFor(textTotalHT.getField(), TM.tr("TotalPanel.total.taxExcluded")), c); //$NON-NLS-1$
18 ilm 359
 
360
        c.gridx++;
361
        c.weightx = 1;
362
        this.add(textTotalHT, c);
363
 
28 ilm 364
        if (DefaultNXProps.getInstance().getBooleanValue(AbstractVenteArticleItemTable.ARTICLE_SERVICE, false)) {
365
            // Service
366
            c.gridx = 4;
367
            c.gridy++;
368
            c.weightx = 0;
156 ilm 369
            this.add(new JLabelBold(TM.tr("TotalPanel.service.taxExcluded")), c); //$NON-NLS-1$
28 ilm 370
            c.gridx++;
371
            c.weightx = 1;
372
            this.add(this.textService, c);
373
        }
18 ilm 374
        // TVA
375
        c.gridx = 4;
376
        c.gridy++;
377
        c.weightx = 0;
156 ilm 378
        this.add(getLabelBoldFor(textTotalTVA.getField(), TM.tr("TotalPanel.total.VAT")), c); //$NON-NLS-1$
18 ilm 379
        c.gridx++;
380
        c.weightx = 1;
381
        this.add(textTotalTVA, c);
382
 
383
        // Sep
384
        c.gridwidth = 2;
385
        c.gridx = 4;
386
        c.gridy++;
387
        c.weightx = 1;
388
        c.fill = GridBagConstraints.BOTH;
389
        this.add(createSeparator(), c);
390
 
61 ilm 391
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 392
            // devise
393
            c.gridwidth = 1;
394
            c.gridx = 4;
395
            c.gridy++;
396
            c.weightx = 0;
397
            c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 398
            this.add(getLabelBoldFor(textTotalDevise.getField(), TM.tr("TotalPanel.currencyTotal")), c); //$NON-NLS-1$
19 ilm 399
            c.gridx++;
400
            c.weightx = 1;
401
            textTotalDevise.setFont(textTotalHT.getFont());
402
            this.add(textTotalDevise, c);
403
        }
142 ilm 404
 
405
        // Eco-contribution
406
        if (showEco) {
407
            c.gridx = 4;
408
            c.gridy++;
409
            c.weightx = 0;
156 ilm 410
            this.add(new JLabel(TM.tr("TotalPanel.ecoTax")), c); //$NON-NLS-1$
142 ilm 411
            c.gridx++;
412
            c.weightx = 1;
413
            this.add(this.textTotalEco, c);
414
        }
415
 
18 ilm 416
        // TTC
417
        c.gridwidth = 1;
418
        c.gridx = 4;
419
        c.gridy++;
420
        c.weightx = 0;
421
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 422
        this.add(getLabelBoldFor(textTotalTTC.getField(), TM.tr("TotalPanel.total.taxIncluded")), c); //$NON-NLS-1$
18 ilm 423
        c.gridx++;
424
        c.weightx = 1;
425
        textTotalTTC.setFont(textTotalHT.getFont());
426
        this.add(textTotalTTC, c);
427
 
428
        updateTotal();
429
        this.table.getRowValuesTableModel().addTableModelListener(this);
430
        this.table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
431
            public void valueChanged(ListSelectionEvent e) {
432
                updateTotal();
433
            }
434
        });
435
    }
436
 
156 ilm 437
    public void setTextFraisDoc(DeviseField textFraisDoc, SQLRequestComboBox boxTVA) {
438
        this.textFraisDoc = textFraisDoc;
439
        this.selFraisDocTVA = boxTVA;
440
    }
441
 
174 ilm 442
    public void setTextFixTVA(JTextField textTVAFix) {
443
        this.textTVAFix = textTVAFix;
444
    }
445
 
142 ilm 446
    public void setIntraComm(boolean intraComm) {
447
        this.intraComm = intraComm;
448
        updateTotal();
449
    }
450
 
80 ilm 451
    private boolean achat = false;
452
 
453
    public void setAchat(boolean b) {
454
        this.achat = b;
455
    }
456
 
18 ilm 457
    private void reconfigure(JTextField field) {
458
        field.setEditable(false);
459
        field.setHorizontalAlignment(JTextField.RIGHT);
460
        field.setBorder(null);
461
        field.setColumns(11);
93 ilm 462
        field.setOpaque(false);
18 ilm 463
        field.setMinimumSize(new Dimension(150, 20));
464
        field.setPreferredSize(new Dimension(150, 20));
465
        field.setBackground(UIManager.getColor("control"));
466
        field.setEditable(false);
467
        field.setEnabled(false);
468
        field.setDisabledTextColor(Color.BLACK);
469
    }
470
 
471
    public void tableChanged(TableModelEvent e) {
73 ilm 472
        updateTotal();
18 ilm 473
    }
474
 
67 ilm 475
    private static String CLEAR = "";
144 ilm 476
    private SwingWorker<TotalCalculator, Object> worker;
67 ilm 477
 
478
    private void clearTextField() {
479
 
480
        if (textTotalDevise != null) {
481
            textTotalDevise.setText(CLEAR);
482
            textTotalDeviseSel.setText(CLEAR);
483
        }
484
 
485
        textPoids.setText(CLEAR);
486
        textTotalHT.setText(CLEAR);
487
        textService.setText(CLEAR);
488
        textTotalTVA.setText(CLEAR);
489
        textTotalTTC.setText(CLEAR);
490
        textTotalHTSel.setText(CLEAR);
491
        textServiceSel.setText(CLEAR);
492
        textTotalTVASel.setText(CLEAR);
493
        textTotalTTCSel.setText(CLEAR);
494
        if (gestionHA) {
495
            textHA.setText(CLEAR);
496
            marge.setText(CLEAR);
497
            textHASel.setText(CLEAR);
498
            margeSel.setText(CLEAR);
499
        }
500
    }
501
 
93 ilm 502
    private SQLRowAccessor defaultRowCompteProduit = null;
503
 
504
    public void setDefaultCompteProduit(SQLRowAccessor row) {
505
        this.defaultRowCompteProduit = row;
506
    }
507
 
156 ilm 508
    private SQLRowAccessor rowCatComptable = null;
509
 
510
    public void setCategorieComptable(SQLRowAccessor row) {
511
        this.rowCatComptable = row;
512
    }
513
 
18 ilm 514
    /**
515
     *
516
     */
517
    public void updateTotal() {
518
 
67 ilm 519
        final long valRemiseHT;
174 ilm 520
        final BigDecimal valPortHT, fraisDoc, tvaFix;
61 ilm 521
 
67 ilm 522
        clearTextField();
83 ilm 523
        // Clone rows to be thread safe
524
        final List<SQLRowValues> vals = articleTable.getRowValuesAtLevel(1);
525
        final List<SQLRowValues> list = new ArrayList<SQLRowValues>(vals.size());
526
        for (SQLRowValues sqlRowValues : vals) {
527
            list.add(sqlRowValues.asRowValues());
528
        }
61 ilm 529
 
18 ilm 530
 
67 ilm 531
        final TotalCalculatorParameters params = new TotalCalculatorParameters(list);
63 ilm 532
 
67 ilm 533
        // Total Service
534
        String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
535
        Boolean bServiceActive = Boolean.valueOf(val);
18 ilm 536
 
67 ilm 537
        final int[] selectedRows = this.table.getSelectedRows();
18 ilm 538
 
67 ilm 539
        // Remise à inclure
63 ilm 540
 
67 ilm 541
        // FIXME Remise à appliquer en plus sur les frais de ports??
542
        if (this.textRemiseHT.getText().trim().length() > 0) {
543
            if (!this.textRemiseHT.getText().trim().equals("-")) {
544
                valRemiseHT = GestionDevise.parseLongCurrency(this.textRemiseHT.getText().trim());
545
            } else {
546
                valRemiseHT = 0;
547
            }
548
        } else {
549
            valRemiseHT = 0;
550
        }
551
        params.setRemiseHT(valRemiseHT);
63 ilm 552
 
67 ilm 553
        // Frais de port à inclure
554
        if (textPortHT.getText().trim().length() > 0) {
555
            if (!textPortHT.getText().trim().equals("-")) {
556
                long p = GestionDevise.parseLongCurrency(textPortHT.getText().trim());
557
                valPortHT = new BigDecimal(p).movePointLeft(2);
558
            } else {
559
                valPortHT = BigDecimal.ZERO;
560
            }
561
        } else {
562
            valPortHT = BigDecimal.ZERO;
563
        }
564
        params.setPortHT(valPortHT);
63 ilm 565
 
67 ilm 566
        final SQLRow tvaPort = selPortTVA == null ? null : selPortTVA.getSelectedRow();
567
        final SQLRowValues rowValsPort;
568
        // TVA Port inclus
73 ilm 569
        if (tvaPort != null && valPortHT.signum() != 0 && !tvaPort.isUndefined()) {
67 ilm 570
            rowValsPort = new SQLRowValues(articleTable.getSQLElement().getTable());
571
            rowValsPort.put(articleTable.getPrixTotalHTElement().getField().getName(), valPortHT);
572
            rowValsPort.put("QTE", 1);
73 ilm 573
            rowValsPort.put("ID_TAXE", tvaPort.getIDNumber());
94 ilm 574
 
575
            final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE");
576
            final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
577
            SQLRow rowDefaultCptPort;
578
            if (tvaPort.getFloat("TAUX") > 0) {
579
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_SOUMIS");
580
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
581
                    try {
582
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteSoumisTVA");
583
                    } catch (Exception e) {
584
                        e.printStackTrace();
585
                    }
586
                }
587
            } else {
588
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_NON_SOUMIS");
589
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
590
                    try {
591
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteNonSoumisTVA");
592
                    } catch (Exception e) {
593
                        e.printStackTrace();
594
                    }
595
                }
596
            }
149 ilm 597
            final SQLRowValues putRowValues = rowValsPort.putRowValues("ID_ARTICLE");
598
            putRowValues.put("ID_COMPTE_PCE", rowDefaultCptPort.getID());
599
            putRowValues.put("ID_COMPTE_PCE_ACHAT", rowDefaultCptPort.getID());
67 ilm 600
        } else {
601
            rowValsPort = null;
602
        }
156 ilm 603
        // Frais documents
604
        if (textFraisDoc.getText().trim().length() > 0) {
605
            if (!textFraisDoc.getText().trim().equals("-")) {
606
                long p = GestionDevise.parseLongCurrency(textFraisDoc.getText().trim());
607
                fraisDoc = new BigDecimal(p).movePointLeft(2);
608
            } else {
609
                fraisDoc = BigDecimal.ZERO;
610
            }
611
        } else {
612
            fraisDoc = BigDecimal.ZERO;
613
        }
614
        final SQLRow tvafraisDoc = selFraisDocTVA == null ? null : selFraisDocTVA.getSelectedRow();
615
        final SQLRowValues rowValsFraisDoc;
616
        if (tvafraisDoc != null && fraisDoc.signum() != 0 && !tvafraisDoc.isUndefined()) {
617
            rowValsFraisDoc = new SQLRowValues(articleTable.getSQLElement().getTable());
618
            rowValsFraisDoc.put(articleTable.getPrixTotalHTElement().getField().getName(), fraisDoc);
619
            rowValsFraisDoc.put("QTE", 1);
620
            rowValsFraisDoc.put("ID_TAXE", tvafraisDoc.getIDNumber());
621
            rowValsFraisDoc.put("SERVICE", Boolean.TRUE);
622
        } else {
623
            rowValsFraisDoc = null;
624
        }
18 ilm 625
 
174 ilm 626
        // Fix TVA si écart avec facture fournisseur
627
        if (textTVAFix.getText().trim().length() > 0) {
628
            if (!textTVAFix.getText().trim().equals("-")) {
629
                long p = GestionDevise.parseLongCurrency(textTVAFix.getText().trim());
630
                tvaFix = new BigDecimal(p).movePointLeft(2);
631
            } else {
632
                tvaFix = BigDecimal.ZERO;
633
            }
634
        } else {
635
            tvaFix = BigDecimal.ZERO;
636
        }
637
 
67 ilm 638
        final Boolean isServiceActive = bServiceActive;
90 ilm 639
        if (worker != null) {
640
            worker.cancel(true);
641
        }
144 ilm 642
        worker = new SwingWorker<TotalCalculator, Object>() {
19 ilm 643
 
67 ilm 644
            @Override
645
            protected TotalCalculator doInBackground() throws Exception {
90 ilm 646
                Thread.sleep(100);
67 ilm 647
                params.fetchArticle();
18 ilm 648
 
73 ilm 649
                SQLTableElement tableElementTotalDevise = articleTable.getTableElementTotalDevise();
650
                String fieldDevise = (tableElementTotalDevise == null ? null : tableElementTotalDevise.getField().getName());
18 ilm 651
 
73 ilm 652
                SQLTableElement tableElementTotalHA = (articleTable.getPrebilanElement() == null) ? articleTable.getTotalHaElement() : articleTable.getPrebilanElement();
653
                String fieldHA = (tableElementTotalHA == null ? null : tableElementTotalHA.getField().getName());
654
                SQLTableElement tableElementTotalHT = articleTable.getPrixTotalHTElement();
655
                String fieldHT = (tableElementTotalHT == null ? null : tableElementTotalHT.getField().getName());
18 ilm 656
 
156 ilm 657
                final TotalCalculator calc = new TotalCalculator(fieldHA, fieldHT, fieldDevise, achat, null, rowCatComptable);
142 ilm 658
                calc.setIntraComm(intraComm);
93 ilm 659
                if (defaultRowCompteProduit != null) {
660
                    calc.setRowDefaultCptProduit(defaultRowCompteProduit);
661
                }
67 ilm 662
                calc.setSelectedRows(selectedRows);
18 ilm 663
 
67 ilm 664
                // Calcul avant remise
665
                final BigDecimal totalHTAvtremise;
666
 
73 ilm 667
                final int size = list.size();
668
                calc.setServiceActive(isServiceActive);
67 ilm 669
                if (valRemiseHT != 0) {
670
 
73 ilm 671
                    for (int i = 0; i < size; i++) {
67 ilm 672
                        SQLRowValues rowVals = list.get(i);
673
                        calc.addLine(rowVals, params.getMapArticle().get(rowVals.getInt("ID_ARTICLE")), i, false);
674
                    }
675
 
676
                    // TVA Port inclus
677
                    // if (this.selPortTVA != null && !valPortHT.equals(BigDecimal.ZERO)) {
678
                    //
679
                    // SQLRow tvaPort = this.selPortTVA.getSelectedRow();
680
                    // if (tvaPort != null) {
681
                    // calc.addLine(-1, valPortHT, null, null, 1, 0, false, tvaPort, false);
682
                    // }
683
                    // }
684
                    totalHTAvtremise = calc.getTotalHT();
18 ilm 685
                } else {
67 ilm 686
                    totalHTAvtremise = BigDecimal.ZERO;
18 ilm 687
                }
688
 
67 ilm 689
                calc.initValues();
73 ilm 690
                calc.setSelectedRows(selectedRows);
142 ilm 691
                long valRemiseHTReel = valRemiseHT;
692
                if (facturable != null && facturable.getValue() != null) {
693
                    if (facturable.getValue().getMontant() != null && facturable.getValue().getMontant().signum() > 0) {
694
                        valRemiseHTReel = 0;
695
                    } else if (facturable.getValue().getPercent() != null && facturable.getValue().getPercent().signum() > 0) {
696
                        valRemiseHTReel = facturable.getValue().getPercent().movePointLeft(2).multiply(new BigDecimal(valRemiseHT)).setScale(0, RoundingMode.HALF_UP).longValue();
697
                    }
18 ilm 698
 
142 ilm 699
                }
700
                calc.setRemise(valRemiseHTReel, totalHTAvtremise);
701
 
90 ilm 702
                if (Thread.currentThread().isInterrupted()) {
703
                    throw new InterruptedException();
704
                }
65 ilm 705
 
90 ilm 706
 
67 ilm 707
                // Total des elements
73 ilm 708
                int rowCount = size;
67 ilm 709
                for (int i = 0; i < rowCount; i++) {
710
                    SQLRowValues values = list.get(i);
73 ilm 711
 
712
                    Object id = values.getObject("ID_ARTICLE");
713
                    calc.addLine(values, (id == null) ? null : params.getMapArticle().get(id), i, i == (rowCount - 1));
65 ilm 714
                }
715
 
67 ilm 716
                // TVA Port inclus
717
                if (rowValsPort != null) {
94 ilm 718
                    calc.addLine(rowValsPort, rowValsPort.getForeign("ID_ARTICLE"), 0, false);
65 ilm 719
                }
61 ilm 720
 
156 ilm 721
                if (rowValsFraisDoc != null) {
722
                    calc.addLine(rowValsFraisDoc, null, 0, false);
723
                }
724
 
174 ilm 725
                calc.addTVAAdjust(tvaFix);
726
 
67 ilm 727
                // Verification du resultat ht +tva = ttc
728
                calc.checkResult();
90 ilm 729
                if (Thread.currentThread().isInterrupted()) {
730
                    throw new InterruptedException();
731
                }
67 ilm 732
                return calc;
19 ilm 733
            }
61 ilm 734
 
67 ilm 735
            @Override
736
            protected void done() {
91 ilm 737
                // if cancel() is called after doInBackground() nothing happens
738
                // but updating is set to a new instance
739
                if (this.isCancelled() || TotalPanel.this.worker != this) {
740
                    // une autre maj arrive
90 ilm 741
                    return;
742
                }
67 ilm 743
                TotalCalculator calc;
744
                try {
745
                    calc = get();
61 ilm 746
 
67 ilm 747
                    BigDecimal totalHT = calc.getTotalHT();
18 ilm 748
 
67 ilm 749
                    if (textTotalDevise != null) {
750
                        textTotalDevise.setText(GestionDevise.currencyToString(calc.getTotalDevise().setScale(2, RoundingMode.HALF_UP)));
751
                        textTotalDeviseSel.setText(GestionDevise.currencyToString(calc.getTotalDeviseSel().setScale(2, RoundingMode.HALF_UP)));
21 ilm 752
                    }
18 ilm 753
 
67 ilm 754
                    textPoids.setText(String.valueOf(calc.getTotalPoids()));
18 ilm 755
 
142 ilm 756
                    textTotalEco.setText(GestionDevise.currencyToString(calc.getTotalEco()));
757
                    textTotalEcoSel.setText(GestionDevise.currencyToString(calc.getTotalEcoSel()));
67 ilm 758
                    textTotalHT.setText(GestionDevise.currencyToString(totalHT));
759
                    textService.setText(GestionDevise.currencyToString(calc.getTotalService().setScale(2, RoundingMode.HALF_UP)));
760
                    textTotalTVA.setText(GestionDevise.currencyToString(calc.getTotalTVA().setScale(2, RoundingMode.HALF_UP)));
761
                    textTotalTTC.setText(GestionDevise.currencyToString(calc.getTotalTTC().setScale(2, RoundingMode.HALF_UP)));
762
                    BigDecimal totalHTSel = calc.getTotalHTSel();
763
                    textTotalHTSel.setText(GestionDevise.currencyToString(totalHTSel.setScale(2, RoundingMode.HALF_UP)));
764
                    textServiceSel.setText(GestionDevise.currencyToString(calc.getTotalServiceSel().setScale(2, RoundingMode.HALF_UP)));
18 ilm 765
 
67 ilm 766
                    textTotalTVASel.setText(GestionDevise.currencyToString(calc.getTotalTVASel().setScale(2, RoundingMode.HALF_UP)));
767
                    textTotalTTCSel.setText(GestionDevise.currencyToString(calc.getTotalTTCSel().setScale(2, RoundingMode.HALF_UP)));
768
                    if (gestionHA) {
769
                        BigDecimal totalHA = calc.getTotalHA();
770
                        textHA.setText(GestionDevise.currencyToString(totalHA.setScale(2, RoundingMode.HALF_UP)));
18 ilm 771
 
67 ilm 772
                        BigDecimal m = BigDecimal.ZERO;
773
                        BigDecimal d = BigDecimal.ZERO;
142 ilm 774
 
775
                        d = totalHT.subtract(totalHA);
776
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHT.signum() != 0) {
777
                            m = d.divide(totalHT, DecimalUtils.HIGH_PRECISION).movePointRight(2);
778
                        } else if (totalHA.signum() != 0) {
779
                            m = d.divide(totalHA, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 780
                        }
142 ilm 781
 
67 ilm 782
                        if (d.compareTo(BigDecimal.ZERO) <= 0) {
783
                            marge.setForeground(Color.red);
784
                            marge.setDisabledTextColor(Color.RED);
785
                        } else {
786
                            marge.setForeground(textTotalTTC.getForeground());
787
                            marge.setDisabledTextColor(textTotalTTC.getForeground());
788
                        }
789
                        marge.setText("(" + m.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(d.setScale(2, RoundingMode.HALF_UP)));
18 ilm 790
 
67 ilm 791
                        BigDecimal totalHASel = calc.getTotalHASel();
792
                        textHASel.setText(GestionDevise.currencyToString(totalHASel.setScale(2, RoundingMode.HALF_UP)));
793
 
794
                        BigDecimal m2 = BigDecimal.ZERO;
795
                        BigDecimal e = BigDecimal.ZERO;
142 ilm 796
 
797
                        e = totalHTSel.subtract(totalHASel);
798
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHTSel.signum() != 0) {
799
                            m2 = e.divide(totalHTSel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
800
                        } else if (totalHASel.signum() != 0) {
801
                            m2 = e.divide(totalHASel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 802
                        }
142 ilm 803
 
67 ilm 804
                        margeSel.setText("(" + m2.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(e.setScale(2, RoundingMode.HALF_UP)));
805
                        if (e.compareTo(BigDecimal.ZERO) <= 0) {
806
                            margeSel.setForeground(Color.red);
807
                            margeSel.setDisabledTextColor(Color.RED);
808
                        } else {
809
                            margeSel.setForeground(textTotalTTC.getForeground());
810
                            margeSel.setDisabledTextColor(textTotalTTC.getForeground());
811
                        }
812
 
813
                    }
814
                    supp.firePropertyChange("value", null, null);
73 ilm 815
                } catch (Exception e1) {
67 ilm 816
                    ExceptionHandler.handle("", e1);
817
                }
18 ilm 818
            }
819
 
67 ilm 820
        };
821
        worker.execute();
18 ilm 822
    }
823
 
80 ilm 824
    public long getTotalHT() {
825
        return GestionDevise.parseLongCurrency(this.textTotalHT.getText());
826
    }
827
 
828
    private static final JLabel getLabelFor(SQLField field, String defaultLabel) {
829
        if (field == null) {
830
            return new JLabel(defaultLabel);
831
        }
18 ilm 832
        return new JLabel(Configuration.getInstance().getTranslator().getLabelFor(field));
833
 
834
    }
835
 
80 ilm 836
    private static final JLabel getLabelBoldFor(SQLField field, String defaultLabel) {
837
        if (field == null) {
838
            return new JLabelBold(defaultLabel);
839
        }
18 ilm 840
        return new JLabelBold(Configuration.getInstance().getTranslator().getLabelFor(field));
841
 
842
    }
843
 
844
    public void addValueListener(PropertyChangeListener listener) {
845
        this.supp.addPropertyChangeListener(listener);
846
    }
847
 
848
    public void removeValueListener(PropertyChangeListener listener) {
849
        this.supp.removePropertyChangeListener(listener);
850
    }
851
 
852
    private final JSeparator createSeparator() {
853
        final JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
854
        DefaultGridBagConstraints.lockMinimumSize(sep);
855
        return sep;
856
    }
857
}