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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
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.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
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();
81
        comboStockDepart.uiInit(stockElt.createComboRequest());
60
        comboStockDepart.uiInit(stockElt.createComboRequest());
82
 
61
 
83
        final SQLRequestComboBox comboStockArrive = new SQLRequestComboBox();
62
        final SQLRequestComboBox comboStockArrive = new SQLRequestComboBox();
84
        comboStockArrive.uiInit(stockElt.createComboRequest());
63
        comboStockArrive.uiInit(stockElt.createComboRequest());
85
 
64
 
86
        final NumericTextField fieldReel = new NumericTextField();
65
        final NumericTextField fieldReel = new NumericTextField();
87
 
66
 
88
        fieldReel.getDocument().addDocumentListener(new SimpleDocumentListener() {
67
        fieldReel.getDocument().addDocumentListener(new SimpleDocumentListener() {
89
 
68
 
90
            @Override
69
            @Override
91
            public void update(DocumentEvent e) {
70
            public void update(DocumentEvent e) {
92
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
71
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
93
            }
72
            }
94
        });
73
        });
95
 
74
 
96
        comboArticle.addModelListener("wantedID", new PropertyChangeListener() {
75
        comboArticle.addModelListener("wantedID", new PropertyChangeListener() {
97
 
76
 
98
            @Override
77
            @Override
99
            public void propertyChange(PropertyChangeEvent evt) {
78
            public void propertyChange(PropertyChangeEvent evt) {
100
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
79
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
101
            }
80
            }
102
        });
81
        });
103
 
82
 
104
        comboStockArrive.addModelListener("wantedID", new PropertyChangeListener() {
83
        comboStockArrive.addModelListener("wantedID", new PropertyChangeListener() {
105
 
84
 
106
            @Override
85
            @Override
107
            public void propertyChange(PropertyChangeEvent evt) {
86
            public void propertyChange(PropertyChangeEvent evt) {
108
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
87
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
109
            }
88
            }
110
        });
89
        });
111
 
90
 
112
        comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {
91
        comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {
113
 
92
 
114
            @Override
93
            @Override
115
            public void propertyChange(PropertyChangeEvent evt) {
94
            public void propertyChange(PropertyChangeEvent evt) {
116
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
95
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
117
            }
96
            }
118
        });
97
        });
119
 
98
 
120
        GridBagConstraints c = new DefaultGridBagConstraints();
99
        GridBagConstraints c = new DefaultGridBagConstraints();
121
        c.gridx = 0;
100
        c.gridx = 0;
122
        this.add(new JLabel("Intitulé"), c);
101
        this.add(new JLabel("Intitulé"), c);
123
        final JTextField label = new JTextField();
102
        final JTextField label = new JTextField();
124
        c.gridx++;
103
        c.gridx++;
125
        c.gridwidth = 1;
104
        c.gridwidth = 1;
126
        c.weightx = 1;
105
        c.weightx = 1;
127
        this.add(label, c);
106
        this.add(label, c);
128
        label.setText(defaultLabel);
107
        label.setText(defaultLabel);
129
 
108
 
130
        c.gridy++;
109
        c.gridy++;
131
        c.gridx = 0;
110
        c.gridx = 0;
132
        c.weightx = 0;
111
        c.weightx = 0;
133
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
112
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
134
        final JDate date = new JDate(true);
113
        final JDate date = new JDate(true);
135
        c.gridx++;
114
        c.gridx++;
136
        this.add(date, c);
115
        this.add(date, c);
137
 
116
 
138
        c.gridy++;
117
        c.gridy++;
139
        c.gridx = 0;
118
        c.gridx = 0;
140
        c.weightx = 0;
119
        c.weightx = 0;
141
        this.add(new JLabel("Article", SwingConstants.RIGHT), c);
120
        this.add(new JLabel("Article", SwingConstants.RIGHT), c);
142
        c.gridx++;
121
        c.gridx++;
143
        this.add(comboArticle, c);
122
        this.add(comboArticle, c);
144
 
123
 
145
        c.gridy++;
124
        c.gridy++;
146
        c.gridx = 0;
125
        c.gridx = 0;
147
        c.weightx = 0;
126
        c.weightx = 0;
148
        this.add(new JLabel("Départ", SwingConstants.RIGHT), c);
127
        this.add(new JLabel("Départ", SwingConstants.RIGHT), c);
149
        c.gridx++;
128
        c.gridx++;
150
        this.add(comboStockDepart, c);
129
        this.add(comboStockDepart, c);
151
 
130
 
152
        c.gridy++;
131
        c.gridy++;
153
        c.gridx = 0;
132
        c.gridx = 0;
154
        c.weightx = 0;
133
        c.weightx = 0;
155
 
134
 
156
        this.add(new JLabel("Arrivée", SwingConstants.RIGHT), c);
135
        this.add(new JLabel("Arrivée", SwingConstants.RIGHT), c);
157
        c.gridx++;
136
        c.gridx++;
158
        this.add(comboStockArrive, c);
137
        this.add(comboStockArrive, c);
159
 
138
 
160
        c.gridy++;
139
        c.gridy++;
161
        c.gridx = 0;
140
        c.gridx = 0;
162
        c.weightx = 0;
141
        c.weightx = 0;
163
        this.add(new JLabel("Quantité", SwingConstants.RIGHT), c);
142
        this.add(new JLabel("Quantité", SwingConstants.RIGHT), c);
164
        c.gridx++;
143
        c.gridx++;
165
        this.add(fieldReel, c);
144
        this.add(fieldReel, c);
166
 
145
 
167
        c.gridy++;
146
        c.gridy++;
168
        c.gridx = 0;
147
        c.gridx = 0;
169
        JButton buttonCancel = new JButton("Annuler");
148
        JButton buttonCancel = new JButton("Annuler");
170
        JPanel pButton = new JPanel();
149
        JPanel pButton = new JPanel();
171
        pButton.add(buttonCancel);
150
        pButton.add(buttonCancel);
172
        pButton.add(buttonUpdate);
151
        pButton.add(buttonUpdate);
173
        c.gridwidth = 2;
152
        c.gridwidth = 2;
174
        c.anchor = GridBagConstraints.SOUTHEAST;
153
        c.anchor = GridBagConstraints.SOUTHEAST;
175
        c.weightx = 0;
154
        c.weightx = 0;
176
        c.fill = GridBagConstraints.NONE;
155
        c.fill = GridBagConstraints.NONE;
177
        this.add(pButton, c);
156
        this.add(pButton, c);
178
        buttonCancel.addActionListener(new ActionListener() {
157
        buttonCancel.addActionListener(new ActionListener() {
179
 
158
 
180
            @Override
159
            @Override
181
            public void actionPerformed(ActionEvent e) {
160
            public void actionPerformed(ActionEvent e) {
182
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
161
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
183
            }
162
            }
184
        });
163
        });
185
        buttonUpdate.addActionListener(new ActionListener() {
164
        buttonUpdate.addActionListener(new ActionListener() {
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
 
-
 
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
 
176
 
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
}