OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 151 Rev 156
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.sales.order.element;
14
 package org.openconcerto.erp.core.sales.order.element;
15
 
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.erp.core.common.ui.DeviseField;
-
 
19
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
18
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
20
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
19
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
-
 
20
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
-
 
21
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
-
 
22
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
-
 
23
import org.openconcerto.erp.core.supplychain.stock.element.StockSQLElement;
21
import org.openconcerto.sql.element.SQLComponent;
24
import org.openconcerto.sql.element.SQLComponent;
22
import org.openconcerto.sql.element.SQLElement;
25
import org.openconcerto.sql.element.SQLElement;
23
import org.openconcerto.sql.element.UISQLComponent;
26
import org.openconcerto.sql.element.UISQLComponent;
24
import org.openconcerto.sql.model.SQLInjector;
27
import org.openconcerto.sql.model.SQLInjector;
25
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowAccessor;
29
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
30
import org.openconcerto.sql.model.SQLRowListRSH;
27
import org.openconcerto.sql.model.SQLRowValues;
31
import org.openconcerto.sql.model.SQLRowValues;
-
 
32
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
33
import org.openconcerto.sql.model.SQLTable;
-
 
34
import org.openconcerto.sql.model.Where;
-
 
35
import org.openconcerto.sql.request.ListSQLRequest;
-
 
36
import org.openconcerto.sql.request.UpdateBuilder;
29
import org.openconcerto.sql.sqlobject.ElementComboBox;
37
import org.openconcerto.sql.sqlobject.ElementComboBox;
-
 
38
import org.openconcerto.sql.users.UserManager;
-
 
39
import org.openconcerto.sql.view.EditFrame;
-
 
40
import org.openconcerto.sql.view.EditPanel.EditMode;
30
import org.openconcerto.sql.view.list.IListe;
41
import org.openconcerto.sql.view.list.IListe;
31
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
42
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
32
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
43
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
-
 
44
import org.openconcerto.ui.FrameUtil;
33
import org.openconcerto.utils.DecimalUtils;
45
import org.openconcerto.utils.DecimalUtils;
34
import org.openconcerto.utils.ListMap;
46
import org.openconcerto.utils.ListMap;
35
 
47
 
36
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionEvent;
37
import java.math.BigDecimal;
49
import java.math.BigDecimal;
-
 
50
import java.sql.SQLException;
38
import java.util.ArrayList;
51
import java.util.ArrayList;
39
import java.util.Arrays;
52
import java.util.Arrays;
40
import java.util.HashSet;
53
import java.util.HashSet;
41
import java.util.List;
54
import java.util.List;
42
import java.util.Set;
55
import java.util.Set;
Line 52... Line 65...
52
        PredicateRowAction rowAction = new PredicateRowAction(new AbstractAction("Transfert vers commande fournisseur") {
65
        PredicateRowAction rowAction = new PredicateRowAction(new AbstractAction("Transfert vers commande fournisseur") {
53
 
66
 
54
            @Override
67
            @Override
55
            public void actionPerformed(ActionEvent e) {
68
            public void actionPerformed(ActionEvent e) {
56
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
69
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
57
                final List<SQLRowAccessor> arts = new ArrayList<SQLRowAccessor>();
-
 
58
                final Set<Integer> s = new HashSet<Integer>();
-
 
59
                for (SQLRowValues sqlRowValues : selectedRows) {
-
 
60
                    if (sqlRowValues.getObject("ID_ARTICLE") != null && !sqlRowValues.isForeignEmpty("ID_ARTICLE")) {
-
 
61
                        SQLRowAccessor rowArt = sqlRowValues.getForeign("ID_ARTICLE");
-
 
62
                        if (!s.contains(rowArt.getID())) {
-
 
63
                            s.add(rowArt.getID());
-
 
64
                            arts.add(rowArt);
-
 
65
                        }
-
 
66
                    }
-
 
67
                }
-
 
68
                ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
70
                ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
69
 
71
 
70
                    @Override
72
                    @Override
71
                    public void run() {
73
                    public void run() {
72
 
74
 
73
                        createCommandeF(arts);
75
                        createCommandeF(selectedRows);
74
                    }
76
                    }
75
                });
77
                });
76
            }
78
            }
77
 
79
 
78
        }, true);
80
        }, true);
79
        rowAction.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
81
        rowAction.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
80
        getRowActions().add(rowAction);
82
        getRowActions().add(rowAction);
-
 
83
 
-
 
84
        PredicateRowAction rowActionCmd = new PredicateRowAction(new AbstractAction("Modifier la commande associée") {
-
 
85
 
-
 
86
            @Override
-
 
87
            public void actionPerformed(ActionEvent e) {
-
 
88
                SQLRowValues selectedRow = IListe.get(e).getSelectedRow();
-
 
89
                EditFrame f = new EditFrame(getForeignElement("ID_COMMANDE_CLIENT"), EditMode.MODIFICATION);
-
 
90
                f.getSQLComponent().select(selectedRow.getForeignID("ID_COMMANDE_CLIENT"));
-
 
91
                FrameUtil.showPacked(f);
-
 
92
            }
-
 
93
 
-
 
94
        }, true);
-
 
95
        rowActionCmd.setPredicate(IListeEvent.getSingleSelectionPredicate());
-
 
96
        getRowActions().add(rowActionCmd);
-
 
97
 
-
 
98
        if (getTable().getForeignTable("ID_USER_COMMON_CREATE").getRow(UserManager.getUserID()).getBoolean("ADMIN")) {
-
 
99
            PredicateRowAction rowActionForceLivr = new PredicateRowAction(new AbstractAction("Forcer la livraison") {
-
 
100
 
-
 
101
                @Override
-
 
102
                public void actionPerformed(ActionEvent e) {
-
 
103
                    updateForceLivrer(e, Boolean.TRUE);
-
 
104
 
-
 
105
                }
-
 
106
 
-
 
107
            }, true);
-
 
108
            rowActionForceLivr.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
-
 
109
            getRowActions().add(rowActionForceLivr);
-
 
110
 
-
 
111
            PredicateRowAction rowActionAnnuler = new PredicateRowAction(new AbstractAction("Annuler Forcer la livraison") {
-
 
112
 
-
 
113
                @Override
-
 
114
                public void actionPerformed(ActionEvent e) {
-
 
115
                    updateForceLivrer(e, Boolean.FALSE);
-
 
116
 
-
 
117
                }
-
 
118
 
-
 
119
            }, true);
-
 
120
            rowActionAnnuler.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
-
 
121
            getRowActions().add(rowActionAnnuler);
-
 
122
 
-
 
123
            PredicateRowAction rowActionUpdateStockTh = new PredicateRowAction(new AbstractAction("Recalculer le stock théorique") {
-
 
124
 
-
 
125
                @Override
-
 
126
                public void actionPerformed(ActionEvent e) {
-
 
127
                    recalculStockTh();
-
 
128
 
-
 
129
                }
-
 
130
 
-
 
131
            }, true);
-
 
132
            rowActionUpdateStockTh.setPredicate(IListeEvent.createTotalRowCountPredicate(0, Integer.MAX_VALUE));
-
 
133
            getRowActions().add(rowActionUpdateStockTh);
-
 
134
 
-
 
135
        }
-
 
136
 
-
 
137
    }
-
 
138
 
-
 
139
    private void updateForceLivrer(ActionEvent e, Boolean state) {
-
 
140
        final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
-
 
141
        final Set<Integer> ids = new HashSet<Integer>();
-
 
142
        for (SQLRowValues sqlRowValues : selectedRows) {
-
 
143
            ids.add(sqlRowValues.getID());
-
 
144
        }
-
 
145
        UpdateBuilder build = new UpdateBuilder(getTable());
-
 
146
        build.setObject("LIVRE_FORCED", state);
-
 
147
        build.setWhere(new Where(getTable().getKey(), ids));
-
 
148
        getTable().getDBSystemRoot().getDataSource().execute(build.asString());
-
 
149
        IListe.get(e).getModel().updateAll();
-
 
150
    }
-
 
151
 
-
 
152
    public void recalculStockTh() {
-
 
153
 
-
 
154
        // RAZ des stocks TH --> TH = REEL
-
 
155
        final SQLTable tableStock = getTable().getDBRoot().findTable("STOCK");
-
 
156
        String req = "UPDATE " + tableStock.getSQLName().quote() + " SET " + tableStock.getField("QTE_TH").getQuotedName() + " = " + tableStock.getField("QTE_REEL").getQuotedName() + ","
-
 
157
                + tableStock.getField("QTE_RECEPT_ATTENTE").getQuotedName() + " = 0," + tableStock.getField("QTE_LIV_ATTENTE").getQuotedName() + "=0";
-
 
158
        tableStock.getDBSystemRoot().getDataSource().execute(req);
-
 
159
 
-
 
160
        {
-
 
161
            SQLSelect selCmdElt = new SQLSelect();
-
 
162
            final SQLTable tableCmdElt = tableStock.getTable("COMMANDE_ELEMENT");
-
 
163
            selCmdElt.addSelectStar(tableCmdElt);
-
 
164
            Where w = new Where(tableCmdElt.getField("RECU_FORCED"), "=", Boolean.FALSE).and(new Where(tableCmdElt.getField("RECU"), "=", Boolean.FALSE));
-
 
165
            selCmdElt.setWhere(w);
-
 
166
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
-
 
167
            if (res != null && res.size() > 0) {
-
 
168
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
-
 
169
 
-
 
170
                    @Override
-
 
171
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
-
 
172
                        return "Commande fournisseur N°" + rowElt.getForeign("ID_COMMANDE").getString("NUMERO");
-
 
173
                    }
-
 
174
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_RECEPT, false);
-
 
175
                try {
-
 
176
                    updater.update();
-
 
177
                } catch (SQLException e1) {
-
 
178
                    e1.printStackTrace();
-
 
179
                }
-
 
180
            }
-
 
181
        }
-
 
182
        {
-
 
183
            SQLSelect selCmdElt = new SQLSelect();
-
 
184
            final SQLTable tableCmdElt = tableStock.getTable("COMMANDE_CLIENT_ELEMENT");
-
 
185
            selCmdElt.addSelectStar(tableCmdElt);
-
 
186
            Where w = new Where(tableCmdElt.getField("LIVRE_FORCED"), "=", Boolean.FALSE).and(new Where(tableCmdElt.getField("LIVRE"), "=", Boolean.FALSE));
-
 
187
            selCmdElt.setWhere(w);
-
 
188
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
-
 
189
            if (res != null && res.size() > 0) {
-
 
190
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
-
 
191
 
-
 
192
                    @Override
-
 
193
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
-
 
194
                        return "Commande N°" + rowElt.getForeign("ID_COMMANDE_CLIENT").getString("NUMERO");
-
 
195
                    }
-
 
196
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_DELIVER, false);
-
 
197
                try {
-
 
198
                    updater.update();
-
 
199
                } catch (SQLException e1) {
-
 
200
                    e1.printStackTrace();
-
 
201
                }
-
 
202
            }
-
 
203
        }
81
    }
204
    }
82
 
205
 
83
    @Override
206
    @Override
84
    protected String getParentFFName() {
207
    protected String getParentFFName() {
85
        return "ID_COMMANDE_CLIENT";
208
        return "ID_COMMANDE_CLIENT";
Line 97... Line 220...
97
        l.add("T_PV_HT");
220
        l.add("T_PV_HT");
98
        l.add("T_PV_TTC");
221
        l.add("T_PV_TTC");
99
        l.add("QTE");
222
        l.add("QTE");
100
        l.add("QTE_UNITAIRE");
223
        l.add("QTE_UNITAIRE");
101
        l.add("QTE_LIVREE");
224
        l.add("QTE_LIVREE");
-
 
225
        l.add("LIVRE");
-
 
226
        l.add("LIVRE_FORCED");
102
        return l;
227
        return l;
103
    }
228
    }
104
 
229
 
105
    /**
230
    /**
106
     * Transfert d'une commande en commande fournisseur
231
     * Transfert d'une commande en commande fournisseur
Line 113... Line 238...
113
        SQLElement eltArticle = getDirectory().getElement("ARTICLE");
238
        SQLElement eltArticle = getDirectory().getElement("ARTICLE");
114
 
239
 
115
        final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
240
        final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
116
        List<String> fields2copy = Arrays.asList("CODE", "NOM", "VALEUR_METRIQUE_1", "VALEUR_METRIQUE_2", "VALEUR_METRIQUE_3");
241
        List<String> fields2copy = Arrays.asList("CODE", "NOM", "VALEUR_METRIQUE_1", "VALEUR_METRIQUE_2", "VALEUR_METRIQUE_3");
117
 
242
 
118
        Set<Integer> art = new HashSet<Integer>();
243
        // Set<Integer> art = new HashSet<Integer>();
119
        for (SQLRowValues sqlRow : commandeClientEltsRows) {
244
        for (SQLRowValues sqlRow : commandeClientEltsRows) {
120
            // on récupére l'article qui lui correspond
245
            // on récupére l'article qui lui correspond
121
            SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
246
            SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
122
            for (String field : fields2copy) {
247
            for (String field : fields2copy) {
123
                // if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
248
                // if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
Line 147... Line 272...
147
        }
272
        }
148
        // TODO
273
        // TODO
149
        MouvementStockSQLElement.createCommandeF(map, null, "", false);
274
        MouvementStockSQLElement.createCommandeF(map, null, "", false);
150
    }
275
    }
151
 
276
 
152
    public void createCommandeF(final List<? extends SQLRowAccessor> rowsArt) {
277
    public void createCommandeF(final List<? extends SQLRowAccessor> rowsItems) {
153
 
278
 
154
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
279
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
155
 
280
 
156
            @Override
281
            @Override
157
            public void run() {
282
            public void run() {
158
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
283
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
159
 
-
 
-
 
284
                final Set<Integer> stockChecked = new HashSet<Integer>();
160
                for (SQLRowAccessor rowArticleFind : rowsArt) {
285
                for (SQLRowAccessor rowItem : rowsItems) {
-
 
286
                    if (rowItem.getObject("ID_ARTICLE") != null && !rowItem.isForeignEmpty("ID_ARTICLE")) {
-
 
287
                        SQLRowAccessor rowArticleFind = rowItem.getForeign("ID_ARTICLE");
161
 
288
 
162
                    SQLRow row = rowArticleFind.asRow();
289
                        SQLRow row = rowArticleFind.asRow();
-
 
290
                        SQLRowAccessor rowStock = StockSQLElement.getStock(rowItem);
-
 
291
                        if (!stockChecked.contains(rowStock.getID())) {
-
 
292
                            stockChecked.add(rowStock.getID());
-
 
293
 
163
                    final int value = -Math.round(row.getForeign("ID_STOCK").getFloat("QTE_TH") - row.getFloat("QTE_MIN"));
294
                            int value = -Math.round(rowStock.getFloat("QTE_TH") - rowStock.getFloat("QTE_MIN"));
-
 
295
 
164
                    if (value > 0) {
296
                            if (value > 0) {
165
 
297
 
166
                        SQLInjector inj = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("COMMANDE_ELEMENT"));
298
                                SQLInjector inj = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("COMMANDE_ELEMENT"));
167
                        SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(row));
299
                                SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(row));
168
 
300
 
Line 174... Line 306...
174
                        // rowValsElt.put("ID_DEVISE",
306
                                // rowValsElt.put("ID_DEVISE",
175
                        // rowCmd.getForeignRow("ID_TARIF").getForeignID("ID_DEVISE"));
307
                                // rowCmd.getForeignRow("ID_TARIF").getForeignID("ID_DEVISE"));
176
                        map.add(rowArticleFind.getForeign("ID_FOURNISSEUR").asRow(), rowValsElt);
308
                                map.add(rowArticleFind.getForeign("ID_FOURNISSEUR").asRow(), rowValsElt);
177
                    }
309
                            }
178
                }
310
                        }
-
 
311
                    }
-
 
312
                }
179
                MouvementStockSQLElement.createCommandeF(map, null, "", false);
313
                MouvementStockSQLElement.createCommandeF(map, null, "", false);
180
            }
314
            }
181
        });
315
        });
182
 
316
 
183
    }
317
    }
Line 191... Line 325...
191
        l.add("PV_HT");
325
        l.add("PV_HT");
192
        return l;
326
        return l;
193
    }
327
    }
194
 
328
 
195
    @Override
329
    @Override
-
 
330
    protected void _initListRequest(ListSQLRequest req) {
-
 
331
        super._initListRequest(req);
-
 
332
        req.addToGraphToFetch("ID_DEPOT_STOCK");
-
 
333
    }
-
 
334
 
-
 
335
    @Override
196
    public ListMap<String, String> getShowAs() {
336
    public ListMap<String, String> getShowAs() {
197
        final ListMap<String, String> res = new ListMap<String, String>();
337
        final ListMap<String, String> res = new ListMap<String, String>();
198
        res.putCollection("ID_COMMANDE_CLIENT", "NUMERO", "ID_CLIENT", "DATE", "DATE_LIVRAISON_PREV");
338
        res.putCollection("ID_COMMANDE_CLIENT", "NUMERO", "DATE", "DATE_LIVRAISON_PREV", "ID_CLIENT");
-
 
339
 
199
        if (getTable().contains("ID_ARTICLE")) {
340
        if (getTable().contains("ID_ARTICLE")) {
200
            res.putCollection("ID_ARTICLE", "ID_FAMILLE_ARTICLE", "ID_FOURNISSEUR");
341
            res.putCollection("ID_ARTICLE", "ID_FAMILLE_ARTICLE", "ID_FOURNISSEUR");
201
        }
342
        }
202
        res.putCollection(null, "NOM");
343
        res.putCollection(null, "NOM");
203
        return res;
344
        return res;
Line 214... Line 355...
214
                this.addRequiredSQLObject(new JTextField(), "NOM", "left");
355
                this.addRequiredSQLObject(new JTextField(), "NOM", "left");
215
                this.addRequiredSQLObject(new JTextField(), "CODE", "right");
356
                this.addRequiredSQLObject(new JTextField(), "CODE", "right");
216
 
357
 
217
                this.addSQLObject(new ElementComboBox(), "ID_STYLE", "left");
358
                this.addSQLObject(new ElementComboBox(), "ID_STYLE", "left");
218
 
359
 
219
                this.addRequiredSQLObject(new DeviseField(), "PA_HT", "left");
360
                this.addRequiredSQLObject(new JTextField(), "PA_HT", "left");
220
                this.addSQLObject(new DeviseField(), "PV_HT", "right");
361
                this.addSQLObject(new JTextField(), "PV_HT", "right");
221
 
362
 
222
                this.addSQLObject(new JTextField(), "POIDS", "left");
363
                this.addSQLObject(new JTextField(), "POIDS", "left");
223
                this.addSQLObject(new ElementComboBox(), "ID_TAXE", "right");
364
                this.addSQLObject(new ElementComboBox(), "ID_TAXE", "right");
224
            }
365
            }
225
        };
366
        };
226
    }
367
    }
227
 
368
 
228
    @Override
369
    @Override
229
    protected String createCode() {
370
    protected String createCode() {
230
        return createCodeFromPackage() + ".item";
371
        return createCodeOfPackage() + ".item";
231
    }
372
    }
232
}
373
}