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.element;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
17 |
import org.openconcerto.erp.core.common.image.ImageIconWarning;
|
|
|
18 |
import org.openconcerto.erp.core.customerrelationship.customer.element.CourrierClientSQLElement;
|
93 |
ilm |
19 |
import org.openconcerto.erp.core.customerrelationship.customer.element.CustomerSQLElement;
|
18 |
ilm |
20 |
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
|
|
|
21 |
import org.openconcerto.erp.core.humanresources.payroll.element.SalarieSQLElement;
|
|
|
22 |
import org.openconcerto.erp.core.sales.credit.element.AvoirClientSQLElement;
|
|
|
23 |
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
|
|
|
24 |
import org.openconcerto.erp.core.sales.order.element.CommandeClientSQLElement;
|
|
|
25 |
import org.openconcerto.erp.core.sales.quote.element.DevisSQLElement;
|
|
|
26 |
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonSQLElement;
|
61 |
ilm |
27 |
import org.openconcerto.erp.core.supplychain.credit.element.AvoirFournisseurSQLElement;
|
18 |
ilm |
28 |
import org.openconcerto.erp.core.supplychain.order.element.CommandeSQLElement;
|
144 |
ilm |
29 |
import org.openconcerto.erp.core.supplychain.order.element.DemandePrixSQLElement;
|
18 |
ilm |
30 |
import org.openconcerto.erp.core.supplychain.receipt.element.BonReceptionSQLElement;
|
|
|
31 |
import org.openconcerto.sql.Configuration;
|
|
|
32 |
import org.openconcerto.sql.element.BaseSQLComponent;
|
|
|
33 |
import org.openconcerto.sql.element.SQLComponent;
|
|
|
34 |
import org.openconcerto.sql.element.SQLElement;
|
90 |
ilm |
35 |
import org.openconcerto.sql.model.IResultSetHandler;
|
|
|
36 |
import org.openconcerto.sql.model.SQLDataSource;
|
18 |
ilm |
37 |
import org.openconcerto.sql.model.SQLRow;
|
93 |
ilm |
38 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
18 |
ilm |
39 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
|
|
40 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
41 |
import org.openconcerto.sql.model.SQLSelect;
|
142 |
ilm |
42 |
import org.openconcerto.sql.model.SQLSelect.LockStrength;
|
18 |
ilm |
43 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
44 |
import org.openconcerto.sql.model.SQLTableEvent;
|
19 |
ilm |
45 |
import org.openconcerto.sql.model.SQLTableEvent.Mode;
|
18 |
ilm |
46 |
import org.openconcerto.sql.model.SQLTableModifiedListener;
|
|
|
47 |
import org.openconcerto.sql.model.Where;
|
|
|
48 |
import org.openconcerto.ui.DefaultGridBagConstraints;
|
142 |
ilm |
49 |
import org.openconcerto.utils.CollectionUtils;
|
25 |
ilm |
50 |
import org.openconcerto.utils.StringUtils;
|
21 |
ilm |
51 |
import org.openconcerto.utils.Tuple2;
|
25 |
ilm |
52 |
import org.openconcerto.utils.text.SimpleDocumentListener;
|
18 |
ilm |
53 |
|
|
|
54 |
import java.awt.GridBagConstraints;
|
|
|
55 |
import java.awt.GridBagLayout;
|
|
|
56 |
import java.sql.SQLException;
|
|
|
57 |
import java.text.DateFormat;
|
|
|
58 |
import java.text.DecimalFormat;
|
21 |
ilm |
59 |
import java.text.ParseException;
|
18 |
ilm |
60 |
import java.text.SimpleDateFormat;
|
|
|
61 |
import java.util.ArrayList;
|
21 |
ilm |
62 |
import java.util.Arrays;
|
25 |
ilm |
63 |
import java.util.Collections;
|
|
|
64 |
import java.util.Comparator;
|
18 |
ilm |
65 |
import java.util.Date;
|
|
|
66 |
import java.util.HashMap;
|
|
|
67 |
import java.util.List;
|
|
|
68 |
import java.util.Map;
|
25 |
ilm |
69 |
import java.util.Set;
|
156 |
ilm |
70 |
import java.util.Map.Entry;
|
18 |
ilm |
71 |
|
|
|
72 |
import javax.swing.Icon;
|
28 |
ilm |
73 |
import javax.swing.JCheckBox;
|
18 |
ilm |
74 |
import javax.swing.JLabel;
|
90 |
ilm |
75 |
import javax.swing.JOptionPane;
|
18 |
ilm |
76 |
import javax.swing.JTextField;
|
61 |
ilm |
77 |
import javax.swing.SwingConstants;
|
18 |
ilm |
78 |
import javax.swing.event.DocumentEvent;
|
|
|
79 |
|
90 |
ilm |
80 |
import org.apache.commons.dbutils.ResultSetHandler;
|
|
|
81 |
|
18 |
ilm |
82 |
// FIXME bug JTextField for input bigInt
|
|
|
83 |
|
|
|
84 |
public class NumerotationAutoSQLElement extends ComptaSQLConfElement {
|
|
|
85 |
|
|
|
86 |
private static final String FORMAT = "_FORMAT";
|
|
|
87 |
private static final String START = "_START";
|
|
|
88 |
|
|
|
89 |
public NumerotationAutoSQLElement() {
|
|
|
90 |
super("NUMEROTATION_AUTO", "une numérotation automatique", "numérotations automatiques");
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
protected List<String> getListFields() {
|
|
|
94 |
final List<String> list = new ArrayList<String>(2);
|
94 |
ilm |
95 |
list.add("NOM");
|
18 |
ilm |
96 |
return list;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
protected List<String> getComboFields() {
|
|
|
100 |
final List<String> list = new ArrayList<String>(2);
|
94 |
ilm |
101 |
list.add("NOM");
|
18 |
ilm |
102 |
return list;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/*
|
|
|
106 |
* (non-Javadoc)
|
|
|
107 |
*
|
|
|
108 |
* @see org.openconcerto.devis.SQLElement#getComponent()
|
|
|
109 |
*/
|
|
|
110 |
public SQLComponent createComponent() {
|
|
|
111 |
return new BaseSQLComponent(this) {
|
|
|
112 |
|
|
|
113 |
private Icon iconWarning = ImageIconWarning.getInstance();
|
|
|
114 |
|
|
|
115 |
public void addViews() {
|
|
|
116 |
this.setLayout(new GridBagLayout());
|
|
|
117 |
final GridBagConstraints c = new DefaultGridBagConstraints();
|
|
|
118 |
|
25 |
ilm |
119 |
Set<Class<? extends SQLElement>> s = map.keySet();
|
18 |
ilm |
120 |
|
25 |
ilm |
121 |
final ArrayList<Class<? extends SQLElement>> list = new ArrayList<Class<? extends SQLElement>>(s);
|
|
|
122 |
Collections.sort(list, new Comparator<Class<? extends SQLElement>>() {
|
|
|
123 |
public int compare(Class<? extends SQLElement> o1, Class<? extends SQLElement> o2) {
|
28 |
ilm |
124 |
return o1.getSimpleName().toString().compareTo(o2.getSimpleName().toString());
|
25 |
ilm |
125 |
};
|
|
|
126 |
});
|
18 |
ilm |
127 |
|
28 |
ilm |
128 |
List<String> added = new ArrayList<String>();
|
25 |
ilm |
129 |
for (Class<? extends SQLElement> class1 : list) {
|
|
|
130 |
String prefix = map.get(class1);
|
28 |
ilm |
131 |
if (!added.contains(prefix)) {
|
|
|
132 |
c.gridy++;
|
|
|
133 |
c.gridx = 0;
|
|
|
134 |
c.weightx = 0;
|
|
|
135 |
added.add(prefix);
|
|
|
136 |
SQLElement elt = Configuration.getInstance().getDirectory().getElement(class1);
|
80 |
ilm |
137 |
if (elt == null) {
|
|
|
138 |
throw new IllegalArgumentException("Element null for class " + class1);
|
|
|
139 |
}
|
28 |
ilm |
140 |
// Avoir
|
61 |
ilm |
141 |
JLabel labelAvoirFormat = new JLabel(StringUtils.firstUp(elt.getPluralName()) + " " + getLabelFor(prefix + FORMAT), SwingConstants.RIGHT);
|
28 |
ilm |
142 |
this.add(labelAvoirFormat, c);
|
|
|
143 |
c.gridx++;
|
|
|
144 |
c.weightx = 1;
|
|
|
145 |
final JTextField fieldFormat = new JTextField();
|
|
|
146 |
this.add(fieldFormat, c);
|
18 |
ilm |
147 |
|
28 |
ilm |
148 |
final JLabel labelAvoirStart = new JLabel(getLabelFor(prefix + START));
|
|
|
149 |
c.gridx++;
|
|
|
150 |
c.weightx = 0;
|
|
|
151 |
this.add(labelAvoirStart, c);
|
|
|
152 |
c.gridx++;
|
|
|
153 |
c.weightx = 1;
|
|
|
154 |
final JTextField fieldStart = new JTextField();
|
|
|
155 |
this.add(fieldStart, c);
|
18 |
ilm |
156 |
|
28 |
ilm |
157 |
final JLabel labelResult = new JLabel();
|
|
|
158 |
c.gridx++;
|
|
|
159 |
c.weightx = 0;
|
|
|
160 |
this.add(labelResult, c);
|
18 |
ilm |
161 |
|
28 |
ilm |
162 |
if (getTable().getFieldsName().contains(prefix + AUTO_MONTH)) {
|
|
|
163 |
final JCheckBox boxAuto = new JCheckBox(getLabelFor(prefix + AUTO_MONTH));
|
|
|
164 |
c.gridx++;
|
|
|
165 |
c.weightx = 0;
|
|
|
166 |
this.add(boxAuto, c);
|
|
|
167 |
this.addSQLObject(boxAuto, prefix + AUTO_MONTH);
|
|
|
168 |
}
|
|
|
169 |
// Affichage dynamique du résultat
|
|
|
170 |
SimpleDocumentListener listener = new SimpleDocumentListener() {
|
18 |
ilm |
171 |
|
28 |
ilm |
172 |
@Override
|
|
|
173 |
public void update(DocumentEvent e) {
|
|
|
174 |
updateLabel(fieldStart, fieldFormat, labelResult);
|
18 |
ilm |
175 |
|
28 |
ilm |
176 |
}
|
|
|
177 |
};
|
18 |
ilm |
178 |
|
28 |
ilm |
179 |
fieldFormat.getDocument().addDocumentListener(listener);
|
|
|
180 |
fieldStart.getDocument().addDocumentListener(listener);
|
18 |
ilm |
181 |
|
28 |
ilm |
182 |
this.addRequiredSQLObject(fieldFormat, prefix + FORMAT);
|
|
|
183 |
this.addRequiredSQLObject(fieldStart, prefix + START);
|
|
|
184 |
}
|
25 |
ilm |
185 |
}
|
18 |
ilm |
186 |
|
|
|
187 |
// JLabel labelCodeLettrage = new JLabel(getLabelFor("CODE_LETTRAGE"));
|
|
|
188 |
// c.gridy++;
|
|
|
189 |
// c.gridx = 0;
|
|
|
190 |
// c.weightx = 0;
|
|
|
191 |
// this.add(labelCodeLettrage, c);
|
|
|
192 |
// c.gridx++;
|
|
|
193 |
// c.weightx = 1;
|
|
|
194 |
// this.add(this.textCodeLettrage, c);
|
|
|
195 |
//
|
|
|
196 |
// c.gridx++;
|
|
|
197 |
// c.weightx = 0;
|
|
|
198 |
// labelNextCodeLettrage = new JLabel();
|
|
|
199 |
// this.add(labelNextCodeLettrage, c);
|
|
|
200 |
|
|
|
201 |
JLabel labelExemple = new JLabel("Exemple de format : 'Fact'yyyy0000");
|
|
|
202 |
c.gridy++;
|
|
|
203 |
c.gridx = 0;
|
|
|
204 |
c.gridwidth = GridBagConstraints.REMAINDER;
|
|
|
205 |
c.weighty = 1;
|
|
|
206 |
c.anchor = GridBagConstraints.NORTHWEST;
|
|
|
207 |
this.add(labelExemple, c);
|
|
|
208 |
|
|
|
209 |
// this.textCodeLettrage.getDocument().addDocumentListener(this.listenText);
|
|
|
210 |
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
// private void updateLabelNextCode() {
|
|
|
214 |
// String s = getNextCodeLetrrage(this.textCodeLettrage.getText());
|
|
|
215 |
// this.labelNextCodeLettrage.setText(donne + " " + s);
|
|
|
216 |
// }
|
|
|
217 |
|
|
|
218 |
private void updateLabel(JTextField textStart, JTextField textFormat, JLabel label) {
|
|
|
219 |
if (textStart.getText().trim().length() > 0) {
|
90 |
ilm |
220 |
try {
|
|
|
221 |
String numProposition = getNextNumero(textFormat.getText(), Integer.parseInt(textStart.getText()), new Date());
|
18 |
ilm |
222 |
|
90 |
ilm |
223 |
if (numProposition != null) {
|
|
|
224 |
label.setText(" --> " + numProposition);
|
|
|
225 |
label.setIcon(null);
|
|
|
226 |
} else {
|
|
|
227 |
label.setIcon(this.iconWarning);
|
|
|
228 |
label.setText("");
|
|
|
229 |
}
|
|
|
230 |
} catch (IllegalArgumentException e) {
|
|
|
231 |
JOptionPane.showMessageDialog(null, "Le format " + textFormat.getText() + " n'est pas valide!");
|
18 |
ilm |
232 |
}
|
|
|
233 |
} else {
|
|
|
234 |
label.setIcon(this.iconWarning);
|
|
|
235 |
label.setText("");
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
};
|
|
|
240 |
}
|
|
|
241 |
|
28 |
ilm |
242 |
public static final String getLabelFormatFor(Class<? extends SQLElement> clazz) {
|
|
|
243 |
return map.get(clazz) + FORMAT;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
public static final String getLabelNumberFor(Class<? extends SQLElement> clazz) {
|
|
|
247 |
return map.get(clazz) + START;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
public final static String AUTO_MONTH = "_AUTO_MONTH";
|
|
|
251 |
|
142 |
ilm |
252 |
public static final boolean isAuto(Class<? extends SQLElement> clazz, SQLRowAccessor rowNum) throws IllegalArgumentException {
|
|
|
253 |
final String s = getName(clazz);
|
|
|
254 |
return TABLE_NUM.getFieldsName().contains(s + AUTO_MONTH) && rowNum.getBoolean(s + AUTO_MONTH);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
public static final void updateNextNumero(final Class<? extends SQLElement> clazz, final Date storedDate, final String storedNumero) throws SQLException {
|
|
|
258 |
final SQLSelect sel = new SQLSelect();
|
|
|
259 |
sel.addSelectStar(TABLE_NUM);
|
|
|
260 |
sel.setWhere(new Where(TABLE_NUM.getKey(), "=", 2));
|
|
|
261 |
sel.setLockStrength(LockStrength.UPDATE);
|
|
|
262 |
final SQLRow row = CollectionUtils.getSole(SQLRowListRSH.execute(sel, false, false));
|
|
|
263 |
if (isAuto(clazz, row))
|
|
|
264 |
return;
|
|
|
265 |
if (NumerotationAutoSQLElement.getNextNumero(clazz, storedDate, row).equalsIgnoreCase(storedNumero.trim())) {
|
|
|
266 |
final String numberLabel = NumerotationAutoSQLElement.getLabelNumberFor(clazz);
|
|
|
267 |
row.createEmptyUpdateRow().put(numberLabel, row.getInt(numberLabel) + 1).update();
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
|
90 |
ilm |
271 |
/*
|
|
|
272 |
*
|
|
|
273 |
*
|
|
|
274 |
* @throws IllegalArgumentException pattern incorrect
|
|
|
275 |
*/
|
|
|
276 |
public static final String getNextNumero(Class<? extends SQLElement> clazz) throws IllegalArgumentException {
|
28 |
ilm |
277 |
return getNextNumero(clazz, new Date());
|
|
|
278 |
}
|
|
|
279 |
|
90 |
ilm |
280 |
/*
|
|
|
281 |
* Format du type 'Fact'yyyy-MM-dd0000
|
|
|
282 |
*
|
|
|
283 |
* @throws IllegalArgumentException pattern incorrect
|
|
|
284 |
*/
|
|
|
285 |
public static final String getNextNumero(Class<? extends SQLElement> clazz, Date d) throws IllegalArgumentException {
|
93 |
ilm |
286 |
return getNextNumero(clazz, d, null);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
public static final String getNextNumero(Class<? extends SQLElement> clazz, Date d, SQLRowAccessor rowNum) throws IllegalArgumentException {
|
|
|
290 |
if (rowNum == null || rowNum.isUndefined()) {
|
|
|
291 |
rowNum = TABLE_NUM.getRow(2);
|
|
|
292 |
}
|
28 |
ilm |
293 |
|
142 |
ilm |
294 |
if (isAuto(clazz, rowNum)) {
|
|
|
295 |
return getNextForMonth(clazz, d, rowNum);
|
|
|
296 |
} else {
|
|
|
297 |
final String numberLabel = getLabelNumberFor(clazz);
|
149 |
ilm |
298 |
if (rowNum.getObject(numberLabel) == null || rowNum.getString(getLabelFormatFor(clazz)) == null || rowNum.getString(getLabelFormatFor(clazz)).trim().length() == 0) {
|
142 |
ilm |
299 |
return "";
|
|
|
300 |
}
|
|
|
301 |
String format = rowNum.getString(getLabelFormatFor(clazz));
|
|
|
302 |
SQLSelect sel = new SQLSelect();
|
|
|
303 |
sel.addSelect(rowNum.getTable().getField(numberLabel));
|
|
|
304 |
sel.setWhere(rowNum.getTable().getKey(), "=", rowNum.getID());
|
|
|
305 |
final Number l = (Number) rowNum.getTable().getDBSystemRoot().getDataSource().execute(sel.asString(), new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, false));
|
|
|
306 |
int start = l.intValue();
|
|
|
307 |
return getNextNumero(format, start, d);
|
28 |
ilm |
308 |
}
|
18 |
ilm |
309 |
}
|
|
|
310 |
|
21 |
ilm |
311 |
private static final Tuple2<String, String> getPrefixAndSuffix(String format, Date d) {
|
|
|
312 |
|
|
|
313 |
String prefix = "";
|
|
|
314 |
String suffix = "";
|
18 |
ilm |
315 |
int c = format.indexOf('0');
|
|
|
316 |
if (format.trim().length() > 0) {
|
|
|
317 |
if (c >= 0) {
|
21 |
ilm |
318 |
prefix = format.substring(0, c);
|
|
|
319 |
suffix = format.substring(c, format.length());
|
18 |
ilm |
320 |
|
|
|
321 |
try {
|
|
|
322 |
DateFormat dateFormat = new SimpleDateFormat(prefix);
|
21 |
ilm |
323 |
prefix = dateFormat.format(d);
|
18 |
ilm |
324 |
} catch (IllegalArgumentException e) {
|
|
|
325 |
System.err.println("pattern incorrect");
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
} else {
|
|
|
329 |
|
|
|
330 |
try {
|
|
|
331 |
DateFormat dateFormat = new SimpleDateFormat(format);
|
21 |
ilm |
332 |
prefix = dateFormat.format(d);
|
18 |
ilm |
333 |
} catch (IllegalArgumentException e) {
|
|
|
334 |
System.err.println("pattern incorrect");
|
|
|
335 |
}
|
21 |
ilm |
336 |
|
18 |
ilm |
337 |
}
|
|
|
338 |
}
|
21 |
ilm |
339 |
|
|
|
340 |
return Tuple2.create(prefix, suffix);
|
18 |
ilm |
341 |
}
|
|
|
342 |
|
90 |
ilm |
343 |
/*
|
|
|
344 |
* @throws IllegalArgumentException pattern incorrect
|
|
|
345 |
*/
|
|
|
346 |
protected static final String getNextNumero(String format, Integer start, Date d) throws IllegalArgumentException {
|
21 |
ilm |
347 |
if (start != null && start < 0) {
|
|
|
348 |
return null;
|
|
|
349 |
}
|
|
|
350 |
|
28 |
ilm |
351 |
Tuple2<String, String> t = getPrefixAndSuffix(format, d);
|
|
|
352 |
|
21 |
ilm |
353 |
DecimalFormat decimalFormat = new DecimalFormat(t.get1());
|
|
|
354 |
return t.get0() + decimalFormat.format(start);
|
|
|
355 |
}
|
|
|
356 |
|
18 |
ilm |
357 |
public static final String getNextCodeLettrage() {
|
|
|
358 |
SQLRow rowNum = TABLE_NUM.getRow(2);
|
|
|
359 |
final String string = rowNum.getString("CODE_LETTRAGE");
|
|
|
360 |
String s = (string == null) ? "" : string.trim().toUpperCase();
|
|
|
361 |
return getNextCodeLetrrage(s);
|
|
|
362 |
}
|
|
|
363 |
|
180 |
ilm |
364 |
public final String getNextCodeLettragePartiel() {
|
|
|
365 |
SQLRow rowNum = this.getTable().getRow(2);
|
|
|
366 |
final String string = rowNum.getString("CODE_LETTRAGE_PARTIEL");
|
|
|
367 |
String s = (string == null) ? "" : string.trim().toLowerCase();
|
|
|
368 |
return getNextCodeLettragePartiel(s);
|
|
|
369 |
}
|
|
|
370 |
|
18 |
ilm |
371 |
public static final String getNextCodeLetrrage(String code) {
|
|
|
372 |
code = code.trim();
|
|
|
373 |
if (code == null || code.length() == 0) {
|
|
|
374 |
return "AAA";
|
|
|
375 |
} else {
|
|
|
376 |
char[] charArray = code.toCharArray();
|
|
|
377 |
char c = 'A';
|
|
|
378 |
int i = charArray.length - 1;
|
|
|
379 |
while (i >= 0 && (c = charArray[i]) == 'Z') {
|
|
|
380 |
i--;
|
|
|
381 |
}
|
|
|
382 |
if (i >= 0) {
|
|
|
383 |
c++;
|
|
|
384 |
charArray[i] = c;
|
|
|
385 |
for (int j = i + 1; j < charArray.length; j++) {
|
|
|
386 |
charArray[j] = 'A';
|
|
|
387 |
}
|
|
|
388 |
code = String.valueOf(charArray);
|
|
|
389 |
} else {
|
|
|
390 |
// On ajoute une lettre
|
|
|
391 |
final StringBuffer buf = new StringBuffer(code.length() + 1);
|
|
|
392 |
final int nb = code.length() + 1;
|
|
|
393 |
for (int j = 0; j < nb; j++) {
|
|
|
394 |
buf.append('A');
|
|
|
395 |
}
|
|
|
396 |
code = buf.toString();
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
return code;
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
}
|
|
|
403 |
|
180 |
ilm |
404 |
public static final String getNextCodeLettragePartiel(String code) {
|
|
|
405 |
code = code.trim();
|
|
|
406 |
if (code == null || code.length() == 0) {
|
|
|
407 |
return "aaa";
|
|
|
408 |
} else {
|
|
|
409 |
char[] charArray = code.toCharArray();
|
|
|
410 |
char c = 'a';
|
|
|
411 |
int i = charArray.length - 1;
|
|
|
412 |
while (i >= 0 && (c = charArray[i]) == 'z') {
|
|
|
413 |
i--;
|
|
|
414 |
}
|
|
|
415 |
if (i >= 0) {
|
|
|
416 |
c++;
|
|
|
417 |
charArray[i] = c;
|
|
|
418 |
for (int j = i + 1; j < charArray.length; j++) {
|
|
|
419 |
charArray[j] = 'a';
|
|
|
420 |
}
|
|
|
421 |
code = String.valueOf(charArray);
|
|
|
422 |
} else {
|
|
|
423 |
// On ajoute une lettre
|
|
|
424 |
final StringBuffer buf = new StringBuffer(code.length() + 1);
|
|
|
425 |
final int nb = code.length() + 1;
|
|
|
426 |
for (int j = 0; j < nb; j++) {
|
|
|
427 |
buf.append('a');
|
|
|
428 |
}
|
|
|
429 |
code = buf.toString();
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
return code;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
}
|
|
|
436 |
|
73 |
ilm |
437 |
private static boolean isNumeroExist(SQLElement element, int num) {
|
180 |
ilm |
438 |
|
18 |
ilm |
439 |
if (num < 0) {
|
|
|
440 |
return true;
|
|
|
441 |
}
|
|
|
442 |
|
73 |
ilm |
443 |
String s = map.get(element.getClass());
|
|
|
444 |
|
|
|
445 |
for (Class<? extends SQLElement> e : map.keySet()) {
|
|
|
446 |
String prefix = map.get(e);
|
|
|
447 |
if (prefix.equals(s)) {
|
|
|
448 |
SQLSelect sel = new SQLSelect();
|
|
|
449 |
|
|
|
450 |
SQLElement elt = Configuration.getInstance().getDirectory().getElement(e);
|
|
|
451 |
|
94 |
ilm |
452 |
if (elt != null && elt.getTable().getFieldsName().contains("NUMERO")) {
|
93 |
ilm |
453 |
sel.addSelect(elt.getTable().getKey());
|
|
|
454 |
sel.setWhere(new Where(elt.getTable().getField("NUMERO"), "LIKE", getPattern(elt, num)));
|
|
|
455 |
System.err.println("NumerotationAutoSQLElement.isNumeroExist() " + sel.asString());
|
|
|
456 |
List<SQLRow> liste = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new SQLRowListRSH(elt.getTable(), true));
|
|
|
457 |
if (liste.size() > 0) {
|
|
|
458 |
return true;
|
|
|
459 |
}
|
73 |
ilm |
460 |
}
|
|
|
461 |
}
|
|
|
462 |
}
|
|
|
463 |
return false;
|
18 |
ilm |
464 |
}
|
|
|
465 |
|
21 |
ilm |
466 |
protected static final SQLTable TABLE_NUM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("NUMEROTATION_AUTO");
|
|
|
467 |
|
|
|
468 |
public static void main(String[] args) {
|
|
|
469 |
|
|
|
470 |
List<String> l = Arrays.asList("2011/05/001", "2011/05/002", "2011/05/003", "2011/05/004");
|
|
|
471 |
DecimalFormat format = new DecimalFormat("'2011/05/'000");
|
|
|
472 |
for (String string : l) {
|
|
|
473 |
Number n;
|
|
|
474 |
try {
|
|
|
475 |
n = format.parse(string);
|
|
|
476 |
System.err.println(n);
|
|
|
477 |
} catch (ParseException exn) {
|
|
|
478 |
// TODO Bloc catch auto-généré
|
|
|
479 |
exn.printStackTrace();
|
|
|
480 |
}
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
}
|
|
|
484 |
|
142 |
ilm |
485 |
private static String getNextForMonth(Class<? extends SQLElement> clazz, Date d, SQLRowAccessor rowNum) {
|
21 |
ilm |
486 |
|
156 |
ilm |
487 |
List<SQLTable> tables = new ArrayList<>();
|
|
|
488 |
SQLTable table = Configuration.getInstance().getDirectory().getElement(clazz).getTable();
|
|
|
489 |
tables.add(table);
|
67 |
ilm |
490 |
final String s = map.get(clazz);
|
156 |
ilm |
491 |
for (Entry<Class<? extends SQLElement>, String> entry : map.entrySet()) {
|
|
|
492 |
if (!entry.getKey().equals(clazz) && entry.getValue().equals(s)) {
|
|
|
493 |
tables.add(Configuration.getInstance().getDirectory().getElement(entry.getKey()).getTable());
|
|
|
494 |
}
|
|
|
495 |
}
|
|
|
496 |
|
67 |
ilm |
497 |
final String pattern = rowNum.getString(s + FORMAT);
|
|
|
498 |
final Tuple2<String, String> prefixSuffix = getPrefixAndSuffix(pattern, d);
|
|
|
499 |
final String prefix = prefixSuffix.get0();
|
|
|
500 |
final String suffix = prefixSuffix.get1();
|
156 |
ilm |
501 |
final String decimalPattern = "'" + prefix + "'" + suffix;
|
|
|
502 |
final DecimalFormat format = new DecimalFormat(decimalPattern);
|
|
|
503 |
int value = 0;
|
67 |
ilm |
504 |
|
156 |
ilm |
505 |
for (SQLTable sqlTable : tables) {
|
21 |
ilm |
506 |
|
156 |
ilm |
507 |
final SQLSelect sel = new SQLSelect();
|
|
|
508 |
sel.addSelect(sqlTable.getField("NUMERO"));
|
|
|
509 |
sel.addSelect(sqlTable.getKey());
|
|
|
510 |
Where w = new Where(sqlTable.getField("NUMERO"), "LIKE", "%" + prefix + "%");
|
67 |
ilm |
511 |
|
156 |
ilm |
512 |
sel.setWhere(w);
|
|
|
513 |
SQLDataSource source = Configuration.getInstance().getBase().getDataSource();
|
67 |
ilm |
514 |
|
156 |
ilm |
515 |
final ResultSetHandler createFromSelect = SQLRowListRSH.createFromSelect(sel);
|
67 |
ilm |
516 |
|
156 |
ilm |
517 |
final List<SQLRow> l = (List<SQLRow>) source.execute(sel.asString(), new IResultSetHandler(createFromSelect, false));
|
90 |
ilm |
518 |
|
156 |
ilm |
519 |
for (SQLRow sqlRow : l) {
|
90 |
ilm |
520 |
|
156 |
ilm |
521 |
final String numero = sqlRow.getString("NUMERO");
|
|
|
522 |
try {
|
|
|
523 |
|
|
|
524 |
final Number n = format.parse(numero);
|
|
|
525 |
value = Math.max(value, n.intValue());
|
|
|
526 |
} catch (ParseException exn) {
|
|
|
527 |
System.err.println("NumerotationAutoSQLElement.getNextForMonth(): warning: unable to parse " + numero + " with pattern " + decimalPattern + " row:" + sqlRow);
|
|
|
528 |
exn.printStackTrace();
|
|
|
529 |
}
|
90 |
ilm |
530 |
}
|
21 |
ilm |
531 |
}
|
90 |
ilm |
532 |
final String result = format.format(value + 1);
|
|
|
533 |
System.err.println("NumerotationAutoSQLElement.getNextForMonth(): " + result);
|
|
|
534 |
return result;
|
21 |
ilm |
535 |
}
|
|
|
536 |
|
18 |
ilm |
537 |
private static String getPattern(SQLElement elt, int num) {
|
|
|
538 |
SQLRow rowNum = TABLE_NUM.getRow(2);
|
|
|
539 |
String s = map.get(elt.getClass());
|
|
|
540 |
String format = rowNum.getString(s + FORMAT);
|
|
|
541 |
|
|
|
542 |
format = format.replaceAll("y|d|M", "_");
|
|
|
543 |
format = format.replaceAll("'", "");
|
|
|
544 |
int c = format.indexOf('0');
|
|
|
545 |
|
|
|
546 |
String numero = "";
|
|
|
547 |
if (format.trim().length() > 0) {
|
|
|
548 |
if (c >= 0) {
|
|
|
549 |
String prefix = format.substring(0, c);
|
|
|
550 |
String suffix = format.substring(c, format.length());
|
|
|
551 |
String d = prefix;
|
|
|
552 |
|
|
|
553 |
DecimalFormat numberFormat = new DecimalFormat(suffix);
|
|
|
554 |
String n = numberFormat.format(num);
|
|
|
555 |
|
|
|
556 |
numero = d + n;
|
|
|
557 |
} else {
|
|
|
558 |
|
|
|
559 |
String d = format;
|
|
|
560 |
|
|
|
561 |
numero = d + String.valueOf(num);
|
|
|
562 |
}
|
|
|
563 |
} else {
|
|
|
564 |
numero = String.valueOf(num);
|
|
|
565 |
}
|
|
|
566 |
return numero;
|
|
|
567 |
}
|
|
|
568 |
|
|
|
569 |
/**
|
|
|
570 |
* Vérifie et corrige la numérotation
|
|
|
571 |
*
|
|
|
572 |
* @param elt
|
|
|
573 |
*/
|
|
|
574 |
public static void fixNumerotation(SQLElement elt) {
|
|
|
575 |
|
|
|
576 |
SQLRow rowNum = TABLE_NUM.getRow(2);
|
|
|
577 |
String s = map.get(elt.getClass());
|
73 |
ilm |
578 |
if (!rowNum.getTable().contains(s + AUTO_MONTH) || !rowNum.getBoolean(s + AUTO_MONTH)) {
|
18 |
ilm |
579 |
|
93 |
ilm |
580 |
if (rowNum.getObject(s + START) != null) {
|
18 |
ilm |
581 |
|
93 |
ilm |
582 |
int start = rowNum.getInt(s + START);
|
18 |
ilm |
583 |
|
93 |
ilm |
584 |
// si le numero precedent n'existe pas
|
|
|
585 |
if (!isNumeroExist(elt, start - 1)) {
|
18 |
ilm |
586 |
|
93 |
ilm |
587 |
int i = 2;
|
|
|
588 |
|
|
|
589 |
while (!isNumeroExist(elt, start - i)) {
|
|
|
590 |
i++;
|
73 |
ilm |
591 |
}
|
93 |
ilm |
592 |
|
|
|
593 |
if (start - i >= 0) {
|
|
|
594 |
SQLRowValues rowVals = rowNum.createEmptyUpdateRow();
|
|
|
595 |
rowVals.put(s + START, start - i + 1);
|
|
|
596 |
try {
|
|
|
597 |
rowVals.update();
|
|
|
598 |
} catch (SQLException e) {
|
|
|
599 |
e.printStackTrace();
|
|
|
600 |
}
|
|
|
601 |
}
|
18 |
ilm |
602 |
}
|
|
|
603 |
}
|
|
|
604 |
}
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
private static Map<Class<? extends SQLElement>, String> map = new HashMap<Class<? extends SQLElement>, String>();
|
|
|
608 |
|
|
|
609 |
static {
|
|
|
610 |
map.put(AvoirClientSQLElement.class, "AVOIR");
|
|
|
611 |
map.put(SaisieVenteFactureSQLElement.class, "FACT");
|
|
|
612 |
map.put(AvoirClientSQLElement.class, "AVOIR");
|
61 |
ilm |
613 |
map.put(AvoirFournisseurSQLElement.class, "AVOIR_F");
|
18 |
ilm |
614 |
map.put(DevisSQLElement.class, "DEVIS");
|
144 |
ilm |
615 |
map.put(DemandePrixSQLElement.class, "DMD_PRIX");
|
80 |
ilm |
616 |
|
93 |
ilm |
617 |
map.put(CustomerSQLElement.class, "CLIENT");
|
80 |
ilm |
618 |
|
18 |
ilm |
619 |
map.put(BonDeLivraisonSQLElement.class, "BON_L");
|
|
|
620 |
map.put(BonReceptionSQLElement.class, "BON_R");
|
|
|
621 |
map.put(CommandeClientSQLElement.class, "COMMANDE_CLIENT");
|
|
|
622 |
map.put(CommandeSQLElement.class, "COMMANDE");
|
|
|
623 |
map.put(CourrierClientSQLElement.class, "COURRIER");
|
|
|
624 |
map.put(RelanceSQLElement.class, "RELANCE");
|
|
|
625 |
map.put(SalarieSQLElement.class, "SALARIE");
|
|
|
626 |
|
|
|
627 |
}
|
|
|
628 |
|
25 |
ilm |
629 |
/**
|
|
|
630 |
* Permet d'ajouter la gestion de la numérotation automatique. Attention à bien créer les champs
|
|
|
631 |
* name_FORMAT et name_START dans la table NUMEROTATION_AUTO
|
|
|
632 |
*
|
|
|
633 |
* @param elt
|
|
|
634 |
* @param name
|
|
|
635 |
*/
|
|
|
636 |
public static void addClass(Class<? extends SQLElement> elt, String name) {
|
|
|
637 |
map.put(elt, name);
|
|
|
638 |
}
|
|
|
639 |
|
28 |
ilm |
640 |
public static void removeClass(Class<? extends SQLElement> elt) {
|
|
|
641 |
map.remove(elt);
|
|
|
642 |
}
|
|
|
643 |
|
142 |
ilm |
644 |
private static String getName(Class<? extends SQLElement> elt) {
|
|
|
645 |
return map.get(elt);
|
|
|
646 |
}
|
|
|
647 |
|
18 |
ilm |
648 |
public static void addListeners() {
|
|
|
649 |
for (Class<? extends SQLElement> clazz : map.keySet()) {
|
|
|
650 |
final SQLElement elt = Configuration.getInstance().getDirectory().getElement(clazz);
|
|
|
651 |
if (elt != null) {
|
|
|
652 |
elt.getTable().addTableModifiedListener(new SQLTableModifiedListener() {
|
|
|
653 |
@Override
|
|
|
654 |
public void tableModified(SQLTableEvent evt) {
|
|
|
655 |
if (evt.getMode() == Mode.ROW_UPDATED) {
|
|
|
656 |
SQLRow row = evt.getRow();
|
93 |
ilm |
657 |
if (row != null && row.isArchived()) {
|
18 |
ilm |
658 |
fixNumerotation(elt);
|
|
|
659 |
}
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
}
|
|
|
663 |
});
|
|
|
664 |
} else {
|
|
|
665 |
System.err.println(clazz);
|
|
|
666 |
Thread.dumpStack();
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
}
|
|
|
672 |
|
57 |
ilm |
673 |
@Override
|
|
|
674 |
protected String createCode() {
|
|
|
675 |
return "autonumbering";
|
|
|
676 |
}
|
18 |
ilm |
677 |
}
|