OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 28 | Go to most recent revision | Details | Compare with Previous | 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.customerrelationship.customer.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.erp.core.common.ui.DeviseField;
19
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseClientItemTable;
20
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
21
import org.openconcerto.erp.core.finance.payment.component.ModeDeReglementSQLComponent;
22
import org.openconcerto.erp.model.ISQLCompteSelector;
23
import org.openconcerto.erp.preferences.DefaultNXProps;
24
import org.openconcerto.erp.preferences.ModeReglementDefautPrefPanel;
25
import org.openconcerto.sql.Configuration;
26
import org.openconcerto.sql.element.BaseSQLComponent;
27
import org.openconcerto.sql.element.ElementSQLObject;
28
import org.openconcerto.sql.element.SQLComponent;
29
import org.openconcerto.sql.element.SQLElement;
30
import org.openconcerto.sql.model.SQLBackgroundTableCache;
31
import org.openconcerto.sql.model.SQLBase;
32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
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.UndefinedRowValuesCache;
38
import org.openconcerto.sql.model.Where;
39
import org.openconcerto.sql.request.ComboSQLRequest;
40
import org.openconcerto.sql.request.ListSQLRequest;
41
import org.openconcerto.sql.request.SQLRowItemView;
42
import org.openconcerto.sql.sqlobject.ElementComboBox;
43
import org.openconcerto.sql.sqlobject.ITextWithCompletion;
44
import org.openconcerto.sql.sqlobject.JUniqueTextField;
45
import org.openconcerto.sql.sqlobject.SQLTextCombo;
46
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 47
import org.openconcerto.ui.FormLayouter;
18 ilm 48
import org.openconcerto.ui.TitledSeparator;
49
import org.openconcerto.ui.component.ITextArea;
50
 
51
import java.awt.Component;
52
import java.awt.Dimension;
53
import java.awt.FlowLayout;
54
import java.awt.GridBagConstraints;
55
import java.awt.GridBagLayout;
56
import java.awt.Insets;
57
import java.awt.event.ActionEvent;
58
import java.awt.event.ActionListener;
59
import java.beans.PropertyChangeEvent;
60
import java.beans.PropertyChangeListener;
61
import java.sql.SQLException;
62
import java.text.DecimalFormat;
63
import java.util.ArrayList;
64
import java.util.List;
65
 
66
import javax.swing.ButtonGroup;
67
import javax.swing.Icon;
68
import javax.swing.JCheckBox;
69
import javax.swing.JComponent;
70
import javax.swing.JLabel;
71
import javax.swing.JOptionPane;
72
import javax.swing.JPanel;
73
import javax.swing.JTabbedPane;
74
import javax.swing.JTextField;
75
import javax.swing.SwingConstants;
76
import javax.swing.event.DocumentEvent;
77
import javax.swing.event.DocumentListener;
78
 
79
public class ClientNormalSQLElement extends ComptaSQLConfElement {
80
 
81
    public ClientNormalSQLElement() {
82
        super("CLIENT", "un client", "clients");
83
    }
84
 
85
    protected boolean showMdr = true;
86
 
87
    protected List<String> getListFields() {
88
        final List<String> l = new ArrayList<String>();
89
            l.add("CODE");
25 ilm 90
            // l.add("FORME_JURIDIQUE");
18 ilm 91
        l.add("NOM");
25 ilm 92
        if (getTable().getFieldsName().contains("LOCALISATION")) {
93
            l.add("LOCALISATION");
94
        }
18 ilm 95
            l.add("RESPONSABLE");
96
        l.add("ID_ADRESSE");
97
        l.add("TEL");
98
        l.add("FAX");
99
        l.add("MAIL");
100
            l.add("NUMERO_TVA");
101
            l.add("SIRET");
102
            l.add("ID_COMPTE_PCE");
103
            l.add("ID_MODE_REGLEMENT");
104
        l.add("INFOS");
105
        return l;
106
    }
107
 
108
    @Override
19 ilm 109
    public synchronized ListSQLRequest createListRequest() {
18 ilm 110
        return new ListSQLRequest(getTable(), getListFields()) {
111
            @Override
112
            protected void customizeToFetch(SQLRowValues graphToFetch) {
113
                super.customizeToFetch(graphToFetch);
114
                graphToFetch.grow("ID_MODE_REGLEMENT").put("AJOURS", null).put("LENJOUR", null);
115
            }
116
        };
117
 
118
    }
119
 
120
    protected List<String> getComboFields() {
121
        final List<String> l = new ArrayList<String>();
25 ilm 122
        // l.add("FORME_JURIDIQUE");
18 ilm 123
        l.add("NOM");
25 ilm 124
        if (getTable().getFieldsName().contains("LOCALISATION")) {
125
            l.add("LOCALISATION");
126
        } else {
127
            l.add("CODE");
128
        }
18 ilm 129
        return l;
130
    }
131
 
132
    protected List<String> getPrivateFields() {
133
        final List<String> l = new ArrayList<String>();
134
        l.add("ID_ADRESSE");
135
        l.add("ID_ADRESSE_L");
136
        l.add("ID_ADRESSE_F");
137
        l.add("ID_MODE_REGLEMENT");
138
 
139
        return l;
140
    }
141
 
142
    /*
143
     * (non-Javadoc)
144
     *
145
     * @see org.openconcerto.devis.SQLElement#getComponent()
146
     */
147
    public SQLComponent createComponent() {
148
        return new BaseSQLComponent(this) {
149
            int idDefaultCompteClient = 1;
150
            JCheckBox checkAdrLivraison, checkAdrFacturation;
151
 
21 ilm 152
            private ElementComboBox boxPays = null;
153
            final ElementComboBox boxTarif = new ElementComboBox();
154
 
18 ilm 155
            private JTabbedPane tabbedAdresse = new JTabbedPane() {
156
                public void insertTab(String title, Icon icon, Component component, String tip, int index) {
157
                    if (component instanceof JComponent) {
158
                        ((JComponent) component).setOpaque(false);
159
                    }
160
                    super.insertTab(title, icon, component, tip, index);
161
                }
162
 
163
            };
164
            ElementSQLObject componentPrincipale, componentLivraison, componentFacturation;
165
            AdresseClientItemTable adresseTable = new AdresseClientItemTable();
166
            JCheckBox boxGestionAutoCompte;
167
 
168
            private JCheckBox boxAffacturage, boxComptant;
169
            private DeviseField fieldMontantFactMax;
170
            ISQLCompteSelector compteSel;
171
            JComponent textNom;
172
            // ITextWithCompletion textNom;
173
            final ElementComboBox comboPole = new ElementComboBox();
174
            DecimalFormat format = new DecimalFormat("000");
175
 
176
            private SQLTable contactTable = Configuration.getInstance().getDirectory().getElement("CONTACT").getTable();
177
            private ContactItemTable table;
178
            private SQLRowValues defaultContactRowVals = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(this.contactTable));
179
            private SQLRowItemView eltModeRegl;
180
            private JUniqueTextField textCode;
181
            private JLabel labelCpt;
182
 
183
            public void addViews() {
184
                this.setLayout(new GridBagLayout());
185
                final GridBagConstraints c = new DefaultGridBagConstraints();
186
 
187
                this.addView("ID_MODE_REGLEMENT", REQ + ";" + DEC + ";" + SEP);
188
                this.eltModeRegl = this.getView("ID_MODE_REGLEMENT");
189
                final ElementSQLObject comp = (ElementSQLObject) this.eltModeRegl.getComp();
190
                final ModeDeReglementSQLComponent modeReglComp = (ModeDeReglementSQLComponent) comp.getSQLChild();
191
 
192
                // Raison sociale
193
                JLabel labelRS = new JLabel(getLabelFor("FORME_JURIDIQUE"));
194
                labelRS.setHorizontalAlignment(SwingConstants.RIGHT);
195
                SQLTextCombo textType = new SQLTextCombo();
196
 
197
                this.add(labelRS, c);
198
                c.gridx++;
19 ilm 199
                c.weightx = 0.5;
18 ilm 200
                c.fill = GridBagConstraints.BOTH;
19 ilm 201
                DefaultGridBagConstraints.lockMinimumSize(textType);
18 ilm 202
                this.add(textType, c);
203
 
204
                // Code
205
                JLabel labelCode = new JLabel("Code");
206
                labelCode.setHorizontalAlignment(SwingConstants.RIGHT);
207
                this.textCode = new JUniqueTextField();
208
                c.gridx++;
209
                c.weightx = 0;
210
                c.weighty = 0;
211
                c.gridwidth = 1;
212
                c.fill = GridBagConstraints.HORIZONTAL;
213
                this.add(labelCode, c);
214
                c.gridx++;
19 ilm 215
                c.weightx = 0.5;
18 ilm 216
                c.gridwidth = 1;
19 ilm 217
                DefaultGridBagConstraints.lockMinimumSize(textCode);
18 ilm 218
                this.add(this.textCode, c);
219
                // Nom
220
                JLabel labelNom = new JLabel("Nom");
221
                labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
222
                c.gridy++;
223
                c.gridx = 0;
224
                c.weightx = 0;
225
                this.add(labelNom, c);
226
                c.gridx++;
19 ilm 227
                c.gridwidth = 1;
228
                c.weightx = 0.5;
18 ilm 229
 
230
                    this.textNom = new ITextArea();
19 ilm 231
                DefaultGridBagConstraints.lockMinimumSize(textNom);
18 ilm 232
                this.add(this.textNom, c);
233
 
19 ilm 234
                if (getTable().getFieldsName().contains("ID_PAYS")) {
235
                    c.gridx++;
236
                    c.weightx = 0;
237
                    this.add(new JLabel(getLabelFor("ID_PAYS")), c);
238
                    boxPays = new ElementComboBox(true, 25);
239
                    c.gridx++;
240
                    c.weightx = 0.5;
241
                    DefaultGridBagConstraints.lockMinimumSize(boxPays);
242
                    this.add(boxPays, c);
243
                    this.addView(boxPays, "ID_PAYS");
244
                }
25 ilm 245
                if (getTable().getFieldsName().contains("LOCALISATION")) {
246
                    c.gridy++;
247
                    c.gridx = 0;
248
                    c.weightx = 0;
249
                    JLabel comp2 = new JLabel(getLabelFor("LOCALISATION"));
250
                    comp2.setHorizontalAlignment(SwingConstants.RIGHT);
251
                    this.add(comp2, c);
252
                    JTextField loc = new JTextField();
253
                    c.gridx++;
254
                    c.weightx = 1;
255
                    // DefaultGridBagConstraints.lockMinimumSize(boxPays);
256
                    this.add(loc, c);
257
                    this.addView(loc, "LOCALISATION");
258
                }
18 ilm 259
                // Numero intracomm
19 ilm 260
                JLabel labelIntraComm = new JLabel("N° TVA");
18 ilm 261
                labelIntraComm.setHorizontalAlignment(SwingConstants.RIGHT);
262
                c.gridy++;
263
                c.gridx = 0;
264
                c.gridwidth = 1;
265
                c.weightx = 0;
266
                this.add(labelIntraComm, c);
267
 
268
                JTextField textNumIntracomm = new JTextField();
269
                c.gridx++;
19 ilm 270
                c.weightx = 0.5;
271
                DefaultGridBagConstraints.lockMinimumSize(textNumIntracomm);
18 ilm 272
                this.add(textNumIntracomm, c);
273
                JLabel labelSIREN = new JLabel(getLabelFor("SIRET"));
274
                labelSIREN.setHorizontalAlignment(SwingConstants.RIGHT);
275
                c.gridx++;
276
                c.gridwidth = 1;
277
                c.weightx = 0;
278
                this.add(labelSIREN, c);
279
 
280
                JComponent textSiren;
281
                    textSiren = new JTextField();
282
                c.gridx++;
19 ilm 283
                c.weightx = 0.5;
284
                DefaultGridBagConstraints.lockMinimumSize(textSiren);
18 ilm 285
                this.add(textSiren, c);
286
 
287
                // Responsable
288
                final JLabel responsable = new JLabel(this.getLabelFor("RESPONSABLE"));
289
                responsable.setHorizontalAlignment(SwingConstants.RIGHT);
290
                JTextField textResp = new JTextField();
291
                c.gridx = 0;
292
                c.gridy++;
293
                c.weightx = 0;
294
                c.weighty = 0;
295
                c.gridwidth = 1;
296
                this.add(responsable, c);
297
                c.gridx++;
19 ilm 298
                c.weightx = 0.5;
299
                DefaultGridBagConstraints.lockMinimumSize(textResp);
18 ilm 300
                this.add(textResp, c);
301
 
302
                JLabel labelRIB = new JLabel(getLabelFor("RIB"));
303
                labelRIB.setHorizontalAlignment(SwingConstants.RIGHT);
304
                c.gridx++;
305
                c.gridwidth = 1;
306
                c.weightx = 0;
307
                this.add(labelRIB, c);
308
 
309
                JTextField textRib = new JTextField();
310
                c.gridx++;
19 ilm 311
                c.weightx = 0.5;
312
                DefaultGridBagConstraints.lockMinimumSize(textRib);
18 ilm 313
                this.add(textRib, c);
314
 
315
 
316
                // tel
317
                JLabel labelTel = new JLabel("N° de téléphone");
318
                labelTel.setHorizontalAlignment(SwingConstants.RIGHT);
319
                c.gridx = 0;
320
                c.gridy++;
321
                c.weightx = 0;
322
                this.add(labelTel, c);
323
 
324
                final JTextField textTel = new JTextField();
325
                c.gridx++;
19 ilm 326
                c.weightx = 0.5;
327
                DefaultGridBagConstraints.lockMinimumSize(textTel);
18 ilm 328
                this.add(textTel, c);
329
                textTel.getDocument().addDocumentListener(new DocumentListener() {
330
 
331
                    public void changedUpdate(DocumentEvent e) {
332
                        defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
333
                    }
334
 
335
                    public void insertUpdate(DocumentEvent e) {
336
                        defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
337
                    }
338
 
339
                    public void removeUpdate(DocumentEvent e) {
340
                        defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
341
                    }
342
 
343
                });
344
 
345
                // email
346
                JLabel labelMail = new JLabel("E-mail");
347
                labelMail.setHorizontalAlignment(SwingConstants.RIGHT);
348
                c.gridx++;
349
                c.weightx = 0;
350
                this.add(labelMail, c);
351
 
352
                JTextField textMail = new JTextField();
353
                c.gridx++;
19 ilm 354
                c.weightx = 0.5;
355
                DefaultGridBagConstraints.lockMinimumSize(textMail);
18 ilm 356
                this.add(textMail, c);
357
 
358
                // Portable
359
                JLabel labelPortable = new JLabel("N° de portable");
360
                labelPortable.setHorizontalAlignment(SwingConstants.RIGHT);
361
                c.gridx = 0;
362
                c.gridy++;
363
                c.weightx = 0;
364
                this.add(labelPortable, c);
365
 
366
                JTextField textPortable = new JTextField();
367
                c.gridx++;
19 ilm 368
                c.weightx = 0.5;
369
                DefaultGridBagConstraints.lockMinimumSize(textPortable);
18 ilm 370
                this.add(textPortable, c);
371
 
372
                // Fax
373
                JLabel labelFax = new JLabel("N° de fax");
374
                labelFax.setHorizontalAlignment(SwingConstants.RIGHT);
375
                c.gridx++;
376
                c.weightx = 0;
377
                this.add(labelFax, c);
378
 
379
                final JTextField textFax = new JTextField();
380
                c.gridx++;
19 ilm 381
                c.weightx = 0.5;
382
                DefaultGridBagConstraints.lockMinimumSize(textFax);
18 ilm 383
                this.add(textFax, c);
384
 
385
                textFax.getDocument().addDocumentListener(new DocumentListener() {
386
 
387
                    public void changedUpdate(DocumentEvent e) {
388
                        defaultContactRowVals.put("FAX", textFax.getText());
389
                    }
390
 
391
                    public void insertUpdate(DocumentEvent e) {
392
                        defaultContactRowVals.put("FAX", textFax.getText());
393
                    }
394
 
395
                    public void removeUpdate(DocumentEvent e) {
396
                        defaultContactRowVals.put("FAX", textFax.getText());
397
                    }
398
 
399
                });
400
 
401
                // Secteur activité
402
                final boolean customerIsKD;
403
 
25 ilm 404
                // Champ Module
405
                c.gridx = 0;
406
                c.gridy++;
407
                c.gridwidth = GridBagConstraints.REMAINDER;
408
                final JPanel addP = new JPanel();
409
                this.setAdditionalFieldsPanel(new FormLayouter(addP, 1));
410
                this.add(addP, c);
411
 
412
                c.gridy++;
413
                c.gridwidth = 1;
414
 
18 ilm 415
                // Adresse
416
                c.gridx = 0;
417
                c.gridy++;
418
                c.weightx = 1;
419
                c.weighty = 0;
420
                c.gridwidth = GridBagConstraints.REMAINDER;
421
                TitledSeparator sep = new TitledSeparator("Adresse");
422
                this.add(sep, c);
423
 
424
                // Adr principale
425
                this.addView("ID_ADRESSE", REQ + ";" + DEC + ";" + SEP);
426
                this.componentPrincipale = (ElementSQLObject) this.getView("ID_ADRESSE");
427
                this.componentPrincipale.setOpaque(false);
428
                this.tabbedAdresse.add(getLabelFor("ID_ADRESSE"), this.componentPrincipale);
429
 
430
                // Adr facturation
431
                JPanel panelFacturation = new JPanel(new GridBagLayout());
432
                panelFacturation.setOpaque(false);
433
                GridBagConstraints cPanelF = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 1, 2, 1), 0, 0);
434
 
435
                this.addView("ID_ADRESSE_F", DEC + ";" + SEP);
436
                this.componentFacturation = (ElementSQLObject) this.getView("ID_ADRESSE_F");
437
                this.componentFacturation.setOpaque(false);
438
                panelFacturation.add(this.componentFacturation, cPanelF);
439
                this.checkAdrFacturation = new JCheckBox("Adresse de facturation identique à la principale");
440
                this.checkAdrFacturation.setOpaque(false);
441
                cPanelF.gridy++;
442
                panelFacturation.add(this.checkAdrFacturation, cPanelF);
443
                    this.tabbedAdresse.add(getLabelFor("ID_ADRESSE_F"), panelFacturation);
444
                // Adr livraison
445
                JPanel panelLivraison = new JPanel(new GridBagLayout());
446
                panelLivraison.setOpaque(false);
447
                GridBagConstraints cPanelL = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 1, 2, 1), 0, 0);
448
 
449
                this.addView("ID_ADRESSE_L", DEC + ";" + SEP);
450
                this.componentLivraison = (ElementSQLObject) this.getView("ID_ADRESSE_L");
451
                this.componentLivraison.setOpaque(false);
452
                panelLivraison.add(this.componentLivraison, cPanelL);
453
 
454
                this.checkAdrLivraison = new JCheckBox("Adresse de livraison identique à l'adresse principale");
455
                this.checkAdrLivraison.setOpaque(false);
456
                cPanelL.gridy++;
457
                panelLivraison.add(this.checkAdrLivraison, cPanelL);
458
                    this.tabbedAdresse.add(getLabelFor("ID_ADRESSE_L"), panelLivraison);
459
                String labelAdrSuppl = "Adresses supplémentaires";
460
                this.tabbedAdresse.add(labelAdrSuppl, this.adresseTable);
461
 
462
                c.gridx = 0;
463
                c.gridy++;
464
                c.gridwidth = GridBagConstraints.REMAINDER;
465
                this.tabbedAdresse.setOpaque(false);
466
                this.add(this.tabbedAdresse, c);
467
 
468
                c.anchor = GridBagConstraints.WEST;
469
                c.fill = GridBagConstraints.HORIZONTAL;
470
 
471
                // Contact
472
 
473
                TitledSeparator sepContact = new TitledSeparator("Contacts client");
474
                c.weightx = 1;
475
                c.gridwidth = GridBagConstraints.REMAINDER;
476
                c.gridx = 0;
477
                c.gridy++;
478
                this.add(sepContact, c);
479
 
480
                this.table = new ContactItemTable(this.defaultContactRowVals);
481
                this.table.setPreferredSize(new Dimension(this.table.getSize().width, 150));
482
                c.gridx = 0;
483
                c.gridy++;
484
                c.anchor = GridBagConstraints.WEST;
485
                c.fill = GridBagConstraints.BOTH;
486
                c.gridwidth = GridBagConstraints.REMAINDER;
487
                c.weighty = 0.7;
488
                this.add(this.table, c);
489
                c.fill = GridBagConstraints.HORIZONTAL;
490
                c.gridwidth = 1;
491
                c.weighty = 0;
492
 
493
 
494
                // Mode de régelement
495
 
496
                TitledSeparator reglSep = new TitledSeparator(getLabelFor("ID_MODE_REGLEMENT"));
497
                c.gridwidth = GridBagConstraints.REMAINDER;
498
                c.gridy++;
499
                c.gridx = 0;
500
                this.add(reglSep, c);
501
 
502
                c.gridy++;
503
                c.gridx = 0;
504
                this.add(comp, c);
505
 
506
                if (!showMdr) {
507
                    reglSep.setVisible(false);
508
                    comp.setCreated(false);
509
                    comp.setVisible(false);
510
                }
511
 
19 ilm 512
                if (getTable().getFieldsName().contains("ID_TARIF")) {
513
 
514
                    // Tarif
515
                    TitledSeparator tarifSep = new TitledSeparator("Tarif spécial à appliquer");
516
                    c.gridwidth = GridBagConstraints.REMAINDER;
517
                    c.gridy++;
518
                    c.gridx = 0;
519
                    this.add(tarifSep, c);
520
 
521
                    c.gridy++;
522
                    c.gridx = 0;
523
                    c.gridwidth = 1;
524
                    c.weightx = 0;
525
                    this.add(new JLabel(getLabelFor("ID_TARIF")), c);
526
                    c.gridx++;
527
                    c.weightx = 1;
528
                    c.gridwidth = GridBagConstraints.REMAINDER;
529
 
530
                    this.add(boxTarif, c);
531
                    this.addView(boxTarif, "ID_TARIF");
532
                }
533
                if (getTable().getFieldsName().contains("ID_LANGUE")) {
534
                    // Tarif
535
                    TitledSeparator langueSep = new TitledSeparator("Langue à appliquer sur les documents");
536
                    c.gridwidth = GridBagConstraints.REMAINDER;
537
                    c.gridy++;
538
                    c.gridx = 0;
539
                    this.add(langueSep, c);
540
 
541
                    c.gridy++;
542
                    c.gridx = 0;
543
                    c.gridwidth = 1;
544
                    c.weightx = 0;
545
                    this.add(new JLabel(getLabelFor("ID_LANGUE")), c);
546
                    c.gridx++;
547
                    c.weightx = 1;
548
                    c.gridwidth = GridBagConstraints.REMAINDER;
549
                    final ElementComboBox boxLangue = new ElementComboBox();
550
                    this.add(boxLangue, c);
551
                    this.addView(boxLangue, "ID_LANGUE");
552
 
553
                    boxPays.addValueListener(new PropertyChangeListener() {
554
 
555
                        @Override
556
                        public void propertyChange(PropertyChangeEvent evt) {
557
                            SQLRow row = boxPays.getSelectedRow();
558
                            if (row != null) {
559
                                boxTarif.setValue(row.getInt("ID_TARIF"));
560
                                boxLangue.setValue(row.getInt("ID_LANGUE"));
561
                            }
562
                        }
563
                    });
564
                }
18 ilm 565
                // Add on
566
                final JPanel addOnPanel = getAddOnPanel(this);
567
                if (addOnPanel != null) {
568
                    c.gridy++;
569
                    this.add(addOnPanel, c);
570
                }
571
                // Compte associé
572
                this.compteSel = new ISQLCompteSelector(true);
573
                this.boxGestionAutoCompte = new JCheckBox("Gestion Automatique des comptes");
574
                TitledSeparator sepCompte = new TitledSeparator("Compte associé");
575
                this.labelCpt = new JLabel(getLabelFor("ID_COMPTE_PCE"));
576
 
577
                if (!Boolean.valueOf(DefaultNXProps.getInstance().getProperty("HideCompteClient"))) {
578
 
579
                    c.gridx = 0;
580
                    c.gridy++;
581
                    c.weightx = 1;
582
                    c.weighty = 0;
583
                    c.gridwidth = GridBagConstraints.REMAINDER;
584
 
585
                    this.add(sepCompte, c);
586
 
587
                    c.gridwidth = 1;
588
                    c.gridy++;
589
                    c.gridx = 0;
590
                    c.weightx = 0;
591
                    this.add(this.labelCpt, c);
592
 
593
                    c.gridwidth = GridBagConstraints.REMAINDER;
594
                    c.gridx++;
595
                    c.weightx = 1;
596
 
597
                    this.add(this.compteSel, c);
598
 
599
                    this.boxGestionAutoCompte.addActionListener(new ActionListener() {
600
                        @Override
601
                        public void actionPerformed(ActionEvent e) {
602
 
603
                            setCompteVisible(!(boxGestionAutoCompte.isSelected() && getSelectedID() <= 1));
604
                        }
605
                    });
606
                }
607
                // Infos
608
                TitledSeparator infosSep = new TitledSeparator(getLabelFor("INFOS"));
609
                c.gridwidth = GridBagConstraints.REMAINDER;
610
                c.gridy++;
611
                c.gridx = 0;
612
                this.add(infosSep, c);
613
                ITextArea textInfos = new ITextArea();
614
                c.gridy++;
615
                c.weighty = 0.3;
616
                c.fill = GridBagConstraints.BOTH;
617
                this.add(textInfos, c);
618
 
619
                this.checkAdrLivraison.addActionListener(new ActionListener() {
620
 
621
                    public void actionPerformed(java.awt.event.ActionEvent e) {
622
                        boolean b = checkAdrLivraison.isSelected();
623
 
624
                        componentLivraison.setEditable(!b);
625
                        componentLivraison.setCreated(!b);
626
                    };
627
                });
628
 
629
                this.checkAdrFacturation.addActionListener(new ActionListener() {
630
                    public void actionPerformed(ActionEvent e) {
631
 
632
                        boolean b = checkAdrFacturation.isSelected();
633
 
634
                        componentFacturation.setEditable(!b);
635
 
636
                        componentFacturation.setCreated(!b);
637
                    }
638
                });
639
 
640
                this.addSQLObject(textType, "FORME_JURIDIQUE");
641
                this.addView(this.textNom, "NOM", REQ);
642
                this.addSQLObject(this.textCode, "CODE");
643
                this.addSQLObject(textFax, "FAX");
644
                this.addSQLObject(textSiren, "SIRET");
645
                this.addSQLObject(textMail, "MAIL");
646
                this.addSQLObject(textTel, "TEL");
647
                this.addSQLObject(textPortable, "TEL_P");
648
                this.addSQLObject(textNumIntracomm, "NUMERO_TVA");
649
                this.addSQLObject(textResp, "RESPONSABLE");
650
                this.addSQLObject(textInfos, "INFOS");
651
                this.addSQLObject(this.compteSel, "ID_COMPTE_PCE");
652
 
653
                this.checkAdrFacturation.setSelected(true);
654
                this.checkAdrLivraison.setSelected(true);
655
 
656
            }
657
 
658
            private void setCompteVisible(boolean b) {
659
 
660
                this.labelCpt.setVisible(b);
661
                this.compteSel.setVisible(b);
662
            }
663
 
664
            @Override
665
            public void update() {
666
                super.update();
667
                final int selectedID = getSelectedID();
668
                this.table.updateField("ID_CLIENT", selectedID);
669
                this.adresseTable.updateField("ID_CLIENT", selectedID);
670
                if (this.boxGestionAutoCompte.isSelected()) {
671
 
672
                    SQLRow row = getTable().getRow(selectedID);
673
                    if (row.getInt("ID_COMPTE_PCE") <= 1) {
674
                        createCompteClientAuto(selectedID);
675
                    } else {
676
                        SQLRow rowCpt = row.getForeignRow("ID_COMPTE_PCE");
677
                        String num = rowCpt.getString("NUMERO");
678
                        String initialClient = "";
679
                        String text;
680
                        if (this.textNom instanceof ITextArea) {
681
                            ITextArea textNomArea = (ITextArea) this.textNom;
682
                            text = textNomArea.getText();
683
                        } else {
684
                            ITextWithCompletion textNomArea = (ITextWithCompletion) this.textNom;
685
                            text = textNomArea.getValue();
686
                        }
687
                        // final String text = (String) this.textNom.getText();
688
                        if (text != null && text.trim().length() > 1) {
689
                            initialClient += text.trim().toUpperCase().charAt(0);
690
                        }
691
 
692
                        String compte = "411" + initialClient;
693
                        if (!num.startsWith(compte)) {
694
                            int answer = JOptionPane.showConfirmDialog(null, "Voulez vous changer le compte associé au client, le nom a changé?", "Modification compte client",
695
                                    JOptionPane.YES_NO_OPTION);
696
 
697
                            if (answer == JOptionPane.YES_OPTION) {
698
                                createCompteClientAuto(selectedID);
699
                            }
700
                        }
701
                    }
702
 
703
                }
704
            }
705
 
706
            @Override
707
            public void select(SQLRowAccessor r) {
708
 
709
                int idAdrL = 1;
710
                int idAdrF = 1;
711
                if (r != null && r.getID() > 1) {
712
                    final SQLRow row = ClientNormalSQLElement.this.getTable().getRow(r.getID());
713
                    idAdrL = row.getInt("ID_ADRESSE_L");
714
                    idAdrF = row.getInt("ID_ADRESSE_F");
715
                }
716
                super.select(r);
717
 
718
                this.checkAdrLivraison.setSelected(idAdrL == 1);
719
                this.checkAdrFacturation.setSelected(idAdrF == 1);
720
                if (r != null) {
721
                    this.table.insertFrom("ID_CLIENT", r.getID());
722
                    this.adresseTable.insertFrom("ID_CLIENT", r.getID());
723
                    this.defaultContactRowVals.put("TEL_DIRECT", r.getString("TEL"));
724
                    this.defaultContactRowVals.put("FAX", r.getString("FAX"));
725
                    this.textCode.setIdSelected(r.getID());
726
                }
727
            }
728
 
729
            private void createCompteClientAuto(int idClient) {
730
                SQLRowValues rowVals = getTable().getRow(idClient).createEmptyUpdateRow();
731
                String initialClient = "";
732
                String text;
733
                if (this.textNom instanceof ITextArea) {
734
                    ITextArea textNomArea = (ITextArea) this.textNom;
735
                    text = textNomArea.getText();
736
                } else {
737
                    ITextWithCompletion textNomArea = (ITextWithCompletion) this.textNom;
738
                    text = textNomArea.getValue();
739
                }
740
                if (text != null && text.trim().length() > 1) {
741
                    initialClient += text.trim().toUpperCase().charAt(0);
742
                }
743
 
744
                String compte = "411" + initialClient;
745
 
746
                SQLTable table = Configuration.getInstance().getDirectory().getElement("COMPTE_PCE").getTable();
747
                SQLSelect selCompte = new SQLSelect(this.getTable().getBase());
748
                selCompte.addSelectFunctionStar("COUNT");
749
                selCompte.setArchivedPolicy(SQLSelect.BOTH);
750
                selCompte.setWhere(new Where(table.getField("NUMERO"), "LIKE", compte + "%"));
751
                System.err.println(selCompte.asString());
752
                Object o = Configuration.getInstance().getBase().getDataSource().executeScalar(selCompte.asString());
753
 
754
                int nb = 0;
755
                if (o != null) {
756
                    Long i = (Long) o;
757
                    nb = i.intValue();
758
                }
759
 
760
                int idCpt = ComptePCESQLElement.getId(compte + this.format.format(nb), text);
761
                rowVals.put("ID_COMPTE_PCE", idCpt);
762
                try {
763
                    rowVals.update();
764
                } catch (SQLException e) {
765
                    e.printStackTrace();
766
                }
767
            }
768
 
769
            @Override
770
            public int insert(SQLRow order) {
771
 
772
                int id = super.insert(order);
773
                this.table.updateField("ID_CLIENT", id);
774
                this.adresseTable.updateField("ID_CLIENT", id);
775
                if (this.boxGestionAutoCompte.isSelected()) {
776
                    createCompteClientAuto(id);
777
                }
778
                return id;
779
            }
780
 
781
            @Override
782
            protected SQLRowValues createDefaults() {
783
                SQLRowValues vals = new SQLRowValues(this.getTable());
784
                SQLRow r;
785
 
786
                vals.put("MARCHE_PUBLIC", Boolean.TRUE);
787
 
788
                // Mode de règlement par defaut
789
                try {
790
                    r = ModeReglementDefautPrefPanel.getDefaultRow(true);
791
                    SQLElement eltModeReglement = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
792
                    if (r.getID() > 1) {
793
                        SQLRowValues rowVals = eltModeReglement.createCopy(r, null);
794
                        System.err.println(rowVals.getInt("ID_TYPE_REGLEMENT"));
795
                        vals.put("ID_MODE_REGLEMENT", rowVals);
796
                    }
797
                } catch (SQLException e) {
798
                    System.err.println("Impossible de sélectionner le mode de règlement par défaut du client.");
799
                    e.printStackTrace();
800
                }
801
 
802
                // Select Compte client par defaut
803
                final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
804
                final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
805
                final SQLRow rowPrefsCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte).getRowFromId(2);
806
 
807
                this.idDefaultCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
808
                if (this.idDefaultCompteClient <= 1) {
809
                    try {
810
                        this.idDefaultCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
811
                    } catch (Exception e) {
812
                        e.printStackTrace();
813
                    }
814
                }
815
 
816
                vals.put("ID_COMPTE_PCE", this.idDefaultCompteClient);
817
 
818
                return vals;
819
            }
820
        };
821
    }
822
 
823
    protected JPanel getAddOnPanel(BaseSQLComponent c) {
824
        return null;
825
    }
826
 
827
}