180 |
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.nio.charset.Charset;
|
|
|
27 |
import java.text.ParseException;
|
|
|
28 |
import java.text.SimpleDateFormat;
|
|
|
29 |
import java.util.Date;
|
|
|
30 |
import java.util.List;
|
|
|
31 |
|
|
|
32 |
import org.apache.commons.dbutils.handlers.ArrayListHandler;
|
|
|
33 |
|
|
|
34 |
public class ExportToQuadraCompta extends AbstractExport {
|
|
|
35 |
private static final Charset CHARSET = StringUtils.Cp1252;
|
|
|
36 |
private List<Object[]> data;
|
|
|
37 |
|
|
|
38 |
protected ExportToQuadraCompta(DBRoot rootSociete) {
|
|
|
39 |
super(rootSociete, "QuadraCompta", ".txt");
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
@Override
|
|
|
43 |
protected int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
|
|
|
44 |
final SQLTable tableEcriture = getEcritureT();
|
|
|
45 |
final SQLTable tableMouvement = tableEcriture.getForeignTable("ID_MOUVEMENT");
|
|
|
46 |
final SQLTable tableCompte = tableEcriture.getForeignTable("ID_COMPTE_PCE");
|
|
|
47 |
final SQLTable tableJrnl = tableEcriture.getForeignTable("ID_JOURNAL");
|
|
|
48 |
final SQLTable tablePiece = tableMouvement.getForeignTable("ID_PIECE");
|
|
|
49 |
|
|
|
50 |
final SQLSelect sel = createSelect(from, to, selectedJournal, onlyNew);
|
|
|
51 |
|
|
|
52 |
sel.addSelect(tableCompte.getField("NUMERO"));
|
|
|
53 |
sel.addSelect(tableJrnl.getField("CODE"));
|
|
|
54 |
sel.addSelect(tableEcriture.getField("DATE"));
|
|
|
55 |
sel.addSelect(tableEcriture.getField("NOM"));
|
|
|
56 |
sel.addSelect(tableEcriture.getField("DEBIT"));
|
|
|
57 |
sel.addSelect(tableEcriture.getField("CREDIT"));
|
|
|
58 |
sel.addSelect(tablePiece.getField("NOM"));
|
|
|
59 |
|
|
|
60 |
@SuppressWarnings("unchecked")
|
|
|
61 |
final List<Object[]> l = (List<Object[]>) this.getRootSociete().getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
|
|
|
62 |
this.data = l;
|
|
|
63 |
|
|
|
64 |
return l == null ? 0 : l.size();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
@Override
|
|
|
68 |
protected void export(OutputStream out) throws IOException, ParseException {
|
|
|
69 |
final Writer bufOut = new OutputStreamWriter(out, CHARSET);
|
|
|
70 |
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyy");
|
|
|
71 |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
72 |
|
|
|
73 |
for (final Object[] array : this.data) {
|
|
|
74 |
|
|
|
75 |
bufOut.write("M");
|
|
|
76 |
bufOut.write(matchStringToFixedLength(array[0],8,'0', true, false ));
|
|
|
77 |
bufOut.write(matchStringToFixedLength(array[1],2,' '));
|
|
|
78 |
bufOut.write("000");
|
|
|
79 |
|
|
|
80 |
Date date1 = simpleDateFormat.parse(String.valueOf(array[2]).trim());
|
|
|
81 |
bufOut.write(dateFormat.format(date1));
|
|
|
82 |
bufOut.write(" ");
|
|
|
83 |
bufOut.write(matchStringToFixedLength("",20,' '));
|
|
|
84 |
|
|
|
85 |
long debit = ((Number)array[4]).longValue();
|
|
|
86 |
long credit = ((Number)array[5]).longValue();
|
|
|
87 |
|
|
|
88 |
// Exception si débit et crédit > 0
|
|
|
89 |
bufOut.write(getDebitOrCredit(debit,credit));
|
|
|
90 |
bufOut.write(getAmountSign(getAmount(debit,credit)));
|
|
|
91 |
bufOut.write(matchStringToFixedLength(getAmount(debit,credit),12,'0', false, false));
|
|
|
92 |
bufOut.write(matchStringToFixedLength("",44,' '));
|
|
|
93 |
bufOut.write(matchStringToFixedLength(array[6],8,' ',false, true));
|
|
|
94 |
bufOut.write("EUR");
|
|
|
95 |
bufOut.write(matchStringToFixedLength(array[1],3,' '));
|
|
|
96 |
bufOut.write(matchStringToFixedLength(array[3],30,' ', false, false));
|
|
|
97 |
bufOut.write(matchStringToFixedLength("",12,' '));
|
|
|
98 |
|
|
|
99 |
bufOut.write("\r\n");
|
|
|
100 |
}
|
|
|
101 |
bufOut.flush();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Adapte une chaine à une longueur (champs à longueur fixe).
|
|
|
106 |
* @param : un objet que l'on va transformer en chaine.
|
|
|
107 |
* @param : une longueur.
|
|
|
108 |
* @param : un caractère pour le remplissage si la chaine est plus petite que la longueur fixée.
|
|
|
109 |
* @param : placement du remplissage, true = à droite.
|
|
|
110 |
* @param : le coté de la chaine à garder, true = à droite.
|
|
|
111 |
*
|
|
|
112 |
*/
|
|
|
113 |
private String matchStringToFixedLength(Object o, int length, char filler, boolean fillRight, boolean keepRight) {
|
|
|
114 |
String str = String.valueOf(o).trim();
|
|
|
115 |
|
|
|
116 |
if(str.length() >= length) {
|
|
|
117 |
if(keepRight) {
|
|
|
118 |
return str.substring(str.length()-length, str.length());
|
|
|
119 |
}else {
|
|
|
120 |
return str.substring(0,length);
|
|
|
121 |
}
|
|
|
122 |
}else {
|
|
|
123 |
String format;
|
|
|
124 |
if(fillRight) {
|
|
|
125 |
format = "%-"+length+"s";
|
|
|
126 |
}else {
|
|
|
127 |
format = "%"+length+"s";
|
|
|
128 |
}
|
|
|
129 |
return String.format(format, str).replace(' ',filler);
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
private String matchStringToFixedLength(Object o, int length, char filler) {
|
|
|
134 |
return matchStringToFixedLength(o, length, filler, true, true);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
private String getDebitOrCredit(final long debit, final long credit) {
|
|
|
138 |
if(debit > 0 && credit > 0) {
|
|
|
139 |
throw new IllegalStateException("Both credit and debit");
|
|
|
140 |
}else if(debit == 0) {
|
|
|
141 |
return "C";
|
|
|
142 |
}else {
|
|
|
143 |
return "D";
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private String getAmountSign(final long amount) {
|
|
|
148 |
if(amount < 0) {
|
|
|
149 |
return "-";
|
|
|
150 |
}else {
|
|
|
151 |
return "+";
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
private long getAmount(final long debit, final long credit) {
|
|
|
156 |
if(debit > 0) {
|
|
|
157 |
return debit;
|
|
|
158 |
}else {
|
|
|
159 |
return credit;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|