OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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.
 */
 
 /*
 * 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.generationEcritures;

import org.openconcerto.erp.core.common.element.BanqueSQLElement;
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class GenerationMvtSepa extends GenerationEcritures {

    private long montant;

    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
    private final List<SQLRow> sepaRows;
    private SQLRowAccessor banque;

    public GenerationMvtSepa(List<SQLRow> sepaRows) {

        this.sepaRows = sepaRows;
        // SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
        // String num = "";
        // if (chequeRow != null && !chequeRow.isUndefined() &&
        // chequeRow.getString("NUMERO").trim().length() > 0) {
        // num = " N° " + chequeRow.getString("NUMERO");
        // }
        //
        // if (s != null && s.trim().length() > 0) {
        // this.nom = s + (num.trim().length() > 0 ? " - Cheque" + num : "");
        // } else {
        // this.nom = "Reglement cheque client" + num;
        // }
    }

    public void genere() throws Exception {
        System.err.println("génération des ecritures de règlement d'un prelevement client du mouvement " + this.idMvt);
        List<Integer> pieceIDs = new ArrayList<>();
        for (SQLRow rowSepa : sepaRows) {
            this.montant = rowSepa.getLong("MONTANT");
            this.date = rowSepa.getDate("DATE").getTime();
            this.banque = rowSepa.getForeign("ID_" + BanqueSQLElement.TABLENAME);

            final SQLRowAccessor foreignPiece = rowSepa.getForeign("ID_MOUVEMENT").getForeign("ID_PIECE");
            this.nom = "Prélèvement " + foreignPiece.getString("NOM");
            final SQLRowAccessor clientRow = rowSepa.getForeign("ID_CLIENT");

            // Création de l'encaissement
            SQLRowValues rowVals = new SQLRowValues(base.getTable("ENCAISSER_MONTANT"));
            rowVals.put("MONTANT", montant);
            rowVals.put("ID_CLIENT", clientRow != null ? clientRow.getID() : null);
            rowVals.put("TIERS", rowSepa.getString("TIERS"));
            rowVals.put("DATE", this.date);
            SQLRowValues rowValsRegl = new SQLRowValues(base.getTable("MODE_REGLEMENT"));
            rowValsRegl.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.PRELEVEMENT);
            SQLRow copy = rowValsRegl.insert();
            rowVals.put("ID_MODE_REGLEMENT", copy.getID());
            rowVals.put("NOM", this.nom);
            // rowVals.put("ID_MOUVEMENT", this.idMvt);
            SQLRow rowEncaisse = rowVals.insert();

            SQLRowValues rowValsElt = new SQLRowValues(base.getTable("ENCAISSER_MONTANT_ELEMENT"));
            rowValsElt.put("MONTANT_REGLE", this.montant);
            rowValsElt.put("ID_ENCAISSER_MONTANT", rowEncaisse.getID());
            rowValsElt.put("ID_MOUVEMENT_ECHEANCE", rowSepa.getForeignID("ID_MOUVEMENT"));
            rowValsElt.insert();

            this.idMvt = getNewMouvement(rowEncaisse.getTable().getName(), rowEncaisse.getID(), rowSepa.getForeignID("ID_MOUVEMENT"), foreignPiece.getID());
            rowEncaisse.createEmptyUpdateRow().put("ID_MOUVEMENT", this.idMvt).commit();

            // initialisation des valeurs de la map
            this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
            this.putValue("DATE", new java.sql.Date(this.date.getTime()));
            this.putValue("NOM", this.nom);
            if (this.banque == null || this.banque.isUndefined() || this.banque.isForeignEmpty("ID_JOURNAL")) {
                fillJournalBanqueFromRow(rowSepa);
            } else {
                int idJrnl = this.banque.getForeignID("ID_JOURNAL");
                this.putValue("ID_JOURNAL", idJrnl);
            }

            this.putValue("NOM", this.nom + " " + foreignPiece.getString("NOM") + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20));
            // compte Clients
            SQLRowAccessor rowCptTiers = rowSepa.getForeign("ID_COMPTE_PCE_TIERS");
            int idCompteClient = rowCptTiers != null && !rowCptTiers.isUndefined() ? rowCptTiers.getID() : -1;

            if (idCompteClient == -1) {
                if (clientRow != null) {
                    idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
                }
                if (idCompteClient <= 1) {
                    idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
                    if (idCompteClient <= 1) {
                        idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
                    }
                }
            }

            this.putValue("ID_COMPTE_PCE", new Integer(idCompteClient));
            this.putValue("DEBIT", new Long(0));
            this.putValue("CREDIT", new Long(rowSepa.getLong("MONTANT")));
            ajoutEcriture();

            // compte de reglement cheque, ...
            fillCompteBanqueFromRow(rowSepa, "VenteCheque", false);
            this.putValue("NOM", this.nom);
            this.putValue("DEBIT", new Long(this.montant));
            this.putValue("CREDIT", new Long(0));
            ajoutEcriture();

            pieceIDs.add(foreignPiece.getID());

            System.err.println("Ecritures générées pour le mouvement " + this.idMvt);
        }

        lettrageAuto(pieceIDs, this.date);

        // TODO mettre à jour la date de réglement dee la facture
    }

    // private void setDateReglement(int idCheque, Date d) throws SQLException {
    // if (idCheque > 1) {
    // SQLRow chequeRow =
    // Configuration.getInstance().getBase().getTable("CHEQUE_A_ENCAISSER").getRow(idCheque);
    // final int sourceId = MouvementSQLElement.getSourceId(chequeRow.getInt("ID_MOUVEMENT"));
    // SQLRow rowMvt = Configuration.getInstance().getBase().getTable("MOUVEMENT").getRow(sourceId);
    //
    // if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
    // SQLElement eltFacture =
    // Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
    // SQLRow saisieRow = eltFacture.getTable().getRow(rowMvt.getInt("IDSOURCE"));
    // // On fixe la date du paiement
    // SQLRowValues rowValsUpdateVF = saisieRow.createEmptyUpdateRow();
    // rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
    // rowValsUpdateVF.update();
    // }
    // }
    // }

}