OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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