Dépôt officiel du code source de l'ERP OpenConcerto
Rev 182 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.invoice.report;
import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
import org.openconcerto.erp.preferences.PrinterNXProps;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class EtatStockInventaireXmlSheet extends AbstractListeSheetXml {
private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
public static final String TEMPLATE_ID = "EtatStockInventaire";
public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;
private Date date;
private SQLElement eltArticle;
private SQLElement eltStock;
public EtatStockInventaireXmlSheet(SQLRow etatStock) {
super();
this.row = etatStock;
this.printer = PrinterNXProps.getInstance().getStringProperty("BonPrinter");
}
@Override
public String getStoragePathP() {
return "Autres";
}
@Override
public String getDefaultTemplateId() {
return TEMPLATE_ID;
};
@Override
public String getName() {
if (this.date == null) {
this.date = new Date();
}
return "EtatStocks" + this.date.getTime();
}
protected void createListeValues() {
SQLRowValues rowVals = new SQLRowValues(this.row.getTable().getTable("ETAT_STOCK_ELEMENT"));
rowVals.put("QTE", null);
SQLRowValues rowValsArt = rowVals.putRowValues("ID_ARTICLE");
rowValsArt.put("ID_FAMILLE_ARTICLE", null);
rowValsArt.put("CODE", null);
rowValsArt.put("NOM", null);
rowValsArt.put("PA_HT", null);
rowValsArt.put("PV_HT", null);
SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
List<SQLRowValues> values = fetch.fetch(new Where(rowVals.getTable().getField("ID_ETAT_STOCK"), "=", this.row.getID()));
final SQLTable tableF = this.row.getTable().getTable("FAMILLE_ARTICLE");
SQLSelect selFam = new SQLSelect();
selFam.addSelect(tableF.getKey());
selFam.addSelect(tableF.getField("NOM"));
selFam.addSelect(tableF.getField("ID_FAMILLE_ARTICLE_PERE"));
List<SQLRow> fam = SQLRowListRSH.execute(selFam);
Map<Integer, SQLRow> mapF = new HashMap<Integer, SQLRow>();
for (SQLRow sqlRow : fam) {
mapF.put(sqlRow.getID(), sqlRow);
}
final SQLTable tableFourn = this.row.getTable().getTable("FOURNISSEUR");
SQLSelect selFourn = new SQLSelect();
selFourn.addSelect(tableFourn.getKey());
selFourn.addSelect(tableFourn.getField("NOM"));
List<SQLRow> fourn = SQLRowListRSH.execute(selFourn);
Map<Integer, SQLRow> mapFourn = new HashMap<Integer, SQLRow>();
for (SQLRow sqlRow : fourn) {
mapFourn.put(sqlRow.getID(), sqlRow);
}
Map<String, Line> linesFamilles = new HashMap<String, Line>();
Map<Line, Map<Line, List<Line>>> myValues = new TreeMap<Line, Map<Line, List<Line>>>(new Comparator<Line>() {
@Override
public int compare(Line o1, Line o2) {
return o1.getNomArt().compareTo(o2.getNomArt());
}
});
Line lineTotal = new Line("Total", "", BigDecimal.ZERO, BigDecimal.ZERO);
final HashMap<Integer, String> style = new HashMap<Integer, String>();
for (SQLRowValues vals : values) {
BigDecimal qte = vals.getBigDecimal("QTE");
SQLRowAccessor rowValsArticle = vals.getForeign("ID_ARTICLE");
BigDecimal ha = rowValsArticle.getBigDecimal("PA_HT").multiply(qte);
int idFamille = rowValsArticle.getForeignID("ID_FAMILLE_ARTICLE");
SQLRow rowF = mapF.get(idFamille);
Line lineArt = new Line(rowF == null || rowF.isUndefined() ? "Sans famille" : rowF.getString("NOM"), rowValsArticle.getString("NOM"), rowValsArticle.getString("CODE"), ha, qte);
// Init des lines familles
final Line lineF, lineSF;
if (rowF == null) {
if (!linesFamilles.containsKey("Undef")) {
linesFamilles.put("Undef", new Line("Sans famille", "", BigDecimal.ZERO, BigDecimal.ZERO));
linesFamilles.put("Undef-Undef", new Line("", "", BigDecimal.ZERO, BigDecimal.ZERO));
}
lineF = linesFamilles.get("Undef");
lineSF = linesFamilles.get("Undef-Undef");
} else if (rowF.getObject("ID_FAMILLE_ARTICLE_PERE") == null || rowF.isForeignEmpty("ID_FAMILLE_ARTICLE_PERE")) {
if (!linesFamilles.containsKey(String.valueOf(rowF.getID()))) {
linesFamilles.put(String.valueOf(rowF.getID()), new Line(rowF.getString("NOM"), "", BigDecimal.ZERO, BigDecimal.ZERO));
linesFamilles.put(String.valueOf(rowF.getID()) + "-Undef", new Line("", "", BigDecimal.ZERO, BigDecimal.ZERO));
}
if (!linesFamilles.containsKey(String.valueOf(rowF.getID()) + "-Undef")) {
linesFamilles.put(String.valueOf(rowF.getID()) + "-Undef", new Line("", "", BigDecimal.ZERO, BigDecimal.ZERO));
}
lineF = linesFamilles.get(String.valueOf(rowF.getID()));
lineSF = linesFamilles.get(String.valueOf(rowF.getID()) + "-Undef");
} else {
if (!linesFamilles.containsKey(String.valueOf(rowF.getID()))) {
linesFamilles.put(String.valueOf(rowF.getID()), new Line(rowF.getString("NOM"), "", BigDecimal.ZERO, BigDecimal.ZERO));
}
if (!linesFamilles.containsKey(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")))) {
SQLRow rowSF = mapF.get(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE"));
linesFamilles.put(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")), new Line(rowSF.getString("NOM"), "", BigDecimal.ZERO, BigDecimal.ZERO));
}
lineF = linesFamilles.get(String.valueOf(rowF.getForeignID("ID_FAMILLE_ARTICLE_PERE")));
lineSF = linesFamilles.get(String.valueOf(rowF.getID()));
}
// init values
if (!myValues.containsKey(lineF)) {
myValues.put(lineF, new TreeMap<Line, List<Line>>(new Comparator<Line>() {
@Override
public int compare(Line o1, Line o2) {
return o1.getNomArt().compareTo(o2.getNomArt());
}
}));
myValues.get(lineF).put(lineSF, new ArrayList<Line>());
}
Map<Line, List<Line>> mapSF = myValues.get(lineF);
if (!mapSF.containsKey(lineSF)) {
mapSF.put(lineSF, new ArrayList<Line>());
}
// Store values
List<Line> lines = mapSF.get(lineSF);
lines.add(lineArt);
lineTotal.add(lineArt);
lineF.add(lineArt);
lineSF.add(lineArt);
}
// Sort Values
List<Map<String, Object>> listValues = new ArrayList<Map<String, Object>>();
for (Line f : myValues.keySet()) {
listValues.add(f.getMapXMLSheet());
style.put(style.keySet().size(), "Titre 1");
Map<Line, List<Line>> sfs = myValues.get(f);
for (Line sf : sfs.keySet()) {
// listValues.add(sf.getMapXMLSheet());
// style.put(style.keySet().size(), "Titre 2");
List<Line> vals = sfs.get(sf);
Collections.sort(vals, new Comparator<Line>() {
@Override
public int compare(Line o1, Line o2) {
return o1.getNomArt().compareTo(o2.getNomArt());
}
});
for (Line line : vals) {
listValues.add(line.getMapXMLSheet());
style.put(style.keySet().size(), "Normal");
}
}
}
listValues.add(lineTotal.getMapXMLSheet());
style.put(style.keySet().size(), "Titre 1");
final Map<String, Object> valuesSheet = new HashMap<String, Object>();
valuesSheet.put("DATE", "Au " + dateFormat.format(new Date()));
//
this.listAllSheetValues.put(0, listValues);
this.styleAllSheetValues.put(0, style);
this.mapAllSheetValues.put(0, valuesSheet);
}
class Line {
private final String nomArt;
private final String codeArt;
private final String famille;
private BigDecimal totalHA;
private BigDecimal qte;
public Line(String nomArt, String codeArt, BigDecimal totalHA, BigDecimal qte) {
this("", nomArt, codeArt, totalHA, qte);
}
public Line(String famille, String nomArt, String codeArt, BigDecimal totalHA, BigDecimal qte) {
this.famille = famille;
this.nomArt = nomArt;
this.codeArt = codeArt;
this.totalHA = totalHA;
this.qte = qte;
}
public BigDecimal getQte() {
return qte;
}
public String getCodeArt() {
return codeArt;
}
public String getNomArt() {
return nomArt;
}
public BigDecimal getTotalHA() {
return totalHA;
}
public void add(Line l) {
this.totalHA = this.totalHA.add(l.getTotalHA());
this.qte = this.qte.add(l.getQte());
}
public Map<String, Object> getMapXMLSheet() {
Map<String, Object> m = new HashMap<String, Object>();
m.put("FAMILLE", this.famille);
m.put("CODE", getCodeArt());
m.put("NOM", getNomArt());
m.put("QTE", getQte());
m.put("TOTAL_HA", getTotalHA());
return m;
}
}
}