OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 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.invoice.report;
15
 
16
import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
17
import org.openconcerto.erp.preferences.PrinterNXProps;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.element.SQLElement;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowListRSH;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
24
import org.openconcerto.sql.model.SQLSelect;
25
import org.openconcerto.sql.model.SQLTable;
26
 
27
import java.math.BigDecimal;
28
import java.text.DateFormat;
29
import java.text.SimpleDateFormat;
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.Comparator;
33
import java.util.Date;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.TreeMap;
38
 
39
public class ReportingStockXmlSheet extends AbstractListeSheetXml {
40
 
41
    private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
42
 
43
    public static final String TEMPLATE_ID = "EtatStocks";
44
    public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;
45
 
46
    private Date date;
47
    private SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement("ARTICLE");
48
    private SQLElement eltStock = Configuration.getInstance().getDirectory().getElement("STOCK");
49
 
50
    public ReportingStockXmlSheet(boolean fournisseur) {
51
        super();
52
        this.printer = PrinterNXProps.getInstance().getStringProperty("BonPrinter");
53
 
54
    }
55
 
56
    @Override
57
    public String getStoragePathP() {
58
        return "Autres";
59
    }
60
 
61
    @Override
62
    public String getDefaultTemplateId() {
63
        return TEMPLATE_ID;
64
    };
65
 
66
    @Override
67
    public String getName() {
68
        if (this.date == null) {
69
            this.date = new Date();
70
        }
71
        return "EtatStocks" + this.date.getTime();
72
    }
73
 
74
    protected void createListeValues() {
75
 
76
        final SQLTable tableArt = eltArticle.getTable();
77
        SQLRowValues rowVals = new SQLRowValues(tableArt);
78
        rowVals.put("ID_FOURNISSEUR", null);
79
        rowVals.put("ID_FAMILLE_ARTICLE", null);
80
        rowVals.put("CODE", null);
81
        rowVals.put("NOM", null);
82
        rowVals.put("PA_HT", null);
83
        rowVals.put("PV_HT", null);
84
        rowVals.putRowValues("ID_STOCK").putNulls("QTE_REEL");
85
 
86
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
87
        List<SQLRowValues> values = fetch.fetch();
88
 
89
        final SQLTable tableF = tableArt.getTable("FAMILLE_ARTICLE");
90
        SQLSelect selFam = new SQLSelect();
91
        selFam.addSelect(tableF.getKey());
92
        selFam.addSelect(tableF.getField("NOM"));
93
        selFam.addSelect(tableF.getField("ID_FAMILLE_ARTICLE_PERE"));
94
 
95
        List<SQLRow> fam = SQLRowListRSH.execute(selFam);
96
        Map<Integer, SQLRow> mapF = new HashMap<Integer, SQLRow>();
97
        for (SQLRow sqlRow : fam) {
98
            mapF.put(sqlRow.getID(), sqlRow);
99
        }
100
 
101
        final SQLTable tableFourn = tableArt.getTable("FOURNISSEUR");
102
        SQLSelect selFourn = new SQLSelect();
103
        selFourn.addSelect(tableFourn.getKey());
104
        selFourn.addSelect(tableFourn.getField("NOM"));
105
 
106
        List<SQLRow> fourn = SQLRowListRSH.execute(selFourn);
107
        Map<Integer, SQLRow> mapFourn = new HashMap<Integer, SQLRow>();
108
        for (SQLRow sqlRow : fourn) {
109
            mapFourn.put(sqlRow.getID(), sqlRow);
110
        }
111
 
112
        Map<String, Line> linesFamilles = new HashMap<String, Line>();
113
        Map<Line, Map<Line, List<Line>>> myValues = new TreeMap<Line, Map<Line, List<Line>>>(new Comparator<Line>() {
114
            @Override
115
            public int compare(Line o1, Line o2) {
116
                return o1.getNomArt().compareTo(o2.getNomArt());
117
            }
118
        });
119
        Line lineTotal = new Line("Total", "", BigDecimal.ZERO, 0F);
120
        final HashMap<Integer, String> style = new HashMap<Integer, String>();
121
        for (SQLRowValues vals : values) {
122
            Float qte = vals.getForeign("ID_STOCK").getFloat("QTE_REEL");
123
            BigDecimal ha = BigDecimal.ZERO;
124
            if (qte > 0) {
174 ilm 125
                final BigDecimal puHA = vals.getBigDecimal("PA_HT");
126
                ha = puHA.multiply(new BigDecimal(qte));
144 ilm 127
 
128
                int idFamille = vals.getForeignID("ID_FAMILLE_ARTICLE");
129
                SQLRow rowF = mapF.get(idFamille);
174 ilm 130
                Line lineArt = new Line(vals.getString("NOM"), vals.getString("CODE"), puHA, ha, qte);
144 ilm 131
 
132
                // Init des lines familles
133
 
134
                final Line lineF, lineSF;
135
                if (rowF == null) {
136
                    if (!linesFamilles.containsKey("Undef")) {
137
                        linesFamilles.put("Undef", new Line("Sans famille", "", BigDecimal.ZERO, 0F));
138
                        linesFamilles.put("Undef-Undef", new Line("", "", BigDecimal.ZERO, 0F));
139
                    }
140
                    lineF = linesFamilles.get("Undef");
141
                    lineSF = linesFamilles.get("Undef-Undef");
142
                } else if (rowF.getObject("ID_FAMILLE_ARTICLE_PERE") == null || rowF.isForeignEmpty("ID_FAMILLE_ARTICLE_PERE")) {
143
                    if (!linesFamilles.containsKey(String.valueOf(rowF.getID()))) {
144
                        linesFamilles.put(String.valueOf(rowF.getID()), new Line(rowF.getString("NOM"), "", BigDecimal.ZERO, 0F));
145
                        linesFamilles.put(String.valueOf(rowF.getID()) + "-Undef", new Line("", "", BigDecimal.ZERO, 0F));
146
                    }
147
                    if (!linesFamilles.containsKey(String.valueOf(rowF.getID()) + "-Undef")) {
148
                        linesFamilles.put(String.valueOf(rowF.getID()) + "-Undef", new Line("", "", BigDecimal.ZERO, 0F));
149
                    }
150
                    lineF = linesFamilles.get(String.valueOf(rowF.getID()));
151
                    lineSF = linesFamilles.get(String.valueOf(rowF.getID()) + "-Undef");
152
                } else {
153
                    if (!linesFamilles.containsKey(String.valueOf(rowF.getID()))) {
154
                        linesFamilles.put(String.valueOf(rowF.getID()), new Line(rowF.getString("NOM"), "", BigDecimal.ZERO, 0F));
155
                    }
156
                    if (!linesFamilles.containsKey(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")))) {
157
                        SQLRow rowSF = mapF.get(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE"));
158
                        linesFamilles.put(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")), new Line(rowSF.getString("NOM"), "", BigDecimal.ZERO, 0F));
159
                    }
160
                    lineF = linesFamilles.get(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")));
161
                    lineSF = linesFamilles.get(String.valueOf(rowF.getID()));
162
                }
163
 
164
                // init values
165
                if (!myValues.containsKey(lineF)) {
166
                    myValues.put(lineF, new TreeMap<Line, List<Line>>(new Comparator<Line>() {
167
                        @Override
168
                        public int compare(Line o1, Line o2) {
169
                            return o1.getNomArt().compareTo(o2.getNomArt());
170
                        }
171
                    }));
172
                    myValues.get(lineF).put(lineSF, new ArrayList<Line>());
173
                }
174
                Map<Line, List<Line>> mapSF = myValues.get(lineF);
175
                if (!mapSF.containsKey(lineSF)) {
176
                    mapSF.put(lineSF, new ArrayList<Line>());
177
                }
178
 
179
                // Store values
180
                List<Line> lines = mapSF.get(lineSF);
181
                lines.add(lineArt);
182
                lineTotal.add(lineArt);
183
                lineF.add(lineArt);
184
                lineSF.add(lineArt);
185
            }
186
        }
187
 
188
        // Sort Values
189
        List<Map<String, Object>> listValues = new ArrayList<Map<String, Object>>();
190
 
191
        for (Line f : myValues.keySet()) {
192
            listValues.add(f.getMapXMLSheet());
193
            style.put(style.keySet().size(), "Titre 1");
194
            Map<Line, List<Line>> sfs = myValues.get(f);
195
            for (Line sf : sfs.keySet()) {
196
                listValues.add(sf.getMapXMLSheet());
197
                style.put(style.keySet().size(), "Titre 2");
198
                List<Line> vals = sfs.get(sf);
199
                Collections.sort(vals, new Comparator<Line>() {
200
                    @Override
201
                    public int compare(Line o1, Line o2) {
202
                        return o1.getNomArt().compareTo(o2.getNomArt());
203
                    }
204
                });
205
                for (Line line : vals) {
206
                    listValues.add(line.getMapXMLSheet());
207
                    style.put(style.keySet().size(), "Normal");
208
                }
209
            }
210
        }
211
 
212
        listValues.add(lineTotal.getMapXMLSheet());
213
        style.put(style.keySet().size(), "Titre 1");
214
 
215
        final Map<String, Object> valuesSheet = new HashMap<String, Object>();
216
        valuesSheet.put("DATE", "Au " + dateFormat.format(new Date()));
217
        //
218
        this.listAllSheetValues.put(0, listValues);
219
 
220
        this.styleAllSheetValues.put(0, style);
221
        this.mapAllSheetValues.put(0, valuesSheet);
222
    }
223
 
224
    class Line {
225
        final private String nomArt, codeArt;
174 ilm 226
        private BigDecimal totalHA, puHA;
144 ilm 227
        private Float qte;
228
 
229
        public Line(String nomArt, String codeArt, BigDecimal totalHA, Float qte) {
174 ilm 230
            this(nomArt, codeArt, BigDecimal.ZERO, totalHA, qte);
231
        }
232
 
233
        public Line(String nomArt, String codeArt, BigDecimal puHA, BigDecimal totalHA, Float qte) {
144 ilm 234
            this.nomArt = nomArt;
235
            this.codeArt = codeArt;
236
            this.totalHA = totalHA;
174 ilm 237
            this.puHA = puHA;
144 ilm 238
            this.qte = qte;
239
        }
240
 
241
        public Float getQte() {
242
            return qte;
243
        }
244
 
245
        public String getCodeArt() {
246
            return codeArt;
247
        }
248
 
174 ilm 249
        public BigDecimal getPuHA() {
250
            return puHA;
251
        }
252
 
144 ilm 253
        public String getNomArt() {
254
            return nomArt;
255
        }
256
 
257
        public BigDecimal getTotalHA() {
258
            return totalHA;
259
        }
260
 
261
        public void add(Line l) {
262
            this.totalHA = this.totalHA.add(l.getTotalHA());
263
            if (l.getQte() > 0) {
264
                this.qte = this.qte + l.getQte();
265
            }
266
        }
267
 
268
        public Map<String, Object> getMapXMLSheet() {
269
            Map<String, Object> m = new HashMap<String, Object>();
270
            m.put("CODE", getCodeArt());
271
            m.put("NOM", getNomArt());
272
            m.put("QTE", getQte());
174 ilm 273
            m.put("PU_HA", getPuHA());
144 ilm 274
            m.put("TOTAL_HA", getTotalHA());
275
            return m;
276
        }
277
 
278
    }
279
 
280
}