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
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.sales.quote.component;
15
 
41 ilm 16
import static org.openconcerto.utils.CollectionUtils.createSet;
142 ilm 17
 
18 ilm 18
import org.openconcerto.erp.config.ComptaPropsConfiguration;
63 ilm 19
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18 ilm 20
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
73 ilm 21
import org.openconcerto.erp.core.common.ui.AbstractArticleItemTable;
83 ilm 22
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable.TypeCalcul;
23
import org.openconcerto.erp.core.common.ui.Acompte;
24
import org.openconcerto.erp.core.common.ui.AcompteField;
25
import org.openconcerto.erp.core.common.ui.AcompteRowItemView;
18 ilm 26
import org.openconcerto.erp.core.common.ui.DeviseField;
27
import org.openconcerto.erp.core.common.ui.TotalPanel;
93 ilm 28
import org.openconcerto.erp.core.customerrelationship.customer.ui.AddressChoiceUI;
29
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseType;
174 ilm 30
import org.openconcerto.erp.core.customerrelationship.customer.ui.CategorieComptableChoiceUI;
144 ilm 31
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 32
import org.openconcerto.erp.core.sales.quote.element.EtatDevisSQLElement;
33
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
34
import org.openconcerto.erp.core.sales.quote.ui.DevisItemTable;
21 ilm 35
import org.openconcerto.erp.panel.PanelOOSQLComponent;
93 ilm 36
import org.openconcerto.erp.preferences.GestionClientPreferencePanel;
94 ilm 37
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
73 ilm 38
import org.openconcerto.map.ui.ITextComboVilleViewer;
18 ilm 39
import org.openconcerto.sql.Configuration;
40
import org.openconcerto.sql.element.BaseSQLComponent;
41
import org.openconcerto.sql.element.SQLElement;
42
import org.openconcerto.sql.model.SQLBackgroundTableCache;
43
import org.openconcerto.sql.model.SQLRow;
44
import org.openconcerto.sql.model.SQLRowAccessor;
45
import org.openconcerto.sql.model.SQLRowValues;
90 ilm 46
import org.openconcerto.sql.model.SQLSelect;
18 ilm 47
import org.openconcerto.sql.model.SQLTable;
65 ilm 48
import org.openconcerto.sql.model.UndefinedRowValuesCache;
73 ilm 49
import org.openconcerto.sql.model.Where;
93 ilm 50
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 51
import org.openconcerto.sql.sqlobject.ElementComboBox;
52
import org.openconcerto.sql.sqlobject.JUniqueTextField;
142 ilm 53
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
18 ilm 54
import org.openconcerto.sql.sqlobject.SQLTextCombo;
55
import org.openconcerto.sql.ui.RadioButtons;
56
import org.openconcerto.sql.users.UserManager;
142 ilm 57
import org.openconcerto.sql.users.rights.UserRightsManager;
18 ilm 58
import org.openconcerto.sql.view.EditFrame;
59
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 60
import org.openconcerto.ui.FormLayouter;
18 ilm 61
import org.openconcerto.ui.JDate;
62
import org.openconcerto.ui.TitledSeparator;
63
import org.openconcerto.ui.VFlowLayout;
64
import org.openconcerto.ui.component.ITextArea;
65
import org.openconcerto.utils.ExceptionHandler;
90 ilm 66
import org.openconcerto.utils.GestionDevise;
73 ilm 67
import org.openconcerto.utils.cc.ITransformer;
90 ilm 68
import org.openconcerto.utils.checks.ValidState;
18 ilm 69
import org.openconcerto.utils.text.SimpleDocumentListener;
70
 
71
import java.awt.Color;
72
import java.awt.Dimension;
73
import java.awt.GridBagConstraints;
74
import java.awt.GridBagLayout;
75
import java.awt.Insets;
73 ilm 76
import java.awt.event.ItemEvent;
77
import java.awt.event.ItemListener;
90 ilm 78
import java.awt.event.MouseEvent;
19 ilm 79
import java.beans.PropertyChangeEvent;
80
import java.beans.PropertyChangeListener;
18 ilm 81
import java.sql.SQLException;
19 ilm 82
import java.util.Calendar;
90 ilm 83
import java.util.Date;
84
import java.util.HashSet;
18 ilm 85
import java.util.List;
90 ilm 86
import java.util.Set;
18 ilm 87
 
88
import javax.swing.BorderFactory;
89
import javax.swing.ButtonGroup;
73 ilm 90
import javax.swing.JCheckBox;
18 ilm 91
import javax.swing.JLabel;
19 ilm 92
import javax.swing.JOptionPane;
18 ilm 93
import javax.swing.JPanel;
94
import javax.swing.JRadioButton;
95
import javax.swing.JScrollPane;
73 ilm 96
import javax.swing.JTabbedPane;
18 ilm 97
import javax.swing.JTextField;
98
import javax.swing.SwingConstants;
99
import javax.swing.SwingUtilities;
142 ilm 100
import javax.swing.SwingWorker;
18 ilm 101
import javax.swing.event.ChangeEvent;
102
import javax.swing.event.ChangeListener;
103
import javax.swing.event.DocumentEvent;
104
import javax.swing.event.TableModelEvent;
105
import javax.swing.event.TableModelListener;
106
 
107
public class DevisSQLComponent extends BaseSQLComponent {
73 ilm 108
    private AbstractArticleItemTable table;
18 ilm 109
    private JUniqueTextField numeroUniqueDevis;
110
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
111
    private final ITextArea infos = new ITextArea();
112
    private final RadioButtons radioEtat = new RadioButtons("NOM");
113
    private JTextField textPourcentRemise, textPoidsTotal;
114
    private DeviseField textRemiseHT;
115
    private DeviseField fieldHT;
21 ilm 116
    private PanelOOSQLComponent panelOO;
18 ilm 117
 
73 ilm 118
    // Site d'intervention
119
    final JTextField telSite = new JTextField(20);
120
    final ITextComboVilleViewer villeSite = new ITextComboVilleViewer();
121
    final JTextField faxSite = new JTextField(20);
122
    final JTextField telPSite = new JTextField(20);
123
    final JTextField mailSite = new JTextField(20);
124
    final JTextField contactSite = new JTextField(20);
125
    final JTextField desSite = new JTextField(20);
126
    final ITextArea adrSite = new ITextArea();
127
 
128
    // Donneur d'ordre
129
    final JTextField telDonneur = new JTextField(20);
130
    final JTextField sirenDonneur = new JTextField(20);
131
    final ITextComboVilleViewer villeDonneur = new ITextComboVilleViewer();
132
    final JTextField faxDonneur = new JTextField(20);
133
    final JTextField telPDonneur = new JTextField(20);
134
    final JTextField mailDonneur = new JTextField(20);
135
    final JTextField contactDonneur = new JTextField(20);
136
    final JTextField desDonneur = new JTextField(20);
137
    final ITextArea adrDonneur = new ITextArea();
93 ilm 138
    private final boolean displayDpt;
73 ilm 139
 
93 ilm 140
    private final ElementComboBox comboDpt = new ElementComboBox(false, 25);
141
 
18 ilm 142
    public DevisSQLComponent(final SQLElement elt) {
143
        super(elt);
93 ilm 144
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
145
        this.displayDpt = prefs.getBoolean(GestionClientPreferencePanel.DISPLAY_CLIENT_DPT, false);
18 ilm 146
    }
147
 
73 ilm 148
    public AbstractArticleItemTable getRowValuesTable() {
61 ilm 149
        return this.table;
150
    }
151
 
18 ilm 152
    @Override
90 ilm 153
    public Set<String> getPartialResetNames() {
154
        Set<String> s = new HashSet<String>();
155
        s.add("OBJET");
156
        s.add("NUMERO");
157
        return s;
158
    }
159
 
93 ilm 160
    private final JDate dateDevis = new JDate(true);
161
 
90 ilm 162
    @Override
18 ilm 163
    public void addViews() {
164
        setLayout(new GridBagLayout());
165
        final GridBagConstraints c = new DefaultGridBagConstraints();
166
 
25 ilm 167
        // Champ Module
168
        c.gridx = 0;
169
        c.gridy++;
170
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 171
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
41 ilm 172
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
25 ilm 173
        this.add(addP, c);
174
 
175
        c.gridy++;
176
        c.gridwidth = 1;
18 ilm 177
        final JLabel labelNumero = new JLabel(getLabelFor("NUMERO"));
178
        labelNumero.setHorizontalAlignment(SwingConstants.RIGHT);
179
        this.add(labelNumero, c);
180
 
181
        // Ligne 1: Numero du devis
182
 
183
        c.gridx++;
184
        c.weightx = 1;
185
        c.fill = GridBagConstraints.NONE;
142 ilm 186
            this.numeroUniqueDevis = new JUniqueTextField(15) {
187
                @Override
188
                public String getAutoRefreshNumber() {
189
                    if (getMode() == Mode.INSERTION) {
190
                        return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), dateDevis.getDate());
191
                    } else {
192
                        return null;
193
                    }
194
                }
195
            };
90 ilm 196
 
18 ilm 197
        DefaultGridBagConstraints.lockMinimumSize(this.numeroUniqueDevis);
198
        DefaultGridBagConstraints.lockMaximumSize(this.numeroUniqueDevis);
199
        this.add(this.numeroUniqueDevis, c);
200
 
201
        // Date
202
 
203
        c.gridx++;
204
        c.weightx = 0;
205
        c.fill = GridBagConstraints.HORIZONTAL;
206
        final JLabel labelDate = new JLabel(getLabelFor("DATE"));
207
 
208
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
209
 
210
        this.add(labelDate, c);
211
        c.gridx++;
212
 
213
        c.weightx = 1;
214
 
215
        c.fill = GridBagConstraints.NONE;
93 ilm 216
        this.add(this.dateDevis, c);
18 ilm 217
 
93 ilm 218
        this.dateDevis.addValueListener(new PropertyChangeListener() {
219
 
220
            @Override
221
            public void propertyChange(PropertyChangeEvent evt) {
222
                if (!isFilling() && dateDevis.getValue() != null) {
223
                    table.setDateDevise(dateDevis.getValue());
224
                }
225
            }
226
        });
227
 
18 ilm 228
        // Etat devis
229
        this.radioEtat.setLayout(new VFlowLayout());
230
        this.radioEtat.setBorder(BorderFactory.createTitledBorder(getLabelFor("ID_ETAT_DEVIS")));
231
        c.gridwidth = GridBagConstraints.REMAINDER;
61 ilm 232
        c.gridheight = 5;
18 ilm 233
        c.weightx = 0;
234
        c.fill = GridBagConstraints.NONE;
235
        c.anchor = GridBagConstraints.EAST;
236
        c.gridx += 2;
73 ilm 237
        this.radioEtat.initLocalization(new ITransformer<String, String>() {
238
            @Override
239
            public String transformChecked(String id) {
240
                return id;
241
                // return TranslationManager.getInstance().getTranslationForItem(id);
242
            }
243
        });
18 ilm 244
        this.add(this.radioEtat, c);
41 ilm 245
        // this.radioEtat.setVisible(false);
18 ilm 246
 
247
        // Ligne 2: Reference
248
        c.gridx = 0;
249
        c.gridy++;
250
        c.gridwidth = 1;
251
        c.gridheight = 1;
252
        c.fill = GridBagConstraints.HORIZONTAL;
253
 
254
        c.weightx = 0;
255
 
256
        final JLabel labelObjet = new JLabel(getLabelFor("OBJET"));
257
        labelObjet.setHorizontalAlignment(SwingConstants.RIGHT);
258
 
259
        this.add(labelObjet, c);
260
 
261
        final SQLTextCombo textObjet = new SQLTextCombo();
262
        c.gridx++;
263
        c.weightx = 1;
264
        c.gridwidth = 1;
265
        c.fill = GridBagConstraints.BOTH;
266
        this.add(textObjet, c);
267
 
268
        String field;
269
            field = "ID_COMMERCIAL";
270
        // Commercial
271
        final JLabel labelCommercial = new JLabel(getLabelFor(field));
272
        labelCommercial.setHorizontalAlignment(SwingConstants.RIGHT);
273
        c.gridx++;
274
        c.fill = GridBagConstraints.HORIZONTAL;
275
        c.weightx = 0;
276
 
277
        this.add(labelCommercial, c);
278
 
279
        final ElementComboBox comboCommercial = new ElementComboBox(false, 25);
280
 
281
        comboCommercial.setListIconVisible(false);
282
        c.gridx++;
283
        c.weightx = 1;
284
        c.fill = GridBagConstraints.NONE;
285
        c.anchor = GridBagConstraints.WEST;
286
 
287
        this.add(comboCommercial, c);
288
        addRequiredSQLObject(comboCommercial, field);
289
 
290
        // Ligne 3: Client
291
        c.gridx = 0;
292
        c.gridy++;
293
        c.gridwidth = 1;
294
        final JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"));
295
        labelClient.setHorizontalAlignment(SwingConstants.RIGHT);
296
        c.weightx = 0;
297
        c.gridwidth = 1;
298
        c.fill = GridBagConstraints.HORIZONTAL;
299
        this.add(labelClient, c);
300
 
301
        final ElementComboBox comboClient = new ElementComboBox();
302
        c.gridx++;
19 ilm 303
        c.gridwidth = 1;
18 ilm 304
        c.weightx = 0;
305
        c.weighty = 0;
306
        c.fill = GridBagConstraints.NONE;
307
        this.add(comboClient, c);
73 ilm 308
        addRequiredSQLObject(comboClient, "ID_CLIENT");
309
 
94 ilm 310
        comboClient.addModelListener("wantedID", new PropertyChangeListener() {
311
 
312
            @Override
313
            public void propertyChange(PropertyChangeEvent evt) {
314
                int wantedID = comboClient.getWantedID();
315
 
316
                if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
317
                    final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
318
                    if (!rowClient.isForeignEmpty("ID_COMMERCIAL")) {
319
                        comboCommercial.setValue(rowClient.getForeignID("ID_COMMERCIAL"));
320
                    }
321
                }
322
            }
323
        });
93 ilm 324
        if (this.displayDpt) {
325
            c.gridx++;
326
            c.gridwidth = 1;
327
            final JLabel labelDpt = new JLabel(getLabelFor("ID_CLIENT_DEPARTEMENT"));
328
            labelDpt.setHorizontalAlignment(SwingConstants.RIGHT);
329
            c.weightx = 0;
330
            c.gridwidth = 1;
331
            c.fill = GridBagConstraints.HORIZONTAL;
332
            this.add(labelDpt, c);
333
 
334
            c.gridx++;
335
            c.gridwidth = 1;
336
            c.weightx = 0;
337
            c.weighty = 0;
338
            c.fill = GridBagConstraints.NONE;
339
            this.add(this.comboDpt, c);
340
            DefaultGridBagConstraints.lockMinimumSize(this.comboDpt);
341
            addSQLObject(this.comboDpt, "ID_CLIENT_DEPARTEMENT");
342
 
343
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
344
 
345
                @Override
346
                public void propertyChange(PropertyChangeEvent evt) {
347
                    int wantedID = comboClient.getWantedID();
348
 
349
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
350
                        final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
351
                        comboDpt.getRequest().setWhere(new Where(comboDpt.getRequest().getPrimaryTable().getField("ID_CLIENT"), "=", rowClient.getID()));
352
                    } else {
353
                        comboDpt.getRequest().setWhere(null);
354
                    }
355
                }
356
            });
357
 
358
        }
149 ilm 359
        final SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
93 ilm 360
 
19 ilm 361
        final ElementComboBox boxTarif = new ElementComboBox();
149 ilm 362
 
94 ilm 363
            if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ADDRESS_SPEC, true)) {
93 ilm 364
 
94 ilm 365
                final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
366
                final AddressChoiceUI addressUI = new AddressChoiceUI();
367
                addressUI.addToUI(this, c);
368
                comboClient.addModelListener("wantedID", new PropertyChangeListener() {
93 ilm 369
 
94 ilm 370
                    @Override
371
                    public void propertyChange(PropertyChangeEvent evt) {
372
                        int wantedID = comboClient.getWantedID();
373
                        System.err.println("SET WHERE ID_CLIENT = " + wantedID);
374
                        if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
93 ilm 375
 
132 ilm 376
                            addressUI.getComboAdrF().getRequest().setWhere(
377
                                    new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Invoice.getId())));
378
                            addressUI.getComboAdrL().getRequest().setWhere(
379
                                    new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Delivery.getId())));
94 ilm 380
                        } else {
381
                            addressUI.getComboAdrF().getRequest().setWhere(Where.FALSE);
382
                            addressUI.getComboAdrL().getRequest().setWhere(Where.FALSE);
383
                        }
93 ilm 384
                    }
94 ilm 385
                });
386
            }
174 ilm 387
            if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.CATEGORIE_COMPTABLE_SPEC, false)) {
388
                // cat spe
389
                final CategorieComptableChoiceUI catUI = new CategorieComptableChoiceUI();
390
                catUI.addToUI(this, c);
391
                catUI.getCombo().addModelListener("wantedID", new PropertyChangeListener() {
392
 
393
                    @Override
394
                    public void propertyChange(PropertyChangeEvent evt) {
395
                        int wantedID = catUI.getCombo().getWantedID();
396
                        if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
397
                            table.setRowCatComptable(catUI.getCombo().getElement().getTable().getRow(wantedID));
398
                        } else {
399
                            table.setRowCatComptable(null);
400
                        }
401
                    }
402
                });
403
            }
83 ilm 404
            if (getTable().contains("ID_CONTACT")) {
405
                // Contact Client
406
                c.gridx = 0;
407
                c.gridy++;
408
                c.gridwidth = 1;
409
                final JLabel labelContact = new JLabel(getLabelFor("ID_CONTACT"));
410
                labelContact.setHorizontalAlignment(SwingConstants.RIGHT);
411
                c.weightx = 0;
412
                c.gridwidth = 1;
413
                c.fill = GridBagConstraints.HORIZONTAL;
414
                this.add(labelContact, c);
18 ilm 415
 
83 ilm 416
                final ElementComboBox comboContact = new ElementComboBox();
417
                c.gridx++;
418
                c.gridwidth = 1;
419
                c.weightx = 0;
420
                c.weighty = 0;
421
                c.fill = GridBagConstraints.NONE;
422
                this.add(comboContact, c);
423
                final SQLElement contactElement = getElement().getForeignElement("ID_CONTACT");
424
                comboContact.init(contactElement, contactElement.getComboRequest(true));
93 ilm 425
                comboContact.getRequest().setWhere(Where.FALSE);
83 ilm 426
                DefaultGridBagConstraints.lockMinimumSize(comboContact);
427
                this.addView(comboContact, "ID_CONTACT");
428
                comboClient.addModelListener("wantedID", new PropertyChangeListener() {
429
 
430
                    @Override
431
                    public void propertyChange(PropertyChangeEvent evt) {
432
                        int wantedID = comboClient.getWantedID();
433
                        System.err.println("SET WHERE ID_CLIENT = " + wantedID);
434
                        if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
435
 
436
                            final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
437
                            int idClient = rowClient.getID();
438
                            comboContact.getRequest().setWhere(new Where(contactElement.getTable().getField("ID_CLIENT"), "=", idClient));
174 ilm 439
                            if (!isFilling()) {
440
                                table.setClient(rowClient, true);
441
                            }
442
 
83 ilm 443
                        } else {
93 ilm 444
                            comboContact.getRequest().setWhere(Where.FALSE);
174 ilm 445
                            if (!isFilling()) {
446
                                table.setClient(null, true);
447
                            }
83 ilm 448
                            DevisSQLComponent.this.table.setTarif(null, false);
449
                        }
450
                    }
451
                });
452
 
453
            }
454
 
19 ilm 455
        if (getTable().getFieldsName().contains("DATE_VALIDITE")) {
456
            c.gridx++;
93 ilm 457
            c.fill = GridBagConstraints.HORIZONTAL;
458
            this.add(new JLabel(getLabelFor("DATE_VALIDITE"), SwingConstants.RIGHT), c);
459
            c.fill = GridBagConstraints.NONE;
19 ilm 460
            c.gridx++;
461
            JDate dateValidite = new JDate();
462
            this.add(dateValidite, c);
463
            this.addView(dateValidite, "DATE_VALIDITE");
464
        }
465
 
466
        // Tarif
467
        if (this.getTable().getFieldsName().contains("ID_TARIF")) {
468
            // TARIF
469
            c.gridy++;
470
            c.gridx = 0;
471
            c.weightx = 0;
472
            c.weighty = 0;
473
            c.gridwidth = 1;
93 ilm 474
            c.fill = GridBagConstraints.HORIZONTAL;
475
            JLabel comp = new JLabel(getLabelFor("ID_TARIF"), SwingConstants.RIGHT);
61 ilm 476
            this.add(comp, c);
19 ilm 477
            c.gridx++;
93 ilm 478
            c.fill = GridBagConstraints.NONE;
19 ilm 479
            c.weightx = 1;
480
            this.add(boxTarif, c);
481
            this.addView(boxTarif, "ID_TARIF");
482
            DefaultGridBagConstraints.lockMinimumSize(boxTarif);
73 ilm 483
            boxTarif.addModelListener("wantedID", new PropertyChangeListener() {
19 ilm 484
 
485
                @Override
486
                public void propertyChange(PropertyChangeEvent evt) {
73 ilm 487
 
488
                    SQLRow selectedRow = boxTarif.getRequest().getPrimaryTable().getRow(boxTarif.getWantedID());
489
                    table.setTarif(selectedRow, !isFilling());
19 ilm 490
                }
491
            });
93 ilm 492
            if (this.getTable().getFieldsName().contains("DUNNING_DATE")) {
493
                c.gridx++;
494
                c.weightx = 0;
151 ilm 495
                c.fill = GridBagConstraints.HORIZONTAL;
93 ilm 496
                this.add(new JLabel(getLabelFor("DUNNING_DATE"), SwingConstants.RIGHT), c);
497
                c.gridx++;
151 ilm 498
                c.fill = GridBagConstraints.NONE;
93 ilm 499
                JDate dateRelance = new JDate();
500
                this.add(dateRelance, c);
501
                if (getTable().getDBRoot().contains("TARIF_AGENCE")) {
502
                    this.addView(dateRelance, "DUNNING_DATE", REQ);
503
                } else {
504
                    this.addView(dateRelance, "DUNNING_DATE");
505
                }
506
            }
507
 
19 ilm 508
        }
509
 
18 ilm 510
        // Table d'élément
73 ilm 511
            this.table = new DevisItemTable();
512
 
83 ilm 513
            final AcompteField acompteField = new AcompteField();
514
            acompteField.getDocument().addDocumentListener(new SimpleDocumentListener() {
515
 
516
                @Override
517
                public void update(DocumentEvent e) {
518
                    Acompte a = acompteField.getValue();
519
                    ((DevisItemTable) table).calculPourcentage(a, TypeCalcul.CALCUL_REMISE);
520
                }
521
            });
522
 
523
            // Remise
524
            c.gridy++;
525
            c.gridx = 0;
526
            c.weightx = 0;
527
            c.weighty = 0;
528
            c.gridwidth = 1;
529
            JLabel comp = new JLabel(getLabelFor("MONTANT_REMISE"), SwingConstants.RIGHT);
530
            // this.add(comp, c);
531
            c.gridx++;
532
            c.gridwidth = GridBagConstraints.REMAINDER;
533
 
534
            c.weightx = 1;
535
            // this.add(acompteField, c);
536
            this.addView(new AcompteRowItemView(acompteField), "MONTANT_REMISE,POURCENT_REMISE", null);
537
 
538
 
18 ilm 539
        c.fill = GridBagConstraints.BOTH;
540
        c.gridx = 0;
73 ilm 541
        c.gridy += 5;
18 ilm 542
        c.weighty = 1;
543
        c.gridwidth = GridBagConstraints.REMAINDER;
544
        this.add(this.table, c);
73 ilm 545
        this.addView(this.table.getRowValuesTable(), "");
18 ilm 546
 
547
        // Panel en bas
548
        final JPanel bottomPanel = new JPanel(new GridBagLayout());
549
        final GridBagConstraints cBottom = new DefaultGridBagConstraints();
550
        // 1ere Colonne : Infos
551
        cBottom.weightx = 1;
552
        bottomPanel.add(new TitledSeparator(getLabelFor("INFOS")), cBottom);
553
        cBottom.gridy++;
554
        cBottom.fill = GridBagConstraints.BOTH;
555
        cBottom.weighty = 0;
556
 
557
        final JScrollPane scrollPane = new JScrollPane(this.infos);
558
        scrollPane.setBorder(null);
559
        bottomPanel.add(scrollPane, cBottom);
560
 
561
        // 2eme Colonne : Poids total
562
        final JPanel panel = new JPanel(new GridBagLayout());
563
 
564
        this.textPoidsTotal = new JTextField(8);
565
        this.textPoidsTotal.setText("0.0");
566
        final GridBagConstraints cPanel = new DefaultGridBagConstraints();
567
        panel.add(new JLabel(getLabelFor("T_POIDS")), cPanel);
568
        cPanel.weightx = 0;
569
        cPanel.gridx++;
570
        panel.add(this.textPoidsTotal, cPanel);
571
        this.textPoidsTotal.setEnabled(false);
572
        this.textPoidsTotal.setEditable(false);
573
        this.textPoidsTotal.setHorizontalAlignment(SwingConstants.RIGHT);
574
        this.textPoidsTotal.setDisabledTextColor(Color.BLACK);
575
 
576
        // Port
577
 
578
        final DeviseField textPortHT = new DeviseField();
144 ilm 579
        SQLRequestComboBox boxTaxePort = new SQLRequestComboBox(false, 8);
18 ilm 580
 
144 ilm 581
        if (getTable().contains("ID_TAXE_PORT")) {
582
 
583
            JLabel labelPortHT = new JLabel(getLabelFor("PORT_HT"));
584
            labelPortHT.setHorizontalAlignment(SwingConstants.RIGHT);
585
            cPanel.gridx = 0;
586
            cPanel.gridy++;
587
            panel.add(labelPortHT, cPanel);
588
            cPanel.gridx++;
589
            panel.add(textPortHT, cPanel);
590
 
591
            JLabel labelTaxeHT = new JLabel(getLabelFor("ID_TAXE_PORT"));
592
            labelTaxeHT.setHorizontalAlignment(SwingConstants.RIGHT);
593
            cPanel.gridx = 0;
594
            cPanel.gridy++;
595
            panel.add(labelTaxeHT, cPanel);
596
            cPanel.gridx++;
597
            panel.add(boxTaxePort, cPanel);
598
            this.addView(boxTaxePort, "ID_TAXE_PORT", REQ);
599
 
600
        }
601
 
18 ilm 602
        // Remise HT
603
        final JRadioButton radioEuros = new JRadioButton("en €");
604
        final JRadioButton radioPourCent = new JRadioButton("en %");
605
        final ButtonGroup radioGroup = new ButtonGroup();
606
        radioGroup.add(radioEuros);
607
        radioGroup.add(radioPourCent);
608
        radioEuros.setSelected(true);
609
 
610
        final JPanel panelRemise = new JPanel(new GridBagLayout());
611
        final GridBagConstraints cRemise = new DefaultGridBagConstraints();
612
        cRemise.insets = new Insets(0, 0, 1, 0);
613
        final JLabel labelRemise = new JLabel(getLabelFor("REMISE_HT"));
614
        panelRemise.add(labelRemise, cRemise);
132 ilm 615
        // cRemise.gridx++;
616
        // panelRemise.add(radioEuros, cRemise);
18 ilm 617
 
618
        cRemise.gridx++;
619
        cRemise.weightx = 0;
620
        this.textRemiseHT = new DeviseField();
621
        panelRemise.add(this.textRemiseHT, cRemise);
151 ilm 622
        this.textRemiseHT.setMinimumSize(new Dimension(150, this.textRemiseHT.getMinimumSize().height));
623
        this.textRemiseHT.setPreferredSize(new Dimension(150, this.textRemiseHT.getPreferredSize().height));
18 ilm 624
 
149 ilm 625
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
626
            // Acompte
627
            cRemise.gridy++;
628
            cRemise.gridx = 0;
629
            cRemise.weightx = 0;
630
            cRemise.weighty = 0;
631
            cRemise.gridwidth = 1;
632
            JLabel compAcompte = new JLabel(getLabelFor("T_ACOMPTE"), SwingConstants.RIGHT);
633
            panelRemise.add(compAcompte, cRemise);
634
            cRemise.gridx++;
635
            cRemise.gridwidth = GridBagConstraints.REMAINDER;
636
            DeviseField fieldAcompte = new DeviseField();
637
            fieldAcompte.setEnabled(false);
638
            cRemise.weightx = 1;
639
            panelRemise.add(fieldAcompte, cRemise);
640
            this.addView(fieldAcompte, "T_ACOMPTE");
641
        }
642
 
132 ilm 643
        // cRemise.gridx = 1;
644
        // cRemise.gridy++;
645
        // cRemise.weightx = 0;
646
        // panelRemise.add(radioPourCent, cRemise);
647
        //
18 ilm 648
        this.textPourcentRemise = new JTextField(5);
132 ilm 649
        // DefaultGridBagConstraints.lockMinimumSize(this.textPourcentRemise);
650
        // cRemise.gridx++;
651
        // panelRemise.add(this.textPourcentRemise, cRemise);
18 ilm 652
 
653
        cPanel.gridx = 0;
654
        cPanel.gridy++;
655
        cPanel.gridwidth = 2;
656
        panel.add(panelRemise, cPanel);
657
 
658
        cBottom.gridy = 0;
659
        cBottom.gridx++;
660
        cBottom.weighty = 0;
661
        cBottom.weightx = 1;
662
        cBottom.gridheight = 2;
663
        cBottom.fill = GridBagConstraints.HORIZONTAL;
664
        cBottom.anchor = GridBagConstraints.NORTHEAST;
665
        DefaultGridBagConstraints.lockMinimumSize(panel);
132 ilm 666
        bottomPanel.add(panel, cBottom);
18 ilm 667
 
668
        addSQLObject(this.textRemiseHT, "REMISE_HT");
669
        addSQLObject(textPortHT, "PORT_HT");
670
        // this.checkImpression.setSelected(true);
671
 
672
        // Total
673
        this.fieldHT = new DeviseField();
674
        final DeviseField fieldTVA = new DeviseField();
675
        final DeviseField fieldTTC = new DeviseField();
142 ilm 676
        final DeviseField fieldEco = new DeviseField();
19 ilm 677
        final DeviseField fieldDevise = new DeviseField();
18 ilm 678
        final DeviseField fieldService = new DeviseField();
73 ilm 679
        this.fieldHT.setEditable(false);
18 ilm 680
        fieldTVA.setEditable(false);
681
        fieldTTC.setEditable(false);
682
        fieldService.setEditable(false);
142 ilm 683
        fieldEco.setEditable(false);
18 ilm 684
 
142 ilm 685
        addSQLObject(fieldEco, "T_ECO_CONTRIBUTION");
18 ilm 686
        addRequiredSQLObject(this.fieldHT, "T_HT");
687
        addRequiredSQLObject(fieldTVA, "T_TVA");
19 ilm 688
        addSQLObject(fieldDevise, "T_DEVISE");
18 ilm 689
        addRequiredSQLObject(fieldTTC, "T_TTC");
690
        addRequiredSQLObject(fieldService, "T_SERVICE");
19 ilm 691
        JTextField poids = new JTextField();
692
        // addSQLObject(poids, "T_POIDS");
41 ilm 693
 
694
        // FIXME Field add field T_HA dans installation avec recalcul des devis deja saisis
695
        final DeviseField fieldHA = new DeviseField();
696
 
61 ilm 697
        if (getTable().contains("PREBILAN")) {
698
            addSQLObject(fieldHA, "PREBILAN");
699
        } else if (getTable().contains("T_HA")) {
700
 
41 ilm 701
            addSQLObject(fieldHA, "T_HA");
702
        }
703
 
144 ilm 704
        final TotalPanel totalTTC = new TotalPanel(this.table, fieldEco, this.fieldHT, fieldTVA, fieldTTC, textPortHT, this.textRemiseHT, fieldService, fieldHA, fieldDevise, poids, null,
705
                (getTable().contains("ID_TAXE_PORT") ? boxTaxePort : null), null);
18 ilm 706
        cBottom.gridy = 0;
707
        cBottom.gridx += 2;
708
        cBottom.gridheight = 2;
709
        cBottom.gridwidth = 1;
710
        cBottom.fill = GridBagConstraints.NONE;
711
        cBottom.weightx = 0;
712
        cBottom.anchor = GridBagConstraints.NORTHEAST;
713
        DefaultGridBagConstraints.lockMinimumSize(totalTTC);
714
        bottomPanel.add(totalTTC, cBottom);
715
 
716
        c.gridy++;
717
        c.gridx = 0;
718
        c.weighty = 0;
719
        c.fill = GridBagConstraints.HORIZONTAL;
720
        c.gridwidth = GridBagConstraints.REMAINDER;
721
        this.add(bottomPanel, c);
722
 
723
        c.gridx = 0;
724
        c.gridy++;
725
        c.weighty = 0;
726
        c.fill = GridBagConstraints.NONE;
727
        c.anchor = GridBagConstraints.SOUTHEAST;
728
 
21 ilm 729
        this.panelOO = new PanelOOSQLComponent(this);
730
        this.add(this.panelOO, c);
731
 
144 ilm 732
        boxTaxePort.addValueListener(new PropertyChangeListener() {
733
 
734
            @Override
735
            public void propertyChange(PropertyChangeEvent evt) {
736
                totalTTC.updateTotal();
737
            }
738
        });
739
 
18 ilm 740
        textPortHT.getDocument().addDocumentListener(new SimpleDocumentListener() {
741
 
742
            @Override
743
            public void update(final DocumentEvent e) {
744
                totalTTC.updateTotal();
745
            }
746
        });
747
 
748
        this.textRemiseHT.getDocument().addDocumentListener(new SimpleDocumentListener() {
749
 
750
            @Override
751
            public void update(final DocumentEvent e) {
752
                totalTTC.updateTotal();
753
            }
754
        });
755
 
756
        this.textPourcentRemise.getDocument().addDocumentListener(new SimpleDocumentListener() {
757
 
758
            @Override
759
            public void update(final DocumentEvent e) {
760
                calculPourcentage();
761
            }
762
        });
763
 
764
        radioEuros.addChangeListener(new ChangeListener() {
765
 
766
            public void stateChanged(final ChangeEvent e) {
767
                DevisSQLComponent.this.textRemiseHT.setEnabled(radioEuros.isSelected());
768
                DevisSQLComponent.this.textPourcentRemise.setEnabled(!radioEuros.isSelected());
769
            }
770
        });
771
 
772
        this.table.getModel().addTableModelListener(new TableModelListener() {
773
 
774
            public void tableChanged(final TableModelEvent e) {
775
                DevisSQLComponent.this.textPoidsTotal.setText(String.valueOf(DevisSQLComponent.this.table.getPoidsTotal()));
776
            }
777
        });
778
 
779
        addSQLObject(textObjet, "OBJET");
780
        addSQLObject(this.textPoidsTotal, "T_POIDS");
781
        addRequiredSQLObject(dateDevis, "DATE");
782
        addRequiredSQLObject(this.radioEtat, "ID_ETAT_DEVIS");
783
        addRequiredSQLObject(this.numeroUniqueDevis, "NUMERO");
784
        addSQLObject(this.infos, "INFOS");
73 ilm 785
        comboClient.addModelListener("wantedID", new PropertyChangeListener() {
18 ilm 786
 
73 ilm 787
            @Override
788
            public void propertyChange(PropertyChangeEvent arg0) {
789
                if (!isFilling()) {
790
                    Integer id = comboClient.getWantedID();
791
 
792
                    if (id > 1) {
793
 
794
                        SQLRow row = comboClient.getElement().getTable().getRow(id);
795
                        if (comboClient.getElement().getTable().getFieldsName().contains("ID_TARIF")) {
796
 
797
                            SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
798
                            if (!foreignRow.isUndefined() && (boxTarif.getSelectedRow() == null || boxTarif.getSelectedId() != foreignRow.getID())
799
                                    && JOptionPane.showConfirmDialog(null, "Appliquer les tarifs associés au client?") == JOptionPane.YES_OPTION) {
800
                                boxTarif.setValue(foreignRow.getID());
801
                                // SaisieVenteFactureSQLComponent.this.tableFacture.setTarif(foreignRow,
802
                                // true);
803
                            } else {
804
                                boxTarif.setValue(foreignRow.getID());
805
                            }
806
 
807
                            // SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
808
                            // if (foreignRow.isUndefined() &&
809
                            // !row.getForeignRow("ID_DEVISE").isUndefined()) {
810
                            // SQLRowValues rowValsD = new SQLRowValues(foreignRow.getTable());
811
                            // rowValsD.put("ID_DEVISE", row.getObject("ID_DEVISE"));
812
                            // foreignRow = rowValsD;
813
                            //
814
                            // }
815
                            // table.setTarif(foreignRow, true);
816
                        }
817
                    }
818
                }
819
 
820
            }
821
        });
18 ilm 822
        DefaultGridBagConstraints.lockMinimumSize(comboCommercial);
823
        DefaultGridBagConstraints.lockMinimumSize(comboClient);
824
    }
825
 
73 ilm 826
    private enum Type_Diff {
827
        SITE("SITE"), DONNEUR_ORDRE("DONNEUR");
828
        private final String name;
829
 
830
        private Type_Diff(String name) {
831
            this.name = name;
832
        }
833
 
834
        public String getName() {
835
            return this.name;
836
        }
837
    }
838
 
90 ilm 839
    private ValidState validStateContact = ValidState.getTrueInstance();
840
 
841
    @Override
842
    public synchronized ValidState getValidState() {
843
        assert SwingUtilities.isEventDispatchThread();
844
        return super.getValidState().and(this.validStateContact);
845
    }
846
 
132 ilm 847
    private JPanel createGestionSaisieRapport() {
848
        GridBagConstraints cTabSite = new DefaultGridBagConstraints();
849
        JPanel tabSite = new JPanel(new GridBagLayout());
850
 
851
        if (getTable().contains("RAPPORT_A_SAISIR")) {
852
            cTabSite.gridx = 0;
853
            cTabSite.gridy++;
854
            cTabSite.gridheight = 1;
855
            cTabSite.fill = GridBagConstraints.HORIZONTAL;
856
            cTabSite.weightx = 0;
857
 
142 ilm 858
            // Coche
859
            cTabSite.gridx = 0;
860
            cTabSite.gridwidth = 2;
861
            cTabSite.weightx = 0;
862
            JCheckBox boxAsaisir = new JCheckBox("Rapport à saisir");
863
            tabSite.add(boxAsaisir, cTabSite);
864
 
132 ilm 865
            // Type Rapport
866
            JLabel labelRapport = new JLabel(getLabelFor("TYPE_RAPPORT"));
142 ilm 867
            cTabSite.gridx = 2;
132 ilm 868
            tabSite.add(labelRapport, cTabSite);
869
            SQLTextCombo comboType = new SQLTextCombo();
870
            cTabSite.gridwidth = GridBagConstraints.REMAINDER;
871
            cTabSite.gridx++;
142 ilm 872
            cTabSite.weightx = 1;
132 ilm 873
            tabSite.add(comboType, cTabSite);
874
            cTabSite.gridwidth = 1;
875
 
142 ilm 876
            // Nb Rapport
132 ilm 877
            cTabSite.gridx = 0;
878
            cTabSite.gridy++;
142 ilm 879
            cTabSite.weightx = 0;
880
            tabSite.add(new JLabel(getLabelFor("NB_RAPPORT_A_SAISIR")), cTabSite);
881
            cTabSite.gridx++;
882
            cTabSite.weightx = 1;
883
            JTextField fieldNbRapportASaisir = new JTextField(5);
884
            tabSite.add(fieldNbRapportASaisir, cTabSite);
885
 
886
            // Date
887
            cTabSite.gridx++;
888
            cTabSite.weightx = 0;
132 ilm 889
            tabSite.add(new JLabel(getLabelFor("DATE_DEMANDE_SAISIE")), cTabSite);
890
            cTabSite.gridx++;
142 ilm 891
            cTabSite.weightx = 0;
132 ilm 892
            JDate dateDmdSaisie = new JDate();
893
            tabSite.add(dateDmdSaisie, cTabSite);
894
 
895
            cTabSite.gridx++;
896
            cTabSite.gridwidth = 2;
142 ilm 897
            cTabSite.weightx = 0;
132 ilm 898
            JCheckBox boxAcceptSaisie = new JCheckBox("Accepté en saisie");
899
            tabSite.add(boxAcceptSaisie, cTabSite);
900
            cTabSite.gridwidth = 1;
901
 
142 ilm 902
            cTabSite.gridy++;
132 ilm 903
            cTabSite.gridx = 0;
142 ilm 904
            cTabSite.weightx = 0;
905
            tabSite.add(new JLabel(getLabelFor("NB_RAPPORT_SAISI")), cTabSite);
906
            cTabSite.gridx++;
907
            cTabSite.weightx = 1;
908
            JTextField fieldNbRapportSaisi = new JTextField(5);
909
            tabSite.add(fieldNbRapportSaisi, cTabSite);
910
 
911
            cTabSite.gridx++;
912
            cTabSite.weightx = 0;
913
            tabSite.add(new JLabel(getLabelFor("DATE_SAISIE")), cTabSite);
914
            cTabSite.gridx++;
915
            cTabSite.weightx = 0;
916
            JDate dateSaisie = new JDate();
917
            tabSite.add(dateSaisie, cTabSite);
918
            if (!UserRightsManager.getCurrentUserRights().haveRight("VOIR_DEVIS_4_SEMAINES")) {
919
                cTabSite.gridx++;
920
                cTabSite.weightx = 0;
921
                tabSite.add(new JLabel(getLabelFor("DATE_SAISIE_REELLE")), cTabSite);
922
                cTabSite.gridx++;
923
                cTabSite.weightx = 0;
924
                JDate dateSaisieR = new JDate();
925
                tabSite.add(dateSaisieR, cTabSite);
926
                this.addView(dateSaisieR, "DATE_SAISIE_REELLE");
927
            }
132 ilm 928
            cTabSite.gridy++;
142 ilm 929
            cTabSite.weightx = 0;
930
            cTabSite.gridx = 0;
931
            tabSite.add(new JLabel(getLabelFor("TEMPS_SAISIE")), cTabSite);
132 ilm 932
            cTabSite.gridx++;
142 ilm 933
            cTabSite.weightx = 1;
934
            JTextField fieldTpsSaisi = new JTextField(5);
935
            tabSite.add(fieldTpsSaisi, cTabSite);
132 ilm 936
 
937
            cTabSite.gridx++;
142 ilm 938
            cTabSite.weightx = 0;
939
            tabSite.add(new JLabel(getLabelFor("ID_USER_COMMON_SAISIE")), cTabSite);
132 ilm 940
            cTabSite.gridx++;
142 ilm 941
            cTabSite.weightx = 1;
942
            SQLRequestComboBox boxUserSaisi = new SQLRequestComboBox();
943
            tabSite.add(boxUserSaisi, cTabSite);
132 ilm 944
 
945
            // Date de demande
946
            // Nb rapport a saisie
947
            // Type
948
            // Nb Rapport saisie
949
            this.addView(fieldNbRapportASaisir, "NB_RAPPORT_A_SAISIR");
950
            this.addView(fieldNbRapportSaisi, "NB_RAPPORT_SAISI");
951
 
952
            this.addView(dateDmdSaisie, "DATE_DEMANDE_SAISIE");
953
            this.addView(comboType, "TYPE_RAPPORT");
954
            this.addView(dateSaisie, "DATE_SAISIE");
955
            this.addView(boxAcceptSaisie, "ACCEPTE_EN_SAISIE");
956
            this.addView(boxAsaisir, "RAPPORT_A_SAISIR");
142 ilm 957
            this.addView(boxUserSaisi, "ID_USER_COMMON_SAISIE");
958
            this.addView(fieldTpsSaisi, "TEMPS_SAISIE");
132 ilm 959
        }
960
        return tabSite;
961
    }
962
 
73 ilm 963
    private JPanel createPanelDiff(final Type_Diff type) {
964
 
965
        GridBagConstraints cTabSite = new DefaultGridBagConstraints();
966
        JPanel tabSite = new JPanel(new GridBagLayout());
967
 
968
        cTabSite.weightx = 1;
969
        cTabSite.fill = GridBagConstraints.HORIZONTAL;
970
        cTabSite.gridwidth = 2;
971
 
972
        final String name = type.getName();
973
        final JCheckBox boxSiteDiff = new JCheckBox(getLabelFor(name + "_DIFF"));
974
 
975
        tabSite.add(boxSiteDiff, cTabSite);
976
        this.addView(boxSiteDiff, name + "_DIFF");
977
 
978
        final String fieldSiren = "SIREN_" + name;
979
        if (getTable().contains(fieldSiren)) {
980
            final JLabel labelSrenSite = new JLabel(getLabelFor(fieldSiren));
981
            labelSrenSite.setHorizontalAlignment(SwingConstants.RIGHT);
982
            cTabSite.gridwidth = 1;
983
            cTabSite.gridx = 2;
984
            cTabSite.weightx = 0;
985
            tabSite.add(labelSrenSite, cTabSite);
986
 
987
            cTabSite.gridx++;
988
            cTabSite.weightx = 1;
989
            if (type == Type_Diff.SITE) {
990
                throw new IllegalArgumentException("Le siren n'est pas à renseigné pour le site");
991
            }
992
            final JTextField siren = this.sirenDonneur;
993
            tabSite.add(siren, cTabSite);
994
            this.addView(siren, fieldSiren);
995
            DefaultGridBagConstraints.lockMinimumSize(siren);
996
        }
997
        cTabSite.gridy++;
998
        cTabSite.gridx = 0;
999
        cTabSite.weightx = 0;
1000
        cTabSite.fill = GridBagConstraints.HORIZONTAL;
1001
        cTabSite.gridwidth = 1;
1002
        final JLabel labelSiteDes = new JLabel(getLabelFor("DESIGNATION_" + name));
1003
 
1004
        labelSiteDes.setHorizontalAlignment(SwingConstants.RIGHT);
1005
 
1006
        tabSite.add(labelSiteDes, cTabSite);
1007
        cTabSite.gridx++;
1008
        cTabSite.weightx = 1;
1009
        final JTextField designation = type == Type_Diff.SITE ? this.desSite : this.desDonneur;
1010
        tabSite.add(designation, cTabSite);
1011
        this.addView(designation, "DESIGNATION_" + name);
1012
        DefaultGridBagConstraints.lockMinimumSize(designation);
1013
 
1014
        final JLabel labelTelSite = new JLabel(getLabelFor("TEL_" + name));
1015
        labelTelSite.setHorizontalAlignment(SwingConstants.RIGHT);
1016
        cTabSite.gridx++;
1017
        cTabSite.weightx = 0;
1018
        tabSite.add(labelTelSite, cTabSite);
1019
 
1020
        cTabSite.gridx++;
1021
        cTabSite.weightx = 1;
1022
        final JTextField tel = type == Type_Diff.SITE ? this.telSite : this.telDonneur;
1023
        tabSite.add(tel, cTabSite);
1024
        this.addView(tel, "TEL_" + name);
1025
        DefaultGridBagConstraints.lockMinimumSize(tel);
1026
 
1027
        final JLabel labelSiteAdr = new JLabel(getLabelFor("ADRESSE_" + name));
1028
        labelSiteAdr.setHorizontalAlignment(SwingConstants.RIGHT);
1029
        cTabSite.gridy++;
1030
        cTabSite.gridx = 0;
1031
        cTabSite.weightx = 0;
1032
        tabSite.add(labelSiteAdr, cTabSite);
1033
 
1034
        cTabSite.gridx++;
1035
        cTabSite.weightx = 1;
1036
        final ITextArea adresse = type == Type_Diff.SITE ? this.adrSite : this.adrDonneur;
1037
        tabSite.add(adresse, cTabSite);
1038
        this.addView(adresse, "ADRESSE_" + name);
1039
        DefaultGridBagConstraints.lockMinimumSize(adresse);
1040
 
1041
        final JLabel labelTelPSite = new JLabel(getLabelFor("TEL_P_" + name));
1042
        labelTelPSite.setHorizontalAlignment(SwingConstants.RIGHT);
1043
        cTabSite.gridx++;
1044
        cTabSite.weightx = 0;
1045
        tabSite.add(labelTelPSite, cTabSite);
1046
 
1047
        cTabSite.gridx++;
1048
        cTabSite.weightx = 1;
1049
        final JTextField telP = type == Type_Diff.SITE ? this.telPSite : this.telPDonneur;
1050
        tabSite.add(telP, cTabSite);
1051
        this.addView(telP, "TEL_P_" + name);
1052
 
1053
        cTabSite.gridy++;
1054
        cTabSite.gridx = 0;
1055
        cTabSite.weightx = 0;
1056
        final JLabel labelVilleAdr = new JLabel(getLabelFor("VILLE_" + name));
1057
        labelVilleAdr.setHorizontalAlignment(SwingConstants.RIGHT);
1058
        tabSite.add(labelVilleAdr, cTabSite);
1059
 
1060
        cTabSite.gridx++;
1061
        cTabSite.weightx = 1;
1062
        final ITextComboVilleViewer ville = type == Type_Diff.SITE ? this.villeSite : this.villeDonneur;
1063
        tabSite.add(ville, cTabSite);
1064
        this.addView(ville, "VILLE_" + name);
1065
        DefaultGridBagConstraints.lockMinimumSize(ville);
1066
 
1067
        cTabSite.gridx++;
1068
        cTabSite.weightx = 0;
1069
 
1070
        final JLabel labelFaxSite = new JLabel(getLabelFor("FAX_" + name));
1071
        labelFaxSite.setHorizontalAlignment(SwingConstants.RIGHT);
1072
        tabSite.add(labelFaxSite, cTabSite);
1073
 
1074
        cTabSite.gridx++;
1075
        cTabSite.weightx = 1;
1076
        final JTextField fax = type == Type_Diff.SITE ? this.faxSite : this.faxDonneur;
1077
        tabSite.add(fax, cTabSite);
1078
        this.addView(fax, "FAX_" + name);
1079
        DefaultGridBagConstraints.lockMinimumSize(fax);
1080
 
1081
        cTabSite.gridy++;
1082
        cTabSite.gridx = 0;
1083
        cTabSite.weightx = 0;
1084
 
1085
        final JLabel labelContactSite = new JLabel(getLabelFor("CONTACT_" + name));
1086
        labelContactSite.setHorizontalAlignment(SwingConstants.RIGHT);
1087
        tabSite.add(labelContactSite, cTabSite);
1088
 
1089
        cTabSite.gridx++;
1090
        cTabSite.weightx = 1;
1091
        final JTextField contact = type == Type_Diff.SITE ? this.contactSite : this.contactDonneur;
1092
        tabSite.add(contact, cTabSite);
1093
        this.addView(contact, "CONTACT_" + name);
1094
 
1095
        cTabSite.gridx++;
1096
        cTabSite.weightx = 0;
1097
 
1098
        final JLabel labelMailSite = new JLabel(getLabelFor("MAIL_" + name));
1099
        labelMailSite.setHorizontalAlignment(SwingConstants.RIGHT);
1100
        tabSite.add(labelMailSite, cTabSite);
1101
 
1102
        cTabSite.gridx++;
1103
        cTabSite.weightx = 1;
1104
        final JTextField mail = type == Type_Diff.SITE ? this.mailSite : this.mailDonneur;
1105
        tabSite.add(mail, cTabSite);
1106
        this.addView(mail, "MAIL_" + name);
1107
 
1108
        boxSiteDiff.addItemListener(new ItemListener() {
1109
 
1110
            @Override
1111
            public void itemStateChanged(ItemEvent e) {
1112
                final boolean selected = boxSiteDiff.isSelected();
1113
                setSiteEnabled(selected, type);
1114
                if (!selected) {
1115
                    clearFieldDiff(type);
1116
                }
1117
            }
1118
        });
1119
        return tabSite;
1120
    }
1121
 
1122
    private void clearFieldDiff(Type_Diff type) {
1123
        if (type == Type_Diff.SITE) {
1124
            this.desSite.setText("");
1125
            this.adrSite.setText("");
1126
            this.villeSite.setValue(null);
1127
            this.telPSite.setText("");
1128
            this.telSite.setText("");
1129
            this.mailSite.setText("");
1130
            this.contactSite.setText("");
1131
            this.faxSite.setText("");
1132
        } else {
1133
            this.sirenDonneur.setText("");
1134
            this.desDonneur.setText("");
1135
            this.adrDonneur.setText("");
1136
            this.villeDonneur.setValue(null);
1137
            this.telPDonneur.setText("");
1138
            this.telDonneur.setText("");
1139
            this.mailDonneur.setText("");
1140
            this.contactDonneur.setText("");
1141
            this.faxDonneur.setText("");
1142
        }
1143
    }
1144
 
1145
    private void setSiteEnabled(boolean b, Type_Diff type) {
1146
        if (type == Type_Diff.SITE) {
1147
            this.desSite.setEditable(b);
1148
            this.adrSite.setEditable(b);
1149
            this.villeSite.setEnabled(b);
1150
            this.telPSite.setEditable(b);
1151
            this.telSite.setEditable(b);
1152
            this.mailSite.setEditable(b);
1153
            this.contactSite.setEditable(b);
1154
            this.faxSite.setEditable(b);
1155
        } else {
90 ilm 1156
            b = false;
1157
 
73 ilm 1158
            this.sirenDonneur.setEditable(b);
1159
            this.desDonneur.setEditable(b);
1160
            this.adrDonneur.setEditable(b);
1161
            this.villeDonneur.setEnabled(b);
1162
            this.telPDonneur.setEditable(b);
1163
            this.telDonneur.setEditable(b);
1164
            this.mailDonneur.setEditable(b);
1165
            this.contactDonneur.setEditable(b);
1166
            this.faxDonneur.setEditable(b);
1167
        }
1168
    }
1169
 
18 ilm 1170
    @Override
1171
    protected SQLRowValues createDefaults() {
1172
        System.err.println("Create defaults");
73 ilm 1173
 
1174
        setSiteEnabled(false, Type_Diff.DONNEUR_ORDRE);
1175
        setSiteEnabled(false, Type_Diff.SITE);
1176
 
18 ilm 1177
        // Numero incremental auto
1178
        final SQLRowValues rowVals = new SQLRowValues(getTable());
90 ilm 1179
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 1180
 
1181
        // User
1182
        // final SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
1183
        final SQLElement eltComm = Configuration.getInstance().getDirectory().getElement("COMMERCIAL");
1184
        final int idUser = UserManager.getInstance().getCurrentUser().getId();
1185
        //
1186
        // sel.addSelect(eltComm.getTable().getKey());
1187
        // sel.setWhere(new Where(eltComm.getTable().getField("ID_USER_COMMON"), "=", idUser));
1188
        // final List<SQLRow> rowsComm = (List<SQLRow>)
1189
        // Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new
1190
        // SQLRowListRSH(eltComm.getTable()));
1191
 
1192
        SQLRow rowsComm = SQLBackgroundTableCache.getInstance().getCacheForTable(eltComm.getTable()).getFirstRowContains(idUser, eltComm.getTable().getField("ID_USER_COMMON"));
1193
 
1194
        if (rowsComm != null) {
1195
            rowVals.put("ID_COMMERCIAL", rowsComm.getID());
1196
        }
90 ilm 1197
 
41 ilm 1198
        if (getTable().getUndefinedID() == SQLRow.NONEXISTANT_ID) {
1199
            rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.EN_ATTENTE);
1200
        } else {
65 ilm 1201
            SQLRowValues foreign = UndefinedRowValuesCache.getInstance().getDefaultRowValues(getTable());
132 ilm 1202
            if (foreign != null && !foreign.isUndefined() && !foreign.isForeignEmpty("ID_ETAT_DEVIS")) {
1203
                rowVals.put("ID_ETAT_DEVIS", foreign.getForeignID("ID_ETAT_DEVIS"));
41 ilm 1204
            } else {
1205
                rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.EN_ATTENTE);
1206
            }
1207
        }
18 ilm 1208
        rowVals.put("T_HT", Long.valueOf(0));
1209
        rowVals.put("T_TVA", Long.valueOf(0));
1210
        rowVals.put("T_SERVICE", Long.valueOf(0));
1211
        rowVals.put("T_TTC", Long.valueOf(0));
144 ilm 1212
        if (getTable().contains("ID_TAXE_PORT")) {
1213
            SQLRow taxeDefault = TaxeCache.getCache().getFirstTaxe();
1214
            rowVals.put("ID_TAXE_PORT", taxeDefault.getID());
1215
        }
156 ilm 1216
        if (getTable().contains("ID_TAXE_FRAIS_DOCUMENT")) {
1217
            SQLRow taxeDefault = TaxeCache.getCache().getFirstTaxe();
1218
            rowVals.put("ID_TAXE_FRAIS_DOCUMENT", taxeDefault.getID());
1219
        }
19 ilm 1220
        if (getTable().getFieldsName().contains("DATE_VALIDITE")) {
1221
            Calendar cal = Calendar.getInstance();
1222
            cal.add(Calendar.MONTH, 1);
142 ilm 1223
            rowVals.put("DATE_VALIDITE", new java.sql.Date(cal.getTimeInMillis()));
19 ilm 1224
        }
18 ilm 1225
        return rowVals;
1226
    }
1227
 
1228
    private void calculPourcentage() {
1229
        final String remiseP = this.textPourcentRemise.getText().replace(',', '.');
73 ilm 1230
        Long totalHT = this.fieldHT.getValue();
1231
        Long remiseHT = this.textRemiseHT.getValue();
18 ilm 1232
 
1233
        totalHT = totalHT == null ? Long.valueOf(0) : totalHT;
1234
        remiseHT = remiseHT == null ? Long.valueOf(0) : remiseHT;
1235
 
1236
        try {
1237
            final int valueRemise = Integer.valueOf(remiseP);
1238
 
1239
            final long remise = valueRemise * (totalHT.longValue() + remiseHT.longValue()) / 100;
1240
            if (remiseHT != remise) {
73 ilm 1241
                this.textRemiseHT.setValue(remise);
18 ilm 1242
            }
1243
 
1244
        } catch (final NumberFormatException e) {
1245
            ExceptionHandler.handle("Erreur durant le calcul de la remise", e);
1246
        }
1247
 
1248
    }
1249
 
1250
    @Override
1251
    public int insert(final SQLRow order) {
1252
 
25 ilm 1253
        final int idDevis;
142 ilm 1254
        int attempt = 0;
18 ilm 1255
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
142 ilm 1256
        if (!this.numeroUniqueDevis.checkValidation(false)) {
1257
            while (attempt < JUniqueTextField.RETRY_COUNT) {
1258
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), dateDevis.getDate());
1259
                this.numeroUniqueDevis.setText(num);
1260
                attempt++;
1261
                if (this.numeroUniqueDevis.checkValidation(false)) {
1262
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
1263
                    break;
1264
                }
1265
                try {
1266
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
1267
                } catch (InterruptedException e) {
1268
                    e.printStackTrace();
1269
                }
1270
            }
1271
        }
1272
        final String num = this.numeroUniqueDevis.getText();
1273
        if (attempt == JUniqueTextField.RETRY_COUNT) {
1274
            idDevis = getSelectedID();
1275
            ExceptionHandler.handle("Impossible d'ajouter, numéro de devis existant.");
1276
            final Object root = SwingUtilities.getRoot(this);
1277
            if (root instanceof EditFrame) {
1278
                final EditFrame frame = (EditFrame) root;
1279
                frame.getPanel().setAlwaysVisible(true);
1280
            }
1281
        } else {
18 ilm 1282
 
1283
            idDevis = super.insert(order);
1284
            this.table.updateField("ID_DEVIS", idDevis);
1285
            // Création des articles
1286
            this.table.createArticle(idDevis, getElement());
1287
 
1288
            // generation du document
25 ilm 1289
            try {
1290
                final DevisXmlSheet sheet = new DevisXmlSheet(getTable().getRow(idDevis));
1291
                sheet.createDocumentAsynchronous();
1292
                sheet.showPrintAndExportAsynchronous(DevisSQLComponent.this.panelOO.isVisualisationSelected(), DevisSQLComponent.this.panelOO.isImpressionSelected(), true);
1293
            } catch (Exception e) {
1294
                ExceptionHandler.handle("Impossible de créer le devis", e);
1295
            }
18 ilm 1296
 
1297
            // incrémentation du numéro auto
90 ilm 1298
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass()).equalsIgnoreCase(this.numeroUniqueDevis.getText().trim())) {
18 ilm 1299
                final SQLRowValues rowVals = new SQLRowValues(this.tableNum);
90 ilm 1300
                int val = this.tableNum.getRow(2).getInt(NumerotationAutoSQLElement.getLabelNumberFor(getElement().getClass()));
18 ilm 1301
                val++;
90 ilm 1302
                rowVals.put(NumerotationAutoSQLElement.getLabelNumberFor(getElement().getClass()), new Integer(val));
18 ilm 1303
                try {
1304
                    rowVals.update(2);
1305
                } catch (final SQLException e) {
1306
                    e.printStackTrace();
1307
                }
1308
            }
142 ilm 1309
            if (attempt > 0) {
1310
                SwingUtilities.invokeLater(new Runnable() {
1311
                    public void run() {
1312
                        JOptionPane.showMessageDialog(null, "Le numéro a été actualisé en " + num);
1313
                    }
1314
                });
18 ilm 1315
            }
1316
        }
1317
 
1318
        return idDevis;
1319
    }
1320
 
1321
    @Override
1322
    public void select(final SQLRowAccessor r) {
41 ilm 1323
        if (r == null || r.getIDNumber() == null)
1324
            super.select(r);
1325
        else {
1326
            System.err.println(r);
132 ilm 1327
            final SQLRowValues rVals = r.asRowValues().deepCopy();
41 ilm 1328
            final SQLRowValues vals = new SQLRowValues(r.getTable());
1329
            vals.load(rVals, createSet("ID_CLIENT"));
1330
            vals.setID(rVals.getID());
1331
            System.err.println("Select CLIENT");
1332
 
1333
            super.select(vals);
1334
            rVals.remove("ID_CLIENT");
1335
            super.select(rVals);
1336
        }
1337
 
1338
        // super.select(r);
18 ilm 1339
        if (r != null) {
1340
            this.table.insertFrom("ID_DEVIS", r.getID());
41 ilm 1341
            // this.radioEtat.setVisible(r.getID() > getTable().getUndefinedID());
73 ilm 1342
            if (getTable().contains("SITE_DIFF"))
1343
                setSiteEnabled(r.getBoolean("SITE_DIFF"), Type_Diff.SITE);
1344
 
1345
            if (getTable().contains("DONNEUR_DIFF"))
1346
                setSiteEnabled(r.getBoolean("DONNEUR_DIFF"), Type_Diff.DONNEUR_ORDRE);
18 ilm 1347
        }
73 ilm 1348
 
18 ilm 1349
    }
1350
 
1351
    @Override
1352
    public void update() {
1353
 
1354
        if (!this.numeroUniqueDevis.checkValidation()) {
1355
            ExceptionHandler.handle("Impossible de modifier, numéro de devis existant.");
1356
            final Object root = SwingUtilities.getRoot(this);
1357
            if (root instanceof EditFrame) {
1358
                final EditFrame frame = (EditFrame) root;
1359
                frame.getPanel().setAlwaysVisible(true);
1360
            }
1361
            return;
1362
        }
1363
        super.update();
1364
        this.table.updateField("ID_DEVIS", getSelectedID());
1365
        this.table.createArticle(getSelectedID(), getElement());
1366
 
1367
        // generation du document
25 ilm 1368
 
1369
        try {
1370
            final DevisXmlSheet sheet = new DevisXmlSheet(getTable().getRow(getSelectedID()));
1371
            sheet.createDocumentAsynchronous();
1372
            sheet.showPrintAndExportAsynchronous(DevisSQLComponent.this.panelOO.isVisualisationSelected(), DevisSQLComponent.this.panelOO.isImpressionSelected(), true);
1373
        } catch (Exception e) {
1374
            ExceptionHandler.handle("Impossible de créer le devis", e);
1375
        }
1376
 
18 ilm 1377
    }
1378
 
1379
    /**
1380
     * Création d'un devis à partir d'un devis existant
1381
     *
1382
     * @param idDevis
1383
     *
1384
     */
1385
    public void loadDevisExistant(final int idDevis) {
1386
 
1387
        final SQLElement devis = Configuration.getInstance().getDirectory().getElement("DEVIS");
1388
        final SQLElement devisElt = Configuration.getInstance().getDirectory().getElement("DEVIS_ELEMENT");
1389
 
1390
        // On duplique le devis
1391
        if (idDevis > 1) {
1392
            final SQLRow row = devis.getTable().getRow(idDevis);
1393
            final SQLRowValues rowVals = new SQLRowValues(devis.getTable());
1394
            rowVals.put("ID_CLIENT", row.getInt("ID_CLIENT"));
156 ilm 1395
            if (row.getObject("ID_TARIF") != null && !row.isForeignEmpty("ID_TARIF")) {
1396
                rowVals.put("ID_TARIF", row.getInt("ID_TARIF"));
1397
            }
90 ilm 1398
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
73 ilm 1399
 
1400
 
18 ilm 1401
            this.select(rowVals);
1402
        }
1403
 
1404
        // On duplique les elements de devis
1405
        final List<SQLRow> myListItem = devis.getTable().getRow(idDevis).getReferentRows(devisElt.getTable());
1406
 
1407
        if (myListItem.size() != 0) {
1408
            this.table.getModel().clearRows();
1409
 
1410
            for (final SQLRow rowElt : myListItem) {
1411
 
1412
                final SQLRowValues rowVals = rowElt.createUpdateRow();
1413
                rowVals.clearPrimaryKeys();
1414
                this.table.getModel().addRow(rowVals);
1415
                final int rowIndex = this.table.getModel().getRowCount() - 1;
1416
                this.table.getModel().fireTableModelModified(rowIndex);
1417
            }
1418
        } else {
1419
            this.table.getModel().clearRows();
1420
        }
1421
        this.table.getModel().fireTableDataChanged();
1422
        this.table.repaint();
1423
    }
93 ilm 1424
 
18 ilm 1425
}