OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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