OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Rev 142 | 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
 
98
        TotalCalculator calc = getValuesFromElement(true, "T_PA_HT", saisieRow, saisieRow.getTable().getTable("FACTURE_FOURNISSEUR_ELEMENT"), BigDecimal.ZERO, null, null, rowCompteAchat);
99
 
100
        long ttcLongValue = calc.getTotalTTC().movePointRight(2).longValue();
101
        long htLongValue = calc.getTotalHT().movePointRight(2).longValue();
102
 
103
        for (SQLRowAccessor row : calc.getMapHt().keySet()) {
104
            long b = calc.getMapHt().get(row).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
105
            if (b != 0) {
106
                this.mEcritures.put("ID_COMPTE_PCE", Integer.valueOf(row.getID()));
107
                this.mEcritures.put("CREDIT", Long.valueOf(0));
108
                this.mEcritures.put("DEBIT", Long.valueOf(b));
83 ilm 109
                SQLRow rowEcr = ajoutEcriture();
93 ilm 110
                // addAssocAnalytiqueFromProvider(rowEcr, saisieRow);
80 ilm 111
            }
112
        }
113
 
114
        Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
115
        for (SQLRowAccessor rowAc : tvaMap.keySet()) {
116
            long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
117
            if (longValue != 0) {
118
                this.mEcritures.put("ID_COMPTE_PCE", rowAc.getID());
119
                this.mEcritures.put("CREDIT", Long.valueOf(0));
120
                this.mEcritures.put("DEBIT", longValue);
121
                ajoutEcriture();
122
 
123
                if (rowFournisseur.getBoolean("UE")) {
124
                    int idCompteTVAIntra = rowPrefsCompte.getInt("ID_COMPTE_PCE_TVA_INTRA");
125
                    if (idCompteTVAIntra <= 1) {
126
                        idCompteTVAIntra = ComptePCESQLElement.getIdComptePceDefault("TVAIntraComm");
127
                    }
128
                    this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteTVAIntra));
129
                    this.mEcritures.put("DEBIT", new Long(0));
130
                    this.mEcritures.put("CREDIT", new Long(longValue));
131
                    ajoutEcriture();
132
                }
133
            }
134
        }
135
 
136
        // compte Fournisseurs
137
        int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
138
 
139
        if (idCompteFourn <= 1) {
140
            idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
141
            if (idCompteFourn <= 1) {
142
                idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
143
            }
144
        }
145
        this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteFourn));
146
        this.mEcritures.put("DEBIT", new Long(0));
147
        if (rowFournisseur.getBoolean("UE")) {
148
            this.mEcritures.put("CREDIT", new Long(htLongValue));
149
        } else {
150
            this.mEcritures.put("CREDIT", new Long(ttcLongValue));
151
        }
152
        ajoutEcriture();
153
 
154
        new GenerationMvtReglementFactureFournisseur(this.idFacture, this.idMvt);
155
 
156
        // Mise à jour de la clef etrangere mouvement sur la saisie achat
157
        SQLRowValues valEcriture = new SQLRowValues(tableFacture);
158
        valEcriture.put("ID_MOUVEMENT", new Integer(this.idMvt));
159
 
160
        if (valEcriture.getInvalid() == null) {
161
            // ajout de l'ecriture
162
            valEcriture.update(this.idFacture);
163
            displayMvtNumber();
164
        }
165
 
166
    }
167
 
168
    public void run() {
169
        try {
170
            genereMouvement();
171
        } catch (Exception e) {
172
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
173
        }
174
    }
175
}