OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 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.
174 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.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');
182 ilm 68
                if (parts.size() > 6) {
69
                    final String journalCode = unquote(parts.get(1).trim());
70
                    final Date ecritureDate = parseDate(unquote(parts.get(2).trim()));
71
                    final String compteNum = unquote(parts.get(3).trim());
72
                    final String compteLib = unquote(parts.get(4).trim());
73
                    final BigDecimal m = new BigDecimal(cleanMontant(parts.get(5)));
74
                    BigDecimal debit = BigDecimal.ZERO;
75
                    BigDecimal credit = BigDecimal.ZERO;
76
                    if (parts.get(6).equalsIgnoreCase("D")) {
77
                        debit = m;
78
                    } else {
79
                        credit = m;
80
                    }
81
                    final String valid = parts.get(7);
82
                    Date validDate = null;
83
                    if (valid.equalsIgnoreCase("V")) {
84
                        validDate = ecritureDate;
85
                    }
86
                    final String ecritureLib = unquote(parts.get(8).trim());
87
                    final String pieceRef = "Import " + journalCode;
88
                    Piece p = this.mapPiece.get(pieceRef);
89
                    Mouvement mouvement;
90
                    if (p == null) {
91
                        p = new Piece(pieceRef);
92
                        mouvement = new Mouvement();
93
                        p.add(mouvement);
94
                        this.mapPiece.put(pieceRef, p);
95
                    } else {
96
                        mouvement = p.getMouvements().get(0);
97
                    }
98
                    final Ecriture ecriture = new Ecriture(ecritureDate, debit, credit);
99
                    ecriture.setNom(ecritureLib);
100
                    ecriture.setCompte(compteNum, compteLib);
101
                    final String journal = getJournal(journalCode);
102
                    if (journal == null) {
103
                        this.error = "Pas de journal trouvé pour le code journal : " + journalCode;
104
                        return;
105
                    }
106
                    ecriture.setJournal(journalCode, journal);
107
                    ecriture.setDateValidation(validDate);
108
                    if ("AN".equals(journalCode)) {
109
                        ecriture.setaNouveau(true);
110
                    }
174 ilm 111
 
182 ilm 112
                    mouvement.add(ecriture);
174 ilm 113
                } else {
182 ilm 114
                    System.err.println("Ignoring : " + line);
174 ilm 115
                }
116
                // Next
117
                line = bReader.readLine();
118
            }
119
 
120
        }
121
    }
122
 
123
    private String unquote(String str) {
124
        if (str.length() < 3) {
125
            return str;
126
        }
127
        return str.substring(1, str.length() - 1);
128
    }
129
 
130
    private void parseJournaux(File file) throws IOException {
131
        try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
132
            String line = bReader.readLine();
133
            while (!(line.startsWith("##Section") && line.contains("Jnl"))) {
134
                line = bReader.readLine();
135
            }
136
            line = bReader.readLine();
137
            while (line != null && !line.contains("#Section")) {
138
                final List<String> parts = StringUtils.fastSplit(line, '\t');
139
                this.mapJournal.put(unquote(parts.get(0)), unquote(parts.get(1)));
140
                // Next
141
                line = bReader.readLine();
142
            }
143
        }
144
 
145
    }
146
 
147
    private String getJournal(String journalCode) {
148
        return this.mapJournal.get(journalCode);
149
    }
150
 
151
    private String cleanMontant(String str) {
152
        final int l = str.length();
153
        final StringBuilder b = new StringBuilder(l);
154
        for (int i = 0; i < l; i++) {
155
            final char c = str.charAt(i);
156
            if (Character.isDigit(c)) {
157
                b.append(c);
158
            } else if (c == ',') {
159
                b.append('.');
160
            }
161
        }
162
        return b.toString();
163
    }
164
 
165
    private Date parseDate(String str) {
166
        if (str.length() != 10) {
167
            return null;
168
        }
169
        final Calendar c = Calendar.getInstance();
170
 
171
        final int day = Integer.parseInt(str.substring(0, 2));
172
        final int month = Integer.parseInt(str.substring(3, 5));
173
        final int year = Integer.parseInt(str.substring(6, 10));
174
        c.set(year, month - 1, day, 0, 0, 0);
175
        return c.getTime();
176
    }
177
 
178
    // return null is no error
179
    public String getError() {
180
        return this.error;
181
    }
182
 
182 ilm 183
    public List<Piece> getPieces() {
184
        return new ArrayList<Piece>(this.mapPiece.values());
185
    }
186
 
174 ilm 187
    public void importTo(SQLElementDirectory directory, DBRoot rootSociete, User user) throws SQLException {
188
        Exercice e = new Exercice();
189
        e.insert(directory, rootSociete, user, new ArrayList<Piece>(this.mapPiece.values()));
190
    }
191
 
192
    public static void main(String[] args) throws IOException {
193
        ImportRImport i = new ImportRImport();
194
        i.loadFrom(new File("Y:\\Projets\\OpenConcerto\\ExportCiel\\RImport.txt"));
182 ilm 195
        System.err.println("ImportRImport.main() " + i.getPieces().size() + " pieces");
174 ilm 196
        System.err.println("ImportRImport.main()" + i.getError());
182 ilm 197
        for (Piece p : i.getPieces()) {
198
            System.err.println(p.getNom() + " " + p.getMouvements().size() + " mouvements");
199
            for (Mouvement m : p.getMouvements()) {
200
                System.err.println(m.asString());
201
            }
202
        }
203
 
174 ilm 204
    }
205
 
206
}