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