OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.sales.product.action;
14
 package org.openconcerto.erp.core.sales.product.action;
15
 
15
 
16
import org.openconcerto.erp.action.CreateFrameAbstractAction;
16
import org.openconcerto.erp.action.CreateFrameAbstractAction;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
-
 
18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
-
 
19
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
-
 
20
import org.openconcerto.erp.core.sales.product.ui.FamilleArticlePanel;
17
import org.openconcerto.erp.core.sales.product.ui.ListeDesArticlesFrame;
21
import org.openconcerto.erp.panel.ITreeSelection;
-
 
22
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.element.SQLElement;
-
 
24
import org.openconcerto.sql.model.FieldPath;
-
 
25
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
26
import org.openconcerto.sql.model.SQLRowValues;
-
 
27
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
-
 
28
import org.openconcerto.sql.model.SQLSelect;
-
 
29
import org.openconcerto.sql.model.SQLTable;
-
 
30
import org.openconcerto.sql.model.Where;
-
 
31
import org.openconcerto.sql.model.graph.Path;
-
 
32
import org.openconcerto.sql.preferences.SQLPreferences;
-
 
33
import org.openconcerto.sql.request.ListSQLRequest;
-
 
34
import org.openconcerto.sql.request.UpdateBuilder;
-
 
35
import org.openconcerto.sql.view.ListeAddPanel;
-
 
36
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
-
 
37
import org.openconcerto.sql.view.list.IListe;
-
 
38
import org.openconcerto.sql.view.list.SQLTableModelColumn;
-
 
39
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
-
 
40
import org.openconcerto.ui.DefaultGridBagConstraints;
-
 
41
import org.openconcerto.ui.PanelFrame;
-
 
42
import org.openconcerto.utils.CollectionUtils;
-
 
43
import org.openconcerto.utils.DecimalUtils;
-
 
44
import org.openconcerto.utils.Tuple2;
-
 
45
import org.openconcerto.utils.cc.ITransformer;
-
 
46
 
-
 
47
import java.awt.GridBagConstraints;
-
 
48
import java.awt.GridBagLayout;
-
 
49
import java.awt.Insets;
-
 
50
import java.awt.event.ActionEvent;
-
 
51
import java.awt.event.ActionListener;
-
 
52
import java.beans.PropertyChangeEvent;
-
 
53
import java.beans.PropertyChangeListener;
-
 
54
import java.math.BigDecimal;
-
 
55
import java.util.ArrayList;
-
 
56
import java.util.HashSet;
-
 
57
import java.util.List;
-
 
58
import java.util.Set;
-
 
59
 
19
 
60
import javax.swing.Action;
20
import javax.swing.Action;
61
import javax.swing.JButton;
-
 
62
import javax.swing.JFrame;
21
import javax.swing.JFrame;
63
import javax.swing.JLabel;
-
 
64
import javax.swing.JPanel;
-
 
65
import javax.swing.JScrollPane;
-
 
66
import javax.swing.JSplitPane;
-
 
67
import javax.swing.event.TableModelEvent;
-
 
68
import javax.swing.event.TableModelListener;
-
 
69
 
22
 
70
public class ListeDesArticlesAction extends CreateFrameAbstractAction {
23
public class ListeDesArticlesAction extends CreateFrameAbstractAction {
71
 
24
 
72
    private PanelFrame panelFrame;
-
 
73
    String title = "Liste des articles";
-
 
74
    private final SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
-
 
75
    private final SQLTable sqlTableFamilleArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("FAMILLE_ARTICLE");
-
 
76
 
-
 
77
    public ListeDesArticlesAction() {
25
    public ListeDesArticlesAction() {
78
        super();
26
        super();
79
        this.putValue(Action.NAME, "Liste des articles");
27
        this.putValue(Action.NAME, "Liste des articles");
80
    }
28
    }
81
 
29
 
82
    public JFrame createFrame() {
-
 
83
        final SQLElement elt = Configuration.getInstance().getDirectory().getElement(this.sqlTableArticle);
-
 
84
        final FamilleArticlePanel panelFam = new FamilleArticlePanel(elt.getForeignElement("ID_FAMILLE_ARTICLE"));
-
 
85
 
-
 
86
        final SQLTableModelSourceOnline createTableSource = elt.createTableSource();
-
 
87
        SQLPreferences prefs = SQLPreferences.getMemCached(elt.getTable().getDBRoot());
-
 
88
 
-
 
89
        SQLTableModelColumn colStock = null;
-
 
90
        // if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false)) {
-
 
91
        if (elt.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
-
 
92
            colStock = createTableSource.getColumn(createTableSource.getColumns().size() - 2);
-
 
93
        } else {
-
 
94
 
-
 
95
            colStock = new BaseSQLTableModelColumn("Valeur HT du stock", BigDecimal.class) {
-
 
96
 
-
 
97
                @Override
-
 
98
                protected Object show_(SQLRowAccessor r) {
-
 
99
 
-
 
100
                    SQLRowAccessor stock = r.getForeign("ID_STOCK");
-
 
101
                    if (stock == null || stock.isUndefined()) {
-
 
102
                        return BigDecimal.ZERO;
-
 
103
                    } else {
-
 
104
                        float qte = stock.getFloat("QTE_REEL");
-
 
105
                        BigDecimal ha = r.getBigDecimal("PA_HT");
-
 
106
 
-
 
107
                        BigDecimal total = ha.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
-
 
108
                        if (total.signum() == 1) {
-
 
109
                            return total;
-
 
110
                        } else {
-
 
111
                            return BigDecimal.ZERO;
-
 
112
                        }
-
 
113
                    }
-
 
114
                }
-
 
115
 
-
 
116
                @Override
-
 
117
                public Set<FieldPath> getPaths() {
-
 
118
                    final SQLTable table = elt.getTable();
-
 
119
                    Path p = new Path(table);
-
 
120
                    Path p2 = new Path(table).addForeignField("ID_STOCK");
-
 
121
                    return CollectionUtils.createSet(new FieldPath(p, "PA_HT"), new FieldPath(p2, "QTE_REEL"));
-
 
122
                }
-
 
123
            };
-
 
124
            colStock.setRenderer(ComptaSQLConfElement.CURRENCY_RENDERER);
-
 
125
            createTableSource.getColumns().add(colStock);
-
 
126
        }
-
 
127
        // }
-
 
128
        final IListe liste = new IListe(createTableSource);
-
 
129
 
-
 
130
        final ListeAddPanel panel = new ListeAddPanel(elt, liste) {
-
 
131
            @Override
-
 
132
            protected void handleAction(final JButton source, final ActionEvent evt) {
-
 
133
                if (source == this.buttonEffacer && getListe().fetchSelectedRow() != null) {
-
 
134
                    JPanel panel = new JPanel();
-
 
135
                    GridBagConstraints c = new DefaultGridBagConstraints();
-
 
136
                    c.gridwidth = 2;
-
 
137
                    panel.add(new JLabel("Voulez vous supprimer ou rendre obsoléte?"), c);
-
 
138
                    JButton buttonObs = new JButton("Obsolète");
-
 
139
                    JButton buttonSuppr = new JButton("Supprimer");
-
 
140
                    c.gridy++;
-
 
141
                    panel.add(buttonObs, c);
-
 
142
                    c.gridx++;
-
 
143
                    panel.add(buttonSuppr, c);
-
 
144
 
-
 
145
                    final JFrame frame = new PanelFrame(panel, "Suppression d'article");
-
 
146
                    buttonObs.addActionListener(new ActionListener() {
-
 
147
 
-
 
148
                        @Override
-
 
149
                        public void actionPerformed(ActionEvent e) {
-
 
150
                            List<SQLRowValues> rowVals = liste.getSelectedRows();
-
 
151
                            UpdateBuilder build = new UpdateBuilder(sqlTableArticle);
-
 
152
                            build.setObject("OBSOLETE", Boolean.TRUE);
-
 
153
                            List<Integer> ids = new ArrayList<Integer>();
-
 
154
                            for (SQLRowValues sqlRowValues : rowVals) {
-
 
155
                                ids.add(sqlRowValues.getID());
-
 
156
                            }
-
 
157
                            build.setWhere(new Where(sqlTableArticle.getKey(), ids));
-
 
158
                            sqlTableArticle.getDBSystemRoot().getDataSource().execute(build.asString());
-
 
159
                            frame.dispose();
-
 
160
                        }
-
 
161
                    });
-
 
162
                    buttonSuppr.addActionListener(new ActionListener() {
-
 
163
 
-
 
164
                        @Override
-
 
165
                        public void actionPerformed(ActionEvent e) {
-
 
166
                            frame.dispose();
-
 
167
                            SuperHandleAction(source, evt);
-
 
168
                        }
-
 
169
                    });
-
 
170
                    frame.pack();
-
 
171
                    frame.setResizable(false);
-
 
172
                    frame.setLocationRelativeTo(null);
-
 
173
                    frame.setVisible(true);
-
 
174
                } else {
-
 
175
                    super.handleAction(source, evt);
-
 
176
                }
-
 
177
            }
-
 
178
 
-
 
179
            public void SuperHandleAction(final JButton source, final ActionEvent evt) {
-
 
180
                super.handleAction(source, evt);
-
 
181
            }
-
 
182
        };
-
 
183
 
-
 
184
        panel.getListe().getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
185
            @Override
-
 
186
            public SQLSelect transformChecked(SQLSelect input) {
-
 
187
                input.setWhere(getWhere(panelFam, input));
-
 
188
                return input;
-
 
189
            }
-
 
190
        });
-
 
191
 
-
 
192
        // if (elt.getTable().getDBRoot().contains("TARIF_AGENCE")) {
-
 
193
        // if (!UserRightsManager.getCurrentUserRights().haveRight("MODIF_PRODUCT_KIT")) {
-
 
194
        // liste.addSelectionDataListener(new PropertyChangeListener() {
-
 
195
        //
-
 
196
        // @Override
-
 
197
        // public void propertyChange(PropertyChangeEvent evt) {
-
 
198
        // SQLRowValues rowVals = IListe.get(evt).getSelectedRow();
-
 
199
        // if (rowVals != null) {
-
 
200
        // int nbComp =
-
 
201
        // rowVals.getReferentRows(rowVals.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT")).size();
-
 
202
        //
-
 
203
        // panel.setModifyVisible(nbComp == 0 || (rowVals != null &&
-
 
204
        // rowVals.getForeignID("ID_USER_COMMON_CREATE") == UserManager.getUserID()));
-
 
205
        // panel.setDeleteVisible(nbComp == 0 || (rowVals != null &&
-
 
206
        // rowVals.getForeignID("ID_USER_COMMON_CREATE") == UserManager.getUserID()));
-
 
207
        // }
-
 
208
        // }
-
 
209
        // });
-
 
210
        // }
-
 
211
        // }
-
 
212
 
-
 
213
        if (colStock != null) {
-
 
214
            // && !prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false)) {
-
 
215
            List<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>> fields = new ArrayList<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>>(1);
-
 
216
            fields.add(Tuple2.create(colStock, IListTotalPanel.Type.SOMME));
-
 
217
 
-
 
218
            IListTotalPanel total = new IListTotalPanel(liste, fields, null, "Total");
-
 
219
            GridBagConstraints c2 = new DefaultGridBagConstraints();
-
 
220
            c2.gridy = 4;
-
 
221
            c2.anchor = GridBagConstraints.EAST;
-
 
222
            c2.weightx = 0;
-
 
223
            c2.fill = GridBagConstraints.NONE;
-
 
224
            panel.add(total, c2);
-
 
225
        }
-
 
226
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(panelFam), panel);
-
 
227
        JPanel panelAll = new JPanel(new GridBagLayout());
-
 
228
        GridBagConstraints c = new DefaultGridBagConstraints();
-
 
229
        c.fill = GridBagConstraints.BOTH;
-
 
230
        c.weightx = 1;
-
 
231
        c.weighty = 1;
-
 
232
        c.insets = new Insets(0, 0, 0, 0);
-
 
233
        panelAll.add(pane, c);
-
 
234
 
-
 
235
        final ITreeSelection tree = panelFam.getFamilleTree();
-
 
236
        tree.addValueListener(new PropertyChangeListener() {
-
 
237
            @Override
-
 
238
            public void propertyChange(PropertyChangeEvent evt) {
-
 
239
 
-
 
240
                panel.getListe().getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
241
                    @Override
-
 
242
                    public SQLSelect transformChecked(SQLSelect input) {
-
 
243
                        input.setWhere(getWhere(panelFam, input));
-
 
244
                        return input;
-
 
245
                    }
-
 
246
                });
-
 
247
 
-
 
248
            }
-
 
249
        });
-
 
250
 
-
 
251
        // rafraichir le titre à chaque changement de la liste
-
 
252
        panel.getListe().addListener(new TableModelListener() {
-
 
253
            public void tableChanged(TableModelEvent e) {
-
 
254
                setTitle(panel);
-
 
255
            }
-
 
256
        });
-
 
257
        panel.getListe().addListenerOnModel(new PropertyChangeListener() {
-
 
258
            @Override
-
 
259
            public void propertyChange(PropertyChangeEvent evt) {
-
 
260
                if (evt.getPropertyName() == null || evt.getPropertyName().equals("loading") || evt.getPropertyName().equals("searching"))
-
 
261
                    setTitle(panel);
-
 
262
            }
-
 
263
        });
-
 
264
 
-
 
265
        panelFam.getCheckObsolete().addActionListener(new ActionListener() {
-
 
266
            @Override
30
    @Override
267
            public void actionPerformed(ActionEvent e) {
-
 
268
 
-
 
269
                final ListSQLRequest request = panel.getListe().getRequest();
-
 
270
                request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
271
 
-
 
272
                    @Override
-
 
273
                    public SQLSelect transformChecked(SQLSelect input) {
-
 
274
                        input.setWhere(getWhere(panelFam, input));
-
 
275
                        return input;
-
 
276
                    }
-
 
277
                });
-
 
278
            }
-
 
279
        });
-
 
280
 
-
 
281
        this.panelFrame = new PanelFrame(panelAll, "Liste des articles");
-
 
282
        return this.panelFrame;
31
    public JFrame createFrame() {
283
    }
-
 
284
 
-
 
285
    protected void setTitle(ListeAddPanel panel) {
-
 
286
        String title = this.title;
-
 
287
        if (panel.getListe().getModel().isLoading())
-
 
288
            title += ", chargement en cours";
-
 
289
        if (panel.getListe().getModel().isSearching())
-
 
290
            title += ", recherche en cours";
-
 
291
 
-
 
292
        if (this.panelFrame != null)
-
 
293
            this.panelFrame.setTitle(title);
-
 
294
    }
-
 
295
 
-
 
296
    /**
-
 
297
     * Filtre par rapport à la famille sélectionnée
-
 
298
     * 
-
 
299
     * @param panel
-
 
300
     * @return le where approprié
-
 
301
     */
-
 
302
    public Where getWhere(FamilleArticlePanel panel, SQLSelect request) {
-
 
303
        int id = panel.getFamilleTree().getSelectedID();
-
 
304
 
-
 
305
        Where w = null;
-
 
306
 
-
 
307
        if (panel.getCheckObsolete().isSelected()) {
-
 
308
            w = new Where(this.sqlTableArticle.getField("OBSOLETE"), "=", Boolean.FALSE);
-
 
309
 
-
 
310
            // FIXME Fonctionnement avec multidepot
-
 
311
            // w = w.or(new
-
 
312
            // Where(request.getAlias(this.sqlTableArticle.getForeignTable("ID_STOCK").getField("QTE_REEL")),
-
 
313
            // ">", 0));
-
 
314
        }
-
 
315
 
-
 
316
        if (id > 1) {
-
 
317
            Set<Integer> idsMatch = new HashSet<>();
-
 
318
            idsMatch.add(id);
-
 
319
 
-
 
320
            fillChildren(idsMatch, CollectionUtils.createSet(id));
-
 
321
 
-
 
322
            Where w2 = new Where(this.sqlTableArticle.getField("ID_FAMILLE_ARTICLE"), "=", this.sqlTableFamilleArticle.getKey());
-
 
323
            w2 = w2.and(new Where(this.sqlTableFamilleArticle.getKey(), idsMatch));
-
 
324
            if (w != null) {
-
 
325
                w = w.and(w2);
-
 
326
            } else {
-
 
327
                w = w2;
-
 
328
            }
-
 
329
 
-
 
330
        }
-
 
331
        return w;
-
 
332
    }
-
 
333
 
-
 
334
    private void fillChildren(Set<Integer> idsMatch, Set<Integer> father) {
-
 
335
        SQLRowValues rowVals = new SQLRowValues(this.sqlTableFamilleArticle);
-
 
336
        final String keyFieldName = rowVals.getTable().getKey().getName();
32
        return new ListeDesArticlesFrame(Configuration.getInstance().getDirectory());
337
        rowVals.put(keyFieldName, null);
-
 
338
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
-
 
339
        List<SQLRowValues> children = fetcher.fetch(new Where(this.sqlTableFamilleArticle.getField("ID_FAMILLE_ARTICLE_PERE"), father));
-
 
340
        Set<Integer> childToCheck = new HashSet<>();
-
 
341
        for (SQLRowValues child : children) {
-
 
342
            if (!idsMatch.contains(child.getID())) {
-
 
343
                childToCheck.add(child.getID());
-
 
344
                idsMatch.add(child.getID());
-
 
345
            }
-
 
346
        }
-
 
347
        if (!childToCheck.isEmpty()) {
-
 
348
            fillChildren(idsMatch, childToCheck);
-
 
349
        }
-
 
350
 
-
 
351
    }
33
    }
352
}
34
}