OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
177 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
 /*
15
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
16
 *
17
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
18
 *
19
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
20
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
21
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
22
 * language governing permissions and limitations under the License.
23
 *
24
 * When distributing the software, include this License Header Notice in each file.
25
 */
26
 
27
package org.openconcerto.erp.generationEcritures;
28
 
29
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
30
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
31
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
34
import org.openconcerto.sql.model.SQLRowValues;
35
import org.openconcerto.sql.model.SQLTable;
36
import org.openconcerto.utils.StringUtils;
37
 
38
import java.util.ArrayList;
39
import java.util.List;
40
 
41
public class GenerationMvtSepa extends GenerationEcritures {
42
 
43
    private long montant;
44
 
45
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
46
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
47
    private final List<SQLRow> sepaRows;
48
    private SQLRowAccessor banque;
49
 
50
    public GenerationMvtSepa(List<SQLRow> sepaRows) {
51
 
52
        this.sepaRows = sepaRows;
53
        // SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
54
        // String num = "";
55
        // if (chequeRow != null && !chequeRow.isUndefined() &&
56
        // chequeRow.getString("NUMERO").trim().length() > 0) {
57
        // num = " N° " + chequeRow.getString("NUMERO");
58
        // }
59
        //
60
        // if (s != null && s.trim().length() > 0) {
61
        // this.nom = s + (num.trim().length() > 0 ? " - Cheque" + num : "");
62
        // } else {
63
        // this.nom = "Reglement cheque client" + num;
64
        // }
65
    }
66
 
67
    public void genere() throws Exception {
68
        System.err.println("génération des ecritures de règlement d'un prelevement client du mouvement " + this.idMvt);
69
        List<Integer> pieceIDs = new ArrayList<>();
70
        for (SQLRow rowSepa : sepaRows) {
71
            this.montant = rowSepa.getLong("MONTANT");
72
            this.date = rowSepa.getDate("DATE").getTime();
73
            this.banque = rowSepa.getForeign("ID_" + BanqueSQLElement.TABLENAME);
74
 
75
            final SQLRowAccessor foreignPiece = rowSepa.getForeign("ID_MOUVEMENT").getForeign("ID_PIECE");
76
            this.nom = "Prélèvement " + foreignPiece.getString("NOM");
77
            final SQLRowAccessor clientRow = rowSepa.getForeign("ID_CLIENT");
78
 
79
            // Création de l'encaissement
80
            SQLRowValues rowVals = new SQLRowValues(base.getTable("ENCAISSER_MONTANT"));
81
            rowVals.put("MONTANT", montant);
82
            rowVals.put("ID_CLIENT", clientRow != null ? clientRow.getID() : null);
83
            rowVals.put("TIERS", rowSepa.getString("TIERS"));
84
            rowVals.put("DATE", this.date);
85
            SQLRowValues rowValsRegl = new SQLRowValues(base.getTable("MODE_REGLEMENT"));
86
            rowValsRegl.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.PRELEVEMENT);
87
            SQLRow copy = rowValsRegl.insert();
88
            rowVals.put("ID_MODE_REGLEMENT", copy.getID());
89
            rowVals.put("NOM", this.nom);
90
            // rowVals.put("ID_MOUVEMENT", this.idMvt);
91
            SQLRow rowEncaisse = rowVals.insert();
92
 
93
            SQLRowValues rowValsElt = new SQLRowValues(base.getTable("ENCAISSER_MONTANT_ELEMENT"));
94
            rowValsElt.put("MONTANT_REGLE", this.montant);
95
            rowValsElt.put("ID_ENCAISSER_MONTANT", rowEncaisse.getID());
96
            rowValsElt.put("ID_MOUVEMENT_ECHEANCE", rowSepa.getForeignID("ID_MOUVEMENT"));
97
            rowValsElt.insert();
98
 
99
            this.idMvt = getNewMouvement(rowEncaisse.getTable().getName(), rowEncaisse.getID(), rowSepa.getForeignID("ID_MOUVEMENT"), foreignPiece.getID());
100
            rowEncaisse.createEmptyUpdateRow().put("ID_MOUVEMENT", this.idMvt).commit();
101
 
102
            // initialisation des valeurs de la map
103
            this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
104
            this.putValue("DATE", new java.sql.Date(this.date.getTime()));
105
            this.putValue("NOM", this.nom);
106
            if (this.banque == null || this.banque.isUndefined() || this.banque.isForeignEmpty("ID_JOURNAL")) {
107
                fillJournalBanqueFromRow(rowSepa);
108
            } else {
109
                int idJrnl = this.banque.getForeignID("ID_JOURNAL");
110
                this.putValue("ID_JOURNAL", idJrnl);
111
            }
112
 
113
            this.putValue("NOM", this.nom + " " + foreignPiece.getString("NOM") + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20));
114
            // compte Clients
115
            SQLRowAccessor rowCptTiers = rowSepa.getForeign("ID_COMPTE_PCE_TIERS");
116
            int idCompteClient = rowCptTiers != null && !rowCptTiers.isUndefined() ? rowCptTiers.getID() : -1;
117
 
118
            if (idCompteClient == -1) {
119
                if (clientRow != null) {
120
                    idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
121
                }
122
                if (idCompteClient <= 1) {
123
                    idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
124
                    if (idCompteClient <= 1) {
125
                        idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
126
                    }
127
                }
128
            }
129
 
130
            this.putValue("ID_COMPTE_PCE", new Integer(idCompteClient));
131
            this.putValue("DEBIT", new Long(0));
132
            this.putValue("CREDIT", new Long(rowSepa.getLong("MONTANT")));
133
            ajoutEcriture();
134
 
135
            // compte de reglement cheque, ...
136
            fillCompteBanqueFromRow(rowSepa, "VenteCheque", false);
137
            this.putValue("NOM", this.nom);
138
            this.putValue("DEBIT", new Long(this.montant));
139
            this.putValue("CREDIT", new Long(0));
140
            ajoutEcriture();
141
 
142
            pieceIDs.add(foreignPiece.getID());
143
 
144
            System.err.println("Ecritures générées pour le mouvement " + this.idMvt);
145
        }
146
 
147
        lettrageAuto(pieceIDs, this.date);
148
 
149
        // TODO mettre à jour la date de réglement dee la facture
150
    }
151
 
152
    // private void setDateReglement(int idCheque, Date d) throws SQLException {
153
    // if (idCheque > 1) {
154
    // SQLRow chequeRow =
155
    // Configuration.getInstance().getBase().getTable("CHEQUE_A_ENCAISSER").getRow(idCheque);
156
    // final int sourceId = MouvementSQLElement.getSourceId(chequeRow.getInt("ID_MOUVEMENT"));
157
    // SQLRow rowMvt = Configuration.getInstance().getBase().getTable("MOUVEMENT").getRow(sourceId);
158
    //
159
    // if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
160
    // SQLElement eltFacture =
161
    // Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
162
    // SQLRow saisieRow = eltFacture.getTable().getRow(rowMvt.getInt("IDSOURCE"));
163
    // // On fixe la date du paiement
164
    // SQLRowValues rowValsUpdateVF = saisieRow.createEmptyUpdateRow();
165
    // rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
166
    // rowValsUpdateVF.update();
167
    // }
168
    // }
169
    // }
170
 
171
}