OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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.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;
19
import org.openconcerto.erp.core.customerrelationship.customer.element.RelanceSQLElement;
20
import org.openconcerto.erp.core.humanresources.payroll.element.SalarieSQLElement;
21
import org.openconcerto.erp.core.sales.credit.element.AvoirClientSQLElement;
22
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
23
import org.openconcerto.erp.core.sales.order.element.CommandeClientSQLElement;
24
import org.openconcerto.erp.core.sales.quote.element.DevisSQLElement;
25
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonSQLElement;
26
import org.openconcerto.erp.core.supplychain.order.element.CommandeSQLElement;
27
import org.openconcerto.erp.core.supplychain.receipt.element.BonReceptionSQLElement;
28
import org.openconcerto.sql.Configuration;
29
import org.openconcerto.sql.element.BaseSQLComponent;
30
import org.openconcerto.sql.element.SQLComponent;
31
import org.openconcerto.sql.element.SQLElement;
32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowListRSH;
34
import org.openconcerto.sql.model.SQLRowValues;
35
import org.openconcerto.sql.model.SQLSelect;
36
import org.openconcerto.sql.model.SQLTable;
37
import org.openconcerto.sql.model.SQLTableEvent;
38
import org.openconcerto.sql.model.SQLTableModifiedListener;
39
import org.openconcerto.sql.model.Where;
40
import org.openconcerto.sql.model.SQLTableEvent.Mode;
41
import org.openconcerto.ui.DefaultGridBagConstraints;
42
 
43
import java.awt.GridBagConstraints;
44
import java.awt.GridBagLayout;
45
import java.sql.SQLException;
46
import java.text.DateFormat;
47
import java.text.DecimalFormat;
48
import java.text.SimpleDateFormat;
49
import java.util.ArrayList;
50
import java.util.Date;
51
import java.util.HashMap;
52
import java.util.List;
53
import java.util.Map;
54
 
55
import javax.swing.Icon;
56
import javax.swing.JLabel;
57
import javax.swing.JTextField;
58
import javax.swing.event.DocumentEvent;
59
import javax.swing.event.DocumentListener;
60
 
61
// FIXME bug JTextField for input bigInt
62
 
63
public class NumerotationAutoSQLElement extends ComptaSQLConfElement {
64
 
65
    private static final String FORMAT = "_FORMAT";
66
    private static final String START = "_START";
67
 
68
    public NumerotationAutoSQLElement() {
69
        super("NUMEROTATION_AUTO", "une numérotation automatique", "numérotations automatiques");
70
    }
71
 
72
    protected List<String> getListFields() {
73
        final List<String> list = new ArrayList<String>(2);
74
        list.add("DEVIS_FORMAT");
75
        list.add("DEVIS_START");
76
        return list;
77
    }
78
 
79
    protected List<String> getComboFields() {
80
        final List<String> list = new ArrayList<String>(2);
81
        list.add("DEVIS_FORMAT");
82
        list.add("DEVIS_START");
83
        return list;
84
    }
85
 
86
    /*
87
     * (non-Javadoc)
88
     *
89
     * @see org.openconcerto.devis.SQLElement#getComponent()
90
     */
91
    public SQLComponent createComponent() {
92
        return new BaseSQLComponent(this) {
93
 
94
            private JTextField textDevisFormat = new JTextField(16);
95
            private JTextField textDevisStart = new JTextField(6);
96
            private JTextField textFactStart = new JTextField(6);
97
            private JTextField textFactFormat = new JTextField(16);
98
            private JTextField textBonFormat = new JTextField(16);
99
            private JTextField textBonStart = new JTextField(6);
100
            private JTextField textBonRFormat = new JTextField(16);
101
            private JTextField textBonRStart = new JTextField(6);
102
            private JTextField textSalarieFormat = new JTextField(16);
103
            private JTextField textSalarieStart = new JTextField(6);
104
            private JTextField textPropositionFormat = new JTextField(16);
105
            private JTextField textPropositionStart = new JTextField(6);
106
            private JTextField textRelanceFormat = new JTextField(16);
107
            private JTextField textRelanceStart = new JTextField(6);
108
            private JTextField textCmdCliFormat = new JTextField(16);
109
            private JTextField textCmdCliStart = new JTextField(6);
110
            private JTextField textCmdFormat = new JTextField(16);
111
            private JTextField textCmdStart = new JTextField(6);
112
            private JTextField textAffaireFormat = new JTextField(16);
113
            private JTextField textAffaireStart = new JTextField(6);
114
            private JTextField textAvoirFormat = new JTextField(16);
115
            private JTextField textAvoirStart = new JTextField(6);
116
            private JTextField textCourrierFormat = new JTextField(16);
117
            private JTextField textCourrierStart = new JTextField(6);
118
 
119
            private Icon iconWarning = ImageIconWarning.getInstance();
120
 
121
            private JLabel labelNumDevis, labelNumFact, labelNumBon, labelNumSalarie;
122
            private JLabel labelNumRelance, labelNumProposition, labelNumCmdCli;
123
            private JLabel labelNumCmd, labelNumBonR, labelNumAffaire, labelNumAvoir, labelNumCourrier;
124
            // private JLabel labelNextCodeLettrage;
125
            private DocumentListener listenText = new DocumentListener() {
126
 
127
                public void insertUpdate(DocumentEvent e) {
128
                    updateLabels();
129
                }
130
 
131
                public void removeUpdate(DocumentEvent e) {
132
                    updateLabels();
133
                }
134
 
135
                public void changedUpdate(DocumentEvent e) {
136
                    updateLabels();
137
                }
138
            };
139
 
140
            public void addViews() {
141
                this.setLayout(new GridBagLayout());
142
                final GridBagConstraints c = new DefaultGridBagConstraints();
143
 
144
                // Avoir
145
                JLabel labelAvoirFormat = new JLabel("Avoir " + getLabelFor("AVOIR_FORMAT"));
146
                this.add(labelAvoirFormat, c);
147
                c.gridx++;
148
                c.weightx = 1;
149
                this.add(this.textAvoirFormat, c);
150
 
151
                JLabel labelAvoirStart = new JLabel(getLabelFor("AVOIR_START"));
152
                c.gridx++;
153
                c.weightx = 0;
154
                this.add(labelAvoirStart, c);
155
                c.gridx++;
156
                c.weightx = 1;
157
                this.add(this.textAvoirStart, c);
158
 
159
                this.labelNumAvoir = new JLabel();
160
                c.gridx++;
161
                c.weightx = 0;
162
                this.add(this.labelNumAvoir, c);
163
 
164
                // Devis
165
                JLabel labelDevisFormat = new JLabel("Devis " + getLabelFor("DEVIS_FORMAT"));
166
                c.gridy++;
167
                c.gridx = 0;
168
                this.add(labelDevisFormat, c);
169
                c.gridx++;
170
                c.weightx = 1;
171
                this.add(this.textDevisFormat, c);
172
 
173
                JLabel labelDevisStart = new JLabel(getLabelFor("DEVIS_START"));
174
                c.gridx++;
175
                c.weightx = 0;
176
                this.add(labelDevisStart, c);
177
                c.gridx++;
178
                c.weightx = 1;
179
                this.add(this.textDevisStart, c);
180
 
181
                this.labelNumDevis = new JLabel();
182
                c.gridx++;
183
                c.weightx = 0;
184
                this.add(this.labelNumDevis, c);
185
 
186
                // Commande Client
187
                JLabel labelCmdCliFormat = new JLabel("Commande client " + getLabelFor("COMMANDE_CLIENT_FORMAT"));
188
                c.gridy++;
189
                c.gridx = 0;
190
                this.add(labelCmdCliFormat, c);
191
                c.gridx++;
192
                c.weightx = 1;
193
                this.add(this.textCmdCliFormat, c);
194
 
195
                JLabel labelCmdCliStart = new JLabel(getLabelFor("COMMANDE_CLIENT_START"));
196
                c.gridx++;
197
                c.weightx = 0;
198
                this.add(labelCmdCliStart, c);
199
                c.gridx++;
200
                c.weightx = 1;
201
                this.add(this.textCmdCliStart, c);
202
 
203
                this.labelNumCmdCli = new JLabel();
204
                c.gridx++;
205
                c.weightx = 0;
206
                this.add(this.labelNumCmdCli, c);
207
 
208
                // Commande
209
                JLabel labelCmdFormat = new JLabel("Commande " + getLabelFor("COMMANDE_FORMAT"));
210
                c.gridy++;
211
                c.gridx = 0;
212
                this.add(labelCmdFormat, c);
213
                c.gridx++;
214
                c.weightx = 1;
215
                this.add(this.textCmdFormat, c);
216
 
217
                JLabel labelCmdStart = new JLabel(getLabelFor("COMMANDE_START"));
218
                c.gridx++;
219
                c.weightx = 0;
220
                this.add(labelCmdStart, c);
221
                c.gridx++;
222
                c.weightx = 1;
223
                this.add(this.textCmdStart, c);
224
 
225
                this.labelNumCmd = new JLabel();
226
                c.gridx++;
227
                c.weightx = 0;
228
                this.add(this.labelNumCmd, c);
229
 
230
                // Bon
231
                JLabel labelBonFormat = new JLabel("Bon de livraison" + getLabelFor("BON_L_FORMAT"));
232
                c.gridy++;
233
                c.gridx = 0;
234
                this.add(labelBonFormat, c);
235
                c.gridx++;
236
                c.weightx = 1;
237
                this.add(this.textBonFormat, c);
238
 
239
                JLabel labelBonStart = new JLabel(getLabelFor("BON_L_START"));
240
                c.gridx++;
241
                c.weightx = 0;
242
                this.add(labelBonStart, c);
243
                c.gridx++;
244
                c.weightx = 1;
245
                this.add(this.textBonStart, c);
246
 
247
                this.labelNumBon = new JLabel();
248
                c.gridx++;
249
                c.weightx = 0;
250
                this.add(this.labelNumBon, c);
251
 
252
                // Bon Reception
253
                JLabel labelBonRFormat = new JLabel("Bon de réception" + getLabelFor("BON_R_FORMAT"));
254
                c.gridy++;
255
                c.gridx = 0;
256
                this.add(labelBonRFormat, c);
257
                c.gridx++;
258
                c.weightx = 1;
259
                this.add(this.textBonRFormat, c);
260
 
261
                JLabel labelBonRStart = new JLabel(getLabelFor("BON_R_START"));
262
                c.gridx++;
263
                c.weightx = 0;
264
                this.add(labelBonRStart, c);
265
                c.gridx++;
266
                c.weightx = 1;
267
                this.add(this.textBonRStart, c);
268
 
269
                this.labelNumBonR = new JLabel();
270
                c.gridx++;
271
                c.weightx = 0;
272
                this.add(this.labelNumBonR, c);
273
 
274
                // Facture
275
                JLabel labelFactFormat = new JLabel("Facture " + getLabelFor("FACT_FORMAT"));
276
                c.gridy++;
277
                c.gridx = 0;
278
                this.add(labelFactFormat, c);
279
                c.gridx++;
280
                c.weightx = 1;
281
                this.add(this.textFactFormat, c);
282
 
283
                JLabel labelFactStart = new JLabel(getLabelFor("FACT_START"));
284
                c.gridx++;
285
                c.weightx = 0;
286
                this.add(labelFactStart, c);
287
                c.gridx++;
288
                c.weightx = 1;
289
                this.add(this.textFactStart, c);
290
 
291
                this.labelNumFact = new JLabel();
292
                c.gridx++;
293
                c.weightx = 0;
294
                this.add(this.labelNumFact, c);
295
 
296
                // Salarie
297
                JLabel labelSalarieFormat = new JLabel("Salarié " + getLabelFor("SALARIE_FORMAT"));
298
                c.gridy++;
299
                c.gridx = 0;
300
                this.add(labelSalarieFormat, c);
301
                c.gridx++;
302
                c.weightx = 1;
303
                this.add(this.textSalarieFormat, c);
304
 
305
                JLabel labelSalarieStart = new JLabel(getLabelFor("SALARIE_START"));
306
                c.gridx++;
307
                c.weightx = 0;
308
                this.add(labelSalarieStart, c);
309
                c.gridx++;
310
                c.weightx = 1;
311
                this.add(this.textSalarieStart, c);
312
 
313
                this.labelNumSalarie = new JLabel();
314
                c.gridx++;
315
                c.weightx = 0;
316
                this.add(this.labelNumSalarie, c);
317
 
318
                // Affaire
319
                JLabel labelAffaireFormat = new JLabel("Affaire " + getLabelFor("AFFAIRE_FORMAT"));
320
                c.gridy++;
321
                c.gridx = 0;
322
                this.add(labelAffaireFormat, c);
323
                c.gridx++;
324
                c.weightx = 1;
325
                this.add(this.textAffaireFormat, c);
326
 
327
                JLabel labelAffaireStart = new JLabel(getLabelFor("AFFAIRE_START"));
328
                c.gridx++;
329
                c.weightx = 0;
330
                this.add(labelAffaireStart, c);
331
                c.gridx++;
332
                c.weightx = 1;
333
                this.add(this.textAffaireStart, c);
334
 
335
                this.labelNumAffaire = new JLabel();
336
                c.gridx++;
337
                c.weightx = 0;
338
                this.add(this.labelNumAffaire, c);
339
 
340
                // Proposition
341
                JLabel labelPropositionFormat = new JLabel("Proposition " + getLabelFor("PROPOSITION_FORMAT"));
342
                c.gridy++;
343
                c.gridx = 0;
344
                this.add(labelPropositionFormat, c);
345
                c.gridx++;
346
                c.weightx = 1;
347
                this.add(this.textPropositionFormat, c);
348
 
349
                JLabel labelPropositionStart = new JLabel(getLabelFor("PROPOSITION_START"));
350
                c.gridx++;
351
                c.weightx = 0;
352
                this.add(labelPropositionStart, c);
353
                c.gridx++;
354
                c.weightx = 1;
355
                this.add(this.textPropositionStart, c);
356
 
357
                this.labelNumProposition = new JLabel();
358
                c.gridx++;
359
                c.weightx = 0;
360
                this.add(this.labelNumProposition, c);
361
 
362
                // Proposition
363
                JLabel labelCourrierFormat = new JLabel("Courrier " + getLabelFor("COURRIER_FORMAT"));
364
                c.gridy++;
365
                c.gridx = 0;
366
                this.add(labelCourrierFormat, c);
367
                c.gridx++;
368
                c.weightx = 1;
369
                this.add(this.textCourrierFormat, c);
370
 
371
                JLabel labelCourrierStart = new JLabel(getLabelFor("COURRIER_START"));
372
                c.gridx++;
373
                c.weightx = 0;
374
                this.add(labelCourrierStart, c);
375
                c.gridx++;
376
                c.weightx = 1;
377
                this.add(this.textCourrierStart, c);
378
 
379
                this.labelNumCourrier = new JLabel();
380
                c.gridx++;
381
                c.weightx = 0;
382
                this.add(this.labelNumCourrier, c);
383
 
384
                // Relance
385
                JLabel labelRelanceFormat = new JLabel("Relance " + getLabelFor("RELANCE_FORMAT"));
386
                c.gridy++;
387
                c.gridx = 0;
388
                this.add(labelRelanceFormat, c);
389
                c.gridx++;
390
                c.weightx = 1;
391
                this.add(this.textRelanceFormat, c);
392
 
393
                JLabel labelRelanceStart = new JLabel(getLabelFor("RELANCE_START"));
394
                c.gridx++;
395
                c.weightx = 0;
396
                this.add(labelRelanceStart, c);
397
                c.gridx++;
398
                c.weightx = 1;
399
                this.add(this.textRelanceStart, c);
400
 
401
                this.labelNumRelance = new JLabel();
402
                c.gridx++;
403
                c.weightx = 0;
404
                this.add(this.labelNumRelance, c);
405
 
406
                // JLabel labelCodeLettrage = new JLabel(getLabelFor("CODE_LETTRAGE"));
407
                // c.gridy++;
408
                // c.gridx = 0;
409
                // c.weightx = 0;
410
                // this.add(labelCodeLettrage, c);
411
                // c.gridx++;
412
                // c.weightx = 1;
413
                // this.add(this.textCodeLettrage, c);
414
                //
415
                // c.gridx++;
416
                // c.weightx = 0;
417
                // labelNextCodeLettrage = new JLabel();
418
                // this.add(labelNextCodeLettrage, c);
419
 
420
                JLabel labelExemple = new JLabel("Exemple de format : 'Fact'yyyy0000");
421
                c.gridy++;
422
                c.gridx = 0;
423
                c.gridwidth = GridBagConstraints.REMAINDER;
424
                c.weighty = 1;
425
                c.anchor = GridBagConstraints.NORTHWEST;
426
                this.add(labelExemple, c);
427
 
428
                this.textBonFormat.getDocument().addDocumentListener(this.listenText);
429
                this.textBonStart.getDocument().addDocumentListener(this.listenText);
430
                this.textDevisFormat.getDocument().addDocumentListener(this.listenText);
431
                this.textDevisStart.getDocument().addDocumentListener(this.listenText);
432
                this.textFactFormat.getDocument().addDocumentListener(this.listenText);
433
                this.textFactStart.getDocument().addDocumentListener(this.listenText);
434
                this.textSalarieFormat.getDocument().addDocumentListener(this.listenText);
435
                this.textSalarieStart.getDocument().addDocumentListener(this.listenText);
436
                this.textPropositionFormat.getDocument().addDocumentListener(this.listenText);
437
                this.textPropositionStart.getDocument().addDocumentListener(this.listenText);
438
                this.textRelanceFormat.getDocument().addDocumentListener(this.listenText);
439
                this.textRelanceStart.getDocument().addDocumentListener(this.listenText);
440
                this.textCmdCliFormat.getDocument().addDocumentListener(this.listenText);
441
                this.textCmdCliStart.getDocument().addDocumentListener(this.listenText);
442
                this.textCmdFormat.getDocument().addDocumentListener(this.listenText);
443
                this.textCmdStart.getDocument().addDocumentListener(this.listenText);
444
                this.textBonRFormat.getDocument().addDocumentListener(this.listenText);
445
                this.textBonRStart.getDocument().addDocumentListener(this.listenText);
446
                this.textAffaireFormat.getDocument().addDocumentListener(this.listenText);
447
                this.textAffaireStart.getDocument().addDocumentListener(this.listenText);
448
                this.textAvoirFormat.getDocument().addDocumentListener(this.listenText);
449
                this.textAvoirStart.getDocument().addDocumentListener(this.listenText);
450
                this.textCourrierFormat.getDocument().addDocumentListener(this.listenText);
451
                this.textCourrierStart.getDocument().addDocumentListener(this.listenText);
452
                // this.textCodeLettrage.getDocument().addDocumentListener(this.listenText);
453
 
454
                this.addRequiredSQLObject(this.textBonFormat, "BON_L_FORMAT");
455
                this.addRequiredSQLObject(this.textBonStart, "BON_L_START");
456
                this.addRequiredSQLObject(this.textDevisFormat, "DEVIS_FORMAT");
457
                this.addRequiredSQLObject(this.textDevisStart, "DEVIS_START");
458
                this.addRequiredSQLObject(this.textFactFormat, "FACT_FORMAT");
459
                this.addRequiredSQLObject(this.textFactStart, "FACT_START");
460
                this.addRequiredSQLObject(this.textSalarieFormat, "SALARIE_FORMAT");
461
                this.addRequiredSQLObject(this.textSalarieStart, "SALARIE_START");
462
 
463
                this.addRequiredSQLObject(this.textPropositionFormat, "PROPOSITION_FORMAT");
464
                this.addRequiredSQLObject(this.textPropositionStart, "PROPOSITION_START");
465
                this.addRequiredSQLObject(this.textRelanceFormat, "RELANCE_FORMAT");
466
                this.addRequiredSQLObject(this.textRelanceStart, "RELANCE_START");
467
 
468
                this.addRequiredSQLObject(this.textCmdCliFormat, "COMMANDE_CLIENT_FORMAT");
469
                this.addRequiredSQLObject(this.textCmdCliStart, "COMMANDE_CLIENT_START");
470
 
471
                this.addRequiredSQLObject(this.textCmdFormat, "COMMANDE_FORMAT");
472
                this.addRequiredSQLObject(this.textCmdStart, "COMMANDE_START");
473
 
474
                this.addRequiredSQLObject(this.textBonRFormat, "BON_R_FORMAT");
475
                this.addRequiredSQLObject(this.textBonRStart, "BON_R_START");
476
 
477
                this.addRequiredSQLObject(this.textAffaireFormat, "AFFAIRE_FORMAT");
478
                this.addRequiredSQLObject(this.textAffaireStart, "AFFAIRE_START");
479
 
480
                this.addRequiredSQLObject(this.textAvoirFormat, "AVOIR_FORMAT");
481
                this.addRequiredSQLObject(this.textAvoirStart, "AVOIR_START");
482
 
483
                this.addRequiredSQLObject(this.textCourrierFormat, "COURRIER_FORMAT");
484
                this.addRequiredSQLObject(this.textCourrierStart, "COURRIER_START");
485
 
486
                updateLabels();
487
            }
488
 
489
            // private void updateLabelNextCode() {
490
            // String s = getNextCodeLetrrage(this.textCodeLettrage.getText());
491
            // this.labelNextCodeLettrage.setText(donne + " " + s);
492
            // }
493
 
494
            private void updateLabels() {
495
                updateLabel(this.textDevisStart, this.textDevisFormat, this.labelNumDevis);
496
                updateLabel(this.textBonStart, this.textBonFormat, this.labelNumBon);
497
                updateLabel(this.textBonRStart, this.textBonRFormat, this.labelNumBonR);
498
                updateLabel(this.textFactStart, this.textFactFormat, this.labelNumFact);
499
                updateLabel(this.textSalarieStart, this.textSalarieFormat, this.labelNumSalarie);
500
                updateLabel(this.textPropositionStart, this.textPropositionFormat, this.labelNumProposition);
501
                updateLabel(this.textRelanceStart, this.textRelanceFormat, this.labelNumRelance);
502
                updateLabel(this.textCmdCliStart, this.textCmdCliFormat, this.labelNumCmdCli);
503
                updateLabel(this.textCmdStart, this.textCmdFormat, this.labelNumCmd);
504
                updateLabel(this.textAffaireStart, this.textAffaireFormat, this.labelNumAffaire);
505
                updateLabel(this.textAvoirStart, this.textAvoirFormat, this.labelNumAvoir);
506
                updateLabel(this.textCourrierStart, this.textCourrierFormat, this.labelNumCourrier);
507
 
508
            }
509
 
510
            private void updateLabel(JTextField textStart, JTextField textFormat, JLabel label) {
511
                if (textStart.getText().trim().length() > 0) {
512
                    String numProposition = getNextNumero(textFormat.getText(), Integer.parseInt(textStart.getText()));
513
 
514
                    if (numProposition != null) {
515
                        label.setText(" --> " + numProposition);
516
                        label.setIcon(null);
517
                    } else {
518
                        label.setIcon(this.iconWarning);
519
                        label.setText("");
520
                    }
521
                } else {
522
                    label.setIcon(this.iconWarning);
523
                    label.setText("");
524
                }
525
            }
526
 
527
        };
528
    }
529
 
530
    protected static final SQLTable TABLE_NUM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("NUMEROTATION_AUTO");
531
 
532
    // Format du type 'Fact'yyyy-MM-dd0000
533
    public static final String getNextNumero(Class<? extends SQLElement> clazz) {
534
        SQLRow rowNum = TABLE_NUM.getRow(2);
535
        String s = map.get(clazz);
536
        String format = rowNum.getString(s + FORMAT);
537
        int start = rowNum.getInt(s + START);
538
        return getNextNumero(format, start);
539
    }
540
 
541
    protected static final String getNextNumero(String format, int start) {
542
        if (start < 0) {
543
            return null;
544
        }
545
        int c = format.indexOf('0');
546
        if (format.trim().length() > 0) {
547
            if (c >= 0) {
548
                String prefix = format.substring(0, c);
549
                String suffix = format.substring(c, format.length());
550
                String d = prefix;
551
 
552
                try {
553
                    DateFormat dateFormat = new SimpleDateFormat(prefix);
554
                    d = dateFormat.format(new Date());
555
                } catch (IllegalArgumentException e) {
556
                    System.err.println("pattern incorrect");
557
                }
558
 
559
                DecimalFormat numberFormat = new DecimalFormat(suffix);
560
                String n = numberFormat.format(start);
561
 
562
                return d + n;
563
            } else {
564
 
565
                String d = format;
566
                try {
567
                    DateFormat dateFormat = new SimpleDateFormat(format);
568
                    d = dateFormat.format(new Date());
569
                } catch (IllegalArgumentException e) {
570
                    System.err.println("pattern incorrect");
571
                }
572
                return d + String.valueOf(start);
573
            }
574
        } else {
575
            return String.valueOf(start);
576
        }
577
    }
578
 
579
    public static final String getNextCodeLettrage() {
580
        SQLRow rowNum = TABLE_NUM.getRow(2);
581
        final String string = rowNum.getString("CODE_LETTRAGE");
582
        String s = (string == null) ? "" : string.trim().toUpperCase();
583
        return getNextCodeLetrrage(s);
584
    }
585
 
586
    public static final String getNextCodeLetrrage(String code) {
587
        code = code.trim();
588
        if (code == null || code.length() == 0) {
589
            return "AAA";
590
        } else {
591
            char[] charArray = code.toCharArray();
592
            char c = 'A';
593
            int i = charArray.length - 1;
594
            while (i >= 0 && (c = charArray[i]) == 'Z') {
595
                i--;
596
            }
597
            if (i >= 0) {
598
                c++;
599
                charArray[i] = c;
600
                for (int j = i + 1; j < charArray.length; j++) {
601
                    charArray[j] = 'A';
602
                }
603
                code = String.valueOf(charArray);
604
            } else {
605
                // On ajoute une lettre
606
                final StringBuffer buf = new StringBuffer(code.length() + 1);
607
                final int nb = code.length() + 1;
608
                for (int j = 0; j < nb; j++) {
609
                    buf.append('A');
610
                }
611
                code = buf.toString();
612
            }
613
 
614
            return code;
615
        }
616
 
617
    }
618
 
619
    private static boolean isNumeroExist(SQLElement elt, int num) {
620
        if (num < 0) {
621
            return true;
622
        }
623
        SQLSelect sel = new SQLSelect(elt.getTable().getBase());
624
        sel.addSelect(elt.getTable().getKey());
625
 
626
        sel.setWhere(new Where(elt.getTable().getField("NUMERO"), "LIKE", getPattern(elt, num)));
627
        System.err.println("NumerotationAutoSQLElement.isNumeroExist() " + sel.asString());
628
        List<SQLRow> liste = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new SQLRowListRSH(elt.getTable(), true));
629
        return liste.size() > 0;
630
    }
631
 
632
    private static String getPattern(SQLElement elt, int num) {
633
        SQLRow rowNum = TABLE_NUM.getRow(2);
634
        String s = map.get(elt.getClass());
635
        String format = rowNum.getString(s + FORMAT);
636
 
637
        format = format.replaceAll("y|d|M", "_");
638
        format = format.replaceAll("'", "");
639
        int c = format.indexOf('0');
640
 
641
        String numero = "";
642
        if (format.trim().length() > 0) {
643
            if (c >= 0) {
644
                String prefix = format.substring(0, c);
645
                String suffix = format.substring(c, format.length());
646
                String d = prefix;
647
 
648
                DecimalFormat numberFormat = new DecimalFormat(suffix);
649
                String n = numberFormat.format(num);
650
 
651
                numero = d + n;
652
            } else {
653
 
654
                String d = format;
655
 
656
                numero = d + String.valueOf(num);
657
            }
658
        } else {
659
            numero = String.valueOf(num);
660
        }
661
        return numero;
662
    }
663
 
664
    /**
665
     * Vérifie et corrige la numérotation
666
     *
667
     * @param elt
668
     */
669
    public static void fixNumerotation(SQLElement elt) {
670
 
671
        SQLRow rowNum = TABLE_NUM.getRow(2);
672
        String s = map.get(elt.getClass());
673
        int start = rowNum.getInt(s + START);
674
 
675
        // si le numero precedent n'existe pas
676
        if (!isNumeroExist(elt, start - 1)) {
677
 
678
            int i = 2;
679
 
680
            while (!isNumeroExist(elt, start - i)) {
681
                i++;
682
            }
683
 
684
            if (start - i >= 0) {
685
                SQLRowValues rowVals = rowNum.createEmptyUpdateRow();
686
                rowVals.put(s + START, start - i + 1);
687
                try {
688
                    rowVals.update();
689
                } catch (SQLException e) {
690
                    e.printStackTrace();
691
                }
692
            }
693
        }
694
    }
695
 
696
    private static Map<Class<? extends SQLElement>, String> map = new HashMap<Class<? extends SQLElement>, String>();
697
 
698
    static {
699
        map.put(AvoirClientSQLElement.class, "AVOIR");
700
        map.put(SaisieVenteFactureSQLElement.class, "FACT");
701
        map.put(AvoirClientSQLElement.class, "AVOIR");
702
        map.put(DevisSQLElement.class, "DEVIS");
703
        map.put(BonDeLivraisonSQLElement.class, "BON_L");
704
        map.put(BonReceptionSQLElement.class, "BON_R");
705
        map.put(CommandeClientSQLElement.class, "COMMANDE_CLIENT");
706
        map.put(CommandeSQLElement.class, "COMMANDE");
707
        map.put(CourrierClientSQLElement.class, "COURRIER");
708
        map.put(RelanceSQLElement.class, "RELANCE");
709
        map.put(SalarieSQLElement.class, "SALARIE");
710
 
711
    }
712
 
713
    public static void addListeners() {
714
        for (Class<? extends SQLElement> clazz : map.keySet()) {
715
            final SQLElement elt = Configuration.getInstance().getDirectory().getElement(clazz);
716
            if (elt != null) {
717
                elt.getTable().addTableModifiedListener(new SQLTableModifiedListener() {
718
                    @Override
719
                    public void tableModified(SQLTableEvent evt) {
720
                        if (evt.getMode() == Mode.ROW_UPDATED) {
721
                            SQLRow row = evt.getRow();
722
                            if (row.isArchived()) {
723
                                fixNumerotation(elt);
724
                            }
725
                        }
726
 
727
                    }
728
                });
729
            } else {
730
                System.err.println(clazz);
731
                Thread.dumpStack();
732
            }
733
 
734
        }
735
 
736
    }
737
 
738
}