OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 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.stock.action;
15
 
16
import org.openconcerto.erp.action.CreateFrameAbstractAction;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
19
import org.openconcerto.erp.core.common.ui.ListeViewPanel;
20
import org.openconcerto.erp.core.common.ui.PanelFrame;
21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.element.SQLElement;
23
import org.openconcerto.sql.model.FieldPath;
24
import org.openconcerto.sql.model.SQLBackgroundTableCache;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowAccessor;
27
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.Where;
30
import org.openconcerto.sql.model.graph.Path;
31
import org.openconcerto.sql.view.ListeAddPanel;
32
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
33
import org.openconcerto.sql.view.list.IListe;
34
import org.openconcerto.sql.view.list.SQLTableModelColumn;
35
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
36
import org.openconcerto.ui.DefaultGridBagConstraints;
37
import org.openconcerto.utils.CollectionUtils;
38
import org.openconcerto.utils.DecimalUtils;
39
import org.openconcerto.utils.Tuple2;
40
import org.openconcerto.utils.cc.ITransformer;
41
 
42
import java.awt.GridBagConstraints;
43
import java.awt.GridBagLayout;
44
import java.math.BigDecimal;
45
import java.util.ArrayList;
46
import java.util.List;
47
import java.util.Set;
48
 
49
import javax.swing.Action;
50
import javax.swing.JFrame;
51
import javax.swing.JPanel;
52
import javax.swing.JTabbedPane;
53
 
54
public class ListeDesStocksAction extends CreateFrameAbstractAction {
55
 
56
    public ListeDesStocksAction() {
57
        super();
58
        this.putValue(Action.NAME, "Liste des stocks");
59
 
60
    }
61
 
62
    public JFrame createFrame() {
63
        SQLElement eltStock = Configuration.getInstance().getDirectory().getElement("STOCK");
64
        final SQLTable stockTable = eltStock.getTable();
65
        final SQLTable depotTable = stockTable.getForeignTable("ID_DEPOT_STOCK");
66
 
67
        List<SQLRow> rowsEtat = SQLBackgroundTableCache.getInstance().getCacheForTable(depotTable).getRows();
68
 
69
        JTabbedPane tabs = new JTabbedPane();
70
 
71
        for (final SQLRow sqlRow : rowsEtat) {
72
 
73
            final SQLTableModelSourceOnline tableSource = eltStock.getTableSource(true);
74
 
75
            SQLTableModelColumn colStock;
76
            if (stockTable.getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
77
                colStock = tableSource.getColumn(tableSource.getColumns().size() - 2);
78
            } else {
79
 
80
                colStock = new BaseSQLTableModelColumn("Valeur HT du stock", BigDecimal.class) {
81
 
82
                    @Override
83
                    protected Object show_(SQLRowAccessor stock) {
84
 
85
                        if (stock == null || stock.isUndefined()) {
86
                            return BigDecimal.ZERO;
87
                        } else {
88
                            float qte = stock.getFloat("QTE_REEL");
89
                            BigDecimal ha = stock.getForeign("ID_ARTICLE").getBigDecimal("PA_HT");
90
 
91
                            BigDecimal total = ha.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
92
                            if (total.signum() == 1) {
93
                                return total;
94
                            } else {
95
                                return BigDecimal.ZERO;
96
                            }
97
                        }
98
                    }
99
 
100
                    @Override
101
                    public Set<FieldPath> getPaths() {
102
                        final SQLTable table = stockTable;
103
                        Path p = new Path(table);
104
                        Path p2 = new Path(table).addForeignField("ID_ARTICLE");
105
                        return CollectionUtils.createSet(new FieldPath(p2, "PA_HT"));
106
                    }
107
                };
108
                colStock.setRenderer(ComptaSQLConfElement.CURRENCY_RENDERER);
109
                tableSource.getColumns().add(colStock);
110
            }
111
 
112
            tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
113
 
114
                @Override
115
                public SQLSelect transformChecked(SQLSelect input) {
116
                    input.setWhere(new Where(input.getTable("STOCK").getField("ID_DEPOT_STOCK"), "=", sqlRow.getID()));
117
                    return input;
118
                }
119
            });
120
 
121
            final IListe liste = new IListe(tableSource);
122
            ListeAddPanel panel = new ListeAddPanel(eltStock, liste);
123
            panel.setAddVisible(false);
124
            panel.setDeleteVisible(false);
125
            List<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>> fields = new ArrayList<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>>(1);
126
            fields.add(Tuple2.create(colStock, IListTotalPanel.Type.SOMME));
127
 
128
            IListTotalPanel total = new IListTotalPanel(liste, fields, null, "Total");
129
            GridBagConstraints c2 = new DefaultGridBagConstraints();
130
            c2.gridy = 4;
131
            c2.anchor = GridBagConstraints.EAST;
132
            c2.weightx = 0;
133
            c2.fill = GridBagConstraints.NONE;
134
            panel.add(total, c2);
135
            tabs.add(sqlRow.getString("NOM"), panel);
136
        }
137
        JPanel panel = new JPanel(new GridBagLayout());
138
        GridBagConstraints c = new DefaultGridBagConstraints();
139
        c.fill = GridBagConstraints.BOTH;
140
        c.weightx = 1;
141
        c.weighty = 1;
142
        panel.add(tabs, c);
143
        return new PanelFrame(panel, "Liste des stocks");
144
    }
145
}