OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.sales.product.action;
14
 package org.openconcerto.erp.core.sales.product.action;
15
 
15
 
16
import org.openconcerto.erp.core.common.ui.NumericTextField;
16
import org.openconcerto.erp.core.common.ui.NumericTextField;
17
import org.openconcerto.erp.core.sales.product.model.ProductComponent;
17
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
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;
18
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.element.SQLElement;
23
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
-
 
24
import org.openconcerto.sql.model.DBRoot;
-
 
25
import org.openconcerto.sql.model.SQLDataSource;
-
 
26
import org.openconcerto.sql.model.SQLRow;
-
 
27
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
28
import org.openconcerto.sql.model.SQLRowValues;
-
 
29
import org.openconcerto.sql.model.SQLTable;
-
 
30
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
20
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
31
import org.openconcerto.sql.utils.SQLUtils;
-
 
32
import org.openconcerto.ui.DefaultGridBagConstraints;
21
import org.openconcerto.ui.DefaultGridBagConstraints;
33
import org.openconcerto.ui.JDate;
22
import org.openconcerto.ui.JDate;
34
import org.openconcerto.utils.ExceptionHandler;
-
 
35
import org.openconcerto.utils.text.SimpleDocumentListener;
23
import org.openconcerto.utils.text.SimpleDocumentListener;
-
 
24
import org.openconcerto.utils.Tuple2;
36
 
25
 
37
import java.awt.GridBagConstraints;
26
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
27
import java.awt.GridBagLayout;
39
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionEvent;
40
import java.awt.event.ActionListener;
29
import java.awt.event.ActionListener;
41
import java.beans.PropertyChangeEvent;
30
import java.beans.PropertyChangeEvent;
42
import java.beans.PropertyChangeListener;
31
import java.beans.PropertyChangeListener;
43
import java.math.BigDecimal;
32
import java.math.BigDecimal;
44
import java.math.RoundingMode;
-
 
45
import java.sql.SQLException;
-
 
46
import java.text.SimpleDateFormat;
-
 
47
import java.util.ArrayList;
33
import java.util.Arrays;
48
import java.util.Date;
-
 
49
import java.util.List;
-
 
50
 
34
 
51
import javax.swing.JButton;
35
import javax.swing.JButton;
52
import javax.swing.JFrame;
36
import javax.swing.JFrame;
53
import javax.swing.JLabel;
37
import javax.swing.JLabel;
54
import javax.swing.JPanel;
38
import javax.swing.JPanel;
55
import javax.swing.JTextField;
39
import javax.swing.JTextField;
56
import javax.swing.SwingConstants;
40
import javax.swing.SwingConstants;
57
import javax.swing.SwingUtilities;
41
import javax.swing.SwingUtilities;
58
import javax.swing.event.DocumentEvent;
42
import javax.swing.event.DocumentEvent;
59
 
43
 
60
import org.apache.commons.dbutils.ResultSetHandler;
-
 
61
 
-
 
62
public class TransfertStockPanel extends JPanel {
44
public class TransfertStockPanel extends JPanel {
63
 
45
 
64
    private final String mvtStockTableQuoted;
-
 
65
    private static String defaultLabel = "Transfert de stock";
46
    private static String defaultLabel = "Transfert de stock";
66
 
47
 
67
    public TransfertStockPanel(Configuration instance) {
48
    public TransfertStockPanel(Configuration instance) {
68
        super(new GridBagLayout());
49
        super(new GridBagLayout());
69
 
50
 
70
        final JButton buttonUpdate = new JButton("Mettre à jour");
51
        final JButton buttonUpdate = new JButton("Mettre à jour");
71
 
52
 
72
        final SQLTable mvtStockTable = instance.getRoot().findTable("MOUVEMENT_STOCK");
53
        final ReferenceArticleSQLElement articleElt = instance.getDirectory().getElement(ReferenceArticleSQLElement.class);
73
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
-
 
74
 
54
 
75
        final SQLElement articleElt = instance.getDirectory().getElement("ARTICLE");
-
 
76
        final SQLRequestComboBox comboArticle = new SQLRequestComboBox();
55
        final SQLRequestComboBox comboArticle = new SQLRequestComboBox();
77
        comboArticle.uiInit(articleElt.createComboRequest());
56
        comboArticle.uiInit(articleElt.createComboRequest());
78
 
57
 
79
        final SQLElement stockElt = instance.getDirectory().getElement("DEPOT_STOCK");
58
        final SQLElement stockElt = instance.getDirectory().getElement("DEPOT_STOCK");
80
        final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
59
        final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
Line 186... Line 165...
186
 
165
 
187
            @Override
166
            @Override
188
            public void actionPerformed(ActionEvent e) {
167
            public void actionPerformed(ActionEvent e) {
189
                buttonUpdate.setEnabled(false);
168
                buttonUpdate.setEnabled(false);
190
                BigDecimal qteReel = fieldReel.getValue();
169
                BigDecimal qteReel = fieldReel.getValue();
191
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
-
 
192
                boolean usePrice = mvtStockTable.contains("PRICE");
-
 
193
                List<StockItem> stockItems = new ArrayList<StockItem>();
-
 
194
                final Date dateValue = date.getValue();
-
 
195
                final SQLRow selectedRowArticle = comboArticle.getSelectedRow();
-
 
196
                final SQLRow selectedRowDepotDepart = comboStockDepart.getSelectedRow();
-
 
197
                final SQLRow selectedRowDepotArrivee = comboStockArrive.getSelectedRow();
-
 
198
                try {
-
 
199
                    SQLUtils.executeAtomic(selectedRowDepotDepart.getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, SQLException>() {
-
 
200
                        @Override
-
 
201
                        public Object handle(SQLDataSource ds) throws SQLException {
-
 
202
                            {
-
 
203
                                // DEPART
-
 
204
                                final SQLRowAccessor rowStockDepart = ProductComponent.findOrCreateStock(selectedRowArticle, selectedRowDepotDepart);
-
 
205
                                StockItem item = new StockItem(selectedRowArticle, rowStockDepart);
-
 
206
                                if (!item.isStockInit()) {
-
 
207
                                    SQLRowValues rowVals = new SQLRowValues(mvtStockTable.getTable("STOCK"));
-
 
208
                                    rowVals.put("ID_ARTICLE", selectedRowArticle.getID());
-
 
209
                                    rowVals.put("ID_DEPOT_STOCK", selectedRowDepotDepart.getID());
-
 
210
                                    rowVals.commit();
-
 
211
                                    selectedRowArticle.fetchValues();
-
 
212
                                    item = new StockItem(selectedRowArticle, rowStockDepart);
-
 
213
                                }
-
 
214
                                stockItems.add(item);
-
 
215
                                double diff = -qteReel.doubleValue();
-
 
216
                                item.updateQty(diff, TypeStockMouvement.REEL);
-
 
217
                                multipleRequestsHundred
-
 
218
                                        .add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), true, usePrice));
-
 
219
                                item.updateQty(diff, TypeStockMouvement.THEORIQUE);
-
 
220
                                multipleRequestsHundred
-
 
221
                                        .add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), false, usePrice));
-
 
222
                                multipleRequestsHundred.add(item.getUpdateRequest());
-
 
223
                            }
-
 
224
                            // ARRIVEE
-
 
225
                            {
-
 
226
                                final SQLRowAccessor rowStockArrivee = ProductComponent.findOrCreateStock(selectedRowArticle, selectedRowDepotArrivee);
-
 
227
                                StockItem item = new StockItem(selectedRowArticle, rowStockArrivee);
-
 
228
                                if (!item.isStockInit()) {
-
 
229
                                    SQLRowValues rowVals = new SQLRowValues(mvtStockTable.getTable("STOCK"));
-
 
230
                                    rowVals.put("ID_ARTICLE", selectedRowArticle.getID());
-
 
231
                                    rowVals.put("ID_DEPOT_STOCK", selectedRowDepotArrivee.getID());
-
 
232
                                    rowVals.commit();
-
 
233
                                    selectedRowArticle.fetchValues();
-
 
234
                                    item = new StockItem(selectedRowArticle, rowStockArrivee);
-
 
235
                                }
-
 
236
                                stockItems.add(item);
-
 
237
                                double diff = qteReel.doubleValue();
-
 
238
                                item.updateQty(diff, TypeStockMouvement.REEL);
-
 
239
                                multipleRequestsHundred
-
 
240
                                        .add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), true, usePrice));
-
 
241
                                item.updateQty(diff, TypeStockMouvement.THEORIQUE);
-
 
242
                                multipleRequestsHundred
-
 
243
                                        .add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, getLabel(label.getText(), selectedRowDepotDepart, selectedRowDepotArrivee), false, usePrice));
-
 
244
                                multipleRequestsHundred.add(item.getUpdateRequest());
-
 
245
                            }
-
 
246
 
170
 
247
                            final int size = multipleRequestsHundred.size();
-
 
248
                            List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(size);
171
                articleElt.transfert(Arrays.asList(Tuple2.create(comboArticle.getSelectedRow(), qteReel)), comboStockDepart.getSelectedRow(), comboStockArrive.getSelectedRow(), label.getText(),
249
                            for (int i = 0; i < size; i++) {
-
 
250
                                handlers.add(null);
172
                        date.getValue());
251
                            }
-
 
252
                            SQLUtils.executeMultiple(instance.getRoot().getDBSystemRoot(), multipleRequestsHundred, handlers);
-
 
253
 
173
 
254
                            final DBRoot root = mvtStockTable.getDBRoot();
-
 
255
                            if (root.contains("ARTICLE_ELEMENT")) {
-
 
256
                                // Mise à jour des stocks des nomenclatures
-
 
257
                                ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
-
 
258
                                comp.update();
-
 
259
                            }
-
 
260
                            return null;
-
 
261
                        }
-
 
262
                    });
-
 
263
                } catch (SQLException e1) {
-
 
264
                    ExceptionHandler.handle("Stock update error", e1);
-
 
265
                }
-
 
266
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
174
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
267
            }
175
            }
268
        });
-
 
269
    }
-
 
270
 
176
 
271
    private String getLabel(String label, SQLRowAccessor fromDepot, SQLRowAccessor toDepot) {
-
 
272
        return label + " de " + fromDepot.getString("NOM") + " vers " + toDepot.getString("NOM");
-
 
273
    }
-
 
274
 
-
 
275
    private String getMvtRequest(Date time, BigDecimal prc, double qteFinal, StockItem item, String label, boolean reel, boolean usePrice) {
-
 
276
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
-
 
277
        String mvtStockQuery = "INSERT INTO " + this.mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"ID_STOCK\",\"NOM\",\"REEL\",\"ORDRE\"";
-
 
278
 
-
 
279
        if (usePrice && prc != null) {
-
 
280
            mvtStockQuery += ",\"PRICE\"";
-
 
281
        }
177
        });
282
 
-
 
283
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + "," + item.stock.getID() + ",'" + label + "'," + reel
-
 
284
                + ", (SELECT (MAX(\"ORDRE\")+1) FROM " + this.mvtStockTableQuoted + ")";
-
 
285
        if (usePrice && prc != null) {
-
 
286
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
-
 
287
        }
-
 
288
        mvtStockQuery += ")";
-
 
289
        return mvtStockQuery;
-
 
290
    }
178
    }
291
 
179
 
292
    private void updateButtons(final JButton buttonUpdate, final SQLRequestComboBox comboArticle, final SQLRequestComboBox comboStockDepart, final SQLRequestComboBox comboStockArrive,
180
    private void updateButtons(final JButton buttonUpdate, final SQLRequestComboBox comboArticle, final SQLRequestComboBox comboStockDepart, final SQLRequestComboBox comboStockArrive,
293
            final NumericTextField fieldReel) {
181
            final NumericTextField fieldReel) {
294
        buttonUpdate.setEnabled(fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null
182
        buttonUpdate.setEnabled(fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null
295
                && comboStockDepart.getSelectedRow() != null && comboStockArrive.getSelectedRow().getID() != comboStockDepart.getSelectedRow().getID());
183
                && comboStockDepart.getSelectedRow() != null && comboStockArrive.getSelectedRow().getID() != comboStockDepart.getSelectedRow().getID());
296
    }
184
    }
-
 
185
 
297
}
186
}