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.supplychain.order.component;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
94 ilm 17
import org.openconcerto.erp.core.common.component.AdresseSQLComponent;
18 ilm 18
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
63 ilm 19
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18 ilm 20
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
28 ilm 21
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
18 ilm 22
import org.openconcerto.erp.core.common.ui.DeviseField;
23
import org.openconcerto.erp.core.common.ui.TotalPanel;
65 ilm 24
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
94 ilm 25
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
149 ilm 26
import org.openconcerto.erp.core.supplychain.order.element.DemandeAchatItemSQLElement;
18 ilm 27
import org.openconcerto.erp.core.supplychain.order.ui.CommandeItemTable;
83 ilm 28
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
93 ilm 29
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
132 ilm 30
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
94 ilm 31
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
32
import org.openconcerto.erp.panel.PanelOOSQLComponent;
18 ilm 33
import org.openconcerto.erp.preferences.DefaultNXProps;
174 ilm 34
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
93 ilm 35
import org.openconcerto.erp.utils.TM;
18 ilm 36
import org.openconcerto.sql.Configuration;
28 ilm 37
import org.openconcerto.sql.element.DefaultElementSQLObject;
94 ilm 38
import org.openconcerto.sql.element.ElementSQLObject;
18 ilm 39
import org.openconcerto.sql.element.SQLElement;
40
import org.openconcerto.sql.model.SQLBackgroundTableCache;
19 ilm 41
import org.openconcerto.sql.model.SQLInjector;
18 ilm 42
import org.openconcerto.sql.model.SQLRow;
43
import org.openconcerto.sql.model.SQLRowAccessor;
44
import org.openconcerto.sql.model.SQLRowValues;
45
import org.openconcerto.sql.model.SQLTable;
57 ilm 46
import org.openconcerto.sql.model.Where;
156 ilm 47
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 48
import org.openconcerto.sql.sqlobject.ElementComboBox;
49
import org.openconcerto.sql.sqlobject.JUniqueTextField;
61 ilm 50
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
174 ilm 51
import org.openconcerto.sql.sqlobject.SQLTextCombo;
18 ilm 52
import org.openconcerto.sql.users.UserManager;
53
import org.openconcerto.sql.view.EditFrame;
73 ilm 54
import org.openconcerto.sql.view.list.RowValuesTable;
174 ilm 55
import org.openconcerto.sql.view.list.RowValuesTableModel;
65 ilm 56
import org.openconcerto.ui.AutoHideListener;
18 ilm 57
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 58
import org.openconcerto.ui.FormLayouter;
18 ilm 59
import org.openconcerto.ui.JDate;
60
import org.openconcerto.ui.TitledSeparator;
94 ilm 61
import org.openconcerto.ui.component.ComboLockedMode;
18 ilm 62
import org.openconcerto.ui.component.ITextArea;
94 ilm 63
import org.openconcerto.ui.component.ITextCombo;
132 ilm 64
import org.openconcerto.ui.component.InteractionMode;
18 ilm 65
import org.openconcerto.ui.preferences.DefaultProps;
66
import org.openconcerto.utils.ExceptionHandler;
174 ilm 67
import org.openconcerto.utils.checks.ValidState;
18 ilm 68
 
69
import java.awt.Color;
70
import java.awt.GridBagConstraints;
71
import java.awt.GridBagLayout;
28 ilm 72
import java.awt.event.ActionEvent;
73
import java.awt.event.ActionListener;
19 ilm 74
import java.beans.PropertyChangeEvent;
75
import java.beans.PropertyChangeListener;
18 ilm 76
import java.sql.SQLException;
174 ilm 77
import java.util.HashSet;
156 ilm 78
import java.util.List;
174 ilm 79
import java.util.Set;
18 ilm 80
 
81
import javax.swing.JCheckBox;
82
import javax.swing.JLabel;
142 ilm 83
import javax.swing.JOptionPane;
18 ilm 84
import javax.swing.JPanel;
85
import javax.swing.JScrollPane;
86
import javax.swing.JTextField;
87
import javax.swing.SwingConstants;
88
import javax.swing.SwingUtilities;
89
import javax.swing.event.DocumentEvent;
90
import javax.swing.event.DocumentListener;
91
import javax.swing.event.TableModelEvent;
92
import javax.swing.event.TableModelListener;
93
 
94
public class CommandeSQLComponent extends TransfertBaseSQLComponent {
95
 
28 ilm 96
    private CommandeItemTable table = new CommandeItemTable();
94 ilm 97
    private PanelOOSQLComponent panelOO;
98
 
18 ilm 99
    private JUniqueTextField numeroUniqueCommande;
100
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
101
    private final ITextArea infos = new ITextArea(3, 3);
28 ilm 102
    private ElementComboBox fourn = new ElementComboBox();
149 ilm 103
    private final JCheckBox boxLivrClient = new JCheckBox("Livrer directement le client");
61 ilm 104
    private DefaultElementSQLObject compAdr;
105
    final JPanel panelAdrSpec = new JPanel(new GridBagLayout());
90 ilm 106
    protected ElementComboBox boxAdr;
93 ilm 107
    private JDate dateCommande = new JDate(true);
132 ilm 108
    private ElementSQLObject componentPrincipaleAdr;
18 ilm 109
 
110
    public CommandeSQLComponent() {
111
        super(Configuration.getInstance().getDirectory().getElement("COMMANDE"));
112
    }
113
 
28 ilm 114
    public ElementComboBox getBoxFournisseur() {
115
        return this.fourn;
116
    }
117
 
174 ilm 118
    @Override
119
    protected Set<String> createRequiredNames() {
120
        final Set<String> s = new HashSet<>(4);
121
        if (getTable().contains("ID_TYPE_CMD")) {
122
            s.add("ID_TYPE_CMD");
123
            s.add("DA1");
124
            s.add("ID_POLE_PRODUIT");
125
        }
126
        return s;
127
    }
128
 
18 ilm 129
    public void addViews() {
130
        this.setLayout(new GridBagLayout());
131
        final GridBagConstraints c = new DefaultGridBagConstraints();
132
 
133
        // Numero du commande
134
        c.gridx = 0;
135
        c.weightx = 0;
136
        this.add(new JLabel(getLabelFor("NUMERO"), SwingConstants.RIGHT), c);
137
 
142 ilm 138
        this.numeroUniqueCommande = new JUniqueTextField(16) {
139
            @Override
140
            public String getAutoRefreshNumber() {
141
                if (getMode() == Mode.INSERTION) {
142
                    return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), dateCommande.getDate());
143
                } else {
144
                    return null;
145
                }
146
            }
147
        };
18 ilm 148
        c.gridx++;
149
        c.weightx = 1;
150
        c.fill = GridBagConstraints.NONE;
151
        DefaultGridBagConstraints.lockMinimumSize(numeroUniqueCommande);
152
        this.add(this.numeroUniqueCommande, c);
153
 
154
        // Date
155
        JLabel labelDate = new JLabel(getLabelFor("DATE"));
156
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
157
        c.gridx = 2;
158
        c.weightx = 0;
159
        c.fill = GridBagConstraints.HORIZONTAL;
160
        this.add(labelDate, c);
161
 
162
        c.gridx++;
163
        c.fill = GridBagConstraints.NONE;
164
        this.add(dateCommande, c);
165
 
93 ilm 166
        this.dateCommande.addValueListener(new PropertyChangeListener() {
167
 
168
            @Override
169
            public void propertyChange(PropertyChangeEvent evt) {
170
                if (!isFilling() && dateCommande.getValue() != null) {
171
                    table.setDateDevise(dateCommande.getValue());
172
                }
173
            }
174
        });
175
 
18 ilm 176
        // Fournisseur
177
        c.gridx = 0;
178
        c.gridy++;
179
        c.weightx = 0;
180
        c.fill = GridBagConstraints.HORIZONTAL;
181
        this.add(new JLabel(getLabelFor("ID_FOURNISSEUR"), SwingConstants.RIGHT), c);
182
 
93 ilm 183
        c.gridx++;
18 ilm 184
        c.gridwidth = 1;
185
        c.weightx = 1;
186
        c.weighty = 0;
187
        c.fill = GridBagConstraints.NONE;
188
        this.add(this.fourn, c);
149 ilm 189
        addRequiredSQLObject(this.fourn, "ID_FOURNISSEUR");
156 ilm 190
        fourn.addModelListener("wantedID", new PropertyChangeListener() {
18 ilm 191
 
156 ilm 192
            @Override
193
            public void propertyChange(PropertyChangeEvent evt) {
194
                int wantedID = fourn.getWantedID();
195
                if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
196
 
197
                    final SQLRow rowF = getTable().getForeignTable("ID_FOURNISSEUR").getRow(wantedID);
198
 
199
                    if (rowF.getObject("ID_CATEGORIE_COMPTABLE") != null && !rowF.isForeignEmpty("ID_CATEGORIE_COMPTABLE")) {
200
                        table.setRowCatComptable(rowF.getForeign("ID_CATEGORIE_COMPTABLE"));
201
                    } else {
202
                        table.setRowCatComptable(null);
203
                    }
174 ilm 204
                    if (getTable().contains("OFFRE_COM")) {
205
                        ((SQLTextCombo) getView("OFFRE_COM").getComp()).setValue(rowF.getString("RESPONSABLE"));
206
                    }
156 ilm 207
                } else {
208
                    table.setRowCatComptable(null);
209
                }
210
            }
211
        });
212
 
174 ilm 213
        Boolean useCommandeEnCours = SQLPreferences.getMemCached(getElement().getTable().getDBRoot()).getBoolean(GestionCommercialeGlobalPreferencePanel.COMMANDE_FOURNISSEUR_EN_COURS, false);
214
 
215
        if (!getTable().getFieldsName().contains("LIVRER") && useCommandeEnCours) {
28 ilm 216
            // Commande en cours
217
            JCheckBox boxEnCours = new JCheckBox(getLabelFor("EN_COURS"));
93 ilm 218
            c.gridx += 2;
28 ilm 219
            c.weightx = 0;
220
            c.fill = GridBagConstraints.HORIZONTAL;
61 ilm 221
            c.gridwidth = GridBagConstraints.REMAINDER;
28 ilm 222
            this.add(boxEnCours, c);
61 ilm 223
            c.gridwidth = 1;
28 ilm 224
            this.addRequiredSQLObject(boxEnCours, "EN_COURS");
225
        }
19 ilm 226
 
94 ilm 227
        if (getTable().contains("DATE_RECEPTION_DEMANDEE")) {
228
            // Date
229
            JLabel labelDateRecptDemande = new JLabel(getLabelFor("DATE_RECEPTION_DEMANDEE"));
230
            labelDateRecptDemande.setHorizontalAlignment(SwingConstants.RIGHT);
231
            c.gridx = 0;
232
            c.gridy++;
233
            c.weightx = 0;
234
            c.fill = GridBagConstraints.HORIZONTAL;
235
            this.add(labelDateRecptDemande, c);
236
 
237
            c.gridx++;
238
            c.fill = GridBagConstraints.NONE;
239
            JDate dateRecptDemande = new JDate();
240
            this.add(dateRecptDemande, c);
241
            this.addView(dateRecptDemande, "DATE_RECEPTION_DEMANDEE", REQ);
242
 
243
            JLabel labelDateRecptConfirme = new JLabel(getLabelFor("DATE_RECEPTION_CONFIRMEE"));
244
            labelDateRecptConfirme.setHorizontalAlignment(SwingConstants.RIGHT);
245
            c.gridx++;
246
            c.weightx = 0;
247
            c.fill = GridBagConstraints.HORIZONTAL;
248
            this.add(labelDateRecptConfirme, c);
249
 
250
            c.gridx++;
251
            c.fill = GridBagConstraints.NONE;
252
            JDate dateRecptConfirme = new JDate();
253
            this.add(dateRecptConfirme, c);
254
            this.addView(dateRecptConfirme, "DATE_RECEPTION_CONFIRMEE");
255
        }
57 ilm 256
        // Fournisseur
257
        if (getTable().contains("ID_CONTACT_FOURNISSEUR")) {
258
            c.gridx = 0;
259
            c.gridy++;
260
            c.weightx = 0;
261
            c.fill = GridBagConstraints.HORIZONTAL;
262
            this.add(new JLabel(getLabelFor("ID_CONTACT_FOURNISSEUR"), SwingConstants.RIGHT), c);
263
 
264
            c.gridx = GridBagConstraints.RELATIVE;
265
            c.gridwidth = 1;
266
            c.weightx = 1;
267
            c.weighty = 0;
268
            c.fill = GridBagConstraints.HORIZONTAL;
269
            final ElementComboBox boxContactFournisseur = new ElementComboBox();
270
            final SQLElement contactElement = Configuration.getInstance().getDirectory().getElement("CONTACT_FOURNISSEUR");
65 ilm 271
            boxContactFournisseur.init(contactElement, contactElement.getComboRequest(true));
57 ilm 272
            this.add(boxContactFournisseur, c);
273
            this.addView(boxContactFournisseur, "ID_CONTACT_FOURNISSEUR", REQ);
274
 
275
            fourn.addValueListener(new PropertyChangeListener() {
276
 
277
                @Override
278
                public void propertyChange(PropertyChangeEvent arg0) {
279
                    if (fourn.getSelectedRow() != null) {
280
                        boxContactFournisseur.getRequest().setWhere(new Where(contactElement.getTable().getField("ID_FOURNISSEUR"), "=", fourn.getSelectedRow().getID()));
281
                    } else {
282
                        boxContactFournisseur.getRequest().setWhere(null);
283
                    }
284
                }
285
            });
286
        }
28 ilm 287
        // Adresse de livraison
288
        if (getTable().getFieldsName().contains("ID_ADRESSE")) {
57 ilm 289
            if (getTable().getFieldsName().contains("LIVRAISON_F")) {
61 ilm 290
                c.gridx = 0;
291
                c.gridy++;
292
                c.weightx = 0;
293
                c.fill = GridBagConstraints.HORIZONTAL;
90 ilm 294
 
295
                this.boxAdr = new ElementComboBox();
296
                final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
297
                boxAdr.init(adrElement);
298
                c.gridwidth = 1;
299
                final JLabel labelAdrLiv = new JLabel("Adresse de livraison existante");
300
                this.add(labelAdrLiv, c);
301
                c.gridx++;
302
                c.gridwidth = 2;
303
                this.add(boxAdr, c);
304
 
305
                c.gridx = 0;
306
                c.gridy++;
61 ilm 307
                this.add(new JLabel(getLabelFor("ID_ADRESSE")), c);
308
                c.gridx++;
309
                c.gridwidth = GridBagConstraints.REMAINDER;
310
                // c.gridy++;
311
                this.addView("ID_ADRESSE");
312
                final DefaultElementSQLObject comp = (DefaultElementSQLObject) this.getView("ID_ADRESSE").getComp();
132 ilm 313
 
314
                componentPrincipaleAdr = (ElementSQLObject) this.getView("ID_ADRESSE");
315
                ((AdresseSQLComponent) componentPrincipaleAdr.getSQLChild()).setDestinataireVisible(true);
90 ilm 316
                final JCheckBox boxLivr = new JCheckBox("Livré par le fournisseur");
317
                this.add(boxLivr, c);
318
                this.addSQLObject(boxLivr, "LIVRAISON_F");
319
                boxLivr.addActionListener(new ActionListener() {
28 ilm 320
 
90 ilm 321
                    @Override
322
                    public void actionPerformed(ActionEvent e) {
132 ilm 323
 
90 ilm 324
                        if (boxLivr.isSelected() && !comp.isCreated()) {
325
                            comp.setCreated(true);
132 ilm 326
                            componentPrincipaleAdr.setEditable(InteractionMode.READ_WRITE);
90 ilm 327
                            if (CommandeSQLComponent.this.getTable().contains("ID_AFFAIRE")) {
28 ilm 328
 
90 ilm 329
                                final SQLRow selectedRow = ((ElementComboBox) CommandeSQLComponent.this.getView("ID_AFFAIRE").getComp()).getSelectedRow();
330
                                SQLRowValues rowVals = getLivraisonAdr(selectedRow);
61 ilm 331
 
90 ilm 332
                                comp.setValue(rowVals);
333
 
334
                                if (selectedRow != null && !selectedRow.isUndefined()) {
335
                                    final SQLRow clientRow = selectedRow.getForeign("ID_CLIENT");
336
                                    Where w = new Where(boxAdr.getRequest().getPrimaryTable().getField("ID_CLIENT"), "=", clientRow.getID());
337
                                    w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", clientRow.getInt("ID_ADRESSE")));
338
                                    // w = w.or(new
339
                                    // Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=",
340
                                    // clientRow.getInt("ID_ADRESSE_F")));
341
                                    w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", clientRow.getInt("ID_ADRESSE_L")));
342
                                    if (clientRow.getTable().contains("ID_ADRESSE_L_2")) {
343
                                        w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", clientRow.getInt("ID_ADRESSE_L_2")));
344
                                    }
345
                                    if (clientRow.getTable().contains("ID_ADRESSE_L_3")) {
346
                                        w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", clientRow.getInt("ID_ADRESSE_L_3")));
347
                                    }
348
                                    boxAdr.getRequest().setWhere(w);
349
                                } else {
350
                                    boxAdr.getRequest().setWhere(null);
61 ilm 351
                                }
352
 
57 ilm 353
                            }
90 ilm 354
 
355
                        } else {
356
                            if (!boxLivr.isSelected()) {
357
                                comp.setCreated(false);
132 ilm 358
                                componentPrincipaleAdr.setEditable(InteractionMode.DISABLED);
90 ilm 359
                            }
61 ilm 360
                        }
90 ilm 361
                    }
362
                });
57 ilm 363
 
61 ilm 364
                c.gridy++;
365
                this.add(comp, c);
366
                this.add(this.getView("ID_ADRESSE").getComp(), c);
90 ilm 367
 
368
                comp.addValueListener(new PropertyChangeListener() {
369
 
370
                    @Override
371
                    public void propertyChange(PropertyChangeEvent evt) {
372
                        boxAdr.setVisible(comp.isCreated());
373
                        labelAdrLiv.setVisible(comp.isCreated());
374
                    }
375
                });
376
 
377
                boxAdr.addValueListener(new PropertyChangeListener() {
378
 
379
                    @Override
380
                    public void propertyChange(PropertyChangeEvent evt) {
381
                        final SQLRow selectedRow = boxAdr.getSelectedRow();
382
                        if (selectedRow != null && !selectedRow.isUndefined()) {
383
                            SQLRowValues rowVals = selectedRow.asRowValues();
384
                            rowVals.clearPrimaryKeys();
385
                            comp.setValue(rowVals);
386
                        }
387
                    }
388
                });
389
                boxAdr.setVisible(false);
390
                labelAdrLiv.setVisible(false);
391
 
61 ilm 392
            } else {
393
 
394
                c.gridy++;
149 ilm 395
                c.gridx = 1;
156 ilm 396
 
61 ilm 397
                c.gridwidth = GridBagConstraints.REMAINDER;
398
                this.add(boxLivrClient, c);
399
                c.gridwidth = 1;
400
 
401
                final GridBagConstraints cAdr = new DefaultGridBagConstraints();
402
 
403
                panelAdrSpec.add(new JLabel(getLabelFor("ID_CLIENT"), SwingConstants.RIGHT), cAdr);
404
                final ElementComboBox boxClient = new ElementComboBox(true);
405
                cAdr.weightx = 1;
406
                cAdr.gridx++;
407
                panelAdrSpec.add(boxClient, cAdr);
408
                this.addView(boxClient, "ID_CLIENT");
409
 
410
                cAdr.gridy++;
411
                cAdr.weightx = 0;
412
                cAdr.gridx = 0;
93 ilm 413
                panelAdrSpec.add(new JLabel(TM.tr("address"), SwingConstants.RIGHT), cAdr);
61 ilm 414
                final SQLRequestComboBox boxAdr = new SQLRequestComboBox(true);
415
                boxAdr.uiInit(Configuration.getInstance().getDirectory().getElement(getTable().getTable("ADRESSE")).getComboRequest(true));
73 ilm 416
                boxClient.addModelListener("wantedID", new PropertyChangeListener() {
61 ilm 417
 
418
                    @Override
419
                    public void propertyChange(PropertyChangeEvent evt) {
420
                        if (boxClient.getSelectedRow() != null && !boxClient.getSelectedRow().isUndefined()) {
421
                            Where w = new Where(boxAdr.getRequest().getPrimaryTable().getField("ID_CLIENT"), "=", boxClient.getSelectedRow().getID());
422
                            w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", boxClient.getSelectedRow().getInt("ID_ADRESSE")));
423
                            w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", boxClient.getSelectedRow().getInt("ID_ADRESSE_F")));
424
                            w = w.or(new Where(boxAdr.getRequest().getPrimaryTable().getKey(), "=", boxClient.getSelectedRow().getInt("ID_ADRESSE_L")));
425
                            boxAdr.getRequest().setWhere(w);
57 ilm 426
                        } else {
61 ilm 427
                            boxAdr.getRequest().setWhere(null);
28 ilm 428
                        }
429
                    }
57 ilm 430
                });
61 ilm 431
                cAdr.weightx = 1;
432
                cAdr.gridx++;
433
                panelAdrSpec.add(boxAdr, cAdr);
434
 
435
                cAdr.gridx = 0;
436
                cAdr.gridy++;
437
                cAdr.weightx = 0;
438
                if (getMode() == Mode.MODIFICATION) {
439
                    panelAdrSpec.add(new JLabel(getLabelFor("ID_ADRESSE")), cAdr);
440
                }
441
                cAdr.gridx++;
442
                cAdr.gridwidth = GridBagConstraints.REMAINDER;
443
                this.addView("ID_ADRESSE");
444
                compAdr = (DefaultElementSQLObject) this.getView("ID_ADRESSE").getComp();
445
 
446
                cAdr.gridy++;
447
                if (getMode() == Mode.MODIFICATION) {
448
                    panelAdrSpec.add(compAdr, cAdr);
449
                }
450
                boxAdr.addValueListener(new PropertyChangeListener() {
451
 
452
                    @Override
453
                    public void propertyChange(PropertyChangeEvent evt) {
454
                        SQLRow row = boxAdr.getSelectedRow();
455
                        if (row != null && !row.isUndefined()) {
456
                            compAdr.setCreated(true);
457
                            SQLRowValues asRowValues = new SQLRowValues(row.asRowValues());
458
                            compAdr.setValue(asRowValues);
459
                        }
460
                    }
461
                });
462
 
463
                c.gridy++;
464
                c.gridx = 0;
465
                c.gridwidth = GridBagConstraints.REMAINDER;
466
                c.weightx = 1;
467
                this.add(panelAdrSpec, c);
468
                c.gridwidth = 1;
469
                c.weightx = 0;
470
 
471
                boxLivrClient.addActionListener(new ActionListener() {
472
 
473
                    @Override
474
                    public void actionPerformed(ActionEvent e) {
475
                        panelAdrSpec.setVisible(boxLivrClient.isSelected());
476
 
477
                        if (!boxLivrClient.isSelected()) {
478
                            boxClient.setValue((Integer) null);
479
                            boxAdr.setValue((Integer) null);
480
                            compAdr.setCreated(false);
481
                        }
482
                    }
483
                });
484
                panelAdrSpec.setVisible(false);
57 ilm 485
            }
28 ilm 486
        }
487
        c.gridwidth = 1;
488
 
489
        // Champ Module
19 ilm 490
        c.gridx = 0;
491
        c.gridy++;
28 ilm 492
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 493
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
28 ilm 494
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
495
        this.add(addP, c);
19 ilm 496
 
28 ilm 497
        c.gridy++;
19 ilm 498
        c.gridwidth = 1;
499
 
156 ilm 500
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
501
        final boolean showDevise = prefs.getBoolean(AbstractVenteArticleItemTable.ARTICLE_SHOW_DEVISE, false);
502
 
28 ilm 503
        final ElementComboBox boxDevise = new ElementComboBox();
156 ilm 504
        if (showDevise) {
28 ilm 505
            // Devise
506
            c.gridx = 0;
507
            c.gridy++;
508
            c.weightx = 0;
509
            c.fill = GridBagConstraints.HORIZONTAL;
510
            this.add(new JLabel(getLabelFor("ID_DEVISE"), SwingConstants.RIGHT), c);
511
 
94 ilm 512
            c.gridx++;
28 ilm 513
            c.gridwidth = 1;
514
            c.weightx = 1;
515
            c.weighty = 0;
516
            c.fill = GridBagConstraints.NONE;
132 ilm 517
            DefaultGridBagConstraints.lockMinimumSize(boxDevise);
28 ilm 518
            this.add(boxDevise, c);
519
            this.addView(boxDevise, "ID_DEVISE");
93 ilm 520
 
521
            fourn.addValueListener(new PropertyChangeListener() {
522
 
523
                @Override
524
                public void propertyChange(PropertyChangeEvent arg0) {
525
                    if (fourn.getSelectedRow() != null) {
526
                        boxDevise.setValue(fourn.getSelectedRow().getForeignID("ID_DEVISE"));
527
                    } else {
528
                        boxDevise.setValue((SQLRowAccessor) null);
529
                    }
530
                }
531
            });
532
 
94 ilm 533
            if (getTable().contains("INCOTERM")) {
534
                // Incoterm
535
                c.gridx++;
536
                c.weightx = 0;
537
                c.fill = GridBagConstraints.HORIZONTAL;
538
                this.add(new JLabel(getLabelFor("INCOTERM"), SwingConstants.RIGHT), c);
539
 
540
                c.gridx++;
541
                c.gridwidth = 1;
542
                c.weightx = 1;
543
                c.weighty = 0;
544
                c.fill = GridBagConstraints.NONE;
132 ilm 545
                final ITextCombo box = new ITextCombo(ComboLockedMode.LOCKED);
94 ilm 546
 
547
                for (String s : ReferenceArticleSQLElement.CONDITIONS) {
548
                    box.addItem(s);
549
                }
550
                this.add(box, c);
551
                this.addView(box, "INCOTERM", REQ);
132 ilm 552
                box.addValueListener(new PropertyChangeListener() {
553
 
554
                    @Override
555
                    public void propertyChange(PropertyChangeEvent evt) {
556
                        table.setIncoterms(box.getCurrentValue());
557
                    }
558
                });
94 ilm 559
            }
560
 
28 ilm 561
        }
562
 
18 ilm 563
        // Reference
564
        c.gridx = 0;
565
        c.gridy++;
566
        c.gridwidth = 1;
567
        c.weightx = 0;
568
        c.fill = GridBagConstraints.HORIZONTAL;
569
        c.anchor = GridBagConstraints.EAST;
570
        this.add(new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT), c);
571
 
572
        final JTextField textNom = new JTextField();
573
        c.gridx++;
574
        c.weightx = 1;
575
        this.add(textNom, c);
576
 
577
        String field;
578
            field = "ID_COMMERCIAL";
579
        // Commercial
580
        c.weightx = 0;
581
        c.gridx++;
582
        this.add(new JLabel(getLabelFor(field), SwingConstants.RIGHT), c);
583
 
584
        ElementComboBox commSel = new ElementComboBox(false, 25);
585
        c.gridx = GridBagConstraints.RELATIVE;
586
        c.gridwidth = 1;
587
        c.weightx = 1;
588
        c.weighty = 0;
589
        c.fill = GridBagConstraints.NONE;
590
        c.anchor = GridBagConstraints.WEST;
591
        this.add(commSel, c);
592
        addRequiredSQLObject(commSel, field);
593
 
594
        // Table d'élément
595
        c.fill = GridBagConstraints.BOTH;
596
        c.gridy++;
597
        c.gridx = 0;
598
        c.weightx = 0;
599
        c.weighty = 1;
600
        c.gridwidth = 4;
601
        this.add(this.table, c);
156 ilm 602
        if (showDevise) {
18 ilm 603
 
28 ilm 604
            boxDevise.addValueListener(new PropertyChangeListener() {
19 ilm 605
 
28 ilm 606
                @Override
607
                public void propertyChange(PropertyChangeEvent evt) {
608
                    table.setDevise(boxDevise.getSelectedRow());
609
                }
610
            });
611
        }
67 ilm 612
 
613
        this.fourn.addValueListener(new PropertyChangeListener() {
614
 
615
            @Override
616
            public void propertyChange(PropertyChangeEvent evt) {
617
                table.setFournisseur(fourn.getSelectedRow());
618
            }
619
        });
18 ilm 620
        // Bottom
621
        c.gridy++;
622
        c.weighty = 0;
623
        this.add(getBottomPanel(), c);
624
 
625
        c.gridx = 0;
626
        c.gridy++;
94 ilm 627
        c.fill = GridBagConstraints.NONE;
628
        c.anchor = GridBagConstraints.SOUTHEAST;
18 ilm 629
        c.gridwidth = GridBagConstraints.REMAINDER;
630
 
94 ilm 631
        this.panelOO = new PanelOOSQLComponent(this);
632
        this.add(this.panelOO, c);
633
 
18 ilm 634
        addSQLObject(textNom, "NOM");
635
        addRequiredSQLObject(dateCommande, "DATE");
636
        // addRequiredSQLObject(radioEtat, "ID_ETAT_DEVIS");
637
        addRequiredSQLObject(this.numeroUniqueCommande, "NUMERO");
638
        addSQLObject(this.infos, "INFOS");
639
 
90 ilm 640
        this.numeroUniqueCommande.setText(NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 641
 
642
        // radioEtat.setValue(EtatDevisSQLElement.EN_ATTENTE);
643
        // this.numeroUniqueDevis.addLabelWarningMouseListener(new MouseAdapter() {
644
        // public void mousePressed(MouseEvent e) {
645
        //
646
        // if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
647
        // numeroUniqueDevis.setText(NumerotationAutoSQLElement.getNextNumeroDevis());
648
        // }
649
        // }
650
        // });
651
 
652
        DefaultGridBagConstraints.lockMinimumSize(this.fourn);
653
        DefaultGridBagConstraints.lockMinimumSize(commSel);
654
    }
655
 
28 ilm 656
    protected SQLRowValues getLivraisonAdr(SQLRow rowAffaire) {
657
        if (rowAffaire != null) {
658
            SQLRow rowClient = rowAffaire.getForeignRow("ID_CLIENT");
659
            SQLRow rowAdrL = rowClient.getForeignRow("ID_ADRESSE_L");
660
            if (rowAdrL == null || rowAdrL.isUndefined()) {
661
                rowAdrL = rowClient.getForeignRow("ID_ADRESSE");
662
            }
663
            SQLRowValues rowVals = rowAdrL.asRowValues();
664
            rowVals.clearPrimaryKeys();
665
            return rowVals;
666
        } else {
667
            return new SQLRowValues(getTable().getTable("ADRESSE"));
668
        }
669
    }
670
 
18 ilm 671
    private JPanel getBottomPanel() {
672
        final JPanel panel = new JPanel(new GridBagLayout());
673
        final GridBagConstraints c = new DefaultGridBagConstraints();
674
 
675
        // Colonne 1 : Infos
676
        c.gridx = 0;
677
        c.weightx = 1;
678
        c.anchor = GridBagConstraints.WEST;
679
        c.fill = GridBagConstraints.HORIZONTAL;
57 ilm 680
 
18 ilm 681
        panel.add(new TitledSeparator(getLabelFor("INFOS")), c);
682
 
683
        c.gridy++;
684
        c.weighty = 0;
685
        c.weightx = 1;
686
        c.fill = GridBagConstraints.BOTH;
687
        final JScrollPane scrollPane = new JScrollPane(this.infos);
688
        scrollPane.setBorder(null);
689
        panel.add(scrollPane, c);
690
 
691
        // Colonne 2 : Poids & autres
692
        DefaultProps props = DefaultNXProps.getInstance();
693
        Boolean b = props.getBooleanValue("ArticleShowPoids");
694
        final JTextField textPoidsTotal = new JTextField(8);
19 ilm 695
        JTextField poids = new JTextField();
18 ilm 696
        if (b) {
132 ilm 697
            final JPanel panelPoids = new JPanel(new GridBagLayout());
698
            GridBagConstraints cPoids = new DefaultGridBagConstraints();
699
            cPoids.weightx = 0;
700
            panelPoids.add(new JLabel(getLabelFor("T_POIDS")), cPoids);
701
            cPoids.weightx = 1;
18 ilm 702
            textPoidsTotal.setEnabled(false);
703
            textPoidsTotal.setHorizontalAlignment(JTextField.RIGHT);
704
            textPoidsTotal.setDisabledTextColor(Color.BLACK);
132 ilm 705
            cPoids.gridx++;
706
            panelPoids.add(textPoidsTotal, cPoids);
18 ilm 707
 
708
            c.gridx++;
709
            c.gridy = 0;
710
            c.weightx = 0;
711
            c.weighty = 0;
712
            c.gridwidth = 1;
713
            c.gridheight = 2;
714
            c.fill = GridBagConstraints.NONE;
715
            c.anchor = GridBagConstraints.NORTHEAST;
716
            panel.add(panelPoids, c);
717
            DefaultGridBagConstraints.lockMinimumSize(panelPoids);
19 ilm 718
            addSQLObject(textPoidsTotal, "T_POIDS");
719
        } else {
720
            addSQLObject(poids, "T_POIDS");
18 ilm 721
        }
57 ilm 722
 
723
        DeviseField textPortHT = new DeviseField();
149 ilm 724
        DeviseField textRemiseHT = new DeviseField();
132 ilm 725
        ElementComboBox comboTaxePort = new ElementComboBox(false, 10);
57 ilm 726
 
727
        if (getTable().contains("PORT_HT")) {
149 ilm 728
 
729
            addSQLObject(textPortHT, "PORT_HT");
65 ilm 730
            final JPanel panelPoids = new JPanel(new GridBagLayout());
731
            GridBagConstraints cPort = new DefaultGridBagConstraints();
732
            cPort.gridx = 0;
132 ilm 733
            cPort.fill = GridBagConstraints.NONE;
65 ilm 734
            cPort.weightx = 0;
735
            panelPoids.add(new JLabel(getLabelFor("PORT_HT")), cPort);
57 ilm 736
            textPortHT.setHorizontalAlignment(JTextField.RIGHT);
65 ilm 737
            cPort.gridx++;
738
            cPort.weightx = 1;
739
            panelPoids.add(textPortHT, cPort);
57 ilm 740
 
65 ilm 741
            cPort.gridy++;
742
            cPort.gridx = 0;
743
            cPort.weightx = 0;
744
            addRequiredSQLObject(comboTaxePort, "ID_TAXE_PORT");
745
            panelPoids.add(new JLabel(getLabelFor("ID_TAXE_PORT")), cPort);
746
            cPort.gridx++;
132 ilm 747
            cPort.weightx = 1;
65 ilm 748
            panelPoids.add(comboTaxePort, cPort);
57 ilm 749
 
149 ilm 750
            addSQLObject(textRemiseHT, "REMISE_HT");
751
            cPort.gridy++;
752
            cPort.gridx = 0;
753
            cPort.fill = GridBagConstraints.NONE;
754
            cPort.weightx = 0;
755
            panelPoids.add(new JLabel(getLabelFor("REMISE_HT")), cPort);
756
            textRemiseHT.setHorizontalAlignment(JTextField.RIGHT);
757
            cPort.gridx++;
758
            cPort.weightx = 1;
759
            panelPoids.add(textRemiseHT, cPort);
760
 
57 ilm 761
            c.gridx++;
762
            c.gridy = 0;
763
            c.weightx = 0;
764
            c.weighty = 0;
765
            c.gridwidth = 1;
766
            c.gridheight = 2;
767
            c.fill = GridBagConstraints.NONE;
768
            c.anchor = GridBagConstraints.NORTHEAST;
769
            panel.add(panelPoids, c);
770
            DefaultGridBagConstraints.lockMinimumSize(panelPoids);
771
        }
18 ilm 772
        // Total
57 ilm 773
 
18 ilm 774
        DeviseField fieldHT = new DeviseField();
142 ilm 775
        DeviseField fieldEco = new DeviseField();
18 ilm 776
        DeviseField fieldTVA = new DeviseField();
777
        DeviseField fieldTTC = new DeviseField();
19 ilm 778
        DeviseField fieldDevise = new DeviseField();
18 ilm 779
        DeviseField fieldService = new DeviseField();
780
        fieldHT.setOpaque(false);
781
        fieldTVA.setOpaque(false);
782
        fieldTTC.setOpaque(false);
783
        fieldService.setOpaque(false);
142 ilm 784
        addRequiredSQLObject(fieldEco, "T_ECO_CONTRIBUTION");
19 ilm 785
        addRequiredSQLObject(fieldDevise, "T_DEVISE");
18 ilm 786
        addRequiredSQLObject(fieldHT, "T_HT");
787
        addRequiredSQLObject(fieldTVA, "T_TVA");
57 ilm 788
 
18 ilm 789
        addRequiredSQLObject(fieldTTC, "T_TTC");
790
        addRequiredSQLObject(fieldService, "T_SERVICE");
94 ilm 791
 
792
        // Disable
142 ilm 793
        this.allowEditable("T_ECO_CONTRIBUTION", false);
94 ilm 794
        this.allowEditable("T_HT", false);
795
        this.allowEditable("T_TVA", false);
796
        this.allowEditable("T_TTC", false);
797
        this.allowEditable("T_SERVICE", false);
798
 
142 ilm 799
        final TotalPanel totalTTC = new TotalPanel(this.table, fieldEco, fieldHT, fieldTVA, fieldTTC, textPortHT, textRemiseHT, fieldService, null, fieldDevise, null, null,
800
                (getTable().contains("ID_TAXE_PORT") ? comboTaxePort : null), null);
18 ilm 801
 
802
        c.gridx++;
803
        c.gridy--;
804
        c.gridwidth = GridBagConstraints.REMAINDER;
805
        c.gridheight = 2;
806
        c.anchor = GridBagConstraints.NORTHEAST;
807
        c.fill = GridBagConstraints.BOTH;
808
        c.weighty = 0;
132 ilm 809
        c.weightx = 0;
18 ilm 810
        DefaultGridBagConstraints.lockMinimumSize(totalTTC);
811
 
812
        panel.add(totalTTC, c);
813
 
65 ilm 814
        c.gridy += 3;
815
        c.gridheight = 2;
816
        c.fill = GridBagConstraints.NONE;
817
        c.anchor = GridBagConstraints.EAST;
818
        panel.add(getModuleTotalPanel(), c);
819
 
18 ilm 820
        table.getModel().addTableModelListener(new TableModelListener() {
821
 
822
            public void tableChanged(TableModelEvent e) {
823
                textPoidsTotal.setText(String.valueOf(table.getPoidsTotal()));
824
            }
825
        });
826
 
827
        textPortHT.getDocument().addDocumentListener(new DocumentListener() {
828
            public void changedUpdate(DocumentEvent e) {
829
                totalTTC.updateTotal();
830
            }
831
 
832
            public void removeUpdate(DocumentEvent e) {
833
                totalTTC.updateTotal();
834
            }
835
 
836
            public void insertUpdate(DocumentEvent e) {
837
                totalTTC.updateTotal();
838
            }
839
        });
840
 
65 ilm 841
        comboTaxePort.addValueListener(new PropertyChangeListener() {
842
 
843
            @Override
844
            public void propertyChange(PropertyChangeEvent evt) {
845
                totalTTC.updateTotal();
846
            }
847
        });
848
 
18 ilm 849
        textRemiseHT.getDocument().addDocumentListener(new DocumentListener() {
850
            public void changedUpdate(DocumentEvent e) {
851
                totalTTC.updateTotal();
852
            }
853
 
854
            public void removeUpdate(DocumentEvent e) {
855
                totalTTC.updateTotal();
856
            }
857
 
858
            public void insertUpdate(DocumentEvent e) {
859
                totalTTC.updateTotal();
860
            }
861
        });
862
        return panel;
863
    }
864
 
65 ilm 865
    protected JPanel getModuleTotalPanel() {
866
 
867
        return AutoHideListener.listen(new JPanel());
868
    }
869
 
18 ilm 870
    public int insert(SQLRow order) {
871
 
872
        int idCommande = getSelectedID();
873
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
142 ilm 874
        int attempt = 0;
875
        if (!this.numeroUniqueCommande.checkValidation(false)) {
876
            while (attempt < JUniqueTextField.RETRY_COUNT) {
877
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), dateCommande.getDate());
878
                this.numeroUniqueCommande.setText(num);
879
                attempt++;
880
                if (this.numeroUniqueCommande.checkValidation(false)) {
881
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
882
                    break;
883
                }
884
                try {
885
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
886
                } catch (InterruptedException e) {
887
                    e.printStackTrace();
888
                }
889
            }
890
        }
891
        final String num = this.numeroUniqueCommande.getText();
892
        if (attempt == JUniqueTextField.RETRY_COUNT) {
893
            idCommande = getSelectedID();
894
            ExceptionHandler.handle("Impossible d'ajouter, numéro de commande existant.");
895
            final Object root = SwingUtilities.getRoot(this);
896
            if (root instanceof EditFrame) {
897
                final EditFrame frame = (EditFrame) root;
898
                frame.getPanel().setAlwaysVisible(true);
899
            }
900
        } else {
18 ilm 901
            idCommande = super.insert(order);
902
            this.table.updateField("ID_COMMANDE", idCommande);
903
 
904
            // Création des articles
905
            this.table.createArticle(idCommande, this.getElement());
906
 
83 ilm 907
            try {
908
                updateStock(idCommande);
909
            } catch (SQLException e) {
910
                ExceptionHandler.handle("Erreur lors de la mise à jour du stock!", e);
911
            }
912
 
18 ilm 913
            // generation du document
25 ilm 914
            final CommandeXmlSheet sheet = new CommandeXmlSheet(getTable().getRow(idCommande));
915
            sheet.createDocumentAsynchronous();
94 ilm 916
            sheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
18 ilm 917
 
918
            // incrémentation du numéro auto
90 ilm 919
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass()).equalsIgnoreCase(this.numeroUniqueCommande.getText().trim())) {
18 ilm 920
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
90 ilm 921
                int val = this.tableNum.getRow(2).getInt(NumerotationAutoSQLElement.getLabelNumberFor(getElement().getClass()));
18 ilm 922
                val++;
90 ilm 923
                rowVals.put(NumerotationAutoSQLElement.getLabelNumberFor(getElement().getClass()), new Integer(val));
18 ilm 924
 
925
                try {
926
                    rowVals.update(2);
927
                } catch (SQLException e) {
928
                    e.printStackTrace();
929
                }
930
            }
149 ilm 931
            DemandeAchatItemSQLElement elt = getElement().getDirectory().getElement(DemandeAchatItemSQLElement.class);
932
            elt.updateStatus(getTable(), getTable().getTable("COMMANDE_ELEMENT"), idCommande);
933
 
142 ilm 934
            if (attempt > 0) {
935
                SwingUtilities.invokeLater(new Runnable() {
936
                    public void run() {
937
                        JOptionPane.showMessageDialog(null, "Le numéro a été actualisé en " + num);
938
                    }
939
                });
18 ilm 940
            }
941
        }
942
 
943
        return idCommande;
944
    }
945
 
946
    @Override
947
    public void select(SQLRowAccessor r) {
61 ilm 948
        if (!getTable().contains("LIVRAISON_F") && r != null && !r.isUndefined()) {
18 ilm 949
 
94 ilm 950
            SQLRowAccessor adr = (r.getFields().contains("ID_ADRESSE") ? r.getForeign("ID_ADRESSE") : null);
61 ilm 951
            boxLivrClient.setSelected(adr != null && !adr.isUndefined());
952
            panelAdrSpec.setVisible(boxLivrClient.isSelected());
953
 
954
            if (!boxLivrClient.isSelected()) {
955
                compAdr.setCreated(false);
956
            }
957
        }
132 ilm 958
        if (getTable().contains("LIVRAISON_F") && componentPrincipaleAdr != null) {
959
            final boolean bLivraison = r != null && r.getFields().contains("ID_ADRESSE") && !r.isForeignEmpty("ID_ADRESSE");
960
            componentPrincipaleAdr.setEditable(bLivraison ? InteractionMode.READ_WRITE : InteractionMode.DISABLED);
961
        }
61 ilm 962
 
18 ilm 963
        super.select(r);
964
    }
965
 
966
    @Override
967
    public void update() {
968
        if (!this.numeroUniqueCommande.checkValidation()) {
969
            ExceptionHandler.handle("Impossible d'ajouter, numéro de commande client existant.");
970
            Object root = SwingUtilities.getRoot(this);
971
            if (root instanceof EditFrame) {
972
                EditFrame frame = (EditFrame) root;
973
                frame.getPanel().setAlwaysVisible(true);
974
            }
975
            return;
976
        }
977
 
978
        super.update();
83 ilm 979
        final int id = getSelectedID();
980
        this.table.updateField("ID_COMMANDE", id);
981
        this.table.createArticle(id, this.getElement());
982
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
18 ilm 983
 
83 ilm 984
            @Override
985
            public void run() {
986
                try {
987
 
988
                    // Mise à jour du stock
989
                    updateStock(id);
990
                } catch (Exception e) {
991
                    ExceptionHandler.handle("Update error", e);
992
                }
993
            }
994
        });
149 ilm 995
 
996
        DemandeAchatItemSQLElement elt = getElement().getDirectory().getElement(DemandeAchatItemSQLElement.class);
997
        elt.updateStatus(getTable(), getTable().getTable("COMMANDE_ELEMENT"), getSelectedID());
998
 
18 ilm 999
        // generation du document
83 ilm 1000
        final CommandeXmlSheet sheet = new CommandeXmlSheet(getTable().getRow(id));
25 ilm 1001
        sheet.createDocumentAsynchronous();
94 ilm 1002
        sheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
25 ilm 1003
 
18 ilm 1004
    }
1005
 
83 ilm 1006
    protected String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
1007
        return "Commande fournisseur N°" + row.getString("NUMERO");
1008
    }
1009
 
1010
    /**
1011
     * Mise à jour des stocks pour chaque article composant la facture
1012
     *
1013
     * @throws SQLException
1014
     */
1015
    private void updateStock(int id) throws SQLException {
1016
 
1017
        SQLRow row = getTable().getRow(id);
1018
        StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
1019
            @Override
1020
            public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
1021
                return getLibelleStock(rowOrigin, rowElt);
1022
            }
93 ilm 1023
        }, row, row.getReferentRows(getTable().getTable("COMMANDE_ELEMENT")), TypeStockUpdate.VIRTUAL_RECEPT);
83 ilm 1024
 
1025
        stockUpdater.update();
1026
    }
1027
 
174 ilm 1028
    @Override
1029
    public synchronized ValidState getValidState() {
1030
        if (getTable().contains("ID_TYPE_CMD") && getTable().contains("ID_AFFAIRE") && getTable().getTable("COMMANDE_ELEMENT").contains("ID_AFFAIRE")) {
1031
            SQLRequestComboBox boxAff = (SQLRequestComboBox) getView("ID_AFFAIRE").getComp();
1032
            final SQLRow selectedAff = boxAff.getSelectedRow();
1033
            if (selectedAff == null || selectedAff.isUndefined()) {
1034
                RowValuesTableModel tableRow = this.table.getRowValuesTable().getRowValuesTableModel();
1035
 
1036
                for (int i = 0; i < tableRow.getRowCount(); i++) {
1037
                    SQLRowValues rowVals = tableRow.getRowValuesAt(i);
1038
                    if (rowVals.getInt("QTE") != 0 && (rowVals.getObject("ID_AFFAIRE") == null || rowVals.isForeignEmpty("ID_AFFAIRE"))) {
1039
                        return ValidState.create(false, "Aucune affaire globale sélectionnée et une ligne avec une quantité > 0 n'est pas affectée à une affaire!");
1040
                    }
1041
                }
1042
            } else {
1043
                ValidState.create(false, "Aucune affaire globale sélectionnée!");
1044
            }
1045
        }
1046
        return super.getValidState();
1047
    }
1048
 
18 ilm 1049
    public void setDefaults() {
1050
        this.resetValue();
90 ilm 1051
        this.numeroUniqueCommande.setText(NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 1052
        this.table.getModel().clearRows();
1053
    }
1054
 
1055
    @Override
1056
    protected SQLRowValues createDefaults() {
1057
        SQLRowValues rowVals = new SQLRowValues(getTable());
1058
        rowVals.put("T_POIDS", 0.0F);
19 ilm 1059
        rowVals.put("EN_COURS", Boolean.TRUE);
18 ilm 1060
 
1061
        // User
1062
        // SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
1063
        SQLElement eltComm = Configuration.getInstance().getDirectory().getElement("COMMERCIAL");
1064
        int idUser = UserManager.getInstance().getCurrentUser().getId();
1065
 
1066
        // sel.addSelect(eltComm.getTable().getKey());
1067
        // sel.setWhere(new Where(eltComm.getTable().getField("ID_USER_COMMON"), "=", idUser));
1068
        // List<SQLRow> rowsComm = (List<SQLRow>)
1069
        // Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new
1070
        // SQLRowListRSH(eltComm.getTable()));
1071
        SQLRow rowsComm = SQLBackgroundTableCache.getInstance().getCacheForTable(eltComm.getTable()).getFirstRowContains(idUser, eltComm.getTable().getField("ID_USER_COMMON"));
1072
 
1073
        if (rowsComm != null) {
1074
            rowVals.put("ID_COMMERCIAL", rowsComm.getID());
1075
        }
1076
        rowVals.put("T_HT", Long.valueOf(0));
1077
        rowVals.put("T_SERVICE", Long.valueOf(0));
28 ilm 1078
        rowVals.put("T_DEVISE", Long.valueOf(0));
18 ilm 1079
        rowVals.put("T_TVA", Long.valueOf(0));
1080
        rowVals.put("T_TTC", Long.valueOf(0));
90 ilm 1081
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 1082
 
65 ilm 1083
        if (getTable().contains("ID_TAXE_PORT")) {
80 ilm 1084
            rowVals.put("ID_TAXE_PORT", TaxeCache.getCache().getFirstTaxe().getID());
65 ilm 1085
        }
132 ilm 1086
        if (getTable().contains("LIVRAISON_F") && componentPrincipaleAdr != null) {
1087
            componentPrincipaleAdr.setEditable(InteractionMode.DISABLED);
1088
        }
65 ilm 1089
 
18 ilm 1090
        return rowVals;
1091
    }
19 ilm 1092
 
90 ilm 1093
    public CommandeItemTable getRowValuesTablePanel() {
1094
        return this.table;
1095
    }
1096
 
73 ilm 1097
    @Override
1098
    public RowValuesTable getRowValuesTable() {
1099
        return this.table.getRowValuesTable();
19 ilm 1100
    }
1101
 
1102
    /**
1103
     * Chargement des éléments d'une commande dans la table
1104
     *
1105
     * @param idCommande
1106
     *
1107
     */
1108
    public void loadCommande(int idCommande) {
1109
 
1110
        SQLElement commande = Configuration.getInstance().getDirectory().getElement("COMMANDE_CLIENT");
1111
        SQLElement commandeElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_CLIENT_ELEMENT");
1112
 
1113
        if (idCommande > 1) {
1114
            SQLInjector injector = SQLInjector.getInjector(commande.getTable(), this.getTable());
1115
            this.select(injector.createRowValuesFrom(idCommande));
1116
        }
1117
 
1118
        loadItem(this.table, commande, idCommande, commandeElt);
1119
    }
1120
 
1121
    /**
1122
     * Chargement des éléments d'un devis dans la table
1123
     *
1124
     * @param idDevis
1125
     *
1126
     */
1127
    public void loadDevis(int idDevis) {
1128
 
1129
        SQLElement devis = Configuration.getInstance().getDirectory().getElement("DEVIS");
1130
        SQLElement devisElt = Configuration.getInstance().getDirectory().getElement("DEVIS_ELEMENT");
1131
 
1132
        if (idDevis > 1) {
1133
            SQLInjector injector = SQLInjector.getInjector(devis.getTable(), this.getTable());
1134
            this.select(injector.createRowValuesFrom(idDevis));
1135
        }
1136
 
1137
        loadItem(this.table, devis, idDevis, devisElt);
1138
    }
1139
 
1140
    /**
1141
     * Chargement des éléments d'une facture dans la table
1142
     *
1143
     * @param idFact
1144
     *
1145
     */
1146
    public void loadFacture(int idFact) {
1147
 
1148
        SQLElement facture = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
1149
        SQLElement factureElt = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE_ELEMENT");
1150
 
1151
        if (idFact > 1) {
1152
            SQLInjector injector = SQLInjector.getInjector(facture.getTable(), this.getTable());
1153
            this.select(injector.createRowValuesFrom(idFact));
1154
        }
1155
 
1156
        loadItem(this.table, facture, idFact, factureElt);
1157
    }
1158
 
93 ilm 1159
    @Override
1160
    protected void refreshAfterSelect(SQLRowAccessor rSource) {
1161
        if (this.dateCommande.getValue() != null) {
1162
            this.table.setDateDevise(this.dateCommande.getValue());
1163
        }
1164
 
1165
    }
156 ilm 1166
 
1167
    public void duplicate(final int idCmd) {
1168
 
1169
        final SQLElement cmd = Configuration.getInstance().getDirectory().getElement("COMMANDE");
1170
        final SQLElement cmdElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_ELEMENT");
1171
 
1172
        if (idCmd > 1) {
1173
            final SQLRow row = cmd.getTable().getRow(idCmd);
1174
            final SQLRowValues rowVals = new SQLRowValues(cmd.getTable());
1175
            rowVals.put("ID_FOURNISSEUR", row.getInt("ID_FOURNISSEUR"));
1176
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
1177
 
1178
            this.select(rowVals);
1179
        }
1180
 
1181
        final List<SQLRow> myListItem = cmd.getTable().getRow(idCmd).getReferentRows(cmdElt.getTable());
1182
 
1183
        if (myListItem.size() != 0) {
1184
            this.table.getModel().clearRows();
1185
 
1186
            for (final SQLRow rowElt : myListItem) {
1187
 
1188
                final SQLRowValues rowVals = rowElt.createUpdateRow();
1189
                rowVals.clearPrimaryKeys();
1190
                this.table.getModel().addRow(rowVals);
1191
                final int rowIndex = this.table.getModel().getRowCount() - 1;
1192
                this.table.getModel().fireTableModelModified(rowIndex);
1193
            }
1194
        } else {
1195
            this.table.getModel().clearRows();
1196
        }
1197
        this.table.getModel().fireTableDataChanged();
1198
        this.table.repaint();
1199
 
1200
    }
18 ilm 1201
}