OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.supplychain.order.action;
15
 
16
import org.openconcerto.erp.action.CreateFrameAbstractAction;
174 ilm 17
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
18 ilm 18
import org.openconcerto.sql.Configuration;
94 ilm 19
import org.openconcerto.sql.element.SQLElement;
20
import org.openconcerto.sql.model.FieldPath;
174 ilm 21
import org.openconcerto.sql.model.SQLField;
94 ilm 22
import org.openconcerto.sql.model.SQLRowAccessor;
23
import org.openconcerto.sql.model.graph.Path;
24
import org.openconcerto.sql.model.graph.PathBuilder;
18 ilm 25
import org.openconcerto.sql.view.IListFrame;
26
import org.openconcerto.sql.view.ListeAddPanel;
94 ilm 27
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
25 ilm 28
import org.openconcerto.sql.view.list.IListe;
94 ilm 29
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
174 ilm 30
import org.openconcerto.ui.DefaultGridBagConstraints;
94 ilm 31
import org.openconcerto.ui.table.PercentTableCellRenderer;
32
import org.openconcerto.utils.CollectionUtils;
33
import org.openconcerto.utils.DecimalUtils;
18 ilm 34
 
174 ilm 35
import java.awt.GridBagConstraints;
144 ilm 36
import java.math.BigDecimal;
37
import java.math.RoundingMode;
38
import java.util.Collection;
174 ilm 39
import java.util.HashMap;
40
import java.util.Map;
144 ilm 41
import java.util.Set;
42
 
43
import javax.swing.Action;
44
import javax.swing.JFrame;
45
 
18 ilm 46
public class ListeDesCommandesAction extends CreateFrameAbstractAction {
47
    public ListeDesCommandesAction() {
48
        super();
49
        this.putValue(Action.NAME, "Liste des commandes fournisseurs");
50
    }
51
 
52
    public JFrame createFrame() {
53
 
94 ilm 54
        final SQLElement elementCmd = Configuration.getInstance().getDirectory().getElement("COMMANDE");
55
        final SQLTableModelSourceOnline tableSource = elementCmd.getTableSource(true);
174 ilm 56
        BaseSQLTableModelColumn colAvancementR = new BaseSQLTableModelColumn("Avancement réception", BigDecimal.class) {
94 ilm 57
 
58
            @Override
59
            protected Object show_(SQLRowAccessor r) {
60
 
174 ilm 61
                return getAvancementLFromBR(r);
94 ilm 62
            }
63
 
64
            @Override
65
            public Set<FieldPath> getPaths() {
174 ilm 66
                final Path p = new PathBuilder(elementCmd.getTable()).addTable("COMMANDE_ELEMENT").build();
67
                return CollectionUtils.createSet(new FieldPath(p, "RECU_FORCED"), new FieldPath(p, "RECU"), new FieldPath(p, "QTE_RECUE"), new FieldPath(p, "PA_HT"), new FieldPath(p, "ID_ARTICLE"),
68
                        new FieldPath(p, "QTE"), new FieldPath(p, "QTE_UNITAIRE"));
94 ilm 69
            }
70
        };
174 ilm 71
        tableSource.getColumns().add(colAvancementR);
72
        colAvancementR.setRenderer(new PercentTableCellRenderer());
73
 
74
        BaseSQLTableModelColumn colAvancement = new BaseSQLTableModelColumn("Avancement facturation", BigDecimal.class) {
75
 
76
            @Override
77
            protected Object show_(SQLRowAccessor r) {
78
 
79
                return getAvancementFact(r);
80
            }
81
 
82
            @Override
83
            public Set<FieldPath> getPaths() {
84
                final Path p = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("SAISIE_ACHAT").build();
85
                final Path p2 = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("FACTURE_FOURNISSEUR").build();
86
 
87
                final Path pBr = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("BON_RECEPTION").build();
88
                final Path trBr = pBr.add(elementCmd.getTable().getTable("TR_BON_RECEPTION"));
89
                final Path pFact2 = trBr.addTable("FACTURE_FOURNISSEUR");
90
                final Path pAchat2 = trBr.addTable("SAISIE_ACHAT");
91
                return CollectionUtils.createSet(new FieldPath(p, "MONTANT_HT"), new FieldPath(p2, "T_HT"), new FieldPath(pAchat2, "MONTANT_HT"), new FieldPath(pFact2, "T_HT"));
92
            }
93
        };
94 ilm 94
        tableSource.getColumns().add(colAvancement);
95
        colAvancement.setRenderer(new PercentTableCellRenderer());
96
 
174 ilm 97
        final ListeAddPanel panel = new ListeAddPanel(elementCmd, new IListe(tableSource));
98
        // Date panel
99
        Map<IListe, SQLField> map = new HashMap<IListe, SQLField>();
100
        map.put(panel.getListe(), elementCmd.getTable().getField("DATE"));
94 ilm 101
 
174 ilm 102
        IListFilterDatePanel datePanel = new IListFilterDatePanel(map, IListFilterDatePanel.getDefaultMap());
103
        GridBagConstraints c = new DefaultGridBagConstraints();
104
        c.weightx = 0;
105
        c.fill = GridBagConstraints.NONE;
106
        c.gridy = 4;
107
        c.anchor = GridBagConstraints.CENTER;
108
        c.weighty = 0;
109
        datePanel.setFilterOnDefault();
110
        panel.add(datePanel, c);
111
        final IListFrame frame = new IListFrame(panel);
112
 
18 ilm 113
        return frame;
114
    }
115
 
174 ilm 116
    private BigDecimal getAvancementLFromBR(SQLRowAccessor r) {
117
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("COMMANDE_ELEMENT"));
118
        BigDecimal totalQte = BigDecimal.ZERO;
119
        BigDecimal totalQteL = BigDecimal.ZERO;
120
        for (SQLRowAccessor row : rows) {
121
            BigDecimal qte = row.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(row.getInt("QTE")));
122
            // On ne prend en compte que les articles ou les lignes différentes de 0
123
            if (!row.isForeignEmpty("ID_ARTICLE") || row.getBigDecimal("PA_HT").signum() != 0) {
124
                totalQte = totalQte.add(qte);
125
                if (row.getBoolean("RECU_FORCED") || row.getBoolean("RECU")) {
126
                    totalQteL = totalQteL.add(qte);
127
                } else if (row.getBigDecimal("QTE_RECUE") != null) {
128
                    final BigDecimal qteLivree = row.getBigDecimal("QTE_RECUE");
129
                    if (qteLivree != null) {
130
                        totalQteL = totalQteL.add(qteLivree);
131
                    }
132
                }
133
            }
134
        }
135
        if (totalQte.signum() != 0) {
136
            return totalQteL.divide(totalQte, DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
137
        } else {
138
            return BigDecimal.ONE.movePointRight(2);
139
        }
140
    }
141
 
142
    BigDecimal bigDecimal100 = new BigDecimal(100.0);
143
 
144
    private BigDecimal getAvancementFact(SQLRowAccessor r) {
145
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE"));
146
        long totalFact = 0;
147
        long total = r.getLong("T_HT");
148
        for (SQLRowAccessor row : rows) {
149
            if (!row.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
150
                SQLRowAccessor rowFact = row.getForeign("ID_FACTURE_FOURNISSEUR");
151
                Long l = rowFact.getLong("T_HT");
152
                totalFact += l;
153
            }
154
            if (!row.isForeignEmpty("ID_SAISIE_ACHAT")) {
155
                SQLRowAccessor rowFact = row.getForeign("ID_SAISIE_ACHAT");
156
                Long l = rowFact.getLong("MONTANT_HT");
157
                totalFact += l;
158
            }
159
 
160
            if (!row.isForeignEmpty("ID_BON_RECEPTION")) {
161
                Collection<? extends SQLRowAccessor> rowsBr = row.getForeign("ID_BON_RECEPTION").getReferentRows(r.getTable().getTable("TR_BON_RECEPTION"));
162
                for (SQLRowAccessor rowTrBr : rowsBr) {
163
                    if (!rowTrBr.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
164
                        SQLRowAccessor rowFact = rowTrBr.getForeign("ID_FACTURE_FOURNISSEUR");
165
                        Long l = rowFact.getLong("T_HT");
166
                        totalFact += l;
167
                    }
168
                    if (!rowTrBr.isForeignEmpty("ID_SAISIE_ACHAT")) {
169
                        SQLRowAccessor rowFact = rowTrBr.getForeign("ID_SAISIE_ACHAT");
170
                        Long l = rowFact.getLong("MONTANT_HT");
171
                        totalFact += l;
172
                    }
173
 
174
                }
175
            }
176
        }
177
 
178
        if (total > 0) {
179
            return this.bigDecimal100.min(new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP));
180
        } else {
181
            return BigDecimal.ONE.movePointRight(2);
182
        }
183
    }
184
 
94 ilm 185
    private BigDecimal getAvancement(SQLRowAccessor r) {
186
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE"));
187
        long totalFact = 0;
188
        long total = r.getLong("T_HT");
189
        for (SQLRowAccessor row : rows) {
190
            if (!row.isForeignEmpty("ID_BON_RECEPTION")) {
191
                SQLRowAccessor rowFact = row.getForeign("ID_BON_RECEPTION");
192
                Long l = rowFact.getLong("TOTAL_HT");
193
                totalFact += l;
194
            }
195
        }
196
        if (total > 0) {
197
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
198
        } else {
199
            return BigDecimal.ONE.movePointRight(2);
200
        }
201
    }
202
 
18 ilm 203
}