OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.quote.component;
15
 
41 ilm 16
import static org.openconcerto.utils.CollectionUtils.createSet;
18 ilm 17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
19
import org.openconcerto.erp.core.common.ui.DeviseField;
20
import org.openconcerto.erp.core.common.ui.TotalPanel;
21
import org.openconcerto.erp.core.sales.quote.element.DevisSQLElement;
22
import org.openconcerto.erp.core.sales.quote.element.EtatDevisSQLElement;
23
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
24
import org.openconcerto.erp.core.sales.quote.ui.DevisItemTable;
21 ilm 25
import org.openconcerto.erp.panel.PanelOOSQLComponent;
18 ilm 26
import org.openconcerto.sql.Configuration;
27
import org.openconcerto.sql.element.BaseSQLComponent;
28
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.model.SQLBackgroundTableCache;
30
import org.openconcerto.sql.model.SQLRow;
31
import org.openconcerto.sql.model.SQLRowAccessor;
32
import org.openconcerto.sql.model.SQLRowValues;
33
import org.openconcerto.sql.model.SQLTable;
34
import org.openconcerto.sql.sqlobject.ElementComboBox;
35
import org.openconcerto.sql.sqlobject.JUniqueTextField;
36
import org.openconcerto.sql.sqlobject.SQLTextCombo;
37
import org.openconcerto.sql.ui.RadioButtons;
38
import org.openconcerto.sql.users.UserManager;
39
import org.openconcerto.sql.view.EditFrame;
40
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 41
import org.openconcerto.ui.FormLayouter;
18 ilm 42
import org.openconcerto.ui.JDate;
43
import org.openconcerto.ui.TitledSeparator;
44
import org.openconcerto.ui.VFlowLayout;
45
import org.openconcerto.ui.component.ITextArea;
46
import org.openconcerto.utils.ExceptionHandler;
47
import org.openconcerto.utils.GestionDevise;
48
import org.openconcerto.utils.text.SimpleDocumentListener;
49
 
50
import java.awt.Color;
51
import java.awt.Dimension;
52
import java.awt.GridBagConstraints;
53
import java.awt.GridBagLayout;
54
import java.awt.Insets;
19 ilm 55
import java.beans.PropertyChangeEvent;
56
import java.beans.PropertyChangeListener;
18 ilm 57
import java.sql.SQLException;
19 ilm 58
import java.util.Calendar;
18 ilm 59
import java.util.List;
60
 
61
import javax.swing.BorderFactory;
62
import javax.swing.ButtonGroup;
63
import javax.swing.JLabel;
19 ilm 64
import javax.swing.JOptionPane;
18 ilm 65
import javax.swing.JPanel;
66
import javax.swing.JRadioButton;
67
import javax.swing.JScrollPane;
68
import javax.swing.JTextField;
69
import javax.swing.SwingConstants;
70
import javax.swing.SwingUtilities;
71
import javax.swing.event.ChangeEvent;
72
import javax.swing.event.ChangeListener;
73
import javax.swing.event.DocumentEvent;
74
import javax.swing.event.TableModelEvent;
75
import javax.swing.event.TableModelListener;
76
 
77
public class DevisSQLComponent extends BaseSQLComponent {
78
    private DevisItemTable table;
79
    private JUniqueTextField numeroUniqueDevis;
80
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
81
    private final ITextArea infos = new ITextArea();
82
    private final RadioButtons radioEtat = new RadioButtons("NOM");
83
    private JTextField textPourcentRemise, textPoidsTotal;
84
    private DeviseField textRemiseHT;
85
    private DeviseField fieldHT;
21 ilm 86
    private PanelOOSQLComponent panelOO;
18 ilm 87
 
88
    public DevisSQLComponent(final SQLElement elt) {
89
        super(elt);
90
    }
91
 
61 ilm 92
    public DevisItemTable getRowValuesTable() {
93
        return this.table;
94
    }
95
 
18 ilm 96
    @Override
97
    public void addViews() {
98
        setLayout(new GridBagLayout());
99
        final GridBagConstraints c = new DefaultGridBagConstraints();
100
 
25 ilm 101
        // Champ Module
102
        c.gridx = 0;
103
        c.gridy++;
104
        c.gridwidth = GridBagConstraints.REMAINDER;
105
        final JPanel addP = new JPanel();
41 ilm 106
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
25 ilm 107
        this.add(addP, c);
108
 
109
        c.gridy++;
110
        c.gridwidth = 1;
18 ilm 111
        final JLabel labelNumero = new JLabel(getLabelFor("NUMERO"));
112
        labelNumero.setHorizontalAlignment(SwingConstants.RIGHT);
113
        this.add(labelNumero, c);
114
 
115
        // Ligne 1: Numero du devis
116
 
117
        c.gridx++;
118
        c.weightx = 1;
119
        c.fill = GridBagConstraints.NONE;
120
        this.numeroUniqueDevis = new JUniqueTextField(15);
121
        DefaultGridBagConstraints.lockMinimumSize(this.numeroUniqueDevis);
122
        DefaultGridBagConstraints.lockMaximumSize(this.numeroUniqueDevis);
123
        this.add(this.numeroUniqueDevis, c);
124
 
125
        // Date
126
 
127
        c.gridx++;
128
        c.weightx = 0;
129
        c.fill = GridBagConstraints.HORIZONTAL;
130
        final JLabel labelDate = new JLabel(getLabelFor("DATE"));
131
 
132
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
133
 
134
        this.add(labelDate, c);
135
        c.gridx++;
136
 
137
        c.weightx = 1;
138
 
139
        c.fill = GridBagConstraints.NONE;
140
        final JDate dateDevis = new JDate(true);
141
        this.add(dateDevis, c);
142
 
143
        // Etat devis
144
        this.radioEtat.setLayout(new VFlowLayout());
145
        this.radioEtat.setBorder(BorderFactory.createTitledBorder(getLabelFor("ID_ETAT_DEVIS")));
146
        c.gridwidth = GridBagConstraints.REMAINDER;
61 ilm 147
        c.gridheight = 5;
18 ilm 148
        c.weightx = 0;
149
        c.fill = GridBagConstraints.NONE;
150
        c.anchor = GridBagConstraints.EAST;
151
        c.gridx += 2;
152
        this.add(this.radioEtat, c);
41 ilm 153
        // this.radioEtat.setVisible(false);
18 ilm 154
 
155
        // Ligne 2: Reference
156
        c.gridx = 0;
157
        c.gridy++;
158
        c.gridwidth = 1;
159
        c.gridheight = 1;
160
        c.fill = GridBagConstraints.HORIZONTAL;
161
 
162
        c.weightx = 0;
163
 
164
        final JLabel labelObjet = new JLabel(getLabelFor("OBJET"));
165
        labelObjet.setHorizontalAlignment(SwingConstants.RIGHT);
166
 
167
        this.add(labelObjet, c);
168
 
169
        final SQLTextCombo textObjet = new SQLTextCombo();
170
        c.gridx++;
171
        c.weightx = 1;
172
        c.gridwidth = 1;
173
        c.fill = GridBagConstraints.BOTH;
174
        this.add(textObjet, c);
175
 
176
        String field;
177
            field = "ID_COMMERCIAL";
178
        // Commercial
179
        final JLabel labelCommercial = new JLabel(getLabelFor(field));
180
        labelCommercial.setHorizontalAlignment(SwingConstants.RIGHT);
181
        c.gridx++;
182
        c.fill = GridBagConstraints.HORIZONTAL;
183
        c.weightx = 0;
184
 
185
        this.add(labelCommercial, c);
186
 
187
        final ElementComboBox comboCommercial = new ElementComboBox(false, 25);
188
 
189
        comboCommercial.setListIconVisible(false);
190
        c.gridx++;
191
        c.weightx = 1;
192
        c.fill = GridBagConstraints.NONE;
193
        c.anchor = GridBagConstraints.WEST;
194
 
195
        this.add(comboCommercial, c);
196
        addRequiredSQLObject(comboCommercial, field);
197
 
198
        // Ligne 3: Client
199
        c.gridx = 0;
200
        c.gridy++;
201
        c.gridwidth = 1;
202
        final JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"));
203
        labelClient.setHorizontalAlignment(SwingConstants.RIGHT);
204
        c.weightx = 0;
205
        c.gridwidth = 1;
206
        c.fill = GridBagConstraints.HORIZONTAL;
207
        this.add(labelClient, c);
208
 
209
        final ElementComboBox comboClient = new ElementComboBox();
210
        c.gridx++;
19 ilm 211
        c.gridwidth = 1;
18 ilm 212
        c.weightx = 0;
213
        c.weighty = 0;
214
        c.fill = GridBagConstraints.NONE;
215
        this.add(comboClient, c);
19 ilm 216
        final ElementComboBox boxTarif = new ElementComboBox();
217
        comboClient.addValueListener(new PropertyChangeListener() {
18 ilm 218
 
19 ilm 219
            @Override
41 ilm 220
            public void propertyChange(PropertyChangeEvent arg0) {
19 ilm 221
                if (comboClient.getValue() != null) {
222
                    Integer id = comboClient.getValue();
223
 
224
                    if (id > 1) {
225
 
226
                        SQLRow row = comboClient.getElement().getTable().getRow(id);
227
                        if (comboClient.getElement().getTable().getFieldsName().contains("ID_TARIF")) {
228
 
229
                            SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
230
                            if (!foreignRow.isUndefined() && (boxTarif.getSelectedRow() == null || boxTarif.getSelectedId() != foreignRow.getID())
231
                                    && JOptionPane.showConfirmDialog(null, "Appliquer les tarifs associés au client?") == JOptionPane.YES_OPTION) {
232
                                boxTarif.setValue(foreignRow.getID());
233
                                // SaisieVenteFactureSQLComponent.this.tableFacture.setTarif(foreignRow,
234
                                // true);
235
                            } else {
236
                                boxTarif.setValue(foreignRow.getID());
237
                            }
238
 
239
                            // SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
240
                            // if (foreignRow.isUndefined() &&
241
                            // !row.getForeignRow("ID_DEVISE").isUndefined()) {
242
                            // SQLRowValues rowValsD = new SQLRowValues(foreignRow.getTable());
243
                            // rowValsD.put("ID_DEVISE", row.getObject("ID_DEVISE"));
244
                            // foreignRow = rowValsD;
245
                            //
246
                            // }
247
                            // table.setTarif(foreignRow, true);
248
                        }
249
                    }
250
                }
251
 
252
            }
253
        });
254
 
255
        if (getTable().getFieldsName().contains("DATE_VALIDITE")) {
256
            c.gridx++;
257
            this.add(new JLabel(getLabelFor("DATE_VALIDITE")), c);
258
            c.gridx++;
259
            JDate dateValidite = new JDate();
260
            this.add(dateValidite, c);
261
            this.addView(dateValidite, "DATE_VALIDITE");
262
        }
263
 
264
        // Tarif
265
        if (this.getTable().getFieldsName().contains("ID_TARIF")) {
266
            // TARIF
267
            c.gridy++;
268
            c.gridx = 0;
269
            c.weightx = 0;
270
            c.weighty = 0;
271
            c.gridwidth = 1;
61 ilm 272
            JLabel comp = new JLabel("Tarif à appliquer", SwingConstants.RIGHT);
273
            this.add(comp, c);
19 ilm 274
            c.gridx++;
275
            c.gridwidth = GridBagConstraints.REMAINDER;
276
 
277
            c.weightx = 1;
278
            this.add(boxTarif, c);
279
            this.addView(boxTarif, "ID_TARIF");
280
            DefaultGridBagConstraints.lockMinimumSize(boxTarif);
281
            boxTarif.addValueListener(new PropertyChangeListener() {
282
 
283
                @Override
284
                public void propertyChange(PropertyChangeEvent evt) {
285
                    table.setTarif(boxTarif.getSelectedRow(), false);
286
                }
287
            });
288
        }
289
 
18 ilm 290
        // Table d'élément
291
        this.table = new DevisItemTable();
292
        c.fill = GridBagConstraints.BOTH;
293
        c.gridx = 0;
61 ilm 294
        c.gridy += 2;
18 ilm 295
        c.weighty = 1;
296
        c.gridwidth = GridBagConstraints.REMAINDER;
297
        this.add(this.table, c);
298
 
299
        // Panel en bas
300
        final JPanel bottomPanel = new JPanel(new GridBagLayout());
301
        final GridBagConstraints cBottom = new DefaultGridBagConstraints();
302
        // 1ere Colonne : Infos
303
        cBottom.weightx = 1;
304
        bottomPanel.add(new TitledSeparator(getLabelFor("INFOS")), cBottom);
305
        cBottom.gridy++;
306
        cBottom.fill = GridBagConstraints.BOTH;
307
        cBottom.weighty = 0;
308
 
309
        final JScrollPane scrollPane = new JScrollPane(this.infos);
310
        scrollPane.setBorder(null);
311
        bottomPanel.add(scrollPane, cBottom);
312
 
313
        // 2eme Colonne : Poids total
314
        final JPanel panel = new JPanel(new GridBagLayout());
315
 
316
        this.textPoidsTotal = new JTextField(8);
317
        this.textPoidsTotal.setText("0.0");
318
        final GridBagConstraints cPanel = new DefaultGridBagConstraints();
319
        panel.add(new JLabel(getLabelFor("T_POIDS")), cPanel);
320
        cPanel.weightx = 0;
321
        cPanel.gridx++;
322
        panel.add(this.textPoidsTotal, cPanel);
323
        this.textPoidsTotal.setEnabled(false);
324
        this.textPoidsTotal.setEditable(false);
325
        this.textPoidsTotal.setHorizontalAlignment(SwingConstants.RIGHT);
326
        this.textPoidsTotal.setDisabledTextColor(Color.BLACK);
327
 
328
        // Port
329
        cPanel.gridy++;
330
        cPanel.gridx = 0;
331
        panel.add(new JLabel(getLabelFor("PORT_HT"), SwingConstants.RIGHT), cPanel);
332
 
333
        cPanel.gridx++;
334
        final DeviseField textPortHT = new DeviseField();
335
        panel.add(textPortHT, cPanel);
336
 
337
        // Remise HT
338
        final JRadioButton radioEuros = new JRadioButton("en €");
339
        final JRadioButton radioPourCent = new JRadioButton("en %");
340
        final ButtonGroup radioGroup = new ButtonGroup();
341
        radioGroup.add(radioEuros);
342
        radioGroup.add(radioPourCent);
343
        radioEuros.setSelected(true);
344
 
345
        final JPanel panelRemise = new JPanel(new GridBagLayout());
346
        final GridBagConstraints cRemise = new DefaultGridBagConstraints();
347
        cRemise.insets = new Insets(0, 0, 1, 0);
348
        final JLabel labelRemise = new JLabel(getLabelFor("REMISE_HT"));
349
        panelRemise.add(labelRemise, cRemise);
350
        cRemise.gridx++;
351
        panelRemise.add(radioEuros, cRemise);
352
 
353
        cRemise.gridx++;
354
        cRemise.weightx = 0;
355
        this.textRemiseHT = new DeviseField();
356
        panelRemise.add(this.textRemiseHT, cRemise);
357
        this.textRemiseHT.setMinimumSize(new Dimension(150, 20));
358
        this.textRemiseHT.setPreferredSize(new Dimension(150, 20));
359
 
360
        cRemise.gridx = 1;
361
        cRemise.gridy++;
362
        cRemise.weightx = 0;
363
        panelRemise.add(radioPourCent, cRemise);
364
 
365
        this.textPourcentRemise = new JTextField(5);
366
        DefaultGridBagConstraints.lockMinimumSize(textPourcentRemise);
367
        cRemise.gridx++;
368
        panelRemise.add(this.textPourcentRemise, cRemise);
369
 
370
        cPanel.gridx = 0;
371
        cPanel.gridy++;
372
        cPanel.gridwidth = 2;
373
        panel.add(panelRemise, cPanel);
374
 
375
        cBottom.gridy = 0;
376
        cBottom.gridx++;
377
        cBottom.weighty = 0;
378
        cBottom.weightx = 1;
379
        cBottom.gridheight = 2;
380
        cBottom.fill = GridBagConstraints.HORIZONTAL;
381
        cBottom.anchor = GridBagConstraints.NORTHEAST;
382
        DefaultGridBagConstraints.lockMinimumSize(panel);
383
        // bottomPanel.add(panel, cBottom);
384
 
385
        addSQLObject(this.textRemiseHT, "REMISE_HT");
386
        addSQLObject(textPortHT, "PORT_HT");
387
        // this.checkImpression.setSelected(true);
388
 
389
        // Total
390
        this.fieldHT = new DeviseField();
391
        final DeviseField fieldTVA = new DeviseField();
392
        final DeviseField fieldTTC = new DeviseField();
19 ilm 393
        final DeviseField fieldDevise = new DeviseField();
18 ilm 394
        final DeviseField fieldService = new DeviseField();
395
        fieldHT.setEditable(false);
396
        fieldTVA.setEditable(false);
397
        fieldTTC.setEditable(false);
398
        fieldService.setEditable(false);
399
 
400
        addRequiredSQLObject(this.fieldHT, "T_HT");
401
        addRequiredSQLObject(fieldTVA, "T_TVA");
19 ilm 402
        addSQLObject(fieldDevise, "T_DEVISE");
18 ilm 403
        addRequiredSQLObject(fieldTTC, "T_TTC");
404
        addRequiredSQLObject(fieldService, "T_SERVICE");
19 ilm 405
        JTextField poids = new JTextField();
406
        // addSQLObject(poids, "T_POIDS");
41 ilm 407
 
408
        // FIXME Field add field T_HA dans installation avec recalcul des devis deja saisis
409
        final DeviseField fieldHA = new DeviseField();
410
 
61 ilm 411
        if (getTable().contains("PREBILAN")) {
412
            addSQLObject(fieldHA, "PREBILAN");
413
        } else if (getTable().contains("T_HA")) {
414
 
41 ilm 415
            addSQLObject(fieldHA, "T_HA");
416
        }
417
 
61 ilm 418
        final TotalPanel totalTTC = new TotalPanel(this.table, this.fieldHT, fieldTVA, fieldTTC, textPortHT, this.textRemiseHT, fieldService, fieldHA, fieldDevise, poids, null);
18 ilm 419
 
420
        cBottom.gridy = 0;
421
        cBottom.gridx += 2;
422
        cBottom.gridheight = 2;
423
        cBottom.gridwidth = 1;
424
        cBottom.fill = GridBagConstraints.NONE;
425
        cBottom.weightx = 0;
426
        cBottom.anchor = GridBagConstraints.NORTHEAST;
427
        DefaultGridBagConstraints.lockMinimumSize(totalTTC);
428
        bottomPanel.add(totalTTC, cBottom);
429
 
430
        c.gridy++;
431
        c.gridx = 0;
432
        c.weighty = 0;
433
        c.fill = GridBagConstraints.HORIZONTAL;
434
        c.gridwidth = GridBagConstraints.REMAINDER;
435
        this.add(bottomPanel, c);
436
 
437
        c.gridx = 0;
438
        c.gridy++;
439
        c.weighty = 0;
440
        c.fill = GridBagConstraints.NONE;
441
        c.anchor = GridBagConstraints.SOUTHEAST;
442
 
21 ilm 443
        this.panelOO = new PanelOOSQLComponent(this);
444
        this.add(this.panelOO, c);
445
 
18 ilm 446
        textPortHT.getDocument().addDocumentListener(new SimpleDocumentListener() {
447
 
448
            @Override
449
            public void update(final DocumentEvent e) {
450
                totalTTC.updateTotal();
451
            }
452
        });
453
 
454
        this.textRemiseHT.getDocument().addDocumentListener(new SimpleDocumentListener() {
455
 
456
            @Override
457
            public void update(final DocumentEvent e) {
458
                totalTTC.updateTotal();
459
            }
460
        });
461
 
462
        this.textPourcentRemise.getDocument().addDocumentListener(new SimpleDocumentListener() {
463
 
464
            @Override
465
            public void update(final DocumentEvent e) {
466
                calculPourcentage();
467
            }
468
        });
469
 
470
        radioEuros.addChangeListener(new ChangeListener() {
471
 
472
            public void stateChanged(final ChangeEvent e) {
473
                DevisSQLComponent.this.textRemiseHT.setEnabled(radioEuros.isSelected());
474
                DevisSQLComponent.this.textPourcentRemise.setEnabled(!radioEuros.isSelected());
475
            }
476
        });
477
 
478
        this.table.getModel().addTableModelListener(new TableModelListener() {
479
 
480
            public void tableChanged(final TableModelEvent e) {
481
                DevisSQLComponent.this.textPoidsTotal.setText(String.valueOf(DevisSQLComponent.this.table.getPoidsTotal()));
482
            }
483
        });
484
 
485
        addRequiredSQLObject(comboClient, "ID_CLIENT");
486
        addSQLObject(textObjet, "OBJET");
487
        addSQLObject(this.textPoidsTotal, "T_POIDS");
488
        addRequiredSQLObject(dateDevis, "DATE");
489
        addRequiredSQLObject(this.radioEtat, "ID_ETAT_DEVIS");
490
        addRequiredSQLObject(this.numeroUniqueDevis, "NUMERO");
491
        addSQLObject(this.infos, "INFOS");
492
 
493
        DefaultGridBagConstraints.lockMinimumSize(comboCommercial);
494
        DefaultGridBagConstraints.lockMinimumSize(comboClient);
495
    }
496
 
497
    @Override
498
    protected SQLRowValues createDefaults() {
499
        System.err.println("Create defaults");
500
        // Numero incremental auto
501
        final SQLRowValues rowVals = new SQLRowValues(getTable());
502
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(DevisSQLElement.class));
503
 
504
        // User
505
        // final SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
506
        final SQLElement eltComm = Configuration.getInstance().getDirectory().getElement("COMMERCIAL");
507
        final int idUser = UserManager.getInstance().getCurrentUser().getId();
508
        //
509
        // sel.addSelect(eltComm.getTable().getKey());
510
        // sel.setWhere(new Where(eltComm.getTable().getField("ID_USER_COMMON"), "=", idUser));
511
        // final List<SQLRow> rowsComm = (List<SQLRow>)
512
        // Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new
513
        // SQLRowListRSH(eltComm.getTable()));
514
 
515
        SQLRow rowsComm = SQLBackgroundTableCache.getInstance().getCacheForTable(eltComm.getTable()).getFirstRowContains(idUser, eltComm.getTable().getField("ID_USER_COMMON"));
516
 
517
        if (rowsComm != null) {
518
            rowVals.put("ID_COMMERCIAL", rowsComm.getID());
519
        }
41 ilm 520
        if (getTable().getUndefinedID() == SQLRow.NONEXISTANT_ID) {
521
            rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.EN_ATTENTE);
522
        } else {
523
            SQLRow rowUndef = getTable().getRow(getTable().getUndefinedID());
524
            SQLRow foreign = rowUndef.getForeign("ID_ETAT_DEVIS");
525
            if (foreign != null && !foreign.isUndefined()) {
526
                rowVals.put("ID_ETAT_DEVIS", foreign.getID());
527
            } else {
528
                rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.EN_ATTENTE);
529
            }
530
        }
18 ilm 531
        rowVals.put("T_HT", Long.valueOf(0));
532
        rowVals.put("T_TVA", Long.valueOf(0));
533
        rowVals.put("T_SERVICE", Long.valueOf(0));
534
        rowVals.put("T_TTC", Long.valueOf(0));
19 ilm 535
 
536
        if (getTable().getFieldsName().contains("DATE_VALIDITE")) {
537
            Calendar cal = Calendar.getInstance();
538
            cal.add(Calendar.MONTH, 1);
539
            rowVals.put("DATE_VALIDITE", cal.getTime());
540
        }
18 ilm 541
        return rowVals;
542
    }
543
 
544
    private void calculPourcentage() {
545
        final String remiseP = this.textPourcentRemise.getText().replace(',', '.');
546
        Long totalHT = (Long) this.fieldHT.getUncheckedValue();
547
        Long remiseHT = (Long) this.textRemiseHT.getUncheckedValue();
548
 
549
        totalHT = totalHT == null ? Long.valueOf(0) : totalHT;
550
        remiseHT = remiseHT == null ? Long.valueOf(0) : remiseHT;
551
 
552
        try {
553
            final int valueRemise = Integer.valueOf(remiseP);
554
 
555
            final long remise = valueRemise * (totalHT.longValue() + remiseHT.longValue()) / 100;
556
            if (remiseHT != remise) {
557
                this.textRemiseHT.setValue(GestionDevise.currencyToString(remise));
558
            }
559
 
560
        } catch (final NumberFormatException e) {
561
            ExceptionHandler.handle("Erreur durant le calcul de la remise", e);
562
        }
563
 
564
    }
565
 
566
    @Override
567
    public int insert(final SQLRow order) {
568
 
25 ilm 569
        final int idDevis;
18 ilm 570
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
571
        if (this.numeroUniqueDevis.checkValidation()) {
572
 
573
            idDevis = super.insert(order);
574
            this.table.updateField("ID_DEVIS", idDevis);
575
            // Création des articles
576
            this.table.createArticle(idDevis, getElement());
577
 
578
            // generation du document
25 ilm 579
            try {
580
                final DevisXmlSheet sheet = new DevisXmlSheet(getTable().getRow(idDevis));
581
                sheet.createDocumentAsynchronous();
582
                sheet.showPrintAndExportAsynchronous(DevisSQLComponent.this.panelOO.isVisualisationSelected(), DevisSQLComponent.this.panelOO.isImpressionSelected(), true);
583
            } catch (Exception e) {
584
                ExceptionHandler.handle("Impossible de créer le devis", e);
585
            }
18 ilm 586
 
587
            // incrémentation du numéro auto
588
            if (NumerotationAutoSQLElement.getNextNumero(DevisSQLElement.class).equalsIgnoreCase(this.numeroUniqueDevis.getText().trim())) {
589
                final SQLRowValues rowVals = new SQLRowValues(this.tableNum);
41 ilm 590
                int val = this.tableNum.getRow(2).getInt(NumerotationAutoSQLElement.getLabelNumberFor(DevisSQLElement.class));
18 ilm 591
                val++;
41 ilm 592
                rowVals.put(NumerotationAutoSQLElement.getLabelNumberFor(DevisSQLElement.class), new Integer(val));
18 ilm 593
                try {
594
                    rowVals.update(2);
595
                } catch (final SQLException e) {
596
                    e.printStackTrace();
597
                }
598
            }
599
        } else {
25 ilm 600
            idDevis = getSelectedID();
18 ilm 601
            ExceptionHandler.handle("Impossible d'ajouter, numéro de devis existant.");
602
            final Object root = SwingUtilities.getRoot(this);
603
            if (root instanceof EditFrame) {
604
                final EditFrame frame = (EditFrame) root;
605
                frame.getPanel().setAlwaysVisible(true);
606
            }
607
        }
608
 
609
        return idDevis;
610
    }
611
 
612
    @Override
613
    public void select(final SQLRowAccessor r) {
614
        if (r != null) {
615
            this.numeroUniqueDevis.setIdSelected(r.getID());
616
        }
41 ilm 617
 
618
        if (r == null || r.getIDNumber() == null)
619
            super.select(r);
620
        else {
621
            System.err.println(r);
622
            final SQLRowValues rVals = r.asRowValues();
623
            final SQLRowValues vals = new SQLRowValues(r.getTable());
624
            vals.load(rVals, createSet("ID_CLIENT"));
625
            vals.setID(rVals.getID());
626
            System.err.println("Select CLIENT");
627
 
628
            super.select(vals);
629
            rVals.remove("ID_CLIENT");
630
            super.select(rVals);
631
        }
632
 
633
        // super.select(r);
18 ilm 634
        if (r != null) {
635
            this.table.insertFrom("ID_DEVIS", r.getID());
41 ilm 636
            // this.radioEtat.setVisible(r.getID() > getTable().getUndefinedID());
18 ilm 637
        }
638
    }
639
 
640
    @Override
641
    public void update() {
642
 
643
        if (!this.numeroUniqueDevis.checkValidation()) {
644
            ExceptionHandler.handle("Impossible de modifier, numéro de devis existant.");
645
            final Object root = SwingUtilities.getRoot(this);
646
            if (root instanceof EditFrame) {
647
                final EditFrame frame = (EditFrame) root;
648
                frame.getPanel().setAlwaysVisible(true);
649
            }
650
            return;
651
        }
652
        super.update();
653
        this.table.updateField("ID_DEVIS", getSelectedID());
654
        this.table.createArticle(getSelectedID(), getElement());
655
 
656
        // generation du document
25 ilm 657
 
658
        try {
659
            final DevisXmlSheet sheet = new DevisXmlSheet(getTable().getRow(getSelectedID()));
660
            sheet.createDocumentAsynchronous();
661
            sheet.showPrintAndExportAsynchronous(DevisSQLComponent.this.panelOO.isVisualisationSelected(), DevisSQLComponent.this.panelOO.isImpressionSelected(), true);
662
        } catch (Exception e) {
663
            ExceptionHandler.handle("Impossible de créer le devis", e);
664
        }
665
 
18 ilm 666
    }
667
 
668
    /**
669
     * Création d'un devis à partir d'un devis existant
670
     *
671
     * @param idDevis
672
     *
673
     */
674
    public void loadDevisExistant(final int idDevis) {
675
 
676
        final SQLElement devis = Configuration.getInstance().getDirectory().getElement("DEVIS");
677
        final SQLElement devisElt = Configuration.getInstance().getDirectory().getElement("DEVIS_ELEMENT");
678
 
679
        // On duplique le devis
680
        if (idDevis > 1) {
681
            final SQLRow row = devis.getTable().getRow(idDevis);
682
            final SQLRowValues rowVals = new SQLRowValues(devis.getTable());
683
            rowVals.put("ID_CLIENT", row.getInt("ID_CLIENT"));
684
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(DevisSQLElement.class));
685
            this.select(rowVals);
686
        }
687
 
688
        // On duplique les elements de devis
689
        final List<SQLRow> myListItem = devis.getTable().getRow(idDevis).getReferentRows(devisElt.getTable());
690
 
691
        if (myListItem.size() != 0) {
692
            this.table.getModel().clearRows();
693
 
694
            for (final SQLRow rowElt : myListItem) {
695
 
696
                final SQLRowValues rowVals = rowElt.createUpdateRow();
697
                rowVals.clearPrimaryKeys();
698
                this.table.getModel().addRow(rowVals);
699
                final int rowIndex = this.table.getModel().getRowCount() - 1;
700
                this.table.getModel().fireTableModelModified(rowIndex);
701
            }
702
        } else {
703
            this.table.getModel().clearRows();
704
        }
705
        this.table.getModel().fireTableDataChanged();
706
        this.table.repaint();
707
    }
708
}