OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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