80 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
80 |
ilm |
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 |
|
174 |
ilm |
45 |
public static final char ZONE_SEPARATOR = '\t';
|
|
|
46 |
public static final char RECORD_SEPARATOR = '\n';
|
182 |
ilm |
47 |
|
174 |
ilm |
48 |
public static final List<String> COLS = Arrays.asList("JournalCode", "JournalLib", "EcritureNum", "EcritureDate", "CompteNum", "CompteLib", "CompAuxNum", "CompAuxLib", "PieceRef", "PieceDate",
|
80 |
ilm |
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) {
|
182 |
ilm |
102 |
final String formattedAmount = this.format.format(BigDecimal.valueOf(cents.longValue()).movePointLeft(2));
|
142 |
ilm |
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 |
}
|
182 |
ilm |
110 |
final String escapedString = strongEscape(s).trim();
|
|
|
111 |
line.add(escapedString);
|
80 |
ilm |
112 |
}
|
|
|
113 |
|
182 |
ilm |
114 |
private final String strongEscape(String str) {
|
|
|
115 |
if (str == null) {
|
|
|
116 |
return "";
|
|
|
117 |
}
|
|
|
118 |
final int length = str.length();
|
|
|
119 |
final StringBuilder b = new StringBuilder(length);
|
|
|
120 |
for (int i = 0; i < length; i++) {
|
|
|
121 |
final char c = str.charAt(i);
|
|
|
122 |
final char newChar;
|
|
|
123 |
if (c > 31 && c < 126 && c != ';') {
|
|
|
124 |
// ';' is escaped to avoid issue with Excel
|
|
|
125 |
newChar = c;
|
|
|
126 |
} else if (c == 'é' || c == 'è' || c == 'ê') {
|
|
|
127 |
newChar = 'e';
|
|
|
128 |
} else if (c == 'â' || c == 'à') {
|
|
|
129 |
newChar = 'a';
|
|
|
130 |
} else if (c == 'î') {
|
|
|
131 |
newChar = 'i';
|
|
|
132 |
} else if (c == 'ù' || c == 'û') {
|
|
|
133 |
newChar = 'u';
|
|
|
134 |
} else if (c == 'ô') {
|
|
|
135 |
newChar = 'o';
|
|
|
136 |
} else if (c == 'ç') {
|
|
|
137 |
newChar = 'c';
|
|
|
138 |
} else {
|
|
|
139 |
newChar = ' ';
|
|
|
140 |
}
|
|
|
141 |
b.append(newChar);
|
|
|
142 |
}
|
|
|
143 |
return b.toString();
|
|
|
144 |
}
|
|
|
145 |
|
80 |
ilm |
146 |
@Override
|
|
|
147 |
protected void export(OutputStream out) throws IOException {
|
142 |
ilm |
148 |
final Writer bufOut = new OutputStreamWriter(out, StringUtils.ISO8859_15);
|
80 |
ilm |
149 |
final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
150 |
final int fieldsCount = COLS.size();
|
|
|
151 |
|
|
|
152 |
for (final String colName : COLS) {
|
|
|
153 |
bufOut.write(colName);
|
142 |
ilm |
154 |
bufOut.write(ZONE_SEPARATOR);
|
80 |
ilm |
155 |
}
|
142 |
ilm |
156 |
bufOut.write(RECORD_SEPARATOR);
|
80 |
ilm |
157 |
|
|
|
158 |
final List<String> line = new ArrayList<String>(fieldsCount);
|
|
|
159 |
for (final Object[] array : this.data) {
|
|
|
160 |
line.clear();
|
|
|
161 |
|
|
|
162 |
// JournalCode
|
|
|
163 |
addField(line, (String) array[0]);
|
|
|
164 |
// JournalLib
|
|
|
165 |
addField(line, (String) array[1]);
|
|
|
166 |
// EcritureNum
|
|
|
167 |
addField(line, String.valueOf(array[2]));
|
|
|
168 |
// EcritureDate
|
|
|
169 |
final String ecritureDate = dateFormat.format(array[3]);
|
|
|
170 |
line.add(ecritureDate);
|
|
|
171 |
// CompteNum
|
142 |
ilm |
172 |
if (array[4] != null) {
|
|
|
173 |
addField(line, (String) array[4]);
|
|
|
174 |
} else {
|
|
|
175 |
bufOut.close();
|
|
|
176 |
JOptionPane.showMessageDialog(new JFrame(), "Une écriture n'a pas de numéro de compte :\n" + line, "Erreur FEC", JOptionPane.ERROR_MESSAGE);
|
|
|
177 |
return;
|
|
|
178 |
}
|
80 |
ilm |
179 |
// CompteLib
|
142 |
ilm |
180 |
if (array[5] != null) {
|
|
|
181 |
addField(line, (String) array[5]);
|
|
|
182 |
} else {
|
|
|
183 |
bufOut.close();
|
|
|
184 |
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);
|
|
|
185 |
return;
|
|
|
186 |
}
|
80 |
ilm |
187 |
// CompAuxNum
|
|
|
188 |
addEmptyField(line);
|
|
|
189 |
// CompAuxLib
|
|
|
190 |
addEmptyField(line);
|
|
|
191 |
// PieceRef
|
|
|
192 |
addField(line, (String) array[6]);
|
|
|
193 |
// PieceDate TODO ID_MOUVEMENT_PERE* ; SOURCE.DATE
|
|
|
194 |
line.add(ecritureDate);
|
|
|
195 |
// EcritureLib
|
132 |
ilm |
196 |
String s = (String) array[7];
|
|
|
197 |
if (s == null || s.trim().length() == 0) {
|
|
|
198 |
s = "Sans libellé";
|
|
|
199 |
}
|
|
|
200 |
addField(line, s);
|
80 |
ilm |
201 |
// Debit
|
|
|
202 |
addAmountField(line, (Number) array[8]);
|
|
|
203 |
// Credit
|
|
|
204 |
addAmountField(line, (Number) array[9]);
|
|
|
205 |
// EcritureLet
|
83 |
ilm |
206 |
addField(line, (String) array[11]);
|
|
|
207 |
|
80 |
ilm |
208 |
// DateLet
|
83 |
ilm |
209 |
if (array[10] != null) {
|
|
|
210 |
final String ecritureDateLettrage = dateFormat.format(array[10]);
|
|
|
211 |
line.add(ecritureDateLettrage);
|
|
|
212 |
} else {
|
|
|
213 |
line.add("");
|
|
|
214 |
}
|
80 |
ilm |
215 |
// ValidDate
|
83 |
ilm |
216 |
if (array[12] != null) {
|
|
|
217 |
final String ecritureDateValid = dateFormat.format(array[12]);
|
|
|
218 |
line.add(ecritureDateValid);
|
|
|
219 |
} else {
|
|
|
220 |
line.add("");
|
182 |
ilm |
221 |
if (this.cloture) {
|
132 |
ilm |
222 |
bufOut.close();
|
|
|
223 |
JOptionPane.showMessageDialog(new JFrame(), "Une écriture n'est pas validée (pas de date):\n" + line, "Erreur FEC", JOptionPane.ERROR_MESSAGE);
|
|
|
224 |
return;
|
|
|
225 |
}
|
83 |
ilm |
226 |
}
|
80 |
ilm |
227 |
// Montantdevise
|
90 |
ilm |
228 |
addAmountField(line, ((Number) array[8]).longValue() + ((Number) array[9]).longValue());
|
80 |
ilm |
229 |
// Idevise
|
90 |
ilm |
230 |
line.add("EUR");
|
80 |
ilm |
231 |
|
|
|
232 |
assert line.size() == fieldsCount;
|
|
|
233 |
for (int i = 0; i < fieldsCount; i++) {
|
|
|
234 |
final String zone = line.get(i);
|
|
|
235 |
// blank field
|
|
|
236 |
if (zone != null)
|
|
|
237 |
bufOut.write(zone);
|
142 |
ilm |
238 |
bufOut.write(ZONE_SEPARATOR);
|
80 |
ilm |
239 |
}
|
142 |
ilm |
240 |
bufOut.write(RECORD_SEPARATOR);
|
80 |
ilm |
241 |
}
|
83 |
ilm |
242 |
bufOut.close();
|
80 |
ilm |
243 |
}
|
|
|
244 |
}
|