OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
19 ilm 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.sales.product.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
182 ilm 17
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
19 ilm 18
import org.openconcerto.sql.element.BaseSQLComponent;
19
import org.openconcerto.sql.element.SQLComponent;
182 ilm 20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowAccessor;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
19 ilm 24
import org.openconcerto.ui.DefaultGridBagConstraints;
182 ilm 25
import org.openconcerto.utils.DecimalUtils;
26
import org.openconcerto.utils.StringUtils;
19 ilm 27
 
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
182 ilm 30
import java.math.BigDecimal;
31
import java.math.RoundingMode;
19 ilm 32
import java.util.ArrayList;
182 ilm 33
import java.util.HashSet;
19 ilm 34
import java.util.List;
182 ilm 35
import java.util.Set;
19 ilm 36
 
37
import javax.swing.JLabel;
67 ilm 38
import javax.swing.JTextField;
182 ilm 39
import javax.swing.event.DocumentEvent;
19 ilm 40
 
41
public class ArticleTarifSQLElement extends ComptaSQLConfElement {
42
 
43
    public ArticleTarifSQLElement() {
44
        super("ARTICLE_TARIF", "un tarif d'article", "tarifs d'article");
45
    }
46
 
47
    protected List<String> getListFields() {
48
        final List<String> l = new ArrayList<String>();
182 ilm 49
        l.add("ID_TARIF");
50
        l.add("ID_ARTICLE");
51
        l.add("QTE");
52
        l.add("PV_HT");
83 ilm 53
        l.add("PV_TTC");
19 ilm 54
        return l;
55
    }
56
 
57
    protected List<String> getComboFields() {
58
        final List<String> l = new ArrayList<String>();
182 ilm 59
        l.add("ID_TARIF");
60
        l.add("ID_ARTICLE");
83 ilm 61
        l.add("PV_TTC");
19 ilm 62
        return l;
63
    }
64
 
90 ilm 65
    @Override
66
    protected String getParentFFName() {
67
        return "ID_ARTICLE";
68
    }
69
 
182 ilm 70
    @Override
71
    public Set<String> getInsertOnlyFields() {
72
        Set<String> req = new HashSet<>();
73
        req.add("ID_TARIF");
74
        req.add("ID_ARTICLE");
75
        return req;
76
    }
77
 
78
    @Override
79
    public Set<String> getReadOnlyFields() {
80
        Set<String> ro = new HashSet<>();
81
        ro.add("PV_HT");
82
        ro.add("PV_TTC");
83
        return ro;
84
    }
85
 
19 ilm 86
    public SQLComponent createComponent() {
87
        return new BaseSQLComponent(this) {
88
 
89
            public void addViews() {
90
                this.setLayout(new GridBagLayout());
91
                final GridBagConstraints c = new DefaultGridBagConstraints();
92
 
182 ilm 93
                c.gridx = 0;
94
                c.weightx = 0;
19 ilm 95
                // Nom
182 ilm 96
                this.add(new JLabel(getLabelFor("ID_TARIF")), c);
97
                c.gridx++;
98
                c.weightx = 1;
99
                final SQLRequestComboBox tarifCombo = new SQLRequestComboBox();
100
                this.add(tarifCombo, c);
101
                this.addRequiredSQLObject(tarifCombo, "ID_TARIF");
19 ilm 102
 
182 ilm 103
                c.gridy++;
104
                c.gridx = 0;
105
                c.weightx = 0;
106
                this.add(new JLabel(getLabelFor("ID_ARTICLE")), c);
19 ilm 107
                c.gridx++;
182 ilm 108
                c.weightx = 1;
109
                final SQLRequestComboBox artCombo = new SQLRequestComboBox();
110
                this.add(artCombo, c);
111
                this.addRequiredSQLObject(artCombo, "ID_ARTICLE");
19 ilm 112
 
182 ilm 113
                c.gridy++;
114
                c.gridx = 0;
115
                c.weightx = 0;
116
                JLabel labelQTE = new JLabel(getLabelFor("QTE"));
117
                JTextField textQTE = new JTextField();
118
                this.add(labelQTE, c);
119
                c.gridx++;
120
                c.weightx = 1;
121
                this.add(textQTE, c);
122
                this.addRequiredSQLObject(textQTE, "QTE");
19 ilm 123
 
182 ilm 124
                c.gridy++;
125
                c.gridx = 0;
126
                c.weightx = 0;
127
                JLabel labelRemise = new JLabel(getLabelFor("POURCENT_REMISE"));
128
                JTextField textRemise = new JTextField();
129
                this.add(labelRemise, c);
130
                c.gridx++;
131
                c.weightx = 1;
132
                this.add(textRemise, c);
133
                this.addSQLObject(textRemise, "POURCENT_REMISE");
134
 
135
                c.gridy++;
136
                c.gridx = 0;
137
                c.weightx = 0;
138
                // Nom
139
                JLabel labelPVM = new JLabel(getLabelFor("PRIX_METRIQUE_VT_1"));
140
                JTextField textPVM = new JTextField();
141
                this.add(labelPVM, c);
142
                c.gridx++;
143
                c.weightx = 1;
144
                this.add(textPVM, c);
145
                this.addSQLObject(textPVM, "PRIX_METRIQUE_VT_1");
146
 
147
                c.gridy++;
148
                c.gridx = 0;
149
                c.weightx = 0;
150
                JLabel labelPV = new JLabel(getLabelFor("PV_HT"));
151
                final JTextField textPV = new JTextField();
152
                this.add(labelPV, c);
153
                c.gridx++;
154
                c.weightx = 1;
155
                this.add(textPV, c);
156
                this.addSQLObject(textPV, "PV_HT");
157
 
158
                c.gridy++;
159
                c.gridx = 0;
160
                c.weightx = 0;
161
                this.add(new JLabel(getLabelFor("ID_TAXE")), c);
162
                c.gridx++;
163
                c.weightx = 1;
164
                final SQLRequestComboBox taxeCombo = new SQLRequestComboBox();
165
                this.add(taxeCombo, c);
166
                this.addSQLObject(taxeCombo, "ID_TAXE");
167
 
168
                c.gridy++;
169
                c.gridx = 0;
170
                c.weightx = 0;
171
                // Nom
172
                JLabel labelPVTTC = new JLabel(getLabelFor("PV_TTC"));
173
                final JTextField textTTC = new JTextField();
174
                this.add(labelPVTTC, c);
175
                c.gridx++;
176
                c.weightx = 1;
177
                this.add(textTTC, c);
178
                this.addSQLObject(textTTC, "PV_TTC");
179
 
180
                textPVM.getDocument().addDocumentListener(new org.openconcerto.utils.text.SimpleDocumentListener() {
181
                    @Override
182
                    public void update(DocumentEvent e) {
183
 
184
                        final BigDecimal ht = StringUtils.getBigDecimalFromUserText(textPVM.getText());
185
 
186
                        if (ht != null) {
187
 
188
                            SQLRowAccessor rTaxe = taxeCombo.getSelectedRowAccessor();
189
                            if (rTaxe == null || rTaxe.isUndefined()) {
190
                                SQLRowAccessor a = artCombo.getSelectedRowAccessor();
191
                                if (a != null && !a.isUndefined()) {
192
                                    rTaxe = a.asRow().fetchNewRow().getForeignRow("ID_TAXE");
193
                                }
194
                                if (rTaxe == null || rTaxe.isUndefined()) {
195
                                    rTaxe = TaxeCache.getCache().getFirstTaxe();
196
                                }
197
                            }
198
 
199
                            Float resultTaux = TaxeCache.getCache().getTauxFromId(rTaxe.getID());
200
 
201
                            if (resultTaux == null) {
202
                                final SQLRow i = TaxeCache.getCache().getFirstTaxe();
203
                                resultTaux = i.getFloat("TAUX");
204
                            }
205
 
206
                            float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
207
 
208
                            BigDecimal r = ht.multiply(BigDecimal.valueOf(taux).divide(BigDecimal.valueOf(100)).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
209
                            textPV.setText(textPVM.getText());
210
                            textTTC.setText(r.setScale(2, RoundingMode.HALF_UP).toString());
211
                        } else {
212
                            textPV.setText("");
213
                            textTTC.setText("");
214
                        }
215
                    }
216
                });
217
 
19 ilm 218
            }
219
        };
220
    }
57 ilm 221
 
222
    @Override
182 ilm 223
    public SQLRowValues createDefaultRowValues(String token) {
224
 
225
        SQLRowValues rowVals = super.createDefaultRowValues(token);
226
        rowVals.put("QTE", 1);
227
        return rowVals;
228
    }
229
 
230
    @Override
57 ilm 231
    protected String createCode() {
156 ilm 232
        return createCodeOfPackage() + ".price";
57 ilm 233
    }
19 ilm 234
}