OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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