OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 174 | 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.supplychain.stock.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
156 ilm 17
import org.openconcerto.erp.core.common.ui.PanelFrame;
18
import org.openconcerto.erp.core.sales.product.action.InventairePanel;
19
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
20
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
18 ilm 21
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
156 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
28
import org.openconcerto.sql.sqlobject.ElementComboBox;
29
import org.openconcerto.sql.view.list.IListe;
30
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
31
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 32
import org.openconcerto.ui.DefaultGridBagConstraints;
156 ilm 33
import org.openconcerto.ui.FrameUtil;
34
import org.openconcerto.utils.ExceptionHandler;
132 ilm 35
import org.openconcerto.utils.ListMap;
18 ilm 36
 
37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
156 ilm 39
import java.awt.event.ActionEvent;
40
import java.sql.SQLException;
41
import java.util.Arrays;
42
import java.util.Collection;
43
import java.util.HashSet;
18 ilm 44
import java.util.List;
156 ilm 45
import java.util.Set;
18 ilm 46
 
156 ilm 47
import javax.swing.AbstractAction;
18 ilm 48
import javax.swing.JLabel;
49
import javax.swing.JTextField;
50
 
51
public class StockSQLElement extends ComptaSQLConfElement {
52
 
53
    public StockSQLElement() {
54
        super("STOCK", "un stock", "stocks");
156 ilm 55
        getRowActions().addAll(new MouseSheetXmlListeListener(FicheArticleXmlSheet.class).getRowActions());
56
        PredicateRowAction stock = new PredicateRowAction(new AbstractAction("Mettre à jour les stocks") {
57
 
58
            @Override
59
            public void actionPerformed(ActionEvent e) {
60
 
61
                PanelFrame p = new PanelFrame(new InventairePanel(IListe.get(e), IListe.get(e).getSelectedRows()), "Mise à jour des stocks");
62
                FrameUtil.show(p);
63
 
64
            }
65
        }, true, false);
66
        stock.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
67
        getRowActions().add(stock);
18 ilm 68
    }
69
 
70
    protected List<String> getListFields() {
156 ilm 71
        return Arrays.asList("ID_ARTICLE", "QTE_MIN", "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
18 ilm 72
    }
73
 
156 ilm 74
    @Override
75
    public Set<String> getReadOnlyFields() {
76
        Set<String> s = new HashSet<>();
77
        if (getTable().contains("ID_ARTICLE")) {
78
            s.add("ID_ARTICLE");
79
        }
80
        s.add("QTE_TH");
81
        s.add("QTE_REEL");
82
        s.add("QTE_LIV_ATTENTE");
83
        s.add("QTE_RECEPT_ATTENTE");
84
        if (getTable().contains("ID_DEPOT_STOCK")) {
85
            s.add("ID_DEPOT_STOCK");
86
        }
87
        return s;
88
    }
89
 
90
    @Override
18 ilm 91
    protected List<String> getComboFields() {
156 ilm 92
        return Arrays.asList("ID_DEPOT_STOCK", "QTE_REEL");
18 ilm 93
    }
94
 
83 ilm 95
    @Override
156 ilm 96
    protected String getParentFFName() {
97
        return "ID_ARTICLE";
83 ilm 98
    }
99
 
132 ilm 100
    @Override
101
    public ListMap<String, String> getShowAs() {
156 ilm 102
        if (getTable().contains("ID_DEPOT_STOCK")) {
103
            return ListMap.singleton(null, "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
104
        } else {
105
            return ListMap.singleton(null, "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
106
        }
132 ilm 107
    }
108
 
156 ilm 109
    public static SQLRowAccessor getStockFetched(SQLRowAccessor rowValsSource) {
110
        SQLRowAccessor rowStock = null;
111
        final int idDepot;
112
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
113
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
114
        } else {
115
            idDepot = rowValsSource.getForeign("ID_ARTICLE").getForeignID("ID_DEPOT_STOCK");
116
        }
117
        SQLTable stockTable = rowValsSource.getTable().getTable("STOCK");
118
 
119
        Collection<? extends SQLRowAccessor> rows = rowValsSource.getForeign("ID_ARTICLE").getReferentRows(stockTable);
120
        for (SQLRowAccessor sqlRowAccessor : rows) {
121
            if (sqlRowAccessor.getForeignID("ID_DEPOT_STOCK") == idDepot) {
122
                rowStock = sqlRowAccessor;
123
                break;
124
            }
125
        }
126
 
127
        return rowStock;
128
    }
129
 
130
    public static SQLRowAccessor getStock(SQLRowAccessor rowValsSource) {
131
 
132
        SQLRowAccessor rowStock = null;
133
        final int idDepot;
134
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
135
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
136
        } else {
137
            idDepot = rowValsSource.getForeign("ID_ARTICLE").getForeignID("ID_DEPOT_STOCK");
138
        }
139
        SQLTable stockTable = rowValsSource.getTable().getTable("STOCK");
140
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
141
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
142
 
143
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
144
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot);
145
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsSource.getForeignID("ID_ARTICLE"));
146
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
147
        if (rowValsResult.isEmpty()) {
148
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
149
            rowValsStock.put("ID_ARTICLE", rowValsSource.getForeignID("ID_ARTICLE"));
150
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
151
            rowValsStock.put("QTE_TH", 0F);
152
            rowValsStock.put("QTE_REEL", 0F);
153
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
154
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
155
            try {
156
                rowStock = rowValsStock.insert();
157
                if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
158
                    rowValsSource.getForeign("ID_ARTICLE").createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
159
                }
160
            } catch (SQLException e) {
161
                ExceptionHandler.handle("Erreur lors la création du stock!", e);
162
            }
163
        } else if (rowValsResult.size() == 1) {
164
            rowStock = rowValsResult.iterator().next();
165
        } else if (rowValsResult.size() > 1) {
166
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsSource.getForeign("ID_ARTICLE").getID() + " Depot " + idDepot);
167
        }
168
        return rowStock;
169
    }
170
 
18 ilm 171
    /*
172
     * (non-Javadoc)
173
     *
174
     * @see org.openconcerto.devis.SQLElement#getComponent()
175
     */
176
    public SQLComponent createComponent() {
177
 
178
        return new BaseSQLComponent(this) {
179
 
180
            public void addViews() {
181
                this.setLayout(new GridBagLayout());
182
                final GridBagConstraints c = new DefaultGridBagConstraints();
183
 
156 ilm 184
                // Article
185
                JLabel labelA = new JLabel(getLabelFor("ID_ARTICLE"));
186
                c.weightx = 0;
187
                this.add(labelA, c);
188
 
189
                c.gridx++;
190
                ElementComboBox boxA = new ElementComboBox();
191
                this.add(boxA, c);
192
 
193
                // Depot
194
                JLabel labelD = new JLabel(getLabelFor("ID_DEPOT_STOCK"));
195
                c.gridx++;
196
                c.weightx = 0;
197
                this.add(labelD, c);
198
 
199
                c.gridx++;
200
                ElementComboBox boxD = new ElementComboBox();
201
                this.add(boxD, c);
202
 
18 ilm 203
                // Qté Réelle
204
                JLabel labelQteR = new JLabel(getLabelFor("QTE_REEL"));
156 ilm 205
                c.gridx = 0;
206
                c.gridy++;
207
                c.weightx = 0;
18 ilm 208
                this.add(labelQteR, c);
209
 
210
                c.gridx++;
156 ilm 211
                JTextField textQteReel = new JTextField(6);
212
                this.add(textQteReel, c);
18 ilm 213
 
156 ilm 214
                // Qté Réelle
215
                JLabel labelQteT = new JLabel(getLabelFor("QTE_TH"));
216
                c.gridx++;
217
                c.weightx = 0;
218
                this.add(labelQteT, c);
18 ilm 219
 
156 ilm 220
                c.gridx++;
221
                JTextField textQteT = new JTextField(6);
222
                this.add(textQteT, c);
223
 
224
                // Qté Réelle
225
                JLabel labelQteRe = new JLabel(getLabelFor("QTE_RECEPT_ATTENTE"));
226
                c.gridx = 0;
227
                c.gridy++;
228
                c.weightx = 0;
229
                this.add(labelQteRe, c);
230
 
231
                c.gridx++;
232
                JTextField textQteRe = new JTextField(6);
233
                this.add(textQteRe, c);
234
 
235
                JLabel labelQteL = new JLabel(getLabelFor("QTE_LIV_ATTENTE"));
236
                c.gridx++;
237
                c.weightx = 0;
238
                this.add(labelQteL, c);
239
 
240
                c.gridx++;
241
                JTextField textQteL = new JTextField(6);
242
                this.add(textQteL, c);
243
 
244
                // Qté Min
245
                JLabel labelQteTMin = new JLabel(getLabelFor("QTE_MIN"));
246
                c.gridx = 0;
247
                c.gridy++;
248
                c.weightx = 0;
249
                this.add(labelQteTMin, c);
250
 
251
                c.gridx++;
252
                JTextField textQteMin = new JTextField(6);
253
                this.add(textQteMin, c);
254
 
255
                this.addSQLObject(textQteReel, "QTE_REEL");
256
                this.addSQLObject(textQteT, "QTE_TH");
257
                this.addSQLObject(textQteMin, "QTE_MIN");
258
                this.addSQLObject(textQteL, "QTE_LIV_ATTENTE");
259
                this.addSQLObject(textQteRe, "QTE_RECEPT_ATTENTE");
260
                this.addSQLObject(boxA, "ID_ARTICLE");
261
                this.addSQLObject(boxD, "ID_DEPOT_STOCK");
18 ilm 262
            }
263
        };
264
    }
156 ilm 265
 
266
    @Override
267
    protected String createCode() {
268
        return "supplychain.stock";
269
    }
18 ilm 270
}