OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
67 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
 
16
import org.openconcerto.erp.action.CreateFrameAbstractAction;
17
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
18
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
93 ilm 19
import org.openconcerto.erp.core.sales.order.ui.EtatCommandeClient;
20
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
67 ilm 21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.element.SQLElement;
80 ilm 23
import org.openconcerto.sql.model.FieldPath;
24
import org.openconcerto.sql.model.SQLRowAccessor;
93 ilm 25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.Where;
80 ilm 27
import org.openconcerto.sql.model.graph.Path;
28
import org.openconcerto.sql.model.graph.PathBuilder;
93 ilm 29
import org.openconcerto.sql.preferences.SQLPreferences;
67 ilm 30
import org.openconcerto.sql.view.IListFrame;
31
import org.openconcerto.sql.view.ListeAddPanel;
80 ilm 32
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
67 ilm 33
import org.openconcerto.sql.view.list.IListe;
90 ilm 34
import org.openconcerto.sql.view.list.SQLTableModelColumn;
73 ilm 35
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
67 ilm 36
import org.openconcerto.ui.DefaultGridBagConstraints;
73 ilm 37
import org.openconcerto.ui.FrameUtil;
38
import org.openconcerto.ui.state.WindowStateManager;
80 ilm 39
import org.openconcerto.ui.table.PercentTableCellRenderer;
40
import org.openconcerto.utils.CollectionUtils;
90 ilm 41
import org.openconcerto.utils.DecimalUtils;
42
import org.openconcerto.utils.Tuple2;
80 ilm 43
import org.openconcerto.utils.cc.ITransformer;
67 ilm 44
 
45
import java.awt.GridBagConstraints;
73 ilm 46
import java.awt.GridBagLayout;
47
import java.io.File;
80 ilm 48
import java.math.BigDecimal;
49
import java.math.RoundingMode;
67 ilm 50
import java.util.ArrayList;
80 ilm 51
import java.util.Collection;
67 ilm 52
import java.util.List;
80 ilm 53
import java.util.Set;
67 ilm 54
 
55
import javax.swing.Action;
80 ilm 56
import javax.swing.JButton;
67 ilm 57
import javax.swing.JFrame;
58
import javax.swing.JPanel;
93 ilm 59
import javax.swing.JTabbedPane;
67 ilm 60
 
61
public class ListeDesCommandesClientAction extends CreateFrameAbstractAction {
62
 
63
    public ListeDesCommandesClientAction() {
64
        super();
65
        this.putValue(Action.NAME, "Liste des commandes clients");
66
    }
67
 
90 ilm 68
    private BaseSQLTableModelColumn colAvancement;
69
 
67 ilm 70
    public JFrame createFrame() {
73 ilm 71
        final JFrame frame = new JFrame("Commandes clients");
72
        // Actions
93 ilm 73
        final SQLElement eltCmd = Configuration.getInstance().getDirectory().getElement("COMMANDE_CLIENT");
74
        SQLPreferences prefs = SQLPreferences.getMemCached(eltCmd.getTable().getDBRoot());
75
        boolean cmdState = prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ORDER_PACKAGING_MANAGEMENT, true);
80 ilm 76
 
93 ilm 77
        if (cmdState && eltCmd.getTable().contains("ETAT_COMMANDE")) {
78
            JTabbedPane pane = new JTabbedPane();
79
            for (EtatCommandeClient etat : EtatCommandeClient.values()) {
80
                final JPanel orderPanel = createAllOrderPanel(etat);
81
                pane.add(etat.getTranslation(), orderPanel);
82
            }
83
            frame.getContentPane().add(pane);
84
        } else {
85
            final JPanel orderPanel = createAllOrderPanel(null);
86
 
87
            frame.getContentPane().add(orderPanel);
88
        }
80 ilm 89
        FrameUtil.setBounds(frame);
90
        final File file = IListFrame.getConfigFile(eltCmd, frame.getClass());
91
        if (file != null)
92
            new WindowStateManager(frame, file).loadState();
93
        return frame;
94
    }
95
 
93 ilm 96
    JPanel createAllOrderPanel(final EtatCommandeClient etat) {
80 ilm 97
        final SQLElement eltCmd = Configuration.getInstance().getDirectory().getElement("COMMANDE_CLIENT");
98
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
93 ilm 99
        if (etat != null) {
100
            tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
101
                public SQLSelect transformChecked(SQLSelect input) {
102
                    input.setWhere(new Where(eltCmd.getTable().getField("ETAT_COMMANDE"), "=", etat.getId()));
103
                    return input;
104
                }
105
            });
106
        }
67 ilm 107
 
142 ilm 108
        BaseSQLTableModelColumn colLiv2 = new BaseSQLTableModelColumn("Avancement livraison v2", BigDecimal.class) {
73 ilm 109
 
142 ilm 110
            @Override
111
            protected Object show_(SQLRowAccessor r) {
73 ilm 112
 
142 ilm 113
                return getAvancementLFromBL(r);
114
            }
80 ilm 115
 
142 ilm 116
            @Override
117
            public Set<FieldPath> getPaths() {
118
                final Path p = new PathBuilder(eltCmd.getTable()).addTable("COMMANDE_CLIENT_ELEMENT").build();
156 ilm 119
                return CollectionUtils.createSet(new FieldPath(p, "ID_ARTICLE"), new FieldPath(p, "PV_HT"), new FieldPath(p, "QTE_LIVREE"), new FieldPath(p, "QTE"), new FieldPath(p, "QTE_UNITAIRE"),
120
                        new FieldPath(p, "LIVRE_FORCED"), new FieldPath(p, "LIVRE"));
142 ilm 121
            }
122
        };
123
        tableSource.getColumns().add(colLiv2);
124
        colLiv2.setRenderer(new PercentTableCellRenderer());
125
 
126
        if (eltCmd.getTable().getDBRoot().contains("TARIF_AGENCE")) {
127
            this.colAvancement = colLiv2;
132 ilm 128
        } else {
142 ilm 129
 
132 ilm 130
            this.colAvancement = new BaseSQLTableModelColumn("Avancement facturation", BigDecimal.class) {
131
 
132
                @Override
133
                protected Object show_(SQLRowAccessor r) {
134
 
135
                    return getAvancement(r);
136
                }
137
 
138
                @Override
139
                public Set<FieldPath> getPaths() {
140
                    final Path p = new PathBuilder(eltCmd.getTable()).addTable("TR_COMMANDE_CLIENT").addTable("SAISIE_VENTE_FACTURE").build();
141
                    return CollectionUtils.createSet(new FieldPath(p, "T_HT"));
142
                }
143
            };
144
        }
145
 
90 ilm 146
        tableSource.getColumns().add(this.colAvancement);
147
        this.colAvancement.setRenderer(new PercentTableCellRenderer());
94 ilm 148
        final ListeAddPanel panel = getPanel(eltCmd, tableSource);
73 ilm 149
        return panel;
150
    }
151
 
142 ilm 152
    private BigDecimal getAvancementLFromBL(SQLRowAccessor r) {
153
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("COMMANDE_CLIENT_ELEMENT"));
154
        BigDecimal totalQte = BigDecimal.ZERO;
155
        BigDecimal totalQteL = BigDecimal.ZERO;
156
        for (SQLRowAccessor row : rows) {
157
            BigDecimal qte = row.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(row.getInt("QTE")));
156 ilm 158
            // On ne prend en compte que les articles ou les lignes différentes de 0
159
            if (!row.isForeignEmpty("ID_ARTICLE") || row.getBigDecimal("PV_HT").signum() != 0) {
160
                totalQte = totalQte.add(qte);
161
                if (row.getBoolean("LIVRE_FORCED") || row.getBoolean("LIVRE")) {
162
                    totalQteL = totalQteL.add(qte);
163
                } else if (row.getBigDecimal("QTE_LIVREE") != null) {
164
                    final BigDecimal qteLivree = row.getBigDecimal("QTE_LIVREE");
165
                    if (qteLivree != null) {
166
                        totalQteL = totalQteL.add(qteLivree);
167
                    }
149 ilm 168
                }
142 ilm 169
            }
170
        }
171
        if (totalQte.signum() != 0) {
172
            return totalQteL.divide(totalQte, DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
173
        } else {
174
            return BigDecimal.ONE.movePointRight(2);
175
        }
176
    }
177
 
132 ilm 178
    private BigDecimal getAvancementL(SQLRowAccessor r) {
179
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE_CLIENT"));
180
        long totalFact = 0;
181
        long total = r.getLong("T_HT");
182
        for (SQLRowAccessor row : rows) {
183
            if (!row.isForeignEmpty("ID_BON_DE_LIVRAISON")) {
184
                SQLRowAccessor rowFact = row.getForeign("ID_BON_DE_LIVRAISON");
185
                Long l = rowFact.getLong("TOTAL_HT");
186
                totalFact += l;
187
            }
188
        }
189
        if (total > 0) {
190
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
191
        } else {
192
            return BigDecimal.ONE.movePointRight(2);
193
        }
194
    }
195
 
80 ilm 196
    private BigDecimal getAvancement(SQLRowAccessor r) {
197
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE_CLIENT"));
198
        long totalFact = 0;
199
        long total = r.getLong("T_HT");
200
        for (SQLRowAccessor row : rows) {
201
            if (!row.isForeignEmpty("ID_SAISIE_VENTE_FACTURE")) {
202
                SQLRowAccessor rowFact = row.getForeign("ID_SAISIE_VENTE_FACTURE");
203
                Long l = rowFact.getLong("T_HT");
204
                totalFact += l;
205
            }
206
        }
207
        if (total > 0) {
90 ilm 208
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
80 ilm 209
        } else {
210
            return BigDecimal.ONE.movePointRight(2);
211
        }
212
    }
213
 
94 ilm 214
    private ListeAddPanel getPanel(final SQLElement eltCmd, final SQLTableModelSourceOnline tableSource) {
80 ilm 215
        final ListeAddPanel panel = new ListeAddPanel(eltCmd, new IListe(tableSource)) {
216
            @Override
217
            protected void createUI() {
218
                super.createUI();
219
                this.btnMngr.setAdditional(this.buttonEffacer, new ITransformer<JButton, String>() {
73 ilm 220
 
80 ilm 221
                    @Override
222
                    public String transformChecked(JButton input) {
223
 
224
                        SQLRowAccessor row = getListe().fetchSelectedRow();
225
 
226
                        BigDecimal b = getAvancement(row);
227
 
93 ilm 228
                        if (row.getLong("T_HT") > 0 && b.signum() != 0) {
80 ilm 229
                            return "Vous ne pouvez pas supprimer une commande facturée !";
230
                        }
231
                        return null;
232
                    }
233
                });
234
 
235
            }
236
        };
237
 
90 ilm 238
        final List<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>> fields = new ArrayList<Tuple2<? extends SQLTableModelColumn, IListTotalPanel.Type>>(2);
142 ilm 239
        // if (panel.getListe().getSource().getColumn(eltCmd.getTable().getField("T_HA")) != null) {
240
        // fields.add(Tuple2.create(panel.getListe().getSource().getColumn(eltCmd.getTable().getField("T_HA")),
241
        // IListTotalPanel.Type.SOMME));
242
        // }
90 ilm 243
        fields.add(Tuple2.create(panel.getListe().getSource().getColumn(eltCmd.getTable().getField("T_HT")), IListTotalPanel.Type.SOMME));
244
        fields.add(Tuple2.create(this.colAvancement, IListTotalPanel.Type.AVANCEMENT_TTC));
245
        final IListTotalPanel totalPanel = new IListTotalPanel(panel.getListe(), fields, null, "Total des commandes de la liste");
67 ilm 246
 
73 ilm 247
        final GridBagConstraints c = new DefaultGridBagConstraints();
67 ilm 248
        c.gridwidth = GridBagConstraints.REMAINDER;
73 ilm 249
        c.fill = GridBagConstraints.BOTH;
67 ilm 250
        c.anchor = GridBagConstraints.EAST;
251
        c.weightx = 1;
252
        c.gridy = 4;
253
 
254
        // Date panel
73 ilm 255
        final IListFilterDatePanel datePanel = new IListFilterDatePanel(panel.getListe(), eltCmd.getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
67 ilm 256
 
73 ilm 257
        datePanel.setFilterOnDefault();
67 ilm 258
 
73 ilm 259
        final JPanel bottomPanel = new JPanel();
260
        bottomPanel.setLayout(new GridBagLayout());
261
        bottomPanel.setOpaque(false);
262
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
263
        c2.fill = GridBagConstraints.NONE;
264
        c2.weightx = 1;
265
        bottomPanel.add(datePanel, c2);
67 ilm 266
 
73 ilm 267
        c2.gridx++;
268
        c2.weightx = 0;
269
        c2.anchor = GridBagConstraints.EAST;
270
        bottomPanel.add(totalPanel, c2);
67 ilm 271
 
73 ilm 272
        panel.add(bottomPanel, c);
273
        return panel;
274
    }
67 ilm 275
 
276
}