OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 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 ImportFEC {
    private String error = null;
    private Map<String, Piece> mapPiece = new HashMap<>();

    public ImportFEC() {
        //
    }

    public void loadFrom(File file) throws IOException {
        this.error = null;
        this.mapPiece = new HashMap<>();

        try (BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1252")))) {
            String line = bReader.readLine();
            final List<String> headers = StringUtils.fastSplit(line, ExportFEC.ZONE_SEPARATOR);
            for (int i = 0; i < headers.size(); i++) {
                String c1 = headers.get(i);
                String c2 = ExportFEC.COLS.get(i);
                // Typo chez les stars de Ciel : MontantDevise au lieu de Montantdevise
                if (!c1.equalsIgnoreCase(c2)) {
                    this.error = "L'entête de la colonne " + (i + 1) + " : " + c1 + " n'est pas " + c2;
                    return;
                }
            }
            line = bReader.readLine();
            while (line != null) {
                final List<String> parts = fastSplit(line, ExportFEC.ZONE_SEPARATOR);
                final String journalCode = parts.get(0).trim();
                final String journalLib = parts.get(1).trim();
                // final String ecritureNum = parts.get(2).trim();
                final Date ecritureDate = parseDate(parts.get(3).trim());
                final String compteNum = parts.get(4).trim();
                final String compteLib = parts.get(5).trim();
                // final String compAuxNum = parts.get(6).trim();
                // final String compAuxLib = parts.get(7).trim();
                String pieceRef = parts.get(8).trim();
                // final String pieceDate = parts.get(9).trim();
                final String ecritureLib = parts.get(10).trim();
                final BigDecimal debit = new BigDecimal(parts.get(11).replace(',', '.'));
                final BigDecimal credit = new BigDecimal(parts.get(12).replace(',', '.'));
                final String ecritureLet = parts.get(13).trim();
                final Date dateLet = parseDate(parts.get(14).trim());
                final Date validDate = parseDate(parts.get(15).trim());
                // final String montantdevise = parts.get(16).trim();
                // final String idevise = parts.get(17).trim();

                if (pieceRef.isEmpty()) {
                    pieceRef = "Import";
                }
                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);
                ecriture.setJournal(journalCode, journalLib);
                ecriture.setDateValidation(validDate);
                ecriture.setDateLettrage(dateLet);
                ecriture.setLettrage(ecritureLet);
                if ("AN".equals(journalCode)) {
                    ecriture.setaNouveau(true);
                }
                mouvement.add(ecriture);

                // Next
                line = bReader.readLine();
            }

        }
    }

    private Date parseDate(String str) {
        if (str.length() != 8) {
            return null;
        }
        final Calendar c = Calendar.getInstance();
        final int year = Integer.parseInt(str.substring(0, 4));
        final int month = Integer.parseInt(str.substring(4, 6));
        final int day = Integer.parseInt(str.substring(6, 8));
        c.set(year, month - 1, day, 0, 0, 0);
        return c.getTime();
    }

    // return null is no error
    public String getError() {
        return this.error;
    }

    public void importTo(SQLElementDirectory directory, DBRoot rootSociete, User user) throws SQLException {
        final Exercice e = new Exercice();
        e.insert(directory, rootSociete, user, new ArrayList<Piece>(this.mapPiece.values()));
    }

    public static void main(String[] args) throws IOException {
        final ImportFEC i = new ImportFEC();
        i.loadFrom(new File("Y:\\Projets\\OpenConcerto\\ExportCiel\\FEC20171231.txt"));
        System.err.println("ImportFEC.main()" + i.getError());
    }

    public static final List<String> fastSplit(final String string, final char sep) {
        final List<String> l = new ArrayList<>();
        final int length = string.length();
        final char[] cars = string.toCharArray();
        int rfirst = 0;

        for (int i = 0; i < length; i++) {
            if (cars[i] == sep) {
                l.add(new String(cars, rfirst, i - rfirst));
                rfirst = i + 1;
            }
        }
        if (rfirst <= length) {
            // <= au lieu de < pour Ciel....
            l.add(new String(cars, rfirst, length - rfirst));
        }
        return l;
    }
}