OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 41 | 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.supplychain.order.component;
15
 
16
import org.openconcerto.erp.core.common.ui.DeviseField;
17
import org.openconcerto.erp.core.common.ui.MontantPanel;
18
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
19
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
20
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieAchat;
21
import org.openconcerto.erp.model.ISQLCompteSelector;
22
import org.openconcerto.erp.preferences.ModeReglementDefautPrefPanel;
23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.BaseSQLComponent;
25
import org.openconcerto.sql.element.ElementSQLObject;
26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.SQLBackgroundTableCache;
28
import org.openconcerto.sql.model.SQLInjector;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowAccessor;
31
import org.openconcerto.sql.model.SQLRowValues;
32
import org.openconcerto.sql.model.SQLTable;
33
import org.openconcerto.sql.model.Where;
34
import org.openconcerto.sql.sqlobject.ElementComboBox;
35
import org.openconcerto.sql.sqlobject.SQLTextCombo;
36
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 37
import org.openconcerto.ui.FormLayouter;
18 ilm 38
import org.openconcerto.ui.JDate;
39
import org.openconcerto.ui.TitledSeparator;
40
import org.openconcerto.ui.component.ITextArea;
41
import org.openconcerto.utils.GestionDevise;
21 ilm 42
import org.openconcerto.utils.checks.ValidState;
18 ilm 43
import org.openconcerto.utils.text.SimpleDocumentListener;
44
 
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
47
import java.awt.Insets;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.beans.PropertyChangeEvent;
51
import java.beans.PropertyChangeListener;
52
import java.sql.SQLException;
53
 
54
import javax.swing.ButtonGroup;
55
import javax.swing.JCheckBox;
56
import javax.swing.JLabel;
57
import javax.swing.JOptionPane;
58
import javax.swing.JPanel;
59
import javax.swing.JRadioButton;
60
import javax.swing.JScrollPane;
61
import javax.swing.JTextField;
62
import javax.swing.SwingConstants;
63
import javax.swing.event.DocumentEvent;
64
 
65
public class SaisieAchatSQLComponent extends BaseSQLComponent {
66
 
67
    public SaisieAchatSQLComponent(SQLElement element) {
68
        super(element);
69
    }
70
 
71
    private JTextField textNumeroFacture;
72
    private JTextField textNumeroCmd;
73
    private JTextField textSource, textIdSource;
74
 
75
    private DeviseField fieldMontantRegle = new DeviseField();
76
 
77
    private JRadioButton radioButtonNumeroFacture;
78
    private JCheckBox checkImmo;
79
    private JRadioButton radioButtonNumeroCmd;
80
    private ButtonGroup grp1;
81
    private MontantPanel montant;
82
    private ElementComboBox nomFournisseur;
83
    private ElementComboBox comboAvoir;
84
    private ElementSQLObject eltModeRegl;
85
    private int idFournSelect = 1;
86
    private PropertyChangeListener listenerModeReglDefaut = new PropertyChangeListener() {
87
 
88
        public void propertyChange(PropertyChangeEvent evt) {
89
 
90
            Integer idSeleted = SaisieAchatSQLComponent.this.nomFournisseur.getValue();
91
 
92
            // System.err.println("Select Fournisseur " + idSeleted);
93
 
94
            if (idSeleted != null && idSeleted > 1) {
95
                SQLElement fournisseur = Configuration.getInstance().getDirectory().getElement("FOURNISSEUR");
96
                SQLRow rowFourn = fournisseur.getTable().getRow(idSeleted);
97
                SaisieAchatSQLComponent.this.montant.setUE(rowFourn.getBoolean("UE"));
98
 
99
                if (getSelectedID() <= 1 || SaisieAchatSQLComponent.this.idFournSelect != idSeleted) {
100
                    int idModeRegl = rowFourn.getInt("ID_MODE_REGLEMENT");
101
                    if (idModeRegl > 1 && SaisieAchatSQLComponent.this.eltModeRegl != null && getMode() == Mode.INSERTION) {
102
                        SQLElement sqlEltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
103
                        SQLRow rowModeRegl = sqlEltModeRegl.getTable().getRow(idModeRegl);
104
                        SQLRowValues rowVals = rowModeRegl.createUpdateRow();
105
                        rowVals.clearPrimaryKeys();
106
                        SaisieAchatSQLComponent.this.eltModeRegl.setValue(rowVals);
107
                        System.err.println("Select Mode regl " + idModeRegl);
108
                    }
109
                }
110
            }
111
            SaisieAchatSQLComponent.this.idFournSelect = (idSeleted == null) ? 1 : idSeleted;
112
        }
113
 
114
    };
115
 
116
    public void addViews() {
117
        this.setLayout(new GridBagLayout());
118
        final GridBagConstraints c = new DefaultGridBagConstraints();
119
 
120
        // Source de la saisie --> Commande, BR
121
        this.textIdSource = new JTextField();
122
        this.textSource = new JTextField();
123
 
25 ilm 124
        // Champ Module
125
        c.gridx = 0;
126
        c.gridy++;
127
        c.gridwidth = GridBagConstraints.REMAINDER;
128
        final JPanel addP = new JPanel();
129
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 1));
130
        this.add(addP, c);
131
 
132
        c.gridy++;
133
        c.gridwidth = 1;
134
 
18 ilm 135
        /*******************************************************************************************
136
         * * RENSEIGNEMENTS
137
         ******************************************************************************************/
138
 
139
        c.gridwidth = GridBagConstraints.REMAINDER;
140
        c.gridheight = 1;
141
        TitledSeparator sep = new TitledSeparator("Renseignements");
142
        this.add(sep, c);
143
 
144
        c.gridy++;
145
        c.gridwidth = 1;
146
        c.gridx = 0;
147
 
148
        // libellé
149
        JLabel labelLibelle = new JLabel("Achat de ");
150
        SQLTextCombo textLibelle = new SQLTextCombo();
151
        c.weightx = 0;
152
        labelLibelle.setHorizontalAlignment(SwingConstants.RIGHT);
153
        this.add(labelLibelle, c);
154
        c.gridx++;
155
        c.weightx = 1;
156
        c.fill = GridBagConstraints.BOTH;
157
        this.add(textLibelle, c);
158
 
159
        // Date
160
 
161
        c.gridx++;
162
        c.weightx = 0;
163
        c.fill = GridBagConstraints.HORIZONTAL;
164
        this.add(new JLabel("Date"), c);
165
        c.gridx++;
166
        c.weightx = 1;
167
        JDate dateSaisie = new JDate(true);
168
        c.fill = GridBagConstraints.NONE;
169
 
170
        this.add(dateSaisie, c);
171
 
172
        // Fournisseurs
173
        c.gridy++;
174
        c.gridx = 0;
175
        c.fill = GridBagConstraints.HORIZONTAL;
176
        c.weightx = 0;
177
 
178
        this.add(new JLabel("Fournisseur", SwingConstants.RIGHT), c);
179
        c.gridx++;
180
 
181
        c.gridwidth = 4;
182
        c.weightx = 1;
183
        c.weighty = 0;
184
        c.fill = GridBagConstraints.NONE;
185
        this.nomFournisseur = new ElementComboBox();
186
        this.add(this.nomFournisseur, c);
187
 
188
        // Ligne 3 : Compte de charges
189
        c.gridy++;
190
        c.weightx = 0;
191
        c.gridx = 0;
192
        c.fill = GridBagConstraints.HORIZONTAL;
193
        final ISQLCompteSelector compteSel = new ISQLCompteSelector();
194
        this.add(new JLabel(getLabelFor("ID_COMPTE_PCE")), c);
195
        c.gridwidth = GridBagConstraints.REMAINDER;
196
        c.gridx++;
197
        c.weightx = 1;
198
        this.add(compteSel, c);
199
 
200
        c.gridwidth = 1;
201
 
202
        // Ligne 4 : Numero facture fournisseur
203
        this.radioButtonNumeroFacture = new JRadioButton(getLabelFor("NUMERO_FACTURE"));
204
        this.radioButtonNumeroFacture.addActionListener(new ActionListener() {
205
            public void actionPerformed(ActionEvent e) {
206
                SaisieAchatSQLComponent.this.textNumeroFacture.setEnabled(true);
207
                SaisieAchatSQLComponent.this.textNumeroCmd.setEnabled(false);
208
            }
209
        });
210
        c.fill = GridBagConstraints.NONE;
211
        c.weightx = 0;
212
        c.gridx = 0;
213
        c.gridy++;
214
        c.gridwidth = 1;
215
        this.add(this.radioButtonNumeroFacture, c);
216
 
217
        this.textNumeroFacture = new JTextField(16);
218
        DefaultGridBagConstraints.lockMinimumSize(textNumeroFacture);
219
        c.gridx = 1;
220
        c.gridwidth = 1;
221
        this.add(this.textNumeroFacture, c);
222
 
223
        // Ligne 5 : numéro commande fournisseur
224
        this.radioButtonNumeroCmd = new JRadioButton(getLabelFor("NUMERO_COMMANDE"));
225
        this.radioButtonNumeroCmd.addActionListener(new ActionListener() {
226
            public void actionPerformed(ActionEvent e) {
227
                // numeroCmd.setValue(textNumeroCmd.getText());
228
                // numeroFact.setValue("");
229
                SaisieAchatSQLComponent.this.textNumeroFacture.setEnabled(false);
230
                SaisieAchatSQLComponent.this.textNumeroCmd.setEnabled(true);
231
            }
232
        });
233
        c.gridx = 0;
234
        c.gridy++;
235
        c.gridwidth = 1;
236
        this.add(this.radioButtonNumeroCmd, c);
237
 
238
        this.textNumeroCmd = new JTextField(16);
239
        c.gridx = 1;
240
        c.gridwidth = 1;
241
        DefaultGridBagConstraints.lockMinimumSize(textNumeroCmd);
242
        this.add(this.textNumeroCmd, c);
243
 
244
        c.gridx++;
245
        c.gridwidth = GridBagConstraints.REMAINDER;
246
        c.weightx = 1;
247
        c.anchor = GridBagConstraints.EAST;
248
        this.checkImmo = new JCheckBox(getLabelFor("IMMO"));
249
        this.add(this.checkImmo, c);
250
        c.anchor = GridBagConstraints.WEST;
251
 
252
        // textNumeroCmd
253
        this.grp1 = new ButtonGroup();
254
        this.grp1.add(this.radioButtonNumeroCmd);
255
        this.grp1.add(this.radioButtonNumeroFacture);
256
 
257
        this.radioButtonNumeroFacture.setSelected(true);
258
        this.textNumeroFacture.setEnabled(true);
259
        this.textNumeroCmd.setEnabled(false);
260
 
261
        // MONTANT
262
 
263
        c.gridx = 0;
264
        c.gridy++;
265
        c.insets = new Insets(10, 2, 1, 2);
266
        c.gridwidth = GridBagConstraints.REMAINDER;
267
        c.fill = GridBagConstraints.BOTH;
268
        sep = new TitledSeparator("Montant en euros");
269
        this.add(sep, c);
270
 
271
        c.insets = new Insets(2, 2, 1, 2);
272
        c.gridwidth = 4;
273
 
274
        // Montant
275
 
276
        c.gridx = 0;
277
        c.gridy++;
278
        c.gridwidth = 4;
279
        c.weightx = 1;
280
        c.anchor = GridBagConstraints.WEST;
281
        c.fill = GridBagConstraints.NONE;
282
        this.montant = new MontantPanel();
283
        this.add(this.montant, c);
284
 
285
        // Avoir
286
        JPanel panelAvoir = new JPanel();
287
        this.comboAvoir = new ElementComboBox(true);
288
        panelAvoir.add(new JLabel(getLabelFor("ID_AVOIR_FOURNISSEUR")));
289
        this.comboAvoir.setAddIconVisible(false);
290
        panelAvoir.add(this.comboAvoir);
291
 
292
        panelAvoir.add(new JLabel("Montant réglé"));
293
        panelAvoir.add(this.fieldMontantRegle);
294
        this.fieldMontantRegle.setEditable(false);
295
 
296
        c.gridx = 0;
297
        c.gridy++;
298
        c.gridwidth = GridBagConstraints.REMAINDER;
299
        c.weightx = 0;
300
        c.fill = GridBagConstraints.NONE;
301
        this.add(panelAvoir, c);
302
 
303
        /*******************************************************************************************
304
         * * MODE DE REGLEMENT
305
         ******************************************************************************************/
306
 
307
        // Mode de règlement
308
        c.gridwidth = GridBagConstraints.REMAINDER;
309
        c.gridx = 0;
310
        c.gridy++;
311
        c.insets = new Insets(10, 2, 1, 2);
312
        c.fill = GridBagConstraints.HORIZONTAL;
313
        sep = new TitledSeparator("Mode de règlement");
314
 
315
        this.add(sep, c);
316
 
317
        c.insets = new Insets(2, 2, 1, 2);
318
 
319
        c.gridx = 0;
320
        c.gridy++;
321
        c.gridwidth = GridBagConstraints.REMAINDER;
322
        this.addView("ID_MODE_REGLEMENT", REQ + ";" + DEC + ";" + SEP);
323
        this.eltModeRegl = (ElementSQLObject) this.getView("ID_MODE_REGLEMENT");
324
        this.add(this.eltModeRegl, c);
325
 
326
        /*******************************************************************************************
327
         * * INFORMATIONS COMPLEMENTAIRES
328
         ******************************************************************************************/
329
        c.gridwidth = GridBagConstraints.REMAINDER;
330
        c.gridx = 0;
331
        c.gridy++;
332
        sep = new TitledSeparator("Informations complémentaires");
333
        c.insets = new Insets(10, 2, 1, 2);
334
        this.add(sep, c);
335
        c.insets = new Insets(2, 2, 1, 2);
336
 
337
        c.gridx = 0;
338
        c.gridy++;
339
        c.gridheight = GridBagConstraints.REMAINDER;
340
        c.gridwidth = GridBagConstraints.REMAINDER;
341
        c.weighty = 1;
342
        c.weightx = 1;
343
        c.fill = GridBagConstraints.BOTH;
344
        ITextArea textInfos = new ITextArea(4, 4);
345
 
346
        JScrollPane scrollPane = new JScrollPane(textInfos);
347
        DefaultGridBagConstraints.lockMinimumSize(scrollPane);
348
 
349
        this.add(textInfos, c);
350
 
351
        this.addSQLObject(textInfos, "INFOS");
352
        this.addRequiredSQLObject(dateSaisie, "DATE");
353
        this.addRequiredSQLObject(this.nomFournisseur, "ID_FOURNISSEUR");
354
        this.addRequiredSQLObject(this.montant.getChoixTaxe(), "ID_TAXE");
355
        this.addRequiredSQLObject(this.montant.getMontantTTC(), "MONTANT_TTC");
356
        this.addRequiredSQLObject(this.montant.getMontantHT(), "MONTANT_HT");
357
        this.addRequiredSQLObject(this.montant.getMontantTVA(), "MONTANT_TVA");
358
        this.addSQLObject(textLibelle, "NOM");
359
        this.addSQLObject(this.textNumeroFacture, "NUMERO_FACTURE");
360
        this.addSQLObject(this.textNumeroCmd, "NUMERO_COMMANDE");
361
        this.addSQLObject(this.textIdSource, "IDSOURCE");
362
        this.addSQLObject(this.textSource, "SOURCE");
363
        this.addRequiredSQLObject(compteSel, "ID_COMPTE_PCE");
364
        this.addSQLObject(this.comboAvoir, "ID_AVOIR_FOURNISSEUR");
365
        this.addSQLObject(this.checkImmo, "IMMO");
366
 
367
        this.montant.setChoixTaxe(2);
368
 
369
        this.nomFournisseur.addValueListener(this.listenerModeReglDefaut);
370
 
371
        this.montant.getMontantTTC().getDocument().addDocumentListener(new SimpleDocumentListener() {
372
            @Override
373
            public void update(DocumentEvent e) {
374
                refreshText();
375
            }
376
        });
377
 
378
        this.comboAvoir.addValueListener(new PropertyChangeListener() {
379
            @Override
380
            public void propertyChange(PropertyChangeEvent evt) {
381
                refreshText();
382
            }
383
        });
384
 
385
        final SQLTable tableAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR").getTable();
386
        this.nomFournisseur.addValueListener(new PropertyChangeListener() {
387
            @Override
388
            public void propertyChange(PropertyChangeEvent evt) {
389
 
390
                Where w = new Where(tableAvoir.getField("SOLDE"), "=", Boolean.FALSE);
391
                if (SaisieAchatSQLComponent.this.nomFournisseur.isEmpty()) {
392
                    w.and(new Where(getTable().getBase().getTable("AVOIR_FOURNISSEUR").getField("ID_FOURNISSEUR"), "=", -1));
393
                } else {
394
                    w.and(new Where(getTable().getBase().getTable("AVOIR_FOURNISSEUR").getField("ID_FOURNISSEUR"), "=", SaisieAchatSQLComponent.this.nomFournisseur.getValue()));
395
                }
396
                if (getSelectedID() > 1) {
397
                    SQLRow row = getTable().getRow(getSelectedID());
398
                    w = w.or(new Where(getTable().getBase().getTable("AVOIR_FOURNISSEUR").getKey(), "=", row.getInt("ID_AVOIR_FOURNISSEUR")));
399
                }
400
 
401
                SaisieAchatSQLComponent.this.comboAvoir.getRequest().setWhere(null);
402
                SaisieAchatSQLComponent.this.comboAvoir.getRequest().setWhere(w);
403
                SaisieAchatSQLComponent.this.comboAvoir.fillCombo();
404
            }
405
        });
406
 
407
        Where w = new Where(tableAvoir.getField("SOLDE"), "=", Boolean.FALSE);
408
        this.comboAvoir.getRequest().setWhere(w);
409
        this.comboAvoir.fillCombo();
410
 
411
        // Lock UI
412
        DefaultGridBagConstraints.lockMinimumSize(nomFournisseur);
413
        DefaultGridBagConstraints.lockMinimumSize(panelAvoir);
414
        DefaultGridBagConstraints.lockMinimumSize(montant);
415
        DefaultGridBagConstraints.lockMaximumSize(montant);
416
 
417
    }
418
 
419
    @Override
21 ilm 420
    public ValidState getValidState() {
421
        ValidState result = super.getValidState();
422
        if (result.isValid()) {
423
            if (this.montant.getMontantTTC() != null && this.montant.getMontantTTC().getUncheckedValue() != null) {
18 ilm 424
 
21 ilm 425
                long l = ((Long) this.montant.getMontantTTC().getUncheckedValue());
18 ilm 426
 
21 ilm 427
                if (this.comboAvoir != null && !this.comboAvoir.isEmpty() && this.comboAvoir.getSelectedId() > 1) {
428
                    SQLElement eltAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR");
429
                    SQLRow rowAvoir = eltAvoir.getTable().getRow(this.comboAvoir.getSelectedId());
430
                    l -= ((Number) rowAvoir.getObject("MONTANT_TTC")).longValue();
431
                }
432
                if (l < 0) {
433
                    result = new ValidState(false, "Le montant est négatif");
434
                }
18 ilm 435
            }
436
        }
21 ilm 437
        return result;
18 ilm 438
    }
439
 
440
    /***********************************************************************************************
441
     * * GENERATION DU MOUVEMENT ET DES ECRITURES ASSOCIEES A L'INSERTION
442
     **********************************************************************************************/
443
    public int insert(SQLRow order) {
444
        final int id = super.insert(order);
445
 
446
        // on solde l'avoir
447
        if (this.comboAvoir.getSelectedId() > 1) {
448
            SQLTable tableAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR").getTable();
449
            SQLRow rowAvoir = tableAvoir.getRow(this.comboAvoir.getSelectedId());
450
            SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
451
            rowVals.put("SOLDE", Boolean.TRUE);
452
            try {
453
                rowVals.update();
454
            } catch (SQLException e) {
455
                e.printStackTrace();
456
            }
457
        }
458
 
459
        new GenerationMvtSaisieAchat(id);
460
 
461
        return id;
462
    }
463
 
464
    public void select(SQLRowAccessor r) {
465
 
466
        // FIXME probleme selection du mode de reglement par defaut -> l'id est celui du défaut dans
467
        // la saisie
468
        this.nomFournisseur.rmValueListener(this.listenerModeReglDefaut);
469
 
470
        super.select(r);
471
 
472
        if (r != null) {
473
            System.err.println(r);
474
            // System.err.println("Select Mode regl " + r.getInt("ID_MODE_REGLEMENT"));
475
            this.montant.calculMontant();
476
            Object idF = r.getObject("ID_FOURNISSEUR");
477
            System.err.println("Founisseur " + idF);
478
            if (idF != null) {
479
                int idSeleted = Integer.valueOf(idF.toString());
480
 
481
                if (idSeleted > 1) {
482
                    SQLElement fournisseur = Configuration.getInstance().getDirectory().getElement("FOURNISSEUR");
483
 
484
                    SQLRow rowFourn = fournisseur.getTable().getRow(idSeleted);
485
 
486
                    this.idFournSelect = rowFourn.getID();
487
                    this.montant.setUE(rowFourn.getBoolean("UE"));
488
                }
489
            }
490
            System.out.println("select id Saisie Achat " + r.getID());
491
        }
492
        this.nomFournisseur.addValueListener(this.listenerModeReglDefaut);
493
 
494
    }
495
 
496
    final SQLTable tablePrefCompte = getTable().getTable("PREFS_COMPTE");
497
 
498
    @Override
499
    protected SQLRowValues createDefaults() {
500
        SQLRowValues vals = new SQLRowValues(this.getTable());
501
        SQLRow r;
502
 
503
        try {
504
            r = ModeReglementDefautPrefPanel.getDefaultRow(false);
505
            SQLElement eltModeReglement = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
506
 
507
            if (r.getID() > 1) {
508
                SQLRowValues rowVals = eltModeReglement.createCopy(r, null);
509
                System.err.println("Primary Keys " + rowVals.getID());
510
                rowVals.clearPrimaryKeys();
511
                System.err.println(rowVals.getInt("ID_TYPE_REGLEMENT"));
512
                vals.put("ID_MODE_REGLEMENT", rowVals);
513
            }
514
        } catch (SQLException e) {
515
            System.err.println("Impossible de sélectionner le mode de règlement par défaut du client.");
516
            e.printStackTrace();
517
        }
518
 
519
        // Select Compte charge par defaut
520
 
521
        final SQLRow rowPrefsCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte).getRowFromId(2);
522
        // compte Achat
523
        int idCompteAchat = rowPrefsCompte.getInt("ID_COMPTE_PCE_ACHAT");
524
        if (idCompteAchat <= 1) {
525
            try {
526
                idCompteAchat = ComptePCESQLElement.getIdComptePceDefault("Achats");
527
            } catch (Exception e) {
528
                e.printStackTrace();
529
            }
530
        }
531
        vals.put("ID_COMPTE_PCE", idCompteAchat);
532
        vals.put("ID_TAXE", 2);
533
        return vals;
534
    }
535
 
536
    public void update() {
537
        if (JOptionPane.showConfirmDialog(this, "Attention en modifiant cette facture, vous supprimerez les chéques et les échéances associés. Continuer?", "Modification de facture",
538
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
539
            super.update();
540
 
541
            // on solde l'avoir
542
            if (this.comboAvoir.getSelectedId() > 1) {
543
                SQLTable tableAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR").getTable();
544
                SQLRow rowAvoir = tableAvoir.getRow(this.comboAvoir.getSelectedId());
545
                SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
546
                rowVals.put("SOLDE", Boolean.TRUE);
547
                try {
548
                    rowVals.update();
549
                } catch (SQLException e) {
550
                    e.printStackTrace();
551
                }
552
            }
553
 
554
            SQLRow row = getTable().getRow(this.getSelectedID());
555
            int idMvt = row.getInt("ID_MOUVEMENT");
556
            System.err.println("__________***************** UPDATE" + idMvt);
557
 
558
            // on supprime tout ce qui est lié à la facture
559
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
560
            eltEcr.archiveMouvementProfondeur(idMvt, false);
561
 
562
            // regenere les ecritures
563
            new GenerationMvtSaisieAchat(this.getSelectedID(), idMvt);
564
        }
565
    }
566
 
567
    private void refreshText() {
568
        Number n = (Number) this.montant.getMontantTTC().getUncheckedValue();
569
        if (this.comboAvoir.getSelectedId() > 1) {
570
            SQLTable tableAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR").getTable();
571
            if (n != null) {
572
                long ttc = n.longValue();
573
                SQLRow rowAvoir = tableAvoir.getRow(this.comboAvoir.getSelectedId());
574
                long totalAvoir = ((Number) rowAvoir.getObject("MONTANT_TTC")).longValue();
575
                this.fieldMontantRegle.setValue(GestionDevise.currencyToString(ttc - totalAvoir));
576
            } else {
577
                this.fieldMontantRegle.setValue(GestionDevise.currencyToString(0));
578
            }
579
        } else {
580
            if (n != null) {
581
                this.fieldMontantRegle.setValue(GestionDevise.currencyToString(n.longValue()));
582
            } else {
583
                this.fieldMontantRegle.setValue(GestionDevise.currencyToString(0));
584
            }
585
        }
586
    }
587
 
588
    public final void loadCommande(int id) {
589
        loadFromTable("COMMANDE", id);
590
    }
591
 
592
    public final void loadBonReception(int id) {
593
        loadFromTable("BON_RECEPTION", id);
594
    }
595
 
596
    private final void loadFromTable(final String tableName, final int id) {
597
        // Mise à jour des totaux
598
        this.montant.setEnabled(false);
599
        this.montant.getChoixTaxe().setVisible(false);
600
        if (id > 1) {
601
            final SQLElement eltCommande = Configuration.getInstance().getDirectory().getElement(tableName);
602
            final SQLInjector injector = SQLInjector.getInjector(eltCommande.getTable(), this.getTable());
603
            this.select(injector.createRowValuesFrom(id));
604
        }
605
    }
606
}