OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Rev 142 | 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;
18 ilm 17
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
63 ilm 18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18 ilm 19
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
20
import org.openconcerto.erp.core.common.ui.DeviseField;
73 ilm 21
import org.openconcerto.erp.core.common.ui.TotalPanel;
93 ilm 22
import org.openconcerto.erp.core.customerrelationship.customer.ui.AddressChoiceUI;
23
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseType;
24
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 25
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureItemSQLElement;
26
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonItemSQLElement;
27
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
28
import org.openconcerto.erp.core.sales.shipment.ui.BonDeLivraisonItemTable;
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;
61 ilm 31
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
21 ilm 32
import org.openconcerto.erp.panel.PanelOOSQLComponent;
61 ilm 33
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
93 ilm 34
import org.openconcerto.erp.preferences.GestionClientPreferencePanel;
94 ilm 35
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 36
import org.openconcerto.sql.Configuration;
37
import org.openconcerto.sql.element.SQLElement;
38
import org.openconcerto.sql.model.SQLRow;
39
import org.openconcerto.sql.model.SQLRowAccessor;
40
import org.openconcerto.sql.model.SQLRowValues;
41
import org.openconcerto.sql.model.SQLSelect;
42
import org.openconcerto.sql.model.SQLTable;
61 ilm 43
import org.openconcerto.sql.model.Where;
44
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 45
import org.openconcerto.sql.sqlobject.ElementComboBox;
46
import org.openconcerto.sql.sqlobject.JUniqueTextField;
28 ilm 47
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
18 ilm 48
import org.openconcerto.sql.view.EditFrame;
73 ilm 49
import org.openconcerto.sql.view.list.RowValuesTable;
18 ilm 50
import org.openconcerto.sql.view.list.RowValuesTableModel;
51
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 52
import org.openconcerto.ui.FormLayouter;
18 ilm 53
import org.openconcerto.ui.JDate;
54
import org.openconcerto.ui.TitledSeparator;
55
import org.openconcerto.ui.component.ITextArea;
56
import org.openconcerto.utils.ExceptionHandler;
132 ilm 57
import org.openconcerto.utils.NumberUtils;
18 ilm 58
 
59
import java.awt.Color;
60
import java.awt.GridBagConstraints;
61
import java.awt.GridBagLayout;
62
import java.awt.Insets;
63
import java.awt.event.ActionEvent;
64
import java.awt.event.ActionListener;
19 ilm 65
import java.beans.PropertyChangeEvent;
66
import java.beans.PropertyChangeListener;
132 ilm 67
import java.math.BigDecimal;
18 ilm 68
import java.sql.SQLException;
69
import java.util.ArrayList;
132 ilm 70
import java.util.HashMap;
18 ilm 71
import java.util.List;
132 ilm 72
import java.util.Map;
18 ilm 73
 
74
import javax.swing.JButton;
93 ilm 75
import javax.swing.JCheckBox;
18 ilm 76
import javax.swing.JLabel;
19 ilm 77
import javax.swing.JOptionPane;
18 ilm 78
import javax.swing.JPanel;
79
import javax.swing.JScrollPane;
80
import javax.swing.JTextField;
81
import javax.swing.SwingConstants;
82
import javax.swing.SwingUtilities;
93 ilm 83
import javax.swing.event.DocumentEvent;
84
import javax.swing.event.DocumentListener;
18 ilm 85
 
86
import org.apache.commons.dbutils.handlers.ArrayListHandler;
87
 
88
public class BonDeLivraisonSQLComponent extends TransfertBaseSQLComponent {
89
    private BonDeLivraisonItemTable tableBonItem;
90
    private ElementComboBox selectCommande, comboClient;
21 ilm 91
    private PanelOOSQLComponent panelOO;
18 ilm 92
    private JUniqueTextField textNumeroUnique;
93
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
94
    private final DeviseField textTotalHT = new DeviseField(6);
95
    private final DeviseField textTotalTVA = new DeviseField(6);
96
    private final DeviseField textTotalTTC = new DeviseField(6);
97
    private final JTextField textPoidsTotal = new JTextField(6);
98
    private final JTextField textNom = new JTextField(25);
93 ilm 99
    private final JDate date = new JDate(true);
100
    private final boolean displayDpt;
101
    private final ElementComboBox comboDpt = new ElementComboBox();
18 ilm 102
 
103
    public BonDeLivraisonSQLComponent() {
104
        super(Configuration.getInstance().getDirectory().getElement("BON_DE_LIVRAISON"));
93 ilm 105
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
106
        this.displayDpt = prefs.getBoolean(GestionClientPreferencePanel.DISPLAY_CLIENT_DPT, false);
18 ilm 107
    }
108
 
109
    @Override
73 ilm 110
    protected RowValuesTable getRowValuesTable() {
111
        return this.tableBonItem.getRowValuesTable();
112
    }
113
 
114
    @Override
18 ilm 115
    protected SQLRowValues createDefaults() {
90 ilm 116
        this.textNumeroUnique.setText(NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
18 ilm 117
        this.tableBonItem.getModel().clearRows();
93 ilm 118
        SQLRowValues rowVals = super.createDefaults();
119
        if (rowVals == null) {
120
            rowVals = new SQLRowValues(getTable());
121
        }
122
        if (getTable().contains("CREATE_VIRTUAL_STOCK")) {
123
            rowVals.put("CREATE_VIRTUAL_STOCK", Boolean.TRUE);
124
        }
125
        if (getTable().contains("ID_TAXE_PORT")) {
126
            SQLRow taxeDefault = TaxeCache.getCache().getFirstTaxe();
127
            rowVals.put("ID_TAXE_PORT", taxeDefault.getID());
128
        }
129
        return rowVals;
18 ilm 130
    }
131
 
132
    public void addViews() {
133
        this.textTotalHT.setOpaque(false);
134
        this.textTotalTVA.setOpaque(false);
135
        this.textTotalTTC.setOpaque(false);
93 ilm 136
        if (getTable().contains("CREATE_VIRTUAL_STOCK")) {
137
            this.addView(new JCheckBox(), "CREATE_VIRTUAL_STOCK");
138
        }
18 ilm 139
        this.selectCommande = new ElementComboBox();
140
 
141
        this.setLayout(new GridBagLayout());
142
 
143
        final GridBagConstraints c = new DefaultGridBagConstraints();
144
 
25 ilm 145
        // Champ Module
146
        c.gridx = 0;
147
        c.gridy++;
148
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 149
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
41 ilm 150
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
25 ilm 151
        this.add(addP, c);
152
 
153
        c.gridy++;
154
        c.gridwidth = 1;
155
 
18 ilm 156
        // Numero
157
        JLabel labelNum = new JLabel(getLabelFor("NUMERO"));
158
        labelNum.setHorizontalAlignment(SwingConstants.RIGHT);
159
        this.add(labelNum, c);
160
 
161
        this.textNumeroUnique = new JUniqueTextField(16);
162
        c.gridx++;
163
        c.weightx = 1;
164
        c.weighty = 0;
165
        c.fill = GridBagConstraints.NONE;
166
        DefaultGridBagConstraints.lockMinimumSize(textNumeroUnique);
167
        this.add(this.textNumeroUnique, c);
168
 
169
        // Date
170
        c.gridx++;
171
        c.fill = GridBagConstraints.HORIZONTAL;
172
        c.weightx = 0;
173
        this.add(new JLabel(getLabelFor("DATE"), SwingConstants.RIGHT), c);
174
 
175
        c.gridx++;
176
        c.weightx = 0;
177
        c.weighty = 0;
178
        c.fill = GridBagConstraints.NONE;
179
        this.add(date, c);
180
 
93 ilm 181
        this.date.addValueListener(new PropertyChangeListener() {
182
 
183
            @Override
184
            public void propertyChange(PropertyChangeEvent evt) {
185
                if (!isFilling() && date.getValue() != null) {
186
                    tableBonItem.setDateDevise(date.getValue());
187
                }
188
            }
189
        });
190
 
18 ilm 191
        // Reference
192
        c.gridy++;
193
        c.gridx = 0;
194
        c.fill = GridBagConstraints.HORIZONTAL;
195
        this.add(new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT), c);
196
        c.gridx++;
197
        c.weightx = 1;
198
        this.add(this.textNom, c);
41 ilm 199
        if (getTable().contains("DATE_LIVRAISON")) {
200
            // Date livraison
201
            c.gridx++;
202
            c.fill = GridBagConstraints.HORIZONTAL;
203
            c.weightx = 0;
204
            this.add(new JLabel(getLabelFor("DATE_LIVRAISON"), SwingConstants.RIGHT), c);
18 ilm 205
 
41 ilm 206
            JDate dateLivraison = new JDate(true);
207
            c.gridx++;
208
            c.weightx = 0;
209
            c.weighty = 0;
210
            c.fill = GridBagConstraints.NONE;
211
            this.add(dateLivraison, c);
212
            this.addView(dateLivraison, "DATE_LIVRAISON");
213
        }
18 ilm 214
        // Client
61 ilm 215
        JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"), SwingConstants.RIGHT);
18 ilm 216
        c.gridx = 0;
217
        c.gridy++;
218
        c.weightx = 0;
61 ilm 219
        c.fill = GridBagConstraints.HORIZONTAL;
18 ilm 220
        c.weighty = 0;
221
        this.add(labelClient, c);
222
 
223
        c.gridx++;
224
        c.weightx = 0;
225
        c.weighty = 0;
226
        c.fill = GridBagConstraints.NONE;
227
        this.comboClient = new ElementComboBox();
41 ilm 228
        this.add(this.comboClient, c);
93 ilm 229
        this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
230
 
231
        if (this.displayDpt) {
232
            c.gridx++;
233
            c.gridwidth = 1;
234
            final JLabel labelDpt = new JLabel(getLabelFor("ID_CLIENT_DEPARTEMENT"));
235
            labelDpt.setHorizontalAlignment(SwingConstants.RIGHT);
236
            c.weightx = 0;
237
            c.gridwidth = 1;
238
            c.fill = GridBagConstraints.HORIZONTAL;
239
            this.add(labelDpt, c);
240
 
241
            c.gridx++;
242
            c.gridwidth = 1;
243
            c.weightx = 0;
244
            c.weighty = 0;
245
            c.fill = GridBagConstraints.NONE;
246
            this.add(this.comboDpt, c);
247
            DefaultGridBagConstraints.lockMinimumSize(this.comboDpt);
248
            addSQLObject(this.comboDpt, "ID_CLIENT_DEPARTEMENT");
249
 
250
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
251
 
252
                @Override
253
                public void propertyChange(PropertyChangeEvent evt) {
254
                    int wantedID = comboClient.getWantedID();
255
 
256
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
257
                        final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
258
                        comboDpt.getRequest().setWhere(new Where(comboDpt.getRequest().getPrimaryTable().getField("ID_CLIENT"), "=", rowClient.getID()));
259
                    } else {
260
                        comboDpt.getRequest().setWhere(null);
261
                    }
262
                }
263
            });
264
 
265
        }
94 ilm 266
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
267
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ADDRESS_SPEC, true)) {
93 ilm 268
 
94 ilm 269
            final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
270
            final AddressChoiceUI addressUI = new AddressChoiceUI();
271
            addressUI.addToUI(this, c);
272
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
93 ilm 273
 
94 ilm 274
                @Override
275
                public void propertyChange(PropertyChangeEvent evt) {
276
                    int wantedID = comboClient.getWantedID();
277
                    System.err.println("SET WHERE ID_CLIENT = " + wantedID);
278
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
93 ilm 279
 
94 ilm 280
                        addressUI.getComboAdrF().getRequest().setWhere(
281
                                new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Invoice.getId())));
282
                        addressUI.getComboAdrL().getRequest().setWhere(
283
                                new Where(adrElement.getTable().getField("ID_CLIENT"), "=", wantedID).and(new Where(adrElement.getTable().getField("TYPE"), "=", AdresseType.Delivery.getId())));
284
                    } else {
285
                        addressUI.getComboAdrF().getRequest().setWhere(Where.FALSE);
286
                        addressUI.getComboAdrL().getRequest().setWhere(Where.FALSE);
287
                    }
93 ilm 288
                }
94 ilm 289
            });
290
        }
41 ilm 291
        if (getTable().contains("SPEC_LIVRAISON")) {
292
            // Date livraison
293
            c.gridx++;
294
            c.fill = GridBagConstraints.HORIZONTAL;
295
            c.weightx = 0;
296
            this.add(new JLabel(getLabelFor("SPEC_LIVRAISON"), SwingConstants.RIGHT), c);
18 ilm 297
 
41 ilm 298
            JTextField specLivraison = new JTextField();
299
            c.gridx++;
300
            c.weightx = 0;
301
            c.weighty = 0;
302
            this.add(specLivraison, c);
303
            this.addView(specLivraison, "SPEC_LIVRAISON");
304
        }
305
 
93 ilm 306
        if (getTable().contains("ID_CONTACT")) {
307
            // Contact Client
308
            c.gridx = 0;
309
            c.gridy++;
310
            c.gridwidth = 1;
311
            final JLabel labelContact = new JLabel(getLabelFor("ID_CONTACT"));
312
            labelContact.setHorizontalAlignment(SwingConstants.RIGHT);
313
            c.weightx = 0;
314
            c.gridwidth = 1;
315
            c.fill = GridBagConstraints.HORIZONTAL;
316
            this.add(labelContact, c);
317
 
318
            final ElementComboBox comboContact = new ElementComboBox();
319
            c.gridx++;
320
            c.gridwidth = 1;
321
            c.weightx = 0;
322
            c.weighty = 0;
323
            c.fill = GridBagConstraints.NONE;
324
            this.add(comboContact, c);
325
            final SQLElement contactElement = getElement().getForeignElement("ID_CONTACT");
326
            comboContact.init(contactElement, contactElement.getComboRequest(true));
327
            comboContact.getRequest().setWhere(Where.FALSE);
328
            DefaultGridBagConstraints.lockMinimumSize(comboContact);
329
            this.addView(comboContact, "ID_CONTACT");
330
            comboClient.addModelListener("wantedID", new PropertyChangeListener() {
331
 
332
                @Override
333
                public void propertyChange(PropertyChangeEvent evt) {
334
                    int wantedID = comboClient.getWantedID();
335
                    System.err.println("SET WHERE ID_CLIENT = " + wantedID);
336
                    if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
337
 
338
                        final SQLRow rowClient = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
339
                        int idClient = rowClient.getID();
340
                        comboContact.getRequest().setWhere(new Where(contactElement.getTable().getField("ID_CLIENT"), "=", idClient));
341
                    } else {
342
                        comboContact.getRequest().setWhere(Where.FALSE);
343
                        // DevisSQLComponent.this.table.setTarif(null, false);
344
                    }
345
                }
346
            });
347
 
348
        }
349
 
19 ilm 350
        final ElementComboBox boxTarif = new ElementComboBox();
41 ilm 351
        this.comboClient.addValueListener(new PropertyChangeListener() {
352
            @Override
353
            public void propertyChange(PropertyChangeEvent evt) {
354
                if (comboClient.getElement().getTable().contains("ID_TARIF")) {
28 ilm 355
                    if (BonDeLivraisonSQLComponent.this.isFilling())
356
                        return;
357
                    final SQLRow row = ((SQLRequestComboBox) evt.getSource()).getSelectedRow();
358
                    if (row != null) {
359
                        // SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
360
                        // if (foreignRow.isUndefined() &&
361
                        // !row.getForeignRow("ID_DEVISE").isUndefined()) {
362
                        // SQLRowValues rowValsD = new SQLRowValues(foreignRow.getTable());
363
                        // rowValsD.put("ID_DEVISE", row.getObject("ID_DEVISE"));
364
                        // foreignRow = rowValsD;
365
                        //
366
                        // }
367
                        // tableBonItem.setTarif(foreignRow, true);
368
                        SQLRowAccessor foreignRow = row.getForeignRow("ID_TARIF");
369
                        if (!foreignRow.isUndefined() && (boxTarif.getSelectedRow() == null || boxTarif.getSelectedId() != foreignRow.getID())
370
                                && JOptionPane.showConfirmDialog(null, "Appliquer les tarifs associés au client?") == JOptionPane.YES_OPTION) {
371
                            boxTarif.setValue(foreignRow.getID());
372
                            // SaisieVenteFactureSQLComponent.this.tableFacture.setTarif(foreignRow,
373
                            // true);
374
                        } else {
375
                            boxTarif.setValue(foreignRow.getID());
19 ilm 376
                        }
377
                    }
378
                }
41 ilm 379
            }
380
        });
19 ilm 381
 
18 ilm 382
        // Bouton tout livrer
383
        JButton boutonAll = new JButton("Tout livrer");
384
 
385
        boutonAll.addActionListener(new ActionListener() {
386
            public void actionPerformed(ActionEvent e) {
387
                RowValuesTableModel m = BonDeLivraisonSQLComponent.this.tableBonItem.getModel();
388
 
389
                // on livre tout les éléments
390
                for (int i = 0; i < m.getRowCount(); i++) {
391
                    SQLRowValues rowVals = m.getRowValuesAt(i);
392
                    Object o = rowVals.getObject("QTE");
393
                    int qte = o == null ? 0 : ((Number) o).intValue();
394
                    m.putValue(qte, i, "QTE_LIVREE");
395
                }
396
            }
397
        });
398
 
19 ilm 399
        // Tarif
400
        if (this.getTable().getFieldsName().contains("ID_TARIF")) {
401
            // TARIF
402
            c.gridy++;
403
            c.gridx = 0;
404
            c.weightx = 0;
405
            c.weighty = 0;
406
            c.gridwidth = 1;
93 ilm 407
            c.fill = GridBagConstraints.HORIZONTAL;
408
            this.add(new JLabel(getLabelFor("ID_TARIF"), SwingUtilities.RIGHT), c);
19 ilm 409
            c.gridx++;
41 ilm 410
            c.gridwidth = 1;
93 ilm 411
            c.fill = GridBagConstraints.NONE;
19 ilm 412
            c.weightx = 1;
413
            this.add(boxTarif, c);
414
            this.addView(boxTarif, "ID_TARIF");
73 ilm 415
            boxTarif.addModelListener("wantedID", new PropertyChangeListener() {
19 ilm 416
 
417
                @Override
418
                public void propertyChange(PropertyChangeEvent evt) {
73 ilm 419
                    SQLRow selectedRow = boxTarif.getRequest().getPrimaryTable().getRow(boxTarif.getWantedID());
420
                    tableBonItem.setTarif(selectedRow, !isFilling());
19 ilm 421
                }
422
            });
423
        }
41 ilm 424
 
425
        if (getTable().contains("A_ATTENTION")) {
426
            // Date livraison
427
            c.gridx++;
428
            c.fill = GridBagConstraints.HORIZONTAL;
429
            c.weightx = 0;
430
            this.add(new JLabel(getLabelFor("A_ATTENTION"), SwingConstants.RIGHT), c);
431
 
432
            JTextField specLivraison = new JTextField();
433
            c.gridx++;
434
            c.weightx = 0;
435
            c.weighty = 0;
436
            this.add(specLivraison, c);
437
            this.addView(specLivraison, "A_ATTENTION");
438
        }
439
 
18 ilm 440
        // Element du bon
441
        List<JButton> l = new ArrayList<JButton>();
442
        l.add(boutonAll);
443
        this.tableBonItem = new BonDeLivraisonItemTable(l);
444
 
445
        c.gridx = 0;
446
        c.gridy++;
447
        c.weightx = 1;
448
        c.weighty = 1;
449
        c.gridwidth = GridBagConstraints.REMAINDER;
450
        c.fill = GridBagConstraints.BOTH;
451
        this.add(this.tableBonItem, c);
452
        c.anchor = GridBagConstraints.EAST;
453
        // Totaux
454
        reconfigure(this.textTotalHT);
455
        reconfigure(this.textTotalTVA);
456
        reconfigure(this.textTotalTTC);
457
 
93 ilm 458
        DeviseField textPortHT = new DeviseField(5);
459
        DeviseField textRemiseHT = new DeviseField();
460
 
461
        // Total
462
        DeviseField fieldDevise = new DeviseField();
463
        DeviseField fieldService = new DeviseField();
464
        DeviseField fieldHA = new DeviseField();
465
        fieldHA.setOpaque(false);
466
        fieldService.setOpaque(false);
467
        if (getTable().contains("TOTAL_DEVISE")) {
468
            addSQLObject(fieldDevise, "TOTAL_DEVISE");
469
            addRequiredSQLObject(fieldService, "TOTAL_SERVICE");
470
        }
471
        if (getTable().contains("PREBILAN")) {
472
            addSQLObject(fieldHA, "PREBILAN");
473
        } else if (getTable().contains("T_HA")) {
474
            addSQLObject(fieldHA, "T_HA");
94 ilm 475
            this.allowEditable("T_HA", false);
93 ilm 476
        }
94 ilm 477
        // Disable
93 ilm 478
 
479
        SQLRequestComboBox boxTaxePort = new SQLRequestComboBox(false, 8);
480
 
18 ilm 481
        // Poids Total
482
        c.gridy++;
483
        c.gridx = 1;
484
        c.weightx = 0;
485
        c.weighty = 0;
486
        c.anchor = GridBagConstraints.EAST;
487
        c.gridwidth = 1;
488
        c.fill = GridBagConstraints.NONE;
73 ilm 489
        this.addSQLObject(this.textPoidsTotal, "TOTAL_POIDS");
490
        this.addRequiredSQLObject(this.textTotalHT, "TOTAL_HT");
491
        this.addRequiredSQLObject(this.textTotalTVA, "TOTAL_TVA");
492
        this.addRequiredSQLObject(this.textTotalTTC, "TOTAL_TTC");
94 ilm 493
        this.allowEditable("TOTAL_HT", false);
494
        this.allowEditable("TOTAL_TVA", false);
495
        this.allowEditable("TOTAL_TTC", false);
496
        this.allowEditable("TOTAL_POIDS", false);
93 ilm 497
        final TotalPanel panelTotal = new TotalPanel(tableBonItem, textTotalHT, textTotalTVA, textTotalTTC, textPortHT, textRemiseHT, fieldService, fieldHA, fieldDevise, this.textPoidsTotal, null,
498
                (getTable().contains("ID_TAXE_PORT") ? boxTaxePort : null));
499
 
500
        // if (b) {
501
        JPanel panel = new JPanel(new GridBagLayout());
502
        GridBagConstraints cFrais = new DefaultGridBagConstraints();
503
        panel.add(new JLabel(getLabelFor("TOTAL_POIDS")), cFrais);
504
 
505
        this.textPoidsTotal.setEnabled(false);
506
        this.textPoidsTotal.setHorizontalAlignment(JTextField.RIGHT);
507
        this.textPoidsTotal.setDisabledTextColor(Color.BLACK);
508
        cFrais.gridx++;
509
        panel.add(this.textPoidsTotal, cFrais);
510
 
511
        panel.setOpaque(false);
512
        DefaultGridBagConstraints.lockMinimumSize(panel);
513
 
514
        DefaultGridBagConstraints.lockMinimumSize(textPortHT);
515
        addSQLObject(textPortHT, "PORT_HT");
516
        DefaultGridBagConstraints.lockMinimumSize(textRemiseHT);
517
        addSQLObject(textRemiseHT, "REMISE_HT");
518
 
519
        // Frais de port
520
 
521
        if (getTable().contains("ID_TAXE_PORT")) {
522
 
523
            JLabel labelPortHT = new JLabel(getLabelFor("PORT_HT"));
524
            labelPortHT.setHorizontalAlignment(SwingConstants.RIGHT);
525
            cFrais.gridx = 0;
526
            cFrais.gridy++;
527
            panel.add(labelPortHT, cFrais);
528
            cFrais.gridx++;
529
            panel.add(textPortHT, cFrais);
530
 
531
            JLabel labelTaxeHT = new JLabel(getLabelFor("ID_TAXE_PORT"));
532
            labelTaxeHT.setHorizontalAlignment(SwingConstants.RIGHT);
533
            cFrais.gridx = 0;
534
            cFrais.gridy++;
535
            panel.add(labelTaxeHT, cFrais);
536
            cFrais.gridx++;
537
            panel.add(boxTaxePort, cFrais);
538
            this.addView(boxTaxePort, "ID_TAXE_PORT", REQ);
539
 
540
            boxTaxePort.addValueListener(new PropertyChangeListener() {
541
 
542
                @Override
543
                public void propertyChange(PropertyChangeEvent evt) {
544
                    panelTotal.updateTotal();
545
                }
546
            });
547
        }
548
 
549
        // Remise
550
        JLabel labelRemiseHT = new JLabel(getLabelFor("REMISE_HT"));
551
        labelRemiseHT.setHorizontalAlignment(SwingConstants.RIGHT);
552
        cFrais.gridy++;
553
        cFrais.gridx = 0;
554
        panel.add(labelRemiseHT, cFrais);
555
        cFrais.gridx++;
556
        panel.add(textRemiseHT, cFrais);
557
 
558
        c.gridx = 1;
559
        c.weightx = 0;
560
        c.weighty = 0;
561
        c.gridwidth = 1;
562
        c.fill = GridBagConstraints.NONE;
563
        c.anchor = GridBagConstraints.NORTHEAST;
564
        this.add(panel, c);
565
 
18 ilm 566
        c.gridx = 2;
567
        c.gridwidth = GridBagConstraints.REMAINDER;
568
        c.weightx = 0;
569
        c.weighty = 0;
570
        c.anchor = GridBagConstraints.EAST;
571
        c.fill = GridBagConstraints.HORIZONTAL;
73 ilm 572
        this.add(panelTotal, c);
18 ilm 573
 
574
        c.anchor = GridBagConstraints.WEST;
575
 
576
        /*******************************************************************************************
577
         * * INFORMATIONS COMPLEMENTAIRES
578
         ******************************************************************************************/
579
        c.gridwidth = GridBagConstraints.REMAINDER;
580
        c.weightx = 1;
581
        c.fill = GridBagConstraints.HORIZONTAL;
582
        c.gridx = 0;
583
        c.gridy++;
93 ilm 584
        TitledSeparator sep = new TitledSeparator(getLabelFor("INFOS"));
18 ilm 585
        c.insets = new Insets(10, 2, 1, 2);
586
        this.add(sep, c);
587
        c.insets = new Insets(2, 2, 1, 2);
588
 
589
        ITextArea textInfos = new ITextArea(4, 4);
590
 
591
        c.gridx = 0;
592
        c.gridy++;
593
        c.gridheight = 1;
594
        c.gridwidth = GridBagConstraints.REMAINDER;
595
        c.weightx = 1;
596
        c.weighty = 0;
597
        c.fill = GridBagConstraints.BOTH;
598
 
599
        final JScrollPane scrollPane = new JScrollPane(textInfos);
600
        this.add(scrollPane, c);
601
        textInfos.setBorder(null);
602
        DefaultGridBagConstraints.lockMinimumSize(scrollPane);
603
 
604
        c.gridx = 0;
605
        c.gridy++;
606
        c.gridheight = 1;
607
        c.gridwidth = 4;
608
        c.weightx = 0;
609
        c.weighty = 0;
610
        c.fill = GridBagConstraints.NONE;
611
        c.anchor = GridBagConstraints.EAST;
612
 
21 ilm 613
        this.panelOO = new PanelOOSQLComponent(this);
614
        this.add(this.panelOO, c);
615
 
18 ilm 616
        this.addRequiredSQLObject(date, "DATE");
617
        this.addSQLObject(textInfos, "INFOS");
618
        this.addSQLObject(this.textNom, "NOM");
619
        this.addSQLObject(this.selectCommande, "ID_COMMANDE_CLIENT");
620
        this.addRequiredSQLObject(this.textNumeroUnique, "NUMERO");
73 ilm 621
 
18 ilm 622
        // Doit etre locké a la fin
623
        DefaultGridBagConstraints.lockMinimumSize(comboClient);
624
 
93 ilm 625
        textPortHT.getDocument().addDocumentListener(new DocumentListener() {
626
            public void changedUpdate(DocumentEvent e) {
627
                panelTotal.updateTotal();
628
            }
629
 
630
            public void removeUpdate(DocumentEvent e) {
631
                panelTotal.updateTotal();
632
            }
633
 
634
            public void insertUpdate(DocumentEvent e) {
635
                panelTotal.updateTotal();
636
            }
637
        });
638
 
639
        textRemiseHT.getDocument().addDocumentListener(new DocumentListener() {
640
            public void changedUpdate(DocumentEvent e) {
641
                panelTotal.updateTotal();
642
            }
643
 
644
            public void removeUpdate(DocumentEvent e) {
645
                panelTotal.updateTotal();
646
            }
647
 
648
            public void insertUpdate(DocumentEvent e) {
649
                panelTotal.updateTotal();
650
            }
651
        });
652
 
18 ilm 653
    }
654
 
28 ilm 655
    public BonDeLivraisonItemTable getTableBonItem() {
656
        return this.tableBonItem;
657
    }
658
 
18 ilm 659
    private void reconfigure(JTextField field) {
660
        field.setEnabled(false);
661
        field.setHorizontalAlignment(JTextField.RIGHT);
662
        field.setDisabledTextColor(Color.BLACK);
663
        field.setBorder(null);
664
    }
665
 
666
    public int insert(SQLRow order) {
667
 
668
        int idBon = getSelectedID();
669
        // on verifie qu'un bon du meme numero n'a pas été inséré entre temps
670
        if (this.textNumeroUnique.checkValidation()) {
671
            idBon = super.insert(order);
672
            this.tableBonItem.updateField("ID_BON_DE_LIVRAISON", idBon);
673
            this.tableBonItem.createArticle(idBon, this.getElement());
674
 
675
            // generation du document
676
            BonLivraisonXmlSheet bSheet = new BonLivraisonXmlSheet(getTable().getRow(idBon));
25 ilm 677
            bSheet.createDocumentAsynchronous();
678
            bSheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
18 ilm 679
 
680
            // incrémentation du numéro auto
93 ilm 681
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass()).equalsIgnoreCase(this.textNumeroUnique.getText().trim())) {
18 ilm 682
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
683
                int val = this.tableNum.getRow(2).getInt("BON_L_START");
684
                val++;
685
                rowVals.put("BON_L_START", new Integer(val));
686
 
687
                try {
688
                    rowVals.update(2);
689
                } catch (SQLException e) {
690
                    e.printStackTrace();
691
                }
692
            }
693
 
61 ilm 694
            SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
695
 
696
            if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
697
 
73 ilm 698
                try {
699
                    updateStock(idBon);
700
                } catch (SQLException e) {
701
                    throw new IllegalStateException(e);
702
                }
61 ilm 703
            }
18 ilm 704
            // updateQte(idBon);
705
        } else {
706
            ExceptionHandler.handle("Impossible d'ajouter, numéro de bon de livraison existant.");
707
            Object root = SwingUtilities.getRoot(this);
708
            if (root instanceof EditFrame) {
709
                EditFrame frame = (EditFrame) root;
710
                frame.getPanel().setAlwaysVisible(true);
711
            }
712
        }
713
 
714
        return idBon;
715
    }
716
 
717
    @Override
718
    public void select(SQLRowAccessor r) {
28 ilm 719
        if (r == null || r.getIDNumber() == null)
720
            super.select(r);
721
        else {
722
            System.err.println(r);
132 ilm 723
            final SQLRowValues rVals = r.asRowValues().deepCopy();
28 ilm 724
            final SQLRowValues vals = new SQLRowValues(r.getTable());
725
            vals.load(rVals, createSet("ID_CLIENT"));
726
            vals.setID(rVals.getID());
727
            System.err.println("Select CLIENT");
728
            super.select(vals);
729
            rVals.remove("ID_CLIENT");
730
            super.select(rVals);
731
        }
18 ilm 732
    }
733
 
734
    @Override
735
    public void update() {
736
        if (!this.textNumeroUnique.checkValidation()) {
737
            ExceptionHandler.handle("Impossible d'ajouter, numéro de bon de livraison existant.");
738
            Object root = SwingUtilities.getRoot(this);
739
            if (root instanceof EditFrame) {
740
                EditFrame frame = (EditFrame) root;
741
                frame.getPanel().setAlwaysVisible(true);
742
            }
743
            return;
744
        }
745
        super.update();
746
        this.tableBonItem.updateField("ID_BON_DE_LIVRAISON", getSelectedID());
747
        this.tableBonItem.createArticle(getSelectedID(), this.getElement());
748
 
749
        // generation du document
750
        BonLivraisonXmlSheet bSheet = new BonLivraisonXmlSheet(getTable().getRow(getSelectedID()));
25 ilm 751
        bSheet.createDocumentAsynchronous();
752
        bSheet.showPrintAndExportAsynchronous(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
753
 
61 ilm 754
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
755
        SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
756
        if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
757
 
73 ilm 758
            try {
759
                updateStock(getSelectedID());
760
            } catch (SQLException e) {
761
                throw new IllegalStateException(e);
762
            }
61 ilm 763
        }
764
 
18 ilm 765
    }
766
 
132 ilm 767
    /**
768
     * Chargement des qtés restantes à livrer
769
     *
770
     * @param l
771
     */
772
    public void loadQuantity(List<SQLRowValues> l) {
773
        Map<Integer, SQLRowValues> map = new HashMap<Integer, SQLRowValues>();
774
        for (SQLRowValues sqlRowValues : l) {
775
            if (!sqlRowValues.isForeignEmpty("ID_ARTICLE")) {
776
                final int foreignID = sqlRowValues.getForeignID("ID_ARTICLE");
777
                if (!map.containsKey(foreignID)) {
778
                    map.put(foreignID, sqlRowValues);
779
                } else {
780
                    SQLRowValues vals = map.get(foreignID);
781
                    if (sqlRowValues.getInt("QTE_LIVREE") > 0) {
782
                        if (NumberUtils.areNumericallyEqual(sqlRowValues.getBigDecimal("QTE_UNITAIRE"), BigDecimal.ONE) || sqlRowValues.getInt("QTE_LIVREE") > 1) {
783
                            vals.put("QTE_LIVREE", vals.getInt("QTE_LIVREE") + sqlRowValues.getInt("QTE_LIVREE"));
784
                        } else {
785
                            vals.put("QTE_UNITAIRE", vals.getBigDecimal("QTE_UNITAIRE").add(sqlRowValues.getBigDecimal("QTE_UNITAIRE")));
786
                        }
787
                    }
788
                }
789
            }
790
        }
791
        int count = this.tableBonItem.getModel().getRowCount();
792
        for (int i = 0; i < count; i++) {
793
            final SQLRowValues rowValuesAt = this.tableBonItem.getModel().getRowValuesAt(i);
794
            rowValuesAt.put("QTE_LIVREE", rowValuesAt.getObject("QTE"));
795
        }
796
 
797
        for (int i = 0; i < count; i++) {
798
            SQLRowValues r = this.tableBonItem.getModel().getRowValuesAt(i);
799
            SQLRowValues rowTR = map.get(r.getForeignID("ID_ARTICLE"));
800
            if (rowTR != null && !rowTR.isUndefined()) {
801
                if (r.getInt("QTE_LIVREE") > 0 && rowTR.getInt("QTE_LIVREE") > 0) {
802
                    if (NumberUtils.areNumericallyEqual(r.getBigDecimal("QTE_UNITAIRE"), BigDecimal.ONE) || r.getInt("QTE_LIVREE") > 1) {
803
                        this.tableBonItem.getModel().putValue(r.getInt("QTE_LIVREE") - rowTR.getInt("QTE_LIVREE"), i, "QTE_LIVREE");
804
                    } else {
805
                        this.tableBonItem.getModel().putValue(r.getBigDecimal("QTE_UNITAIRE").subtract(rowTR.getBigDecimal("QTE_UNITAIRE")), i, "QTE_UNITAIRE");
806
                    }
807
                }
808
            } else {
809
                this.tableBonItem.getModel().putValue(r.getObject("QTE"), i, "QTE_LIVREE");
810
            }
811
        }
812
    }
813
 
18 ilm 814
    /***********************************************************************************************
815
     * Mise à jour des quantités livrées dans les élements de facture
816
     *
817
     * @param idBon id du bon de livraison
73 ilm 818
     * @throws SQLException
18 ilm 819
     */
73 ilm 820
    public void updateQte(int idBon) throws SQLException {
18 ilm 821
 
822
        SQLTable tableFactureElem = new SaisieVenteFactureItemSQLElement().getTable();
823
        SQLSelect selBonItem = new SQLSelect(getTable().getBase());
824
        BonDeLivraisonItemSQLElement bonElt = new BonDeLivraisonItemSQLElement();
825
        selBonItem.addSelect(bonElt.getTable().getField("ID_SAISIE_VENTE_FACTURE_ELEMENT"));
826
        selBonItem.addSelect(bonElt.getTable().getField("QTE_LIVREE"));
65 ilm 827
        selBonItem.setWhere(bonElt.getTable().getField("ID_BON_DE_LIVRAISON"), "=", idBon);
18 ilm 828
 
829
        String reqBonItem = selBonItem.asString();
830
        Object obBonItem = getTable().getBase().getDataSource().execute(reqBonItem, new ArrayListHandler());
831
 
832
        final List<Object[]> myListBonItem = (List<Object[]>) obBonItem;
833
        final int size = myListBonItem.size();
73 ilm 834
 
835
        for (int i = 0; i < size; i++) {
836
            final Object[] objTmp = myListBonItem.get(i);
837
            final SQLRow rowFactElem = tableFactureElem.getRow(((Number) objTmp[0]).intValue());
838
            final SQLRowValues rowVals = new SQLRowValues(tableFactureElem);
839
            rowVals.put("QTE_LIVREE", Integer.valueOf(rowFactElem.getInt("QTE_LIVREE") + ((Number) objTmp[1]).intValue()));
840
            rowVals.update(rowFactElem.getID());
18 ilm 841
        }
842
 
843
    }
844
 
845
    /***********************************************************************************************
846
     * Mise à jour des quantités livrées dans les élements de facture
847
     *
848
     * @param idBon id du bon de livraison
73 ilm 849
     * @throws SQLException
18 ilm 850
     */
73 ilm 851
    public void cancelUpdateQte(int idBon) throws SQLException {
18 ilm 852
 
853
        SQLTable tableFactureElem = new SaisieVenteFactureItemSQLElement().getTable();
854
        SQLSelect selBonItem = new SQLSelect(getTable().getBase());
855
        BonDeLivraisonItemSQLElement bonElt = new BonDeLivraisonItemSQLElement();
856
        selBonItem.addSelect(bonElt.getTable().getField("ID_SAISIE_VENTE_FACTURE_ELEMENT"));
857
        selBonItem.addSelect(bonElt.getTable().getField("QTE_LIVREE"));
65 ilm 858
        selBonItem.setWhere(bonElt.getTable().getField("ID_BON_DE_LIVRAISON"), "=", idBon);
18 ilm 859
 
860
        String reqBonItem = selBonItem.asString();
861
        Object obBonItem = getTable().getBase().getDataSource().execute(reqBonItem, new ArrayListHandler());
862
 
863
        final List<Object[]> myListBonItem = (List<Object[]>) obBonItem;
864
        final int size = myListBonItem.size();
73 ilm 865
 
866
        for (int i = 0; i < size; i++) {
867
            final Object[] objTmp = myListBonItem.get(i);
868
            final SQLRow rowFactElem = tableFactureElem.getRow(((Number) objTmp[0]).intValue());
869
            final SQLRowValues rowVals = new SQLRowValues(tableFactureElem);
870
            rowVals.put("QTE_LIVREE", Integer.valueOf(((Number) objTmp[1]).intValue() - rowFactElem.getInt("QTE_LIVREE")));
871
            rowVals.update(rowFactElem.getID());
18 ilm 872
        }
873
 
874
    }
875
 
83 ilm 876
    protected String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
61 ilm 877
        return "BL N°" + row.getString("NUMERO");
878
    }
879
 
880
    /**
881
     * Mise à jour des stocks pour chaque article composant la facture
73 ilm 882
     *
883
     * @throws SQLException
61 ilm 884
     */
73 ilm 885
    private void updateStock(int id) throws SQLException {
61 ilm 886
 
887
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
888
        if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
889
 
83 ilm 890
            SQLRow row = getTable().getRow(id);
891
            StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
61 ilm 892
                @Override
83 ilm 893
                public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
61 ilm 894
                    return getLibelleStock(rowOrigin, rowElt);
895
                }
93 ilm 896
            }, row, row.getReferentRows(getTable().getTable("BON_DE_LIVRAISON_ELEMENT")),
897
                    getTable().contains("CREATE_VIRTUAL_STOCK") && row.getBoolean("CREATE_VIRTUAL_STOCK") ? TypeStockUpdate.REAL_VIRTUAL_DELIVER : TypeStockUpdate.REAL_DELIVER);
83 ilm 898
 
899
            stockUpdater.update();
61 ilm 900
        }
901
    }
902
 
93 ilm 903
    @Override
904
    protected void refreshAfterSelect(SQLRowAccessor rSource) {
905
 
906
        tableBonItem.setDateDevise(date.getValue());
907
    }
908
 
18 ilm 909
}