OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Details | Compare with Previous | 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;
132 ilm 20
import org.openconcerto.sql.model.Where;
80 ilm 21
import org.openconcerto.utils.StringUtils;
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.text.DateFormat;
29
import java.text.DecimalFormat;
30
import java.text.DecimalFormatSymbols;
31
import java.text.SimpleDateFormat;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.Date;
35
import java.util.List;
36
import java.util.Locale;
37
 
93 ilm 38
import javax.swing.JFrame;
39
import javax.swing.JOptionPane;
40
 
80 ilm 41
import org.apache.commons.dbutils.handlers.ArrayListHandler;
42
 
43
public class ExportFEC extends AbstractExport {
44
 
142 ilm 45
    private static final char ZONE_SEPARATOR = '\t';
46
    private static final char RECORD_SEPARATOR = '\n';
47
    private static final char REPLACEMENT = ' ';
80 ilm 48
    static private final List<String> COLS = Arrays.asList("JournalCode", "JournalLib", "EcritureNum", "EcritureDate", "CompteNum", "CompteLib", "CompAuxNum", "CompAuxLib", "PieceRef", "PieceDate",
49
            "EcritureLib", "Debit", "Credit", "EcritureLet", "DateLet", "ValidDate", "Montantdevise", "Idevise");
50
 
142 ilm 51
    private final DecimalFormat format = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.FRANCE));
80 ilm 52
    private List<Object[]> data;
53
 
132 ilm 54
    private final boolean cloture;
55
 
56
    public ExportFEC(DBRoot rootSociete, boolean cloture) {
80 ilm 57
        super(rootSociete, "FEC", ".csv");
132 ilm 58
        this.cloture = cloture;
80 ilm 59
    }
60
 
61
    @Override
62
    protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
63
        final SQLTable tableEcriture = getEcritureT();
64
        final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
65
        final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
66
        final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
67
        final SQLTable tablePiece = tableMouvement.getForeignTable("ID_PIECE");
68
 
69
        final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
70
        sel.addSelect(tableJrnl.getField("CODE"));
71
        sel.addSelect(tableJrnl.getField("NOM"));
72
        sel.addSelect(tableMouvement.getField("NUMERO"));
73
        sel.addSelect(tableEcriture.getField("DATE"));
74
        sel.addSelect(tableCompte.getField("NUMERO"));
75
        sel.addSelect(tableCompte.getField("NOM"));
76
        sel.addSelect(tablePiece.getField("NOM"));
77
        // TODO ID_MOUVEMENT_PERE* ; SOURCE.DATE
78
        sel.addSelect(tableEcriture.getField("NOM"));
79
        sel.addSelect(tableEcriture.getField("DEBIT"));
80
        sel.addSelect(tableEcriture.getField("CREDIT"));
83 ilm 81
        sel.addSelect(tableEcriture.getField("DATE_LETTRAGE"));
82
        sel.addSelect(tableEcriture.getField("LETTRAGE"));
83
        sel.addSelect(tableEcriture.getField("DATE_VALIDE"));
80 ilm 84
 
85
        sel.addFieldOrder(tableEcriture.getField("DATE"));
86
        sel.addFieldOrder(tableMouvement.getField("NUMERO"));
156 ilm 87
        final Where w = new Where(tableEcriture.getField("DEBIT"), "!=", tableEcriture.getField("CREDIT"));
88
        final Where w2 = new Where(tableEcriture.getField("CLOTURE"), "!=", Boolean.TRUE);
89
        sel.setWhere(sel.getWhere().and(w).and(w2));
80 ilm 90
 
91
        @SuppressWarnings("unchecked")
92
        final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
93
        this.data = l;
94
        return l == null ? 0 : l.size();
95
    }
96
 
97
    private final void addEmptyField(final List<String> line) {
98
        line.add(null);
99
    }
100
 
101
    private final void addAmountField(final List<String> line, final Number cents) {
142 ilm 102
        final String formattedAmount = format.format(BigDecimal.valueOf(cents.longValue()).movePointLeft(2));
103
        line.add(formattedAmount);
80 ilm 104
    }
105
 
106
    private final void addField(final List<String> line, final String s) {
142 ilm 107
        if (s == null) {
108
            throw new NullPointerException("Valeur manquante pour remplir la ligne : " + line);
109
        }
156 ilm 110
        final String escapedString = StringUtils.toAsciiString(s).trim();
111
        line.add(escapedString.replace(ZONE_SEPARATOR, REPLACEMENT).replace(RECORD_SEPARATOR, REPLACEMENT));
80 ilm 112
    }
113
 
114
    @Override
115
    protected void export(OutputStream out) throws IOException {
142 ilm 116
        final Writer bufOut = new OutputStreamWriter(out, StringUtils.ISO8859_15);
80 ilm 117
        final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
118
        final int fieldsCount = COLS.size();
119
 
120
        for (final String colName : COLS) {
121
            bufOut.write(colName);
142 ilm 122
            bufOut.write(ZONE_SEPARATOR);
80 ilm 123
        }
142 ilm 124
        bufOut.write(RECORD_SEPARATOR);
80 ilm 125
 
126
        final List<String> line = new ArrayList<String>(fieldsCount);
127
        for (final Object[] array : this.data) {
128
            line.clear();
129
 
130
            // JournalCode
131
            addField(line, (String) array[0]);
132
            // JournalLib
133
            addField(line, (String) array[1]);
134
            // EcritureNum
135
            addField(line, String.valueOf(array[2]));
136
            // EcritureDate
137
            final String ecritureDate = dateFormat.format(array[3]);
138
            line.add(ecritureDate);
139
            // CompteNum
142 ilm 140
            if (array[4] != null) {
141
                addField(line, (String) array[4]);
142
            } else {
143
                bufOut.close();
144
                JOptionPane.showMessageDialog(new JFrame(), "Une écriture n'a pas de numéro de compte :\n" + line, "Erreur FEC", JOptionPane.ERROR_MESSAGE);
145
                return;
146
            }
80 ilm 147
            // CompteLib
142 ilm 148
            if (array[5] != null) {
149
                addField(line, (String) array[5]);
150
            } else {
151
                bufOut.close();
152
                JOptionPane.showMessageDialog(new JFrame(), "Une écriture n'a pas de libellé de compte pour le compte " + array[4].toString() + " :\n" + line, "Erreur FEC", JOptionPane.ERROR_MESSAGE);
153
                return;
154
            }
80 ilm 155
            // CompAuxNum
156
            addEmptyField(line);
157
            // CompAuxLib
158
            addEmptyField(line);
159
            // PieceRef
160
            addField(line, (String) array[6]);
161
            // PieceDate TODO ID_MOUVEMENT_PERE* ; SOURCE.DATE
162
            line.add(ecritureDate);
163
            // EcritureLib
132 ilm 164
            String s = (String) array[7];
165
            if (s == null || s.trim().length() == 0) {
166
                s = "Sans libellé";
167
            }
168
            addField(line, s);
80 ilm 169
            // Debit
170
            addAmountField(line, (Number) array[8]);
171
            // Credit
172
            addAmountField(line, (Number) array[9]);
173
            // EcritureLet
83 ilm 174
            addField(line, (String) array[11]);
175
 
80 ilm 176
            // DateLet
83 ilm 177
            if (array[10] != null) {
178
                final String ecritureDateLettrage = dateFormat.format(array[10]);
179
                line.add(ecritureDateLettrage);
180
            } else {
181
                line.add("");
182
            }
80 ilm 183
            // ValidDate
83 ilm 184
            if (array[12] != null) {
185
                final String ecritureDateValid = dateFormat.format(array[12]);
186
                line.add(ecritureDateValid);
187
            } else {
188
                line.add("");
132 ilm 189
                if (cloture) {
190
                    bufOut.close();
191
                    JOptionPane.showMessageDialog(new JFrame(), "Une écriture n'est pas validée (pas de date):\n" + line, "Erreur FEC", JOptionPane.ERROR_MESSAGE);
192
                    return;
193
                }
83 ilm 194
            }
80 ilm 195
            // Montantdevise
90 ilm 196
            addAmountField(line, ((Number) array[8]).longValue() + ((Number) array[9]).longValue());
80 ilm 197
            // Idevise
90 ilm 198
            line.add("EUR");
80 ilm 199
 
200
            assert line.size() == fieldsCount;
201
            for (int i = 0; i < fieldsCount; i++) {
202
                final String zone = line.get(i);
203
                // blank field
204
                if (zone != null)
205
                    bufOut.write(zone);
142 ilm 206
                bufOut.write(ZONE_SEPARATOR);
80 ilm 207
            }
142 ilm 208
            bufOut.write(RECORD_SEPARATOR);
80 ilm 209
        }
83 ilm 210
        bufOut.close();
80 ilm 211
    }
212
}