OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Rev 182 | Go to most recent revision | 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;
180 ilm 23
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
174 ilm 24
import org.openconcerto.sql.model.DBRoot;
180 ilm 25
import org.openconcerto.sql.model.SQLDataSource;
174 ilm 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;
31
import org.openconcerto.sql.utils.SQLUtils;
32
import org.openconcerto.ui.DefaultGridBagConstraints;
33
import org.openconcerto.ui.JDate;
34
import org.openconcerto.utils.ExceptionHandler;
35
import org.openconcerto.utils.text.SimpleDocumentListener;
36
 
37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
39
import java.awt.event.ActionEvent;
40
import java.awt.event.ActionListener;
41
import java.beans.PropertyChangeEvent;
42
import java.beans.PropertyChangeListener;
43
import java.math.BigDecimal;
44
import java.math.RoundingMode;
45
import java.sql.SQLException;
46
import java.text.SimpleDateFormat;
47
import java.util.ArrayList;
48
import java.util.Date;
49
import java.util.List;
50
 
51
import javax.swing.JButton;
52
import javax.swing.JFrame;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55
import javax.swing.JTextField;
56
import javax.swing.SwingConstants;
57
import javax.swing.SwingUtilities;
58
import javax.swing.event.DocumentEvent;
59
 
60
import org.apache.commons.dbutils.ResultSetHandler;
61
 
62
public class TransfertStockPanel extends JPanel {
63
 
64
    private final String mvtStockTableQuoted;
65
    private static String defaultLabel = "Transfert de stock";
66
 
67
    public TransfertStockPanel(Configuration instance) {
68
        super(new GridBagLayout());
69
 
70
        final JButton buttonUpdate = new JButton("Mettre à jour");
71
 
72
        final SQLTable mvtStockTable = instance.getRoot().findTable("MOUVEMENT_STOCK");
73
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
74
 
75
        final SQLElement articleElt = instance.getDirectory().getElement("ARTICLE");
76
        final SQLRequestComboBox comboArticle = new SQLRequestComboBox();
77
        comboArticle.uiInit(articleElt.createComboRequest());
78
 
79
        final SQLElement stockElt = instance.getDirectory().getElement("DEPOT_STOCK");
80
        final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
81
        comboStockDepart.uiInit(stockElt.createComboRequest());
82
 
83
        final SQLRequestComboBox comboStockArrive = new SQLRequestComboBox();
84
        comboStockArrive.uiInit(stockElt.createComboRequest());
85
 
86
        final NumericTextField fieldReel = new NumericTextField();
87
 
88
        fieldReel.getDocument().addDocumentListener(new SimpleDocumentListener() {
89
 
90
            @Override
91
            public void update(DocumentEvent e) {
180 ilm 92
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
174 ilm 93
            }
94
        });
95
 
96
        comboArticle.addModelListener("wantedID", new PropertyChangeListener() {
97
 
98
            @Override
99
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 100
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
174 ilm 101
            }
102
        });
103
 
104
        comboStockArrive.addModelListener("wantedID", new PropertyChangeListener() {
105
 
106
            @Override
107
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 108
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
174 ilm 109
            }
110
        });
111
 
112
        comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {
113
 
114
            @Override
115
            public void propertyChange(PropertyChangeEvent evt) {
180 ilm 116
                updateButtons(buttonUpdate, comboArticle, comboStockDepart, comboStockArrive, fieldReel);
174 ilm 117
            }
118
        });
119
 
120
        GridBagConstraints c = new DefaultGridBagConstraints();
180 ilm 121
        c.gridx = 0;
174 ilm 122
        this.add(new JLabel("Intitulé"), c);
123
        final JTextField label = new JTextField();
124
        c.gridx++;
180 ilm 125
        c.gridwidth = 1;
174 ilm 126
        c.weightx = 1;
127
        this.add(label, c);
128
        label.setText(defaultLabel);
129
 
130
        c.gridy++;
131
        c.gridx = 0;
132
        c.weightx = 0;
133
        this.add(new JLabel("Date", SwingConstants.RIGHT), c);
134
        final JDate date = new JDate(true);
135
        c.gridx++;
136
        this.add(date, c);
137
 
138
        c.gridy++;
139
        c.gridx = 0;
140
        c.weightx = 0;
141
        this.add(new JLabel("Article", SwingConstants.RIGHT), c);
180 ilm 142
        c.gridx++;
174 ilm 143
        this.add(comboArticle, c);
144
 
145
        c.gridy++;
146
        c.gridx = 0;
147
        c.weightx = 0;
148
        this.add(new JLabel("Départ", SwingConstants.RIGHT), c);
180 ilm 149
        c.gridx++;
174 ilm 150
        this.add(comboStockDepart, c);
151
 
152
        c.gridy++;
153
        c.gridx = 0;
154
        c.weightx = 0;
180 ilm 155
 
174 ilm 156
        this.add(new JLabel("Arrivée", SwingConstants.RIGHT), c);
180 ilm 157
        c.gridx++;
174 ilm 158
        this.add(comboStockArrive, c);
159
 
160
        c.gridy++;
161
        c.gridx = 0;
162
        c.weightx = 0;
180 ilm 163
        this.add(new JLabel("Quantité", SwingConstants.RIGHT), c);
164
        c.gridx++;
174 ilm 165
        this.add(fieldReel, c);
166
 
167
        c.gridy++;
168
        c.gridx = 0;
169
        JButton buttonCancel = new JButton("Annuler");
170
        JPanel pButton = new JPanel();
171
        pButton.add(buttonCancel);
172
        pButton.add(buttonUpdate);
180 ilm 173
        c.gridwidth = 2;
174
        c.anchor = GridBagConstraints.SOUTHEAST;
174 ilm 175
        c.weightx = 0;
176
        c.fill = GridBagConstraints.NONE;
177
        this.add(pButton, c);
178
        buttonCancel.addActionListener(new ActionListener() {
179
 
180
            @Override
181
            public void actionPerformed(ActionEvent e) {
182
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
183
            }
184
        });
185
        buttonUpdate.addActionListener(new ActionListener() {
186
 
187
            @Override
188
            public void actionPerformed(ActionEvent e) {
189
                buttonUpdate.setEnabled(false);
190
                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();
177 ilm 196
                final SQLRow selectedRowDepotDepart = comboStockDepart.getSelectedRow();
197
                final SQLRow selectedRowDepotArrivee = comboStockArrive.getSelectedRow();
180 ilm 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
                            }
174 ilm 246
 
180 ilm 247
                            final int size = multipleRequestsHundred.size();
248
                            List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(size);
249
                            for (int i = 0; i < size; i++) {
250
                                handlers.add(null);
251
                            }
252
                            SQLUtils.executeMultiple(instance.getRoot().getDBSystemRoot(), multipleRequestsHundred, handlers);
174 ilm 253
 
180 ilm 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;
174 ilm 261
                        }
180 ilm 262
                    });
174 ilm 263
                } catch (SQLException e1) {
264
                    ExceptionHandler.handle("Stock update error", e1);
265
                }
266
                ((JFrame) SwingUtilities.getRoot(TransfertStockPanel.this)).dispose();
267
            }
268
        });
269
    }
270
 
177 ilm 271
    private String getLabel(String label, SQLRowAccessor fromDepot, SQLRowAccessor toDepot) {
272
        return label + " de " + fromDepot.getString("NOM") + " vers " + toDepot.getString("NOM");
273
    }
274
 
174 ilm 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");
180 ilm 277
        String mvtStockQuery = "INSERT INTO " + this.mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"ID_STOCK\",\"NOM\",\"REEL\",\"ORDRE\"";
174 ilm 278
 
279
        if (usePrice && prc != null) {
280
            mvtStockQuery += ",\"PRICE\"";
281
        }
282
 
283
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + "," + item.stock.getID() + ",'" + label + "'," + reel
180 ilm 284
                + ", (SELECT (MAX(\"ORDRE\")+1) FROM " + this.mvtStockTableQuoted + ")";
174 ilm 285
        if (usePrice && prc != null) {
286
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
287
        }
288
        mvtStockQuery += ")";
289
        return mvtStockQuery;
290
    }
180 ilm 291
 
292
    private void updateButtons(final JButton buttonUpdate, final SQLRequestComboBox comboArticle, final SQLRequestComboBox comboStockDepart, final SQLRequestComboBox comboStockArrive,
293
            final NumericTextField fieldReel) {
294
        buttonUpdate.setEnabled(fieldReel.getText().trim().length() > 0 && comboArticle.getSelectedRow() != null && comboStockArrive.getSelectedRow() != null
295
                && comboStockDepart.getSelectedRow() != null && comboStockArrive.getSelectedRow().getID() != comboStockDepart.getSelectedRow().getID());
296
    }
174 ilm 297
}