OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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