OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
177 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
 
22
import java.io.IOException;
23
import java.io.OutputStream;
24
import java.io.OutputStreamWriter;
25
import java.io.Writer;
26
import java.math.BigDecimal;
27
import java.text.DateFormat;
28
import java.text.DecimalFormat;
29
import java.text.DecimalFormatSymbols;
30
import java.text.SimpleDateFormat;
31
import java.util.Date;
32
import java.util.List;
33
import java.util.Locale;
34
 
35
import org.apache.commons.dbutils.handlers.ArrayListHandler;
36
 
37
public class ExportEBPPivot extends AbstractExport {
38
 
39
    // Format pseudo CSV
40
    // ###EBPPivotV1
41
    // 1,150116,AC,401,,"SOGEAB INTERC","0001",1884.03,C,150116,EUR
42
    // 2,150116,AC,6222,,"SOGEAB INTERC","0001",1570.03,D,,EUR
43
    // 3,150116,AC,4456614,,"SOGEAB INTERC","0001",314.00,D,,EUR
44
 
45
    // numéro d'écriture
46
    // date
47
    // journal
48
    // numero de compte
49
    // vide
50
    // label ecriture
51
    // label piece
52
    // montant xxxxx.xx
53
    // D si débit, C si crédit
54
    // date ou ""
55
    // EUR
56
 
57
    // Note : Ascii pur (ne pas exporter d'accents), fin de ligne: \r\n
58
 
59
    private final DecimalFormat decimalFormat = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.UK));
60
 
61
    private String formatCents(final Number n) {
62
        return this.decimalFormat.format(BigDecimal.valueOf(n.longValue()).movePointLeft(2));
63
    }
64
 
65
    private List<Object[]> data;
66
 
67
    public ExportEBPPivot(DBRoot rootSociete) {
68
        super(rootSociete, "EBP_Pivot", ".txt");
69
    }
70
 
71
    @Override
72
    protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
73
        final SQLTable tableEcriture = getEcritureT();
74
        final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
75
        final SQLTable tablePiece = tableMouvement.getForeignTable("ID_PIECE");
76
        final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
77
        final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
78
 
79
        final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
80
        sel.addSelect(tableJrnl.getField("CODE"));
81
        sel.addSelect(tableJrnl.getField("NOM"));
82
        sel.addSelect(tableEcriture.getField("ID"));
83
        sel.addSelect(tableEcriture.getField("DATE"));
84
        sel.addSelect(tableCompte.getField("NUMERO"));
85
        sel.addSelect(tableCompte.getField("NOM"));
86
        sel.addSelect(tableMouvement.getField("NUMERO"));
87
        sel.addSelect(tableEcriture.getField("NOM"));
88
        sel.addSelect(tableEcriture.getField("DEBIT"));
89
        sel.addSelect(tableEcriture.getField("CREDIT"));
90
        sel.addSelect(tablePiece.getField("NOM"));
91
        sel.addFieldOrder(tableJrnl.getField("CODE"));
92
        sel.addFieldOrder(tableEcriture.getField("DATE"));
93
        sel.addFieldOrder(tableMouvement.getField("NUMERO"));
94
 
95
        @SuppressWarnings("unchecked")
96
        final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
97
        this.data = l;
98
        return l == null ? 0 : l.size();
99
    }
100
 
101
    @Override
102
    protected void export(OutputStream out) throws IOException {
103
        final Writer bufOut = new OutputStreamWriter(out, StringUtils.ASCII);
104
        final DateFormat dateFormat = new SimpleDateFormat("ddMMyy");
105
        bufOut.write("###EBPPivotV1\r\n");
106
        for (final Object[] array : this.data) {
107
            // numéro d'écriture
108
            bufOut.write(String.valueOf(array[2]));
109
            bufOut.write(',');
110
            // date
111
            bufOut.write(dateFormat.format((Date) array[3]));
112
            bufOut.write(',');
113
            // journal
114
            String codeJournal = String.valueOf(array[0]);
115
            bufOut.write(codeJournal);
116
            bufOut.write(',');
117
            // numero de compte
118
            String numeroCompte = String.valueOf(array[4]);
119
            bufOut.write(StringUtils.toAsciiString(numeroCompte));
120
            bufOut.write(',');
121
            // vide
122
            bufOut.write(',');
123
            // label ecriture
124
            String libelleEcriture = String.valueOf(array[7]);
125
            bufOut.write('\"');
126
            bufOut.write(StringUtils.toAsciiString(libelleEcriture));
127
            bufOut.write('\"');
128
            bufOut.write(',');
129
            // label piece
130
            String libellePiece = String.valueOf(array[10]);
131
            bufOut.write('\"');
132
            bufOut.write(StringUtils.toAsciiString(libellePiece));
133
            bufOut.write('\"');
134
            bufOut.write(',');
135
            // montant xxxxx.xx
136
            final long debit = ((Number) array[8]).longValue();
137
            final long credit = ((Number) array[9]).longValue();
138
            if (debit > 0 && credit > 0)
139
                throw new IllegalStateException("debit et credit >0");
140
            final long cents;
141
            final char sign;
142
            if (credit > 0) {
143
                cents = credit;
144
                sign = 'C';
145
            } else {
146
                cents = debit;
147
                sign = 'D';
148
            }
149
            bufOut.write(formatCents(cents));
150
            bufOut.write(',');
151
 
152
            // D si débit, C si crédit
153
            bufOut.write(sign);
154
            bufOut.write(',');
155
            // vide
156
            bufOut.write(',');
157
 
158
            // EUR
159
            bufOut.write("EUR");
160
 
161
            bufOut.write("\r\n");
162
            bufOut.flush();
163
        }
164
    }
165
}