OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | 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;
17
import org.openconcerto.erp.model.PrixHT;
18
import org.openconcerto.erp.model.PrixTTC;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.model.SQLBackgroundTableCache;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.sqlobject.ElementComboBox;
23
import org.openconcerto.utils.GestionDevise;
24
 
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.beans.PropertyChangeEvent;
31
import java.beans.PropertyChangeListener;
32
 
33
import javax.swing.ButtonGroup;
34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.JRadioButton;
37
import javax.swing.SwingConstants;
38
import javax.swing.SwingUtilities;
39
import javax.swing.event.DocumentEvent;
40
import javax.swing.event.DocumentListener;
41
 
42
/***************************************************************************************************
43
 * SELECTION D'UN MONTANT TTC OU HT + VALEUR TVA
44
 *
45
 * Pour initialiser le ComboSelection --> addRequiredSQLObject(montant.getChoixTaxe(), "ID_TAXE");
46
 * --> setChoixTaxe(String value);
47
 **************************************************************************************************/
48
 
49
public class MontantPanel extends JPanel {
50
 
51
    private JRadioButton checkHT;
52
    private JRadioButton checkTTC;
53
    private DeviseField textHT;
54
    private DeviseField textTTC;
55
    private DeviseField textTaxe;
56
    private ElementComboBox comboTaxe;
57
    private boolean ue = false;
58
    private boolean enabled = true;
59
    private JLabel labelUE = new JLabel("Calcul d'une TVA intracommunautaire");
60
 
61
    private final DocumentListener listenerTextHT = new DocumentListener() {
62
        public void changedUpdate(DocumentEvent e) {
63
            calculMontant();
64
        }
65
 
66
        public void removeUpdate(DocumentEvent e) {
67
            calculMontant();
68
        }
69
 
70
        public void insertUpdate(DocumentEvent e) {
71
            calculMontant();
72
        }
73
    };
74
 
75
    private final DocumentListener listenerTextTTC = new DocumentListener() {
76
        public void changedUpdate(DocumentEvent e) {
77
            calculMontant();
78
        }
79
 
80
        public void removeUpdate(DocumentEvent e) {
81
            calculMontant();
82
        }
83
 
84
        public void insertUpdate(DocumentEvent e) {
85
            calculMontant();
86
        }
87
    };
177 ilm 88
    private final DocumentListener listenerTextTaxe = new DocumentListener() {
89
        public void changedUpdate(DocumentEvent e) {
18 ilm 90
 
177 ilm 91
            if (MontantPanel.this.enabled) {
92
                long taxe = GestionDevise.parseLongCurrency(MontantPanel.this.textTaxe.getText());
93
                if (MontantPanel.this.checkHT.isSelected()) {
94
                    long ht = GestionDevise.parseLongCurrency(MontantPanel.this.textHT.getText());
95
                    long ttc = taxe + ht;
96
                    MontantPanel.this.textTTC.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTTC);
97
                    MontantPanel.this.textTTC.setText(GestionDevise.currencyToString(ttc));
98
                    MontantPanel.this.textTTC.getDocument().addDocumentListener(MontantPanel.this.listenerTextTTC);
99
                } else {
100
                    long ttc = GestionDevise.parseLongCurrency(MontantPanel.this.textTTC.getText());
101
                    long ht = ttc - taxe;
102
                    MontantPanel.this.textHT.getDocument().removeDocumentListener(MontantPanel.this.listenerTextHT);
103
                    MontantPanel.this.textHT.setText(GestionDevise.currencyToString(ht));
104
                    MontantPanel.this.textHT.getDocument().addDocumentListener(MontantPanel.this.listenerTextHT);
105
                }
106
            }
107
 
108
        }
109
 
110
        public void removeUpdate(DocumentEvent e) {
111
            changedUpdate(e);
112
        }
113
 
114
        public void insertUpdate(DocumentEvent e) {
115
            changedUpdate(e);
116
        }
117
    };
118
 
18 ilm 119
    public MontantPanel() {
120
        uiInit();
121
    }
122
 
123
    private void uiInit() {
124
        this.setOpaque(false);
125
        this.setLayout(new GridBagLayout());
126
        GridBagConstraints c = new GridBagConstraints();
127
        c.insets = new Insets(2, 2, 1, 2);
128
        c.fill = GridBagConstraints.HORIZONTAL;
129
        c.anchor = GridBagConstraints.WEST;
130
        c.gridx = 0;
131
        c.gridy = 0;
132
        c.gridwidth = 1;
133
        this.labelUE.setVisible(this.ue);
134
 
135
        /*******************************************************************************************
136
         * MONTANT HT
137
         ******************************************************************************************/
138
 
139
        this.checkHT = new JRadioButton("HT");
140
        this.checkHT.addActionListener(new ActionListener() {
141
            public void actionPerformed(ActionEvent e) {
142
                setHT(true);
143
            }
144
        });
145
        c.weightx = 0;
146
        this.add(this.checkHT, c);
147
 
148
        this.textHT = new DeviseField();
149
 
150
        c.gridx++;
151
        c.weightx = 1;
152
        this.add(this.textHT, c);
153
 
154
        /*******************************************************************************************
155
         * CHOIX TAXE
156
         ******************************************************************************************/
157
        c.gridx++;
158
        c.weightx = 0;
159
        this.add(new JLabel("TVA"), c);
160
        // choix taxe
161
        c.insets = new Insets(2, 10, 1, 2);
162
        this.comboTaxe = new ElementComboBox(false, 8);
163
        this.comboTaxe.setButtonsVisible(false);
164
 
165
        c.gridx++;
166
        this.add(this.comboTaxe, c);
167
 
168
        this.comboTaxe.addValueListener(new PropertyChangeListener() {
169
            public void propertyChange(PropertyChangeEvent evt) {
170
                calculMontant();
171
            }
172
        });
173
        c.insets = new Insets(2, 2, 1, 2);
174
 
175
        // Montant taxe
176
        this.textTaxe = new DeviseField();
177
        this.textTaxe.setEditable(false);
178
        this.textTaxe.setEnabled(false);
179
        c.gridx++;
180
        c.weightx = 0;
181
        this.add(this.textTaxe, c);
182
 
183
        /*******************************************************************************************
184
         * MONTANT TTC
185
         ******************************************************************************************/
186
 
187
        this.checkTTC = new JRadioButton("TTC");
188
        this.checkTTC.addActionListener(new ActionListener() {
189
            public void actionPerformed(ActionEvent e) {
190
                setHT(false);
191
            }
192
        });
193
 
194
        c.gridy++;
195
        c.gridx = 0;
196
        c.weightx = 0;
197
        this.add(this.checkTTC, c);
198
        this.textTTC = new DeviseField();
199
        c.gridx++;
200
        c.weightx = 1;
201
        this.add(this.textTTC, c);
202
        c.gridx++;
203
        c.gridwidth = GridBagConstraints.REMAINDER;
204
        this.labelUE.setHorizontalAlignment(SwingConstants.CENTER);
205
        this.add(this.labelUE, c);
206
 
207
        ButtonGroup grp1 = new ButtonGroup();
208
        grp1.add(this.checkTTC);
209
        grp1.add(this.checkHT);
210
        this.checkHT.setSelected(true);
211
        setHT(true);
212
        this.textTTC.getDocument().addDocumentListener(this.listenerTextTTC);
213
        this.textHT.getDocument().addDocumentListener(this.listenerTextHT);
177 ilm 214
        this.textTaxe.getDocument().addDocumentListener(this.listenerTextTaxe);
18 ilm 215
    }
216
 
217
    private void setHT(boolean b) {
218
        if (b) {
219
            this.textHT.setEditable(true);
220
            this.textHT.setEnabled(true);
221
            this.textTTC.setEditable(false);
222
            this.textTTC.setEnabled(false);
223
        } else {
224
            this.textHT.setEditable(false);
225
            this.textHT.setEnabled(false);
226
            this.textTTC.setEditable(true);
227
            this.textTTC.setEnabled(true);
228
        }
229
    }
230
 
231
    public void calculMontant() {
232
 
233
        if (this.enabled) {
234
 
177 ilm 235
            float taux;
236
            PrixHT pHT;
237
            PrixTTC pTTC;
18 ilm 238
            // taux de la TVA selectionnee
239
            int idTaxe = this.comboTaxe.getSelectedId();
240
            if (idTaxe > 1) {
80 ilm 241
                SQLRow ligneTaxe = SQLBackgroundTableCache.getInstance().getCacheForTable(((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("TAXE"))
242
                        .getRowFromId(idTaxe);
18 ilm 243
                taux = (ligneTaxe.getFloat("TAUX")) / 100.0F;
244
 
245
                // calcul des montants HT ou TTC
246
                if (this.checkHT.isSelected()) {
247
 
248
                    if (this.textHT.getText().trim().length() > 0) {
249
                        pHT = new PrixHT(0);
80 ilm 250
                        try {
251
                            if (!this.textHT.getText().trim().equals("-")) {
252
                                pHT = new PrixHT(GestionDevise.parseLongCurrency(this.textHT.getText()));
253
                            }
254
                        } catch (Exception e) {
255
                            // text is not corret, default to 0
18 ilm 256
                        }
257
                        // affichage
258
                        String tva = GestionDevise.currencyToString(pHT.calculLongTVA(taux));
259
 
260
                        String ttc;
261
                        if (this.ue) {
262
                            ttc = this.textHT.getText();
263
                        } else {
264
                            ttc = GestionDevise.currencyToString(pHT.calculLongTTC(taux));
265
                        }
80 ilm 266
                        updateText(tva, ttc, pHT.toString(), true);
177 ilm 267
                    } else {
80 ilm 268
                        updateText("", "", "", true);
177 ilm 269
                    }
18 ilm 270
                } else {
271
 
272
                    if (this.textTTC.getText().trim().length() > 0) {
273
 
274
                        pTTC = new PrixTTC(0);
80 ilm 275
                        try {
276
                            if (!this.textTTC.getText().trim().equals("-")) {
277
                                pTTC = new PrixTTC(GestionDevise.parseLongCurrency(this.textTTC.getText()));
278
                            }
279
                        } catch (Exception e) {
280
                            // text is not corret, default to 0
18 ilm 281
                        }
282
                        String tva;
283
                        // affichage
284
                        if (this.ue) {
285
                            PrixHT prixHT = new PrixHT(pTTC.getLongValue());
286
                            tva = GestionDevise.currencyToString(prixHT.calculLongTVA(taux));
287
                        } else {
288
                            tva = GestionDevise.currencyToString(pTTC.calculLongTVA(taux));
289
                        }
290
                        String ht;
291
                        if (this.ue) {
292
                            ht = this.textTTC.getText();
293
                        } else {
294
                            ht = GestionDevise.currencyToString(pTTC.calculLongHT(taux));
295
                        }
80 ilm 296
                        updateText(tva, pTTC.toString(), ht, false);
18 ilm 297
                    } else
80 ilm 298
                        updateText("", "", "", false);
18 ilm 299
                }
300
            }
301
        }
302
    }
303
 
80 ilm 304
    private void updateText(final String prixTVA, final String prixTTC, final String prixHT, final boolean isHT) {
18 ilm 305
        SwingUtilities.invokeLater(new Runnable() {
306
 
307
            public void run() {
308
                MontantPanel.this.textHT.getDocument().removeDocumentListener(MontantPanel.this.listenerTextHT);
309
                MontantPanel.this.textTTC.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTTC);
177 ilm 310
                MontantPanel.this.textTaxe.getDocument().removeDocumentListener(MontantPanel.this.listenerTextTaxe);
80 ilm 311
                // Update text with formated values
312
                if (!isHT) {
18 ilm 313
                    MontantPanel.this.textHT.setText(prixHT);
314
                } else {
315
                    MontantPanel.this.textTTC.setText(prixTTC);
316
                }
317
                MontantPanel.this.textTaxe.setText(prixTVA);
80 ilm 318
                setHT(isHT);
18 ilm 319
                MontantPanel.this.textHT.getDocument().addDocumentListener(MontantPanel.this.listenerTextHT);
320
                MontantPanel.this.textTTC.getDocument().addDocumentListener(MontantPanel.this.listenerTextTTC);
177 ilm 321
                MontantPanel.this.textTaxe.getDocument().addDocumentListener(MontantPanel.this.listenerTextTaxe);
18 ilm 322
            }
323
        });
324
    }
325
 
326
    public void setEnabled(boolean b) {
327
        this.enabled = b;
328
        this.checkHT.setEnabled(b);
329
        this.checkTTC.setEnabled(b);
177 ilm 330
        this.setHT(this.checkHT.isSelected());
18 ilm 331
    }
332
 
333
    public DeviseField getMontantTTC() {
334
        return this.textTTC;
335
    }
336
 
337
    public DeviseField getMontantHT() {
338
        return this.textHT;
339
    }
340
 
341
    public DeviseField getMontantTVA() {
342
        return this.textTaxe;
343
    }
344
 
345
    public ElementComboBox getChoixTaxe() {
346
        return this.comboTaxe;
347
    }
348
 
349
    public void setChoixTaxe(int value) {
350
        this.comboTaxe.setValue(value);
351
    }
352
 
353
    public void setUE(boolean b) {
354
        this.ue = b;
355
        this.labelUE.setVisible(b);
356
        calculMontant();
357
    }
177 ilm 358
 
18 ilm 359
}