OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
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.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GenerationMvtReglementChequeClient extends GenerationEcritures {

    private long montant;

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

    public GenerationMvtReglementChequeClient(int idMvt, long montant, Date d, int idCheque, String s, SQLRowAccessor banque) {
        this.montant = montant;
        this.date = d;
        this.idMvt = idMvt;
        this.idCheque = idCheque;
        this.banque = banque;
        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);
        SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
        SQLRow clientRow = base.getTable("CLIENT").getRow(chequeRow.getInt("ID_CLIENT"));

        this.nom = this.nom + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);

        // initialisation des valeurs de la map
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
        this.putValue("NOM", this.nom);
        this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));

        if (this.banque == null || this.banque.isUndefined() || this.banque.isForeignEmpty("ID_JOURNAL")) {
            fillJournalBanqueFromRow(chequeRow);
        } else {
            int idJrnl = this.banque.getForeignID("ID_JOURNAL");
            this.putValue("ID_JOURNAL", idJrnl);
        }

        setDateReglement(this.idCheque, this.date);

        // compte Clients
        int idCompteClient = -1;
        if (chequeRow.getBoolean("SANS_VALEUR_ENCAISSEMENT")) {
            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(this.montant));
        ajoutEcriture();
        System.err.println("First ECriture for mvt " + this.idMvt);

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

        // FIXME remove getConf
        SQLRow rowSoc = ComptaPropsConfiguration.getInstanceCompta().getRowSociete();
        if (rowSoc.getTable().contains("TVA_ENCAISSEMENT") && rowSoc.getBoolean("TVA_ENCAISSEMENT")) {
            SQLRow rowTaxe = TaxeCache.getCache().getFirstTaxe();
            if (rowTaxe.contains("ID_COMPTE_PCE_COLLECTE_ENCAISSEMENT")) {
                SQLRowAccessor rowCompteTvaEnc = rowTaxe.getNonEmptyForeign("ID_COMPTE_PCE_COLLECTE_ENCAISSEMENT");
                SQLRowAccessor rowCompteTvaCol = rowTaxe.getNonEmptyForeign("ID_COMPTE_PCE_COLLECTE");
                if (rowCompteTvaCol != null && rowCompteTvaEnc != null) {
                    Float taux = rowTaxe.getFloat("TAUX");
                    long ht = Math.round(montant / (1.0 + (taux / 100.0)));
                    long tva = montant - ht;
                    this.putValue("ID_COMPTE_PCE", rowCompteTvaCol.getID());
                    this.putValue("DEBIT", Long.valueOf(tva));
                    this.putValue("CREDIT", Long.valueOf(0));
                    ajoutEcriture();

                    this.putValue("ID_COMPTE_PCE", rowCompteTvaEnc.getID());
                    this.putValue("DEBIT", Long.valueOf(0));
                    this.putValue("CREDIT", Long.valueOf(tva));
                    ajoutEcriture();

                }
            }
        }

        List<Integer> pieceIDs = new ArrayList<Integer>();
        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();
            }
        }
    }

}