OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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