OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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.generationEcritures;

import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class GenerationMvtSaisieKm extends GenerationEcritures {

    private int idSaisieKm;
    private static final String source = "SAISIE_KM";

    public static final String ID = "accounting.records.km";

    public GenerationMvtSaisieKm(int idSaisieKm) {
        this.idSaisieKm = idSaisieKm;
    }

    public int genereMouvement() throws SQLException {

        final SQLTable table = base.getTable("SAISIE_KM");
        SQLRow saisieRow = table.getRow(this.idSaisieKm);

        // iniatilisation des valeurs de la map
        this.date = (Date) saisieRow.getObject("DATE");
        final String labelSaisie = saisieRow.getObject("NOM").toString();
        this.nom = labelSaisie;
        this.putValue("DATE", this.date);
        this.putValue("NOM", this.nom);
        this.putValue("ID_JOURNAL", saisieRow.getObject("ID_JOURNAL"));
        this.putValue("ID_MOUVEMENT", new Integer(1));

        // on calcule le nouveau numero de mouvement
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
        SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
        rowValsPiece.put("NOM", (labelSaisie.length() == 0 ? "Saisie au km " : labelSaisie));
        if (provider != null) {
            provider.putPieceLabel(saisieRow, rowValsPiece);
        }

        getNewMouvement(GenerationMvtSaisieKm.source, this.idSaisieKm, 1, rowValsPiece);

        // gnération des ecritures
        SQLTable tableElt = table.getDBRoot().findTable("SAISIE_KM_ELEMENT");
        List<SQLRow> set = saisieRow.getReferentRows(tableElt);

        SQLTable tableAssoc = Configuration.getInstance().getRoot().findTable("ASSOCIATION_ANALYTIQUE");

        for (SQLRow rowElement : set) {

            int idCpt = ComptePCESQLElement.getId(rowElement.getString("NUMERO"), rowElement.getString("NOM"));

            if (ecritureTable.contains("DATE_ECHEANCE") && table.getDBRoot().findTable("SAISIE_KM_ELEMENT").contains("DATE_ECHEANCE")) {
                this.putValue("DATE_ECHEANCE", rowElement.getObject("DATE_ECHEANCE"));
            }
            // Ajout de l'écriture
            this.putValue("ID_COMPTE_PCE", new Integer(idCpt));
            this.putValue("NOM", rowElement.getString("NOM_ECRITURE"));
            this.putValue("DEBIT", rowElement.getObject("DEBIT"));
            this.putValue("CREDIT", rowElement.getObject("CREDIT"));
            if (tableElt.contains("NOM_PIECE")) {
                this.putValue("NOM_PIECE", rowElement.getObject("NOM_PIECE"));
            }
            SQLRow rowEcr = ajoutEcriture();

            // List<SQLRow> assocs = rowElement.getReferentRows(tableAssoc);
            // for (SQLRow sqlRow : assocs) {
            // if (!sqlRow.isUndefined()) {
            // addAssocAnalytique(rowEcr, sqlRow.getInt("ID_POSTE_ANALYTIQUE"));
            // }
            // }

            // Mise à jour de la clef étrangère écriture de l'élément saisie au km
            if (rowEcr != null && !rowEcr.isUndefined()) {
                SQLRowValues vals = rowElement.createEmptyUpdateRow();
                vals.put("ID_ECRITURE", rowEcr.getID());
                vals.update();
            }
        }

        return this.idMvt;
    }
}