OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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