OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | 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;
17
import org.openconcerto.erp.core.supplychain.stock.element.ComposedItemStockUpdater;
18
import org.openconcerto.erp.core.supplychain.stock.element.StockItem;
19
import org.openconcerto.erp.core.supplychain.stock.element.StockItem.TypeStockMouvement;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.DBRoot;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowValues;
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
 
63
    public InventairePanel(final IListe liste, final List<? extends SQLRowAccessor> articles) {
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
 
161
        if (articles.size() == 1) {
162
            SQLRowAccessor r = articles.get(0);
163
            if (!r.isForeignEmpty("ID_STOCK")) {
164
                SQLRowAccessor stock = r.getForeign("ID_STOCK");
165
                fieldReel.setText(String.valueOf(stock.getFloat("QTE_REEL")));
166
                fieldLivAtt.setText(String.valueOf(stock.getFloat("QTE_LIV_ATTENTE")));
167
                fieldRecpAtt.setText(String.valueOf(stock.getFloat("QTE_RECEPT_ATTENTE")));
168
                fieldTh.setText(String.valueOf(stock.getFloat("QTE_TH")));
169
            }
170
        }
171
 
172
        c.gridy++;
173
        c.gridx = 0;
174
        final JButton buttonUpdate = new JButton("Mettre à jour");
175
        JButton buttonCancel = new JButton("Annuler");
176
        JPanel pButton = new JPanel();
177
        pButton.add(buttonCancel);
178
        pButton.add(buttonUpdate);
179
        c.gridwidth = GridBagConstraints.REMAINDER;
180
        c.anchor = GridBagConstraints.EAST;
181
        c.weightx = 0;
182
        c.fill = GridBagConstraints.NONE;
183
        this.add(pButton, c);
184
        buttonCancel.addActionListener(new ActionListener() {
185
 
186
            @Override
187
            public void actionPerformed(ActionEvent e) {
188
                ((JFrame) SwingUtilities.getRoot(InventairePanel.this)).dispose();
189
            }
190
        });
191
        buttonUpdate.addActionListener(new ActionListener() {
192
 
193
            @Override
194
            public void actionPerformed(ActionEvent e) {
195
 
196
                buttonUpdate.setEnabled(false);
197
                defaultLabel = label.getText();
198
                BigDecimal qteReel = fieldReel.getValue();
199
                BigDecimal qteLivAttente = fieldLivAtt.getValue();
200
                BigDecimal qteRecptAttente = fieldRecpAtt.getValue();
201
 
202
                List<List<String>> multipleRequests = new ArrayList<List<String>>();
203
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
204
                boolean usePrice = mvtStockTable.contains("PRICE");
205
 
206
                final Date dateValue = date.getValue();
207
                for (SQLRowAccessor sqlRowAccessor : articles) {
208
                    if (multipleRequestsHundred.size() > 100) {
209
                        multipleRequests.add(multipleRequestsHundred);
210
                        multipleRequestsHundred = new ArrayList<String>(100);
211
                    }
212
                    StockItem item = new StockItem(sqlRowAccessor);
213
                    boolean modified = false;
214
                    if (qteReel != null && !NumberUtils.areNumericallyEqual(qteReel, item.getRealQty())) {
215
                        double diff = qteReel.doubleValue() - item.getRealQty();
216
                        item.updateQty(diff, TypeStockMouvement.REEL);
217
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), true, usePrice));
218
                        modified = true;
219
                    }
220
 
221
                    if (qteLivAttente != null && !NumberUtils.areNumericallyEqual(qteLivAttente, item.getDeliverQty())) {
222
                        double diff = item.getDeliverQty() - qteLivAttente.doubleValue();
223
                        item.updateQty(diff, TypeStockMouvement.THEORIQUE, diff > 0);
224
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), false, usePrice));
225
                        modified = true;
226
                    }
227
 
228
                    if (qteRecptAttente != null && !NumberUtils.areNumericallyEqual(qteRecptAttente, item.getReceiptQty())) {
229
                        double diff = qteRecptAttente.doubleValue() - item.getReceiptQty();
230
                        item.updateQty(Math.abs(diff), TypeStockMouvement.THEORIQUE, diff < 0);
231
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), false, usePrice));
232
                        modified = true;
233
                    }
234
                    if (modified) {
235
                        if (item.isStockInit()) {
236
                            multipleRequestsHundred.add(item.getUpdateRequest());
237
                        } else {
238
                            SQLRowValues rowVals = new SQLRowValues(sqlRowAccessor.getTable().getForeignTable("ID_STOCK"));
239
                            rowVals.put("QTE_REEL", item.getRealQty());
240
                            rowVals.put("QTE_TH", item.getVirtualQty());
241
                            rowVals.put("QTE_LIV_ATTENTE", item.getDeliverQty());
242
                            rowVals.put("QTE_RECEPT_ATTENTE", item.getReceiptQty());
243
                            SQLRowValues rowValsArt = item.getArticle().createEmptyUpdateRow();
244
                            rowValsArt.put("ID_STOCK", rowVals);
245
                            try {
246
                                rowValsArt.commit();
247
                            } catch (SQLException e1) {
248
                                e1.printStackTrace();
249
                            }
250
                        }
251
                    }
252
 
253
                }
254
                multipleRequests.add(multipleRequestsHundred);
255
                try {
256
                    for (List<String> requests : multipleRequests) {
257
 
258
                        final int size = requests.size();
259
                        List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(size);
260
                        for (int i = 0; i < size; i++) {
261
                            handlers.add(null);
262
                        }
263
                        SQLUtils.executeMultiple(Configuration.getInstance().getRoot().getDBSystemRoot(), requests, handlers);
264
 
265
                    }
266
                } catch (SQLException e1) {
267
                    ExceptionHandler.handle("Stock update error", e1);
268
                }
269
 
270
                final DBRoot root = mvtStockTable.getDBRoot();
271
                if (root.contains("ARTICLE_ELEMENT")) {
272
                    List<StockItem> stockItems = new ArrayList<StockItem>();
273
                    for (SQLRowAccessor sqlRowAccessor2 : articles) {
274
                        final SQLRow asRow = sqlRowAccessor2.asRow();
275
                        asRow.fetchValues();
276
                        stockItems.add(new StockItem(asRow));
277
                    }
278
                    // Mise à jour des stocks des nomenclatures
279
                    ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
280
                    try {
281
                        comp.update();
282
                    } catch (SQLException e1) {
283
                        e1.printStackTrace();
284
                    }
285
                }
286
 
287
                liste.getModel().updateAll();
288
                ((JFrame) SwingUtilities.getRoot(InventairePanel.this)).dispose();
289
            }
290
        });
291
    }
292
 
293
    private String getMvtRequest(Date time, BigDecimal prc, double qteFinal, StockItem item, String label, boolean reel, boolean usePrice) {
294
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
295
        String mvtStockQuery = "INSERT INTO " + mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"NOM\",\"REEL\",\"ORDRE\"";
296
 
297
        if (usePrice && prc != null) {
298
            mvtStockQuery += ",\"PRICE\"";
299
        }
300
 
301
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + ",'" + label + "'," + reel + ", (SELECT (MAX(\"ORDRE\")+1) FROM "
302
                + mvtStockTableQuoted + ")";
303
        if (usePrice && prc != null) {
304
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
305
        }
306
        mvtStockQuery += ")";
307
        return mvtStockQuery;
308
    }
309
}