OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | 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.credit.component;
15
 
93 ilm 16
import java.awt.GridBagConstraints;
17
import java.awt.GridBagLayout;
18
import java.awt.event.ActionEvent;
19
import java.awt.event.ActionListener;
20
import java.beans.PropertyChangeEvent;
21
import java.beans.PropertyChangeListener;
22
import java.util.Date;
23
 
24
import javax.swing.JCheckBox;
25
import javax.swing.JLabel;
26
import javax.swing.JPanel;
27
import javax.swing.JScrollPane;
28
import javax.swing.JTextField;
29
import javax.swing.SwingConstants;
30
 
18 ilm 31
import org.openconcerto.erp.config.ComptaPropsConfiguration;
32
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
63 ilm 33
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
61 ilm 34
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18 ilm 35
import org.openconcerto.erp.core.common.ui.MontantPanel;
36
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
37
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
80 ilm 38
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 39
import org.openconcerto.erp.generationEcritures.GenerationMvtAvoirFournisseur;
40
import org.openconcerto.erp.model.ISQLCompteSelector;
41
import org.openconcerto.sql.Configuration;
42
import org.openconcerto.sql.element.ElementSQLObject;
43
import org.openconcerto.sql.element.SQLElement;
44
import org.openconcerto.sql.model.SQLBase;
45
import org.openconcerto.sql.model.SQLRow;
46
import org.openconcerto.sql.model.SQLRowAccessor;
47
import org.openconcerto.sql.model.SQLRowValues;
48
import org.openconcerto.sql.model.SQLTable;
49
import org.openconcerto.sql.sqlobject.ElementComboBox;
50
import org.openconcerto.ui.DefaultGridBagConstraints;
41 ilm 51
import org.openconcerto.ui.FormLayouter;
18 ilm 52
import org.openconcerto.ui.JDate;
53
import org.openconcerto.ui.TitledSeparator;
73 ilm 54
import org.openconcerto.ui.component.ITextArea;
93 ilm 55
import org.openconcerto.ui.component.InteractionMode;
73 ilm 56
import org.openconcerto.utils.ExceptionHandler;
18 ilm 57
 
58
public class AvoirFournisseurSQLComponent extends TransfertBaseSQLComponent implements ActionListener {
59
 
60
    private JTextField textNom;
61
    private JDate date;
62
    private ElementComboBox selectFournisseur;
63
    private MontantPanel montantPanel;
64
    private JTextField textNumero = new JTextField(16);
65
    private JCheckBox boxAdeduire = new JCheckBox(getLabelFor("A_DEDUIRE"));
66
    private JCheckBox boxImmo = new JCheckBox(getLabelFor("IMMO"));
67
    private ElementSQLObject eltModeRegl;
68
    private PropertyChangeListener listenerModeReglDefaut = new PropertyChangeListener() {
69
 
70
        public void propertyChange(PropertyChangeEvent evt) {
71
 
61 ilm 72
            SQLRow rowSelected = AvoirFournisseurSQLComponent.this.selectFournisseur.getSelectedRow();
18 ilm 73
 
61 ilm 74
            if (rowSelected != null && !rowSelected.isUndefined()) {
75
                rowSelected = rowSelected.asRow();
76
                AvoirFournisseurSQLComponent.this.montantPanel.setUE(rowSelected.getBoolean("UE"));
77
                int idModeRegl = rowSelected.getInt("ID_MODE_REGLEMENT");
18 ilm 78
 
79
                if (idModeRegl > 1 && AvoirFournisseurSQLComponent.this.eltModeRegl != null) {
80
                    SQLElement sqlEltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
81
                    SQLRow rowModeRegl = sqlEltModeRegl.getTable().getRow(idModeRegl);
82
                    SQLRowValues rowVals = rowModeRegl.createUpdateRow();
83
                    rowVals.clearPrimaryKeys();
84
                    AvoirFournisseurSQLComponent.this.eltModeRegl.setValue(rowVals);
85
                }
86
 
87
            } else {
88
                AvoirFournisseurSQLComponent.this.montantPanel.setUE(false);
89
            }
90
        }
91
 
92
    };
93
 
94
    @Override
95
    protected SQLRowValues createDefaults() {
96
        SQLRowValues vals = new SQLRowValues(this.getTable());
97
        vals.put("A_DEDUIRE", Boolean.TRUE);
98
 
99
        // Select Compte charge par defaut
100
        final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
101
        final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
102
        final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
103
        // compte Achat
104
        int idCompteAchat = rowPrefsCompte.getInt("ID_COMPTE_PCE_ACHAT");
105
        if (idCompteAchat <= 1) {
106
            try {
107
                idCompteAchat = ComptePCESQLElement.getIdComptePceDefault("Achats");
108
            } catch (Exception e) {
109
                e.printStackTrace();
110
            }
111
        }
112
        vals.put("ID_COMPTE_PCE", idCompteAchat);
177 ilm 113
        vals.put("ID_TAXE", TaxeCache.getCache().getFirstTaxeAchat().getID());
93 ilm 114
        vals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), new Date()));
18 ilm 115
        return vals;
116
    }
117
 
118
    public AvoirFournisseurSQLComponent() {
119
 
120
        super(Configuration.getInstance().getDirectory().getElement("AVOIR_FOURNISSEUR"));
121
    }
122
 
123
    public void addViews() {
124
        this.setLayout(new GridBagLayout());
125
 
126
        final GridBagConstraints c = new DefaultGridBagConstraints();
127
 
41 ilm 128
        // Champ Module
129
        c.gridx = 0;
130
        c.gridy++;
131
        c.gridwidth = GridBagConstraints.REMAINDER;
63 ilm 132
        final JPanel addP = ComptaSQLConfElement.createAdditionalPanel();
90 ilm 133
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 2));
41 ilm 134
        this.add(addP, c);
135
 
136
        c.gridy++;
137
        c.gridwidth = 1;
138
 
18 ilm 139
        this.textNom = new JTextField();
93 ilm 140
 
18 ilm 141
        this.selectFournisseur = new ElementComboBox();
142
        this.montantPanel = new MontantPanel();
143
 
144
        // Numero
145
        this.add(new JLabel(getLabelFor("NUMERO"), SwingConstants.RIGHT), c);
146
        c.gridx++;
147
        c.weightx = 1;
148
        c.fill = GridBagConstraints.NONE;
149
        DefaultGridBagConstraints.lockMinimumSize(this.textNumero);
150
        this.add(this.textNumero, c);
151
 
152
        // Date
153
        c.gridx++;
154
        c.weightx = 0;
155
        c.fill = GridBagConstraints.HORIZONTAL;
156
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
157
        c.gridx++;
158
        c.weightx = 0;
159
        c.fill = GridBagConstraints.NONE;
93 ilm 160
        this.date = new JDate(true);
18 ilm 161
        this.add(this.date, c);
162
 
163
        // Libellé
164
        c.gridwidth = 1;
165
        c.weightx = 0;
166
        c.gridy++;
167
        c.fill = GridBagConstraints.HORIZONTAL;
168
        c.gridx = 0;
169
        final JLabel labelLib = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);
170
        this.add(labelLib, c);
171
        c.gridx++;
172
        c.weightx = 1;
173
        c.gridwidth = 3;
174
        this.add(this.textNom, c);
175
 
176
        // Fournisseur
177
        c.gridx = 0;
178
        c.gridy++;
179
        c.fill = GridBagConstraints.HORIZONTAL;
180
        c.anchor = GridBagConstraints.WEST;
181
        c.weightx = 0;
182
        c.gridwidth = 1;
183
        final JLabel labelFourn = new JLabel(getLabelFor("ID_FOURNISSEUR"), SwingConstants.RIGHT);
184
 
185
        this.add(labelFourn, c);
186
 
187
        c.gridx++;
188
        c.fill = GridBagConstraints.NONE;
189
        c.gridwidth = GridBagConstraints.REMAINDER;
190
        c.weightx = 0;
191
        this.add(this.selectFournisseur, c);
192
 
193
        c.gridx = 0;
194
        c.gridy++;
80 ilm 195
        c.fill = GridBagConstraints.HORIZONTAL;
196
        c.gridwidth = 1;
197
        this.add(new JLabel("Montant", SwingConstants.RIGHT), c);
18 ilm 198
        c.weighty = 0;
199
        c.anchor = GridBagConstraints.WEST;
200
        c.gridwidth = GridBagConstraints.REMAINDER;
80 ilm 201
        c.gridx++;
18 ilm 202
        this.montantPanel = new MontantPanel();
203
        this.add(this.montantPanel, c);
204
 
205
        // /
206
 
207
        TitledSeparator title = new TitledSeparator("Comptabilité");
208
        c.gridx = 0;
209
        c.fill = GridBagConstraints.HORIZONTAL;
210
        c.gridy++;
211
        c.gridwidth = 4;
212
        this.add(title, c);
213
 
214
        // Ligne 5 Compte de charges
215
        c.gridy++;
216
        c.weightx = 0;
217
        c.gridx = 0;
218
        c.fill = GridBagConstraints.NONE;
219
        c.gridwidth = 1;
220
        final ISQLCompteSelector compteSel = new ISQLCompteSelector();
221
        this.add(new JLabel(getLabelFor("ID_COMPTE_PCE")), c);
222
        c.gridwidth = GridBagConstraints.REMAINDER;
223
        c.gridx++;
224
        c.weightx = 1;
225
        this.add(compteSel, c);
226
 
227
        // Immo
228
        c.gridwidth = 1;
229
 
230
        c.anchor = GridBagConstraints.WEST;
231
        c.gridwidth = GridBagConstraints.REMAINDER;
80 ilm 232
        c.gridx = 1;
18 ilm 233
        c.gridy++;
234
        c.weighty = 0;
235
        this.add(this.boxImmo, c);
236
 
237
        // Mode de règlement
238
        // this.addView("ID_MODE_REGLEMENT", DEC + ";" + SEP);
239
        // eltModeRegl = (ElementSQLObject) this.getView("ID_MODE_REGLEMENT");
240
        //
241
        // JPanel panelBottom = new JPanel();
242
        // panelBottom.add(eltModeRegl);
243
 
244
        /*******************************************************************************************
245
         * * CALCUL DES TOTAUX
246
         ******************************************************************************************/
247
 
248
        // c.anchor = GridBagConstraints.WEST;
249
        // c.gridwidth = GridBagConstraints.REMAINDER;
250
        // c.gridx = 0;
251
        // c.gridy++;
252
        // c.weighty = 0;
253
        // this.add(this.boxAdeduire, c);
254
        //
255
        // c.anchor = GridBagConstraints.WEST;
256
        // c.gridwidth = GridBagConstraints.REMAINDER;
257
        // c.gridx = 0;
258
        // c.gridy++;
259
        // c.weighty = 0;
260
        // this.add(panelBottom, c);
261
        // Filler
73 ilm 262
 
263
        c.gridheight = 1;
264
        c.gridx = 0;
265
        c.gridy++;
80 ilm 266
        c.fill = GridBagConstraints.HORIZONTAL;
73 ilm 267
        c.gridwidth = GridBagConstraints.REMAINDER;
80 ilm 268
        this.add(new TitledSeparator(getLabelFor("INFOS")), c);
73 ilm 269
 
270
        c.gridy++;
271
        c.fill = GridBagConstraints.BOTH;
272
        c.weighty = 1;
273
        c.gridwidth = GridBagConstraints.REMAINDER;
274
        ITextArea infos = new ITextArea(4, 4);
275
        infos.setBorder(null);
276
        JScrollPane scrollPane = new JScrollPane(infos);
277
        DefaultGridBagConstraints.lockMinimumSize(scrollPane);
278
        this.add(scrollPane, c);
279
        if (getTable().contains("INFOS")) {
280
            this.addSQLObject(infos, "INFOS");
281
        }
282
 
18 ilm 283
        JPanel p = new JPanel();
284
 
285
        p.setOpaque(false);
286
        c.gridy++;
287
        c.weighty = 1;
288
        this.add(p, c);
289
 
290
        this.addSQLObject(this.textNom, "NOM");
291
        // this.addSQLObject(this.boxAdeduire, "A_DEDUIRE");
292
        this.addRequiredSQLObject(this.textNumero, "NUMERO");
293
        this.addRequiredSQLObject(this.date, "DATE");
294
        this.addRequiredSQLObject(this.selectFournisseur, "ID_FOURNISSEUR");
295
        this.addRequiredSQLObject(this.montantPanel.getMontantHT(), "MONTANT_HT");
296
        this.addRequiredSQLObject(this.montantPanel.getMontantTVA(), "MONTANT_TVA");
297
        this.addRequiredSQLObject(this.montantPanel.getMontantTTC(), "MONTANT_TTC");
298
        this.addRequiredSQLObject(this.montantPanel.getChoixTaxe(), "ID_TAXE");
299
        this.addSQLObject(compteSel, "ID_COMPTE_PCE");
300
        this.addSQLObject(this.boxImmo, "IMMO");
301
        this.selectFournisseur.addValueListener(this.listenerModeReglDefaut);
302
        // this.boxAdeduire.addActionListener(this);
303
 
80 ilm 304
        this.montantPanel.setChoixTaxe(TaxeCache.getCache().getFirstTaxe().getID());
18 ilm 305
    }
306
 
307
    @Override
308
    public int insert(SQLRow order) {
309
 
310
        int id = getSelectedID();
311
 
312
        id = super.insert(order);
73 ilm 313
        try {
314
            final SQLTable tableNum = this.getTable().getBase().getTable("NUMEROTATION_AUTO");
315
            final SQLRow row = getTable().getRow(id);
18 ilm 316
 
73 ilm 317
            // incrémentation du numéro auto
93 ilm 318
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), row.getDate("DATE").getTime()).equalsIgnoreCase(this.textNumero.getText().trim())) {
73 ilm 319
                SQLRowValues rowVals = new SQLRowValues(tableNum);
320
                int val = tableNum.getRow(2).getInt("AVOIR_F_START");
321
                val++;
322
                rowVals.put("AVOIR_F_START", Integer.valueOf(val));
61 ilm 323
                rowVals.update(2);
324
 
325
            }
73 ilm 326
 
93 ilm 327
            GenerationMvtAvoirFournisseur gen = new GenerationMvtAvoirFournisseur(row);
73 ilm 328
            gen.genereMouvement();
329
        } catch (Exception e) {
330
            ExceptionHandler.handle("Erreur d'insertion", e);
61 ilm 331
        }
18 ilm 332
        return id;
333
    }
334
 
335
    @Override
336
    public void select(SQLRowAccessor r) {
337
        this.selectFournisseur.rmValueListener(this.listenerModeReglDefaut);
338
        super.select(r);
339
        if (r != null && r.getID() > 1) {
340
            int i = r.getInt("ID_FOURNISSEUR");
341
            if (i > 1) {
342
                SQLRowAccessor rowFourn = r.getForeign("ID_FOURNISSEUR");
343
                if (rowFourn != null && rowFourn.getID() > 1) {
344
                    AvoirFournisseurSQLComponent.this.montantPanel.setUE(rowFourn.getBoolean("UE"));
345
                }
346
            }
347
        }
348
        this.selectFournisseur.addValueListener(this.listenerModeReglDefaut);
349
    }
350
 
351
    @Override
352
    public void update() {
353
        super.update();
73 ilm 354
        try {
355
            SQLRow row = getTable().getRow(getSelectedID());
18 ilm 356
 
73 ilm 357
            int idMvt = row.getInt("ID_MOUVEMENT");
18 ilm 358
 
73 ilm 359
            // on supprime tout ce qui est lié à la facture d'avoir
360
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
361
            eltEcr.archiveMouvementProfondeur(idMvt, false);
18 ilm 362
 
93 ilm 363
            GenerationMvtAvoirFournisseur gen = new GenerationMvtAvoirFournisseur(row, idMvt);
73 ilm 364
            gen.genereMouvement();
365
        } catch (Exception e) {
366
            ExceptionHandler.handle("Erreur Archivage des fils", e);
367
        }
18 ilm 368
    }
369
 
370
    public void actionPerformed(ActionEvent e) {
371
        if (e.getSource() == this.boxAdeduire) {
372
            if (this.eltModeRegl != null) {
93 ilm 373
                final boolean b = this.boxAdeduire.isSelected();
374
                InteractionMode mode = null;
375
                this.eltModeRegl.setEditable((!b) ? InteractionMode.READ_WRITE : InteractionMode.DISABLED);
376
                this.eltModeRegl.setCreated(!b);
18 ilm 377
            }
378
        }
379
    }
380
 
93 ilm 381
    @Override
382
    protected void refreshAfterSelect(SQLRowAccessor rSource) {
383
        // TODO Auto-generated method stub
384
 
385
    }
386
 
18 ilm 387
}