174 |
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.erp.generationEcritures.Ecriture;
|
|
|
17 |
import org.openconcerto.erp.generationEcritures.Exercice;
|
|
|
18 |
import org.openconcerto.erp.generationEcritures.Mouvement;
|
|
|
19 |
import org.openconcerto.erp.generationEcritures.Piece;
|
|
|
20 |
import org.openconcerto.sql.element.SQLElementDirectory;
|
|
|
21 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
22 |
import org.openconcerto.sql.users.User;
|
|
|
23 |
import org.openconcerto.utils.StringUtils;
|
|
|
24 |
|
|
|
25 |
import java.io.BufferedReader;
|
|
|
26 |
import java.io.File;
|
|
|
27 |
import java.io.FileInputStream;
|
|
|
28 |
import java.io.IOException;
|
|
|
29 |
import java.io.InputStreamReader;
|
|
|
30 |
import java.math.BigDecimal;
|
|
|
31 |
import java.nio.charset.Charset;
|
|
|
32 |
import java.sql.SQLException;
|
|
|
33 |
import java.util.ArrayList;
|
|
|
34 |
import java.util.Calendar;
|
|
|
35 |
import java.util.Date;
|
|
|
36 |
import java.util.HashMap;
|
|
|
37 |
import java.util.List;
|
|
|
38 |
import java.util.Map;
|
|
|
39 |
|
|
|
40 |
public class ImportRImport {
|
|
|
41 |
private String error = null;
|
|
|
42 |
private final Map<String, Piece> mapPiece = new HashMap<>();
|
|
|
43 |
private final Map<String, String> mapJournal = new HashMap<>();
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Import format Ciel.
|
|
|
47 |
*
|
|
|
48 |
* Une pièce par journal
|
|
|
49 |
*
|
|
|
50 |
*/
|
|
|
51 |
public ImportRImport() {
|
|
|
52 |
//
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public void loadFrom(File file) throws IOException {
|
|
|
56 |
this.mapPiece.clear();
|
|
|
57 |
this.mapJournal.clear();
|
|
|
58 |
this.error = null;
|
|
|
59 |
parseJournaux(file);
|
|
|
60 |
try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
|
|
|
61 |
String line = bReader.readLine();
|
|
|
62 |
while (!(line.startsWith("##Section") && line.contains("Mvt"))) {
|
|
|
63 |
line = bReader.readLine();
|
|
|
64 |
}
|
|
|
65 |
line = bReader.readLine();
|
|
|
66 |
while (line != null && !line.contains("#Section")) {
|
|
|
67 |
final List<String> parts = StringUtils.fastSplit(line, '\t');
|
|
|
68 |
final String journalCode = unquote(parts.get(1).trim());
|
|
|
69 |
final Date ecritureDate = parseDate(unquote(parts.get(2).trim()));
|
|
|
70 |
final String compteNum = unquote(parts.get(3).trim());
|
|
|
71 |
final String compteLib = unquote(parts.get(4).trim());
|
|
|
72 |
final BigDecimal m = new BigDecimal(cleanMontant(parts.get(5)));
|
|
|
73 |
BigDecimal debit = BigDecimal.ZERO;
|
|
|
74 |
BigDecimal credit = BigDecimal.ZERO;
|
|
|
75 |
if (parts.get(6).equalsIgnoreCase("D")) {
|
|
|
76 |
debit = m;
|
|
|
77 |
} else {
|
|
|
78 |
credit = m;
|
|
|
79 |
}
|
|
|
80 |
final String valid = parts.get(7);
|
|
|
81 |
Date validDate = null;
|
|
|
82 |
if (valid.equalsIgnoreCase("V")) {
|
|
|
83 |
validDate = ecritureDate;
|
|
|
84 |
}
|
|
|
85 |
final String ecritureLib = unquote(parts.get(8).trim());
|
|
|
86 |
final String pieceRef = "Import " + journalCode;
|
|
|
87 |
Piece p = this.mapPiece.get(pieceRef);
|
|
|
88 |
Mouvement mouvement;
|
|
|
89 |
if (p == null) {
|
|
|
90 |
p = new Piece(pieceRef);
|
|
|
91 |
mouvement = new Mouvement();
|
|
|
92 |
p.add(mouvement);
|
|
|
93 |
|
|
|
94 |
} else {
|
|
|
95 |
mouvement = p.getMouvements().get(0);
|
|
|
96 |
}
|
|
|
97 |
final Ecriture ecriture = new Ecriture(ecritureDate, debit, credit);
|
|
|
98 |
ecriture.setNom(ecritureLib);
|
|
|
99 |
ecriture.setCompte(compteNum, compteLib);
|
|
|
100 |
final String journal = getJournal(journalCode);
|
|
|
101 |
if (journal == null) {
|
|
|
102 |
this.error = "Pas de journal trouvé pour le code journal : " + journalCode;
|
|
|
103 |
return;
|
|
|
104 |
}
|
|
|
105 |
ecriture.setJournal(journalCode, journal);
|
|
|
106 |
ecriture.setDateValidation(validDate);
|
|
|
107 |
if ("AN".equals(journalCode)) {
|
|
|
108 |
ecriture.setaNouveau(true);
|
|
|
109 |
}
|
|
|
110 |
mouvement.add(ecriture);
|
|
|
111 |
|
|
|
112 |
// Next
|
|
|
113 |
line = bReader.readLine();
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
private String unquote(String str) {
|
|
|
120 |
if (str.length() < 3) {
|
|
|
121 |
return str;
|
|
|
122 |
}
|
|
|
123 |
return str.substring(1, str.length() - 1);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
private void parseJournaux(File file) throws IOException {
|
|
|
127 |
try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
|
|
|
128 |
String line = bReader.readLine();
|
|
|
129 |
while (!(line.startsWith("##Section") && line.contains("Jnl"))) {
|
|
|
130 |
line = bReader.readLine();
|
|
|
131 |
}
|
|
|
132 |
line = bReader.readLine();
|
|
|
133 |
while (line != null && !line.contains("#Section")) {
|
|
|
134 |
final List<String> parts = StringUtils.fastSplit(line, '\t');
|
|
|
135 |
this.mapJournal.put(unquote(parts.get(0)), unquote(parts.get(1)));
|
|
|
136 |
// Next
|
|
|
137 |
line = bReader.readLine();
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
private String getJournal(String journalCode) {
|
|
|
144 |
return this.mapJournal.get(journalCode);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private String cleanMontant(String str) {
|
|
|
148 |
final int l = str.length();
|
|
|
149 |
final StringBuilder b = new StringBuilder(l);
|
|
|
150 |
for (int i = 0; i < l; i++) {
|
|
|
151 |
final char c = str.charAt(i);
|
|
|
152 |
if (Character.isDigit(c)) {
|
|
|
153 |
b.append(c);
|
|
|
154 |
} else if (c == ',') {
|
|
|
155 |
b.append('.');
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
return b.toString();
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
private Date parseDate(String str) {
|
|
|
162 |
if (str.length() != 10) {
|
|
|
163 |
return null;
|
|
|
164 |
}
|
|
|
165 |
final Calendar c = Calendar.getInstance();
|
|
|
166 |
|
|
|
167 |
final int day = Integer.parseInt(str.substring(0, 2));
|
|
|
168 |
final int month = Integer.parseInt(str.substring(3, 5));
|
|
|
169 |
final int year = Integer.parseInt(str.substring(6, 10));
|
|
|
170 |
c.set(year, month - 1, day, 0, 0, 0);
|
|
|
171 |
return c.getTime();
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// return null is no error
|
|
|
175 |
public String getError() {
|
|
|
176 |
return this.error;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public void importTo(SQLElementDirectory directory, DBRoot rootSociete, User user) throws SQLException {
|
|
|
180 |
Exercice e = new Exercice();
|
|
|
181 |
e.insert(directory, rootSociete, user, new ArrayList<Piece>(this.mapPiece.values()));
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
public static void main(String[] args) throws IOException {
|
|
|
185 |
ImportRImport i = new ImportRImport();
|
|
|
186 |
i.loadFrom(new File("Y:\\Projets\\OpenConcerto\\ExportCiel\\RImport.txt"));
|
|
|
187 |
System.err.println("ImportRImport.main()" + i.getError());
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
}
|