OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | 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.
 */
 
 /*
 * 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.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreMode;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.utils.StringUtils;

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

public class GenerationMvtDepotChequeClient extends GenerationEcritures {

    private long montant;

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

    public GenerationMvtDepotChequeClient(SQLRow depot) {
        this.montant = depot.getLong("MONTANT");
        this.date = depot.getDate("DATE").getTime();
        this.banque = depot.getForeign("ID_" + BanqueSQLElement.TABLENAME);
        this.depot = depot;
        // 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 cheque client du mouvement " + this.idMvt);
        this.nom = this.depot.getString("NOM");
        if (this.depot.getObject("ID_MOUVEMENT") == null || this.depot.isForeignEmpty("ID_MOUVEMENT")) {
            this.idMvt = getNewMouvement(depot.getTable().getName(), depot.getID(), 1, this.nom);
        } else {
            this.idMvt = this.depot.getForeignID("ID_MOUVEMENT");
            SQLRowValues rowValspiece = this.depot.getForeign("ID_MOUVEMENT").getForeign("ID_PIECE").createEmptyUpdateRow();
            rowValspiece.put("NOM", this.nom);
            rowValspiece.update();
        }

        // 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(depot);
        } else {
            int idJrnl = this.banque.getForeignID("ID_JOURNAL");
            this.putValue("ID_JOURNAL", idJrnl);
        }

        List<Integer> pieceIDs = new ArrayList<Integer>();
        SQLRowValues rowValsDepotElt = new SQLRowValues(depot.getTable().getTable("DEPOT_CHEQUE_ELEMENT"));
        rowValsDepotElt.putNulls("MONTANT", "TIERS", "PIECE");
        rowValsDepotElt.putRowValues("ID_CLIENT").putNulls("NOM", "ID_COMPTE_PCE");
        final SQLRowValues rowValuesChq = rowValsDepotElt.putRowValues("ID_CHEQUE_A_ENCAISSER");
        rowValuesChq.putNulls("SANS_VALEUR_ENCAISSEMENT").putRowValues("ID_MOUVEMENT").putRowValues("ID_PIECE").putNulls("NOM");
        rowValuesChq.putNulls("ID_COMPTE_PCE_TIERS").putNulls("NUMERO");
        List<SQLRowValues> cheques = SQLRowValuesListFetcher.create(rowValsDepotElt).fetch(new Where(rowValsDepotElt.getTable().getField("ID_DEPOT_CHEQUE"), "=", depot.getID()));
        for (SQLRowValues sqlRowAccessor : cheques) {
            final SQLRowAccessor clientRow = sqlRowAccessor.getForeign("ID_CLIENT");
            // this.nom = this.nom + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
            this.putValue("NOM", this.nom + " " + sqlRowAccessor.getString("PIECE") + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20));
            SQLRowAccessor chequeRow = sqlRowAccessor.getForeign("ID_CHEQUE_A_ENCAISSER");
            pieceIDs.add(chequeRow.getForeign("ID_MOUVEMENT").getForeignID("ID_PIECE"));
            // compte Clients
            SQLRowAccessor rowCptTiers = chequeRow.getForeign("ID_COMPTE_PCE_TIERS");
            int idCompteClient = rowCptTiers != null && !rowCptTiers.isUndefined() ? rowCptTiers.getID() : -1;

            if (chequeRow.getBoolean("SANS_VALEUR_ENCAISSEMENT")) {
                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");
                        }
                    }
                }
            } else {
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_VALEUR_ENCAISSEMENT");
                if (idCompteClient <= 1) {
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("ValeurEncaissement");
                }
            }

            this.putValue("ID_COMPTE_PCE", new Integer(idCompteClient));
            this.putValue("DEBIT", new Long(0));
            this.putValue("CREDIT", new Long(sqlRowAccessor.getLong("MONTANT")));
            SQLRow insertedRow = ajoutEcriture();

            sqlRowAccessor.createEmptyUpdateRow().put("ID_ECRITURE", insertedRow.getID()).getGraph().store(StoreMode.COMMIT, false);
            sqlRowAccessor.getForeign("ID_CHEQUE_A_ENCAISSER").createEmptyUpdateRow().put("ENCAISSE", Boolean.TRUE).getGraph().store(StoreMode.COMMIT, false);
        }
        // compte de reglement cheque, ...
        fillCompteBanqueFromRow(depot, "VenteCheque", false);
        this.putValue("NOM", this.nom);
        this.putValue("DEBIT", new Long(this.montant));
        this.putValue("CREDIT", new Long(0));
        SQLRow insertedRow = ajoutEcriture();

        depot.createEmptyUpdateRow().put("ID_MOUVEMENT", idMvt).put("ID_ECRITURE", insertedRow.getID()).getGraph().store(StoreMode.COMMIT, false);

        pieceIDs.add(mouvementTable.getRow(idMvt).getForeignID("ID_PIECE"));
        lettrageAuto(pieceIDs, this.date);

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

    }

    // 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();
    // }
    // }
    // }

}