OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.utils.ExceptionHandler;
25
 
26
import java.sql.SQLException;
27
import java.sql.Timestamp;
28
import java.util.Date;
29
 
30
 
31
public class GenerationMvtReglementChequeClient extends GenerationEcritures {
32
 
33
    private long montant;
34
 
35
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
36
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
37
    private int idCheque;
38
 
39
    public void genere() {
40
 
41
        System.err.println("génération des ecritures de règlement d'un cheque client du mouvement " + this.idMvt);
42
        SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
43
        SQLRow clientRow = base.getTable("CLIENT").getRow(chequeRow.getInt("ID_CLIENT"));
44
 
45
        // iniatilisation des valeurs de la map
46
        // this.date = new Date();
47
 
48
        this.mEcritures.put("DATE", new java.sql.Date(this.date.getTime()));
49
        this.mEcritures.put("NOM", this.nom);
50
        this.mEcritures.put("ID_JOURNAL", JournalSQLElement.BANQUES);
51
        this.mEcritures.put("ID_MOUVEMENT", new Integer(this.idMvt));
52
 
53
        setDateReglement(this.idCheque, this.date);
54
 
55
        // compte Clients
56
        int idCompteClient = -1;
57
        if (clientRow != null) {
58
            idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
59
        }
60
        if (idCompteClient <= 1) {
61
            idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
62
            if (idCompteClient <= 1) {
63
                try {
64
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
65
                } catch (Exception e) {
66
                    e.printStackTrace();
67
                }
68
            }
69
        }
70
        try {
71
            int idPce = base.getTable("TYPE_REGLEMENT").getRow(2).getInt("ID_COMPTE_PCE_CLIENT");
72
            if (idPce <= 1) {
73
                try {
74
                    idPce = ComptePCESQLElement.getIdComptePceDefault("VenteCheque");
75
                } catch (Exception e) {
76
                    e.printStackTrace();
77
                }
78
            }
79
 
80
            SQLRow rowBanque = chequeRow.getForeignRow("ID_BANQUE_POLE_PRODUIT");
81
            if (rowBanque != null && rowBanque.getID() > 1) {
82
                SQLRow rowJournal = rowBanque.getForeignRow("ID_JOURNAL");
83
                if (rowJournal != null && rowJournal.getID() > 1) {
84
                    this.mEcritures.put("ID_JOURNAL", rowJournal.getID());
85
                }
86
                SQLRow rowCompteBanque = rowBanque.getForeignRow("ID_COMPTE_PCE");
87
                if (rowCompteBanque != null && rowCompteBanque.getID() > 1) {
88
                    idPce = rowCompteBanque.getID();
89
                }
90
            }
91
 
92
            this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteClient));
93
            this.mEcritures.put("DEBIT", new Long(0));
94
            this.mEcritures.put("CREDIT", new Long(this.montant));
95
            ajoutEcriture();
96
            System.err.println("First ECriture for mvt " + this.idMvt);
97
 
98
            // compte de reglement cheque, ...
99
            this.mEcritures.put("ID_COMPTE_PCE", new Integer(idPce));
100
            this.mEcritures.put("DEBIT", new Long(this.montant));
101
            this.mEcritures.put("CREDIT", new Long(0));
102
            ajoutEcriture();
103
            System.err.println("Ecritures générées pour le mouvement " + this.idMvt);
104
        } catch (IllegalArgumentException e) {
105
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
106
            e.printStackTrace();
107
        }
108
    }
109
 
110
    private void setDateReglement(int idCheque, Date d) {
111
 
112
        if (idCheque > 1) {
113
 
114
            SQLRow chequeRow = Configuration.getInstance().getBase().getTable("CHEQUE_A_ENCAISSER").getRow(idCheque);
115
 
116
            final int sourceId = MouvementSQLElement.getSourceId(chequeRow.getInt("ID_MOUVEMENT"));
117
            SQLRow rowMvt = Configuration.getInstance().getBase().getTable("MOUVEMENT").getRow(sourceId);
118
 
119
            if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
120
                SQLElement eltFacture = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
121
                SQLRow saisieRow = eltFacture.getTable().getRow(rowMvt.getInt("IDSOURCE"));
122
                // On fixe la date du paiement
123
                SQLRowValues rowValsUpdateVF = saisieRow.createEmptyUpdateRow();
124
                rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
125
                try {
126
                    rowValsUpdateVF.update();
127
                } catch (SQLException e1) {
128
                    // TODO Auto-generated catch block
129
                    e1.printStackTrace();
130
                }
131
            }
132
 
133
        }
134
 
135
    }
136
 
137
    public GenerationMvtReglementChequeClient(int idMvt, long montant, Date d, int idCheque, String s) {
138
 
139
        this.montant = montant;
140
        this.date = d;
141
        this.idMvt = idMvt;
142
        this.idCheque = idCheque;
143
        if (s != null && s.trim().length() > 0) {
144
            this.nom = s;
145
        } else {
146
            this.nom = "Reglement cheque client";
147
        }
148
    }
149
}