OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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