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 |
|
|
|
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.nio.charset.Charset;
|
|
|
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 |
static private final Charset CHARSET = StringUtils.ISO8859_15;
|
|
|
45 |
static private final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.FRANCE));
|
|
|
46 |
|
|
|
47 |
static private final List<String> COLS = Arrays.asList("JournalCode", "JournalLib", "EcritureNum", "EcritureDate", "CompteNum", "CompteLib", "CompAuxNum", "CompAuxLib", "PieceRef", "PieceDate",
|
|
|
48 |
"EcritureLib", "Debit", "Credit", "EcritureLet", "DateLet", "ValidDate", "Montantdevise", "Idevise");
|
|
|
49 |
|
|
|
50 |
static private String formatCents(final Number n) {
|
|
|
51 |
return DECIMAL_FORMAT.format(BigDecimal.valueOf(n.longValue()).movePointLeft(2));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private List<Object[]> data;
|
|
|
55 |
private final char zoneSep = '\t';
|
|
|
56 |
private final char recordSep = '\n';
|
|
|
57 |
private final char replacement = ' ';
|
|
|
58 |
|
|
|
59 |
public ExportFEC(DBRoot rootSociete) {
|
|
|
60 |
super(rootSociete, "FEC", ".csv");
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
@Override
|
|
|
64 |
protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
|
|
|
65 |
final SQLTable tableEcriture = getEcritureT();
|
|
|
66 |
final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
|
|
|
67 |
final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
|
|
|
68 |
final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
|
|
|
69 |
final SQLTable tablePiece = tableMouvement.getForeignTable("ID_PIECE");
|
|
|
70 |
|
|
|
71 |
final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
|
|
|
72 |
sel.addSelect(tableJrnl.getField("CODE"));
|
|
|
73 |
sel.addSelect(tableJrnl.getField("NOM"));
|
|
|
74 |
sel.addSelect(tableMouvement.getField("NUMERO"));
|
|
|
75 |
sel.addSelect(tableEcriture.getField("DATE"));
|
|
|
76 |
sel.addSelect(tableCompte.getField("NUMERO"));
|
|
|
77 |
sel.addSelect(tableCompte.getField("NOM"));
|
|
|
78 |
sel.addSelect(tablePiece.getField("NOM"));
|
|
|
79 |
// TODO ID_MOUVEMENT_PERE* ; SOURCE.DATE
|
|
|
80 |
sel.addSelect(tableEcriture.getField("NOM"));
|
|
|
81 |
sel.addSelect(tableEcriture.getField("DEBIT"));
|
|
|
82 |
sel.addSelect(tableEcriture.getField("CREDIT"));
|
83 |
ilm |
83 |
sel.addSelect(tableEcriture.getField("DATE_LETTRAGE"));
|
|
|
84 |
sel.addSelect(tableEcriture.getField("LETTRAGE"));
|
|
|
85 |
sel.addSelect(tableEcriture.getField("DATE_VALIDE"));
|
80 |
ilm |
86 |
|
|
|
87 |
sel.addFieldOrder(tableEcriture.getField("DATE"));
|
|
|
88 |
sel.addFieldOrder(tableMouvement.getField("NUMERO"));
|
|
|
89 |
|
|
|
90 |
@SuppressWarnings("unchecked")
|
|
|
91 |
final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
|
|
|
92 |
this.data = l;
|
|
|
93 |
return l == null ? 0 : l.size();
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private final void addEmptyField(final List<String> line) {
|
|
|
97 |
line.add(null);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
private final void addAmountField(final List<String> line, final Number cents) {
|
|
|
101 |
line.add(formatCents(cents));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
private final void addField(final List<String> line, final String s) {
|
|
|
105 |
if (this.zoneSep == this.replacement || this.recordSep == this.replacement)
|
|
|
106 |
throw new IllegalStateException("Wrong separators");
|
|
|
107 |
// TODO remove \r
|
|
|
108 |
line.add(s.trim().replace(this.zoneSep, this.replacement).replace(this.recordSep, this.replacement));
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
@Override
|
|
|
112 |
protected void export(OutputStream out) throws IOException {
|
|
|
113 |
final Writer bufOut = new OutputStreamWriter(out, CHARSET);
|
|
|
114 |
final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
115 |
final int fieldsCount = COLS.size();
|
|
|
116 |
|
|
|
117 |
for (final String colName : COLS) {
|
|
|
118 |
bufOut.write(colName);
|
|
|
119 |
bufOut.write(this.zoneSep);
|
|
|
120 |
}
|
|
|
121 |
bufOut.write(this.recordSep);
|
|
|
122 |
|
|
|
123 |
final List<String> line = new ArrayList<String>(fieldsCount);
|
|
|
124 |
for (final Object[] array : this.data) {
|
|
|
125 |
line.clear();
|
|
|
126 |
|
|
|
127 |
// JournalCode
|
|
|
128 |
addField(line, (String) array[0]);
|
|
|
129 |
// JournalLib
|
|
|
130 |
addField(line, (String) array[1]);
|
|
|
131 |
// EcritureNum
|
|
|
132 |
addField(line, String.valueOf(array[2]));
|
|
|
133 |
// EcritureDate
|
|
|
134 |
final String ecritureDate = dateFormat.format(array[3]);
|
|
|
135 |
line.add(ecritureDate);
|
|
|
136 |
// CompteNum
|
|
|
137 |
addField(line, (String) array[4]);
|
|
|
138 |
// CompteLib
|
|
|
139 |
addField(line, (String) array[5]);
|
|
|
140 |
// CompAuxNum
|
|
|
141 |
addEmptyField(line);
|
|
|
142 |
// CompAuxLib
|
|
|
143 |
addEmptyField(line);
|
|
|
144 |
// PieceRef
|
|
|
145 |
addField(line, (String) array[6]);
|
|
|
146 |
// PieceDate TODO ID_MOUVEMENT_PERE* ; SOURCE.DATE
|
|
|
147 |
line.add(ecritureDate);
|
|
|
148 |
// EcritureLib
|
|
|
149 |
addField(line, (String) array[7]);
|
|
|
150 |
// Debit
|
|
|
151 |
addAmountField(line, (Number) array[8]);
|
|
|
152 |
// Credit
|
|
|
153 |
addAmountField(line, (Number) array[9]);
|
|
|
154 |
// EcritureLet
|
83 |
ilm |
155 |
addField(line, (String) array[11]);
|
|
|
156 |
|
80 |
ilm |
157 |
// DateLet
|
83 |
ilm |
158 |
if (array[10] != null) {
|
|
|
159 |
final String ecritureDateLettrage = dateFormat.format(array[10]);
|
|
|
160 |
line.add(ecritureDateLettrage);
|
|
|
161 |
} else {
|
|
|
162 |
line.add("");
|
|
|
163 |
}
|
80 |
ilm |
164 |
// ValidDate
|
83 |
ilm |
165 |
if (array[12] != null) {
|
|
|
166 |
final String ecritureDateValid = dateFormat.format(array[12]);
|
|
|
167 |
line.add(ecritureDateValid);
|
|
|
168 |
} else {
|
|
|
169 |
line.add("");
|
93 |
ilm |
170 |
bufOut.close();
|
|
|
171 |
JOptionPane.showMessageDialog(new JFrame(), "Erreur", "Une écriture n'est pas validée (pas de date):\n" + line, JOptionPane.ERROR_MESSAGE);
|
83 |
ilm |
172 |
}
|
80 |
ilm |
173 |
// Montantdevise
|
90 |
ilm |
174 |
addAmountField(line, ((Number) array[8]).longValue() + ((Number) array[9]).longValue());
|
80 |
ilm |
175 |
// Idevise
|
90 |
ilm |
176 |
line.add("EUR");
|
80 |
ilm |
177 |
|
|
|
178 |
assert line.size() == fieldsCount;
|
|
|
179 |
for (int i = 0; i < fieldsCount; i++) {
|
|
|
180 |
final String zone = line.get(i);
|
|
|
181 |
// blank field
|
|
|
182 |
if (zone != null)
|
|
|
183 |
bufOut.write(zone);
|
93 |
ilm |
184 |
bufOut.write(this.zoneSep);
|
80 |
ilm |
185 |
}
|
|
|
186 |
bufOut.write(this.recordSep);
|
|
|
187 |
}
|
83 |
ilm |
188 |
bufOut.close();
|
80 |
ilm |
189 |
}
|
|
|
190 |
}
|