OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 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.core.common.ui.NumericTextField;
156 ilm 17
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
94 ilm 18
import org.openconcerto.erp.core.supplychain.stock.element.ComposedItemStockUpdater;
19
import org.openconcerto.erp.core.supplychain.stock.element.StockItem;
20
import org.openconcerto.erp.core.supplychain.stock.element.StockItem.TypeStockMouvement;
21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.model.DBRoot;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.utils.SQLUtils;
27
import org.openconcerto.sql.view.list.IListe;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.JDate;
30
import org.openconcerto.ui.JLabelBold;
31
import org.openconcerto.utils.ExceptionHandler;
32
import org.openconcerto.utils.NumberUtils;
33
import org.openconcerto.utils.text.SimpleDocumentListener;
34
 
35
import java.awt.GridBagConstraints;
36
import java.awt.GridBagLayout;
37
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionListener;
39
import java.math.BigDecimal;
40
import java.math.RoundingMode;
41
import java.sql.SQLException;
42
import java.text.SimpleDateFormat;
43
import java.util.ArrayList;
44
import java.util.Date;
45
import java.util.List;
46
 
47
import javax.swing.JButton;
48
import javax.swing.JFrame;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JTextField;
52
import javax.swing.SwingConstants;
53
import javax.swing.SwingUtilities;
54
import javax.swing.event.DocumentEvent;
55
 
56
import org.apache.commons.dbutils.ResultSetHandler;
57
 
58
public class InventairePanel extends JPanel {
59
 
60
    private final String mvtStockTableQuoted;
61
    private static String defaultLabel = "Mise à jour des stocks";
62
 
156 ilm 63
    public InventairePanel(final IListe liste, final List<? extends SQLRowAccessor> stocks) {
94 ilm 64
        super(new GridBagLayout());
65
        final SQLTable mvtStockTable = Configuration.getInstance().getRoot().findTable("MOUVEMENT_STOCK");
66
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
67
 
68
        JLabel qteReel = new JLabel("Quantité réel", SwingConstants.RIGHT);
69
        final NumericTextField fieldReel = new NumericTextField();
70
        JLabel qteLivAtt = new JLabel("Quantité Livraison en attente", SwingConstants.RIGHT);
71
        final NumericTextField fieldLivAtt = new NumericTextField();
72
        JLabel qteRecpAtt = new JLabel("Quantité Réception en attente", SwingConstants.RIGHT);
73
        final NumericTextField fieldRecpAtt = new NumericTextField();
74
        JLabel qteTh = new JLabel("Quantité théorique", SwingConstants.RIGHT);
75
        final NumericTextField fieldTh = new NumericTextField();
76
 
77
        GridBagConstraints c = new DefaultGridBagConstraints();
78
 
79
        this.add(new JLabel("Intitulé"), c);
80
        final JTextField label = new JTextField();
81
        c.gridx++;
82
        c.gridwidth = 2;
83
        c.weightx = 1;
84
        this.add(label, c);
85
        label.setText(defaultLabel);
86
 
87
        c.gridy++;
88
        c.gridx = 0;
89
        c.gridwidth = 1;
90
        c.weightx = 0;
91
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
92
        final JDate date = new JDate(true);
93
        c.gridx++;
94
        c.weightx = 0;
95
        this.add(date, c);
96
        c.gridy++;
97
        c.gridx = 0;
98
        c.gridwidth = 3;
99
        this.add(new JLabelBold("Stock réel"), c);
100
 
101
        c.gridy++;
102
        c.gridx = 0;
103
        c.gridwidth = 2;
104
        c.weightx = 0;
105
        this.add(qteReel, c);
106
        c.gridx += 2;
107
        c.gridwidth = 1;
108
        c.weightx = 1;
109
        this.add(fieldReel, c);
110
 
111
        c.gridy++;
112
        c.gridx = 0;
113
        c.gridwidth = 3;
114
        this.add(new JLabelBold("Stock virtuel"), c);
115
 
116
        c.gridy++;
117
        c.gridx = 0;
118
        c.gridwidth = 2;
119
        c.weightx = 0;
120
        this.add(qteLivAtt, c);
121
        c.gridx += 2;
122
        c.gridwidth = 1;
123
        c.weightx = 1;
124
        this.add(fieldLivAtt, c);
125
 
126
        c.gridy++;
127
        c.gridx = 0;
128
        c.gridwidth = 2;
129
        c.weightx = 0;
130
        this.add(qteRecpAtt, c);
131
        c.gridx += 2;
132
        c.gridwidth = 1;
133
        c.weightx = 1;
134
        this.add(fieldRecpAtt, c);
135
 
136
        c.gridy++;
137
        c.gridx = 0;
138
        c.gridwidth = 2;
139
        c.weightx = 0;
140
        this.add(qteTh, c);
141
        c.gridx += 2;
142
        c.gridwidth = 1;
143
        fieldTh.setEditable(false);
144
        c.weightx = 1;
145
        this.add(fieldTh, c);
146
        SimpleDocumentListener l = new SimpleDocumentListener() {
147
 
148
            @Override
149
            public void update(DocumentEvent e) {
150
                BigDecimal qteReel = (fieldReel.getValue() == null ? BigDecimal.ZERO : fieldReel.getValue());
151
                BigDecimal qteLivAttente = (fieldLivAtt.getValue() == null ? BigDecimal.ZERO : fieldLivAtt.getValue());
152
                BigDecimal qteRecptAttente = (fieldRecpAtt.getValue() == null ? BigDecimal.ZERO : fieldRecpAtt.getValue());
153
 
154
                fieldTh.setValue(qteReel.subtract(qteLivAttente).add(qteRecptAttente));
155
            }
156
        };
157
        fieldLivAtt.getDocument().addDocumentListener(l);
158
        fieldReel.getDocument().addDocumentListener(l);
159
        fieldRecpAtt.getDocument().addDocumentListener(l);
160
 
156 ilm 161
        if (stocks.size() == 1) {
162
            SQLRowAccessor r = stocks.get(0);
163
            SQLRowAccessor stock = r;
164
            fieldReel.setText(String.valueOf(stock.getFloat("QTE_REEL")));
165
            fieldLivAtt.setText(String.valueOf(stock.getFloat("QTE_LIV_ATTENTE")));
166
            fieldRecpAtt.setText(String.valueOf(stock.getFloat("QTE_RECEPT_ATTENTE")));
167
            fieldTh.setText(String.valueOf(stock.getFloat("QTE_TH")));
94 ilm 168
        }
169
 
170
        c.gridy++;
171
        c.gridx = 0;
172
        final JButton buttonUpdate = new JButton("Mettre à jour");
173
        JButton buttonCancel = new JButton("Annuler");
174
        JPanel pButton = new JPanel();
175
        pButton.add(buttonCancel);
176
        pButton.add(buttonUpdate);
177
        c.gridwidth = GridBagConstraints.REMAINDER;
178
        c.anchor = GridBagConstraints.EAST;
179
        c.weightx = 0;
180
        c.fill = GridBagConstraints.NONE;
181
        this.add(pButton, c);
182
        buttonCancel.addActionListener(new ActionListener() {
183
 
184
            @Override
185
            public void actionPerformed(ActionEvent e) {
186
                ((JFrame) SwingUtilities.getRoot(InventairePanel.this)).dispose();
187
            }
188
        });
189
        buttonUpdate.addActionListener(new ActionListener() {
190
 
191
            @Override
192
            public void actionPerformed(ActionEvent e) {
193
 
194
                buttonUpdate.setEnabled(false);
195
                defaultLabel = label.getText();
196
                BigDecimal qteReel = fieldReel.getValue();
197
                BigDecimal qteLivAttente = fieldLivAtt.getValue();
198
                BigDecimal qteRecptAttente = fieldRecpAtt.getValue();
199
 
200
                List<List<String>> multipleRequests = new ArrayList<List<String>>();
201
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
202
                boolean usePrice = mvtStockTable.contains("PRICE");
156 ilm 203
                List<StockItem> stockItems = new ArrayList<StockItem>();
94 ilm 204
                final Date dateValue = date.getValue();
156 ilm 205
                for (SQLRowAccessor sqlRowAccessor : stocks) {
94 ilm 206
                    if (multipleRequestsHundred.size() > 100) {
207
                        multipleRequests.add(multipleRequestsHundred);
208
                        multipleRequestsHundred = new ArrayList<String>(100);
209
                    }
156 ilm 210
                    StockItem item = new StockItem(sqlRowAccessor.getForeign("ID_ARTICLE"), sqlRowAccessor);
211
                    if (!item.isStockInit()) {
212
                        ((ReferenceArticleSQLElement) liste.getSource().getElem().getDirectory().getElement("ARTICLE")).initStock(sqlRowAccessor.getForeignID("ID_ARTICLE"));
213
                        SQLRow rowArticle = sqlRowAccessor.getForeign("ID_ARTICLE").asRow();
214
                        rowArticle.fetchValues();
215
                        item = new StockItem(rowArticle, rowArticle.getForeign("ID_STOCK"));
216
                    }
217
                    stockItems.add(item);
94 ilm 218
                    boolean modified = false;
219
                    if (qteReel != null && !NumberUtils.areNumericallyEqual(qteReel, item.getRealQty())) {
220
                        double diff = qteReel.doubleValue() - item.getRealQty();
221
                        item.updateQty(diff, TypeStockMouvement.REEL);
222
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), true, usePrice));
223
                        modified = true;
224
                    }
225
 
226
                    if (qteLivAttente != null && !NumberUtils.areNumericallyEqual(qteLivAttente, item.getDeliverQty())) {
227
                        double diff = item.getDeliverQty() - qteLivAttente.doubleValue();
228
                        item.updateQty(diff, TypeStockMouvement.THEORIQUE, diff > 0);
229
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), false, usePrice));
230
                        modified = true;
231
                    }
232
 
233
                    if (qteRecptAttente != null && !NumberUtils.areNumericallyEqual(qteRecptAttente, item.getReceiptQty())) {
234
                        double diff = qteRecptAttente.doubleValue() - item.getReceiptQty();
235
                        item.updateQty(Math.abs(diff), TypeStockMouvement.THEORIQUE, diff < 0);
236
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), false, usePrice));
237
                        modified = true;
238
                    }
239
                    if (modified) {
240
                        if (item.isStockInit()) {
241
                            multipleRequestsHundred.add(item.getUpdateRequest());
242
                        } else {
156 ilm 243
                            throw new IllegalStateException();
94 ilm 244
                        }
245
                    }
246
 
247
                }
248
                multipleRequests.add(multipleRequestsHundred);
249
                try {
250
                    for (List<String> requests : multipleRequests) {
251
 
252
                        final int size = requests.size();
253
                        List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(size);
254
                        for (int i = 0; i < size; i++) {
255
                            handlers.add(null);
256
                        }
257
                        SQLUtils.executeMultiple(Configuration.getInstance().getRoot().getDBSystemRoot(), requests, handlers);
258
 
259
                    }
260
                } catch (SQLException e1) {
261
                    ExceptionHandler.handle("Stock update error", e1);
262
                }
263
 
264
                final DBRoot root = mvtStockTable.getDBRoot();
265
                if (root.contains("ARTICLE_ELEMENT")) {
156 ilm 266
                    // List<StockItem> stockItems = new ArrayList<StockItem>();
267
                    // for (SQLRowAccessor sqlRowAccessor2 : stocks) {
268
                    // final SQLRow asRow = sqlRowAccessor2.asRow();
269
                    // asRow.fetchValues();
270
                    // stockItems.add(new StockItem(asRow));
271
                    // }
94 ilm 272
                    // Mise à jour des stocks des nomenclatures
273
                    ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
274
                    try {
275
                        comp.update();
276
                    } catch (SQLException e1) {
277
                        e1.printStackTrace();
278
                    }
279
                }
280
 
281
                liste.getModel().updateAll();
282
                ((JFrame) SwingUtilities.getRoot(InventairePanel.this)).dispose();
283
            }
284
        });
285
    }
286
 
287
    private String getMvtRequest(Date time, BigDecimal prc, double qteFinal, StockItem item, String label, boolean reel, boolean usePrice) {
288
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
156 ilm 289
        String mvtStockQuery = "INSERT INTO " + mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"ID_STOCK\",\"NOM\",\"REEL\",\"ORDRE\"";
94 ilm 290
 
291
        if (usePrice && prc != null) {
292
            mvtStockQuery += ",\"PRICE\"";
293
        }
294
 
156 ilm 295
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + "," + item.stock.getID() + ",'" + label + "'," + reel
296
                + ", (SELECT (MAX(\"ORDRE\")+1) FROM " + mvtStockTableQuoted + ")";
94 ilm 297
        if (usePrice && prc != null) {
298
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
299
        }
300
        mvtStockQuery += ")";
301
        return mvtStockQuery;
302
    }
303
}