OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 90 | 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
 
44
    public GenerationMvtAvoirClient(int idAvoirClient) {
45
        this.idMvt = 1;
46
        this.idAvoirClient = idAvoirClient;
47
    }
48
 
49
    public GenerationMvtAvoirClient(int idAvoirClient, int idMvt) {
50
        this.idMvt = idMvt;
51
        this.idAvoirClient = idAvoirClient;
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
 
65
        // iniatilisation des valeurs de la map
66
        this.date = (Date) avoirRow.getObject("DATE");
67
        this.nom = avoirRow.getObject("NOM").toString();
68
        this.mEcritures.put("DATE", new java.sql.Date(this.date.getTime()));
73 ilm 69
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
70
        provider.putLabel(avoirRow, this.mEcritures);
18 ilm 71
 
72
        this.mEcritures.put("ID_JOURNAL", GenerationMvtAvoirClient.journal);
73
        if (affacturage) {
74
 
75
            int idJrnlFactor = rowPrefsCompte.getInt("ID_JOURNAL_FACTOR");
76
            if (idJrnlFactor > 1) {
77
                this.mEcritures.put("ID_JOURNAL", idJrnlFactor);
78
            }
79
        }
80
 
81
        this.mEcritures.put("ID_MOUVEMENT", Integer.valueOf(1));
82
 
83
        // on cree un nouveau mouvement
84
        if (this.idMvt == 1) {
73 ilm 85
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
86
            provider.putPieceLabel(avoirRow, rowValsPiece);
87
            getNewMouvement(GenerationMvtAvoirClient.source, this.idAvoirClient, 1, rowValsPiece);
18 ilm 88
        } else {
89
            this.mEcritures.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
73 ilm 90
 
91
            SQLRowValues rowValsPiece = pieceTable.getTable("MOUVEMENT").getRow(idMvt).getForeign("ID_PIECE").asRowValues();
92
            provider.putPieceLabel(avoirRow, rowValsPiece);
93
            rowValsPiece.update();
18 ilm 94
        }
73 ilm 95
        BigDecimal portHT = BigDecimal.valueOf(avoirRow.getLong("PORT_HT")).movePointLeft(2);
18 ilm 96
 
73 ilm 97
        TotalCalculator calc = getValuesFromElement(avoirRow, avoirRow.getTable().getTable("AVOIR_CLIENT_ELEMENT"), portHT, null, null);
18 ilm 98
 
73 ilm 99
        for (SQLRowAccessor row : calc.getMapHt().keySet()) {
100
            long b = calc.getMapHt().get(row).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
101
            if (b != 0) {
102
                this.mEcritures.put("ID_COMPTE_PCE", row.getID());
103
                this.mEcritures.put("DEBIT", Long.valueOf(b));
104
                this.mEcritures.put("CREDIT", Long.valueOf(0));
83 ilm 105
                SQLRow rowEcr = ajoutEcriture();
106
                addAssocAnalytiqueFromProvider(rowEcr, avoirRow);
18 ilm 107
            }
73 ilm 108
        }
18 ilm 109
 
73 ilm 110
        // compte TVA
111
        Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
112
        for (SQLRowAccessor rowAc : tvaMap.keySet()) {
113
            long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
114
            if (longValue != 0) {
115
                this.mEcritures.put("ID_COMPTE_PCE", rowAc.getID());
116
                this.mEcritures.put("DEBIT", Long.valueOf(longValue));
117
                this.mEcritures.put("CREDIT", Long.valueOf(0));
118
                ajoutEcriture();
18 ilm 119
            }
73 ilm 120
        }
18 ilm 121
 
73 ilm 122
        // compte Clients
123
        int idCompteClient = avoirRow.getForeignRow("ID_CLIENT").getInt("ID_COMPTE_PCE");
124
        if (idCompteClient <= 1) {
125
            idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
18 ilm 126
            if (idCompteClient <= 1) {
73 ilm 127
                idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
18 ilm 128
            }
73 ilm 129
        }
67 ilm 130
 
73 ilm 131
        this.mEcritures.put("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
132
        this.mEcritures.put("DEBIT", Long.valueOf(0));
133
        long ttc = calc.getTotalTTC().movePointRight(2).longValue();
134
        this.mEcritures.put("CREDIT", Long.valueOf(ttc));
135
        ajoutEcriture();
18 ilm 136
 
73 ilm 137
        // Mise à jour de mouvement associé à la facture d'avoir
138
        SQLRowValues valAvoir = new SQLRowValues(avoirClientTable);
139
        valAvoir.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 140
 
73 ilm 141
        if (valAvoir.getInvalid() == null) {
18 ilm 142
 
73 ilm 143
            valAvoir.update(this.idAvoirClient);
144
        }
18 ilm 145
 
73 ilm 146
        if (affacturage) {
147
            this.mEcritures.put("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
148
            this.mEcritures.put("DEBIT", Long.valueOf(ttc));
149
            this.mEcritures.put("CREDIT", Long.valueOf(0));
150
            ajoutEcriture();
18 ilm 151
 
73 ilm 152
            // compte Factor
153
            int idComptefactor = rowPrefsCompte.getInt("ID_COMPTE_PCE_FACTOR");
154
            if (idComptefactor <= 1) {
155
                idComptefactor = rowPrefsCompte.getInt("ID_COMPTE_PCE_FACTOR");
18 ilm 156
                if (idComptefactor <= 1) {
73 ilm 157
                    try {
158
                        idComptefactor = ComptePCESQLElement.getIdComptePceDefault("Factor");
159
                    } catch (Exception e) {
160
                        e.printStackTrace();
18 ilm 161
                    }
162
                }
163
            }
164
 
73 ilm 165
            this.mEcritures.put("ID_COMPTE_PCE", idComptefactor);
166
            this.mEcritures.put("DEBIT", Long.valueOf(0));
167
            this.mEcritures.put("CREDIT", Long.valueOf(ttc));
168
            ajoutEcriture();
169
        }
18 ilm 170
 
73 ilm 171
        if (avoirRow.getInt("ID_MODE_REGLEMENT") > 1) {
172
            new GenerationMvtReglementAvoir(this.idAvoirClient, this.idMvt);
173
        } else {
174
            valAvoir.put("SOLDE", Boolean.FALSE);
175
            valAvoir.update(this.idAvoirClient);
80 ilm 176
            displayMvtNumber();
18 ilm 177
        }
178
 
179
        return this.idMvt;
180
    }
181
}