OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 180 | 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.weighty = 0;
176
        c.fill = GridBagConstraints.HORIZONTAL;
177
 
178
        // Collone 1 : Selection
179
        c.gridx++;
156 ilm 180
        this.add(new JLabelBold(TM.tr("TotalPanel.selection")), c); //$NON-NLS-1$
18 ilm 181
        c.gridy++;
182
        c.gridx = 1;
183
        c.gridwidth = 2;
184
        c.weightx = 1;
185
        c.fill = GridBagConstraints.BOTH;
186
        this.add(createSeparator(), c);
187
 
188
        c.gridwidth = 1;
189
        c.weightx = 0;
190
        c.fill = GridBagConstraints.HORIZONTAL;
191
        if (this.gestionHA) {
156 ilm 192
            final JLabel labelMarge = new JLabel(TM.tr("TotalPanel.margin")); //$NON-NLS-1$
174 ilm 193
            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 194
 
195
            // Total HA HT
196
            c.gridy++;
94 ilm 197
            this.add(labelHA, c);
198
            labelHA.setVisible(showHA);
18 ilm 199
 
200
            c.gridx++;
201
            c.weightx = 1;
202
            this.add(this.textHASel, c);
203
 
204
            // Marge
205
            c.gridy++;
206
            c.gridx = 1;
207
            c.weightx = 0;
94 ilm 208
            this.add(labelMarge, c);
209
            labelMarge.setVisible(showHA);
18 ilm 210
 
211
            c.gridx++;
212
            c.weightx = 1;
213
            this.add(this.margeSel, c);
214
 
215
            c.gridy++;
216
            c.gridx = 1;
217
            c.gridwidth = 2;
218
            c.weightx = 1;
219
 
220
            c.fill = GridBagConstraints.BOTH;
221
            this.add(createSeparator(), c);
222
 
223
            c.gridwidth = 1;
224
            c.weightx = 0;
225
        }
226
 
227
        // Total HT
228
        c.gridy++;
229
        c.gridx = 1;
230
        c.weightx = 0;
231
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 232
        this.add(getLabelBoldFor(textTotalHT.getField(), TM.tr("TotalPanel.total.taxExcluded")), c); //$NON-NLS-1$
18 ilm 233
 
234
        c.gridx++;
235
        c.weightx = 1;
236
        this.add(this.textTotalHTSel, c);
237
 
238
        // Service
28 ilm 239
        if (DefaultNXProps.getInstance().getBooleanValue(AbstractVenteArticleItemTable.ARTICLE_SERVICE, false)) {
240
            c.gridx = 1;
241
            c.gridy++;
242
            c.weightx = 0;
156 ilm 243
            this.add(new JLabel(TM.tr("TotalPanel.service.taxExcluded")), c); //$NON-NLS-1$
28 ilm 244
            c.gridx++;
245
            c.weightx = 1;
246
            this.add(this.textServiceSel, c);
247
        }
18 ilm 248
 
249
        // TVA
250
        c.gridx = 1;
251
        c.gridy++;
252
        c.weightx = 0;
156 ilm 253
        this.add(getLabelFor(textTotalTVA.getField(), TM.tr("TotalPanel.total.VAT")), c); //$NON-NLS-1$
18 ilm 254
        c.gridx++;
255
        c.weightx = 1;
256
        this.add(this.textTotalTVASel, c);
257
 
258
        c.gridx = 1;
259
        c.gridy++;
260
        c.gridwidth = 2;
261
        c.fill = GridBagConstraints.BOTH;
262
        this.add(createSeparator(), c);
263
 
61 ilm 264
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 265
            // Devise
266
            c.gridwidth = 1;
267
            c.gridx = 1;
268
            c.gridy++;
269
            c.weightx = 0;
270
            c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 271
            this.add(getLabelFor(textTotalDevise.getField(), TM.tr("TotalPanel.currencyTotal")), c); //$NON-NLS-1$
19 ilm 272
            c.gridx++;
273
            c.weightx = 1;
274
            this.add(this.textTotalDeviseSel, c);
275
        }
142 ilm 276
 
277
        // Eco-contribution
278
        final SQLPreferences prefs = SQLPreferences.getMemCached(this.table.getRowValuesTableModel().getSQLElement().getTable().getDBRoot());
279
        final boolean showEco = prefs.getBoolean(AbstractVenteArticleItemTable.SHOW_TOTAL_ECO_CONTRIBUTION, false);
280
        if (showEco) {
281
            c.gridx = 1;
282
            c.gridy++;
283
            c.weightx = 0;
156 ilm 284
            this.add(new JLabel(TM.tr("TotalPanel.ecoTax")), c); //$NON-NLS-1$
142 ilm 285
            c.gridx++;
286
            c.weightx = 1;
287
            this.add(this.textTotalEcoSel, c);
288
        }
289
 
18 ilm 290
        // TTC
291
        c.gridwidth = 1;
292
        c.gridx = 1;
293
        c.gridy++;
294
        c.weightx = 0;
295
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 296
        this.add(getLabelFor(textTotalTTC.getField(), TM.tr("TotalPanel.total.taxIncluded")), c); //$NON-NLS-1$
18 ilm 297
        c.gridx++;
298
        c.weightx = 1;
299
        this.add(this.textTotalTTCSel, c);
300
 
301
        // Global
302
        c.gridx = 3;
303
        c.gridy = 0;
304
        c.gridheight = GridBagConstraints.REMAINDER;
305
        c.weighty = 1;
306
        c.fill = GridBagConstraints.VERTICAL;
307
        c.weightx = 0;
308
        this.add(createSeparator(), c);
309
 
310
        c.gridheight = 1;
311
        c.fill = GridBagConstraints.HORIZONTAL;
312
        c.weighty = 0;
313
        c.gridx++;
156 ilm 314
        this.add(new JLabelBold(TM.tr("TotalPanel.global")), c); //$NON-NLS-1$
18 ilm 315
        c.gridy++;
316
        c.gridx = 4;
317
        c.gridwidth = 2;
318
        c.weightx = 1;
319
        this.add(createSeparator(), c);
320
 
321
        c.gridwidth = 1;
322
        c.weightx = 0;
323
        if (this.gestionHA) {
156 ilm 324
            final JLabel labelMarge = new JLabel(TM.tr("TotalPanel.margin")); //$NON-NLS-1$
174 ilm 325
            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 326
 
327
            // Total HA HT
328
            c.gridy++;
94 ilm 329
            this.add(labelHA, c);
330
            labelHA.setVisible(showHA);
18 ilm 331
 
332
            c.gridx++;
333
            c.weightx = 1;
334
            this.add(this.textHA, c);
335
 
336
            // Marge
337
            c.gridy++;
338
            c.gridx = 4;
339
            c.weightx = 0;
94 ilm 340
            this.add(labelMarge, c);
341
            labelMarge.setVisible(showHA);
18 ilm 342
 
343
            c.gridx++;
344
            c.weightx = 1;
345
            this.add(this.marge, c);
346
 
347
            c.gridy++;
348
            c.gridx = 4;
349
            c.gridwidth = 2;
350
            c.weightx = 1;
351
            this.add(createSeparator(), c);
352
 
353
            c.gridwidth = 1;
354
            c.weightx = 0;
355
        }
356
 
357
        // Total HT
358
        c.gridy++;
359
        c.gridx = 4;
360
        c.weightx = 0;
156 ilm 361
        this.add(getLabelBoldFor(textTotalHT.getField(), TM.tr("TotalPanel.total.taxExcluded")), c); //$NON-NLS-1$
18 ilm 362
 
363
        c.gridx++;
364
        c.weightx = 1;
365
        this.add(textTotalHT, c);
366
 
28 ilm 367
        if (DefaultNXProps.getInstance().getBooleanValue(AbstractVenteArticleItemTable.ARTICLE_SERVICE, false)) {
368
            // Service
369
            c.gridx = 4;
370
            c.gridy++;
371
            c.weightx = 0;
156 ilm 372
            this.add(new JLabelBold(TM.tr("TotalPanel.service.taxExcluded")), c); //$NON-NLS-1$
28 ilm 373
            c.gridx++;
374
            c.weightx = 1;
375
            this.add(this.textService, c);
376
        }
18 ilm 377
        // TVA
378
        c.gridx = 4;
379
        c.gridy++;
380
        c.weightx = 0;
156 ilm 381
        this.add(getLabelBoldFor(textTotalTVA.getField(), TM.tr("TotalPanel.total.VAT")), c); //$NON-NLS-1$
18 ilm 382
        c.gridx++;
383
        c.weightx = 1;
384
        this.add(textTotalTVA, c);
385
 
386
        // Sep
387
        c.gridwidth = 2;
388
        c.gridx = 4;
389
        c.gridy++;
390
        c.weightx = 1;
391
        c.fill = GridBagConstraints.BOTH;
392
        this.add(createSeparator(), c);
393
 
61 ilm 394
        if (articleItemTable.getTableElementTotalDevise() != null) {
19 ilm 395
            // devise
396
            c.gridwidth = 1;
397
            c.gridx = 4;
398
            c.gridy++;
399
            c.weightx = 0;
400
            c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 401
            this.add(getLabelBoldFor(textTotalDevise.getField(), TM.tr("TotalPanel.currencyTotal")), c); //$NON-NLS-1$
19 ilm 402
            c.gridx++;
403
            c.weightx = 1;
404
            textTotalDevise.setFont(textTotalHT.getFont());
405
            this.add(textTotalDevise, c);
406
        }
142 ilm 407
 
408
        // Eco-contribution
409
        if (showEco) {
410
            c.gridx = 4;
411
            c.gridy++;
412
            c.weightx = 0;
156 ilm 413
            this.add(new JLabel(TM.tr("TotalPanel.ecoTax")), c); //$NON-NLS-1$
142 ilm 414
            c.gridx++;
415
            c.weightx = 1;
416
            this.add(this.textTotalEco, c);
417
        }
418
 
18 ilm 419
        // TTC
420
        c.gridwidth = 1;
421
        c.gridx = 4;
422
        c.gridy++;
423
        c.weightx = 0;
424
        c.fill = GridBagConstraints.HORIZONTAL;
156 ilm 425
        this.add(getLabelBoldFor(textTotalTTC.getField(), TM.tr("TotalPanel.total.taxIncluded")), c); //$NON-NLS-1$
18 ilm 426
        c.gridx++;
427
        c.weightx = 1;
428
        textTotalTTC.setFont(textTotalHT.getFont());
429
        this.add(textTotalTTC, c);
430
 
431
        updateTotal();
432
        this.table.getRowValuesTableModel().addTableModelListener(this);
433
        this.table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
434
            public void valueChanged(ListSelectionEvent e) {
435
                updateTotal();
436
            }
437
        });
438
    }
439
 
156 ilm 440
    public void setTextFraisDoc(DeviseField textFraisDoc, SQLRequestComboBox boxTVA) {
441
        this.textFraisDoc = textFraisDoc;
442
        this.selFraisDocTVA = boxTVA;
443
    }
444
 
174 ilm 445
    public void setTextFixTVA(JTextField textTVAFix) {
446
        this.textTVAFix = textTVAFix;
447
    }
448
 
142 ilm 449
    public void setIntraComm(boolean intraComm) {
450
        this.intraComm = intraComm;
451
        updateTotal();
452
    }
453
 
80 ilm 454
    private boolean achat = false;
455
 
456
    public void setAchat(boolean b) {
457
        this.achat = b;
458
    }
459
 
18 ilm 460
    private void reconfigure(JTextField field) {
461
        field.setEditable(false);
462
        field.setHorizontalAlignment(JTextField.RIGHT);
463
        field.setBorder(null);
464
        field.setColumns(11);
93 ilm 465
        field.setOpaque(false);
18 ilm 466
        field.setMinimumSize(new Dimension(150, 20));
467
        field.setPreferredSize(new Dimension(150, 20));
468
        field.setBackground(UIManager.getColor("control"));
469
        field.setEditable(false);
470
        field.setEnabled(false);
471
        field.setDisabledTextColor(Color.BLACK);
472
    }
473
 
474
    public void tableChanged(TableModelEvent e) {
73 ilm 475
        updateTotal();
18 ilm 476
    }
477
 
67 ilm 478
    private static String CLEAR = "";
144 ilm 479
    private SwingWorker<TotalCalculator, Object> worker;
67 ilm 480
 
481
    private void clearTextField() {
482
 
483
        if (textTotalDevise != null) {
484
            textTotalDevise.setText(CLEAR);
485
            textTotalDeviseSel.setText(CLEAR);
486
        }
487
 
488
        textPoids.setText(CLEAR);
489
        textTotalHT.setText(CLEAR);
490
        textService.setText(CLEAR);
491
        textTotalTVA.setText(CLEAR);
492
        textTotalTTC.setText(CLEAR);
493
        textTotalHTSel.setText(CLEAR);
494
        textServiceSel.setText(CLEAR);
495
        textTotalTVASel.setText(CLEAR);
496
        textTotalTTCSel.setText(CLEAR);
497
        if (gestionHA) {
498
            textHA.setText(CLEAR);
499
            marge.setText(CLEAR);
500
            textHASel.setText(CLEAR);
501
            margeSel.setText(CLEAR);
502
        }
503
    }
504
 
93 ilm 505
    private SQLRowAccessor defaultRowCompteProduit = null;
506
 
507
    public void setDefaultCompteProduit(SQLRowAccessor row) {
508
        this.defaultRowCompteProduit = row;
509
    }
510
 
156 ilm 511
    private SQLRowAccessor rowCatComptable = null;
512
 
513
    public void setCategorieComptable(SQLRowAccessor row) {
514
        this.rowCatComptable = row;
515
    }
516
 
18 ilm 517
    /**
518
     *
519
     */
520
    public void updateTotal() {
521
 
67 ilm 522
        final long valRemiseHT;
174 ilm 523
        final BigDecimal valPortHT, fraisDoc, tvaFix;
61 ilm 524
 
67 ilm 525
        clearTextField();
83 ilm 526
        // Clone rows to be thread safe
527
        final List<SQLRowValues> vals = articleTable.getRowValuesAtLevel(1);
528
        final List<SQLRowValues> list = new ArrayList<SQLRowValues>(vals.size());
529
        for (SQLRowValues sqlRowValues : vals) {
530
            list.add(sqlRowValues.asRowValues());
531
        }
61 ilm 532
 
18 ilm 533
 
67 ilm 534
        final TotalCalculatorParameters params = new TotalCalculatorParameters(list);
63 ilm 535
 
67 ilm 536
        // Total Service
537
        String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
538
        Boolean bServiceActive = Boolean.valueOf(val);
18 ilm 539
 
67 ilm 540
        final int[] selectedRows = this.table.getSelectedRows();
18 ilm 541
 
67 ilm 542
        // Remise à inclure
63 ilm 543
 
67 ilm 544
        // FIXME Remise à appliquer en plus sur les frais de ports??
545
        if (this.textRemiseHT.getText().trim().length() > 0) {
546
            if (!this.textRemiseHT.getText().trim().equals("-")) {
547
                valRemiseHT = GestionDevise.parseLongCurrency(this.textRemiseHT.getText().trim());
548
            } else {
549
                valRemiseHT = 0;
550
            }
551
        } else {
552
            valRemiseHT = 0;
553
        }
554
        params.setRemiseHT(valRemiseHT);
63 ilm 555
 
67 ilm 556
        // Frais de port à inclure
557
        if (textPortHT.getText().trim().length() > 0) {
558
            if (!textPortHT.getText().trim().equals("-")) {
559
                long p = GestionDevise.parseLongCurrency(textPortHT.getText().trim());
560
                valPortHT = new BigDecimal(p).movePointLeft(2);
561
            } else {
562
                valPortHT = BigDecimal.ZERO;
563
            }
564
        } else {
565
            valPortHT = BigDecimal.ZERO;
566
        }
567
        params.setPortHT(valPortHT);
63 ilm 568
 
67 ilm 569
        final SQLRow tvaPort = selPortTVA == null ? null : selPortTVA.getSelectedRow();
570
        final SQLRowValues rowValsPort;
571
        // TVA Port inclus
73 ilm 572
        if (tvaPort != null && valPortHT.signum() != 0 && !tvaPort.isUndefined()) {
67 ilm 573
            rowValsPort = new SQLRowValues(articleTable.getSQLElement().getTable());
574
            rowValsPort.put(articleTable.getPrixTotalHTElement().getField().getName(), valPortHT);
575
            rowValsPort.put("QTE", 1);
73 ilm 576
            rowValsPort.put("ID_TAXE", tvaPort.getIDNumber());
94 ilm 577
 
578
            final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE");
579
            final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
580
            SQLRow rowDefaultCptPort;
581
            if (tvaPort.getFloat("TAUX") > 0) {
582
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_SOUMIS");
583
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
584
                    try {
585
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteSoumisTVA");
586
                    } catch (Exception e) {
587
                        e.printStackTrace();
588
                    }
589
                }
590
            } else {
591
                rowDefaultCptPort = rowPrefsCompte.getForeign("ID_COMPTE_PCE_PORT_NON_SOUMIS");
592
                if (rowDefaultCptPort == null || rowDefaultCptPort.isUndefined()) {
593
                    try {
594
                        rowDefaultCptPort = ComptePCESQLElement.getRowComptePceDefault("PortVenteNonSoumisTVA");
595
                    } catch (Exception e) {
596
                        e.printStackTrace();
597
                    }
598
                }
599
            }
149 ilm 600
            final SQLRowValues putRowValues = rowValsPort.putRowValues("ID_ARTICLE");
601
            putRowValues.put("ID_COMPTE_PCE", rowDefaultCptPort.getID());
602
            putRowValues.put("ID_COMPTE_PCE_ACHAT", rowDefaultCptPort.getID());
67 ilm 603
        } else {
604
            rowValsPort = null;
605
        }
156 ilm 606
        // Frais documents
607
        if (textFraisDoc.getText().trim().length() > 0) {
608
            if (!textFraisDoc.getText().trim().equals("-")) {
609
                long p = GestionDevise.parseLongCurrency(textFraisDoc.getText().trim());
610
                fraisDoc = new BigDecimal(p).movePointLeft(2);
611
            } else {
612
                fraisDoc = BigDecimal.ZERO;
613
            }
614
        } else {
615
            fraisDoc = BigDecimal.ZERO;
616
        }
617
        final SQLRow tvafraisDoc = selFraisDocTVA == null ? null : selFraisDocTVA.getSelectedRow();
618
        final SQLRowValues rowValsFraisDoc;
619
        if (tvafraisDoc != null && fraisDoc.signum() != 0 && !tvafraisDoc.isUndefined()) {
620
            rowValsFraisDoc = new SQLRowValues(articleTable.getSQLElement().getTable());
621
            rowValsFraisDoc.put(articleTable.getPrixTotalHTElement().getField().getName(), fraisDoc);
622
            rowValsFraisDoc.put("QTE", 1);
623
            rowValsFraisDoc.put("ID_TAXE", tvafraisDoc.getIDNumber());
624
            rowValsFraisDoc.put("SERVICE", Boolean.TRUE);
625
        } else {
626
            rowValsFraisDoc = null;
627
        }
18 ilm 628
 
174 ilm 629
        // Fix TVA si écart avec facture fournisseur
630
        if (textTVAFix.getText().trim().length() > 0) {
631
            if (!textTVAFix.getText().trim().equals("-")) {
632
                long p = GestionDevise.parseLongCurrency(textTVAFix.getText().trim());
633
                tvaFix = new BigDecimal(p).movePointLeft(2);
634
            } else {
635
                tvaFix = BigDecimal.ZERO;
636
            }
637
        } else {
638
            tvaFix = BigDecimal.ZERO;
639
        }
640
 
67 ilm 641
        final Boolean isServiceActive = bServiceActive;
90 ilm 642
        if (worker != null) {
643
            worker.cancel(true);
644
        }
144 ilm 645
        worker = new SwingWorker<TotalCalculator, Object>() {
19 ilm 646
 
67 ilm 647
            @Override
648
            protected TotalCalculator doInBackground() throws Exception {
90 ilm 649
                Thread.sleep(100);
67 ilm 650
                params.fetchArticle();
18 ilm 651
 
73 ilm 652
                SQLTableElement tableElementTotalDevise = articleTable.getTableElementTotalDevise();
653
                String fieldDevise = (tableElementTotalDevise == null ? null : tableElementTotalDevise.getField().getName());
18 ilm 654
 
73 ilm 655
                SQLTableElement tableElementTotalHA = (articleTable.getPrebilanElement() == null) ? articleTable.getTotalHaElement() : articleTable.getPrebilanElement();
656
                String fieldHA = (tableElementTotalHA == null ? null : tableElementTotalHA.getField().getName());
657
                SQLTableElement tableElementTotalHT = articleTable.getPrixTotalHTElement();
658
                String fieldHT = (tableElementTotalHT == null ? null : tableElementTotalHT.getField().getName());
18 ilm 659
 
156 ilm 660
                final TotalCalculator calc = new TotalCalculator(fieldHA, fieldHT, fieldDevise, achat, null, rowCatComptable);
142 ilm 661
                calc.setIntraComm(intraComm);
93 ilm 662
                if (defaultRowCompteProduit != null) {
663
                    calc.setRowDefaultCptProduit(defaultRowCompteProduit);
664
                }
67 ilm 665
                calc.setSelectedRows(selectedRows);
18 ilm 666
 
67 ilm 667
                // Calcul avant remise
668
                final BigDecimal totalHTAvtremise;
669
 
73 ilm 670
                final int size = list.size();
671
                calc.setServiceActive(isServiceActive);
67 ilm 672
                if (valRemiseHT != 0) {
673
 
73 ilm 674
                    for (int i = 0; i < size; i++) {
67 ilm 675
                        SQLRowValues rowVals = list.get(i);
676
                        calc.addLine(rowVals, params.getMapArticle().get(rowVals.getInt("ID_ARTICLE")), i, false);
677
                    }
678
 
679
                    // TVA Port inclus
680
                    // if (this.selPortTVA != null && !valPortHT.equals(BigDecimal.ZERO)) {
681
                    //
682
                    // SQLRow tvaPort = this.selPortTVA.getSelectedRow();
683
                    // if (tvaPort != null) {
684
                    // calc.addLine(-1, valPortHT, null, null, 1, 0, false, tvaPort, false);
685
                    // }
686
                    // }
687
                    totalHTAvtremise = calc.getTotalHT();
18 ilm 688
                } else {
67 ilm 689
                    totalHTAvtremise = BigDecimal.ZERO;
18 ilm 690
                }
691
 
67 ilm 692
                calc.initValues();
73 ilm 693
                calc.setSelectedRows(selectedRows);
142 ilm 694
                long valRemiseHTReel = valRemiseHT;
695
                if (facturable != null && facturable.getValue() != null) {
696
                    if (facturable.getValue().getMontant() != null && facturable.getValue().getMontant().signum() > 0) {
697
                        valRemiseHTReel = 0;
698
                    } else if (facturable.getValue().getPercent() != null && facturable.getValue().getPercent().signum() > 0) {
699
                        valRemiseHTReel = facturable.getValue().getPercent().movePointLeft(2).multiply(new BigDecimal(valRemiseHT)).setScale(0, RoundingMode.HALF_UP).longValue();
700
                    }
18 ilm 701
 
142 ilm 702
                }
703
                calc.setRemise(valRemiseHTReel, totalHTAvtremise);
704
 
90 ilm 705
                if (Thread.currentThread().isInterrupted()) {
706
                    throw new InterruptedException();
707
                }
65 ilm 708
 
90 ilm 709
 
67 ilm 710
                // Total des elements
73 ilm 711
                int rowCount = size;
67 ilm 712
                for (int i = 0; i < rowCount; i++) {
713
                    SQLRowValues values = list.get(i);
73 ilm 714
 
715
                    Object id = values.getObject("ID_ARTICLE");
716
                    calc.addLine(values, (id == null) ? null : params.getMapArticle().get(id), i, i == (rowCount - 1));
65 ilm 717
                }
718
 
67 ilm 719
                // TVA Port inclus
720
                if (rowValsPort != null) {
94 ilm 721
                    calc.addLine(rowValsPort, rowValsPort.getForeign("ID_ARTICLE"), 0, false);
65 ilm 722
                }
61 ilm 723
 
156 ilm 724
                if (rowValsFraisDoc != null) {
725
                    calc.addLine(rowValsFraisDoc, null, 0, false);
726
                }
727
 
174 ilm 728
                calc.addTVAAdjust(tvaFix);
729
 
67 ilm 730
                // Verification du resultat ht +tva = ttc
731
                calc.checkResult();
90 ilm 732
                if (Thread.currentThread().isInterrupted()) {
733
                    throw new InterruptedException();
734
                }
67 ilm 735
                return calc;
19 ilm 736
            }
61 ilm 737
 
67 ilm 738
            @Override
739
            protected void done() {
91 ilm 740
                // if cancel() is called after doInBackground() nothing happens
741
                // but updating is set to a new instance
742
                if (this.isCancelled() || TotalPanel.this.worker != this) {
743
                    // une autre maj arrive
90 ilm 744
                    return;
745
                }
67 ilm 746
                TotalCalculator calc;
747
                try {
748
                    calc = get();
61 ilm 749
 
67 ilm 750
                    BigDecimal totalHT = calc.getTotalHT();
18 ilm 751
 
67 ilm 752
                    if (textTotalDevise != null) {
753
                        textTotalDevise.setText(GestionDevise.currencyToString(calc.getTotalDevise().setScale(2, RoundingMode.HALF_UP)));
754
                        textTotalDeviseSel.setText(GestionDevise.currencyToString(calc.getTotalDeviseSel().setScale(2, RoundingMode.HALF_UP)));
21 ilm 755
                    }
18 ilm 756
 
67 ilm 757
                    textPoids.setText(String.valueOf(calc.getTotalPoids()));
18 ilm 758
 
142 ilm 759
                    textTotalEco.setText(GestionDevise.currencyToString(calc.getTotalEco()));
760
                    textTotalEcoSel.setText(GestionDevise.currencyToString(calc.getTotalEcoSel()));
67 ilm 761
                    textTotalHT.setText(GestionDevise.currencyToString(totalHT));
762
                    textService.setText(GestionDevise.currencyToString(calc.getTotalService().setScale(2, RoundingMode.HALF_UP)));
763
                    textTotalTVA.setText(GestionDevise.currencyToString(calc.getTotalTVA().setScale(2, RoundingMode.HALF_UP)));
764
                    textTotalTTC.setText(GestionDevise.currencyToString(calc.getTotalTTC().setScale(2, RoundingMode.HALF_UP)));
765
                    BigDecimal totalHTSel = calc.getTotalHTSel();
766
                    textTotalHTSel.setText(GestionDevise.currencyToString(totalHTSel.setScale(2, RoundingMode.HALF_UP)));
767
                    textServiceSel.setText(GestionDevise.currencyToString(calc.getTotalServiceSel().setScale(2, RoundingMode.HALF_UP)));
18 ilm 768
 
67 ilm 769
                    textTotalTVASel.setText(GestionDevise.currencyToString(calc.getTotalTVASel().setScale(2, RoundingMode.HALF_UP)));
770
                    textTotalTTCSel.setText(GestionDevise.currencyToString(calc.getTotalTTCSel().setScale(2, RoundingMode.HALF_UP)));
771
                    if (gestionHA) {
772
                        BigDecimal totalHA = calc.getTotalHA();
773
                        textHA.setText(GestionDevise.currencyToString(totalHA.setScale(2, RoundingMode.HALF_UP)));
18 ilm 774
 
67 ilm 775
                        BigDecimal m = BigDecimal.ZERO;
776
                        BigDecimal d = BigDecimal.ZERO;
142 ilm 777
 
778
                        d = totalHT.subtract(totalHA);
779
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHT.signum() != 0) {
780
                            m = d.divide(totalHT, DecimalUtils.HIGH_PRECISION).movePointRight(2);
781
                        } else if (totalHA.signum() != 0) {
782
                            m = d.divide(totalHA, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 783
                        }
142 ilm 784
 
67 ilm 785
                        if (d.compareTo(BigDecimal.ZERO) <= 0) {
786
                            marge.setForeground(Color.red);
787
                            marge.setDisabledTextColor(Color.RED);
788
                        } else {
789
                            marge.setForeground(textTotalTTC.getForeground());
790
                            marge.setDisabledTextColor(textTotalTTC.getForeground());
791
                        }
792
                        marge.setText("(" + m.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(d.setScale(2, RoundingMode.HALF_UP)));
18 ilm 793
 
67 ilm 794
                        BigDecimal totalHASel = calc.getTotalHASel();
795
                        textHASel.setText(GestionDevise.currencyToString(totalHASel.setScale(2, RoundingMode.HALF_UP)));
796
 
797
                        BigDecimal m2 = BigDecimal.ZERO;
798
                        BigDecimal e = BigDecimal.ZERO;
142 ilm 799
 
800
                        e = totalHTSel.subtract(totalHASel);
801
                        if (DefaultNXProps.getInstance().getBooleanValue(MARGE_MARQUE, false) && totalHTSel.signum() != 0) {
802
                            m2 = e.divide(totalHTSel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
803
                        } else if (totalHASel.signum() != 0) {
804
                            m2 = e.divide(totalHASel, DecimalUtils.HIGH_PRECISION).movePointRight(2);
67 ilm 805
                        }
142 ilm 806
 
67 ilm 807
                        margeSel.setText("(" + m2.setScale(2, RoundingMode.HALF_UP) + "%) " + GestionDevise.currencyToString(e.setScale(2, RoundingMode.HALF_UP)));
808
                        if (e.compareTo(BigDecimal.ZERO) <= 0) {
809
                            margeSel.setForeground(Color.red);
810
                            margeSel.setDisabledTextColor(Color.RED);
811
                        } else {
812
                            margeSel.setForeground(textTotalTTC.getForeground());
813
                            margeSel.setDisabledTextColor(textTotalTTC.getForeground());
814
                        }
815
 
816
                    }
817
                    supp.firePropertyChange("value", null, null);
73 ilm 818
                } catch (Exception e1) {
67 ilm 819
                    ExceptionHandler.handle("", e1);
820
                }
18 ilm 821
            }
822
 
67 ilm 823
        };
824
        worker.execute();
18 ilm 825
    }
826
 
80 ilm 827
    public long getTotalHT() {
828
        return GestionDevise.parseLongCurrency(this.textTotalHT.getText());
829
    }
830
 
831
    private static final JLabel getLabelFor(SQLField field, String defaultLabel) {
832
        if (field == null) {
833
            return new JLabel(defaultLabel);
834
        }
18 ilm 835
        return new JLabel(Configuration.getInstance().getTranslator().getLabelFor(field));
836
 
837
    }
838
 
80 ilm 839
    private static final JLabel getLabelBoldFor(SQLField field, String defaultLabel) {
840
        if (field == null) {
841
            return new JLabelBold(defaultLabel);
842
        }
18 ilm 843
        return new JLabelBold(Configuration.getInstance().getTranslator().getLabelFor(field));
844
 
845
    }
846
 
847
    public void addValueListener(PropertyChangeListener listener) {
848
        this.supp.addPropertyChangeListener(listener);
849
    }
850
 
851
    public void removeValueListener(PropertyChangeListener listener) {
852
        this.supp.removePropertyChangeListener(listener);
853
    }
854
 
855
    private final JSeparator createSeparator() {
856
        final JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
857
        DefaultGridBagConstraints.lockMinimumSize(sep);
858
        return sep;
859
    }
860
}