OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
174 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.sales.product.model.ProductComponent;
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.element.SQLElement;
23
import org.openconcerto.sql.model.DBRoot;
24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
29
import org.openconcerto.sql.utils.SQLUtils;
30
import org.openconcerto.ui.DefaultGridBagConstraints;
31
import org.openconcerto.ui.JDate;
32
import org.openconcerto.utils.ExceptionHandler;
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.beans.PropertyChangeEvent;
40
import java.beans.PropertyChangeListener;
41
import java.math.BigDecimal;
42
import java.math.RoundingMode;
43
import java.sql.SQLException;
44
import java.text.SimpleDateFormat;
45
import java.util.ArrayList;
46
import java.util.Date;
47
import java.util.List;
48
 
49
import javax.swing.JButton;
50
import javax.swing.JFrame;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53
import javax.swing.JTextField;
54
import javax.swing.SwingConstants;
55
import javax.swing.SwingUtilities;
56
import javax.swing.event.DocumentEvent;
57
 
58
import org.apache.commons.dbutils.ResultSetHandler;
59
 
60
public class TransfertStockPanel extends JPanel {
61
 
62
    private final String mvtStockTableQuoted;
63
    private static String defaultLabel = "Transfert de stock";
64
 
65
    public TransfertStockPanel(Configuration instance) {
66
        super(new GridBagLayout());
67
 
68
        final JButton buttonUpdate = new JButton("Mettre à jour");
69
 
70
        final SQLTable mvtStockTable = instance.getRoot().findTable("MOUVEMENT_STOCK");
71
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
72
 
73
        final SQLElement articleElt = instance.getDirectory().getElement("ARTICLE");
74
        final SQLRequestComboBox comboArticle = new SQLRequestComboBox();
75
        comboArticle.uiInit(articleElt.createComboRequest());
76
 
77
        final SQLElement stockElt = instance.getDirectory().getElement("DEPOT_STOCK");
78
        final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
79
        comboStockDepart.uiInit(stockElt.createComboRequest());
80
 
81
        final SQLRequestComboBox comboStockArrive = new SQLRequestComboBox();
82
        comboStockArrive.uiInit(stockElt.createComboRequest());
83
 
84
        JLabel qteReel = new JLabel("Quantité", SwingConstants.RIGHT);
85
        final NumericTextField fieldReel = new NumericTextField();
86
 
87
        fieldReel.getDocument().addDocumentListener(new SimpleDocumentListener() {
88
 
89
            @Override
90
            public void update(DocumentEvent e) {
91
                buttonUpdate.setEnabled(
92
                        fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null && comboStockDepart.getSelectedRow() != null);
93
            }
94
        });
95
 
96
        comboArticle.addModelListener("wantedID", new PropertyChangeListener() {
97
 
98
            @Override
99
            public void propertyChange(PropertyChangeEvent evt) {
100
                buttonUpdate.setEnabled(
101
                        fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null && comboStockDepart.getSelectedRow() != null);
102
            }
103
        });
104
 
105
        comboStockArrive.addModelListener("wantedID", new PropertyChangeListener() {
106
 
107
            @Override
108
            public void propertyChange(PropertyChangeEvent evt) {
109
                buttonUpdate.setEnabled(
110
                        fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null && comboStockDepart.getSelectedRow() != null);
111
            }
112
        });
113
 
114
        comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {
115
 
116
            @Override
117
            public void propertyChange(PropertyChangeEvent evt) {
118
                buttonUpdate.setEnabled(
119
                        fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null && comboStockDepart.getSelectedRow() != null);
120
            }
121
        });
122
 
123
        GridBagConstraints c = new DefaultGridBagConstraints();
124
 
125
        this.add(new JLabel("Intitulé"), c);
126
        final JTextField label = new JTextField();
127
        c.gridx++;
128
        c.gridwidth = 2;
129
        c.weightx = 1;
130
        this.add(label, c);
131
        label.setText(defaultLabel);
132
 
133
        c.gridy++;
134
        c.gridx = 0;
135
        c.gridwidth = 1;
136
        c.weightx = 0;
137
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
138
        final JDate date = new JDate(true);
139
        c.gridx++;
140
        c.weightx = 0;
141
        this.add(date, c);
142
 
143
        c.gridy++;
144
        c.gridx = 0;
145
        c.gridwidth = 2;
146
        c.weightx = 0;
147
        this.add(new JLabel("Article", SwingConstants.RIGHT), c);
148
        c.gridx += 2;
149
        c.gridwidth = 1;
150
        c.weightx = 1;
151
        this.add(comboArticle, c);
152
 
153
        c.gridy++;
154
        c.gridx = 0;
155
        c.gridwidth = 2;
156
        c.weightx = 0;
157
        this.add(new JLabel("Départ", SwingConstants.RIGHT), c);
158
        c.gridx += 2;
159
        c.gridwidth = 1;
160
        c.weightx = 1;
161
        this.add(comboStockDepart, c);
162
 
163
        c.gridy++;
164
        c.gridx = 0;
165
        c.gridwidth = 2;
166
        c.weightx = 0;
167
        this.add(new JLabel("Arrivée", SwingConstants.RIGHT), c);
168
        c.gridx += 2;
169
        c.gridwidth = 1;
170
        c.weightx = 1;
171
        this.add(comboStockArrive, c);
172
 
173
        c.gridy++;
174
        c.gridx = 0;
175
        c.gridwidth = 2;
176
        c.weightx = 0;
177
        this.add(qteReel, c);
178
        c.gridx += 2;
179
        c.gridwidth = 1;
180
        c.weightx = 1;
181
        this.add(fieldReel, c);
182
 
183
        c.gridy++;
184
        c.gridx = 0;
185
        c.gridwidth = 2;
186
        c.weightx = 0;
187
        this.add(qteReel, c);
188
        c.gridx += 2;
189
        c.gridwidth = 1;
190
        c.weightx = 1;
191
        this.add(fieldReel, c);
192
 
193
        c.gridy++;
194
        c.gridx = 0;
195
        JButton buttonCancel = new JButton("Annuler");
196
        JPanel pButton = new JPanel();
197
        pButton.add(buttonCancel);
198
        pButton.add(buttonUpdate);
199
        c.gridwidth = GridBagConstraints.REMAINDER;
200
        c.anchor = GridBagConstraints.EAST;
201
        c.weightx = 0;
202
        c.fill = GridBagConstraints.NONE;
203
        this.add(pButton, c);
204
        buttonCancel.addActionListener(new ActionListener() {
205
 
206
            @Override
207
            public void actionPerformed(ActionEvent e) {
208
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
209
            }
210
        });
211
        buttonUpdate.addActionListener(new ActionListener() {
212
 
213
            @Override
214
            public void actionPerformed(ActionEvent e) {
215
 
216
                buttonUpdate.setEnabled(false);
217
                BigDecimal qteReel = fieldReel.getValue();
218
 
219
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
220
                boolean usePrice = mvtStockTable.contains("PRICE");
221
                List<StockItem> stockItems = new ArrayList<StockItem>();
222
                final Date dateValue = date.getValue();
223
 
224
                final SQLRow selectedRowArticle = comboArticle.getSelectedRow();
225
 
177 ilm 226
                final SQLRow selectedRowDepotDepart = comboStockDepart.getSelectedRow();
227
                final SQLRow selectedRowDepotArrivee = comboStockArrive.getSelectedRow();
174 ilm 228
                {
229
                    // DEPART
230
                    final SQLRowAccessor rowStockDepart = ProductComponent.findOrCreateStock(selectedRowArticle, selectedRowDepotDepart);
231
                    StockItem item = new StockItem(selectedRowArticle, rowStockDepart);
232
                    if (!item.isStockInit()) {
233
                        SQLRowValues rowVals = new SQLRowValues(mvtStockTable.getTable("STOCK"));
234
                        rowVals.put("ID_ARTICLE", selectedRowArticle.getID());
235
                        rowVals.put("ID_DEPOT_STOCK", selectedRowDepotDepart.getID());
236
                        try {
237
                            rowVals.commit();
238
                        } catch (SQLException e1) {
239
                            // TODO Auto-generated catch block
240
                            e1.printStackTrace();
241
                        }
242
                        selectedRowArticle.fetchValues();
243
                        item = new StockItem(selectedRowArticle, rowStockDepart);
244
                    }
245
                    stockItems.add(item);
246
                    double diff = -qteReel.doubleValue();
247
                    item.updateQty(diff, TypeStockMouvement.REEL);
177 ilm 248
                    multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), true, usePrice));
174 ilm 249
 
250
                    item.updateQty(diff, TypeStockMouvement.THEORIQUE);
177 ilm 251
                    multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), false, usePrice));
174 ilm 252
 
253
                    multipleRequestsHundred.add(item.getUpdateRequest());
254
                }
255
                // ARRIVEE
256
                {
257
                    final SQLRowAccessor rowStockArrivee = ProductComponent.findOrCreateStock(selectedRowArticle, selectedRowDepotArrivee);
258
 
259
                    StockItem item = new StockItem(selectedRowArticle, rowStockArrivee);
260
                    if (!item.isStockInit()) {
261
                        SQLRowValues rowVals = new SQLRowValues(mvtStockTable.getTable("STOCK"));
262
                        rowVals.put("ID_ARTICLE", selectedRowArticle.getID());
263
                        rowVals.put("ID_DEPOT_STOCK", selectedRowDepotArrivee.getID());
264
                        try {
265
                            rowVals.commit();
266
                        } catch (SQLException e1) {
267
                            // TODO Auto-generated catch block
268
                            e1.printStackTrace();
269
                        }
270
                        selectedRowArticle.fetchValues();
271
                        item = new StockItem(selectedRowArticle, rowStockArrivee);
272
                    }
273
                    stockItems.add(item);
274
                    double diff = qteReel.doubleValue();
275
                    item.updateQty(diff, TypeStockMouvement.REEL);
177 ilm 276
                    multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), true, usePrice));
174 ilm 277
 
278
                    item.updateQty(diff, TypeStockMouvement.THEORIQUE);
177 ilm 279
                    multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), false, usePrice));
174 ilm 280
 
281
                    multipleRequestsHundred.add(item.getUpdateRequest());
282
                }
283
 
284
                try {
285
 
286
                    final int size = multipleRequestsHundred.size();
287
                    List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(size);
288
                    for (int i = 0; i < size; i++) {
289
                        handlers.add(null);
290
                    }
291
                    SQLUtils.executeMultiple(instance.getRoot().getDBSystemRoot(), multipleRequestsHundred, handlers);
292
 
293
                } catch (SQLException e1) {
294
                    ExceptionHandler.handle("Stock update error", e1);
295
                }
296
 
297
                final DBRoot root = mvtStockTable.getDBRoot();
298
                if (root.contains("ARTICLE_ELEMENT")) {
299
                    // List<StockItem> stockItems = new ArrayList<StockItem>();
300
                    // for (SQLRowAccessor sqlRowAccessor2 : stocks) {
301
                    // final SQLRow asRow = sqlRowAccessor2.asRow();
302
                    // asRow.fetchValues();
303
                    // stockItems.add(new StockItem(asRow));
304
                    // }
305
                    // Mise à jour des stocks des nomenclatures
306
                    ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
307
                    try {
308
                        comp.update();
309
                    } catch (SQLException e1) {
310
                        e1.printStackTrace();
311
                    }
312
                }
313
 
314
                // liste.getModel().updateAll();
315
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
316
            }
317
        });
318
    }
319
 
177 ilm 320
    private String getLabel(String label, SQLRowAccessor fromDepot, SQLRowAccessor toDepot) {
321
 
322
        return label + " de " + fromDepot.getString("NOM") + " vers " + toDepot.getString("NOM");
323
 
324
    }
325
 
174 ilm 326
    private String getMvtRequest(Date time, BigDecimal prc, double qteFinal, StockItem item, String label, boolean reel, boolean usePrice) {
327
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
328
        String mvtStockQuery = "INSERT INTO " + mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"ID_STOCK\",\"NOM\",\"REEL\",\"ORDRE\"";
329
 
330
        if (usePrice && prc != null) {
331
            mvtStockQuery += ",\"PRICE\"";
332
        }
333
 
334
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + "," + item.stock.getID() + ",'" + label + "'," + reel
335
                + ", (SELECT (MAX(\"ORDRE\")+1) FROM " + mvtStockTableQuoted + ")";
336
        if (usePrice && prc != null) {
337
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
338
        }
339
        mvtStockQuery += ")";
340
        return mvtStockQuery;
341
    }
342
}