OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.finance.tax.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
182 ilm 17
import org.openconcerto.erp.core.sales.product.element.TaxeCatComptableSQLElement;
18
import org.openconcerto.erp.core.sales.product.ui.TaxeCategorieComptableTable;
80 ilm 19
import org.openconcerto.erp.model.ISQLCompteSelector;
182 ilm 20
import org.openconcerto.sql.Configuration;
18 ilm 21
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
80 ilm 23
import org.openconcerto.sql.model.SQLDataSource;
182 ilm 24
import org.openconcerto.sql.model.SQLRow;
80 ilm 25
import org.openconcerto.sql.model.SQLRowAccessor;
182 ilm 26
import org.openconcerto.sql.model.SQLRowValues;
80 ilm 27
import org.openconcerto.sql.model.Where;
28
import org.openconcerto.sql.request.UpdateBuilder;
182 ilm 29
import org.openconcerto.sql.sqlobject.ElementComboBox;
80 ilm 30
import org.openconcerto.sql.utils.SQLUtils;
31
import org.openconcerto.sql.view.list.IListe;
32
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
33
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 34
import org.openconcerto.ui.DefaultGridBagConstraints;
80 ilm 35
import org.openconcerto.utils.ProductInfo;
18 ilm 36
 
37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
80 ilm 39
import java.awt.event.ActionEvent;
182 ilm 40
import java.awt.event.ActionListener;
80 ilm 41
import java.sql.SQLException;
18 ilm 42
import java.util.ArrayList;
43
import java.util.List;
44
 
80 ilm 45
import javax.swing.AbstractAction;
182 ilm 46
import javax.swing.JButton;
18 ilm 47
import javax.swing.JLabel;
80 ilm 48
import javax.swing.JOptionPane;
49
import javax.swing.JPanel;
18 ilm 50
import javax.swing.JTextField;
80 ilm 51
import javax.swing.SwingConstants;
18 ilm 52
 
53
public class TaxeSQLElement extends ComptaSQLConfElement {
54
 
55
    public TaxeSQLElement() {
56
        super("TAXE", "une taxe", "taxes");
80 ilm 57
 
58
        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Définir par défaut") {
59
 
60
            @Override
61
            public void actionPerformed(ActionEvent e) {
62
 
63
                final SQLRowAccessor row = IListe.get(e).getSelectedRow();
64
                final SQLDataSource ds = row.getTable().getDBSystemRoot().getDataSource();
65
 
66
                try {
67
                    SQLUtils.executeAtomic(ds, new SQLUtils.SQLFactory<Object>() {
68
                        @Override
69
                        public Object create() throws SQLException {
70
                            // Transaction
71
                            UpdateBuilder upRemoveDefault = new UpdateBuilder(row.getTable());
72
                            upRemoveDefault.set("DEFAULT", "FALSE");
73
 
74
                            ds.execute(upRemoveDefault.asString());
75
 
76
                            UpdateBuilder upSetDefault = new UpdateBuilder(row.getTable());
77
                            upSetDefault.set("DEFAULT", "TRUE");
78
                            upSetDefault.setWhere(new Where(row.getTable().getKey(), "=", row.getID()));
79
 
80
                            ds.execute(upSetDefault.asString());
81
 
82
                            JOptionPane.showMessageDialog(null, "Rédémarrez " + ProductInfo.getInstance().getName() + " pour valider le changement de TVA par défaut.");
83
 
84
                            return null;
85
                        }
86
                    });
87
                } catch (Exception ex) {
88
                    throw new IllegalStateException("Erreur lors d'affectation de la nouvelle TVA par défaut", ex);
89
                }
90
 
91
            }
92
        }, true);
93
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());
94
 
95
        this.getRowActions().add(action);
96
 
18 ilm 97
    }
98
 
99
    protected List<String> getListFields() {
100
        final List<String> l = new ArrayList<String>();
101
        l.add("NOM");
102
        l.add("TAUX");
80 ilm 103
        l.add("DEFAULT");
18 ilm 104
        return l;
105
    }
106
 
107
    protected List<String> getComboFields() {
108
        final List<String> l = new ArrayList<String>();
109
        l.add("NOM");
110
        return l;
111
    }
112
 
113
    public SQLComponent createComponent() {
114
        return new BaseSQLComponent(this) {
115
 
182 ilm 116
            TaxeCategorieComptableTable tableCatComptable = new TaxeCategorieComptableTable(getElement().getDirectory().getElement(TaxeCatComptableSQLElement.class));
117
 
18 ilm 118
            @Override
119
            protected void addViews() {
120
 
121
                this.setLayout(new GridBagLayout());
122
                GridBagConstraints c = new DefaultGridBagConstraints();
80 ilm 123
                JLabel labelNom = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);
18 ilm 124
 
125
                this.add(labelNom, c);
126
                c.gridx++;
127
                c.weightx = 1;
80 ilm 128
                c.fill = GridBagConstraints.NONE;
18 ilm 129
                JTextField fieldNom = new JTextField(40);
130
                DefaultGridBagConstraints.lockMinimumSize(fieldNom);
131
                this.add(fieldNom, c);
80 ilm 132
                c.gridx = 0;
133
                c.gridy++;
18 ilm 134
                c.weightx = 0;
80 ilm 135
                c.fill = GridBagConstraints.HORIZONTAL;
136
                JLabel labelTaux = new JLabel(getLabelFor("TAUX"), SwingConstants.RIGHT);
18 ilm 137
                this.add(labelTaux, c);
138
                c.gridx++;
80 ilm 139
                c.fill = GridBagConstraints.NONE;
18 ilm 140
                JTextField fieldTaux = new JTextField(6);
141
                DefaultGridBagConstraints.lockMinimumSize(fieldTaux);
142
                this.add(fieldTaux, c);
143
 
80 ilm 144
                JLabel labelCompteCol = new JLabel(getLabelFor("ID_COMPTE_PCE_COLLECTE"), SwingConstants.RIGHT);
145
                c.fill = GridBagConstraints.HORIZONTAL;
146
                c.gridx = 0;
147
                c.gridy++;
148
                this.add(labelCompteCol, c);
149
                c.gridx++;
150
                c.weightx = 1;
151
                c.gridwidth = GridBagConstraints.REMAINDER;
152
                ISQLCompteSelector compteCol = new ISQLCompteSelector();
153
                this.add(compteCol, c);
18 ilm 154
 
80 ilm 155
                JLabel labelCompteDed = new JLabel(getLabelFor("ID_COMPTE_PCE_DED"), SwingConstants.RIGHT);
156
                c.gridx = 0;
157
                c.gridy++;
158
                c.weightx = 0;
159
                c.gridwidth = 1;
160
                c.fill = GridBagConstraints.HORIZONTAL;
161
                this.add(labelCompteDed, c);
162
                c.gridx++;
163
                c.weightx = 1;
164
 
165
                c.gridwidth = GridBagConstraints.REMAINDER;
166
                ISQLCompteSelector compteDed = new ISQLCompteSelector();
167
                this.add(compteDed, c);
168
 
94 ilm 169
                JLabel labelCompteVente = new JLabel(getLabelFor("ID_COMPTE_PCE_VENTE"), SwingConstants.RIGHT);
170
                c.fill = GridBagConstraints.HORIZONTAL;
171
                c.gridwidth = 1;
172
                c.gridx = 0;
173
                c.gridy++;
174
                this.add(labelCompteVente, c);
175
                c.gridx++;
176
                c.weightx = 1;
177
                c.gridwidth = GridBagConstraints.REMAINDER;
178
                ISQLCompteSelector compteVente = new ISQLCompteSelector();
179
                this.add(compteVente, c);
180
 
181
                JLabel labelCompteVenteS = new JLabel(getLabelFor("ID_COMPTE_PCE_VENTE_SERVICE"), SwingConstants.RIGHT);
182
                c.gridx = 0;
183
                c.gridy++;
184
                c.weightx = 0;
185
                c.gridwidth = 1;
186
                c.fill = GridBagConstraints.HORIZONTAL;
187
                this.add(labelCompteVenteS, c);
188
                c.gridx++;
189
                c.weightx = 1;
190
 
191
                c.gridwidth = GridBagConstraints.REMAINDER;
192
                ISQLCompteSelector compteVenteS = new ISQLCompteSelector();
193
                this.add(compteVenteS, c);
142 ilm 194
 
195
                JLabel labelCompteColIntra = new JLabel(getLabelFor("ID_COMPTE_PCE_COLLECTE_INTRA"), SwingConstants.RIGHT);
196
                c.fill = GridBagConstraints.HORIZONTAL;
197
                c.gridx = 0;
198
                c.gridy++;
156 ilm 199
                c.gridwidth = 1;
200
                c.weightx = 0;
142 ilm 201
                this.add(labelCompteColIntra, c);
202
                c.gridx++;
203
                c.weightx = 1;
204
                c.gridwidth = GridBagConstraints.REMAINDER;
205
                ISQLCompteSelector compteColIntra = new ISQLCompteSelector();
206
                this.add(compteColIntra, c);
207
 
208
                JLabel labelCompteDedIntra = new JLabel(getLabelFor("ID_COMPTE_PCE_DED_INTRA"), SwingConstants.RIGHT);
209
                c.gridx = 0;
210
                c.gridy++;
211
                c.weightx = 0;
212
                c.gridwidth = 1;
213
                c.fill = GridBagConstraints.HORIZONTAL;
214
                this.add(labelCompteDedIntra, c);
215
                c.gridx++;
216
                c.weightx = 1;
217
 
218
                c.gridwidth = GridBagConstraints.REMAINDER;
219
                ISQLCompteSelector compteDedIntra = new ISQLCompteSelector();
220
                this.add(compteDedIntra, c);
221
 
80 ilm 222
                c.gridy++;
182 ilm 223
                c.gridx = 0;
80 ilm 224
                c.weighty = 1;
182 ilm 225
                c.weightx = 1;
226
                c.fill = GridBagConstraints.BOTH;
227
                c.gridwidth = GridBagConstraints.REMAINDER;
80 ilm 228
 
182 ilm 229
                this.add(createCategorieComptablePanel(), c);
230
 
80 ilm 231
                this.addSQLObject(compteCol, "ID_COMPTE_PCE_COLLECTE");
232
                this.addSQLObject(compteDed, "ID_COMPTE_PCE_DED");
94 ilm 233
                this.addSQLObject(compteVente, "ID_COMPTE_PCE_VENTE");
234
                this.addSQLObject(compteVenteS, "ID_COMPTE_PCE_VENTE_SERVICE");
142 ilm 235
                this.addSQLObject(compteColIntra, "ID_COMPTE_PCE_COLLECTE_INTRA");
236
                this.addSQLObject(compteDedIntra, "ID_COMPTE_PCE_DED_INTRA");
80 ilm 237
 
18 ilm 238
                this.addRequiredSQLObject(fieldNom, "NOM");
239
                this.addRequiredSQLObject(fieldTaux, "TAUX");
240
            }
182 ilm 241
 
242
            @Override
243
            public void select(SQLRowAccessor r) {
244
                super.select(r);
245
                if (r != null) {
246
                    this.tableCatComptable.getRowValuesTable().insertFrom(r);
247
                }
248
            }
249
 
250
            @Override
251
            public int insert(SQLRow order) {
252
                int id = super.insert(order);
253
                this.tableCatComptable.updateField("ID_TAXE", id);
254
                return id;
255
            }
256
 
257
            @Override
258
            public void update() {
259
                super.update();
260
                this.tableCatComptable.updateField("ID_TAXE", getSelectedID());
261
            }
262
 
263
            protected JPanel createCategorieComptablePanel() {
264
                JPanel panel = new JPanel(new GridBagLayout());
265
                panel.setOpaque(false);
266
                GridBagConstraints c = new DefaultGridBagConstraints();
267
 
268
                // Ajout catégorie
269
                c.gridwidth = 1;
270
                c.weightx = 0;
271
                c.gridy++;
272
                c.gridx = 0;
273
                panel.add(new JLabel("Ajouter la catégorie "), c);
274
 
275
                final ElementComboBox boxCat = new ElementComboBox();
276
                boxCat.init(getDirectory().getElement("CATEGORIE_COMPTABLE"));
277
 
278
                c.gridx++;
279
                panel.add(boxCat, c);
280
 
281
                c.fill = GridBagConstraints.NONE;
282
                c.gridx++;
283
                JButton buttonAjouter = new JButton("Ajouter");
284
                buttonAjouter.setOpaque(false);
285
                panel.add(buttonAjouter, c);
286
                c.gridx++;
287
                JButton buttonSupprimer = new JButton("Supprimer");
288
                buttonSupprimer.setOpaque(false);
289
                panel.add(buttonSupprimer, c);
290
 
291
                c.gridwidth = GridBagConstraints.REMAINDER;
292
                c.fill = GridBagConstraints.BOTH;
293
                c.weightx = 1;
294
                c.weighty = 1;
295
                c.gridy++;
296
                c.gridx = 0;
297
                c.fill = GridBagConstraints.BOTH;
298
                this.tableCatComptable.setOpaque(false);
299
                panel.add(this.tableCatComptable, c);
300
 
301
                // Listeners
302
                buttonAjouter.addActionListener(new ActionListener() {
303
 
304
                    @Override
305
                    public void actionPerformed(ActionEvent e) {
306
 
307
                        SQLRow rowCat = boxCat.getSelectedRow();
308
                        if (rowCat == null || rowCat.isUndefined()) {
309
                            return;
310
                        }
311
                        int nbRows = tableCatComptable.getModel().getRowCount();
312
 
313
                        for (int i = 0; i < nbRows; i++) {
314
                            SQLRowValues rowVals = tableCatComptable.getModel().getRowValuesAt(i);
315
                            int idTarif = Integer.parseInt(rowVals.getObject("ID_CATEGORIE_COMPTABLE").toString());
316
                            if (idTarif == rowCat.getID()) {
317
                                JOptionPane.showMessageDialog(null, "Impossible d'ajouter.\nLa catégorie est déjà présente dans la liste!");
318
                                return;
319
                            }
320
                        }
321
 
322
                        SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("TAXE_CATEGORIE_COMPTABLE"));
323
                        if (getSelectedID() > 1) {
324
                            rowVals.put("ID_TAXE", getSelectedID());
325
                        }
326
                        rowVals.put("ID_CATEGORIE_COMPTABLE", rowCat.getID());
327
                        tableCatComptable.getModel().addRow(rowVals);
328
                    }
329
                });
330
                buttonSupprimer.addActionListener(new ActionListener() {
331
 
332
                    @Override
333
                    public void actionPerformed(ActionEvent e) {
334
                        tableCatComptable.removeSelectedRow();
335
                    }
336
                });
337
                return panel;
338
            }
18 ilm 339
        };
340
    }
341
 
156 ilm 342
    @Override
343
    protected String createCode() {
344
        return "finance.tax";
345
    }
18 ilm 346
}