OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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