OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
80 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.common.ui.TotalCalculator;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.utils.ExceptionHandler;
24
 
25
import java.math.BigDecimal;
26
import java.math.RoundingMode;
27
import java.util.Date;
28
import java.util.Map;
29
 
30
public class GenerationMvtFactureFournisseur extends GenerationEcritures implements Runnable {
31
 
32
    public static final String ID = "accounting.records.supply.order";
33
 
34
    private int idFacture;
35
    private static final String source = "FACTURE_FOURNISSEUR";
36
    private static final Integer journal = new Integer(JournalSQLElement.ACHATS);
37
    private static final SQLTable tableFacture = base.getTable("FACTURE_FOURNISSEUR");
38
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
39
    private static final SQLTable tableFournisseur = base.getTable("FOURNISSEUR");
40
    private static final SQLTable tableMvt = base.getTable("MOUVEMENT");
41
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
42
 
93 ilm 43
    public GenerationMvtFactureFournisseur(SQLRow row, int idMvt) {
44
        setRowAnalytiqueSource(row);
45
        this.idFacture = row.getID();
80 ilm 46
        this.idMvt = idMvt;
47
        (new Thread(GenerationMvtFactureFournisseur.this)).start();
48
    }
49
 
93 ilm 50
    public GenerationMvtFactureFournisseur(SQLRow row) {
51
        this(row, 1);
80 ilm 52
    }
53
 
54
    public void genereMouvement() throws Exception {
55
 
93 ilm 56
        SQLRow saisieRow = getRowAnalytiqueSource();
80 ilm 57
        // SQLRow taxeRow = base.getTable("TAXE").getRow(saisieRow.getInt("ID_TAXE"));
58
 
59
        SQLRow rowFournisseur = tableFournisseur.getRow(saisieRow.getInt("ID_FOURNISSEUR"));
60
 
61
        // iniatilisation des valeurs de la map
62
        this.date = (Date) saisieRow.getObject("DATE");
63
        this.nom = "Achat : " + rowFournisseur.getString("NOM") + " Facture : " + saisieRow.getObject("NUMERO").toString() + " " + saisieRow.getObject("NOM").toString();
64
        this.mEcritures.put("DATE", this.date);
65
        // AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
66
        // provider.putLabel(saisieRow, this.mEcritures);
67
        this.mEcritures.put("NOM", nom);
68
        this.mEcritures.put("ID_JOURNAL", GenerationMvtFactureFournisseur.journal);
69
        this.mEcritures.put("ID_MOUVEMENT", new Integer(1));
70
 
71
        // on calcule le nouveau numero de mouvement
72
        if (this.idMvt == 1) {
73
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
74
            // provider.putPieceLabel(saisieRow, rowValsPiece);
75
            rowValsPiece.put("NOM", saisieRow.getObject("NUMERO").toString());
76
            getNewMouvement(GenerationMvtFactureFournisseur.source, this.idFacture, 1, rowValsPiece);
77
        } else {
78
            SQLRowValues rowValsPiece = pieceTable.getTable("MOUVEMENT").getRow(idMvt).getForeign("ID_PIECE").asRowValues();
79
            // provider.putPieceLabel(saisieRow, rowValsPiece);
80
            rowValsPiece.put("NOM", saisieRow.getObject("NUMERO").toString());
81
            rowValsPiece.update();
82
 
83
            this.mEcritures.put("ID_MOUVEMENT", new Integer(this.idMvt));
84
        }
85
 
86
        // generation des ecritures + maj des totaux du compte associe
87
 
88
        // compte Achat
89
        SQLRow rowCompteAchat = saisieRow.getForeign("ID_COMPTE_PCE");
90
 
91
        if (rowCompteAchat == null || rowCompteAchat.isUndefined()) {
92
            rowCompteAchat = rowPrefsCompte.getForeign("ID_COMPTE_PCE_ACHAT");
93
            if (rowCompteAchat == null || rowCompteAchat.isUndefined()) {
94
                rowCompteAchat = ComptePCESQLElement.getRowComptePceDefault("Achats");
95
            }
96
        }
97
 
142 ilm 98
        TotalCalculator calc = getValuesFromElement(rowFournisseur.getBoolean("UE"), true, "T_PA_HT", saisieRow, saisieRow.getTable().getTable("FACTURE_FOURNISSEUR_ELEMENT"), BigDecimal.ZERO, null,
99
                null, rowCompteAchat);
80 ilm 100
 
101
        long ttcLongValue = calc.getTotalTTC().movePointRight(2).longValue();
102
        long htLongValue = calc.getTotalHT().movePointRight(2).longValue();
103
 
104
        for (SQLRowAccessor row : calc.getMapHt().keySet()) {
105
            long b = calc.getMapHt().get(row).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
106
            if (b != 0) {
107
                this.mEcritures.put("ID_COMPTE_PCE", Integer.valueOf(row.getID()));
108
                this.mEcritures.put("CREDIT", Long.valueOf(0));
109
                this.mEcritures.put("DEBIT", Long.valueOf(b));
83 ilm 110
                SQLRow rowEcr = ajoutEcriture();
93 ilm 111
                // addAssocAnalytiqueFromProvider(rowEcr, saisieRow);
80 ilm 112
            }
113
        }
114
 
115
        Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
116
        for (SQLRowAccessor rowAc : tvaMap.keySet()) {
117
            long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
118
            if (longValue != 0) {
119
                this.mEcritures.put("ID_COMPTE_PCE", rowAc.getID());
120
                this.mEcritures.put("CREDIT", Long.valueOf(0));
121
                this.mEcritures.put("DEBIT", longValue);
122
                ajoutEcriture();
123
 
142 ilm 124
//                if (rowFournisseur.getBoolean("UE")) {
125
//                    int idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
126
//                    if (idCompteTVAIntra <= 1) {
127
//                        idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
128
//                    }
129
//                    this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
130
//                    this.mEcritures.put("DEBIT", new Long(0));
131
//                    this.mEcritures.put("CREDIT", new Long(longValue));
132
//                    ajoutEcriture();
133
//                }
80 ilm 134
            }
135
        }
142 ilm 136
 
137
        Map<SQLRowAccessor, BigDecimal> tvaMapIntra = calc.getMapHtTVAIntra();
138
        for (SQLRowAccessor rowAc : tvaMapIntra.keySet()) {
139
            long longValue = tvaMapIntra.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
140
            if (longValue != 0) {
141
                this.mEcritures.put("ID_COMPTE_PCE", rowAc.getID());
142
                this.mEcritures.put("DEBIT", Long.valueOf(0));
143
                this.mEcritures.put("CREDIT", longValue);
144
                ajoutEcriture();
80 ilm 145
 
142 ilm 146
//                if (rowFournisseur.getBoolean("UE")) {
147
//                    int idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
148
//                    if (idCompteTVAIntra <= 1) {
149
//                        idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
150
//                    }
151
//                    this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
152
//                    this.mEcritures.put("DEBIT", new Long(0));
153
//                    this.mEcritures.put("CREDIT", new Long(longValue));
154
//                    ajoutEcriture();
155
//                }
156
            }
157
        }
158
 
80 ilm 159
        // compte Fournisseurs
160
        int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
161
 
162
        if (idCompteFourn <= 1) {
163
            idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
164
            if (idCompteFourn <= 1) {
165
                idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
166
            }
167
        }
168
        this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteFourn));
169
        this.mEcritures.put("DEBIT", new Long(0));
170
        if (rowFournisseur.getBoolean("UE")) {
171
            this.mEcritures.put("CREDIT", new Long(htLongValue));
172
        } else {
173
            this.mEcritures.put("CREDIT", new Long(ttcLongValue));
174
        }
175
        ajoutEcriture();
176
 
177
        new GenerationMvtReglementFactureFournisseur(this.idFacture, this.idMvt);
178
 
179
        // Mise à jour de la clef etrangere mouvement sur la saisie achat
180
        SQLRowValues valEcriture = new SQLRowValues(tableFacture);
181
        valEcriture.put("ID_MOUVEMENT", new Integer(this.idMvt));
182
 
183
        if (valEcriture.getInvalid() == null) {
184
            // ajout de l'ecriture
185
            valEcriture.update(this.idFacture);
186
            displayMvtNumber();
187
        }
188
 
189
    }
190
 
191
    public void run() {
192
        try {
193
            genereMouvement();
194
        } catch (Exception e) {
195
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
196
        }
197
    }
198
}