OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 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.finance.accounting.element;
15
 
16
import org.openconcerto.erp.core.common.ui.DeviseField;
17
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18
import org.openconcerto.sql.element.BaseSQLComponent;
19
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
20
import org.openconcerto.sql.element.SQLComponent;
21
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
22
import org.openconcerto.ui.DefaultGridBagConstraints;
23
import org.openconcerto.utils.GestionDevise;
24
import org.openconcerto.utils.ListMap;
25
import org.openconcerto.utils.text.SimpleDocumentListener;
26
 
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.beans.PropertyChangeEvent;
30
import java.beans.PropertyChangeListener;
31
import java.util.ArrayList;
32
import java.util.HashSet;
33
import java.util.List;
34
import java.util.Set;
35
 
36
import javax.swing.JLabel;
37
import javax.swing.JTextField;
38
import javax.swing.event.DocumentEvent;
39
 
40
import com.ibm.icu.math.BigDecimal;
41
 
42
public class FraisDocumentSQLElement extends ComptaSQLConfElement {
43
 
44
    public FraisDocumentSQLElement() {
45
        super("FRAIS_DOCUMENT");
46
    }
47
 
48
    protected List<String> getListFields() {
49
        final List<String> list = new ArrayList<String>(2);
50
        list.add("CODE");
51
        list.add("NOM");
52
        list.add("MONTANT_HT");
53
        list.add("ID_TAXE");
54
        list.add("MONTANT_TTC");
55
        return list;
56
    }
57
 
58
    protected List<String> getComboFields() {
59
        final List<String> list = new ArrayList<String>(2);
60
        list.add("CODE");
61
        list.add("NOM");
62
        list.add("MONTANT_TTC");
63
        return list;
64
    }
65
 
66
    @Override
67
    public Set<String> getReadOnlyFields() {
68
        Set<String> s = new HashSet<>();
69
        s.add("MONTANT_TTC");
70
        return s;
71
    }
72
 
73
    @Override
74
    public ListMap<String, String> getShowAs() {
75
        return ListMap.singleton(null, "CODE", "NOM");
76
 
77
    }
78
 
79
    public SQLComponent createComponent() {
80
        return new BaseSQLComponent(this) {
81
 
82
            public void addViews() {
83
                this.setLayout(new GridBagLayout());
84
                final GridBagConstraints c = new DefaultGridBagConstraints();
85
 
86
                // Code
87
                final JLabel labelCode = new JLabel(getLabelFor("CODE"));
88
                c.weightx = 0;
89
                this.add(labelCode, c);
90
                c.gridx++;
91
                c.weightx = 1;
92
                final JTextField textCode = new JTextField();
93
                this.add(textCode, c);
94
 
95
                // Nom
96
                c.gridx++;
97
                c.weightx = 0;
98
                final JLabel labelNom = new JLabel(getLabelFor("NOM"));
99
                this.add(labelNom, c);
100
                c.gridx++;
101
                c.weightx = 1;
102
                final JTextField textNom = new JTextField();
103
                this.add(textNom, c);
104
 
105
                // Montant
106
                c.gridy++;
107
                c.gridx = 0;
108
                c.weightx = 0;
109
 
110
                final JLabel labelTaux = new JLabel(getLabelFor("MONTANT_HT"));
111
                this.add(labelTaux, c);
112
                c.gridx++;
113
                c.weightx = 1;
114
                final DeviseField textTaux = new DeviseField();
115
                this.add(textTaux, c);
116
 
117
                c.gridx++;
118
                c.weightx = 0;
119
 
120
                final JLabel labelTaxe = new JLabel(getLabelFor("ID_TAXE"));
121
                this.add(labelTaxe, c);
122
                c.gridx++;
123
                c.weightx = 1;
124
                final SQLRequestComboBox boxTaxe = new SQLRequestComboBox();
125
                this.add(boxTaxe, c);
126
 
127
                c.gridx++;
128
                c.weightx = 0;
129
 
130
                final JLabel labelTTC = new JLabel(getLabelFor("MONTANT_TTC"));
131
                this.add(labelTTC, c);
132
                c.gridx++;
133
                c.weightx = 1;
134
                final DeviseField textTTC = new DeviseField();
135
                textTTC.setEditable(false);
136
                this.add(textTTC, c);
137
 
138
                this.addRequiredSQLObject(boxTaxe, "ID_TAXE");
139
                boxTaxe.addModelListener("wantedID", new PropertyChangeListener() {
140
 
141
                    @Override
142
                    public void propertyChange(PropertyChangeEvent evt) {
143
                        textTTC.setValue(getMontantTTC(textTaux, boxTaxe));
144
                    }
145
                });
146
 
147
                textTaux.getDocument().addDocumentListener(new SimpleDocumentListener() {
148
 
149
                    @Override
150
                    public void update(DocumentEvent e) {
151
 
152
                        textTTC.setValue(getMontantTTC(textTaux, boxTaxe));
153
                    }
154
                });
155
 
156
                this.addRequiredSQLObject(textTTC, "MONTANT_TTC");
157
                this.addRequiredSQLObject(textTaux, "MONTANT_HT");
158
                this.addRequiredSQLObject(textNom, "NOM");
159
                this.addRequiredSQLObject(textCode, "CODE");
160
            }
161
 
162
            public long getMontantTTC(DeviseField fieldHT, SQLRequestComboBox boxTVA) {
163
                long l = 0;
164
 
165
                long p = GestionDevise.parseLongCurrency(fieldHT.getText().trim());
166
                if (!boxTVA.isEmpty()) {
167
                    Float t = TaxeCache.getCache().getTauxFromId(boxTVA.getWantedID());
168
                    if (t != null) {
169
                        l = new BigDecimal(p).multiply(new BigDecimal(t).movePointLeft(2).add(BigDecimal.ONE)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
170
                    }
171
                }
172
 
173
                return l;
174
            }
175
 
176
        };
177
    }
178
}