OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.panel.compta;
15
 
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLSelect;
19
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.utils.StringUtils;
21
import org.openconcerto.utils.StringUtils.Side;
22
 
23
import java.io.IOException;
24
import java.io.OutputStream;
25
import java.io.OutputStreamWriter;
26
import java.io.Writer;
27
import java.math.BigDecimal;
28
import java.nio.charset.Charset;
29
import java.text.DateFormat;
30
import java.text.DecimalFormat;
31
import java.text.DecimalFormatSymbols;
32
import java.text.SimpleDateFormat;
33
import java.util.Date;
34
import java.util.List;
35
import java.util.Locale;
36
 
37
import org.apache.commons.dbutils.handlers.ArrayListHandler;
38
 
39
public class ExportEBP_ComptaPro extends AbstractExport {
40
    static private final Charset CHARSET = StringUtils.Cp1252;
41
    static private final char SEP = 0x0E;
42
    static private final int[] WIDTHS = new int[] { 6, 8, 40, 8, 15, 60, 15, 60, 14 };
43
    static private final String SPACES = "                                                       ";
44
 
45
    // needs . for decimal separator
46
    static private final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.UK));
47
 
48
    static private String formatCents(final Number n) {
49
        return DECIMAL_FORMAT.format(BigDecimal.valueOf(n.longValue()).movePointLeft(2));
50
    }
51
 
52
    private List<Object[]> data;
53
 
54
    public ExportEBP_ComptaPro(DBRoot rootSociete) {
55
        super(rootSociete, "EBPPro", ".txt");
56
    }
57
 
58
    @Override
59
    protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
60
        final SQLTable tableEcriture = getEcritureT();
61
        final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
62
        final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
63
        final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
64
 
65
        final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
66
        sel.addSelect(tableJrnl.getField("CODE"));
67
        sel.addSelect(tableJrnl.getField("NOM"));
68
        sel.addSelect(tableEcriture.getField("DATE"));
69
        sel.addSelect(tableCompte.getField("NUMERO"));
70
        sel.addSelect(tableCompte.getField("NOM"));
71
        // width wider than 15, so replace with MOUVEMENT
72
        // sel.addSelect(tablePiece.getField("NOM"));
73
        sel.addSelect(tableMouvement.getField("NUMERO"));
74
        sel.addSelect(tableEcriture.getField("NOM"));
75
        sel.addSelect(tableEcriture.getField("DEBIT"));
76
        sel.addSelect(tableEcriture.getField("CREDIT"));
77
 
78
        sel.addFieldOrder(tableJrnl.getField("CODE"));
79
        sel.addFieldOrder(tableEcriture.getField("DATE"));
80
        sel.addFieldOrder(tableMouvement.getField("NUMERO"));
81
 
82
        @SuppressWarnings("unchecked")
83
        final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
84
        this.data = l;
85
        return l == null ? 0 : l.size();
86
    }
87
 
88
    private final String align(final Object o, final int widthIndex) {
89
        return align(o, widthIndex, false);
90
    }
91
 
92
    private final String align(final Object o, final int widthIndex, final boolean allowTruncate) {
93
        final String s = String.valueOf(o).trim();
94
        return StringUtils.getFixedWidthString(s, WIDTHS[widthIndex], Side.LEFT);
95
    }
96
 
97
    @Override
98
    protected void export(OutputStream out) throws IOException {
99
        final Writer bufOut = new OutputStreamWriter(out, CHARSET);
100
        final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
101
        final String firstField = SPACES.substring(0, WIDTHS[0]);
102
        for (final Object[] array : this.data) {
103
            int fieldIndex = 0;
104
            bufOut.write(align(firstField, fieldIndex++));
105
            bufOut.write(SEP);
106
            bufOut.write(align(array[0], fieldIndex++));
107
            bufOut.write(SEP);
108
            // JOURNAL.NOM
109
            bufOut.write(align(array[1], fieldIndex++, true));
110
            bufOut.write(SEP);
111
            bufOut.write(align(dateFormat.format(array[2]), fieldIndex++));
112
            bufOut.write(SEP);
113
            bufOut.write(align(array[3], fieldIndex++));
114
            bufOut.write(SEP);
115
            // COMPTE_PCE.NOM
116
            bufOut.write(align(array[4], fieldIndex++, true));
117
            bufOut.write(SEP);
118
            System.err.println("ExportEBP_Pro.export() " + array[5]);
119
            bufOut.write(align(array[5], fieldIndex++));
120
            bufOut.write(SEP);
121
            // ECRITURE.NOM
122
            bufOut.write(align(array[6], fieldIndex++, true));
123
            bufOut.write(SEP);
124
 
125
            // Amount
126
            final long debit = ((Number) array[7]).longValue();
127
            final long credit = ((Number) array[8]).longValue();
128
            if (debit > 0 && credit > 0)
129
                throw new IllegalStateException("Both credit and debit");
130
            final long cents;
131
            final char sign;
132
            if (credit > 0) {
133
                cents = credit;
134
                sign = '-';
135
            } else {
136
                cents = debit;
137
                sign = ' ';
138
            }
139
            bufOut.write(sign);
140
            // -1 since we wrote the sign
141
            bufOut.write(StringUtils.getFixedWidthString(formatCents(cents), WIDTHS[fieldIndex++] - 1, Side.RIGHT, false));
142
 
143
            bufOut.write("\r\n");
144
        }
145
    }
146
}