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 = " ";
|
132 |
ilm |
44 |
// Format : largeur fixe, separateur : 0x0E (Shift Out)
|
|
|
45 |
// l: 6 : espaces...
|
|
|
46 |
// l: 8 : code du journal, aligné à gauche
|
|
|
47 |
// l: 40 : nom du journal, aligné à gauche
|
|
|
48 |
// l: 8 : date de l'écriture, AAAAMMJJ
|
|
|
49 |
// l: 15 : numéro du compte, aligné à gauche
|
|
|
50 |
// l: 60 : nom du compte, aligné à gauche
|
|
|
51 |
// l: 15 : numéro de l'écriture, aligné à gauche
|
|
|
52 |
// l: 60 : nom de l'écriture, aligné à gauche
|
|
|
53 |
// l: 14 : montant, aligné à droite, premier caractère : le signe
|
80 |
ilm |
54 |
// needs . for decimal separator
|
|
|
55 |
static private final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.UK));
|
|
|
56 |
|
|
|
57 |
static private String formatCents(final Number n) {
|
|
|
58 |
return DECIMAL_FORMAT.format(BigDecimal.valueOf(n.longValue()).movePointLeft(2));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
private List<Object[]> data;
|
|
|
62 |
|
|
|
63 |
public ExportEBP_ComptaPro(DBRoot rootSociete) {
|
|
|
64 |
super(rootSociete, "EBPPro", ".txt");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
@Override
|
|
|
68 |
protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
|
|
|
69 |
final SQLTable tableEcriture = getEcritureT();
|
|
|
70 |
final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
|
|
|
71 |
final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
|
|
|
72 |
final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
|
|
|
73 |
|
|
|
74 |
final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
|
|
|
75 |
sel.addSelect(tableJrnl.getField("CODE"));
|
|
|
76 |
sel.addSelect(tableJrnl.getField("NOM"));
|
|
|
77 |
sel.addSelect(tableEcriture.getField("DATE"));
|
|
|
78 |
sel.addSelect(tableCompte.getField("NUMERO"));
|
|
|
79 |
sel.addSelect(tableCompte.getField("NOM"));
|
|
|
80 |
sel.addSelect(tableMouvement.getField("NUMERO"));
|
|
|
81 |
sel.addSelect(tableEcriture.getField("NOM"));
|
|
|
82 |
sel.addSelect(tableEcriture.getField("DEBIT"));
|
|
|
83 |
sel.addSelect(tableEcriture.getField("CREDIT"));
|
|
|
84 |
sel.addFieldOrder(tableJrnl.getField("CODE"));
|
|
|
85 |
sel.addFieldOrder(tableEcriture.getField("DATE"));
|
|
|
86 |
sel.addFieldOrder(tableMouvement.getField("NUMERO"));
|
|
|
87 |
|
|
|
88 |
@SuppressWarnings("unchecked")
|
|
|
89 |
final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
|
|
|
90 |
this.data = l;
|
|
|
91 |
return l == null ? 0 : l.size();
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private final String align(final Object o, final int widthIndex) {
|
|
|
95 |
return align(o, widthIndex, false);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private final String align(final Object o, final int widthIndex, final boolean allowTruncate) {
|
132 |
ilm |
99 |
String s = String.valueOf(o).trim();
|
|
|
100 |
final int width = WIDTHS[widthIndex];
|
|
|
101 |
if (s.length() > width) {
|
|
|
102 |
s = s.substring(0, width);
|
|
|
103 |
}
|
|
|
104 |
return StringUtils.getFixedWidthString(s, width, Side.LEFT, false);
|
80 |
ilm |
105 |
}
|
|
|
106 |
|
|
|
107 |
@Override
|
|
|
108 |
protected void export(OutputStream out) throws IOException {
|
|
|
109 |
final Writer bufOut = new OutputStreamWriter(out, CHARSET);
|
|
|
110 |
final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
111 |
final String firstField = SPACES.substring(0, WIDTHS[0]);
|
|
|
112 |
for (final Object[] array : this.data) {
|
|
|
113 |
int fieldIndex = 0;
|
132 |
ilm |
114 |
// ESPACES...
|
80 |
ilm |
115 |
bufOut.write(align(firstField, fieldIndex++));
|
|
|
116 |
bufOut.write(SEP);
|
132 |
ilm |
117 |
// JOURNAL.CODE
|
80 |
ilm |
118 |
bufOut.write(align(array[0], fieldIndex++));
|
|
|
119 |
bufOut.write(SEP);
|
|
|
120 |
// JOURNAL.NOM
|
|
|
121 |
bufOut.write(align(array[1], fieldIndex++, true));
|
|
|
122 |
bufOut.write(SEP);
|
132 |
ilm |
123 |
// ECRITURE.DATE
|
80 |
ilm |
124 |
bufOut.write(align(dateFormat.format(array[2]), fieldIndex++));
|
|
|
125 |
bufOut.write(SEP);
|
132 |
ilm |
126 |
// COMPTE_PCE.NUMERO
|
80 |
ilm |
127 |
bufOut.write(align(array[3], fieldIndex++));
|
|
|
128 |
bufOut.write(SEP);
|
|
|
129 |
// COMPTE_PCE.NOM
|
|
|
130 |
bufOut.write(align(array[4], fieldIndex++, true));
|
|
|
131 |
bufOut.write(SEP);
|
132 |
ilm |
132 |
// MOUVEMENT.NUMERO
|
80 |
ilm |
133 |
bufOut.write(align(array[5], fieldIndex++));
|
|
|
134 |
bufOut.write(SEP);
|
|
|
135 |
// ECRITURE.NOM
|
|
|
136 |
bufOut.write(align(array[6], fieldIndex++, true));
|
|
|
137 |
bufOut.write(SEP);
|
|
|
138 |
|
132 |
ilm |
139 |
// Montant
|
80 |
ilm |
140 |
final long debit = ((Number) array[7]).longValue();
|
|
|
141 |
final long credit = ((Number) array[8]).longValue();
|
|
|
142 |
if (debit > 0 && credit > 0)
|
|
|
143 |
throw new IllegalStateException("Both credit and debit");
|
|
|
144 |
final long cents;
|
|
|
145 |
final char sign;
|
|
|
146 |
if (credit > 0) {
|
|
|
147 |
cents = credit;
|
|
|
148 |
sign = '-';
|
|
|
149 |
} else {
|
|
|
150 |
cents = debit;
|
|
|
151 |
sign = ' ';
|
|
|
152 |
}
|
|
|
153 |
bufOut.write(sign);
|
|
|
154 |
// -1 since we wrote the sign
|
132 |
ilm |
155 |
final int wAmount = WIDTHS[fieldIndex++] - 1;
|
|
|
156 |
String amount = formatCents(cents);
|
|
|
157 |
if (amount.length() > wAmount) {
|
|
|
158 |
amount = amount.substring(0, wAmount);
|
|
|
159 |
}
|
|
|
160 |
bufOut.write(StringUtils.getFixedWidthString(amount, wAmount, Side.RIGHT, false));
|
80 |
ilm |
161 |
bufOut.write("\r\n");
|
|
|
162 |
}
|
180 |
ilm |
163 |
bufOut.flush();
|
80 |
ilm |
164 |
}
|
|
|
165 |
}
|