Dépôt officiel du code source de l'ERP OpenConcerto
Rev 174 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.panel.compta;
import org.openconcerto.erp.generationEcritures.Ecriture;
import org.openconcerto.erp.generationEcritures.Exercice;
import org.openconcerto.erp.generationEcritures.Mouvement;
import org.openconcerto.erp.generationEcritures.Piece;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.users.User;
import org.openconcerto.utils.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ImportRImport {
private String error = null;
private final Map<String, Piece> mapPiece = new HashMap<>();
private final Map<String, String> mapJournal = new HashMap<>();
/**
* Import format Ciel.
*
* Une pièce par journal
*
*/
public ImportRImport() {
//
}
public void loadFrom(File file) throws IOException {
this.mapPiece.clear();
this.mapJournal.clear();
this.error = null;
parseJournaux(file);
try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
String line = bReader.readLine();
while (!(line.startsWith("##Section") && line.contains("Mvt"))) {
line = bReader.readLine();
}
line = bReader.readLine();
while (line != null && !line.contains("#Section")) {
final List<String> parts = StringUtils.fastSplit(line, '\t');
if (parts.size() > 6) {
final String journalCode = unquote(parts.get(1).trim());
final Date ecritureDate = parseDate(unquote(parts.get(2).trim()));
final String compteNum = unquote(parts.get(3).trim());
final String compteLib = unquote(parts.get(4).trim());
final BigDecimal m = new BigDecimal(cleanMontant(parts.get(5)));
BigDecimal debit = BigDecimal.ZERO;
BigDecimal credit = BigDecimal.ZERO;
if (parts.get(6).equalsIgnoreCase("D")) {
debit = m;
} else {
credit = m;
}
final String valid = parts.get(7);
Date validDate = null;
if (valid.equalsIgnoreCase("V")) {
validDate = ecritureDate;
}
final String ecritureLib = unquote(parts.get(8).trim());
final String pieceRef = "Import " + journalCode;
Piece p = this.mapPiece.get(pieceRef);
Mouvement mouvement;
if (p == null) {
p = new Piece(pieceRef);
mouvement = new Mouvement();
p.add(mouvement);
this.mapPiece.put(pieceRef, p);
} else {
mouvement = p.getMouvements().get(0);
}
final Ecriture ecriture = new Ecriture(ecritureDate, debit, credit);
ecriture.setNom(ecritureLib);
ecriture.setCompte(compteNum, compteLib);
final String journal = getJournal(journalCode);
if (journal == null) {
this.error = "Pas de journal trouvé pour le code journal : " + journalCode;
return;
}
ecriture.setJournal(journalCode, journal);
ecriture.setDateValidation(validDate);
if ("AN".equals(journalCode)) {
ecriture.setaNouveau(true);
}
mouvement.add(ecriture);
} else {
System.err.println("Ignoring : " + line);
}
// Next
line = bReader.readLine();
}
}
}
private String unquote(String str) {
if (str.length() < 3) {
return str;
}
return str.substring(1, str.length() - 1);
}
private void parseJournaux(File file) throws IOException {
try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
String line = bReader.readLine();
while (!(line.startsWith("##Section") && line.contains("Jnl"))) {
line = bReader.readLine();
}
line = bReader.readLine();
while (line != null && !line.contains("#Section")) {
final List<String> parts = StringUtils.fastSplit(line, '\t');
this.mapJournal.put(unquote(parts.get(0)), unquote(parts.get(1)));
// Next
line = bReader.readLine();
}
}
}
private String getJournal(String journalCode) {
return this.mapJournal.get(journalCode);
}
private String cleanMontant(String str) {
final int l = str.length();
final StringBuilder b = new StringBuilder(l);
for (int i = 0; i < l; i++) {
final char c = str.charAt(i);
if (Character.isDigit(c)) {
b.append(c);
} else if (c == ',') {
b.append('.');
}
}
return b.toString();
}
private Date parseDate(String str) {
if (str.length() != 10) {
return null;
}
final Calendar c = Calendar.getInstance();
final int day = Integer.parseInt(str.substring(0, 2));
final int month = Integer.parseInt(str.substring(3, 5));
final int year = Integer.parseInt(str.substring(6, 10));
c.set(year, month - 1, day, 0, 0, 0);
return c.getTime();
}
// return null is no error
public String getError() {
return this.error;
}
public List<Piece> getPieces() {
return new ArrayList<Piece>(this.mapPiece.values());
}
public void importTo(SQLElementDirectory directory, DBRoot rootSociete, User user) throws SQLException {
Exercice e = new Exercice();
e.insert(directory, rootSociete, user, new ArrayList<Piece>(this.mapPiece.values()));
}
public static void main(String[] args) throws IOException {
ImportRImport i = new ImportRImport();
i.loadFrom(new File("Y:\\Projets\\OpenConcerto\\ExportCiel\\RImport.txt"));
System.err.println("ImportRImport.main() " + i.getPieces().size() + " pieces");
System.err.println("ImportRImport.main()" + i.getError());
for (Piece p : i.getPieces()) {
System.err.println(p.getNom() + " " + p.getMouvements().size() + " mouvements");
for (Mouvement m : p.getMouvements()) {
System.err.println(m.asString());
}
}
}
}