OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.config.ComptaPropsConfiguration;
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.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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SituationCompteXmlSheet extends AbstractListeSheetXml {

    private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
    private SQLElement eltEch;
    private SQLElement eltVf;
    private ComptaPropsConfiguration conf;

    public SituationCompteXmlSheet(ComptaPropsConfiguration conf, SQLRow rowClient) {
        super(rowClient);
        this.printer = PrinterNXProps.getInstance().getStringProperty("BonPrinter");
        this.eltEch = conf.getDirectory().getElement("ECHEANCE_CLIENT");
        this.conf = conf;
    }

    @Override
    protected String getStoragePathP() {
        return "Autres";
    }

    public String getDefaultTemplateId() {
        return "SituationCompte";
    }

    private Date d = null;

    @Override
    public String getName() {
        if (this.d == null) {
            this.d = new Date();
        }
        return "SituationCompte-" + this.d.getTime();
    }

    protected void createListeValues() {

        // On récupére les échéances en cours
        final SQLTable echTable = eltEch.getTable();
        SQLRowValues rowVals = new SQLRowValues(echTable);
        rowVals.putNulls("DATE", "MONTANT");
        rowVals.putRowValues("ID_SAISIE_VENTE_FACTURE").putNulls("NUMERO", "NET_A_PAYER", "DATE", "NOM");
        Where w = new Where(echTable.getField("REGLE"), "=", Boolean.FALSE);
        w = w.and(new Where(echTable.getField("REG_COMPTA"), "=", Boolean.FALSE));
        w = w.and(new Where(echTable.getField("ID_CLIENT"), "=", row.getID()));

        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
        fetcher.addSelTransf(new ITransformer<SQLSelect, SQLSelect>() {

            @Override
            public SQLSelect transformChecked(SQLSelect input) {
                input.addFieldOrder(echTable.getField("DATE"));

                return input;
            }
        }, 0);
        List<SQLRowValues> result = fetcher.fetch(w);

        List<Map<String, Object>> listValues = new ArrayList<Map<String, Object>>();
        Map<Integer, String> styleValues = new HashMap<Integer, String>();
        BigDecimal totalRegle = BigDecimal.ZERO;
        BigDecimal totalEch = BigDecimal.ZERO;
        BigDecimal totalEchPasse = BigDecimal.ZERO;
        Calendar today = Calendar.getInstance();
        for (SQLRowValues sqlRow : result) {
            Map<String, Object> mValues = new HashMap<String, Object>();
            final Calendar dateEch = sqlRow.getDate("DATE");
            mValues.put("DATE_ECHEANCE", dateFormat.format(dateEch.getTime()));
            final BigDecimal montantDu = new BigDecimal(sqlRow.getLong("MONTANT"));
            mValues.put("DU", montantDu);

            totalEch = totalEch.add(montantDu);

            SQLRowAccessor rowFact = sqlRow.getNonEmptyForeign("ID_SAISIE_VENTE_FACTURE");
            final BigDecimal regle;
            if (rowFact != null) {
                mValues.put("PIECE", rowFact.getString("NUMERO"));
                mValues.put("LIBELLE", rowFact.getString("NOM"));
                mValues.put("DATE", dateFormat.format(rowFact.getDate("DATE").getTime()));
                final BigDecimal montantFact = new BigDecimal(rowFact.getLong("NET_A_PAYER"));
                regle = montantFact.subtract(montantDu);
                mValues.put("DU", montantFact);
                mValues.put("REGLE", montantFact.subtract(montantDu));
                mValues.put("SOLDE", montantDu);

            } else {
                regle = BigDecimal.ZERO;
                mValues.put("SOLDE", montantDu);
            }
            if (dateEch.before(today)) {
                totalEchPasse = totalEchPasse.add(montantDu);
                styleValues.put(listValues.size(), "Normal");
            } else {
                styleValues.put(listValues.size(), "Titre 1");
            }
            totalRegle = totalRegle.add(regle);

            // SQLRow rowClient = sqlRow.getForeignRow("ID_CLIENT");

            listValues.add(mValues);

            // List<SQLRow> enc = sqlRow.getReferentRows(eltEncElt.getTable());
            //
            // for (SQLRow sqlRow2 : enc) {
            // Map<String, Object> mValuesEnc = new HashMap<String, Object>();
            // SQLRow rowEnc = sqlRow2.getForeignRow("ID_ENCAISSER_MONTANT");
            // SQLRow rowMdr = rowEnc.getForeignRow("ID_MODE_REGLEMENT");
            // mValuesEnc.put("NUMERO_FACTURE", "");
            // mValuesEnc.put("REFERENCE", rowMdr.getString("NOM"));
            // mValuesEnc.put("DATE", dateFormat.format(rowEnc.getDate("DATE").getTime()));
            // mValuesEnc.put("NOM_CLIENT", "");
            // mValuesEnc.put("CODE_CLIENT", "");
            // mValuesEnc.put("TELEPHONE", "");
            // mValuesEnc.put("MODE_REGLEMENT",
            // rowMdr.getForeignRow("ID_TYPE_REGLEMENT").getString("NOM"));
            // mValuesEnc.put("MONTANT",
            // GestionDevise.currencyToString(sqlRow2.getLong("MONTANT_REGLE")));
            // styleValues.put(listValues.size(), "Titre 1");
            // listValues.add(mValuesEnc);
            //
            // }
            // if (enc != null && enc.size() > 0) {
            // Map<String, Object> mValuesEnc = new HashMap<String, Object>();
            // mValuesEnc.put("DATE", dateFormat.format(sqlRow.getDate("DATE").getTime()));
            // mValuesEnc.put("MODE_REGLEMENT", "Restant à régler");
            // mValuesEnc.put("MONTANT", GestionDevise.currencyToString(sqlRow.getLong("MONTANT")));
            // styleValues.put(listValues.size(), "Titre 1");
            // listValues.add(mValuesEnc);
            // }

        }

        SQLRow rowSoc = conf.getRowSociete();
        SQLRow adrSoc = rowSoc.getForeign("ID_ADRESSE_COMMON");

        Map<String, Object> sheetVals = new HashMap<String, Object>();

        sheetVals.put("TOTAL_REGLE", totalRegle);
        sheetVals.put("TOTAL_ECHEANCE", totalEch);
        sheetVals.put("TOTAL_FUTUR", totalEch.subtract(totalEchPasse));
        sheetVals.put("TOTAL_PASSE", totalEchPasse);

        sheetVals.put("NOM_CLIENT", row.getString("NOM"));
        sheetVals.put("DATE", new Date());
        sheetVals.put("CODE_CLIENT", row.getString("CODE"));
        sheetVals.put("TELEPHONE", row.getString("TEL"));
        SQLRow rowAdrClient = row.getForeign("ID_ADRESSE");
        sheetVals.put("ADRESSE", rowAdrClient.getString("RUE"));
        sheetVals.put("VILLE", rowAdrClient.getString("CODE_POSTAL") + " " + rowAdrClient.getString("VILLE"));

        sheetVals.put("SOCIETE_NOM", rowSoc.getString("NOM"));
        sheetVals.put("SOCIETE_TEL", rowSoc.getString("NUM_TEL"));
        sheetVals.put("SOCIETE_SIRET", rowSoc.getString("NUM_SIRET"));
        sheetVals.put("SOCIETE_APE", rowSoc.getString("NUM_APE"));
        sheetVals.put("SOCIETE_NII", rowSoc.getString("NUM_NII"));
        sheetVals.put("SOCIETE_BIC", rowSoc.getString("BIC"));
        sheetVals.put("SOCIETE_IBAN", rowSoc.getString("IBAN"));
        sheetVals.put("SOCIETE_MAIL", rowSoc.getString("MAIL"));

        sheetVals.put("SOCIETE_ADRESSE", adrSoc.getString("RUE"));
        sheetVals.put("SOCIETE_VILLE", adrSoc.getString("CODE_POSTAL") + " " + adrSoc.getString("VILLE"));
        this.mapAllSheetValues.put(0, sheetVals);
        this.listAllSheetValues.put(0, listValues);
        this.styleAllSheetValues.put(0, styleValues);

    }
}