OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.generationEcritures;
15
 
16
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
17
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
156 ilm 18
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 19
import org.openconcerto.erp.model.PrixHT;
20
import org.openconcerto.erp.model.PrixTTC;
21
import org.openconcerto.sql.model.SQLRow;
156 ilm 22
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLTable;
25
 
26
import java.util.Date;
27
 
28
public class GenerationMvtAvoirFournisseur extends GenerationEcritures {
29
 
30
    private static final String source = "AVOIR_FOURNISSEUR";
31
    // TODO dans quel journal les ecritures des avoirs? OD?
32
    private static final Integer journal = Integer.valueOf(JournalSQLElement.ACHATS);
33
    private int idAvoirFourn;
34
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
35
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
36
 
93 ilm 37
    public GenerationMvtAvoirFournisseur(SQLRow row) {
38
        this(row, 1);
18 ilm 39
    }
40
 
93 ilm 41
    public GenerationMvtAvoirFournisseur(SQLRow row, int idMvt) {
18 ilm 42
        this.idMvt = idMvt;
93 ilm 43
        this.idAvoirFourn = row.getID();
44
        setRowAnalytiqueSource(row);
18 ilm 45
    }
46
 
73 ilm 47
    public int genereMouvement() throws Exception {
18 ilm 48
 
49
        SQLTable avoirFournTable = base.getTable("AVOIR_FOURNISSEUR");
50
        SQLTable fournTable = base.getTable("FOURNISSEUR");
51
 
52
        SQLRow avoirRow = avoirFournTable.getRow(this.idAvoirFourn);
53
        SQLRow rowFourn = fournTable.getRow(avoirRow.getInt("ID_FOURNISSEUR"));
54
 
55
        // Calcul des montants
56
        PrixTTC prixTTC = new PrixTTC(((Long) avoirRow.getObject("MONTANT_TTC")).longValue());
57
        PrixHT prixHT = new PrixHT(((Long) avoirRow.getObject("MONTANT_HT")).longValue());
58
        PrixHT prixTVA = new PrixHT(((Long) avoirRow.getObject("MONTANT_TVA")).longValue());
59
 
60
        // iniatilisation des valeurs de la map
61
        this.date = (Date) avoirRow.getObject("DATE");
62
        this.nom = avoirRow.getObject("NOM").toString();
149 ilm 63
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
64
        this.putValue("NOM", "Avoir fournisseur : " + avoirRow.getString("NUMERO") + " " + rowFourn.getString("NOM"));
65
        this.putValue("ID_JOURNAL", GenerationMvtAvoirFournisseur.journal);
66
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));
18 ilm 67
 
68
        // on cree un nouveau mouvement
69
        if (this.idMvt == 1) {
70
            getNewMouvement(GenerationMvtAvoirFournisseur.source, this.idAvoirFourn, 1, "Avoir Fournisseur : " + avoirRow.getString("NUMERO"));
71
        } else {
149 ilm 72
            this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 73
        }
74
 
75
        // generation des ecritures + maj des totaux du compte associe
76
 
77
        // compte Achat
78
        int idCompteAchat = avoirRow.getInt("ID_COMPTE_PCE");
79
 
80
        if (idCompteAchat <= 1) {
81
            idCompteAchat = rowPrefsCompte.getInt("ID_COMPTE_PCE_ACHAT");
82
            if (idCompteAchat <= 1) {
83
                try {
84
                    idCompteAchat = ComptePCESQLElement.getIdComptePceDefault("Achats");
85
                } catch (Exception e) {
86
                    e.printStackTrace();
87
                }
88
            }
89
        }
149 ilm 90
        this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteAchat));
91
        this.putValue("DEBIT", Long.valueOf(0));
92
        this.putValue("CREDIT", Long.valueOf(prixHT.getLongValue()));
93 ilm 93
        SQLRow rowEcr = ajoutEcriture();
94
        // addAssocAnalytiqueFromProvider(rowEcr, avoirRow);
18 ilm 95
 
156 ilm 96
        SQLRowAccessor taxe = TaxeCache.getCache().getRowFromId(avoirRow.getForeignID("ID_TAXE"));
73 ilm 97
        if (prixTVA.getLongValue() > 0) {
98
            // compte TVA
99
            int idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_ACHAT");
100
            if (avoirRow.getBoolean("IMMO")) {
101
                idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_IMMO");
102
                if (idCompteTVA <= 1) {
103
                    idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVAImmo");
18 ilm 104
                }
73 ilm 105
            } else {
156 ilm 106
                if (rowFourn.getBoolean("UE")) {
107
                    idCompteTVA = taxe.getForeignID("ID_COMPTE_PCE_DED_INTRA");
108
                    if (idCompteTVA <= 1) {
109
                        idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
110
                        if (idCompteTVA <= 1) {
111
                            idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
112
                        }
113
                    }
114
                } else {
115
                    idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_ACHAT");
116
                    if (idCompteTVA <= 1) {
117
                        idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVADeductible");
118
                    }
73 ilm 119
                }
120
            }
18 ilm 121
 
149 ilm 122
            this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteTVA));
123
            this.putValue("DEBIT", Long.valueOf(0));
124
            this.putValue("CREDIT", Long.valueOf(prixTVA.getLongValue()));
73 ilm 125
            ajoutEcriture();
18 ilm 126
 
73 ilm 127
            if (rowFourn.getBoolean("UE")) {
156 ilm 128
                int idCompteTVAIntra = taxe.getForeignID("ID_COMPTE_PCE_COLLECTE_INTRA");
73 ilm 129
                if (idCompteTVAIntra <= 1) {
156 ilm 130
                    idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
131
                    if (idCompteTVAIntra <= 1) {
132
                        idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
133
                    }
18 ilm 134
                }
149 ilm 135
                this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteTVAIntra));
136
                this.putValue("DEBIT", Long.valueOf(prixTVA.getLongValue()));
137
                this.putValue("CREDIT", Long.valueOf(0));
73 ilm 138
                ajoutEcriture();
18 ilm 139
            }
73 ilm 140
        }
141
        // compte Fournisseur
142
        int idCompteFourn = rowFourn.getInt("ID_COMPTE_PCE");
143
        if (idCompteFourn <= 1) {
144
            idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
18 ilm 145
            if (idCompteFourn <= 1) {
73 ilm 146
                idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
18 ilm 147
            }
73 ilm 148
        }
149 ilm 149
        this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteFourn));
150
        this.putValue("DEBIT", Long.valueOf(prixTTC.getLongValue()));
151
        this.putValue("CREDIT", Long.valueOf(0));
73 ilm 152
        ajoutEcriture();
18 ilm 153
 
73 ilm 154
        // Mise à jour de mouvement associé à la facture d'avoir
155
        SQLRowValues valAvoir = new SQLRowValues(avoirFournTable);
156
        valAvoir.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 157
 
73 ilm 158
        if (valAvoir.getInvalid() == null) {
18 ilm 159
 
73 ilm 160
            valAvoir.update(this.idAvoirFourn);
80 ilm 161
            displayMvtNumber();
73 ilm 162
        }
18 ilm 163
 
73 ilm 164
        // if (avoirRow.getInt("ID_MODE_REGLEMENT") > 1) {
165
        // new GenerationMvtReglementAvoir(this.idAvoirFourn, this.idMvt);
166
        // } else {
167
        // valAvoir.put("SOLDE", Boolean.FALSE);
168
        // try {
169
        // valAvoir.update(this.idAvoirClient);
170
        // } catch (SQLException e) {
171
        // e.printStackTrace();
172
        // }
173
        // }
18 ilm 174
 
175
        return this.idMvt;
176
    }
177
}