OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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