OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.sql.Configuration;
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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class ReportingClientXml extends AbstractListeSheetXml {

    public static final String TEMPLATE_ID = "ReportingClient";
    public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;

    private final Calendar toDay = Calendar.getInstance();
    private final Date d1, d2;
    private DateFormat format;

    public ReportingClientXml(SQLRow rowClient, Date d1, Date d2) {
        super(rowClient);
        this.d1 = d1;
        this.d2 = d2;
    }

    @Override
    protected void createListeValues() {
        final SQLTable table = Configuration.getInstance().getDirectory().getElement("CLIENT").getTable();
        SQLRowValues rowVals = new SQLRowValues(table);
        rowVals.putNulls("NOM");

        final SQLTable tableFacture = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE").getTable();
        SQLRowValues rowValsF = new SQLRowValues(tableFacture);
        rowValsF.put("ID_CLIENT", rowVals);
        rowValsF.putNulls("NUMERO", "NOM", "T_HT", "T_TTC", "DATE", "INFOS");

        final SQLTable tableEch = Configuration.getInstance().getDirectory().getElement("ECHEANCE_CLIENT").getTable();
        SQLRowValues rowValsE = new SQLRowValues(tableEch);
        rowValsE.put("ID_SAISIE_VENTE_FACTURE", rowValsF);
        rowValsE.putNulls("REGLE", "DATE", "MONTANT", "REG_COMPTA", "DATE_LAST_RELANCE");

        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsF);

        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {

            @Override
            public SQLSelect transformChecked(SQLSelect input) {

                Where w = new Where(tableFacture.getField("ID_CLIENT"), "=", row.getID());
                if (d1 != null || d2 != null) {
                    if (d1 == null) {
                        w = w.and(new Where(tableFacture.getField("DATE"), "<=", d2));
                    } else if (d2 == null) {
                        w = w.and(new Where(tableFacture.getField("DATE"), ">=", d1));
                    } else {
                        w = w.and(new Where(tableFacture.getField("DATE"), d1, d2));
                    }
                }
                input.setWhere(w);
                return input;
            }
        });

        List<SQLRowValues> l = fetcher.fetch();

        this.mapAllSheetValues = new HashMap<Integer, Map<String, Object>>();
        Map<String, Object> mapValues = new HashMap<String, Object>();
        mapValues.put("CLIENT", this.row.getString("NOM"));
        mapValues.put("DATE", toDay.getTime());

        String upTo = "Invoices up to ";
        String since = "Invoices since ";
        String between = "Invoices between ";
        String and = " and ";
        if (this.row.getObject("ID_LANGUE") != null && this.row.getInt("ID_LANGUE") == 2) {

            upTo = "Factures jusqu'au ";
            since = "Factures depuis le ";
            between = "Factures entre le ";
            and = " et le ";
            format = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
        } else {
            format = new SimpleDateFormat("d MMMM yyyy", Locale.US);
            // format = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
        }

        if (d1 != null || d2 != null) {
            if (d1 == null) {
                mapValues.put("PERIOD", upTo + format.format(d2));
            } else if (d2 == null) {
                mapValues.put("PERIOD", since + format.format(d1));

            } else {
                mapValues.put("PERIOD", between + format.format(d1) + and + format.format(d2));
            }
        }

        this.mapAllSheetValues.put(0, mapValues);

        List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();

        this.listAllSheetValues.put(0, values);
        this.styleAllSheetValues = new HashMap<Integer, Map<Integer, String>>();
        Map<Integer, String> styles = new HashMap<Integer, String>();
        this.styleAllSheetValues.put(0, styles);

        Map<String, Object> total = new HashMap<String, Object>();
        total.put("NUMERO_FACTURE", "TOTAL");
        double totalTTC = 0;
        double totalDu = 0;
        for (SQLRowValues sqlRowValues : l) {
            Map<String, Object> line = new HashMap<String, Object>();
            line.put("NUMERO_FACTURE", sqlRowValues.getObject("NUMERO"));
            line.put("INFOS", sqlRowValues.getObject("INFOS"));
            line.put("NOM", sqlRowValues.getObject("NOM"));

            Calendar c = sqlRowValues.getDate("DATE");
            line.put("DATE", c.getTime());

            final double ttc = sqlRowValues.getLong("T_TTC") / 100.0D;
            line.put("T_TTC", ttc);

            double du = 0;

            // Client
            SQLRowAccessor rowC = sqlRowValues.getForeign("ID_CLIENT");
            if (rowC != null && !rowC.isUndefined()) {
                line.put("CLIENT", rowC.getObject("NOM"));
            }

            Calendar dateEch = Calendar.getInstance();
            dateEch.setTime(c.getTime());
            // Echeance
            Collection<SQLRowValues> echeances = sqlRowValues.getReferentRows(tableEch);
            for (SQLRowValues sqlRowValues2 : echeances) {
                if (!sqlRowValues2.getBoolean("REGLE") && !sqlRowValues2.getBoolean("REG_COMPTA")) {
                    du += sqlRowValues2.getLong("MONTANT") / 100.0D;
                    dateEch = sqlRowValues2.getDate("DATE");
                }
            }

            line.put("ECHEANCE", dateEch.getTime());

            line.put("DU", du);
            line.put("REGLE", ttc - du);

            totalTTC += ttc;
            totalDu += du;

            if (du == 0) {
                styles.put(values.size(), "Normal");
            } else {
                if (!toDay.after(dateEch)) {
                    styles.put(values.size(), "Titre 1");
                } else {
                    styles.put(values.size(), "Titre 2");
                }
            }
            values.add(line);
        }

        total.put("T_TTC", totalTTC);
        total.put("DU", totalDu);
        total.put("REGLE", totalTTC - totalDu);
        styles.put(values.size(), "Titre 3");
        values.add(total);

    }

    @Override
    public String getDefaultTemplateId() {

        return TEMPLATE_ID;
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "ReportingClient" + this.row.getString("NOM");
    }

}