OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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.pos.element;
15
 
93 ilm 16
import java.awt.GridBagConstraints;
17
import java.awt.GridBagLayout;
18
import java.awt.Insets;
19
import java.awt.event.ActionListener;
20
import java.awt.event.KeyAdapter;
21
import java.awt.event.KeyEvent;
22
import java.beans.PropertyChangeEvent;
23
import java.beans.PropertyChangeListener;
24
import java.math.BigDecimal;
25
import java.sql.SQLException;
26
import java.text.DateFormat;
27
import java.text.SimpleDateFormat;
28
import java.util.ArrayList;
29
import java.util.Arrays;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.HashMap;
33
import java.util.HashSet;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.Set;
37
 
38
import javax.swing.JCheckBox;
39
import javax.swing.JLabel;
40
import javax.swing.JOptionPane;
41
import javax.swing.JTextField;
42
import javax.swing.SwingConstants;
43
import javax.swing.SwingUtilities;
44
import javax.swing.event.DocumentEvent;
45
import javax.swing.event.DocumentListener;
46
 
47
import org.apache.commons.dbutils.handlers.ArrayListHandler;
48
 
18 ilm 49
import org.openconcerto.erp.config.ComptaPropsConfiguration;
50
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
51
import org.openconcerto.erp.core.common.ui.DeviseField;
52
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
80 ilm 53
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
18 ilm 54
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
55
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteComptoir;
56
import org.openconcerto.erp.model.PrixTTC;
57
import org.openconcerto.erp.preferences.ModeReglementDefautPrefPanel;
58
import org.openconcerto.sql.Configuration;
59
import org.openconcerto.sql.element.BaseSQLComponent;
60
import org.openconcerto.sql.element.ElementSQLObject;
61
import org.openconcerto.sql.element.SQLComponent;
62
import org.openconcerto.sql.element.SQLElement;
132 ilm 63
import org.openconcerto.sql.element.TreesOfSQLRows;
18 ilm 64
import org.openconcerto.sql.model.SQLRow;
65
import org.openconcerto.sql.model.SQLRowAccessor;
66
import org.openconcerto.sql.model.SQLRowValues;
67
import org.openconcerto.sql.model.SQLSelect;
68
import org.openconcerto.sql.model.SQLTable;
69
import org.openconcerto.sql.model.Where;
70
import org.openconcerto.sql.sqlobject.ElementComboBox;
80 ilm 71
import org.openconcerto.sql.sqlobject.SQLSearchableTextCombo;
18 ilm 72
import org.openconcerto.ui.DefaultGridBagConstraints;
73
import org.openconcerto.ui.JDate;
74
import org.openconcerto.ui.TitledSeparator;
75
import org.openconcerto.ui.component.ITextArea;
93 ilm 76
import org.openconcerto.ui.component.InteractionMode;
18 ilm 77
import org.openconcerto.ui.warning.JLabelWarning;
73 ilm 78
import org.openconcerto.utils.ExceptionHandler;
18 ilm 79
import org.openconcerto.utils.GestionDevise;
83 ilm 80
import org.openconcerto.utils.ListMap;
81
import org.openconcerto.utils.StringUtils;
21 ilm 82
import org.openconcerto.utils.checks.EmptyListener;
83
import org.openconcerto.utils.checks.EmptyObj;
84
import org.openconcerto.utils.checks.ValidState;
85
import org.openconcerto.utils.text.SimpleDocumentListener;
18 ilm 86
 
87
public class SaisieVenteComptoirSQLElement extends ComptaSQLConfElement {
88
 
89
    public SaisieVenteComptoirSQLElement() {
90
        super("SAISIE_VENTE_COMPTOIR", "une saisie d'une vente comptoir", "saisies de  ventes comptoir");
91
    }
92
 
93
    protected List<String> getListFields() {
94
        final List<String> l = new ArrayList<String>();
95
        l.add("DATE");
96
        l.add("NOM");
97
        l.add("ID_CLIENT");
80 ilm 98
        l.add("ID_MODE_REGLEMENT");
18 ilm 99
        l.add("MONTANT_HT");
83 ilm 100
        l.add("MONTANT_SERVICE");
18 ilm 101
        l.add("MONTANT_TTC");
102
        return l;
103
    }
104
 
105
    protected List<String> getComboFields() {
106
        final List<String> l = new ArrayList<String>();
107
        l.add("DATE");
108
        l.add("MONTANT_TTC");
109
        return l;
110
    }
111
 
112
    /*
113
     * (non-Javadoc)
114
     *
115
     * @see org.openconcerto.devis.SQLElement#getComponent()
116
     */
117
    public SQLComponent createComponent() {
118
        return new BaseSQLComponent(this) {
80 ilm 119
            private final JCheckBox checkService = new JCheckBox("dont ");
120
            private SQLSearchableTextCombo textNom;
18 ilm 121
            private DeviseField textMontantTTC;
122
            private DeviseField textMontantService;
123
            private ElementComboBox comboFournisseur;
124
            private JTextField textEcheance;
125
 
126
            private ElementComboBox comboTaxe;
127
            private DeviseField textMontantHT;
128
            private JCheckBox checkCommande;
129
            private JDate dateSaisie;
130
 
83 ilm 131
            private ElementComboBox nomArticle;
18 ilm 132
            private Date dateEch;
133
            private JLabel labelEcheancejours = new JLabel("jours");
134
            private ElementComboBox comboAvoir;
135
            private ElementComboBox comboClient;
19 ilm 136
 
21 ilm 137
            private final JLabel labelWarning = new JLabelWarning("le montant du service ne peut pas dépasser le total HT!");
138
            private ValidState validState = ValidState.getTrueInstance();
73 ilm 139
            // FIXME: use w
18 ilm 140
            private Where w;
141
            private DocumentListener docTTCListen;
142
            private PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
143
 
144
                public void propertyChange(PropertyChangeEvent evt) {
145
 
80 ilm 146
                    if ((nomArticle.getValidState().isValid()) && (!nomArticle.isEmpty()) && !isFilling()) {
18 ilm 147
                        int idArticle = nomArticle.getValue().intValue();
148
 
149
                        SQLTable tableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
150
                        SQLRow rowArticle = tableArticle.getRow(idArticle);
151
                        if (rowArticle != null) {
152
                            comboTaxe.setValue(rowArticle.getInt("ID_TAXE"));
153
 
67 ilm 154
                            textMontantTTC.setText(((BigDecimal) rowArticle.getObject("PV_TTC")).toString());
18 ilm 155
                        }
156
                        System.out.println("value article Changed");
157
 
158
                    }
159
                }
160
            };
161
 
162
            public void addViews() {
163
                this.setLayout(new GridBagLayout());
164
                final GridBagConstraints c = new DefaultGridBagConstraints();
165
 
166
                this.docTTCListen = new DocumentListener() {
167
                    public void changedUpdate(DocumentEvent e) {
168
                        calculMontant();
169
                    }
170
 
171
                    public void removeUpdate(DocumentEvent e) {
172
                        calculMontant();
173
                    }
174
 
175
                    public void insertUpdate(DocumentEvent e) {
176
                        calculMontant();
177
                    }
178
                };
179
 
180
                /***********************************************************************************
181
                 * * RENSEIGNEMENTS
182
                 **********************************************************************************/
183
                c.gridwidth = GridBagConstraints.REMAINDER;
184
                c.gridheight = 1;
185
                TitledSeparator sep = new TitledSeparator("Renseignements");
186
                this.add(sep, c);
187
                c.insets = new Insets(2, 2, 1, 2);
188
 
189
                c.gridwidth = 1;
190
 
191
                // Libellé vente
192
                JLabel labelNom = new JLabel(getLabelFor("NOM"));
193
                labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
194
                c.gridy++;
195
                c.gridx = 0;
196
                c.weightx = 0;
197
                this.add(labelNom, c);
198
 
80 ilm 199
                this.textNom = new SQLSearchableTextCombo();
18 ilm 200
                c.gridx++;
201
                c.weightx = 1;
202
                c.gridwidth = 2;
203
                this.add(this.textNom, c);
204
 
205
                // Date
206
                JLabel labelDate = new JLabel(getLabelFor("DATE"));
207
 
208
                this.dateSaisie = new JDate(true);
19 ilm 209
                c.gridwidth = 1;
80 ilm 210
                // c.gridx += 2;
211
                c.gridx = 0;
212
                c.gridy++;
18 ilm 213
                c.weightx = 0;
214
                labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
215
                this.add(labelDate, c);
216
                c.gridx++;
217
                c.weightx = 1;
218
                this.add(this.dateSaisie, c);
219
 
220
                // article
221
                c.gridy++;
222
                c.gridx = 0;
223
 
83 ilm 224
                this.nomArticle = new ElementComboBox();
18 ilm 225
 
226
                JLabel labelNomArticle = new JLabel(getLabelFor("ID_ARTICLE"));
227
                c.weightx = 0;
228
                labelNomArticle.setHorizontalAlignment(SwingConstants.RIGHT);
80 ilm 229
                // this.add(labelNomArticle, c);
18 ilm 230
 
231
                c.gridx++;
232
                c.weightx = 0;
233
                c.gridwidth = GridBagConstraints.REMAINDER;
80 ilm 234
                // this.add(this.nomArticle, c);
18 ilm 235
 
236
                this.nomArticle.addValueListener(this.propertyChangeListener);
237
 
238
                // client
239
                this.comboClient = new ElementComboBox();
240
                this.comboClient.addValueListener(new PropertyChangeListener() {
241
                    public void propertyChange(PropertyChangeEvent evt) {
242
 
243
                        if (comboClient.isEmpty()) {
244
                            w = new Where(getTable().getBase().getTable("AVOIR_CLIENT").getField("ID_CLIENT"), "=", -1);
245
                        } else {
246
                            w = new Where(getTable().getBase().getTable("AVOIR_CLIENT").getField("ID_CLIENT"), "=", comboClient.getSelectedId());
247
                        }
248
                    }
249
                });
250
 
251
                JLabel labelNomClient = new JLabel(getLabelFor("ID_CLIENT"));
252
                c.gridy++;
253
                c.gridx = 0;
254
                c.gridwidth = 1;
255
                c.weightx = 0;
256
                labelNomClient.setHorizontalAlignment(SwingConstants.RIGHT);
257
                this.add(labelNomClient, c);
258
 
259
                c.gridx++;
260
                c.gridwidth = GridBagConstraints.REMAINDER;
261
                c.weightx = 0;
262
                this.add(this.comboClient, c);
263
 
264
                // Selection d'un avoir si le client en possede
265
 
19 ilm 266
                this.comboAvoir = new ElementComboBox();
18 ilm 267
 
268
                JLabel labelAvoirClient = new JLabel(getLabelFor("ID_AVOIR_CLIENT"));
269
                c.gridy++;
270
                c.gridx = 0;
271
                c.gridwidth = 1;
272
                c.weightx = 0;
273
                labelAvoirClient.setHorizontalAlignment(SwingConstants.RIGHT);
80 ilm 274
                // this.add(labelAvoirClient, c);
18 ilm 275
 
276
                c.gridx++;
277
                c.gridwidth = GridBagConstraints.REMAINDER;
278
                c.weightx = 0;
80 ilm 279
                // this.add(this.comboAvoir, c);
18 ilm 280
 
151 ilm 281
                // Compte spec de produit
282
                JLabel labelCptProduit = new JLabel(getLabelFor("ID_COMPTE_PCE_PRODUIT"));
283
                c.gridy++;
284
                c.gridx = 0;
285
                c.gridwidth = 1;
286
                c.weightx = 0;
287
                labelCptProduit.setHorizontalAlignment(SwingConstants.RIGHT);
288
                this.add(labelCptProduit, c);
289
 
290
                c.gridx++;
291
                c.gridwidth = GridBagConstraints.REMAINDER;
292
                c.weightx = 0;
293
                ElementComboBox comboCptProduit = new ElementComboBox();
294
                this.add(comboCptProduit, c);
295
                addView(comboCptProduit, "ID_COMPTE_PCE_PRODUIT");
296
 
297
                // Compte spec de produit
298
                JLabel labelCptService = new JLabel(getLabelFor("ID_COMPTE_PCE_SERVICE"));
299
                c.gridy++;
300
                c.gridx = 0;
301
                c.gridwidth = 1;
302
                c.weightx = 0;
303
                labelCptService.setHorizontalAlignment(SwingConstants.RIGHT);
304
                this.add(labelCptService, c);
305
 
306
                c.gridx++;
307
                c.gridwidth = GridBagConstraints.REMAINDER;
308
                c.weightx = 0;
309
                ElementComboBox comboCptService = new ElementComboBox();
310
                this.add(comboCptService, c);
311
                addView(comboCptService, "ID_COMPTE_PCE_SERVICE");
312
 
18 ilm 313
                /***********************************************************************************
314
                 * * MONTANT
315
                 **********************************************************************************/
316
                c.gridwidth = GridBagConstraints.REMAINDER;
317
                c.gridx = 0;
318
                c.gridy++;
319
                c.weightx = 1;
320
                sep = new TitledSeparator("Montant en Euros");
321
                c.insets = new Insets(10, 2, 1, 2);
322
                this.add(sep, c);
323
                c.insets = new Insets(2, 2, 1, 2);
324
 
325
                c.gridwidth = 1;
326
 
80 ilm 327
                final JLabel labelMontantHT = new JLabel(getLabelFor("MONTANT_HT"));
328
                labelMontantHT.setHorizontalAlignment(SwingConstants.RIGHT);
329
                this.textMontantHT = new DeviseField();
330
                this.textMontantHT.setEditable(false);
331
                this.textMontantHT.setEnabled(false);
332
                this.textMontantHT.setFocusable(false);
333
 
334
                final JLabel labelMontantTTC = new JLabel(getLabelFor("MONTANT_TTC"));
18 ilm 335
                labelMontantTTC.setHorizontalAlignment(SwingConstants.RIGHT);
336
                this.textMontantTTC = new DeviseField();
337
                this.textMontantTTC.getDocument().addDocumentListener(this.docTTCListen);
338
 
339
                // Montant HT
340
                c.fill = GridBagConstraints.HORIZONTAL;
83 ilm 341
 
18 ilm 342
                c.weightx = 0;
343
                c.gridy++;
344
                c.gridx = 0;
345
                c.gridwidth = 1;
346
                this.add(labelMontantHT, c);
347
 
348
                c.gridx++;
349
                c.weightx = 1;
350
                this.add(this.textMontantHT, c);
351
 
80 ilm 352
                // Montant Service
18 ilm 353
 
80 ilm 354
                c.gridx++;
355
 
18 ilm 356
                c.weightx = 0;
357
                this.add(checkService, c);
358
 
359
                checkService.addActionListener(new ActionListener() {
360
 
361
                    public void actionPerformed(java.awt.event.ActionEvent e) {
80 ilm 362
                        if (isFilling())
363
                            return;
18 ilm 364
                        boolean b = checkService.isSelected();
365
                        textMontantService.setEditable(b);
366
                        textMontantService.setEnabled(b);
80 ilm 367
                        textMontantService.setFocusable(b);
18 ilm 368
 
369
                        if (!b) {
370
                            textMontantService.setText("");
371
                            // montantServiceValide = true;
80 ilm 372
                        } else {
373
                            textMontantService.setText(textMontantHT.getText());
18 ilm 374
                        }
375
                    };
376
                });
377
 
378
                this.textMontantService = new DeviseField();
379
                c.gridx++;
380
                c.weightx = 1;
381
                this.add(this.textMontantService, c);
382
 
383
                JLabel labelMontantService = new JLabel("de service HT");
384
                c.weightx = 0;
385
                c.gridx++;
386
                this.add(labelMontantService, c);
387
 
388
                c.gridx++;
389
                c.gridwidth = GridBagConstraints.REMAINDER;
21 ilm 390
                this.add(this.labelWarning, c);
391
                this.labelWarning.setVisible(false);
18 ilm 392
 
80 ilm 393
                // Choix TVA
394
                c.gridy++;
395
                c.gridx = 0;
396
                c.weightx = 0;
397
                c.gridwidth = 1;
398
 
399
                final JLabel labelTaxe = new JLabel(getLabelFor("ID_TAXE"));
400
                labelTaxe.setHorizontalAlignment(SwingUtilities.RIGHT);
401
                this.add(labelTaxe, c);
402
 
403
                c.gridx++;
404
                this.comboTaxe = new ElementComboBox(false);
405
 
406
                c.fill = GridBagConstraints.NONE;
407
 
408
                this.add(this.comboTaxe, c);
409
 
410
                c.fill = GridBagConstraints.HORIZONTAL;
411
 
412
                // Montant TTC
413
                c.gridy++;
414
                c.gridx = 0;
415
                c.weightx = 0;
416
                this.add(labelMontantTTC, c);
417
 
418
                c.gridx++;
419
                c.weightx = 0;
420
                this.add(this.textMontantTTC, c);
421
 
18 ilm 422
                /***********************************************************************************
423
                 * * MODE DE REGLEMENT
424
                 **********************************************************************************/
425
                c.gridwidth = GridBagConstraints.REMAINDER;
426
                c.gridx = 0;
427
                c.gridy++;
428
                sep = new TitledSeparator("Mode de règlement");
429
                c.insets = new Insets(10, 2, 1, 2);
430
                this.add(sep, c);
431
                c.insets = new Insets(2, 2, 1, 2);
432
 
433
                c.gridx = 0;
434
                c.gridy++;
435
                c.gridwidth = GridBagConstraints.REMAINDER;
436
                this.addView("ID_MODE_REGLEMENT", REQ + ";" + DEC + ";" + SEP);
437
                ElementSQLObject eltModeRegl = (ElementSQLObject) this.getView("ID_MODE_REGLEMENT");
438
                this.add(eltModeRegl, c);
439
 
440
                /***********************************************************************************
441
                 * * COMMANDE
442
                 **********************************************************************************/
443
                c.gridwidth = GridBagConstraints.REMAINDER;
444
                c.gridx = 0;
445
                c.gridy++;
446
                sep = new TitledSeparator("Commande");
447
                c.insets = new Insets(10, 2, 1, 2);
448
                this.add(sep, c);
449
                c.insets = new Insets(2, 2, 1, 2);
450
 
451
                this.checkCommande = new JCheckBox("Produit à commander");
452
 
453
                c.gridx = 0;
454
                c.gridy++;
455
                c.weightx = 0;
456
                c.gridwidth = 2;
80 ilm 457
                // this.add(this.checkCommande, c);
18 ilm 458
 
459
                this.checkCommande.addActionListener(new ActionListener() {
460
 
461
                    public void actionPerformed(java.awt.event.ActionEvent e) {
462
                        boolean b = checkCommande.isSelected();
463
                        comboFournisseur.setEnabled(b);
93 ilm 464
                        comboFournisseur.setInteractionMode((b) ? InteractionMode.READ_WRITE : InteractionMode.DISABLED);
18 ilm 465
 
466
                        textEcheance.setEditable(b);
467
                        textEcheance.setEnabled(b);
468
 
21 ilm 469
                        updateValidState();
18 ilm 470
                    };
471
                });
472
 
473
                // Fournisseurs
474
                JLabel labelFournisseur = new JLabel(getLabelFor("ID_FOURNISSEUR"));
475
                c.gridx = 0;
476
                c.gridy++;
477
                c.weightx = 0;
478
                c.gridwidth = 1;
80 ilm 479
                labelFournisseur.setHorizontalAlignment(SwingConstants.RIGHT);
480
                // this.add(labelFournisseur, c);
18 ilm 481
 
482
                this.comboFournisseur = new ElementComboBox();
483
 
484
                c.gridx++;
485
                c.weightx = 1;
486
                c.gridwidth = 4;
80 ilm 487
                // this.add(this.comboFournisseur, c);
18 ilm 488
 
489
                // Echeance
490
                JLabel labelEcheance = new JLabel("Echeance");
491
                c.gridx = 0;
492
                c.gridy++;
493
                c.weightx = 0;
494
                c.gridwidth = 1;
495
                labelEcheance.setHorizontalAlignment(SwingConstants.RIGHT);
80 ilm 496
                // this.add(labelEcheance, c);
18 ilm 497
 
498
                c.gridx++;
499
                c.weightx = 1;
500
                this.textEcheance = new JTextField();
80 ilm 501
                // this.add(this.textEcheance, c);
18 ilm 502
                this.textEcheance.addKeyListener(new KeyAdapter() {
503
 
504
                    public void keyReleased(KeyEvent e) {
505
                        calculDate();
506
                    }
507
                });
508
 
509
                c.gridx++;
510
                c.weightx = 0;
80 ilm 511
                // this.add(this.labelEcheancejours, c);
18 ilm 512
 
513
                /***********************************************************************************
514
                 * * INFORMATIONS COMPLEMENTAIRES
515
                 **********************************************************************************/
516
                c.gridwidth = GridBagConstraints.REMAINDER;
517
                c.gridx = 0;
518
                c.gridy++;
519
                sep = new TitledSeparator("Informations complémentaires");
520
                c.insets = new Insets(10, 2, 1, 2);
80 ilm 521
                // this.add(sep, c);
18 ilm 522
                c.insets = new Insets(2, 2, 1, 2);
523
 
524
                ITextArea textInfos = new ITextArea();
525
 
526
                c.gridx = 0;
527
                c.gridy++;
528
                c.gridheight = GridBagConstraints.REMAINDER;
529
                c.gridwidth = GridBagConstraints.REMAINDER;
530
                c.weightx = 1;
531
                c.weighty = 1;
532
                c.fill = GridBagConstraints.BOTH;
80 ilm 533
                // this.add(textInfos, c);
18 ilm 534
 
535
                this.addSQLObject(this.textNom, "NOM");
536
                this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
537
                this.addSQLObject(this.nomArticle, "ID_ARTICLE");
538
                this.addRequiredSQLObject(this.textMontantTTC, "MONTANT_TTC");
539
                this.addRequiredSQLObject(this.textMontantHT, "MONTANT_HT");
540
                this.addSQLObject(this.textMontantService, "MONTANT_SERVICE");
541
                this.addRequiredSQLObject(this.dateSaisie, "DATE");
542
                this.addSQLObject(textInfos, "INFOS");
543
                this.addSQLObject(this.comboFournisseur, "ID_FOURNISSEUR");
544
                this.addSQLObject(this.textEcheance, "ECHEANCE");
545
                this.addRequiredSQLObject(this.comboTaxe, "ID_TAXE");
546
                this.addSQLObject(this.comboAvoir, "ID_AVOIR_CLIENT");
547
                this.comboTaxe.setButtonsVisible(false);
548
                this.comboTaxe.setValue(2);
549
 
550
                checkService.setSelected(false);
551
                this.textMontantService.setEditable(false);
552
                this.textMontantService.setEnabled(false);
553
 
554
                this.checkCommande.setSelected(false);
93 ilm 555
                this.comboFournisseur.setInteractionMode(InteractionMode.DISABLED);
18 ilm 556
                this.comboFournisseur.setEnabled(false);
557
                this.textEcheance.setEditable(false);
558
                this.textEcheance.setEnabled(false);
559
 
560
                this.comboTaxe.addValueListener(new PropertyChangeListener() {
561
 
562
                    public void propertyChange(PropertyChangeEvent evt) {
563
                        calculMontant();
564
                    }
565
                });
566
 
21 ilm 567
                final SimpleDocumentListener docL = new SimpleDocumentListener() {
568
                    @Override
569
                    public void update(DocumentEvent e) {
570
                        updateValidState();
18 ilm 571
                    }
21 ilm 572
                };
573
                this.textMontantService.getDocument().addDocumentListener(docL);
574
                this.textMontantHT.getDocument().addDocumentListener(docL);
575
                this.comboFournisseur.addEmptyListener(new EmptyListener() {
576
                    @Override
577
                    public void emptyChange(EmptyObj src, boolean newValue) {
578
                        updateValidState();
18 ilm 579
                    }
580
                });
581
 
582
                /*
583
                 * this.nomClient.addValueListener(new PropertyChangeListener() {
584
                 *
585
                 * public void propertyChange(PropertyChangeEvent evt) { if (nomClient.isValid()) {
586
                 * System.err.println("Changed Combo Avoir"); comboAvoir = new ElementComboBox(new
587
                 * AvoirClientSQLElement(new Where(new
588
                 * AvoirClientSQLElement().getTable().getField("ID_CLIENT"), "=",
589
                 * nomClient.getValue()))); if (comboAvoir != null) { comboAvoir.setEnabled(true); }
590
                 * } else { comboAvoir.setEnabled(false); } } });
591
                 */
592
 
593
            }
594
 
80 ilm 595
            @Override
596
            public Set<String> getPartialResetNames() {
597
                Set<String> s = new HashSet<String>();
598
                s.addAll(super.getPartialResetNames());
599
                s.add("MONTANT_TTC");
83 ilm 600
                s.add("MONTANT_SERVICE");
601
                s.add("MONTANT_HT");
80 ilm 602
                s.add("NOM");
603
                s.add("ID_AVOIR_CLIENT");
604
                s.add("ID_ARTICLE");
605
                s.add("INFOS");
606
                return s;
607
            }
608
 
18 ilm 609
            private void calculMontant() {
610
 
80 ilm 611
                if (!isFilling()) {
18 ilm 612
 
80 ilm 613
                    float taux;
614
                    // PrixHT pHT;
615
                    PrixTTC pTTC;
616
                    // taux de la TVA selectionnee
617
                    int idTaxe = this.comboTaxe.getSelectedId();
618
                    if (idTaxe > 1) {
619
                        SQLRow ligneTaxe = getTable().getBase().getTable("TAXE").getRow(idTaxe);
620
                        if (ligneTaxe != null) {
621
                            taux = (ligneTaxe.getFloat("TAUX")) / 100.0F;
18 ilm 622
 
80 ilm 623
                            // calcul des montants HT ou TTC
624
                            if (this.textMontantTTC.getText().trim().length() > 0) {
625
 
626
                                if (this.textMontantTTC.getText().trim().equals("-")) {
627
                                    pTTC = new PrixTTC(0);
628
                                } else {
629
                                    pTTC = new PrixTTC(GestionDevise.parseLongCurrency(this.textMontantTTC.getText()));
630
                                }
631
 
632
                                // affichage
633
                                updateTextHT(GestionDevise.currencyToString(pTTC.calculLongHT(taux)));
18 ilm 634
                            } else {
80 ilm 635
                                updateTextHT("");
18 ilm 636
                            }
637
                        }
638
                    }
639
                }
640
            }
641
 
642
            private boolean isMontantServiceValid() {
643
                String montant = this.textMontantService.getText().trim();
644
                String montantHT = this.textMontantHT.getText().trim();
645
 
646
                boolean b;
647
                if (montant.length() == 0) {
648
                    b = true;
649
                } else {
650
                    if (montantHT.length() == 0) {
651
                        b = false;
652
                    } else {
653
                        b = (GestionDevise.parseLongCurrency(montantHT) >= GestionDevise.parseLongCurrency(montant));
654
                    }
655
                }
656
 
21 ilm 657
                this.labelWarning.setVisible(!b);
18 ilm 658
                System.err.println("Montant service is valid ? " + b + " --> HT val " + montantHT + " --> service val " + montant);
659
 
660
                return b;
661
            }
662
 
663
            /*
664
             * private void calculMontantHT() {
665
             *
666
             * float taux; PrixHT pHT;
667
             *
668
             * if (!this.comboTaxe.isEmpty()) { // taux de la TVA selectionnee SQLRow ligneTaxe =
132 ilm 669
             * Configuration.getInstance().getBase().getTable( "TAXE").getRow(((Integer)
18 ilm 670
             * this.comboTaxe.getValue()).intValue()); taux = (ligneTaxe.getFloat("TAUX")) / 100; //
671
             * calcul des montants HT ou TTC if (this.textMontantHT.getText().trim().length() > 0) {
672
             *
673
             * if (this.textMontantHT.getText().trim().equals("-")) { pHT = new PrixHT(0); } else {
674
             * pHT = new PrixHT(Float.parseFloat(this.textMontantHT.getText())); } // affichage
675
             * updateTextTTC(String.valueOf(pHT.CalculTTC(taux))); } else updateTextTTC(""); } }
676
             */
677
 
678
            private void updateTextHT(final String prixHT) {
679
                SwingUtilities.invokeLater(new Runnable() {
680
 
681
                    public void run() {
682
 
683
                        textMontantHT.setText(prixHT);
684
                    }
685
                });
686
            }
687
 
688
            @Override
689
            protected SQLRowValues createDefaults() {
690
                SQLRowValues vals = new SQLRowValues(this.getTable());
691
                SQLRowAccessor r;
692
 
693
                try {
694
                    r = ModeReglementDefautPrefPanel.getDefaultRow(true);
695
                    SQLElement eltModeReglement = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
696
                    if (r.getID() > 1) {
697
                        SQLRowValues rowVals = eltModeReglement.createCopy(r.getID());
698
                        System.err.println(rowVals.getInt("ID_TYPE_REGLEMENT"));
699
                        vals.put("ID_MODE_REGLEMENT", rowVals);
700
                    }
701
                } catch (SQLException e) {
702
                    System.err.println("Impossible de sélectionner le mode de règlement par défaut du client.");
703
                    e.printStackTrace();
704
                }
705
                return vals;
706
            }
707
 
708
            // private void updateTextTTC(final String prixTTC) {
709
            // SwingUtilities.invokeLater(new Runnable() {
710
            //
711
            // public void run() {
712
            //
713
            // if (docTTCListen != null) {
714
            //
715
            // textMontantTTC.getDocument().removeDocumentListener(docTTCListen);
716
            // }
717
            //
718
            // textMontantTTC.setText(prixTTC);
719
            //
720
            // // textTaxe.setText(prixTVA);
721
            // textMontantTTC.getDocument().addDocumentListener(docTTCListen);
722
            // }
723
            // });
724
            // }
725
 
726
            private void calculDate() {
727
 
728
                int aJ = 0;
729
 
730
                // on récupére les valeurs saisies
731
                if (this.textEcheance.getText().trim().length() != 0) {
732
 
733
                    try {
734
                        aJ = Integer.parseInt(this.textEcheance.getText());
735
                    } catch (Exception e) {
736
                        System.out.println("Erreur de format sur TextField Ajour " + this.textEcheance.getText());
737
                    }
738
                }
739
 
740
                Calendar cal = Calendar.getInstance();
741
 
742
                // on fixe le temps sur ToDay + Ajour
743
                cal.setTime(new Date());
142 ilm 744
                long tempsMil = aJ * 86400000L;
18 ilm 745
                cal.setTimeInMillis(cal.getTimeInMillis() + tempsMil);
746
 
747
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
748
                this.dateEch = cal.getTime();
749
                this.labelEcheancejours.setText("jours, soit le " + dateFormat.format(this.dateEch));
750
            }
751
 
21 ilm 752
            private void updateValidState() {
753
                this.setValidState(this.computeValidState());
754
            }
18 ilm 755
 
21 ilm 756
            private ValidState computeValidState() {
757
                ValidState res = ValidState.getTrueInstance();
758
                if (!this.isMontantServiceValid())
759
                    res = res.and(ValidState.createCached(false, this.labelWarning.getText()));
760
                if (this.checkCommande.isSelected())
761
                    res = res.and(ValidState.createCached(!this.comboFournisseur.isEmpty(), "Fournisseur non renseigné"));
762
                return res;
763
            }
18 ilm 764
 
21 ilm 765
            private final void setValidState(ValidState validState) {
766
                if (!validState.equals(this.validState)) {
767
                    this.validState = validState;
768
                    this.fireValidChange();
769
                }
18 ilm 770
            }
771
 
21 ilm 772
            @Override
773
            public synchronized ValidState getValidState() {
774
                return super.getValidState().and(this.validState);
775
            }
776
 
18 ilm 777
            public int insert(SQLRow order) {
778
 
779
                // On teste si l'article n'existe pas, on le crée
83 ilm 780
                if (this.nomArticle.isEmpty()) {
18 ilm 781
                    createArticle();
782
                }
783
 
80 ilm 784
                if (this.textNom.getValue() == null || this.textNom.getValue().trim().length() <= 0) {
83 ilm 785
                    this.textNom.setValue(this.nomArticle.getTextComp().getText());
18 ilm 786
                }
787
                final int id = super.insert(order);
788
                // on verifie si le produit est à commander
789
                if (this.checkCommande.isSelected()) {
790
                    createCommande(id);
791
                }
792
 
80 ilm 793
                SQLRow rowArt = getTable().getRow(id).getForeignRow("ID_ARTICLE");
794
                if (rowArt != null && !rowArt.isUndefined() && rowArt.getBoolean("GESTION_STOCK")) {
795
                    Configuration.getInstance().getNonInteractiveSQLExecutor().execute(new Runnable() {
18 ilm 796
 
80 ilm 797
                        @Override
798
                        public void run() {
18 ilm 799
 
80 ilm 800
                            final SQLRow rowVC = getTable().getRow(id);
801
                            // Mise à jour des stocks
802
                            final SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
803
                            final SQLRowValues rowVals = new SQLRowValues(eltMvtStock.getTable());
804
                            rowVals.put("QTE", -1);
805
                            rowVals.put("NOM", "Saisie vente comptoir");
806
                            rowVals.put("IDSOURCE", id);
807
                            rowVals.put("SOURCE", getTable().getName());
808
                            rowVals.put("ID_ARTICLE", rowVC.getInt("ID_ARTICLE"));
156 ilm 809
                            rowVals.put("ID_STOCK", rowVC.getForeign("ID_ARTICLE").getForeignID("ID_STOCK"));
80 ilm 810
                            rowVals.put("DATE", rowVC.getObject("DATE"));
18 ilm 811
 
80 ilm 812
                            try {
813
                                final SQLRow row = rowVals.insert();
93 ilm 814
                                final ListMap<SQLRow, SQLRowValues> map = ((MouvementStockSQLElement) Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK"))
815
                                        .updateStock(Arrays.asList(row), false);
80 ilm 816
                                MouvementStockSQLElement.createCommandeF(map, null);
817
 
818
                            } catch (SQLException e) {
819
                                ExceptionHandler.handle("Erreur lors de la création des mouvements de stock", e);
820
                            }
73 ilm 821
                        }
80 ilm 822
                    });
73 ilm 823
 
80 ilm 824
                }
825
                new GenerationMvtSaisieVenteComptoir(id);
18 ilm 826
                return id;
827
            }
828
 
829
            private void createCommande(final int id) {
830
 
831
                System.out.println("Ajout d'une commande");
832
                SQLRow rowSaisie = SaisieVenteComptoirSQLElement.this.getTable().getRow(id);
833
                Map<String, Object> m = new HashMap<String, Object>();
834
                // NOM, DATE, ECHEANCE, IDSOURCE, SOURCE, ID_CLI, ID_FOURN, ID_ARTICLE
835
                m.put("NOM", rowSaisie.getObject("NOM"));
836
                m.put("DATE", rowSaisie.getObject("DATE"));
837
                m.put("DATE_ECHEANCE", new java.sql.Date(this.dateEch.getTime()));
838
 
839
                m.put("IDSOURCE", new Integer(id));
840
                m.put("SOURCE", "SAISIE_VENTE_COMPTOIR");
841
 
842
                m.put("ID_CLIENT", rowSaisie.getObject("ID_CLIENT"));
843
                m.put("ID_FOURNISSEUR", rowSaisie.getObject("ID_FOURNISSEUR"));
844
                m.put("ID_ARTICLE", rowSaisie.getObject("ID_ARTICLE"));
845
 
846
                SQLTable tableCmd = getTable().getBase().getTable("COMMANDE_CLIENT");
847
 
848
                SQLRowValues valCmd = new SQLRowValues(tableCmd, m);
849
 
850
                try {
851
                    if (valCmd.getInvalid() == null) {
852
                        // ajout de l'ecriture
853
                        valCmd.insert();
854
                    }
855
                } catch (Exception e) {
856
                    System.err.println("Erreur à l'insertion dans la table " + valCmd.getTable().getName());
857
                    e.printStackTrace();
858
                }
859
            }
860
 
861
            private void createArticle() {
862
                System.out.println("Création de l'article");
863
 
83 ilm 864
                String tNomArticle = this.nomArticle.getTextComp().getText();
865
                String codeArticle = "";// this.nomArticle.getTextOpt();
80 ilm 866
 
867
                if (tNomArticle.trim().length() == 0 && codeArticle.trim().length() == 0) {
868
                    return;
869
                }
870
 
871
                SQLTable articleTable = getTable().getBase().getTable("ARTICLE");
872
 
18 ilm 873
                int idTaxe = this.comboTaxe.getSelectedId();
83 ilm 874
                final BigDecimal prix = StringUtils.getBigDecimalFromUserText(this.textMontantHT.getText());
875
                final BigDecimal prixTTC = StringUtils.getBigDecimalFromUserText(this.textMontantTTC.getText());
18 ilm 876
 
877
                if (tNomArticle.trim().length() == 0) {
878
                    tNomArticle = "Nom Indefini";
879
                }
880
                if (codeArticle.trim().length() == 0) {
881
                    codeArticle = "Indefini";
882
                }
883
 
884
                SQLRowValues vals = new SQLRowValues(articleTable);
885
                vals.put("NOM", tNomArticle);
886
                vals.put("CODE", codeArticle);
887
                vals.put("PA_HT", prix);
888
                vals.put("PV_HT", prix);
80 ilm 889
                vals.put("PRIX_METRIQUE_VT_1", prix);
890
                vals.put("PRIX_METRIQUE_HA_1", prix);
18 ilm 891
                vals.put("PV_TTC", prixTTC);
80 ilm 892
                vals.put("ID_UNITE_VENTE", UniteVenteArticleSQLElement.A_LA_PIECE);
18 ilm 893
                vals.put("ID_TAXE", new Integer(idTaxe));
894
                vals.put("CREATION_AUTO", Boolean.TRUE);
80 ilm 895
                vals.put("GESTION_STOCK", Boolean.FALSE);
18 ilm 896
 
897
                try {
898
                    SQLRow row = vals.insert();
83 ilm 899
                    this.nomArticle.setValue(row);
18 ilm 900
                } catch (SQLException e) {
901
 
902
                    e.printStackTrace();
903
                }
904
            }
905
 
906
            public void update() {
907
 
908
                if (JOptionPane.showConfirmDialog(this, "Attention en modifiant cette vente comptoir, vous supprimerez les chéques et les échéances associés. Continuer?",
909
                        "Modification de vente comptoir", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
80 ilm 910
 
18 ilm 911
                    // on efface les mouvements de stocks associés
912
                    SQLRow row = getTable().getRow(this.getSelectedID());
913
                    SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
80 ilm 914
                    SQLSelect sel = new SQLSelect();
18 ilm 915
                    sel.addSelect(eltMvtStock.getTable().getField("ID"));
916
                    Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
917
                    Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
918
                    sel.setWhere(w.and(w2));
919
 
920
                    List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
921
                    if (l != null) {
922
                        for (int i = 0; i < l.size(); i++) {
923
                            Object[] tmp = (Object[]) l.get(i);
924
                            try {
925
                                eltMvtStock.archive(((Number) tmp[0]).intValue());
926
                            } catch (SQLException e) {
927
                                e.printStackTrace();
928
                            }
929
                        }
930
                    }
931
 
83 ilm 932
                    if (this.textNom.getValue() != null && this.textNom.getValue().trim().length() <= 0) {
933
                        this.textNom.setValue(this.nomArticle.getTextComp().getText());
18 ilm 934
                    }
935
 
936
                    // On teste si l'article n'existe pas, on le crée
83 ilm 937
                    if (this.nomArticle.getValue() == null || Integer.parseInt(this.nomArticle.getValue().toString()) == -1) {
18 ilm 938
                        createArticle();
939
                    }
940
 
941
                    // TODO check echeance, ---> creation article, commande??
942
                    super.update();
943
 
944
                    row = getTable().getRow(this.getSelectedID());
945
                    int idMvt = row.getInt("ID_MOUVEMENT");
946
                    System.out.println(row.getID() + "__________***************** UPDATE " + idMvt);
947
 
948
                    // on supprime tout ce qui est lié à la facture
949
                    EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
950
                    eltEcr.archiveMouvementProfondeur(idMvt, false);
951
 
80 ilm 952
                    SQLRow rowArt = row.getForeignRow("ID_ARTICLE");
953
                    if (rowArt != null && !rowArt.isUndefined() && rowArt.getBoolean("GESTION_STOCK")) {
954
                        // Mise à jour des stocks
955
                        SQLRowValues rowVals = new SQLRowValues(eltMvtStock.getTable());
956
                        rowVals.put("QTE", -1);
957
                        rowVals.put("NOM", "Saisie vente comptoir");
958
                        rowVals.put("IDSOURCE", getSelectedID());
959
                        rowVals.put("SOURCE", getTable().getName());
960
                        rowVals.put("ID_ARTICLE", row.getInt("ID_ARTICLE"));
156 ilm 961
                        rowVals.put("ID_STOCK", row.getForeign("ID_ARTICLE").getForeignID("ID_STOCK"));
80 ilm 962
                        rowVals.put("DATE", row.getObject("DATE"));
963
                        try {
964
                            SQLRow rowNew = rowVals.insert();
93 ilm 965
                            final ListMap<SQLRow, SQLRowValues> map = ((MouvementStockSQLElement) Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK"))
966
                                    .updateStock(Arrays.asList(rowNew), false);
80 ilm 967
                            ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
968
 
969
                                @Override
970
                                public void run() {
971
                                    MouvementStockSQLElement.createCommandeF(map, null);
972
                                }
973
                            });
974
                        } catch (SQLException e) {
975
                            e.printStackTrace();
976
                        }
18 ilm 977
                    }
80 ilm 978
                    if (idMvt > 1) {
979
                        new GenerationMvtSaisieVenteComptoir(this.getSelectedID(), idMvt);
980
                    } else {
981
                        new GenerationMvtSaisieVenteComptoir(this.getSelectedID());
982
                    }
18 ilm 983
                }
984
            }
985
 
986
            @Override
987
            public void select(SQLRowAccessor r) {
988
                this.textMontantTTC.getDocument().removeDocumentListener(this.docTTCListen);
83 ilm 989
                this.nomArticle.rmValueListener(this.propertyChangeListener);
18 ilm 990
 
991
                super.select(r);
992
 
83 ilm 993
                checkService.setSelected(r != null && r.getObject("MONTANT_SERVICE") != null && r.getLong("MONTANT_SERVICE") > 0);
80 ilm 994
 
18 ilm 995
                this.textMontantTTC.getDocument().addDocumentListener(this.docTTCListen);
996
                this.nomArticle.addValueListener(this.propertyChangeListener);
997
            }
83 ilm 998
 
999
            @Override
1000
            public void select(SQLRowAccessor r, Set<String> views) {
1001
                super.select(r, views);
1002
                checkService.setSelected(r != null && r.getObject("MONTANT_SERVICE") != null && r.getLong("MONTANT_SERVICE") > 0);
1003
            }
1004
 
18 ilm 1005
        };
1006
    }
1007
 
1008
    @Override
132 ilm 1009
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
1010
        // TODO Auto-generated method stub
1011
        super.archive(trees, cutLinks);
18 ilm 1012
 
132 ilm 1013
        for (SQLRow row : trees.getRows()) {
18 ilm 1014
 
132 ilm 1015
            // Mise à jour des stocks
1016
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
1017
            SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
1018
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
1019
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
1020
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
1021
            sel.setWhere(w.and(w2));
18 ilm 1022
 
132 ilm 1023
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
1024
            if (l != null) {
1025
                for (int i = 0; i < l.size(); i++) {
1026
                    Object[] tmp = (Object[]) l.get(i);
1027
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
1028
                }
18 ilm 1029
            }
1030
        }
1031
    }
57 ilm 1032
 
1033
    @Override
1034
    protected String createCode() {
156 ilm 1035
        return createCodeOfPackage() + ".sale";
57 ilm 1036
    }
18 ilm 1037
}