OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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
 *
185 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 ImportFEC {
41
    private String error = null;
42
    private Map<String, Piece> mapPiece = new HashMap<>();
43
 
44
    public ImportFEC() {
45
        //
46
    }
47
 
48
    public void loadFrom(File file) throws IOException {
49
        this.error = null;
177 ilm 50
        this.mapPiece = new HashMap<>();
174 ilm 51
 
52
        try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
53
            String line = bReader.readLine();
54
            final List<String> headers = StringUtils.fastSplit(line, ExportFEC.ZONE_SEPARATOR);
185 ilm 55
            for (int i = 0; i < headers.size() && i < ExportFEC.COLS.size(); i++) {
56
 
174 ilm 57
                String c1 = headers.get(i);
58
                String c2 = ExportFEC.COLS.get(i);
59
                // Typo chez les stars de Ciel : MontantDevise au lieu de Montantdevise
60
                if (!c1.equalsIgnoreCase(c2)) {
61
                    this.error = "L'entête de la colonne " + (i + 1) + " : " + c1 + " n'est pas " + c2;
62
                    return;
63
                }
64
            }
65
            line = bReader.readLine();
66
            while (line != null) {
67
                final List<String> parts = fastSplit(line, ExportFEC.ZONE_SEPARATOR);
68
                final String journalCode = parts.get(0).trim();
69
                final String journalLib = parts.get(1).trim();
185 ilm 70
                final String ecritureNum = parts.get(2).trim();
174 ilm 71
                final Date ecritureDate = parseDate(parts.get(3).trim());
72
                final String compteNum = parts.get(4).trim();
73
                final String compteLib = parts.get(5).trim();
74
                // final String compAuxNum = parts.get(6).trim();
75
                // final String compAuxLib = parts.get(7).trim();
185 ilm 76
 
77
                String mvtKey = ecritureNum + "-" + journalCode;
174 ilm 78
                String pieceRef = parts.get(8).trim();
79
                // final String pieceDate = parts.get(9).trim();
80
                final String ecritureLib = parts.get(10).trim();
185 ilm 81
                final BigDecimal debit = new BigDecimal(parts.get(11).replace(" ", "").replace(',', '.'));
82
                final BigDecimal credit = new BigDecimal(parts.get(12).replace(" ", "").replace(',', '.'));
174 ilm 83
                final String ecritureLet = parts.get(13).trim();
84
                final Date dateLet = parseDate(parts.get(14).trim());
85
                final Date validDate = parseDate(parts.get(15).trim());
86
                // final String montantdevise = parts.get(16).trim();
87
                // final String idevise = parts.get(17).trim();
88
 
89
                if (pieceRef.isEmpty()) {
185 ilm 90
                    pieceRef = mvtKey;
174 ilm 91
                }
185 ilm 92
                Piece p = this.mapPiece.get(mvtKey);
174 ilm 93
                Mouvement mouvement;
94
                if (p == null) {
95
                    p = new Piece(pieceRef);
96
                    mouvement = new Mouvement();
97
                    p.add(mouvement);
185 ilm 98
                    this.mapPiece.put(mvtKey, p);
174 ilm 99
                } else {
100
                    mouvement = p.getMouvements().get(0);
101
                }
102
                final Ecriture ecriture = new Ecriture(ecritureDate, debit, credit);
103
                ecriture.setNom(ecritureLib);
104
                ecriture.setCompte(compteNum, compteLib);
105
                ecriture.setJournal(journalCode, journalLib);
106
                ecriture.setDateValidation(validDate);
107
                ecriture.setDateLettrage(dateLet);
108
                ecriture.setLettrage(ecritureLet);
109
                if ("AN".equals(journalCode)) {
110
                    ecriture.setaNouveau(true);
111
                }
112
                mouvement.add(ecriture);
113
 
114
                // Next
115
                line = bReader.readLine();
116
            }
117
 
118
        }
119
    }
120
 
121
    private Date parseDate(String str) {
122
        if (str.length() != 8) {
123
            return null;
124
        }
125
        final Calendar c = Calendar.getInstance();
126
        final int year = Integer.parseInt(str.substring(0, 4));
127
        final int month = Integer.parseInt(str.substring(4, 6));
128
        final int day = Integer.parseInt(str.substring(6, 8));
129
        c.set(year, month - 1, day, 0, 0, 0);
130
        return c.getTime();
131
    }
132
 
133
    // return null is no error
134
    public String getError() {
135
        return this.error;
136
    }
137
 
138
    public void importTo(SQLElementDirectory directory, DBRoot rootSociete, User user) throws SQLException {
139
        final Exercice e = new Exercice();
140
        e.insert(directory, rootSociete, user, new ArrayList<Piece>(this.mapPiece.values()));
141
    }
142
 
143
    public static void main(String[] args) throws IOException {
144
        final ImportFEC i = new ImportFEC();
145
        i.loadFrom(new File("Y:\\Projets\\OpenConcerto\\ExportCiel\\FEC20171231.txt"));
146
        System.err.println("ImportFEC.main()" + i.getError());
147
    }
148
 
149
    public static final List<String> fastSplit(final String string, final char sep) {
150
        final List<String> l = new ArrayList<>();
151
        final int length = string.length();
152
        final char[] cars = string.toCharArray();
153
        int rfirst = 0;
154
 
155
        for (int i = 0; i < length; i++) {
156
            if (cars[i] == sep) {
157
                l.add(new String(cars, rfirst, i - rfirst));
158
                rfirst = i + 1;
159
            }
160
        }
161
        if (rfirst <= length) {
162
            // <= au lieu de < pour Ciel....
163
            l.add(new String(cars, rfirst, length - rfirst));
164
        }
165
        return l;
166
    }
167
}