102 |
ilm |
1 |
package org.openconcerto.modules.common.batchprocessing;
|
|
|
2 |
|
|
|
3 |
import java.awt.GridBagConstraints;
|
|
|
4 |
import java.awt.GridBagLayout;
|
|
|
5 |
import java.awt.event.ActionEvent;
|
|
|
6 |
import java.awt.event.ActionListener;
|
|
|
7 |
import java.math.BigDecimal;
|
|
|
8 |
import java.sql.SQLException;
|
|
|
9 |
import java.util.List;
|
|
|
10 |
|
|
|
11 |
import javax.swing.ButtonGroup;
|
|
|
12 |
import javax.swing.JPanel;
|
|
|
13 |
import javax.swing.JRadioButton;
|
|
|
14 |
import javax.swing.JTextField;
|
|
|
15 |
|
183 |
ilm |
16 |
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
|
|
|
17 |
import org.openconcerto.erp.utils.ConvertDevise;
|
|
|
18 |
import org.openconcerto.sql.model.SQLField;
|
181 |
ilm |
19 |
import org.openconcerto.sql.model.SQLRow;
|
102 |
ilm |
20 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
21 |
import org.openconcerto.sql.model.SQLRowValues;
|
183 |
ilm |
22 |
import org.openconcerto.sql.model.Where;
|
|
|
23 |
import org.openconcerto.sql.request.UpdateBuilder;
|
102 |
ilm |
24 |
import org.openconcerto.ui.DefaultGridBagConstraints;
|
|
|
25 |
|
|
|
26 |
public class NumberProcessor extends JPanel implements BatchProcessor {
|
|
|
27 |
|
181 |
ilm |
28 |
private final BatchField batchfield;
|
102 |
ilm |
29 |
// Editors
|
|
|
30 |
final JTextField tReplace = new JTextField();
|
|
|
31 |
private JRadioButton bReplace;
|
|
|
32 |
final JTextField tAdd = new JTextField();
|
|
|
33 |
private JRadioButton bAdd;
|
|
|
34 |
|
|
|
35 |
final JTextField tRemove = new JTextField();
|
|
|
36 |
private JRadioButton bRemove;
|
|
|
37 |
|
181 |
ilm |
38 |
public NumberProcessor(BatchField field) {
|
|
|
39 |
this.batchfield = field;
|
102 |
ilm |
40 |
|
|
|
41 |
this.setLayout(new GridBagLayout());
|
|
|
42 |
bReplace = new JRadioButton("remplacer par");
|
|
|
43 |
bAdd = new JRadioButton("augmenter de");
|
|
|
44 |
bRemove = new JRadioButton("diminuer de");
|
|
|
45 |
|
|
|
46 |
final ButtonGroup group = new ButtonGroup();
|
|
|
47 |
group.add(bReplace);
|
|
|
48 |
group.add(bAdd);
|
|
|
49 |
group.add(bRemove);
|
|
|
50 |
|
|
|
51 |
GridBagConstraints c = new DefaultGridBagConstraints();
|
|
|
52 |
// replace
|
|
|
53 |
this.add(bReplace, c);
|
|
|
54 |
c.gridx++;
|
|
|
55 |
c.weightx = 1;
|
|
|
56 |
this.add(tReplace, c);
|
|
|
57 |
// add
|
|
|
58 |
c.gridx = 0;
|
|
|
59 |
c.gridy++;
|
|
|
60 |
c.weightx = 0;
|
|
|
61 |
this.add(bAdd, c);
|
|
|
62 |
c.gridx++;
|
|
|
63 |
c.weightx = 1;
|
|
|
64 |
this.add(tAdd, c);
|
|
|
65 |
// remove
|
|
|
66 |
c.gridx = 0;
|
|
|
67 |
c.gridy++;
|
|
|
68 |
c.weightx = 0;
|
|
|
69 |
this.add(bRemove, c);
|
|
|
70 |
c.gridx++;
|
|
|
71 |
c.weightx = 1;
|
|
|
72 |
this.add(tRemove, c);
|
|
|
73 |
//
|
|
|
74 |
tAdd.setEnabled(false);
|
|
|
75 |
tRemove.setEnabled(false);
|
|
|
76 |
|
|
|
77 |
tAdd.setInputVerifier(new DecimalOrPercentVerifier());
|
|
|
78 |
tRemove.setInputVerifier(new DecimalOrPercentVerifier());
|
|
|
79 |
|
|
|
80 |
group.setSelected(bReplace.getModel(), true);
|
|
|
81 |
|
|
|
82 |
bReplace.addActionListener(new ActionListener() {
|
|
|
83 |
|
|
|
84 |
@Override
|
|
|
85 |
public void actionPerformed(ActionEvent e) {
|
|
|
86 |
tReplace.setEnabled(true);
|
|
|
87 |
tAdd.setEnabled(false);
|
|
|
88 |
tRemove.setEnabled(false);
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
bAdd.addActionListener(new ActionListener() {
|
|
|
92 |
|
|
|
93 |
@Override
|
|
|
94 |
public void actionPerformed(ActionEvent e) {
|
|
|
95 |
tReplace.setEnabled(false);
|
|
|
96 |
tAdd.setEnabled(true);
|
|
|
97 |
tRemove.setEnabled(false);
|
|
|
98 |
|
|
|
99 |
}
|
|
|
100 |
});
|
|
|
101 |
bRemove.addActionListener(new ActionListener() {
|
|
|
102 |
|
|
|
103 |
@Override
|
|
|
104 |
public void actionPerformed(ActionEvent e) {
|
|
|
105 |
tReplace.setEnabled(false);
|
|
|
106 |
tAdd.setEnabled(false);
|
|
|
107 |
tRemove.setEnabled(true);
|
|
|
108 |
|
|
|
109 |
}
|
|
|
110 |
});
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
@Override
|
183 |
ilm |
114 |
public void process(List<SQLRowAccessor> r) throws SQLException {
|
102 |
ilm |
115 |
if (bReplace.isSelected()) {
|
|
|
116 |
BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
|
183 |
ilm |
117 |
final SQLField fieldToMatch = batchfield.getFieldToMatch();
|
|
|
118 |
if (fieldToMatch != null) {
|
181 |
ilm |
119 |
|
183 |
ilm |
120 |
UpdateBuilder build = new UpdateBuilder(fieldToMatch.getTable());
|
|
|
121 |
build.setObject(batchfield.getField().getName(), (BigDecimal) decimalToFieldType(v));
|
|
|
122 |
build.setWhere(
|
|
|
123 |
Where.inValues(fieldToMatch.getTable().getField("ID_" + r.get(0).getTable().getName()), SQLRow.getIDs(r)).and(new Where(fieldToMatch, "=", batchfield.getMatchingValue())));
|
|
|
124 |
fieldToMatch.getTable().getDBSystemRoot().getDataSource().execute(build.asString());
|
181 |
ilm |
125 |
|
183 |
ilm |
126 |
} else {
|
|
|
127 |
for (SQLRowAccessor sqlRowAccessor : r) {
|
|
|
128 |
|
|
|
129 |
if (batchfield.getForeignLinkRow() != null) {
|
|
|
130 |
|
|
|
131 |
final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
|
|
|
132 |
if (referentRow.isEmpty()) {
|
|
|
133 |
SQLRowValues rowVals = new SQLRowValues(batchfield.getField().getTable());
|
|
|
134 |
rowVals.put("ID_" + sqlRowAccessor.getTable().getName(), sqlRowAccessor.getID());
|
|
|
135 |
rowVals.put("ID_" + batchfield.getForeignLinkRow().getTable().getName(), batchfield.getForeignLinkRow().getID());
|
|
|
136 |
rowVals.put(batchfield.getField().getName(), decimalToFieldType(v));
|
|
|
137 |
final BigDecimal taux = BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(sqlRowAccessor.getForeignID("ID_TAXE")));
|
|
|
138 |
final BigDecimal ht = (BigDecimal) decimalToFieldType(v);
|
|
|
139 |
final BigDecimal ttc = ConvertDevise.getTtcFromHt(ht, taux, rowVals.getTable().getField("PV_TTC").getType().getDecimalDigits());
|
|
|
140 |
rowVals.put("PV_TTC", ttc);
|
|
|
141 |
rowVals.put("PRIX_METRIQUE_VT_1", decimalToFieldType(v));
|
|
|
142 |
rowVals.commit();
|
|
|
143 |
} else {
|
|
|
144 |
for (SQLRow sqlRowT : referentRow) {
|
|
|
145 |
SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
|
|
|
146 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(v));
|
|
|
147 |
if (!batchfield.getField().getName().equals("QTE_MIN") && !batchfield.getField().getName().equals("POURCENT_REMISE")) {
|
|
|
148 |
// processBeforeUpdate(sqlRowT, rowValues);
|
|
|
149 |
final BigDecimal taux = sqlRowT.isForeignEmpty("ID_TAXE") ? BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(sqlRowAccessor.getForeignID("ID_TAXE")))
|
|
|
150 |
: BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(sqlRowT.getForeignID("ID_TAXE")));
|
|
|
151 |
final BigDecimal ht = rowValues.getBigDecimal("PV_HT");
|
|
|
152 |
final BigDecimal ttc = ConvertDevise.getTtcFromHt(ht, taux, rowValues.getTable().getField("PV_TTC").getType().getDecimalDigits());
|
|
|
153 |
rowValues.put("PV_TTC", ttc);
|
|
|
154 |
rowValues.put("PRIX_METRIQUE_VT_1", ht);
|
|
|
155 |
}
|
|
|
156 |
rowValues.update();
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
} else {
|
|
|
160 |
final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
|
181 |
ilm |
161 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(v));
|
183 |
ilm |
162 |
processBeforeUpdate(sqlRowAccessor, rowValues);
|
181 |
ilm |
163 |
rowValues.update();
|
|
|
164 |
}
|
|
|
165 |
}
|
102 |
ilm |
166 |
}
|
|
|
167 |
} else if (bAdd.isSelected()) {
|
|
|
168 |
|
|
|
169 |
String t = this.tAdd.getText().trim();
|
|
|
170 |
boolean isPercent = false;
|
|
|
171 |
if (t.endsWith("%")) {
|
|
|
172 |
t = t.substring(0, t.length() - 1);
|
|
|
173 |
isPercent = true;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
BigDecimal v = new BigDecimal(t);
|
|
|
177 |
|
|
|
178 |
for (SQLRowAccessor sqlRowAccessor : r) {
|
181 |
ilm |
179 |
|
|
|
180 |
if (batchfield.getForeignLinkRow() != null) {
|
|
|
181 |
|
|
|
182 |
final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
|
|
|
183 |
for (SQLRow sqlRowT : referentRow) {
|
|
|
184 |
|
|
|
185 |
SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
|
|
|
186 |
BigDecimal value = sqlRowT.asRow().getBigDecimal(batchfield.getField().getName());
|
|
|
187 |
|
|
|
188 |
if (value != null) {
|
|
|
189 |
if (isPercent) {
|
|
|
190 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
|
|
|
191 |
} else {
|
|
|
192 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
|
|
|
193 |
}
|
|
|
194 |
processBeforeUpdate(sqlRowT, rowValues);
|
|
|
195 |
rowValues.update();
|
|
|
196 |
}
|
102 |
ilm |
197 |
}
|
181 |
ilm |
198 |
} else {
|
|
|
199 |
|
|
|
200 |
final SQLRowValues rowValues;
|
|
|
201 |
final BigDecimal value;
|
|
|
202 |
|
|
|
203 |
rowValues = sqlRowAccessor.createEmptyUpdateRow();
|
|
|
204 |
value = sqlRowAccessor.asRow().getBigDecimal(batchfield.getField().getName());
|
|
|
205 |
|
|
|
206 |
if (value != null) {
|
|
|
207 |
if (isPercent) {
|
|
|
208 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
|
|
|
209 |
} else {
|
|
|
210 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
|
|
|
211 |
}
|
|
|
212 |
processBeforeUpdate(sqlRowAccessor, rowValues);
|
|
|
213 |
rowValues.update();
|
|
|
214 |
}
|
102 |
ilm |
215 |
}
|
|
|
216 |
}
|
|
|
217 |
} else if (bRemove.isSelected()) {
|
|
|
218 |
String t = this.tRemove.getText().trim();
|
|
|
219 |
boolean isPercent = false;
|
|
|
220 |
if (t.endsWith("%")) {
|
|
|
221 |
t = t.substring(0, t.length() - 1);
|
|
|
222 |
isPercent = true;
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
BigDecimal v = new BigDecimal(t);
|
|
|
226 |
for (SQLRowAccessor sqlRowAccessor : r) {
|
181 |
ilm |
227 |
if (batchfield.getForeignLinkRow() != null) {
|
|
|
228 |
final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
|
|
|
229 |
if (referentRow != null && !referentRow.isEmpty()) {
|
|
|
230 |
for (SQLRow sqlRowT : referentRow) {
|
102 |
ilm |
231 |
|
181 |
ilm |
232 |
SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
|
|
|
233 |
final BigDecimal value = sqlRowT.getBigDecimal(batchfield.getField().getName());
|
|
|
234 |
if (value != null) {
|
|
|
235 |
if (isPercent) {
|
|
|
236 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
|
|
|
237 |
} else {
|
|
|
238 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
|
|
|
239 |
}
|
|
|
240 |
processBeforeUpdate(sqlRowT, rowValues);
|
|
|
241 |
rowValues.update();
|
|
|
242 |
}
|
|
|
243 |
}
|
102 |
ilm |
244 |
}
|
181 |
ilm |
245 |
} else {
|
|
|
246 |
|
|
|
247 |
SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
|
|
|
248 |
final BigDecimal value = sqlRowAccessor.asRow().getBigDecimal(batchfield.getField().getName());
|
|
|
249 |
|
|
|
250 |
if (value != null) {
|
|
|
251 |
if (isPercent) {
|
|
|
252 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
|
|
|
253 |
} else {
|
|
|
254 |
rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
|
|
|
255 |
}
|
|
|
256 |
processBeforeUpdate(sqlRowAccessor, rowValues);
|
|
|
257 |
rowValues.update();
|
|
|
258 |
}
|
102 |
ilm |
259 |
}
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
private Object decimalToFieldType(BigDecimal v) {
|
181 |
ilm |
265 |
final Class<?> javaType = batchfield.getField().getType().getJavaType();
|
102 |
ilm |
266 |
if (javaType.equals(BigDecimal.class)) {
|
|
|
267 |
return v;
|
|
|
268 |
} else if (javaType.equals(Float.class)) {
|
|
|
269 |
return v.floatValue();
|
|
|
270 |
} else if (javaType.equals(Double.class)) {
|
|
|
271 |
return v.doubleValue();
|
|
|
272 |
} else if (javaType.equals(Integer.class)) {
|
|
|
273 |
return v.intValue();
|
|
|
274 |
} else if (javaType.equals(Long.class)) {
|
|
|
275 |
return v.longValue();
|
|
|
276 |
}
|
|
|
277 |
return v;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
@Override
|
|
|
281 |
public boolean checkParameters() {
|
|
|
282 |
if (bReplace.isSelected()) {
|
|
|
283 |
try {
|
|
|
284 |
BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
|
|
|
285 |
return v != null;
|
|
|
286 |
} catch (Exception e) {
|
|
|
287 |
return false;
|
|
|
288 |
}
|
|
|
289 |
} else if (bAdd.isSelected()) {
|
|
|
290 |
return tAdd.getInputVerifier().verify(tAdd);
|
|
|
291 |
|
|
|
292 |
} else if (bRemove.isSelected()) {
|
|
|
293 |
return tRemove.getInputVerifier().verify(tRemove);
|
|
|
294 |
}
|
|
|
295 |
return false;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
@Override
|
|
|
299 |
public void processBeforeUpdate(SQLRowAccessor from, SQLRowValues to) {
|
|
|
300 |
// TODO Auto-generated method stub
|
|
|
301 |
|
|
|
302 |
}
|
|
|
303 |
}
|