OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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