OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 180 | 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();
149 ilm 64
        this.putValue("DATE", this.date);
80 ilm 65
        // AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
66
        // provider.putLabel(saisieRow, this.mEcritures);
149 ilm 67
        this.putValue("NOM", nom);
68
        this.putValue("ID_JOURNAL", GenerationMvtFactureFournisseur.journal);
69
        this.putValue("ID_MOUVEMENT", new Integer(1));
80 ilm 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
 
149 ilm 83
            this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
80 ilm 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
        }
149 ilm 97
        BigDecimal portHT = BigDecimal.valueOf(saisieRow.getLong("PORT_HT")).movePointLeft(2);
98
        TotalCalculator calc = getValuesFromElement(rowFournisseur.getBoolean("UE"), true, "T_PA_HT", saisieRow, saisieRow.getTable().getTable("FACTURE_FOURNISSEUR_ELEMENT"), portHT,
156 ilm 99
                saisieRow.getForeign("ID_TAXE_PORT"), BigDecimal.ZERO, null, 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) {
149 ilm 107
                this.putValue("ID_COMPTE_PCE", Integer.valueOf(row.getID()));
108
                this.putValue("CREDIT", Long.valueOf(0));
109
                this.putValue("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) {
149 ilm 119
                this.putValue("ID_COMPTE_PCE", rowAc.getID());
120
                this.putValue("CREDIT", Long.valueOf(0));
121
                this.putValue("DEBIT", longValue);
80 ilm 122
                ajoutEcriture();
123
 
149 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.putValue("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
130
                // this.putValue("DEBIT", new Long(0));
131
                // this.putValue("CREDIT", new Long(longValue));
132
                // ajoutEcriture();
133
                // }
80 ilm 134
            }
135
        }
149 ilm 136
 
142 ilm 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) {
149 ilm 141
                this.putValue("ID_COMPTE_PCE", rowAc.getID());
142
                this.putValue("DEBIT", Long.valueOf(0));
143
                this.putValue("CREDIT", longValue);
142 ilm 144
                ajoutEcriture();
80 ilm 145
 
149 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.putValue("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
152
                // this.putValue("DEBIT", new Long(0));
153
                // this.putValue("CREDIT", new Long(longValue));
154
                // ajoutEcriture();
155
                // }
142 ilm 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
        }
149 ilm 168
        this.putValue("ID_COMPTE_PCE", new Integer(idCompteFourn));
174 ilm 169
        if (rowFournisseur.getTable().getTable("ECRITURE").contains("CODE_CLIENT")) {
170
            this.putValue("CODE_CLIENT", rowFournisseur.getString("CODE"));
171
        }
172
 
149 ilm 173
        this.putValue("DEBIT", new Long(0));
80 ilm 174
        if (rowFournisseur.getBoolean("UE")) {
149 ilm 175
            this.putValue("CREDIT", new Long(htLongValue));
80 ilm 176
        } else {
149 ilm 177
            this.putValue("CREDIT", new Long(ttcLongValue));
80 ilm 178
        }
179
        ajoutEcriture();
180
 
181
        new GenerationMvtReglementFactureFournisseur(this.idFacture, this.idMvt);
182
 
183
        // Mise à jour de la clef etrangere mouvement sur la saisie achat
184
        SQLRowValues valEcriture = new SQLRowValues(tableFacture);
185
        valEcriture.put("ID_MOUVEMENT", new Integer(this.idMvt));
186
 
187
        if (valEcriture.getInvalid() == null) {
188
            // ajout de l'ecriture
189
            valEcriture.update(this.idFacture);
190
            displayMvtNumber();
191
        }
192
 
193
    }
194
 
195
    public void run() {
196
        try {
197
            genereMouvement();
198
        } catch (Exception e) {
199
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
200
        }
201
    }
202
}