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 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
142 ilm 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.order.action;
15
 
156 ilm 16
import org.openconcerto.erp.action.CreateIListFrameAbstractAction;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
142 ilm 18
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
19
import org.openconcerto.erp.core.common.ui.ListeViewPanel;
182 ilm 20
import org.openconcerto.erp.core.common.ui.NumberCellRenderer;
142 ilm 21
import org.openconcerto.erp.core.sales.order.element.CommandeClientElementSQLElement;
174 ilm 22
import org.openconcerto.erp.core.sales.product.ui.QteAcommanderRenderer;
182 ilm 23
import org.openconcerto.erp.core.supplychain.stock.action.ListeDesStocksAction;
156 ilm 24
import org.openconcerto.erp.core.supplychain.stock.element.StockSQLElement;
25
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
142 ilm 26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.FieldPath;
28
import org.openconcerto.sql.model.FieldRef;
29
import org.openconcerto.sql.model.SQLName;
30
import org.openconcerto.sql.model.SQLRowAccessor;
31
import org.openconcerto.sql.model.SQLRowValues;
32
import org.openconcerto.sql.model.SQLSelect;
33
import org.openconcerto.sql.model.SQLSelectJoin;
34
import org.openconcerto.sql.model.Where;
35
import org.openconcerto.sql.model.graph.Path;
156 ilm 36
import org.openconcerto.sql.preferences.SQLPreferences;
142 ilm 37
import org.openconcerto.sql.view.IListFrame;
156 ilm 38
import org.openconcerto.sql.view.IListPanel;
142 ilm 39
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
40
import org.openconcerto.sql.view.list.IListe;
41
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
42
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
156 ilm 43
import org.openconcerto.sql.view.list.SQLTableModelSource;
142 ilm 44
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
45
import org.openconcerto.ui.DefaultGridBagConstraints;
46
import org.openconcerto.ui.FrameUtil;
47
import org.openconcerto.utils.CollectionUtils;
48
import org.openconcerto.utils.DecimalUtils;
49
import org.openconcerto.utils.cc.ITransformer;
50
 
51
import java.awt.GridBagConstraints;
52
import java.awt.GridBagLayout;
53
import java.awt.event.ActionEvent;
54
import java.math.BigDecimal;
55
import java.math.RoundingMode;
56
import java.util.Collection;
57
import java.util.List;
58
import java.util.Set;
59
 
60
import javax.swing.AbstractAction;
61
import javax.swing.JPanel;
62
 
156 ilm 63
public class ListeDesElementsACommanderClientAction extends CreateIListFrameAbstractAction<CommandeClientElementSQLElement> {
142 ilm 64
 
156 ilm 65
    public ListeDesElementsACommanderClientAction(final ComptaPropsConfiguration conf) {
66
        super(conf, CommandeClientElementSQLElement.class);
142 ilm 67
    }
68
 
156 ilm 69
    @Override
70
    protected String getPanelVariant() {
71
        return this.getClass().getSimpleName();
142 ilm 72
    }
73
 
156 ilm 74
    @Override
75
    protected SQLTableModelSource createTableSource() {
76
        final SQLTableModelSource tableSource = super.createTableSource();
77
        final CommandeClientElementSQLElement eltCmd = getElem();
142 ilm 78
        tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
79
 
80
            @Override
81
            public SQLSelect transformChecked(SQLSelect input) {
82
                // new SQLName(eltCmd.getTable().getDBRoot().getName(),
83
                // tableBLElement.getName()).quote()
84
                final String quoteQteL = new SQLName(input.getAlias(eltCmd.getTable()).getAlias(), "QTE_LIVREE").quote();
85
                final String quoteQte = new SQLName(input.getAlias(eltCmd.getTable()).getAlias(), "QTE").quote();
86
                final String quoteQteU = new SQLName(input.getAlias(eltCmd.getTable()).getAlias(), "QTE_UNITAIRE").quote();
87
                Where w = Where.createRaw(quoteQteL + " < (" + quoteQte + "*" + quoteQteU + ")", eltCmd.getTable().getField("QTE_LIVREE"), eltCmd.getTable().getField("QTE"),
88
                        eltCmd.getTable().getField("QTE_UNITAIRE"));
156 ilm 89
                w = w.and(new Where(eltCmd.getTable().getField("LIVRE_FORCED"), "=", Boolean.FALSE));
142 ilm 90
                input.setWhere(w);
91
                return input;
92
            }
93
        });
94
 
156 ilm 95
        SQLPreferences prefs = SQLPreferences.getMemCached(eltCmd.getTable().getDBRoot());
96
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false)) {
97
            BaseSQLTableModelColumn colStockD = new BaseSQLTableModelColumn("Dépôt", String.class) {
98
 
99
                @Override
100
                protected Object show_(SQLRowAccessor r) {
101
 
102
                    final SQLRowAccessor foreign = r.getForeign("ID_ARTICLE");
103
                    if (foreign != null && !foreign.isUndefined()) {
104
                        SQLRowAccessor foreign2 = StockSQLElement.getStockFetched(r);
105
                        if (foreign2 != null && !foreign2.isUndefined()) {
106
                            return foreign2.getForeign("ID_DEPOT_STOCK").getString("NOM");
107
                        }
108
                    }
109
                    return "";
110
                }
111
 
112
                @Override
113
                public Set<FieldPath> getPaths() {
114
                    Path p = new Path(eltCmd.getTable());
115
                    p = p.add(p.getLast().getField("ID_ARTICLE"));
116
                    Path p2 = p.add(p.getLast().getTable("STOCK").getField("ID_ARTICLE"));
117
                    Path p3 = p2.add(p2.getLast().getField("ID_DEPOT_STOCK"));
118
                    Path p4 = p.add(p.getLast().getField("ID_DEPOT_STOCK"));
119
                    return CollectionUtils.createSet(new FieldPath(p3, "NOM"), new FieldPath(p4, "NOM"));
120
                }
121
            };
122
            tableSource.getColumns().add(colStockD);
123
        }
142 ilm 124
        BaseSQLTableModelColumn colStockR = new BaseSQLTableModelColumn("Stock Reel", Float.class) {
125
 
126
            @Override
127
            protected Object show_(SQLRowAccessor r) {
128
 
129
                final SQLRowAccessor foreign = r.getForeign("ID_ARTICLE");
174 ilm 130
 
142 ilm 131
                if (foreign != null && !foreign.isUndefined()) {
174 ilm 132
                    if (foreign.getBoolean("GESTION_STOCK")) {
133
                        SQLRowAccessor foreign2 = StockSQLElement.getStockFetched(r);
134
                        if (foreign2 != null && !foreign2.isUndefined()) {
135
                            return foreign2.getFloat("QTE_REEL");
136
                        }
142 ilm 137
                    }
138
                }
174 ilm 139
                return null;
142 ilm 140
            }
141
 
142
            @Override
143
            public Set<FieldPath> getPaths() {
144
                Path p = new Path(eltCmd.getTable());
145
                p = p.add(p.getLast().getField("ID_ARTICLE"));
156 ilm 146
                Path p2 = p.add(p.getLast().getTable("STOCK").getField("ID_ARTICLE"));
147
                return CollectionUtils.createSet(new FieldPath(p, "ID_DEPOT_STOCK"), new FieldPath(p2, "QTE_REEL"), new FieldPath(p2, "ID_DEPOT_STOCK"));
142 ilm 148
            }
149
        };
150
        tableSource.getColumns().add(colStockR);
151
 
152
        BaseSQLTableModelColumn colLiv2 = new BaseSQLTableModelColumn("Stock TH", Float.class) {
153
 
154
            @Override
155
            protected Object show_(SQLRowAccessor r) {
156
 
157
                final SQLRowAccessor foreign = r.getForeign("ID_ARTICLE");
158
                if (foreign != null && !foreign.isUndefined()) {
174 ilm 159
                    if (foreign.getBoolean("GESTION_STOCK")) {
160
 
161
                        SQLRowAccessor foreign2 = StockSQLElement.getStockFetched(r);
162
                        if (foreign2 != null && !foreign2.isUndefined()) {
163
                            return foreign2.getFloat("QTE_TH");
164
                        }
142 ilm 165
                    }
166
                }
174 ilm 167
                return null;
142 ilm 168
            }
169
 
170
            @Override
171
            public Set<FieldPath> getPaths() {
172
                Path p = new Path(eltCmd.getTable());
173
                p = p.add(p.getLast().getField("ID_ARTICLE"));
156 ilm 174
                Path p2 = p.add(p.getLast().getTable("STOCK").getField("ID_ARTICLE"));
175
                return CollectionUtils.createSet(new FieldPath(p, "ID_DEPOT_STOCK"), new FieldPath(p2, "QTE_TH"), new FieldPath(p2, "ID_DEPOT_STOCK"));
142 ilm 176
            }
177
        };
178
        tableSource.getColumns().add(colLiv2);
179
 
156 ilm 180
        BaseSQLTableModelColumn colStockMin = new BaseSQLTableModelColumn("Stock Min", Float.class) {
142 ilm 181
 
182
            @Override
183
            protected Object show_(SQLRowAccessor r) {
184
 
185
                final SQLRowAccessor foreign = r.getForeign("ID_ARTICLE");
156 ilm 186
                if (foreign != null && !foreign.isUndefined()) {
174 ilm 187
                    if (foreign.getBoolean("GESTION_STOCK")) {
188
 
189
                        SQLRowAccessor foreign2 = StockSQLElement.getStockFetched(r);
190
                        if (foreign2 != null && !foreign2.isUndefined()) {
191
                            return foreign2.getFloat("QTE_MIN");
192
                        }
156 ilm 193
                    }
194
                }
174 ilm 195
                return null;
142 ilm 196
            }
197
 
198
            @Override
199
            public Set<FieldPath> getPaths() {
200
                Path p = new Path(eltCmd.getTable());
201
                p = p.add(p.getLast().getField("ID_ARTICLE"));
156 ilm 202
                Path p2 = p.add(p.getLast().getTable("STOCK").getField("ID_ARTICLE"));
203
                return CollectionUtils.createSet(new FieldPath(p, "ID_DEPOT_STOCK"), new FieldPath(p2, "QTE_MIN"), new FieldPath(p2, "ID_DEPOT_STOCK"));
142 ilm 204
            }
205
        };
206
        tableSource.getColumns().add(colStockMin);
207
 
208
        BaseSQLTableModelColumn colSug = new BaseSQLTableModelColumn("Qtè à commander", Float.class) {
209
 
210
            @Override
211
            protected Object show_(SQLRowAccessor r) {
212
 
213
                final SQLRowAccessor foreign = r.getForeign("ID_ARTICLE");
214
                if (foreign != null && !foreign.isUndefined()) {
174 ilm 215
                    if (foreign.getBoolean("GESTION_STOCK")) {
216
 
217
                        SQLRowAccessor foreign2 = StockSQLElement.getStockFetched(r);
218
                        if (foreign2 != null && !foreign2.isUndefined()) {
219
                            float qteMin = foreign2.getFloat("QTE_MIN");
220
                            float manque = foreign2.getFloat("QTE_TH") - qteMin;
221
                            if (manque < 0) {
222
                                return -manque;
223
                            } else {
224
                                return 0f;
225
                            }
142 ilm 226
                        }
227
                    }
228
                }
174 ilm 229
                return r.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(r.getInt("QTE"))).floatValue();
156 ilm 230
 
142 ilm 231
            }
232
 
233
            @Override
234
            public Set<FieldPath> getPaths() {
156 ilm 235
                Path p = new Path(eltCmd.getTable());
236
                p = p.add(p.getLast().getField("ID_ARTICLE"));
237
                Path p2 = p.add(p.getLast().getTable("STOCK").getField("ID_ARTICLE"));
238
                return CollectionUtils.createSet(new FieldPath(p, "ID_DEPOT_STOCK"), new FieldPath(p2, "QTE_TH"), new FieldPath(p2, "QTE_MIN"), new FieldPath(p2, "ID_DEPOT_STOCK"));
142 ilm 239
            }
240
        };
241
        tableSource.getColumns().add(colSug);
174 ilm 242
        colSug.setRenderer(new QteAcommanderRenderer());
142 ilm 243
 
156 ilm 244
        return tableSource;
142 ilm 245
    }
246
 
247
    private BigDecimal getAvancementLFromBL(SQLRowAccessor r) {
248
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("COMMANDE_CLIENT_ELEMENT"));
249
        BigDecimal totalQte = BigDecimal.ZERO;
250
        BigDecimal totalQteL = BigDecimal.ZERO;
251
        for (SQLRowAccessor row : rows) {
252
            BigDecimal qte = row.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(row.getInt("QTE")));
253
            totalQte = totalQte.add(qte);
254
            if (row.getBoolean("LIVRE_FORCED") || row.getBoolean("LIVRE")) {
255
                totalQteL = totalQteL.add(qte);
256
            } else {
257
                totalQteL = totalQteL.add(row.getBigDecimal("QTE_LIVREE"));
258
            }
259
        }
260
        if (totalQte.signum() != 0) {
261
            return totalQteL.divide(totalQte, DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
262
        } else {
263
            return BigDecimal.ONE.movePointRight(2);
264
        }
265
    }
266
 
156 ilm 267
    @Override
268
    protected IListPanel instantiateListPanel(SQLTableModelSource tableSource, String panelVariant) {
269
        final IListPanel panel = super.instantiateListPanel(tableSource, panelVariant);
270
        final CommandeClientElementSQLElement eltCmd = getElem();
142 ilm 271
        // final List<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>> fields = new
272
        // ArrayList<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>>(2);
273
        // fields.add(Tuple2.create(panel.getListe().getSource().getColumn(eltCmd.getTable().getField("T_HT")),
274
        // IListTotalPanel.Type.SOMME));
275
        // fields.add(Tuple2.create(this.colAvancement, IListTotalPanel.Type.AVANCEMENT_TTC));
276
        // final IListTotalPanel totalPanel = new IListTotalPanel(panel.getListe(), fields, null,
277
        // "Total des commandes de la liste");
278
 
279
        final GridBagConstraints c = new DefaultGridBagConstraints();
280
        c.gridwidth = GridBagConstraints.REMAINDER;
281
        c.fill = GridBagConstraints.BOTH;
282
        c.anchor = GridBagConstraints.EAST;
283
        c.weightx = 1;
284
        c.gridy = 4;
285
 
286
        // Date panel
287
        final IListFilterDatePanel datePanel = new IListFilterDatePanel(panel.getListe(), eltCmd.getTable().getForeignTable("ID_COMMANDE_CLIENT").getField("DATE"),
288
                IListFilterDatePanel.getDefaultMap());
289
 
290
        datePanel.setFilterOnDefault();
291
 
292
        final JPanel bottomPanel = new JPanel();
293
        bottomPanel.setLayout(new GridBagLayout());
294
        bottomPanel.setOpaque(false);
295
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
296
        c2.fill = GridBagConstraints.NONE;
297
        c2.weightx = 1;
298
        bottomPanel.add(datePanel, c2);
299
 
300
        // c2.gridx++;
301
        // c2.weightx = 0;
302
        // c2.anchor = GridBagConstraints.EAST;
303
        // bottomPanel.add(totalPanel, c2);
304
 
305
        panel.add(bottomPanel, c);
156 ilm 306
 
307
        PredicateRowAction action = new PredicateRowAction(new AbstractAction("Calcul des besoins") {
308
 
309
            @Override
310
            public void actionPerformed(ActionEvent e) {
311
 
182 ilm 312
                final SQLPreferences prefs = SQLPreferences.getMemCached(eltCmd.getTable().getDBRoot());
156 ilm 313
 
182 ilm 314
                if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false)) {
315
                    ListeDesStocksAction action = new ListeDesStocksAction(true);
316
                    FrameUtil.show(action.createFrame());
317
                } else {
156 ilm 318
 
182 ilm 319
                    final SQLElement artElt = eltCmd.getForeignElement("ID_ARTICLE");
320
                    final SQLTableModelSourceOnline createTableSource = artElt.createTableSource();
321
                    createTableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
322
                        @Override
323
                        public SQLSelect transformChecked(SQLSelect input) {
324
                            FieldRef refStock = input.getAlias(artElt.getTable().getForeignTable("ID_STOCK").getField("QTE_TH"));
325
                            FieldRef refStockMin = input.getAlias(artElt.getTable().getForeignTable("ID_STOCK").getField("QTE_MIN"));
326
 
327
                            SQLSelectJoin j = input.getJoinFromField(artElt.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"));
328
                            if (j == null) {
329
                                j = input.addBackwardJoin("LEFT", artElt.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"), null);
330
                            }
331
 
332
                            Where w = new Where(refStock, "<", refStockMin);
333
                            w = w.and(new Where(j.getJoinedTable().getKey(), "=", (Object) null));
334
                            input.setWhere(w);
335
                            // input.setHaving(Where.createRaw("COUNT(\"" +
336
                            // j.getJoinedTable().getKey()
337
                            // + "\")" + " = 0", Arrays.asList(j.getJoinedTable().getKey())));
338
                            return input;
339
                        }
340
                    });
341
 
342
                    IListe listeArt = new IListe(createTableSource);
343
                    final PredicateRowAction predicateACtion = new PredicateRowAction(new AbstractAction("Passer une commande fournisseur") {
344
 
345
                        @Override
346
                        public void actionPerformed(ActionEvent e) {
347
                            List<SQLRowAccessor> selectedRows = IListe.get(e).getSelectedRowAccessors();
348
                            eltCmd.transfertCommande(selectedRows);
349
                        }
350
                    }, true);
351
                    predicateACtion.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
352
                    listeArt.addIListeAction(predicateACtion);
353
                    ListeViewPanel p = new ListeViewPanel(artElt, listeArt);
354
                    IListFrame f = new IListFrame(p);
355
                    FrameUtil.show(f);
356
                }
156 ilm 357
            }
358
 
359
        }, true);
360
        action.setPredicate(IListeEvent.createTotalRowCountPredicate(0, Integer.MAX_VALUE));
361
        panel.getListe().addIListeAction(action);
362
 
142 ilm 363
        return panel;
364
    }
365
 
366
}