OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 174 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 11... Line 11...
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.supplychain.stock.element;
14
 package org.openconcerto.erp.core.supplychain.stock.element;
15
 
15
 
-
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.ui.PanelFrame;
18
import org.openconcerto.erp.core.common.ui.PanelFrame;
18
import org.openconcerto.erp.core.sales.product.action.InventairePanel;
19
import org.openconcerto.erp.core.sales.product.action.InventairePanel;
19
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
20
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
20
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
21
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
21
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
23
import org.openconcerto.sql.element.SQLComponent;
-
 
24
import org.openconcerto.sql.element.SQLElement;
-
 
25
import org.openconcerto.sql.model.DBRoot;
-
 
26
import org.openconcerto.sql.model.SQLInjector;
23
import org.openconcerto.sql.model.SQLRow;
27
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowAccessor;
28
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLRowValues;
26
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
30
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
-
 
31
import org.openconcerto.sql.model.SQLSelect;
27
import org.openconcerto.sql.model.SQLTable;
32
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
33
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.sqlobject.ElementComboBox;
34
import org.openconcerto.sql.sqlobject.ElementComboBox;
-
 
35
import org.openconcerto.sql.users.rights.UserRightsManager;
30
import org.openconcerto.sql.view.list.IListe;
36
import org.openconcerto.sql.view.list.IListe;
31
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
37
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
32
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
38
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
33
import org.openconcerto.ui.DefaultGridBagConstraints;
39
import org.openconcerto.ui.DefaultGridBagConstraints;
34
import org.openconcerto.ui.FrameUtil;
40
import org.openconcerto.ui.FrameUtil;
35
import org.openconcerto.utils.ExceptionHandler;
41
import org.openconcerto.utils.DecimalUtils;
36
import org.openconcerto.utils.ListMap;
42
import org.openconcerto.utils.ListMap;
37
 
43
 
38
import java.awt.GridBagConstraints;
44
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
45
import java.awt.GridBagLayout;
40
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionEvent;
-
 
47
import java.math.BigDecimal;
41
import java.sql.SQLException;
48
import java.sql.SQLException;
42
import java.util.Arrays;
49
import java.util.Arrays;
43
import java.util.Collection;
50
import java.util.Collection;
44
import java.util.HashSet;
51
import java.util.HashSet;
45
import java.util.List;
52
import java.util.List;
46
import java.util.Set;
53
import java.util.Set;
47
 
54
 
48
import javax.swing.AbstractAction;
55
import javax.swing.AbstractAction;
49
import javax.swing.JLabel;
56
import javax.swing.JLabel;
-
 
57
import javax.swing.JOptionPane;
50
import javax.swing.JTextField;
58
import javax.swing.JTextField;
51
 
59
 
52
public class StockSQLElement extends ComptaSQLConfElement {
60
public class StockSQLElement extends ComptaSQLConfElement {
53
 
61
 
54
    public StockSQLElement() {
62
    public StockSQLElement() {
55
        super("STOCK", "un stock", "stocks");
63
        super("STOCK", "un stock", "stocks");
56
        getRowActions().addAll(new MouseSheetXmlListeListener(FicheArticleXmlSheet.class).getRowActions());
64
        getRowActions().addAll(new MouseSheetXmlListeListener(this, FicheArticleXmlSheet.class).getRowActions());
57
        PredicateRowAction stock = new PredicateRowAction(new AbstractAction("Mettre à jour les stocks") {
65
        PredicateRowAction stock = new PredicateRowAction(new AbstractAction("Mettre à jour les stocks") {
58
 
66
 
59
            @Override
67
            @Override
60
            public void actionPerformed(ActionEvent e) {
68
            public void actionPerformed(ActionEvent e) {
61
 
-
 
-
 
69
                if (UserRightsManager.getCurrentUserRights().canAdd(getTable().getDBRoot().getTable("STOCK"))) {
62
                PanelFrame p = new PanelFrame(new InventairePanel(IListe.get(e), IListe.get(e).getSelectedRows()), "Mise à jour des stocks");
70
                    final PanelFrame p = new PanelFrame(new InventairePanel(IListe.get(e), IListe.get(e).getSelectedRows()), "Mise à jour des stocks");
63
                FrameUtil.show(p);
71
                    FrameUtil.show(p);
64
 
-
 
-
 
72
                } else {
-
 
73
                    JOptionPane.showMessageDialog(null, "Vous n'avez les droits suffisants pour modifier manuellement les stocks!");
-
 
74
                }
65
            }
75
            }
66
        }, true, false);
76
        }, true, false);
67
        stock.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
77
        stock.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
68
        getRowActions().add(stock);
78
        getRowActions().add(stock);
-
 
79
 
-
 
80
        PredicateRowAction cmd = new PredicateRowAction(new AbstractAction("Passer une commande") {
-
 
81
 
-
 
82
            @Override
-
 
83
            public void actionPerformed(ActionEvent e) {
-
 
84
 
-
 
85
                transfertCommande(IListe.get(e).getSelectedRowAccessors());
-
 
86
 
-
 
87
            }
-
 
88
        }, true, false);
-
 
89
        cmd.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
-
 
90
        getRowActions().add(cmd);
69
    }
91
    }
70
 
92
 
71
    protected List<String> getListFields() {
93
    protected List<String> getListFields() {
72
        return Arrays.asList("ID_ARTICLE", "QTE_MIN", "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
94
        return Arrays.asList("ID_ARTICLE", "QTE_MIN", "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
73
    }
95
    }
Line 126... Line 148...
126
        }
148
        }
127
 
149
 
128
        return rowStock;
150
        return rowStock;
129
    }
151
    }
130
 
152
 
131
    public static SQLRowAccessor getStock(SQLRowAccessor rowValsSource) {
153
    public static SQLRowAccessor getStock(SQLRowAccessor rowValsSource) throws SQLException {
132
 
154
 
133
        SQLRowAccessor rowStock = null;
155
        SQLRowAccessor rowStock = null;
134
        final int idDepot;
156
        final int idDepot;
135
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
157
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
136
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
158
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
Line 142... Line 164...
142
                System.err.println("REFETCH ARTICLE");
164
                System.err.println("REFETCH ARTICLE");
143
                Thread.dumpStack();
165
                Thread.dumpStack();
144
            }
166
            }
145
            idDepot = rowValsArt.getForeignID("ID_DEPOT_STOCK");
167
            idDepot = rowValsArt.getForeignID("ID_DEPOT_STOCK");
146
        }
168
        }
-
 
169
        final int idArticle = rowValsSource.getForeignID("ID_ARTICLE");
-
 
170
        DBRoot root = rowValsSource.getTable().getDBRoot();
-
 
171
 
147
        SQLTable stockTable = rowValsSource.getTable().getTable("STOCK");
172
        SQLTable stockTable = root.getTable("STOCK");
148
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
173
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
149
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
174
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
150
 
175
 
151
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
176
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
152
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot);
177
        Where w = new Where(stockTable.getField("ID_DEPOT_STOCK"), "=", idDepot);
-
 
178
 
153
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsSource.getForeignID("ID_ARTICLE"));
179
        Where w2 = new Where(stockTable.getField("ID_ARTICLE"), "=", idArticle);
154
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
180
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
155
        if (rowValsResult.isEmpty()) {
181
        if (rowValsResult.isEmpty()) {
156
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
182
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
157
            rowValsStock.put("ID_ARTICLE", rowValsSource.getForeignID("ID_ARTICLE"));
183
            rowValsStock.put("ID_ARTICLE", idArticle);
158
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
184
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
159
            rowValsStock.put("QTE_TH", 0F);
185
            rowValsStock.put("QTE_TH", 0F);
160
            rowValsStock.put("QTE_REEL", 0F);
186
            rowValsStock.put("QTE_REEL", 0F);
161
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
187
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
162
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
188
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
163
            try {
-
 
-
 
189
 
164
                rowStock = rowValsStock.insert();
190
            rowStock = rowValsStock.insert();
165
                if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
191
            if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
166
                    rowValsSource.getForeign("ID_ARTICLE").createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
192
                rowValsSource.getForeign("ID_ARTICLE").createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
167
                }
193
            }
168
            } catch (SQLException e) {
-
 
169
                ExceptionHandler.handle("Erreur lors la création du stock!", e);
-
 
170
            }
194
 
171
        } else if (rowValsResult.size() == 1) {
195
        } else if (rowValsResult.size() == 1) {
172
            rowStock = rowValsResult.iterator().next();
196
            rowStock = rowValsResult.iterator().next();
173
        } else if (rowValsResult.size() > 1) {
197
        } else if (rowValsResult.size() > 1) {
174
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsSource.getForeign("ID_ARTICLE").getID() + " Depot " + idDepot);
198
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsSource.getForeign("ID_ARTICLE").getID() + " Depot " + idDepot);
175
        }
199
        }
176
        return rowStock;
200
        return rowStock;
177
    }
201
    }
178
 
202
 
-
 
203
    /**
-
 
204
     * Stock d'un article dans un dépôt donné
-
 
205
     * 
-
 
206
     * @param idDepot dépôt
-
 
207
     * @param idArticle article
-
 
208
     * @param createIfMissing initialisation du stock à 0 si n'existe pas
-
 
209
     * @return le stock (null si createIfMissing à false et que que le stock n'existe pas)
-
 
210
     */
-
 
211
    public SQLRow getStock(int idDepot, int idArticle, boolean createIfMissing) throws SQLException {
-
 
212
        final SQLTable stockTable = getTable();
-
 
213
        final SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
-
 
214
        putRowValuesStock.putNulls(stockTable.getFieldsName());
-
 
215
        final SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
-
 
216
        final Where w = new Where(stockTable.getField("ID_DEPOT_STOCK"), "=", idDepot);
-
 
217
        final Where w2 = new Where(stockTable.getField("ID_ARTICLE"), "=", idArticle);
-
 
218
        final Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
-
 
219
        SQLRow result;
-
 
220
        if (rowValsResult.isEmpty()) {
-
 
221
            if (!createIfMissing) {
-
 
222
                return null;
-
 
223
            }
-
 
224
            final SQLRowValues rowValsStock = new SQLRowValues(stockTable);
-
 
225
            rowValsStock.put("ID_ARTICLE", idArticle);
-
 
226
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
-
 
227
            rowValsStock.put("QTE_TH", 0F);
-
 
228
            rowValsStock.put("QTE_REEL", 0F);
-
 
229
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
-
 
230
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
-
 
231
            result = rowValsStock.insert();
-
 
232
 
-
 
233
        } else if (rowValsResult.size() == 1) {
-
 
234
            result = rowValsResult.iterator().next().asRow();
-
 
235
        } else {
-
 
236
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + idArticle + " Depot " + idDepot);
-
 
237
        }
-
 
238
        return result;
-
 
239
    }
-
 
240
 
179
    /*
241
    /*
180
     * (non-Javadoc)
242
     * (non-Javadoc)
181
     * 
243
     * 
182
     * @see org.openconcerto.devis.SQLElement#getComponent()
244
     * @see org.openconcerto.devis.SQLElement#getComponent()
183
     */
245
     */
Line 269... Line 331...
269
                this.addSQLObject(boxD, "ID_DEPOT_STOCK");
331
                this.addSQLObject(boxD, "ID_DEPOT_STOCK");
270
            }
332
            }
271
        };
333
        };
272
    }
334
    }
273
 
335
 
-
 
336
    /**
-
 
337
     * Transfert d'une commande en commande fournisseur
-
 
338
     * 
-
 
339
     * @param commandeID
-
 
340
     */
-
 
341
    public void transfertCommande(List<SQLRowAccessor> rows) {
-
 
342
 
-
 
343
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
-
 
344
 
-
 
345
            @Override
-
 
346
            public void run() {
-
 
347
 
-
 
348
                SQLTable tableCmdElt = getDirectory().getElement("COMMANDE_ELEMENT").getTable();
-
 
349
                SQLElement eltArticle = getDirectory().getElement("ARTICLE");
-
 
350
 
-
 
351
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
-
 
352
 
-
 
353
                for (SQLRowAccessor sqlRow : rows) {
-
 
354
 
-
 
355
                    SQLRowAccessor rowArticleFind = sqlRow.getForeign("ID_ARTICLE");
-
 
356
                    int qte = 1;
-
 
357
                    BigDecimal qteUV = BigDecimal.ONE;
-
 
358
 
-
 
359
                    qte = -Math.round(sqlRow.getFloat("QTE_TH") - sqlRow.getFloat("QTE_MIN"));
-
 
360
 
-
 
361
                    SQLInjector inj = SQLInjector.getInjector(eltArticle.getTable(), tableCmdElt);
-
 
362
                    SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind.asRow()));
-
 
363
                    rowValsElt.put("ID_DEPOT_STOCK", sqlRow.getForeignID("ID_DEPOT_STOCK"));
-
 
364
                    rowValsElt.put("QTE", qte);
-
 
365
                    rowValsElt.put("QTE_UNITAIRE", qteUV);
-
 
366
 
-
 
367
                    rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
-
 
368
                    // TODO récupérer le prix depuis les tarifs d'achat
-
 
369
                    rowValsElt.put("T_PA_HT", ((BigDecimal) rowValsElt.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), DecimalUtils.HIGH_PRECISION));
-
 
370
                    rowValsElt.put("T_PA_TTC",
-
 
371
                            ((BigDecimal) rowValsElt.getObject("T_PA_HT")).multiply(new BigDecimal((rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)), DecimalUtils.HIGH_PRECISION));
-
 
372
                    final SQLRow fournisseur = rowArticleFind.asRow().getForeignRow("ID_FOURNISSEUR");
-
 
373
                    if (fournisseur != null && !fournisseur.isUndefined() && !rowArticleFind.isUndefined()) {
-
 
374
                        SQLRowValues rowValsCode = new SQLRowValues(tableCmdElt.getForeignTable("ID_CODE_FOURNISSEUR"));
-
 
375
                        rowValsCode.putNulls("ID");
-
 
376
                        final Where w = new Where(rowValsCode.getTable().getField("ID_ARTICLE"), "=", rowArticleFind.getID())
-
 
377
                                .and(new Where(rowValsCode.getTable().getField("ID_FOURNISSEUR"), "=", fournisseur.getID()));
-
 
378
                        final List<SQLRowValues> fetch = SQLRowValuesListFetcher.create(rowValsCode).fetch(w);
-
 
379
                        for (SQLRowValues sqlRowAccessor : fetch) {
-
 
380
                            rowValsElt.put("ID_CODE_FOURNISSEUR", sqlRowAccessor.getID());
-
 
381
                        }
-
 
382
                    }
-
 
383
                    map.add(fournisseur, rowValsElt);
-
 
384
                }
-
 
385
 
-
 
386
                MouvementStockSQLElement.createCommandeF(map, null, "");
-
 
387
            }
-
 
388
        });
-
 
389
 
-
 
390
    }
-
 
391
 
274
    @Override
392
    @Override
275
    protected String createCode() {
393
    protected String createCode() {
276
        return "supplychain.stock";
394
        return "supplychain.stock";
277
    }
395
    }
278
}
396
}