OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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.panel.compta;

import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.utils.StringUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.dbutils.handlers.ArrayListHandler;

public class ExportEBPPivot extends AbstractExport {

    // Format pseudo CSV
    // ###EBPPivotV1
    // 1,150116,AC,401,,"SOGEAB INTERC","0001",1884.03,C,150116,EUR
    // 2,150116,AC,6222,,"SOGEAB INTERC","0001",1570.03,D,,EUR
    // 3,150116,AC,4456614,,"SOGEAB INTERC","0001",314.00,D,,EUR

    // numéro d'écriture
    // date
    // journal
    // numero de compte
    // vide
    // label ecriture
    // label piece
    // montant xxxxx.xx
    // D si débit, C si crédit
    // date ou ""
    // EUR

    // Note : Ascii pur (ne pas exporter d'accents), fin de ligne: \r\n

    private final DecimalFormat decimalFormat = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.UK));

    private String formatCents(final Number n) {
        return this.decimalFormat.format(BigDecimal.valueOf(n.longValue()).movePointLeft(2));
    }

    private List<Object[]> data;

    public ExportEBPPivot(DBRoot rootSociete) {
        super(rootSociete, "EBP_Pivot", ".txt");
    }

    @Override
    protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
        final SQLTable tableEcriture = getEcritureT();
        final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
        final SQLTable tablePiece = tableMouvement.getForeignTable("ID_PIECE");
        final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
        final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");

        final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
        sel.addSelect(tableJrnl.getField("CODE"));
        sel.addSelect(tableJrnl.getField("NOM"));
        sel.addSelect(tableEcriture.getField("ID"));
        sel.addSelect(tableEcriture.getField("DATE"));
        sel.addSelect(tableCompte.getField("NUMERO"));
        sel.addSelect(tableCompte.getField("NOM"));
        sel.addSelect(tableMouvement.getField("NUMERO"));
        sel.addSelect(tableEcriture.getField("NOM"));
        sel.addSelect(tableEcriture.getField("DEBIT"));
        sel.addSelect(tableEcriture.getField("CREDIT"));
        sel.addSelect(tablePiece.getField("NOM"));
        sel.addFieldOrder(tableJrnl.getField("CODE"));
        sel.addFieldOrder(tableEcriture.getField("DATE"));
        sel.addFieldOrder(tableMouvement.getField("NUMERO"));

        @SuppressWarnings("unchecked")
        final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
        this.data = l;
        return l == null ? 0 : l.size();
    }

    @Override
    protected void export(OutputStream out) throws IOException {
        final Writer bufOut = new OutputStreamWriter(out, StringUtils.ASCII);
        final DateFormat dateFormat = new SimpleDateFormat("ddMMyy");
        bufOut.write("###EBPPivotV1\r\n");
        for (final Object[] array : this.data) {
            // numéro d'écriture
            bufOut.write(String.valueOf(array[2]));
            bufOut.write(',');
            // date
            bufOut.write(dateFormat.format((Date) array[3]));
            bufOut.write(',');
            // journal
            String codeJournal = String.valueOf(array[0]);
            bufOut.write(codeJournal);
            bufOut.write(',');
            // numero de compte
            String numeroCompte = String.valueOf(array[4]);
            bufOut.write(StringUtils.toAsciiString(numeroCompte));
            bufOut.write(',');
            // vide
            bufOut.write(',');
            // label ecriture
            String libelleEcriture = String.valueOf(array[7]);
            bufOut.write('\"');
            bufOut.write(StringUtils.toAsciiString(libelleEcriture));
            bufOut.write('\"');
            bufOut.write(',');
            // label piece
            String libellePiece = String.valueOf(array[10]);
            bufOut.write('\"');
            bufOut.write(StringUtils.toAsciiString(libellePiece));
            bufOut.write('\"');
            bufOut.write(',');
            // montant xxxxx.xx
            final long debit = ((Number) array[8]).longValue();
            final long credit = ((Number) array[9]).longValue();
            if (debit > 0 && credit > 0)
                throw new IllegalStateException("debit et credit >0");
            final long cents;
            final char sign;
            if (credit > 0) {
                cents = credit;
                sign = 'C';
            } else {
                cents = debit;
                sign = 'D';
            }
            bufOut.write(formatCents(cents));
            bufOut.write(',');

            // D si débit, C si crédit
            bufOut.write(sign);
            bufOut.write(',');
            // vide
            bufOut.write(',');

            // EUR
            bufOut.write("EUR");

            bufOut.write("\r\n");
            bufOut.flush();
        }
    }
}