OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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
 
180 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
180 ilm 19
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.element.SQLElement;
22
import org.openconcerto.sql.model.SQLRow;
144 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLTable;
151 ilm 26
import org.openconcerto.utils.StringUtils;
18 ilm 27
 
28
import java.sql.SQLException;
29
import java.sql.Timestamp;
151 ilm 30
import java.util.ArrayList;
18 ilm 31
import java.util.Date;
151 ilm 32
import java.util.List;
18 ilm 33
 
34
public class GenerationMvtReglementChequeClient extends GenerationEcritures {
35
 
36
    private long montant;
37
 
38
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
39
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
40
    private int idCheque;
144 ilm 41
    private SQLRowAccessor banque;
18 ilm 42
 
144 ilm 43
    public GenerationMvtReglementChequeClient(int idMvt, long montant, Date d, int idCheque, String s, SQLRowAccessor banque) {
73 ilm 44
        this.montant = montant;
45
        this.date = d;
46
        this.idMvt = idMvt;
47
        this.idCheque = idCheque;
144 ilm 48
        this.banque = banque;
80 ilm 49
        SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
50
        String num = "";
51
        if (chequeRow != null && !chequeRow.isUndefined() && chequeRow.getString("NUMERO").trim().length() > 0) {
52
            num = " N° " + chequeRow.getString("NUMERO");
53
        }
54
 
73 ilm 55
        if (s != null && s.trim().length() > 0) {
80 ilm 56
            this.nom = s + (num.trim().length() > 0 ? " - Cheque" + num : "");
73 ilm 57
        } else {
80 ilm 58
            this.nom = "Reglement cheque client" + num;
73 ilm 59
        }
60
    }
18 ilm 61
 
73 ilm 62
    public void genere() throws Exception {
18 ilm 63
        System.err.println("génération des ecritures de règlement d'un cheque client du mouvement " + this.idMvt);
64
        SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
65
        SQLRow clientRow = base.getTable("CLIENT").getRow(chequeRow.getInt("ID_CLIENT"));
66
 
151 ilm 67
        this.nom = this.nom + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
68
 
73 ilm 69
        // initialisation des valeurs de la map
149 ilm 70
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
71
        this.putValue("NOM", this.nom);
72
        this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
18 ilm 73
 
144 ilm 74
        if (this.banque == null || this.banque.isUndefined() || this.banque.isForeignEmpty("ID_JOURNAL")) {
75
            fillJournalBanqueFromRow(chequeRow);
76
        } else {
77
            int idJrnl = this.banque.getForeignID("ID_JOURNAL");
149 ilm 78
            this.putValue("ID_JOURNAL", idJrnl);
144 ilm 79
        }
83 ilm 80
 
18 ilm 81
        setDateReglement(this.idCheque, this.date);
82
 
83
        // compte Clients
84
        int idCompteClient = -1;
142 ilm 85
        if (chequeRow.getBoolean("SANS_VALEUR_ENCAISSEMENT")) {
86
            if (clientRow != null) {
87
                idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
88
            }
18 ilm 89
            if (idCompteClient <= 1) {
142 ilm 90
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
91
                if (idCompteClient <= 1) {
92
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
93
                }
18 ilm 94
            }
142 ilm 95
        } else {
96
            idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_VALEUR_ENCAISSEMENT");
97
            if (idCompteClient <= 1) {
98
                idCompteClient = ComptePCESQLElement.getIdComptePceDefault("ValeurEncaissement");
99
            }
18 ilm 100
        }
101
 
149 ilm 102
        this.putValue("ID_COMPTE_PCE", new Integer(idCompteClient));
103
        this.putValue("DEBIT", new Long(0));
104
        this.putValue("CREDIT", new Long(this.montant));
73 ilm 105
        ajoutEcriture();
106
        System.err.println("First ECriture for mvt " + this.idMvt);
107
 
108
        // compte de reglement cheque, ...
83 ilm 109
        fillCompteBanqueFromRow(chequeRow, "VenteCheque", false);
149 ilm 110
        this.putValue("DEBIT", new Long(this.montant));
111
        this.putValue("CREDIT", new Long(0));
73 ilm 112
        ajoutEcriture();
151 ilm 113
 
180 ilm 114
        // FIXME remove getConf
115
        SQLRow rowSoc = ComptaPropsConfiguration.getInstanceCompta().getRowSociete();
116
        if (rowSoc.getTable().contains("TVA_ENCAISSEMENT") && rowSoc.getBoolean("TVA_ENCAISSEMENT")) {
117
            SQLRow rowTaxe = TaxeCache.getCache().getFirstTaxe();
118
            if (rowTaxe.contains("ID_COMPTE_PCE_COLLECTE_ENCAISSEMENT")) {
119
                SQLRowAccessor rowCompteTvaEnc = rowTaxe.getNonEmptyForeign("ID_COMPTE_PCE_COLLECTE_ENCAISSEMENT");
120
                SQLRowAccessor rowCompteTvaCol = rowTaxe.getNonEmptyForeign("ID_COMPTE_PCE_COLLECTE");
121
                if (rowCompteTvaCol != null && rowCompteTvaEnc != null) {
122
                    Float taux = rowTaxe.getFloat("TAUX");
123
                    long ht = Math.round(montant / (1.0 + (taux / 100.0)));
124
                    long tva = montant - ht;
125
                    this.putValue("ID_COMPTE_PCE", rowCompteTvaCol.getID());
126
                    this.putValue("DEBIT", Long.valueOf(tva));
127
                    this.putValue("CREDIT", Long.valueOf(0));
128
                    ajoutEcriture();
129
 
130
                    this.putValue("ID_COMPTE_PCE", rowCompteTvaEnc.getID());
131
                    this.putValue("DEBIT", Long.valueOf(0));
132
                    this.putValue("CREDIT", Long.valueOf(tva));
133
                    ajoutEcriture();
134
 
135
                }
136
            }
137
        }
138
 
151 ilm 139
        List<Integer> pieceIDs = new ArrayList<Integer>();
140
        pieceIDs.add(mouvementTable.getRow(idMvt).getForeignID("ID_PIECE"));
141
        lettrageAuto(pieceIDs, this.date);
142
 
73 ilm 143
        System.err.println("Ecritures générées pour le mouvement " + this.idMvt);
144
 
18 ilm 145
    }
146
 
73 ilm 147
    private void setDateReglement(int idCheque, Date d) throws SQLException {
18 ilm 148
        if (idCheque > 1) {
149
            SQLRow chequeRow = Configuration.getInstance().getBase().getTable("CHEQUE_A_ENCAISSER").getRow(idCheque);
150
            final int sourceId = MouvementSQLElement.getSourceId(chequeRow.getInt("ID_MOUVEMENT"));
151
            SQLRow rowMvt = Configuration.getInstance().getBase().getTable("MOUVEMENT").getRow(sourceId);
152
 
153
            if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
154
                SQLElement eltFacture = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
155
                SQLRow saisieRow = eltFacture.getTable().getRow(rowMvt.getInt("IDSOURCE"));
156
                // On fixe la date du paiement
157
                SQLRowValues rowValsUpdateVF = saisieRow.createEmptyUpdateRow();
158
                rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
73 ilm 159
                rowValsUpdateVF.update();
18 ilm 160
            }
161
        }
162
    }
163
 
164
}