OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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