OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Rev 156 | 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.
 */
 
 package org.openconcerto.erp.generationEcritures;

import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
import org.openconcerto.erp.model.PrixHT;
import org.openconcerto.erp.model.PrixTTC;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;

import java.util.Date;

public class GenerationMvtAvoirFournisseur extends GenerationEcritures {

    private static final String source = "AVOIR_FOURNISSEUR";
    // TODO dans quel journal les ecritures des avoirs? OD?
    private static final Integer journal = Integer.valueOf(JournalSQLElement.ACHATS);
    private int idAvoirFourn;
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);

    public GenerationMvtAvoirFournisseur(SQLRow row) {
        this(row, 1);
    }

    public GenerationMvtAvoirFournisseur(SQLRow row, int idMvt) {
        this.idMvt = idMvt;
        this.idAvoirFourn = row.getID();
        setRowAnalytiqueSource(row);
    }

    public int genereMouvement() throws Exception {

        SQLTable avoirFournTable = base.getTable("AVOIR_FOURNISSEUR");
        SQLTable fournTable = base.getTable("FOURNISSEUR");

        SQLRow avoirRow = avoirFournTable.getRow(this.idAvoirFourn);
        SQLRow rowFourn = fournTable.getRow(avoirRow.getInt("ID_FOURNISSEUR"));

        // Calcul des montants
        PrixTTC prixTTC = new PrixTTC(((Long) avoirRow.getObject("MONTANT_TTC")).longValue());
        PrixHT prixHT = new PrixHT(((Long) avoirRow.getObject("MONTANT_HT")).longValue());
        PrixHT prixTVA = new PrixHT(((Long) avoirRow.getObject("MONTANT_TVA")).longValue());

        // iniatilisation des valeurs de la map
        this.date = (Date) avoirRow.getObject("DATE");
        this.nom = avoirRow.getObject("NOM").toString();
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
        this.putValue("NOM", "Avoir fournisseur : " + avoirRow.getString("NUMERO") + " " + rowFourn.getString("NOM"));
        this.putValue("ID_JOURNAL", GenerationMvtAvoirFournisseur.journal);
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));

        // on cree un nouveau mouvement
        if (this.idMvt == 1) {
            getNewMouvement(GenerationMvtAvoirFournisseur.source, this.idAvoirFourn, 1, "Avoir Fournisseur : " + avoirRow.getString("NUMERO"));
        } else {
            this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
        }

        // generation des ecritures + maj des totaux du compte associe

        // compte Achat
        int idCompteAchat = avoirRow.getInt("ID_COMPTE_PCE");

        if (idCompteAchat <= 1) {
            idCompteAchat = rowPrefsCompte.getInt("ID_COMPTE_PCE_ACHAT");
            if (idCompteAchat <= 1) {
                try {
                    idCompteAchat = ComptePCESQLElement.getIdComptePceDefault("Achats");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteAchat));
        this.putValue("DEBIT", Long.valueOf(0));
        this.putValue("CREDIT", Long.valueOf(prixHT.getLongValue()));
        SQLRow rowEcr = ajoutEcriture();
        // addAssocAnalytiqueFromProvider(rowEcr, avoirRow);

        if (prixTVA.getLongValue() > 0) {
            // compte TVA
            int idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_ACHAT");
            if (avoirRow.getBoolean("IMMO")) {
                idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_IMMO");
                if (idCompteTVA <= 1) {
                    idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVAImmo");
                }
            } else {
                idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_ACHAT");
                if (idCompteTVA <= 1) {
                    idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVADeductible");
                }
            }

            this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteTVA));
            this.putValue("DEBIT", Long.valueOf(0));
            this.putValue("CREDIT", Long.valueOf(prixTVA.getLongValue()));
            ajoutEcriture();

            if (rowFourn.getBoolean("UE")) {
                int idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
                if (idCompteTVAIntra <= 1) {
                    idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
                }
                this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteTVAIntra));
                this.putValue("DEBIT", Long.valueOf(prixTVA.getLongValue()));
                this.putValue("CREDIT", Long.valueOf(0));
                ajoutEcriture();
            }
        }
        // compte Fournisseur
        int idCompteFourn = rowFourn.getInt("ID_COMPTE_PCE");
        if (idCompteFourn <= 1) {
            idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
            if (idCompteFourn <= 1) {
                idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
            }
        }
        this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteFourn));
        this.putValue("DEBIT", Long.valueOf(prixTTC.getLongValue()));
        this.putValue("CREDIT", Long.valueOf(0));
        ajoutEcriture();

        // Mise à jour de mouvement associé à la facture d'avoir
        SQLRowValues valAvoir = new SQLRowValues(avoirFournTable);
        valAvoir.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));

        if (valAvoir.getInvalid() == null) {

            valAvoir.update(this.idAvoirFourn);
            displayMvtNumber();
        }

        // if (avoirRow.getInt("ID_MODE_REGLEMENT") > 1) {
        // new GenerationMvtReglementAvoir(this.idAvoirFourn, this.idMvt);
        // } else {
        // valAvoir.put("SOLDE", Boolean.FALSE);
        // try {
        // valAvoir.update(this.idAvoirClient);
        // } catch (SQLException e) {
        // e.printStackTrace();
        // }
        // }

        return this.idMvt;
    }
}