OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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;
73 ilm 18
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
19
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
18 ilm 20
import org.openconcerto.erp.model.PrixHT;
21
import org.openconcerto.erp.model.PrixTTC;
22
import org.openconcerto.sql.model.SQLRow;
142 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.utils.ExceptionHandler;
27
 
28
import java.util.Date;
29
 
30
public class GenerationMvtSaisieAchat extends GenerationEcritures implements Runnable {
31
 
73 ilm 32
    public static final String ID = "accounting.records.supply.order";
33
 
93 ilm 34
    private final SQLRow saisieRow;
18 ilm 35
    private static final String source = "SAISIE_ACHAT";
36
    private static final Integer journal = new Integer(JournalSQLElement.ACHATS);
37
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
38
    private static final SQLTable tableFournisseur = base.getTable("FOURNISSEUR");
39
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
40
 
142 ilm 41
    public GenerationMvtSaisieAchat(SQLRow row) {
93 ilm 42
        setRowAnalytiqueSource(row);
43
        this.saisieRow = row;
142 ilm 44
        this.idMvt = row.getInt("ID_MOUVEMENT");
18 ilm 45
    }
46
 
73 ilm 47
    public void genereMouvement() throws Exception {
93 ilm 48
        SQLRow rowFournisseur = tableFournisseur.getRow(this.saisieRow.getInt("ID_FOURNISSEUR"));
18 ilm 49
 
50
        // iniatilisation des valeurs de la map
51
        this.date = (Date) saisieRow.getObject("DATE");
182 ilm 52
        this.nom = " Fourn. : " + rowFournisseur.getString("NOM") + " " + saisieRow.getObject("NOM").toString() + " Facture : " + this.saisieRow.getObject("NUMERO_FACTURE").toString();
149 ilm 53
        this.putValue("DATE", this.date);
73 ilm 54
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
55
        provider.putLabel(saisieRow, this.mEcritures);
56
 
149 ilm 57
        this.putValue("ID_JOURNAL", GenerationMvtSaisieAchat.journal);
58
        this.putValue("ID_MOUVEMENT", new Integer(1));
18 ilm 59
 
60
        // Calcul des montants
93 ilm 61
        PrixTTC prixTTC = new PrixTTC(this.saisieRow.getLong("MONTANT_TTC"));
62
        PrixHT prixTVA = new PrixHT(this.saisieRow.getLong("MONTANT_TVA"));
63
        PrixHT prixHT = new PrixHT(this.saisieRow.getLong("MONTANT_HT"));
18 ilm 64
 
65
        // on calcule le nouveau numero de mouvement
66
        if (this.idMvt == 1) {
73 ilm 67
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
68
            provider.putPieceLabel(saisieRow, rowValsPiece);
93 ilm 69
            getNewMouvement(GenerationMvtSaisieAchat.source, this.saisieRow.getID(), 1, rowValsPiece);
18 ilm 70
        } else {
73 ilm 71
            SQLRowValues rowValsPiece = pieceTable.getTable("MOUVEMENT").getRow(idMvt).getForeign("ID_PIECE").asRowValues();
93 ilm 72
            provider.putPieceLabel(this.saisieRow, rowValsPiece);
73 ilm 73
            rowValsPiece.update();
74
 
149 ilm 75
            this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
18 ilm 76
        }
77
 
78
        // generation des ecritures + maj des totaux du compte associe
79
 
80
        // compte Achat
93 ilm 81
        int idCompteAchat = this.saisieRow.getInt("ID_COMPTE_PCE");
18 ilm 82
 
83
        if (idCompteAchat <= 1) {
93 ilm 84
            idCompteAchat = this.rowPrefsCompte.getInt("ID_COMPTE_PCE_ACHAT");
18 ilm 85
            if (idCompteAchat <= 1) {
73 ilm 86
                idCompteAchat = ComptePCESQLElement.getIdComptePceDefault("Achats");
18 ilm 87
            }
88
        }
149 ilm 89
        this.putValue("ID_COMPTE_PCE", new Integer(idCompteAchat));
90
        this.putValue("DEBIT", new Long(prixHT.getLongValue()));
91
        this.putValue("CREDIT", new Long(0));
83 ilm 92
        SQLRow rowEcr = ajoutEcriture();
18 ilm 93
 
93 ilm 94
        // addAssocAnalytiqueFromProvider(rowEcr, saisieRow);
83 ilm 95
 
18 ilm 96
        // compte TVA
97
        if (prixTVA.getLongValue() > 0) {
98
            int idCompteTVA;
99
            if (saisieRow.getBoolean("IMMO")) {
100
                idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_IMMO");
101
                if (idCompteTVA <= 1) {
73 ilm 102
                    idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVAImmo");
18 ilm 103
                }
104
            } else {
142 ilm 105
                SQLRowAccessor rowCptTva = this.saisieRow.getForeign("ID_TAXE").getForeign(rowFournisseur.getBoolean("UE") ? "ID_COMPTE_PCE_DED_INTRA" : "ID_COMPTE_PCE_DED");
106
                if (rowCptTva == null || rowCptTva.isUndefined()) {
107
                    idCompteTVA = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_ACHAT");
108
                    if (idCompteTVA <= 1) {
109
                        idCompteTVA = ComptePCESQLElement.getIdComptePceDefault("TVADeductible");
110
                    }
111
                } else {
112
                    idCompteTVA = rowCptTva.getID();
18 ilm 113
                }
114
            }
149 ilm 115
            this.putValue("ID_COMPTE_PCE", new Integer(idCompteTVA));
116
            this.putValue("DEBIT", new Long(prixTVA.getLongValue()));
117
            this.putValue("CREDIT", new Long(0));
18 ilm 118
            ajoutEcriture();
119
 
120
            if (rowFournisseur.getBoolean("UE")) {
142 ilm 121
                SQLRowAccessor rowCptTva = this.saisieRow.getForeign("ID_TAXE").getForeign("ID_COMPTE_PCE_COLLECTE_INTRA");
122
                int idCompteTVAIntra;
123
                if (rowCptTva == null || rowCptTva.isUndefined()) {
124
                    idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
125
 
126
                    if (idCompteTVAIntra <= 1) {
127
                        idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
128
                    }
129
                } else {
130
                    idCompteTVAIntra = rowCptTva.getID();
18 ilm 131
                }
149 ilm 132
                this.putValue("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
133
                this.putValue("DEBIT", new Long(0));
134
                this.putValue("CREDIT", new Long(prixTVA.getLongValue()));
18 ilm 135
                ajoutEcriture();
136
            }
137
        }
138
 
139
        // compte Fournisseurs
140
        int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
141
 
142
        if (idCompteFourn <= 1) {
143
            idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
144
            if (idCompteFourn <= 1) {
73 ilm 145
                idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
18 ilm 146
            }
147
        }
149 ilm 148
        this.putValue("ID_COMPTE_PCE", new Integer(idCompteFourn));
149
        this.putValue("DEBIT", new Long(0));
18 ilm 150
        if (rowFournisseur.getBoolean("UE")) {
149 ilm 151
            this.putValue("CREDIT", new Long(prixHT.getLongValue()));
18 ilm 152
        } else {
149 ilm 153
            this.putValue("CREDIT", new Long(prixTTC.getLongValue()));
18 ilm 154
        }
155
        ajoutEcriture();
156
 
93 ilm 157
        new GenerationMvtReglementAchat(this.saisieRow, this.idMvt);
18 ilm 158
 
159
        // Mise à jour de la clef etrangere mouvement sur la saisie achat
93 ilm 160
        SQLRowValues valAchat = this.saisieRow.createEmptyUpdateRow();
161
        valAchat.put("ID_MOUVEMENT", new Integer(this.idMvt));
162
        valAchat.update();
163
        displayMvtNumber();
18 ilm 164
    }
165
 
166
    public void run() {
167
        try {
168
            genereMouvement();
73 ilm 169
        } catch (Exception e) {
18 ilm 170
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
171
        }
172
    }
173
}