OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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.
 */
 
 /*
 * Créé le 19 nov. 2012
 */
package org.openconcerto.erp.generationDoc.gestcomm;

import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRowAccessor;
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 org.openconcerto.utils.cc.ITransformer;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReportingTaxeComplementaireSheetXML extends AbstractListeSheetXml {

    public static final String TEMPLATE_ID = "ReportingTaxeComplementaire";
    public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;
    private Date date;

    private Date dateD, dateF;

    public ReportingTaxeComplementaireSheetXML(Date debut, Date fin) {
        super();

        this.dateD = debut;
        this.dateF = fin;

    }

    @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 "ReportingTaxeCompl" + this.date.getTime();
    }

    @Override
    protected void createListeValues() {

        this.mapAllSheetValues = new HashMap<Integer, Map<String, Object>>();
        this.listAllSheetValues = new HashMap<Integer, List<Map<String, Object>>>();
        this.styleAllSheetValues = new HashMap<Integer, Map<Integer, String>>();

        fillSynthese();

    }

    private static SQLTable tableVF = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE").getTable();

    class TaxeComplRecap {
        private final String code, nom, numeroCptProduit, numeroCpt;
        private BigDecimal percentTaxe, totalBase, qte;

        public TaxeComplRecap(SQLRowAccessor foreignTaxeCompl) {
            this.code = foreignTaxeCompl.getString("CODE");
            this.nom = foreignTaxeCompl.getString("NOM");
            this.qte = BigDecimal.ZERO;
            this.percentTaxe = foreignTaxeCompl.getBigDecimal("POURCENT");
            this.totalBase = BigDecimal.ZERO;
            if (foreignTaxeCompl.getObject("ID_COMPTE_PCE") != null && !foreignTaxeCompl.isForeignEmpty("ID_COMPTE_PCE")) {
                this.numeroCpt = foreignTaxeCompl.getForeign("ID_COMPTE_PCE").getString("NUMERO");
            } else {
                this.numeroCpt = "";
            }
            if (foreignTaxeCompl.getObject("ID_COMPTE_PCE_PRODUITS") != null && !foreignTaxeCompl.isForeignEmpty("ID_COMPTE_PCE_PRODUITS")) {
                this.numeroCptProduit = foreignTaxeCompl.getForeign("ID_COMPTE_PCE_PRODUITS").getString("NUMERO");
            } else {
                this.numeroCptProduit = "";
            }
        }

        public void cumul(BigDecimal qte, BigDecimal total) {
            this.qte = qte.add(this.qte);
            this.totalBase = total.add(this.totalBase);
        }

        public String getCode() {
            return code;
        }

        public String getNom() {
            return nom;
        }

        public String getNumeroCpt() {
            return numeroCpt;
        }

        public String getNumeroCptProduit() {
            return numeroCptProduit;
        }

        public BigDecimal getQte() {
            return qte;
        }

        public BigDecimal getPercentTaxe() {
            return percentTaxe;

        }

        public BigDecimal getTotalBase() {
            return totalBase;
        }

        public BigDecimal getTotalTaxe() {
            return this.totalBase.multiply(this.percentTaxe.movePointLeft(2)).setScale(2, RoundingMode.HALF_UP);
        }

        public BigDecimal getTotalBaseHT() {
            return this.totalBase.subtract(getTotalTaxe());
        }
    }

    private void fillSynthese() {
        final SQLTable tableVF = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE");
        final SQLTable tableVFElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");

        SQLRowValues rowvalsVF = new SQLRowValues(tableVF);
        rowvalsVF.put("NUMERO", null);
        rowvalsVF.put("DATE", null);

        SQLRowValues rowvalsVFElt = new SQLRowValues(tableVFElt);
        rowvalsVFElt.put("ID_SAISIE_VENTE_FACTURE", rowvalsVF);
        rowvalsVFElt.put("T_PV_HT", null);
        rowvalsVFElt.put("QTE", null);
        rowvalsVFElt.put("QTE_UNITAIRE", null);
        final SQLRowValues rowValsTaxeCompl = rowvalsVFElt.putRowValues("ID_ARTICLE").putRowValues("ID_TAXE_COMPLEMENTAIRE");
        rowValsTaxeCompl.putNulls("CODE", "NOM", "POURCENT");
        rowValsTaxeCompl.putRowValues("ID_COMPTE_PCE_PRODUITS").putNulls("NUMERO", "NOM");
        rowValsTaxeCompl.putRowValues("ID_COMPTE_PCE").putNulls("NUMERO", "NOM");
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowvalsVFElt);
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {

            @Override
            public SQLSelect transformChecked(SQLSelect input) {
                Where w = new Where(input.getAlias(tableVF).getField("DATE"), dateD, dateF);
                input.setWhere(w);
                return input;
            }
        });

        Map<Integer, TaxeComplRecap> recap = new HashMap<Integer, ReportingTaxeComplementaireSheetXML.TaxeComplRecap>();
        List<SQLRowValues> results = fetcher.fetch();
        for (SQLRowValues sqlRowValues : results) {
            TaxeComplRecap r;
            if (sqlRowValues.getForeign("ID_ARTICLE") != null && !sqlRowValues.isForeignEmpty("ID_ARTICLE")) {
                final SQLRowAccessor foreignArt = sqlRowValues.getForeign("ID_ARTICLE");
                if (foreignArt.getForeign("ID_TAXE_COMPLEMENTAIRE") != null && !foreignArt.isForeignEmpty("ID_TAXE_COMPLEMENTAIRE")) {
                    final SQLRowAccessor foreignTaxeCompl = foreignArt.getForeign("ID_TAXE_COMPLEMENTAIRE");
                    if (recap.containsKey(foreignTaxeCompl.getID())) {
                        r = recap.get(foreignTaxeCompl.getID());
                    } else {
                        r = new TaxeComplRecap(foreignTaxeCompl);
                        recap.put(foreignTaxeCompl.getID(), r);
                    }
                    r.cumul(sqlRowValues.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRowValues.getInt("QTE"))), sqlRowValues.getBigDecimal("T_PV_HT"));
                }
            }
        }

        List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
        this.listAllSheetValues.put(0, values);
        Map<Integer, String> style = new HashMap<Integer, String>();
        this.styleAllSheetValues.put(0, style);

        BigDecimal totalBaseHT = BigDecimal.ZERO;
        BigDecimal totalTaxe = BigDecimal.ZERO;
        BigDecimal qteTotal = BigDecimal.ZERO;

        for (TaxeComplRecap item : recap.values()) {
            Map<String, Object> vals = new HashMap<String, Object>();

            vals.put("COMPTE_NUMERO", item.getNumeroCpt());
            vals.put("COMPTE_PRODUIT_NUMERO", item.getNumeroCptProduit());

            vals.put("CODE", item.getCode());
            vals.put("NOM", item.getNom());
            vals.put("QTE", item.getQte());
            vals.put("POURCENT", item.getPercentTaxe());
            vals.put("TOTAL_BASE", item.getTotalBaseHT());
            vals.put("TOTAL_TAXE", item.getTotalTaxe());
            style.put(values.size(), "Normal");
            totalBaseHT = totalBaseHT.add(item.getTotalBaseHT());
            totalTaxe = totalTaxe.add(item.getTotalTaxe());
            qteTotal = qteTotal.add(item.getQte());
            values.add(vals);
        }

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Map<String, Object> vals = new HashMap<String, Object>();
        vals.put("DATE_DEB", this.dateD);
        vals.put("DATE_FIN", this.dateF);
        vals.put("PERIODE", "Période du " + format.format(this.dateD) + " au " + format.format(this.dateF));
        vals.put("TOTAL_TAXE", totalTaxe);
        vals.put("TOTAL_BASE", totalBaseHT);
        vals.put("TOTAL_QTE", qteTotal);
        this.mapAllSheetValues.put(0, vals);
        // style.put(values.size(), "Titre 1");
    }
}