OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | 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;
19 ilm 68
    private JTextField textPoids;
69
    private DeviseField textTotalDevise, textTotalDeviseSel;
18 ilm 70
    private DeviseField textService, textServiceSel;
71
    private DeviseField textHA, textHASel;
72
    private JTextField marge, margeSel;
156 ilm 73
    private DeviseField textFraisDoc, textFraisDocSel;
18 ilm 74
    private boolean gestionHA = false;
75
    private PropertyChangeSupport supp;
76
    private int columnIndexEchHT = -1;
77
    private int columnIndexEchTTC = -1;
73 ilm 78
    private SQLTableElement ha;
65 ilm 79
    private SQLRequestComboBox selPortTVA;
156 ilm 80
    private SQLRequestComboBox selFraisDocTVA = null;
142 ilm 81
    private AcompteField facturable;
82
    private boolean intraComm = false;
18 ilm 83
 
67 ilm 84
    AbstractArticleItemTable articleTable;
85
 
142 ilm 86
    public TotalPanel(AbstractArticleItemTable articleItemTable, DeviseField textTotalEco, DeviseField textTotalHT, DeviseField textTotalTVA, DeviseField textTotalTTC, DeviseField textPortHT,
87
            DeviseField textRemiseHT, DeviseField textService, DeviseField textTotalHA, DeviseField textTotalDevise, JTextField textTotalPoids, JPanel tableEchantillon) {
88
        this(articleItemTable, textTotalEco, textTotalHT, textTotalTVA, textTotalTTC, textPortHT, textRemiseHT, textService, textTotalHA, textTotalDevise, textTotalPoids, tableEchantillon, null,
89
                null);
65 ilm 90
    }
18 ilm 91
 
142 ilm 92
    public TotalPanel(AbstractArticleItemTable articleItemTable, DeviseField textTotalEco, DeviseField textTotalHT, DeviseField textTotalTVA, DeviseField textTotalTTC, DeviseField textPortHT,
93
            DeviseField textRemiseHT, DeviseField textService, DeviseField textTotalHA, DeviseField textTotalDevise, JTextField textTotalPoids, JPanel tableEchantillon, SQLRequestComboBox selPortTva,
94
            AcompteField facturable) {
65 ilm 95
 
18 ilm 96
        super();
93 ilm 97
        this.setOpaque(false);
65 ilm 98
        this.selPortTVA = selPortTva;
142 ilm 99
        this.facturable = facturable;
67 ilm 100
        this.articleTable = articleItemTable;
18 ilm 101
        this.supp = new PropertyChangeSupport(this);
61 ilm 102
        this.table = articleItemTable.getRowValuesTable();
67 ilm 103
 
104
        this.ha = (articleItemTable.getPrebilanElement() == null) ? articleItemTable.getHaElement() : articleItemTable.getPrebilanElement();
61 ilm 105
        this.gestionHA = ha != null && articleItemTable.getQteElement() != null;
18 ilm 106
 
61 ilm 107
        this.textPoids = (textTotalPoids == null ? new JTextField() : textTotalPoids);
18 ilm 108
        this.textTotalHT = textTotalHT;
109
        this.textTotalHT.setBold();
142 ilm 110
        this.textTotalEco = textTotalEco;
18 ilm 111
        this.textTotalTVA = textTotalTVA;
112
        this.textTotalTTC = textTotalTTC;
113
        this.textPortHT = textPortHT;
114
        this.textRemiseHT = textRemiseHT;
115
        this.textService = textService;
156 ilm 116
        this.textFraisDoc = new DeviseField();
117
        this.textFraisDocSel = new DeviseField();
18 ilm 118
        this.textTotalHTSel = new DeviseField(true);
119
        this.textServiceSel = new DeviseField();
142 ilm 120
        this.textTotalEcoSel = new DeviseField();
18 ilm 121
        this.textTotalTTCSel = new DeviseField();
122
        this.textTotalTVASel = new DeviseField();
94 ilm 123
        this.textHA = (textTotalHA == null) ? new DeviseField() : textTotalHA;
124
        this.textHASel = new DeviseField();
18 ilm 125
        this.marge = new JTextField();
126
        this.margeSel = new JTextField();
144 ilm 127
        final UserRights rights = UserRightsManager.getCurrentUserRights();
67 ilm 128
 
94 ilm 129
        final boolean showHA = rights.haveRight(AbstractVenteArticleItemTable.SHOW_PRIX_ACHAT_CODE);
130
        this.textHA.setVisible(showHA);
131
        this.textHASel.setVisible(showHA);
132
        this.marge.setVisible(showHA);
133
        this.margeSel.setVisible(showHA);
134
 
61 ilm 135
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 136
            this.textTotalDevise = textTotalDevise;
137
            this.textTotalDeviseSel = new DeviseField();
138
            reconfigure(this.textTotalDevise);
139
            reconfigure(this.textTotalDeviseSel);
140
        }
18 ilm 141
        reconfigure(this.textTotalHT);
142 ilm 142
        reconfigure(this.textTotalEco);
18 ilm 143
        reconfigure(this.textTotalTVA);
144
        reconfigure(this.textTotalTTC);
145
        reconfigure(this.textService);
146
        reconfigure(this.textHA);
147
        reconfigure(this.marge);
148
        reconfigure(this.textTotalHTSel);
156 ilm 149
        reconfigure(this.textFraisDoc);
150
        reconfigure(this.textFraisDocSel);
142 ilm 151
        reconfigure(this.textTotalEcoSel);
18 ilm 152
        reconfigure(this.textTotalTVASel);
153
        reconfigure(this.textTotalTTCSel);
154
        reconfigure(this.textServiceSel);
155
        reconfigure(this.textHASel);
156
        reconfigure(this.margeSel);
157
 
67 ilm 158
        // String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
159
        // Boolean b = Boolean.valueOf(val);
18 ilm 160
 
67 ilm 161
        // if (this.columnIndexHT < 0 || this.columnIndexTVA < 0 || (b != null && b.booleanValue()
162
        // && this.columnIndexService < 0)) {
163
        // throw new IllegalArgumentException("Impossible de trouver la colonne de " +
164
        // articleItemTable.getPrixTotalHTElement() + " / " + articleItemTable.getTVAElement() +
165
        // " / "
166
        // + articleItemTable.getPrixServiceElement());
167
        // }
18 ilm 168
        this.setLayout(new GridBagLayout());
169
        GridBagConstraints c = new DefaultGridBagConstraints();
170
 
171
        c.gridheight = 1;
172
        c.weighty = 0;
173
        c.fill = GridBagConstraints.HORIZONTAL;
174
 
175
        // Collone 1 : Selection
176
        c.gridx++;
156 ilm 177
        this.add(new JLabelBold(TM.tr("TotalPanel.selection")), c); //$NON-NLS-1$
18 ilm 178
        c.gridy++;
179
        c.gridx = 1;
180
        c.gridwidth = 2;
181
        c.weightx = 1;
182
        c.fill = GridBagConstraints.BOTH;
183
        this.add(createSeparator(), c);
184
 
185
        c.gridwidth = 1;
186
        c.weightx = 0;
187
        c.fill = GridBagConstraints.HORIZONTAL;
188
        if (this.gestionHA) {
156 ilm 189
            final JLabel labelMarge = new JLabel(TM.tr("TotalPanel.margin")); //$NON-NLS-1$
190
            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 191
 
192
            // Total HA HT
193
            c.gridy++;
94 ilm 194
            this.add(labelHA, c);
195
            labelHA.setVisible(showHA);
18 ilm 196
 
197
            c.gridx++;
198
            c.weightx = 1;
199
            this.add(this.textHASel, c);
200
 
201
            // Marge
202
            c.gridy++;
203
            c.gridx = 1;
204
            c.weightx = 0;
94 ilm 205
            this.add(labelMarge, c);
206
            labelMarge.setVisible(showHA);
18 ilm 207
 
208
            c.gridx++;
209
            c.weightx = 1;
210
            this.add(this.margeSel, c);
211
 
212
            c.gridy++;
213
            c.gridx = 1;
214
            c.gridwidth = 2;
215
            c.weightx = 1;
216
 
217
            c.fill = GridBagConstraints.BOTH;
218
            this.add(createSeparator(), c);
219
 
220
            c.gridwidth = 1;
221
            c.weightx = 0;
222
        }
223
 
224
        // Total HT
225
        c.gridy++;
226
        c.gridx = 1;
227
        c.weightx = 0;
228
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 229
        this.add(getLabelBoldFor(textTotalHT.getField(), TM.tr("TotalPanel.total.taxExcluded")), c); //$NON-NLS-1$
18 ilm 230
 
231
        c.gridx++;
232
        c.weightx = 1;
233
        this.add(this.textTotalHTSel, c);
234
 
235
        // Service
28 ilm 236
        if (DefaultNXProps.getInstance().getBooleanValue(AbstractVenteArticleItemTable.ARTICLE_SERVICE, false)) {
237
            c.gridx = 1;
238
            c.gridy++;
239
            c.weightx = 0;
156 ilm 240
            this.add(new JLabel(TM.tr("TotalPanel.service.taxExcluded")), c); //$NON-NLS-1$
28 ilm 241
            c.gridx++;
242
            c.weightx = 1;
243
            this.add(this.textServiceSel, c);
244
        }
18 ilm 245
 
246
        // TVA
247
        c.gridx = 1;
248
        c.gridy++;
249
        c.weightx = 0;
156 ilm 250
        this.add(getLabelFor(textTotalTVA.getField(), TM.tr("TotalPanel.total.VAT")), c); //$NON-NLS-1$
18 ilm 251
        c.gridx++;
252
        c.weightx = 1;
253
        this.add(this.textTotalTVASel, c);
254
 
255
        c.gridx = 1;
256
        c.gridy++;
257
        c.gridwidth = 2;
258
        c.fill = GridBagConstraints.BOTH;
259
        this.add(createSeparator(), c);
260
 
61 ilm 261
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 262
            // Devise
263
            c.gridwidth = 1;
264
            c.gridx = 1;
265
            c.gridy++;
266
            c.weightx = 0;
267
            c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 268
            this.add(getLabelFor(textTotalDevise.getField(), TM.tr("TotalPanel.currencyTotal")), c); //$NON-NLS-1$
19 ilm 269
            c.gridx++;
270
            c.weightx = 1;
271
            this.add(this.textTotalDeviseSel, c);
272
        }
142 ilm 273
 
274
        // Eco-contribution
275
        final SQLPreferences prefs = SQLPreferences.getMemCached(this.table.getRowValuesTableModel().getSQLElement().getTable().getDBRoot());
276
        final boolean showEco = prefs.getBoolean(AbstractVenteArticleItemTable.SHOW_TOTAL_ECO_CONTRIBUTION, false);
277
        if (showEco) {
278
            c.gridx = 1;
279
            c.gridy++;
280
            c.weightx = 0;
156 ilm 281
            this.add(new JLabel(TM.tr("TotalPanel.ecoTax")), c); //$NON-NLS-1$
142 ilm 282
            c.gridx++;
283
            c.weightx = 1;
284
            this.add(this.textTotalEcoSel, c);
285
        }
286
 
18 ilm 287
        // TTC
288
        c.gridwidth = 1;
289
        c.gridx = 1;
290
        c.gridy++;
291
        c.weightx = 0;
292
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 293
        this.add(getLabelFor(textTotalTTC.getField(), TM.tr("TotalPanel.total.taxIncluded")), c); //$NON-NLS-1$
18 ilm 294
        c.gridx++;
295
        c.weightx = 1;
296
        this.add(this.textTotalTTCSel, c);
297
 
298
        // Global
299
        c.gridx = 3;
300
        c.gridy = 0;
301
        c.gridheight = GridBagConstraints.REMAINDER;
302
        c.weighty = 1;
303
        c.fill = GridBagConstraints.VERTICAL;
304
        c.weightx = 0;
305
        this.add(createSeparator(), c);
306
 
307
        c.gridheight = 1;
308
        c.fill = GridBagConstraints.HORIZONTAL;
309
        c.weighty = 0;
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$
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
 
142 ilm 442
    public void setIntraComm(boolean intraComm) {
443
        this.intraComm = intraComm;
444
        updateTotal();
445
    }
446
 
80 ilm 447
    private boolean achat = false;
448
 
449
    public void setAchat(boolean b) {
450
        this.achat = b;
451
    }
452
 
18 ilm 453
    private void reconfigure(JTextField field) {
454
        field.setEditable(false);
455
        field.setHorizontalAlignment(JTextField.RIGHT);
456
        field.setBorder(null);
457
        field.setColumns(11);
93 ilm 458
        field.setOpaque(false);
18 ilm 459
        field.setMinimumSize(new Dimension(150, 20));
460
        field.setPreferredSize(new Dimension(150, 20));
461
        field.setBackground(UIManager.getColor("control"));
462
        field.setEditable(false);
463
        field.setEnabled(false);
464
        field.setDisabledTextColor(Color.BLACK);
465
    }
466
 
467
    public void tableChanged(TableModelEvent e) {
73 ilm 468
        updateTotal();
18 ilm 469
    }
470
 
67 ilm 471
    private static String CLEAR = "";
144 ilm 472
    private SwingWorker<TotalCalculator, Object> worker;
67 ilm 473
 
474
    private void clearTextField() {
475
 
476
        if (textTotalDevise != null) {
477
            textTotalDevise.setText(CLEAR);
478
            textTotalDeviseSel.setText(CLEAR);
479
        }
480
 
481
        textPoids.setText(CLEAR);
482
        textTotalHT.setText(CLEAR);
483
        textService.setText(CLEAR);
484
        textTotalTVA.setText(CLEAR);
485
        textTotalTTC.setText(CLEAR);
486
        textTotalHTSel.setText(CLEAR);
487
        textServiceSel.setText(CLEAR);
488
        textTotalTVASel.setText(CLEAR);
489
        textTotalTTCSel.setText(CLEAR);
490
        if (gestionHA) {
491
            textHA.setText(CLEAR);
492
            marge.setText(CLEAR);
493
            textHASel.setText(CLEAR);
494
            margeSel.setText(CLEAR);
495
        }
496
    }
497
 
93 ilm 498
    private SQLRowAccessor defaultRowCompteProduit = null;
499
 
500
    public void setDefaultCompteProduit(SQLRowAccessor row) {
501
        this.defaultRowCompteProduit = row;
502
    }
503
 
156 ilm 504
    private SQLRowAccessor rowCatComptable = null;
505
 
506
    public void setCategorieComptable(SQLRowAccessor row) {
507
        this.rowCatComptable = row;
508
    }
509
 
18 ilm 510
    /**
511
     *
512
     */
513
    public void updateTotal() {
514
 
67 ilm 515
        final long valRemiseHT;
156 ilm 516
        final BigDecimal valPortHT, fraisDoc;
61 ilm 517
 
67 ilm 518
        clearTextField();
83 ilm 519
        // Clone rows to be thread safe
520
        final List<SQLRowValues> vals = articleTable.getRowValuesAtLevel(1);
521
        final List<SQLRowValues> list = new ArrayList<SQLRowValues>(vals.size());
522
        for (SQLRowValues sqlRowValues : vals) {
523
            list.add(sqlRowValues.asRowValues());
524
        }
61 ilm 525
 
18 ilm 526
 
67 ilm 527
        final TotalCalculatorParameters params = new TotalCalculatorParameters(list);
63 ilm 528
 
67 ilm 529
        // Total Service
530
        String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
531
        Boolean bServiceActive = Boolean.valueOf(val);
18 ilm 532
 
67 ilm 533
        final int[] selectedRows = this.table.getSelectedRows();
18 ilm 534
 
67 ilm 535
        // Remise à inclure
63 ilm 536
 
67 ilm 537
        // FIXME Remise à appliquer en plus sur les frais de ports??
538
        if (this.textRemiseHT.getText().trim().length() > 0) {
539
            if (!this.textRemiseHT.getText().trim().equals("-")) {
540
                valRemiseHT = GestionDevise.parseLongCurrency(this.textRemiseHT.getText().trim());
541
            } else {
542
                valRemiseHT = 0;
543
            }
544
        } else {
545
            valRemiseHT = 0;
546
        }
547
        params.setRemiseHT(valRemiseHT);
63 ilm 548
 
67 ilm 549
        // Frais de port à inclure
550
        if (textPortHT.getText().trim().length() > 0) {
551
            if (!textPortHT.getText().trim().equals("-")) {
552
                long p = GestionDevise.parseLongCurrency(textPortHT.getText().trim());
553
                valPortHT = new BigDecimal(p).movePointLeft(2);
554
            } else {
555
                valPortHT = BigDecimal.ZERO;
556
            }
557
        } else {
558
            valPortHT = BigDecimal.ZERO;
559
        }
560
        params.setPortHT(valPortHT);
63 ilm 561
 
67 ilm 562
        final SQLRow tvaPort = selPortTVA == null ? null : selPortTVA.getSelectedRow();
563
        final SQLRowValues rowValsPort;
564
        // TVA Port inclus
73 ilm 565
        if (tvaPort != null && valPortHT.signum() != 0 && !tvaPort.isUndefined()) {
67 ilm 566
            rowValsPort = new SQLRowValues(articleTable.getSQLElement().getTable());
567
            rowValsPort.put(articleTable.getPrixTotalHTElement().getField().getName(), valPortHT);
568
            rowValsPort.put("QTE", 1);
73 ilm 569
            rowValsPort.put("ID_TAXE", tvaPort.getIDNumber());
94 ilm 570
 
571
            final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE");
572
            final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
573
            SQLRow rowDefaultCptPort;
574
            if (tvaPort.getFloat("TAUX") > 0) {
575
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_SOUMIS");
576
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
577
                    try {
578
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteSoumisTVA");
579
                    } catch (Exception e) {
580
                        e.printStackTrace();
581
                    }
582
                }
583
            } else {
584
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_NON_SOUMIS");
585
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
586
                    try {
587
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteNonSoumisTVA");
588
                    } catch (Exception e) {
589
                        e.printStackTrace();
590
                    }
591
                }
592
            }
149 ilm 593
            final SQLRowValues putRowValues = rowValsPort.putRowValues("ID_ARTICLE");
594
            putRowValues.put("ID_COMPTE_PCE", rowDefaultCptPort.getID());
595
            putRowValues.put("ID_COMPTE_PCE_ACHAT", rowDefaultCptPort.getID());
67 ilm 596
        } else {
597
            rowValsPort = null;
598
        }
156 ilm 599
        // Frais documents
600
        if (textFraisDoc.getText().trim().length() > 0) {
601
            if (!textFraisDoc.getText().trim().equals("-")) {
602
                long p = GestionDevise.parseLongCurrency(textFraisDoc.getText().trim());
603
                fraisDoc = new BigDecimal(p).movePointLeft(2);
604
            } else {
605
                fraisDoc = BigDecimal.ZERO;
606
            }
607
        } else {
608
            fraisDoc = BigDecimal.ZERO;
609
        }
610
        final SQLRow tvafraisDoc = selFraisDocTVA == null ? null : selFraisDocTVA.getSelectedRow();
611
        final SQLRowValues rowValsFraisDoc;
612
        if (tvafraisDoc != null && fraisDoc.signum() != 0 && !tvafraisDoc.isUndefined()) {
613
            rowValsFraisDoc = new SQLRowValues(articleTable.getSQLElement().getTable());
614
            rowValsFraisDoc.put(articleTable.getPrixTotalHTElement().getField().getName(), fraisDoc);
615
            rowValsFraisDoc.put("QTE", 1);
616
            rowValsFraisDoc.put("ID_TAXE", tvafraisDoc.getIDNumber());
617
            rowValsFraisDoc.put("SERVICE", Boolean.TRUE);
618
        } else {
619
            rowValsFraisDoc = null;
620
        }
18 ilm 621
 
67 ilm 622
        final Boolean isServiceActive = bServiceActive;
90 ilm 623
        if (worker != null) {
624
            worker.cancel(true);
625
        }
144 ilm 626
        worker = new SwingWorker<TotalCalculator, Object>() {
19 ilm 627
 
67 ilm 628
            @Override
629
            protected TotalCalculator doInBackground() throws Exception {
90 ilm 630
                Thread.sleep(100);
67 ilm 631
                params.fetchArticle();
18 ilm 632
 
73 ilm 633
                SQLTableElement tableElementTotalDevise = articleTable.getTableElementTotalDevise();
634
                String fieldDevise = (tableElementTotalDevise == null ? null : tableElementTotalDevise.getField().getName());
18 ilm 635
 
73 ilm 636
                SQLTableElement tableElementTotalHA = (articleTable.getPrebilanElement() == null) ? articleTable.getTotalHaElement() : articleTable.getPrebilanElement();
637
                String fieldHA = (tableElementTotalHA == null ? null : tableElementTotalHA.getField().getName());
638
                SQLTableElement tableElementTotalHT = articleTable.getPrixTotalHTElement();
639
                String fieldHT = (tableElementTotalHT == null ? null : tableElementTotalHT.getField().getName());
18 ilm 640
 
156 ilm 641
                final TotalCalculator calc = new TotalCalculator(fieldHA, fieldHT, fieldDevise, achat, null, rowCatComptable);
142 ilm 642
                calc.setIntraComm(intraComm);
93 ilm 643
                if (defaultRowCompteProduit != null) {
644
                    calc.setRowDefaultCptProduit(defaultRowCompteProduit);
645
                }
67 ilm 646
                calc.setSelectedRows(selectedRows);
18 ilm 647
 
67 ilm 648
                // Calcul avant remise
649
                final BigDecimal totalHTAvtremise;
650
 
73 ilm 651
                final int size = list.size();
652
                calc.setServiceActive(isServiceActive);
67 ilm 653
                if (valRemiseHT != 0) {
654
 
73 ilm 655
                    for (int i = 0; i < size; i++) {
67 ilm 656
                        SQLRowValues rowVals = list.get(i);
657
                        calc.addLine(rowVals, params.getMapArticle().get(rowVals.getInt("ID_ARTICLE")), i, false);
658
                    }
659
 
660
                    // TVA Port inclus
661
                    // if (this.selPortTVA != null && !valPortHT.equals(BigDecimal.ZERO)) {
662
                    //
663
                    // SQLRow tvaPort = this.selPortTVA.getSelectedRow();
664
                    // if (tvaPort != null) {
665
                    // calc.addLine(-1, valPortHT, null, null, 1, 0, false, tvaPort, false);
666
                    // }
667
                    // }
668
                    totalHTAvtremise = calc.getTotalHT();
18 ilm 669
                } else {
67 ilm 670
                    totalHTAvtremise = BigDecimal.ZERO;
18 ilm 671
                }
672
 
67 ilm 673
                calc.initValues();
73 ilm 674
                calc.setSelectedRows(selectedRows);
142 ilm 675
                long valRemiseHTReel = valRemiseHT;
676
                if (facturable != null && facturable.getValue() != null) {
677
                    if (facturable.getValue().getMontant() != null && facturable.getValue().getMontant().signum() > 0) {
678
                        valRemiseHTReel = 0;
679
                    } else if (facturable.getValue().getPercent() != null && facturable.getValue().getPercent().signum() > 0) {
680
                        valRemiseHTReel = facturable.getValue().getPercent().movePointLeft(2).multiply(new BigDecimal(valRemiseHT)).setScale(0, RoundingMode.HALF_UP).longValue();
681
                    }
18 ilm 682
 
142 ilm 683
                }
684
                calc.setRemise(valRemiseHTReel, totalHTAvtremise);
685
 
90 ilm 686
                if (Thread.currentThread().isInterrupted()) {
687
                    throw new InterruptedException();
688
                }
65 ilm 689
 
90 ilm 690
 
67 ilm 691
                // Total des elements
73 ilm 692
                int rowCount = size;
67 ilm 693
                for (int i = 0; i < rowCount; i++) {
694
                    SQLRowValues values = list.get(i);
73 ilm 695
 
696
                    Object id = values.getObject("ID_ARTICLE");
697
                    calc.addLine(values, (id == null) ? null : params.getMapArticle().get(id), i, i == (rowCount - 1));
65 ilm 698
                }
699
 
67 ilm 700
                // TVA Port inclus
701
                if (rowValsPort != null) {
94 ilm 702
                    calc.addLine(rowValsPort, rowValsPort.getForeign("ID_ARTICLE"), 0, false);
65 ilm 703
                }
61 ilm 704
 
156 ilm 705
                if (rowValsFraisDoc != null) {
706
                    calc.addLine(rowValsFraisDoc, null, 0, false);
707
                }
708
 
67 ilm 709
                // Verification du resultat ht +tva = ttc
710
                calc.checkResult();
90 ilm 711
                if (Thread.currentThread().isInterrupted()) {
712
                    throw new InterruptedException();
713
                }
67 ilm 714
                return calc;
19 ilm 715
            }
61 ilm 716
 
67 ilm 717
            @Override
718
            protected void done() {
91 ilm 719
                // if cancel() is called after doInBackground() nothing happens
720
                // but updating is set to a new instance
721
                if (this.isCancelled() || TotalPanel.this.worker != this) {
722
                    // une autre maj arrive
90 ilm 723
                    return;
724
                }
67 ilm 725
                TotalCalculator calc;
726
                try {
727
                    calc = get();
61 ilm 728
 
67 ilm 729
                    BigDecimal totalHT = calc.getTotalHT();
18 ilm 730
 
67 ilm 731
                    if (textTotalDevise != null) {
732
                        textTotalDevise.setText(GestionDevise.currencyToString(calc.getTotalDevise().setScale(2, RoundingMode.HALF_UP)));
733
                        textTotalDeviseSel.setText(GestionDevise.currencyToString(calc.getTotalDeviseSel().setScale(2, RoundingMode.HALF_UP)));
21 ilm 734
                    }
18 ilm 735
 
67 ilm 736
                    textPoids.setText(String.valueOf(calc.getTotalPoids()));
18 ilm 737
 
142 ilm 738
                    textTotalEco.setText(GestionDevise.currencyToString(calc.getTotalEco()));
739
                    textTotalEcoSel.setText(GestionDevise.currencyToString(calc.getTotalEcoSel()));
67 ilm 740
                    textTotalHT.setText(GestionDevise.currencyToString(totalHT));
741
                    textService.setText(GestionDevise.currencyToString(calc.getTotalService().setScale(2, RoundingMode.HALF_UP)));
742
                    textTotalTVA.setText(GestionDevise.currencyToString(calc.getTotalTVA().setScale(2, RoundingMode.HALF_UP)));
743
                    textTotalTTC.setText(GestionDevise.currencyToString(calc.getTotalTTC().setScale(2, RoundingMode.HALF_UP)));
744
                    BigDecimal totalHTSel = calc.getTotalHTSel();
745
                    textTotalHTSel.setText(GestionDevise.currencyToString(totalHTSel.setScale(2, RoundingMode.HALF_UP)));
746
                    textServiceSel.setText(GestionDevise.currencyToString(calc.getTotalServiceSel().setScale(2, RoundingMode.HALF_UP)));
18 ilm 747
 
67 ilm 748
                    textTotalTVASel.setText(GestionDevise.currencyToString(calc.getTotalTVASel().setScale(2, RoundingMode.HALF_UP)));
749
                    textTotalTTCSel.setText(GestionDevise.currencyToString(calc.getTotalTTCSel().setScale(2, RoundingMode.HALF_UP)));
750
                    if (gestionHA) {
751
                        BigDecimal totalHA = calc.getTotalHA();
752
                        textHA.setText(GestionDevise.currencyToString(totalHA.setScale(2, RoundingMode.HALF_UP)));
18 ilm 753
 
67 ilm 754
                        BigDecimal m = BigDecimal.ZERO;
755
                        BigDecimal d = BigDecimal.ZERO;
142 ilm 756
 
757
                        d = totalHT.subtract(totalHA);
758
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHT.signum() != 0) {
759
                            m = d.divide(totalHT, DecimalUtils.HIGH_PRECISION).movePointRight(2);
760
                        } else if (totalHA.signum() != 0) {
761
                            m = d.divide(totalHA, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 762
                        }
142 ilm 763
 
67 ilm 764
                        if (d.compareTo(BigDecimal.ZERO) <= 0) {
765
                            marge.setForeground(Color.red);
766
                            marge.setDisabledTextColor(Color.RED);
767
                        } else {
768
                            marge.setForeground(textTotalTTC.getForeground());
769
                            marge.setDisabledTextColor(textTotalTTC.getForeground());
770
                        }
771
                        marge.setText("(" + m.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(d.setScale(2, RoundingMode.HALF_UP)));
18 ilm 772
 
67 ilm 773
                        BigDecimal totalHASel = calc.getTotalHASel();
774
                        textHASel.setText(GestionDevise.currencyToString(totalHASel.setScale(2, RoundingMode.HALF_UP)));
775
 
776
                        BigDecimal m2 = BigDecimal.ZERO;
777
                        BigDecimal e = BigDecimal.ZERO;
142 ilm 778
 
779
                        e = totalHTSel.subtract(totalHASel);
780
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHTSel.signum() != 0) {
781
                            m2 = e.divide(totalHTSel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
782
                        } else if (totalHASel.signum() != 0) {
783
                            m2 = e.divide(totalHASel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 784
                        }
142 ilm 785
 
67 ilm 786
                        margeSel.setText("(" + m2.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(e.setScale(2, RoundingMode.HALF_UP)));
787
                        if (e.compareTo(BigDecimal.ZERO) <= 0) {
788
                            margeSel.setForeground(Color.red);
789
                            margeSel.setDisabledTextColor(Color.RED);
790
                        } else {
791
                            margeSel.setForeground(textTotalTTC.getForeground());
792
                            margeSel.setDisabledTextColor(textTotalTTC.getForeground());
793
                        }
794
 
795
                    }
796
                    supp.firePropertyChange("value", null, null);
73 ilm 797
                } catch (Exception e1) {
67 ilm 798
                    ExceptionHandler.handle("", e1);
799
                }
18 ilm 800
            }
801
 
67 ilm 802
        };
803
        worker.execute();
18 ilm 804
    }
805
 
80 ilm 806
    public long getTotalHT() {
807
        return GestionDevise.parseLongCurrency(this.textTotalHT.getText());
808
    }
809
 
810
    private static final JLabel getLabelFor(SQLField field, String defaultLabel) {
811
        if (field == null) {
812
            return new JLabel(defaultLabel);
813
        }
18 ilm 814
        return new JLabel(Configuration.getInstance().getTranslator().getLabelFor(field));
815
 
816
    }
817
 
80 ilm 818
    private static final JLabel getLabelBoldFor(SQLField field, String defaultLabel) {
819
        if (field == null) {
820
            return new JLabelBold(defaultLabel);
821
        }
18 ilm 822
        return new JLabelBold(Configuration.getInstance().getTranslator().getLabelFor(field));
823
 
824
    }
825
 
826
    public void addValueListener(PropertyChangeListener listener) {
827
        this.supp.addPropertyChangeListener(listener);
828
    }
829
 
830
    public void removeValueListener(PropertyChangeListener listener) {
831
        this.supp.removePropertyChangeListener(listener);
832
    }
833
 
834
    private final JSeparator createSeparator() {
835
        final JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
836
        DefaultGridBagConstraints.lockMinimumSize(sep);
837
        return sep;
838
    }
839
}