OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 156
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.element.ReferenceArticleSQLElement;
17
import org.openconcerto.erp.core.supplychain.stock.element.ComposedItemStockUpdater;
18
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;
19
import org.openconcerto.erp.core.supplychain.stock.element.StockItem.TypeStockMouvement;
20
import org.openconcerto.erp.core.supplychain.stock.element.StockItem.TypeStockMouvement;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.DBRoot;
22
import org.openconcerto.sql.model.DBRoot;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowValues;
-
 
25
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.utils.SQLUtils;
26
import org.openconcerto.sql.utils.SQLUtils;
27
import org.openconcerto.sql.view.list.IListe;
27
import org.openconcerto.sql.view.list.IListe;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.JDate;
29
import org.openconcerto.ui.JDate;
Line 58... Line 58...
58
public class InventairePanel extends JPanel {
58
public class InventairePanel extends JPanel {
59
 
59
 
60
    private final String mvtStockTableQuoted;
60
    private final String mvtStockTableQuoted;
61
    private static String defaultLabel = "Mise à jour des stocks";
61
    private static String defaultLabel = "Mise à jour des stocks";
62
 
62
 
63
    public InventairePanel(final IListe liste, final List<? extends SQLRowAccessor> articles) {
63
    public InventairePanel(final IListe liste, final List<? extends SQLRowAccessor> stocks) {
64
        super(new GridBagLayout());
64
        super(new GridBagLayout());
65
        final SQLTable mvtStockTable = Configuration.getInstance().getRoot().findTable("MOUVEMENT_STOCK");
65
        final SQLTable mvtStockTable = Configuration.getInstance().getRoot().findTable("MOUVEMENT_STOCK");
66
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
66
        this.mvtStockTableQuoted = mvtStockTable.getSQLName().quote();
67
 
67
 
68
        JLabel qteReel = new JLabel("Quantité réel", SwingConstants.RIGHT);
68
        JLabel qteReel = new JLabel("Quantité réel", SwingConstants.RIGHT);
Line 156... Line 156...
156
        };
156
        };
157
        fieldLivAtt.getDocument().addDocumentListener(l);
157
        fieldLivAtt.getDocument().addDocumentListener(l);
158
        fieldReel.getDocument().addDocumentListener(l);
158
        fieldReel.getDocument().addDocumentListener(l);
159
        fieldRecpAtt.getDocument().addDocumentListener(l);
159
        fieldRecpAtt.getDocument().addDocumentListener(l);
160
 
160
 
161
        if (articles.size() == 1) {
161
        if (stocks.size() == 1) {
162
            SQLRowAccessor r = articles.get(0);
162
            SQLRowAccessor r = stocks.get(0);
163
            if (!r.isForeignEmpty("ID_STOCK")) {
-
 
164
                SQLRowAccessor stock = r.getForeign("ID_STOCK");
163
            SQLRowAccessor stock = r;
165
                fieldReel.setText(String.valueOf(stock.getFloat("QTE_REEL")));
164
            fieldReel.setText(String.valueOf(stock.getFloat("QTE_REEL")));
166
                fieldLivAtt.setText(String.valueOf(stock.getFloat("QTE_LIV_ATTENTE")));
165
            fieldLivAtt.setText(String.valueOf(stock.getFloat("QTE_LIV_ATTENTE")));
167
                fieldRecpAtt.setText(String.valueOf(stock.getFloat("QTE_RECEPT_ATTENTE")));
166
            fieldRecpAtt.setText(String.valueOf(stock.getFloat("QTE_RECEPT_ATTENTE")));
168
                fieldTh.setText(String.valueOf(stock.getFloat("QTE_TH")));
167
            fieldTh.setText(String.valueOf(stock.getFloat("QTE_TH")));
169
            }
168
        }
170
        }
-
 
171
 
169
 
172
        c.gridy++;
170
        c.gridy++;
173
        c.gridx = 0;
171
        c.gridx = 0;
174
        final JButton buttonUpdate = new JButton("Mettre à jour");
172
        final JButton buttonUpdate = new JButton("Mettre à jour");
175
        JButton buttonCancel = new JButton("Annuler");
173
        JButton buttonCancel = new JButton("Annuler");
Line 200... Line 198...
200
                BigDecimal qteRecptAttente = fieldRecpAtt.getValue();
198
                BigDecimal qteRecptAttente = fieldRecpAtt.getValue();
201
 
199
 
202
                List<List<String>> multipleRequests = new ArrayList<List<String>>();
200
                List<List<String>> multipleRequests = new ArrayList<List<String>>();
203
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
201
                List<String> multipleRequestsHundred = new ArrayList<String>(100);
204
                boolean usePrice = mvtStockTable.contains("PRICE");
202
                boolean usePrice = mvtStockTable.contains("PRICE");
205
 
-
 
-
 
203
                List<StockItem> stockItems = new ArrayList<StockItem>();
206
                final Date dateValue = date.getValue();
204
                final Date dateValue = date.getValue();
207
                for (SQLRowAccessor sqlRowAccessor : articles) {
205
                for (SQLRowAccessor sqlRowAccessor : stocks) {
208
                    if (multipleRequestsHundred.size() > 100) {
206
                    if (multipleRequestsHundred.size() > 100) {
209
                        multipleRequests.add(multipleRequestsHundred);
207
                        multipleRequests.add(multipleRequestsHundred);
210
                        multipleRequestsHundred = new ArrayList<String>(100);
208
                        multipleRequestsHundred = new ArrayList<String>(100);
211
                    }
209
                    }
212
                    StockItem item = new StockItem(sqlRowAccessor);
210
                    StockItem item = new StockItem(sqlRowAccessor.getForeign("ID_ARTICLE"), sqlRowAccessor);
-
 
211
                    if (!item.isStockInit()) {
-
 
212
                        ((ReferenceArticleSQLElement) liste.getSource().getElem().getDirectory().getElement("ARTICLE")).initStock(sqlRowAccessor.getForeignID("ID_ARTICLE"));
-
 
213
                        SQLRow rowArticle = sqlRowAccessor.getForeign("ID_ARTICLE").asRow();
-
 
214
                        rowArticle.fetchValues();
-
 
215
                        item = new StockItem(rowArticle, rowArticle.getForeign("ID_STOCK"));
-
 
216
                    }
-
 
217
                    stockItems.add(item);
213
                    boolean modified = false;
218
                    boolean modified = false;
214
                    if (qteReel != null && !NumberUtils.areNumericallyEqual(qteReel, item.getRealQty())) {
219
                    if (qteReel != null && !NumberUtils.areNumericallyEqual(qteReel, item.getRealQty())) {
215
                        double diff = qteReel.doubleValue() - item.getRealQty();
220
                        double diff = qteReel.doubleValue() - item.getRealQty();
216
                        item.updateQty(diff, TypeStockMouvement.REEL);
221
                        item.updateQty(diff, TypeStockMouvement.REEL);
217
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), true, usePrice));
222
                        multipleRequestsHundred.add(getMvtRequest(dateValue, BigDecimal.ZERO, diff, item, label.getText(), true, usePrice));
Line 233... Line 238...
233
                    }
238
                    }
234
                    if (modified) {
239
                    if (modified) {
235
                        if (item.isStockInit()) {
240
                        if (item.isStockInit()) {
236
                            multipleRequestsHundred.add(item.getUpdateRequest());
241
                            multipleRequestsHundred.add(item.getUpdateRequest());
237
                        } else {
242
                        } 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();
243
                            throw new IllegalStateException();
247
                            } catch (SQLException e1) {
-
 
248
                                e1.printStackTrace();
-
 
249
                            }
-
 
250
                        }
244
                        }
251
                    }
245
                    }
252
 
246
 
253
                }
247
                }
254
                multipleRequests.add(multipleRequestsHundred);
248
                multipleRequests.add(multipleRequestsHundred);
Line 267... Line 261...
267
                    ExceptionHandler.handle("Stock update error", e1);
261
                    ExceptionHandler.handle("Stock update error", e1);
268
                }
262
                }
269
 
263
 
270
                final DBRoot root = mvtStockTable.getDBRoot();
264
                final DBRoot root = mvtStockTable.getDBRoot();
271
                if (root.contains("ARTICLE_ELEMENT")) {
265
                if (root.contains("ARTICLE_ELEMENT")) {
272
                    List<StockItem> stockItems = new ArrayList<StockItem>();
266
                    // List<StockItem> stockItems = new ArrayList<StockItem>();
273
                    for (SQLRowAccessor sqlRowAccessor2 : articles) {
267
                    // for (SQLRowAccessor sqlRowAccessor2 : stocks) {
274
                        final SQLRow asRow = sqlRowAccessor2.asRow();
268
                    // final SQLRow asRow = sqlRowAccessor2.asRow();
275
                        asRow.fetchValues();
269
                    // asRow.fetchValues();
276
                        stockItems.add(new StockItem(asRow));
270
                    // stockItems.add(new StockItem(asRow));
277
                    }
271
                    // }
278
                    // Mise à jour des stocks des nomenclatures
272
                    // Mise à jour des stocks des nomenclatures
279
                    ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
273
                    ComposedItemStockUpdater comp = new ComposedItemStockUpdater(root, stockItems);
280
                    try {
274
                    try {
281
                        comp.update();
275
                        comp.update();
282
                    } catch (SQLException e1) {
276
                    } catch (SQLException e1) {
Line 290... Line 284...
290
        });
284
        });
291
    }
285
    }
292
 
286
 
293
    private String getMvtRequest(Date time, BigDecimal prc, double qteFinal, StockItem item, String label, boolean reel, boolean usePrice) {
287
    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");
288
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
295
        String mvtStockQuery = "INSERT INTO " + mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"NOM\",\"REEL\",\"ORDRE\"";
289
        String mvtStockQuery = "INSERT INTO " + mvtStockTableQuoted + " (\"QTE\",\"DATE\",\"ID_ARTICLE\",\"ID_STOCK\",\"NOM\",\"REEL\",\"ORDRE\"";
296
 
290
 
297
        if (usePrice && prc != null) {
291
        if (usePrice && prc != null) {
298
            mvtStockQuery += ",\"PRICE\"";
292
            mvtStockQuery += ",\"PRICE\"";
299
        }
293
        }
300
 
294
 
301
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + ",'" + label + "'," + reel + ", (SELECT (MAX(\"ORDRE\")+1) FROM "
295
        mvtStockQuery += ") VALUES(" + qteFinal + ",'" + dateFormat.format(time) + "'," + item.getArticle().getID() + "," + item.stock.getID() + ",'" + label + "'," + reel
302
                + mvtStockTableQuoted + ")";
296
                + ", (SELECT (MAX(\"ORDRE\")+1) FROM " + mvtStockTableQuoted + ")";
303
        if (usePrice && prc != null) {
297
        if (usePrice && prc != null) {
304
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
298
            mvtStockQuery += "," + prc.setScale(6, RoundingMode.HALF_UP).toString();
305
        }
299
        }
306
        mvtStockQuery += ")";
300
        mvtStockQuery += ")";
307
        return mvtStockQuery;
301
        return mvtStockQuery;