OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
185 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.config.ComptaPropsConfiguration;
67 ilm 17
import org.openconcerto.erp.core.common.ui.TotalCalculator;
18 ilm 18
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
19
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
73 ilm 20
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
21
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
185 ilm 22
import org.openconcerto.erp.preferences.DefaultNXProps;
18 ilm 23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.model.SQLRow;
67 ilm 25
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
28
 
67 ilm 29
import java.math.BigDecimal;
30
import java.math.RoundingMode;
18 ilm 31
import java.util.Date;
61 ilm 32
import java.util.Map;
18 ilm 33
 
34
public class GenerationMvtAvoirClient extends GenerationEcritures {
35
 
73 ilm 36
    public static final String ID = "accounting.records.sales.credit";
37
 
18 ilm 38
    private static final String source = "AVOIR_CLIENT";
39
    // TODO dans quel journal les ecritures des avoirs? OD?
40
    private static final Integer journal = Integer.valueOf(JournalSQLElement.VENTES);
41
    private int idAvoirClient;
42
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
43
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
44
 
93 ilm 45
    public GenerationMvtAvoirClient(SQLRow row) {
46
        this(row, 1);
18 ilm 47
    }
48
 
93 ilm 49
    public GenerationMvtAvoirClient(SQLRow row, int idMvt) {
18 ilm 50
        this.idMvt = idMvt;
93 ilm 51
        setRowAnalytiqueSource(row);
52
        this.idAvoirClient = row.getID();
18 ilm 53
    }
54
 
73 ilm 55
    public int genereMouvement() throws Exception {
185 ilm 56
        boolean genEcrDisabled = DefaultNXProps.getInstance().getBooleanValue(GenerationMvtSaisieVenteFacture.NOT_GEN_ECRITURE, false);
18 ilm 57
 
58
        SQLTable avoirClientTable = base.getTable("AVOIR_CLIENT");
59
 
60
        SQLRow avoirRow = avoirClientTable.getRow(this.idAvoirClient);
61
 
19 ilm 62
        boolean affacturage = avoirRow.getBoolean("AFFACTURE");
18 ilm 63
 
64
        SQLRow rowClient;
65
            rowClient = avoirRow.getForeignRow("ID_CLIENT");
66
 
90 ilm 67
        if (ecritureTable.contains("CODE_CLIENT")) {
149 ilm 68
            this.putValue("CODE_CLIENT", rowClient.getString("CODE"));
90 ilm 69
        }
70
 
18 ilm 71
        // iniatilisation des valeurs de la map
72
        this.date = (Date) avoirRow.getObject("DATE");
73
        this.nom = avoirRow.getObject("NOM").toString();
149 ilm 74
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
73 ilm 75
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
76
        provider.putLabel(avoirRow, this.mEcritures);
18 ilm 77
 
149 ilm 78
        this.putValue("ID_JOURNAL", GenerationMvtAvoirClient.journal);
18 ilm 79
        if (affacturage) {
80
 
81
            int idJrnlFactor = rowPrefsCompte.getInt("ID_JOURNAL_FACTOR");
82
            if (idJrnlFactor > 1) {
149 ilm 83
                this.putValue("ID_JOURNAL", idJrnlFactor);
18 ilm 84
            }
85
        }
86
 
149 ilm 87
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));
18 ilm 88
 
89
        // on cree un nouveau mouvement
90
        if (this.idMvt == 1) {
73 ilm 91
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
92
            provider.putPieceLabel(avoirRow, rowValsPiece);
93
            getNewMouvement(GenerationMvtAvoirClient.source, this.idAvoirClient, 1, rowValsPiece);
18 ilm 94
        } else {
149 ilm 95
            this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
73 ilm 96
 
97
            SQLRowValues rowValsPiece = pieceTable.getTable("MOUVEMENT").getRow(idMvt).getForeign("ID_PIECE").asRowValues();
98
            provider.putPieceLabel(avoirRow, rowValsPiece);
99
            rowValsPiece.update();
18 ilm 100
        }
73 ilm 101
        BigDecimal portHT = BigDecimal.valueOf(avoirRow.getLong("PORT_HT")).movePointLeft(2);
18 ilm 102
 
94 ilm 103
        TotalCalculator calc;
104
        if (rowClient.getTable().contains("ID_COMPTE_PCE_PRODUIT") && !rowClient.isForeignEmpty("ID_COMPTE_PCE_PRODUIT")) {
156 ilm 105
            calc = getValuesFromElement(false, false, "T_PV_HT", avoirRow, avoirRow.getTable().getTable("AVOIR_CLIENT_ELEMENT"), portHT, null, BigDecimal.ZERO, null, null,
106
                    rowClient.getForeign("ID_COMPTE_PCE_PRODUIT"));
94 ilm 107
        } else {
156 ilm 108
            calc = getValuesFromElement(avoirRow, avoirRow.getTable().getTable("AVOIR_CLIENT_ELEMENT"), portHT, null, BigDecimal.ZERO, null, null);
94 ilm 109
        }
144 ilm 110
        Map<SQLRowAccessor, Map<SQLRowAccessor, BigDecimal>> taxeCompl = calc.getMapHtTaxeCompl();
18 ilm 111
 
73 ilm 112
        for (SQLRowAccessor row : calc.getMapHt().keySet()) {
113
            long b = calc.getMapHt().get(row).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
114
            if (b != 0) {
144 ilm 115
                long taxe = 0;
116
 
117
                if (taxeCompl.containsKey(row)) {
118
                    Map<SQLRowAccessor, BigDecimal> compl = taxeCompl.get(row);
119
                    for (SQLRowAccessor rowCompl : compl.keySet()) {
120
                        if (compl.get(rowCompl) != null) {
121
                            long taxeC = compl.get(rowCompl).multiply(rowCompl.getBigDecimal("POURCENT").movePointLeft(2)).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
149 ilm 122
                            this.putValue("ID_COMPTE_PCE", rowCompl.getForeignID("ID_COMPTE_PCE"));
123
                            this.putValue("CREDIT", Long.valueOf(0));
124
                            this.putValue("DEBIT", Long.valueOf(taxeC));
185 ilm 125
                            if (!genEcrDisabled) {
126
                                ajoutEcriture();
127
                            }
144 ilm 128
                            taxe += taxeC;
149 ilm 129
                            // this.putValue("ID_COMPTE_PCE",
144 ilm 130
                            // rowCompl.getForeignID("ID_COMPTE_PCE_PRODUITS"));
149 ilm 131
                            // this.putValue("CREDIT", Long.valueOf(taxeC));
132
                            // this.putValue("DEBIT", Long.valueOf(0));
144 ilm 133
                            // ajoutEcriture();
134
                        }
135
                    }
136
                }
137
 
149 ilm 138
                this.putValue("ID_COMPTE_PCE", row.getID());
139
                this.putValue("DEBIT", Long.valueOf(b - taxe));
140
                this.putValue("CREDIT", Long.valueOf(0));
185 ilm 141
                if (!genEcrDisabled) {
142
                    SQLRow rowEcr = ajoutEcriture();
143
                    // addAssocAnalytiqueFromProvider(rowEcr, avoirRow);
144
                }
18 ilm 145
            }
73 ilm 146
        }
18 ilm 147
 
73 ilm 148
        // compte TVA
149
        Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
150
        for (SQLRowAccessor rowAc : tvaMap.keySet()) {
151
            long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
152
            if (longValue != 0) {
149 ilm 153
                this.putValue("ID_COMPTE_PCE", rowAc.getID());
154
                this.putValue("DEBIT", Long.valueOf(longValue));
155
                this.putValue("CREDIT", Long.valueOf(0));
185 ilm 156
                if (!genEcrDisabled) {
157
                    ajoutEcriture();
158
                }
18 ilm 159
            }
73 ilm 160
        }
18 ilm 161
 
73 ilm 162
        // compte Clients
163
        int idCompteClient = avoirRow.getForeignRow("ID_CLIENT").getInt("ID_COMPTE_PCE");
164
        if (idCompteClient <= 1) {
165
            idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
18 ilm 166
            if (idCompteClient <= 1) {
73 ilm 167
                idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
18 ilm 168
            }
73 ilm 169
        }
67 ilm 170
 
149 ilm 171
        this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
172
        this.putValue("DEBIT", Long.valueOf(0));
73 ilm 173
        long ttc = calc.getTotalTTC().movePointRight(2).longValue();
149 ilm 174
        this.putValue("CREDIT", Long.valueOf(ttc));
185 ilm 175
        if (!genEcrDisabled) {
176
            ajoutEcriture();
177
        }
73 ilm 178
        // Mise à jour de mouvement associé à la facture d'avoir
179
        SQLRowValues valAvoir = new SQLRowValues(avoirClientTable);
180
        valAvoir.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 181
 
73 ilm 182
        if (valAvoir.getInvalid() == null) {
18 ilm 183
 
73 ilm 184
            valAvoir.update(this.idAvoirClient);
185
        }
18 ilm 186
 
73 ilm 187
        if (affacturage) {
149 ilm 188
            this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
189
            this.putValue("DEBIT", Long.valueOf(ttc));
190
            this.putValue("CREDIT", Long.valueOf(0));
185 ilm 191
            if (!genEcrDisabled) {
192
                ajoutEcriture();
193
            }
73 ilm 194
            // compte Factor
195
            int idComptefactor = rowPrefsCompte.getInt("ID_COMPTE_PCE_FACTOR");
196
            if (idComptefactor <= 1) {
197
                idComptefactor = rowPrefsCompte.getInt("ID_COMPTE_PCE_FACTOR");
18 ilm 198
                if (idComptefactor <= 1) {
73 ilm 199
                    try {
200
                        idComptefactor = ComptePCESQLElement.getIdComptePceDefault("Factor");
201
                    } catch (Exception e) {
202
                        e.printStackTrace();
18 ilm 203
                    }
204
                }
205
            }
206
 
149 ilm 207
            this.putValue("ID_COMPTE_PCE", idComptefactor);
208
            this.putValue("DEBIT", Long.valueOf(0));
209
            this.putValue("CREDIT", Long.valueOf(ttc));
185 ilm 210
            if (!genEcrDisabled) {
211
                ajoutEcriture();
212
            }
73 ilm 213
        }
18 ilm 214
 
73 ilm 215
        if (avoirRow.getInt("ID_MODE_REGLEMENT") > 1) {
93 ilm 216
            new GenerationMvtReglementAvoir(avoirRow, this.idMvt);
73 ilm 217
        } else {
218
            valAvoir.put("SOLDE", Boolean.FALSE);
219
            valAvoir.update(this.idAvoirClient);
80 ilm 220
            displayMvtNumber();
18 ilm 221
        }
222
 
223
        return this.idMvt;
224
    }
225
}