OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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