OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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