OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.shipment.component;
15
 
28 ilm 16
import static org.openconcerto.utils.CollectionUtils.createSet;
142 ilm 17
 
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;
21
import org.openconcerto.erp.core.common.ui.DeviseField;
73 ilm 22
import org.openconcerto.erp.core.common.ui.TotalPanel;
93 ilm 23
import org.openconcerto.erp.core.customerrelationship.customer.ui.AddressChoiceUI;
24
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseType;
25
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 26
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureItemSQLElement;
156 ilm 27
import org.openconcerto.erp.core.sales.product.model.ProductComponent;
142 ilm 28
import org.openconcerto.erp.core.sales.product.ui.ReliquatRowValuesTable;
18 ilm 29
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonItemSQLElement;
142 ilm 30
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonSQLElement;
18 ilm 31
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
32
import org.openconcerto.erp.core.sales.shipment.ui.BonDeLivraisonItemTable;
83 ilm 33
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
93 ilm 34
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
61 ilm 35
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
21 ilm 36
import org.openconcerto.erp.panel.PanelOOSQLComponent;
61 ilm 37
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
93 ilm 38
import org.openconcerto.erp.preferences.GestionClientPreferencePanel;
94 ilm 39
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
156 ilm 40
import org.openconcerto.erp.utils.TM;
18 ilm 41
import org.openconcerto.sql.Configuration;
42
import org.openconcerto.sql.element.SQLElement;
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.SQLSelect;
47
import org.openconcerto.sql.model.SQLTable;
61 ilm 48
import org.openconcerto.sql.model.Where;
49
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 50
import org.openconcerto.sql.sqlobject.ElementComboBox;
51
import org.openconcerto.sql.sqlobject.JUniqueTextField;
28 ilm 52
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
18 ilm 53
import org.openconcerto.sql.view.EditFrame;
73 ilm 54
import org.openconcerto.sql.view.list.RowValuesTable;
18 ilm 55
import org.openconcerto.sql.view.list.RowValuesTableModel;
56
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 57
import org.openconcerto.ui.FormLayouter;
18 ilm 58
import org.openconcerto.ui.JDate;
59
import org.openconcerto.ui.TitledSeparator;
60
import org.openconcerto.ui.component.ITextArea;
61
import org.openconcerto.utils.ExceptionHandler;
132 ilm 62
import org.openconcerto.utils.NumberUtils;
18 ilm 63
 
64
import java.awt.Color;
65
import java.awt.GridBagConstraints;
66
import java.awt.GridBagLayout;
67
import java.awt.Insets;
68
import java.awt.event.ActionEvent;
69
import java.awt.event.ActionListener;
19 ilm 70
import java.beans.PropertyChangeEvent;
71
import java.beans.PropertyChangeListener;
132 ilm 72
import java.math.BigDecimal;
18 ilm 73
import java.sql.SQLException;
74
import java.util.ArrayList;
132 ilm 75
import java.util.HashMap;
18 ilm 76
import java.util.List;
132 ilm 77
import java.util.Map;
18 ilm 78
 
79
import javax.swing.JButton;
93 ilm 80
import javax.swing.JCheckBox;
18 ilm 81
import javax.swing.JLabel;
19 ilm 82
import javax.swing.JOptionPane;
18 ilm 83
import javax.swing.JPanel;
84
import javax.swing.JScrollPane;
85
import javax.swing.JTextField;
86
import javax.swing.SwingConstants;
87
import javax.swing.SwingUtilities;
93 ilm 88
import javax.swing.event.DocumentEvent;
89
import javax.swing.event.DocumentListener;
18 ilm 90
 
91
import org.apache.commons.dbutils.handlers.ArrayListHandler;
92
 
93
public class BonDeLivraisonSQLComponent extends TransfertBaseSQLComponent {
94
    private BonDeLivraisonItemTable tableBonItem;
142 ilm 95
    private ReliquatRowValuesTable tableBonReliquatItem;
18 ilm 96
    private ElementComboBox selectCommande, comboClient;
21 ilm 97
    private PanelOOSQLComponent panelOO;
18 ilm 98
    private JUniqueTextField textNumeroUnique;
99
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
100
    private final DeviseField textTotalHT = new DeviseField(6);
101
    private final DeviseField textTotalTVA = new DeviseField(6);
102
    private final DeviseField textTotalTTC = new DeviseField(6);
103
    private final JTextField textPoidsTotal = new JTextField(6);
104
    private final JTextField textNom = new JTextField(25);
93 ilm 105
    private final JDate date = new JDate(true);
106
    private final boolean displayDpt;
107
    private final ElementComboBox comboDpt = new ElementComboBox();
18 ilm 108
 
109
    public BonDeLivraisonSQLComponent() {
110
        super(Configuration.getInstance().getDirectory().getElement("BON_DE_LIVRAISON"));
93 ilm 111
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
112
        this.displayDpt = prefs.getBoolean(GestionClientPreferencePanel.DISPLAY_CLIENT_DPT, false);
18 ilm 113
    }
114
 
115
    @Override
73 ilm 116
    protected RowValuesTable getRowValuesTable() {
117
        return this.tableBonItem.getRowValuesTable();
118
    }
119
 
120
    @Override
18 ilm 121
    protected SQLRowValues createDefaults() {
90 ilm 122
        this.textNumeroUnique.setText(NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 123
        this.tableBonItem.getModel().clearRows();
93 ilm 124
        SQLRowValues rowVals = super.createDefaults();
125
        if (rowVals == null) {
126
            rowVals = new SQLRowValues(getTable());
127
        }
128
        if (getTable().contains("CREATE_VIRTUAL_STOCK")) {
129
            rowVals.put("CREATE_VIRTUAL_STOCK", Boolean.TRUE);
130
        }
131
        if (getTable().contains("ID_TAXE_PORT")) {
132
            SQLRow taxeDefault = TaxeCache.getCache().getFirstTaxe();
133
            rowVals.put("ID_TAXE_PORT", taxeDefault.getID());
134
        }
156 ilm 135
        if (getTable().contains("ID_TAXE_FRAIS_DOCUMENT")) {
136
            SQLRow taxeDefault = TaxeCache.getCache().getFirstTaxe();
137
            rowVals.put("ID_TAXE_FRAIS_DOCUMENT", taxeDefault.getID());
138
        }
93 ilm 139
        return rowVals;
18 ilm 140
    }
141
 
142
    public void addViews() {
143
        this.textTotalHT.setOpaque(false);
144
        this.textTotalTVA.setOpaque(false);
145
        this.textTotalTTC.setOpaque(false);
93 ilm 146
        if (getTable().contains("CREATE_VIRTUAL_STOCK")) {
147
            this.addView(new JCheckBox(), "CREATE_VIRTUAL_STOCK");
148
        }
18 ilm 149
        this.selectCommande = new ElementComboBox();
150
 
151
        this.setLayout(new GridBagLayout());
152
 
153
        final GridBagConstraints c = new DefaultGridBagConstraints();
154
 
25 ilm 155
        // Champ Module
156
        c.gridx = 0;
157
        c.gridy++;
158
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 159
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
41 ilm 160
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
25 ilm 161
        this.add(addP, c);
162
 
163
        c.gridy++;
164
        c.gridwidth = 1;
165
 
18 ilm 166
        // Numero
167
        JLabel labelNum = new JLabel(getLabelFor("NUMERO"));
168
        labelNum.setHorizontalAlignment(SwingConstants.RIGHT);
169
        this.add(labelNum, c);
170
 
142 ilm 171
        this.textNumeroUnique = new JUniqueTextField(16) {
172
            @Override
173
            public String getAutoRefreshNumber() {
174
                if (getMode() == Mode.INSERTION) {
175
                    return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), date.getDate());
176
                } else {
177
                    return null;
178
                }
179
            }
180
        };
18 ilm 181
        c.gridx++;
182
        c.weightx = 1;
183
        c.weighty = 0;
184
        c.fill = GridBagConstraints.NONE;
185
        DefaultGridBagConstraints.lockMinimumSize(textNumeroUnique);
186
        this.add(this.textNumeroUnique, c);
187
 
188
        // Date
189
        c.gridx++;
190
        c.fill = GridBagConstraints.HORIZONTAL;
191
        c.weightx = 0;
192
        this.add(new JLabel(getLabelFor("DATE"), SwingConstants.RIGHT), c);
193
 
194
        c.gridx++;
195
        c.weightx = 0;
196
        c.weighty = 0;
197
        c.fill = GridBagConstraints.NONE;
198
        this.add(date, c);
199
 
93 ilm 200
        this.date.addValueListener(new PropertyChangeListener() {
201
 
202
            @Override
203
            public void propertyChange(PropertyChangeEvent evt) {
204
                if (!isFilling() && date.getValue() != null) {
205
                    tableBonItem.setDateDevise(date.getValue());
206
                }
207
            }
208
        });
209
 
18 ilm 210
        // Reference
211
        c.gridy++;
212
        c.gridx = 0;
213
        c.fill = GridBagConstraints.HORIZONTAL;
214
        this.add(new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT), c);
215
        c.gridx++;
216
        c.weightx = 1;
217
        this.add(this.textNom, c);
41 ilm 218
        if (getTable().contains("DATE_LIVRAISON")) {
219
            // Date livraison
220
            c.gridx++;
221
            c.fill = GridBagConstraints.HORIZONTAL;
222
            c.weightx = 0;
223
            this.add(new JLabel(getLabelFor("DATE_LIVRAISON"), SwingConstants.RIGHT), c);
18 ilm 224
 
41 ilm 225
            JDate dateLivraison = new JDate(true);
226
            c.gridx++;
227
            c.weightx = 0;
228
            c.weighty = 0;
229
            c.fill = GridBagConstraints.NONE;
230
            this.add(dateLivraison, c);
231
            this.addView(dateLivraison, "DATE_LIVRAISON");
232
        }
18 ilm 233
        // Client
61 ilm 234
        JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"), SwingConstants.RIGHT);
18 ilm 235
        c.gridx = 0;
236
        c.gridy++;
237
        c.weightx = 0;
61 ilm 238
        c.fill = GridBagConstraints.HORIZONTAL;
18 ilm 239
        c.weighty = 0;
240
        this.add(labelClient, c);
241
 
242
        c.gridx++;
243
        c.weightx = 0;
244
        c.weighty = 0;
245
        c.fill = GridBagConstraints.NONE;
246
        this.comboClient = new ElementComboBox();
41 ilm 247
        this.add(this.comboClient, c);
93 ilm 248
        this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
249
 
250
        if (this.displayDpt) {
251
            c.gridx++;
252
            c.gridwidth = 1;
253
            final JLabel labelDpt = new JLabel(getLabelFor("ID_CLIENT_DEPARTEMENT"));
254
            labelDpt.setHorizontalAlignment(SwingConstants.RIGHT);
255
            c.weightx = 0;
256
            c.gridwidth = 1;
257
            c.fill = GridBagConstraints.HORIZONTAL;
258
            this.add(labelDpt, c);
259
 
260
            c.gridx++;
261
            c.gridwidth = 1;
262
            c.weightx = 0;
263
            c.weighty = 0;
264
            c.fill = GridBagConstraints.NONE;
265
            this.add(this.comboDpt, c);
266
            DefaultGridBagConstraints.lockMinimumSize(this.comboDpt);
267
            addSQLObject(this.comboDpt, "ID_CLIENT_DEPARTEMENT");
268
 
269
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
270
 
271
                @Override
272
                public void propertyChange(PropertyChangeEvent evt) {
273
                    int wantedID = comboClient.getWantedID();
274
 
275
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
276
                        final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
277
                        comboDpt.getRequest().setWhere(new Where(comboDpt.getRequest().getPrimaryTable().getField("ID_CLIENT"), "=", rowClient.getID()));
278
                    } else {
279
                        comboDpt.getRequest().setWhere(null);
280
                    }
281
                }
282
            });
283
 
284
        }
94 ilm 285
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
286
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ADDRESS_SPEC, true)) {
93 ilm 287
 
94 ilm 288
            final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
289
            final AddressChoiceUI addressUI = new AddressChoiceUI();
290
            addressUI.addToUI(this, c);
291
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
93 ilm 292
 
94 ilm 293
                @Override
294
                public void propertyChange(PropertyChangeEvent evt) {
295
                    int wantedID = comboClient.getWantedID();
296
                    System.err.println("SET WHERE ID_CLIENT = " + wantedID);
297
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
93 ilm 298
 
94 ilm 299
                        addressUI.getComboAdrF().getRequest().setWhere(
300
                                new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Invoice.getId())));
301
                        addressUI.getComboAdrL().getRequest().setWhere(
302
                                new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Delivery.getId())));
303
                    } else {
304
                        addressUI.getComboAdrF().getRequest().setWhere(Where.FALSE);
305
                        addressUI.getComboAdrL().getRequest().setWhere(Where.FALSE);
306
                    }
93 ilm 307
                }
94 ilm 308
            });
309
        }
41 ilm 310
        if (getTable().contains("SPEC_LIVRAISON")) {
311
            // Date livraison
312
            c.gridx++;
313
            c.fill = GridBagConstraints.HORIZONTAL;
314
            c.weightx = 0;
315
            this.add(new JLabel(getLabelFor("SPEC_LIVRAISON"), SwingConstants.RIGHT), c);
18 ilm 316
 
41 ilm 317
            JTextField specLivraison = new JTextField();
318
            c.gridx++;
319
            c.weightx = 0;
320
            c.weighty = 0;
321
            this.add(specLivraison, c);
322
            this.addView(specLivraison, "SPEC_LIVRAISON");
323
        }
324
 
93 ilm 325
        if (getTable().contains("ID_CONTACT")) {
326
            // Contact Client
327
            c.gridx = 0;
328
            c.gridy++;
329
            c.gridwidth = 1;
330
            final JLabel labelContact = new JLabel(getLabelFor("ID_CONTACT"));
331
            labelContact.setHorizontalAlignment(SwingConstants.RIGHT);
332
            c.weightx = 0;
333
            c.gridwidth = 1;
334
            c.fill = GridBagConstraints.HORIZONTAL;
335
            this.add(labelContact, c);
336
 
337
            final ElementComboBox comboContact = new ElementComboBox();
338
            c.gridx++;
339
            c.gridwidth = 1;
340
            c.weightx = 0;
341
            c.weighty = 0;
342
            c.fill = GridBagConstraints.NONE;
343
            this.add(comboContact, c);
344
            final SQLElement contactElement = getElement().getForeignElement("ID_CONTACT");
345
            comboContact.init(contactElement, contactElement.getComboRequest(true));
346
            comboContact.getRequest().setWhere(Where.FALSE);
347
            DefaultGridBagConstraints.lockMinimumSize(comboContact);
348
            this.addView(comboContact, "ID_CONTACT");
349
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
350
 
351
                @Override
352
                public void propertyChange(PropertyChangeEvent evt) {
353
                    int wantedID = comboClient.getWantedID();
354
                    System.err.println("SET WHERE ID_CLIENT = " + wantedID);
355
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
356
 
357
                        final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
358
                        int idClient = rowClient.getID();
359
                        comboContact.getRequest().setWhere(new Where(contactElement.getTable().getField("ID_CLIENT"), "=", idClient));
156 ilm 360
                        if (rowClient.getObject("ID_CATEGORIE_COMPTABLE") != null && !rowClient.isForeignEmpty("ID_CATEGORIE_COMPTABLE")) {
361
                            tableBonItem.setRowCatComptable(rowClient.getForeign("ID_CATEGORIE_COMPTABLE"));
362
                        } else {
363
                            tableBonItem.setRowCatComptable(null);
364
                        }
93 ilm 365
                    } else {
366
                        comboContact.getRequest().setWhere(Where.FALSE);
156 ilm 367
                        tableBonItem.setRowCatComptable(null);
93 ilm 368
                        // DevisSQLComponent.this.table.setTarif(null, false);
369
                    }
370
                }
371
            });
372
 
373
        }
374
 
19 ilm 375
        final ElementComboBox boxTarif = new ElementComboBox();
41 ilm 376
        this.comboClient.addValueListener(new PropertyChangeListener() {
377
            @Override
378
            public void propertyChange(PropertyChangeEvent evt) {
379
                if (comboClient.getElement().getTable().contains("ID_TARIF")) {
28 ilm 380
                    if (BonDeLivraisonSQLComponent.this.isFilling())
381
                        return;
382
                    final SQLRow row = ((SQLRequestComboBox) evt.getSource()).getSelectedRow();
383
                    if (row != null) {
384
                        // SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
385
                        // if (foreignRow.isUndefined() &&
386
                        // !row.getForeignRow("ID_DEVISE").isUndefined()) {
387
                        // SQLRowValues rowValsD = new SQLRowValues(foreignRow.getTable());
388
                        // rowValsD.put("ID_DEVISE", row.getObject("ID_DEVISE"));
389
                        // foreignRow = rowValsD;
390
                        //
391
                        // }
392
                        // tableBonItem.setTarif(foreignRow, true);
393
                        SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
394
                        if (!foreignRow.isUndefined() && (boxTarif.getSelectedRow() == null || boxTarif.getSelectedId() != foreignRow.getID())
156 ilm 395
                                && JOptionPane.showConfirmDialog(null, TM.tr("deliveryForm.applyClientRates")) == JOptionPane.YES_OPTION) { //$NON-NLS-1$
28 ilm 396
                            boxTarif.setValue(foreignRow.getID());
397
                            // SaisieVenteFactureSQLComponent.this.tableFacture.setTarif(foreignRow,
398
                            // true);
399
                        } else {
400
                            boxTarif.setValue(foreignRow.getID());
19 ilm 401
                        }
402
                    }
403
                }
41 ilm 404
            }
405
        });
19 ilm 406
 
18 ilm 407
        // Bouton tout livrer
156 ilm 408
        JButton boutonAll = new JButton(TM.tr("deliveryForm.shipAll")); //$NON-NLS-1$
18 ilm 409
 
410
        boutonAll.addActionListener(new ActionListener() {
411
            public void actionPerformed(ActionEvent e) {
412
                RowValuesTableModel m = BonDeLivraisonSQLComponent.this.tableBonItem.getModel();
413
 
414
                // on livre tout les éléments
415
                for (int i = 0; i < m.getRowCount(); i++) {
416
                    SQLRowValues rowVals = m.getRowValuesAt(i);
417
                    Object o = rowVals.getObject("QTE");
418
                    int qte = o == null ? 0 : ((Number) o).intValue();
419
                    m.putValue(qte, i, "QTE_LIVREE");
420
                }
421
            }
422
        });
423
 
19 ilm 424
        // Tarif
425
        if (this.getTable().getFieldsName().contains("ID_TARIF")) {
426
            // TARIF
427
            c.gridy++;
428
            c.gridx = 0;
429
            c.weightx = 0;
430
            c.weighty = 0;
431
            c.gridwidth = 1;
93 ilm 432
            c.fill = GridBagConstraints.HORIZONTAL;
433
            this.add(new JLabel(getLabelFor("ID_TARIF"), SwingUtilities.RIGHT), c);
19 ilm 434
            c.gridx++;
41 ilm 435
            c.gridwidth = 1;
93 ilm 436
            c.fill = GridBagConstraints.NONE;
19 ilm 437
            c.weightx = 1;
438
            this.add(boxTarif, c);
439
            this.addView(boxTarif, "ID_TARIF");
73 ilm 440
            boxTarif.addModelListener("wantedID", new PropertyChangeListener() {
19 ilm 441
 
442
                @Override
443
                public void propertyChange(PropertyChangeEvent evt) {
73 ilm 444
                    SQLRow selectedRow = boxTarif.getRequest().getPrimaryTable().getRow(boxTarif.getWantedID());
445
                    tableBonItem.setTarif(selectedRow, !isFilling());
19 ilm 446
                }
447
            });
448
        }
41 ilm 449
 
450
        if (getTable().contains("A_ATTENTION")) {
451
            // Date livraison
452
            c.gridx++;
453
            c.fill = GridBagConstraints.HORIZONTAL;
454
            c.weightx = 0;
455
            this.add(new JLabel(getLabelFor("A_ATTENTION"), SwingConstants.RIGHT), c);
456
 
457
            JTextField specLivraison = new JTextField();
458
            c.gridx++;
459
            c.weightx = 0;
460
            c.weighty = 0;
461
            this.add(specLivraison, c);
462
            this.addView(specLivraison, "A_ATTENTION");
463
        }
464
 
18 ilm 465
        // Element du bon
466
        List<JButton> l = new ArrayList<JButton>();
467
        l.add(boutonAll);
468
        this.tableBonItem = new BonDeLivraisonItemTable(l);
469
 
470
        c.gridx = 0;
471
        c.gridy++;
472
        c.weightx = 1;
473
        c.weighty = 1;
474
        c.gridwidth = GridBagConstraints.REMAINDER;
475
        c.fill = GridBagConstraints.BOTH;
476
        this.add(this.tableBonItem, c);
477
        c.anchor = GridBagConstraints.EAST;
478
        // Totaux
479
        reconfigure(this.textTotalHT);
480
        reconfigure(this.textTotalTVA);
481
        reconfigure(this.textTotalTTC);
142 ilm 482
        DeviseField fieldEco = new DeviseField(5);
93 ilm 483
        DeviseField textPortHT = new DeviseField(5);
484
        DeviseField textRemiseHT = new DeviseField();
485
 
486
        // Total
487
        DeviseField fieldDevise = new DeviseField();
488
        DeviseField fieldService = new DeviseField();
489
        DeviseField fieldHA = new DeviseField();
490
        fieldHA.setOpaque(false);
491
        fieldService.setOpaque(false);
492
        if (getTable().contains("TOTAL_DEVISE")) {
493
            addSQLObject(fieldDevise, "TOTAL_DEVISE");
494
            addRequiredSQLObject(fieldService, "TOTAL_SERVICE");
495
        }
496
        if (getTable().contains("PREBILAN")) {
497
            addSQLObject(fieldHA, "PREBILAN");
498
        } else if (getTable().contains("T_HA")) {
499
            addSQLObject(fieldHA, "T_HA");
94 ilm 500
            this.allowEditable("T_HA", false);
93 ilm 501
        }
94 ilm 502
        // Disable
93 ilm 503
 
504
        SQLRequestComboBox boxTaxePort = new SQLRequestComboBox(false, 8);
505
 
18 ilm 506
        // Poids Total
507
        c.gridy++;
508
        c.gridx = 1;
509
        c.weightx = 0;
510
        c.weighty = 0;
511
        c.anchor = GridBagConstraints.EAST;
512
        c.gridwidth = 1;
513
        c.fill = GridBagConstraints.NONE;
73 ilm 514
        this.addSQLObject(this.textPoidsTotal, "TOTAL_POIDS");
142 ilm 515
        this.addSQLObject(fieldEco, "T_ECO_CONTRIBUTION");
73 ilm 516
        this.addRequiredSQLObject(this.textTotalHT, "TOTAL_HT");
517
        this.addRequiredSQLObject(this.textTotalTVA, "TOTAL_TVA");
518
        this.addRequiredSQLObject(this.textTotalTTC, "TOTAL_TTC");
142 ilm 519
        this.allowEditable("T_ECO_CONTRIBUTION", false);
94 ilm 520
        this.allowEditable("TOTAL_HT", false);
521
        this.allowEditable("TOTAL_TVA", false);
522
        this.allowEditable("TOTAL_TTC", false);
523
        this.allowEditable("TOTAL_POIDS", false);
142 ilm 524
        final TotalPanel panelTotal = new TotalPanel(tableBonItem, fieldEco, textTotalHT, textTotalTVA, textTotalTTC, textPortHT, textRemiseHT, fieldService, fieldHA, fieldDevise, this.textPoidsTotal,
525
                null, (getTable().contains("ID_TAXE_PORT") ? boxTaxePort : null), null);
93 ilm 526
        // if (b) {
527
        JPanel panel = new JPanel(new GridBagLayout());
528
        GridBagConstraints cFrais = new DefaultGridBagConstraints();
529
        panel.add(new JLabel(getLabelFor("TOTAL_POIDS")), cFrais);
530
 
531
        this.textPoidsTotal.setEnabled(false);
532
        this.textPoidsTotal.setHorizontalAlignment(JTextField.RIGHT);
533
        this.textPoidsTotal.setDisabledTextColor(Color.BLACK);
534
        cFrais.gridx++;
535
        panel.add(this.textPoidsTotal, cFrais);
536
 
537
        panel.setOpaque(false);
538
        DefaultGridBagConstraints.lockMinimumSize(panel);
539
 
540
        DefaultGridBagConstraints.lockMinimumSize(textPortHT);
541
        addSQLObject(textPortHT, "PORT_HT");
542
        DefaultGridBagConstraints.lockMinimumSize(textRemiseHT);
543
        addSQLObject(textRemiseHT, "REMISE_HT");
544
 
545
        // Frais de port
546
 
547
        if (getTable().contains("ID_TAXE_PORT")) {
548
 
549
            JLabel labelPortHT = new JLabel(getLabelFor("PORT_HT"));
550
            labelPortHT.setHorizontalAlignment(SwingConstants.RIGHT);
551
            cFrais.gridx = 0;
552
            cFrais.gridy++;
553
            panel.add(labelPortHT, cFrais);
554
            cFrais.gridx++;
555
            panel.add(textPortHT, cFrais);
556
 
557
            JLabel labelTaxeHT = new JLabel(getLabelFor("ID_TAXE_PORT"));
558
            labelTaxeHT.setHorizontalAlignment(SwingConstants.RIGHT);
559
            cFrais.gridx = 0;
560
            cFrais.gridy++;
561
            panel.add(labelTaxeHT, cFrais);
562
            cFrais.gridx++;
563
            panel.add(boxTaxePort, cFrais);
564
            this.addView(boxTaxePort, "ID_TAXE_PORT", REQ);
565
 
566
            boxTaxePort.addValueListener(new PropertyChangeListener() {
567
 
568
                @Override
569
                public void propertyChange(PropertyChangeEvent evt) {
570
                    panelTotal.updateTotal();
571
                }
572
            });
573
        }
574
 
575
        // Remise
576
        JLabel labelRemiseHT = new JLabel(getLabelFor("REMISE_HT"));
577
        labelRemiseHT.setHorizontalAlignment(SwingConstants.RIGHT);
578
        cFrais.gridy++;
579
        cFrais.gridx = 0;
580
        panel.add(labelRemiseHT, cFrais);
581
        cFrais.gridx++;
582
        panel.add(textRemiseHT, cFrais);
583
 
584
        c.gridx = 1;
585
        c.weightx = 0;
586
        c.weighty = 0;
587
        c.gridwidth = 1;
588
        c.fill = GridBagConstraints.NONE;
589
        c.anchor = GridBagConstraints.NORTHEAST;
590
        this.add(panel, c);
591
 
18 ilm 592
        c.gridx = 2;
593
        c.gridwidth = GridBagConstraints.REMAINDER;
594
        c.weightx = 0;
595
        c.weighty = 0;
596
        c.anchor = GridBagConstraints.EAST;
597
        c.fill = GridBagConstraints.HORIZONTAL;
73 ilm 598
        this.add(panelTotal, c);
18 ilm 599
 
600
        c.anchor = GridBagConstraints.WEST;
601
 
142 ilm 602
        if (getTable().getDBRoot().contains("RELIQUAT_BL")) {
603
 
604
            c.gridwidth = GridBagConstraints.REMAINDER;
605
            c.weightx = 1;
606
            c.fill = GridBagConstraints.HORIZONTAL;
607
            c.gridx = 0;
608
            c.gridy++;
609
            TitledSeparator sep = new TitledSeparator("Reliquat de kits");
610
            c.insets = new Insets(10, 2, 1, 2);
611
            this.add(sep, c);
612
            c.insets = new Insets(2, 2, 1, 2);
613
 
614
            // Reliquat du bon
615
            this.tableBonReliquatItem = new ReliquatRowValuesTable("RELIQUAT_BL");
616
            c.gridx = 0;
617
            c.gridy++;
618
            c.weightx = 1;
619
            c.weighty = 1;
620
            c.gridwidth = GridBagConstraints.REMAINDER;
621
            c.fill = GridBagConstraints.BOTH;
622
            this.add(this.tableBonReliquatItem, c);
623
            this.tableBonItem.setReliquatTable(tableBonReliquatItem);
624
        }
625
 
18 ilm 626
        /*******************************************************************************************
627
         * * INFORMATIONS COMPLEMENTAIRES
628
         ******************************************************************************************/
629
        c.gridwidth = GridBagConstraints.REMAINDER;
630
        c.weightx = 1;
631
        c.fill = GridBagConstraints.HORIZONTAL;
632
        c.gridx = 0;
633
        c.gridy++;
93 ilm 634
        TitledSeparator sep = new TitledSeparator(getLabelFor("INFOS"));
18 ilm 635
        c.insets = new Insets(10, 2, 1, 2);
636
        this.add(sep, c);
637
        c.insets = new Insets(2, 2, 1, 2);
638
 
639
        ITextArea textInfos = new ITextArea(4, 4);
640
 
641
        c.gridx = 0;
642
        c.gridy++;
643
        c.gridheight = 1;
644
        c.gridwidth = GridBagConstraints.REMAINDER;
645
        c.weightx = 1;
646
        c.weighty = 0;
647
        c.fill = GridBagConstraints.BOTH;
648
 
649
        final JScrollPane scrollPane = new JScrollPane(textInfos);
650
        this.add(scrollPane, c);
651
        textInfos.setBorder(null);
652
        DefaultGridBagConstraints.lockMinimumSize(scrollPane);
653
 
654
        c.gridx = 0;
655
        c.gridy++;
656
        c.gridheight = 1;
657
        c.gridwidth = 4;
658
        c.weightx = 0;
659
        c.weighty = 0;
660
        c.fill = GridBagConstraints.NONE;
661
        c.anchor = GridBagConstraints.EAST;
662
 
21 ilm 663
        this.panelOO = new PanelOOSQLComponent(this);
664
        this.add(this.panelOO, c);
665
 
18 ilm 666
        this.addRequiredSQLObject(date, "DATE");
667
        this.addSQLObject(textInfos, "INFOS");
668
        this.addSQLObject(this.textNom, "NOM");
669
        this.addSQLObject(this.selectCommande, "ID_COMMANDE_CLIENT");
670
        this.addRequiredSQLObject(this.textNumeroUnique, "NUMERO");
73 ilm 671
 
18 ilm 672
        // Doit etre locké a la fin
673
        DefaultGridBagConstraints.lockMinimumSize(comboClient);
674
 
93 ilm 675
        textPortHT.getDocument().addDocumentListener(new DocumentListener() {
676
            public void changedUpdate(DocumentEvent e) {
677
                panelTotal.updateTotal();
678
            }
679
 
680
            public void removeUpdate(DocumentEvent e) {
681
                panelTotal.updateTotal();
682
            }
683
 
684
            public void insertUpdate(DocumentEvent e) {
685
                panelTotal.updateTotal();
686
            }
687
        });
688
 
689
        textRemiseHT.getDocument().addDocumentListener(new DocumentListener() {
690
            public void changedUpdate(DocumentEvent e) {
691
                panelTotal.updateTotal();
692
            }
693
 
694
            public void removeUpdate(DocumentEvent e) {
695
                panelTotal.updateTotal();
696
            }
697
 
698
            public void insertUpdate(DocumentEvent e) {
699
                panelTotal.updateTotal();
700
            }
701
        });
702
 
18 ilm 703
    }
704
 
28 ilm 705
    public BonDeLivraisonItemTable getTableBonItem() {
706
        return this.tableBonItem;
707
    }
708
 
18 ilm 709
    private void reconfigure(JTextField field) {
710
        field.setEnabled(false);
711
        field.setHorizontalAlignment(JTextField.RIGHT);
712
        field.setDisabledTextColor(Color.BLACK);
713
        field.setBorder(null);
714
    }
715
 
716
    public int insert(SQLRow order) {
717
 
718
        int idBon = getSelectedID();
142 ilm 719
        int attempt = 0;
720
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
721
        if (!this.textNumeroUnique.checkValidation(false)) {
722
            while (attempt < JUniqueTextField.RETRY_COUNT) {
723
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), date.getDate());
724
                this.textNumeroUnique.setText(num);
725
                attempt++;
726
                if (this.textNumeroUnique.checkValidation(false)) {
727
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
728
                    break;
729
                }
730
                try {
731
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
732
                } catch (InterruptedException e) {
733
                    e.printStackTrace();
734
                }
735
            }
736
        }
737
        final String num = this.textNumeroUnique.getText();
738
        if (attempt == JUniqueTextField.RETRY_COUNT) {
739
            idBon = getSelectedID();
156 ilm 740
            ExceptionHandler.handle(TM.tr("deliveryForm.cannotAdd.existingNumber")); //$NON-NLS-1$
142 ilm 741
            final Object root = SwingUtilities.getRoot(this);
742
            if (root instanceof EditFrame) {
743
                final EditFrame frame = (EditFrame) root;
744
                frame.getPanel().setAlwaysVisible(true);
745
            }
746
        } else {
156 ilm 747
            if (getTable().getDBRoot().contains("TARIF_AGENCE")) {
748
                final List<ProductComponent> comps = checkStock();
749
                if (comps.size() > 0) {
750
                    String article = "";
751
                    for (ProductComponent comp : comps) {
752
                        article += "\n " + comp.getProduct().getString("CODE") + " --- " + comp.getProduct().getString("NOM");
753
                    }
754
                    JOptionPane.showMessageDialog(null, TM.tr("deliveryForm.cannotCreate.notInStock") + article); //$NON-NLS-1$
755
                    final Object root = SwingUtilities.getRoot(this);
756
                    if (root instanceof EditFrame) {
757
                        final EditFrame frame = (EditFrame) root;
758
                        frame.getPanel().setAlwaysVisible(true);
759
                    }
760
                    return -1;
761
                }
762
            }
18 ilm 763
            idBon = super.insert(order);
142 ilm 764
            if (this.tableBonReliquatItem != null) {
765
                this.tableBonReliquatItem.updateField("ID_BON_DE_LIVRAISON_ORIGINE", idBon);
766
            }
18 ilm 767
            this.tableBonItem.updateField("ID_BON_DE_LIVRAISON", idBon);
768
            this.tableBonItem.createArticle(idBon, this.getElement());
156 ilm 769
            final List<Object> cmdDourceTrRowsFrom = ((BonDeLivraisonSQLElement) getElement()).getSourceTrRowsFrom(idBon, "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
770
            ((BonDeLivraisonSQLElement) getElement()).updateQteLivree(cmdDourceTrRowsFrom, "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
771
            final List<Object> devisSourceTrRowsFrom = ((BonDeLivraisonSQLElement) getElement()).getSourceTrRowsFrom(idBon, "DEVIS_ELEMENT", "DEVIS");
772
            ((BonDeLivraisonSQLElement) getElement()).updateQteLivree(devisSourceTrRowsFrom, "DEVIS_ELEMENT", "DEVIS");
18 ilm 773
            // generation du document
774
            BonLivraisonXmlSheet bSheet = new BonLivraisonXmlSheet(getTable().getRow(idBon));
25 ilm 775
            bSheet.createDocumentAsynchronous();
776
            bSheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
18 ilm 777
 
778
            // incrémentation du numéro auto
93 ilm 779
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass()).equalsIgnoreCase(this.textNumeroUnique.getText().trim())) {
18 ilm 780
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
781
                int val = this.tableNum.getRow(2).getInt("BON_L_START");
782
                val++;
783
                rowVals.put("BON_L_START", new Integer(val));
784
 
785
                try {
786
                    rowVals.update(2);
787
                } catch (SQLException e) {
788
                    e.printStackTrace();
789
                }
790
            }
791
 
61 ilm 792
            SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
793
 
794
            if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
795
 
73 ilm 796
                try {
797
                    updateStock(idBon);
798
                } catch (SQLException e) {
799
                    throw new IllegalStateException(e);
800
                }
61 ilm 801
            }
18 ilm 802
            // updateQte(idBon);
142 ilm 803
            if (attempt > 0) {
804
                SwingUtilities.invokeLater(new Runnable() {
805
                    public void run() {
156 ilm 806
                        JOptionPane.showMessageDialog(null, TM.tr("deliveryForm.numberRefreshed", num)); //$NON-NLS-1$
142 ilm 807
                    }
808
                });
18 ilm 809
            }
810
        }
811
 
812
        return idBon;
813
    }
814
 
156 ilm 815
    private List<ProductComponent> checkStock() {
816
        List<ProductComponent> comps = new ArrayList<ProductComponent>();
817
        final RowValuesTableModel rowValuesTableModel = this.tableBonItem.getRowValuesTable().getRowValuesTableModel();
818
 
819
        for (int i = 0; i < rowValuesTableModel.getRowCount(); i++) {
820
            final SQLRowValues rowValuesAt = rowValuesTableModel.getRowValuesAt(i);
821
            if (rowValuesAt.getInt("QTE_LIVREE") > 0 && rowValuesAt != null && rowValuesAt.getObject("ID_ARTICLE") != null && !rowValuesAt.isForeignEmpty("ID_ARTICLE")) {
822
                SQLRowAccessor article = rowValuesAt.getForeign("ID_ARTICLE");
823
                if (article.getBoolean("GESTION_STOCK")) {
824
                    ProductComponent comp = ProductComponent.createFrom(rowValuesAt);
825
                    if (comp.getStock() != null && !comp.getStock().isUndefined() && comp.getStock().getFloat("QTE_REEL") < 0) {
826
                        comps.add(comp);
827
                    }
828
                }
829
            }
830
        }
831
        return comps;
832
    }
833
 
18 ilm 834
    @Override
835
    public void select(SQLRowAccessor r) {
28 ilm 836
        if (r == null || r.getIDNumber() == null)
837
            super.select(r);
838
        else {
839
            System.err.println(r);
132 ilm 840
            final SQLRowValues rVals = r.asRowValues().deepCopy();
28 ilm 841
            final SQLRowValues vals = new SQLRowValues(r.getTable());
842
            vals.load(rVals, createSet("ID_CLIENT"));
843
            vals.setID(rVals.getID());
844
            System.err.println("Select CLIENT");
845
            super.select(vals);
846
            rVals.remove("ID_CLIENT");
847
            super.select(rVals);
848
        }
142 ilm 849
        if (this.tableBonReliquatItem != null) {
850
            this.tableBonReliquatItem.getRowValuesTable().clear();
851
            if (r != null) {
852
                this.tableBonReliquatItem.getRowValuesTable().insertFrom("ID_BON_LIVRAISON_ORIGINE", r.asRowValues());
853
            }
854
        }
18 ilm 855
    }
856
 
857
    @Override
858
    public void update() {
859
        if (!this.textNumeroUnique.checkValidation()) {
156 ilm 860
            ExceptionHandler.handle(TM.tr("deliveryForm.cannotAdd.existingNumber")); //$NON-NLS-1$
18 ilm 861
            Object root = SwingUtilities.getRoot(this);
862
            if (root instanceof EditFrame) {
863
                EditFrame frame = (EditFrame) root;
864
                frame.getPanel().setAlwaysVisible(true);
865
            }
866
            return;
867
        }
868
        super.update();
142 ilm 869
        if (tableBonReliquatItem != null) {
870
            this.tableBonReliquatItem.updateField("ID_BON_DE_LIVRAISON_ORIGINE", getSelectedID());
871
        }
156 ilm 872
        final List<Object> cmdClientFrom = ((BonDeLivraisonSQLElement) getElement()).getSourceTrRowsFrom(getSelectedID(), "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
873
        final List<Object> devisFrom = ((BonDeLivraisonSQLElement) getElement()).getSourceTrRowsFrom(getSelectedID(), "DEVIS_ELEMENT", "DEVIS");
18 ilm 874
        this.tableBonItem.updateField("ID_BON_DE_LIVRAISON", getSelectedID());
875
        this.tableBonItem.createArticle(getSelectedID(), this.getElement());
156 ilm 876
        ((BonDeLivraisonSQLElement) getElement()).updateQteLivree(devisFrom, "DEVIS_ELEMENT", "DEVIS");
877
        ((BonDeLivraisonSQLElement) getElement()).updateQteLivree(cmdClientFrom, "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
18 ilm 878
 
879
        // generation du document
880
        BonLivraisonXmlSheet bSheet = new BonLivraisonXmlSheet(getTable().getRow(getSelectedID()));
25 ilm 881
        bSheet.createDocumentAsynchronous();
882
        bSheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
883
 
61 ilm 884
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
885
        if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
886
 
73 ilm 887
            try {
888
                updateStock(getSelectedID());
889
            } catch (SQLException e) {
890
                throw new IllegalStateException(e);
891
            }
61 ilm 892
        }
893
 
18 ilm 894
    }
895
 
132 ilm 896
    /**
897
     * Chargement des qtés restantes à livrer
898
     *
899
     * @param l
900
     */
156 ilm 901
    public void loadQuantity(List<SQLRowValues> l, final String fromTableElt) {
132 ilm 902
        Map<Integer, SQLRowValues> map = new HashMap<Integer, SQLRowValues>();
903
        for (SQLRowValues sqlRowValues : l) {
156 ilm 904
            // On ne prend en compte que les articles ou les lignes différentes de 0, autres lignes
905
            // considérées comme commentaires
906
            if (!sqlRowValues.isForeignEmpty("ID_ARTICLE") || sqlRowValues.getBigDecimal("PV_HT").signum() != 0) {
907
                if (!sqlRowValues.isForeignEmpty("ID_" + fromTableElt)) {
908
                    final int foreignID = sqlRowValues.getForeignID("ID_" + fromTableElt);
909
                    if (!map.containsKey(foreignID)) {
910
                        map.put(foreignID, sqlRowValues);
911
                    } else {
912
                        SQLRowValues vals = map.get(foreignID);
913
                        if (sqlRowValues.getInt("QTE_LIVREE") > 0) {
914
                            if (NumberUtils.areNumericallyEqual(sqlRowValues.getBigDecimal("QTE_UNITAIRE"), BigDecimal.ONE) || sqlRowValues.getInt("QTE_LIVREE") > 1) {
915
                                vals.put("QTE_LIVREE", vals.getInt("QTE_LIVREE") + sqlRowValues.getInt("QTE_LIVREE"));
916
                            } else {
917
                                vals.put("QTE_UNITAIRE", vals.getBigDecimal("QTE_UNITAIRE").add(sqlRowValues.getBigDecimal("QTE_UNITAIRE")));
918
                            }
132 ilm 919
                        }
920
                    }
921
                }
922
            }
923
        }
924
        int count = this.tableBonItem.getModel().getRowCount();
925
        for (int i = 0; i < count; i++) {
926
            final SQLRowValues rowValuesAt = this.tableBonItem.getModel().getRowValuesAt(i);
927
            rowValuesAt.put("QTE_LIVREE", rowValuesAt.getObject("QTE"));
928
        }
929
 
930
        for (int i = 0; i < count; i++) {
931
            SQLRowValues r = this.tableBonItem.getModel().getRowValuesAt(i);
156 ilm 932
            SQLRowValues rowTR = map.get(r.getForeignID("ID_" + fromTableElt));
132 ilm 933
            if (rowTR != null && !rowTR.isUndefined()) {
934
                if (r.getInt("QTE_LIVREE") > 0 && rowTR.getInt("QTE_LIVREE") > 0) {
935
                    if (NumberUtils.areNumericallyEqual(r.getBigDecimal("QTE_UNITAIRE"), BigDecimal.ONE) || r.getInt("QTE_LIVREE") > 1) {
936
                        this.tableBonItem.getModel().putValue(r.getInt("QTE_LIVREE") - rowTR.getInt("QTE_LIVREE"), i, "QTE_LIVREE");
937
                    } else {
938
                        this.tableBonItem.getModel().putValue(r.getBigDecimal("QTE_UNITAIRE").subtract(rowTR.getBigDecimal("QTE_UNITAIRE")), i, "QTE_UNITAIRE");
939
                    }
940
                }
941
            } else {
942
                this.tableBonItem.getModel().putValue(r.getObject("QTE"), i, "QTE_LIVREE");
943
            }
944
        }
945
    }
946
 
18 ilm 947
    /***********************************************************************************************
948
     * Mise à jour des quantités livrées dans les élements de facture
949
     *
950
     * @param idBon id du bon de livraison
73 ilm 951
     * @throws SQLException
18 ilm 952
     */
73 ilm 953
    public void updateQte(int idBon) throws SQLException {
18 ilm 954
 
156 ilm 955
        SQLTable tableFactureElem = getDirectory().getElement(SaisieVenteFactureItemSQLElement.class).getTable();
956
        SQLSelect selBonItem = new SQLSelect();
957
        BonDeLivraisonItemSQLElement bonElt = getDirectory().getElement(BonDeLivraisonItemSQLElement.class);
18 ilm 958
        selBonItem.addSelect(bonElt.getTable().getField("ID_SAISIE_VENTE_FACTURE_ELEMENT"));
959
        selBonItem.addSelect(bonElt.getTable().getField("QTE_LIVREE"));
65 ilm 960
        selBonItem.setWhere(bonElt.getTable().getField("ID_BON_DE_LIVRAISON"), "=", idBon);
18 ilm 961
 
962
        String reqBonItem = selBonItem.asString();
963
        Object obBonItem = getTable().getBase().getDataSource().execute(reqBonItem, new ArrayListHandler());
964
 
965
        final List<Object[]> myListBonItem = (List<Object[]>) obBonItem;
966
        final int size = myListBonItem.size();
73 ilm 967
 
968
        for (int i = 0; i < size; i++) {
969
            final Object[] objTmp = myListBonItem.get(i);
970
            final SQLRow rowFactElem = tableFactureElem.getRow(((Number) objTmp[0]).intValue());
971
            final SQLRowValues rowVals = new SQLRowValues(tableFactureElem);
972
            rowVals.put("QTE_LIVREE", Integer.valueOf(rowFactElem.getInt("QTE_LIVREE") + ((Number) objTmp[1]).intValue()));
973
            rowVals.update(rowFactElem.getID());
18 ilm 974
        }
975
 
976
    }
977
 
978
    /***********************************************************************************************
979
     * Mise à jour des quantités livrées dans les élements de facture
980
     *
981
     * @param idBon id du bon de livraison
73 ilm 982
     * @throws SQLException
18 ilm 983
     */
73 ilm 984
    public void cancelUpdateQte(int idBon) throws SQLException {
18 ilm 985
 
156 ilm 986
        SQLTable tableFactureElem = getDirectory().getElement(SaisieVenteFactureItemSQLElement.class).getTable();
987
        SQLSelect selBonItem = new SQLSelect();
988
        BonDeLivraisonItemSQLElement bonElt = getDirectory().getElement(BonDeLivraisonItemSQLElement.class);
18 ilm 989
        selBonItem.addSelect(bonElt.getTable().getField("ID_SAISIE_VENTE_FACTURE_ELEMENT"));
990
        selBonItem.addSelect(bonElt.getTable().getField("QTE_LIVREE"));
65 ilm 991
        selBonItem.setWhere(bonElt.getTable().getField("ID_BON_DE_LIVRAISON"), "=", idBon);
18 ilm 992
 
993
        String reqBonItem = selBonItem.asString();
994
        Object obBonItem = getTable().getBase().getDataSource().execute(reqBonItem, new ArrayListHandler());
995
 
996
        final List<Object[]> myListBonItem = (List<Object[]>) obBonItem;
997
        final int size = myListBonItem.size();
73 ilm 998
 
999
        for (int i = 0; i < size; i++) {
1000
            final Object[] objTmp = myListBonItem.get(i);
1001
            final SQLRow rowFactElem = tableFactureElem.getRow(((Number) objTmp[0]).intValue());
1002
            final SQLRowValues rowVals = new SQLRowValues(tableFactureElem);
1003
            rowVals.put("QTE_LIVREE", Integer.valueOf(((Number) objTmp[1]).intValue() - rowFactElem.getInt("QTE_LIVREE")));
1004
            rowVals.update(rowFactElem.getID());
18 ilm 1005
        }
1006
 
1007
    }
1008
 
83 ilm 1009
    protected String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
61 ilm 1010
        return "BL N°" + row.getString("NUMERO");
1011
    }
1012
 
1013
    /**
1014
     * Mise à jour des stocks pour chaque article composant la facture
73 ilm 1015
     *
1016
     * @throws SQLException
61 ilm 1017
     */
73 ilm 1018
    private void updateStock(int id) throws SQLException {
61 ilm 1019
 
1020
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
1021
        if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
1022
 
83 ilm 1023
            SQLRow row = getTable().getRow(id);
1024
            StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
61 ilm 1025
                @Override
83 ilm 1026
                public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
61 ilm 1027
                    return getLibelleStock(rowOrigin, rowElt);
1028
                }
93 ilm 1029
            }, row, row.getReferentRows(getTable().getTable("BON_DE_LIVRAISON_ELEMENT")),
1030
                    getTable().contains("CREATE_VIRTUAL_STOCK") && row.getBoolean("CREATE_VIRTUAL_STOCK") ? TypeStockUpdate.REAL_VIRTUAL_DELIVER : TypeStockUpdate.REAL_DELIVER);
83 ilm 1031
 
142 ilm 1032
            if (getTable().getDBRoot().contains("RELIQUAT_BL")) {
1033
                List<SQLRow> l = row.getReferentRows(getTable().getTable("RELIQUAT_BL").getField("ID_BON_DE_LIVRAISON_ORIGINE"));
1034
                for (SQLRow sqlRow : l) {
1035
                    stockUpdater.addReliquat(sqlRow.getForeign("ID_ARTICLE"), sqlRow.getInt("QTE"), sqlRow.getBigDecimal("QTE_UNITAIRE"));
1036
                }
1037
            }
1038
 
83 ilm 1039
            stockUpdater.update();
61 ilm 1040
        }
1041
    }
1042
 
156 ilm 1043
    public void duplicate(final int idCmd) {
1044
 
1045
        final SQLElement cmd = Configuration.getInstance().getDirectory().getElement("BON_DE_LIVRAISON");
1046
        final SQLElement cmdElt = Configuration.getInstance().getDirectory().getElement("BON_DE_LIVRAISON_ELEMENT");
1047
 
1048
        if (idCmd > 1) {
1049
            final SQLRow row = cmd.getTable().getRow(idCmd);
1050
            final SQLRowValues rowVals = new SQLRowValues(cmd.getTable());
1051
            rowVals.put("ID_CLIENT", row.getInt("ID_CLIENT"));
1052
            rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
1053
 
1054
            this.select(rowVals);
1055
        }
1056
 
1057
        final List<SQLRow> myListItem = cmd.getTable().getRow(idCmd).getReferentRows(cmdElt.getTable());
1058
 
1059
        if (myListItem.size() != 0) {
1060
            getRowValuesTable().getRowValuesTableModel().clearRows();
1061
 
1062
            for (final SQLRow rowElt : myListItem) {
1063
 
1064
                final SQLRowValues rowVals = rowElt.createUpdateRow();
1065
                rowVals.clearPrimaryKeys();
1066
                rowVals.putEmptyLink("ID_COMMANDE_CLIENT_ELEMENT");
1067
                getRowValuesTable().getRowValuesTableModel().addRow(rowVals);
1068
                final int rowIndex = getRowValuesTable().getRowValuesTableModel().getRowCount() - 1;
1069
                getRowValuesTable().getRowValuesTableModel().fireTableModelModified(rowIndex);
1070
            }
1071
        } else {
1072
            getRowValuesTable().getRowValuesTableModel().clearRows();
1073
        }
1074
        getRowValuesTable().getRowValuesTableModel().fireTableDataChanged();
1075
        getRowValuesTable().repaint();
1076
 
1077
    }
1078
 
142 ilm 1079
    public void loadFromReliquat(List<SQLRowValues> l) {
1080
        this.tableBonItem.insertFromReliquat(l);
1081
        this.tableBonItem.setEnabled(false);
1082
    }
1083
 
93 ilm 1084
    @Override
1085
    protected void refreshAfterSelect(SQLRowAccessor rSource) {
1086
 
1087
        tableBonItem.setDateDevise(date.getValue());
1088
    }
1089
 
18 ilm 1090
}