OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 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.sql.model.SQLRow;
32
import org.openconcerto.sql.model.SQLRowAccessor;
33
import org.openconcerto.sql.model.SQLRowValues;
34
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreMode;
35
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
36
import org.openconcerto.sql.model.SQLTable;
37
import org.openconcerto.sql.model.Where;
38
import org.openconcerto.utils.StringUtils;
39
 
40
import java.util.ArrayList;
41
import java.util.List;
42
 
43
public class GenerationMvtDepotChequeClient extends GenerationEcritures {
44
 
45
    private long montant;
46
 
47
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
48
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
49
    private final SQLRow depot;
50
    private SQLRowAccessor banque;
51
 
52
    public GenerationMvtDepotChequeClient(SQLRow depot) {
53
        this.montant = depot.getLong("MONTANT");
54
        this.date = depot.getDate("DATE").getTime();
55
        this.banque = depot.getForeign("ID_" + BanqueSQLElement.TABLENAME);
56
        this.depot = depot;
57
        // SQLRow chequeRow = base.getTable("CHEQUE_A_ENCAISSER").getRow(this.idCheque);
58
        // String num = "";
59
        // if (chequeRow != null && !chequeRow.isUndefined() &&
60
        // chequeRow.getString("NUMERO").trim().length() > 0) {
61
        // num = " N° " + chequeRow.getString("NUMERO");
62
        // }
63
        //
64
        // if (s != null && s.trim().length() > 0) {
65
        // this.nom = s + (num.trim().length() > 0 ? " - Cheque" + num : "");
66
        // } else {
67
        // this.nom = "Reglement cheque client" + num;
68
        // }
69
    }
70
 
71
    public void genere() throws Exception {
72
        System.err.println("génération des ecritures de règlement d'un cheque client du mouvement " + this.idMvt);
73
        this.nom = this.depot.getString("NOM");
74
        if (this.depot.getObject("ID_MOUVEMENT") == null || this.depot.isForeignEmpty("ID_MOUVEMENT")) {
75
            this.idMvt = getNewMouvement(depot.getTable().getName(), depot.getID(), 1, this.nom);
76
        } else {
77
            this.idMvt = this.depot.getForeignID("ID_MOUVEMENT");
78
            SQLRowValues rowValspiece = this.depot.getForeign("ID_MOUVEMENT").getForeign("ID_PIECE").createEmptyUpdateRow();
79
            rowValspiece.put("NOM", this.nom);
80
            rowValspiece.update();
81
        }
82
 
83
        // initialisation des valeurs de la map
84
        this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
85
        this.putValue("DATE", new java.sql.Date(this.date.getTime()));
86
        this.putValue("NOM", this.nom);
87
        if (this.banque == null || this.banque.isUndefined() || this.banque.isForeignEmpty("ID_JOURNAL")) {
88
            fillJournalBanqueFromRow(depot);
89
        } else {
90
            int idJrnl = this.banque.getForeignID("ID_JOURNAL");
91
            this.putValue("ID_JOURNAL", idJrnl);
92
        }
93
 
94
        List<Integer> pieceIDs = new ArrayList<Integer>();
95
        SQLRowValues rowValsDepotElt = new SQLRowValues(depot.getTable().getTable("DEPOT_CHEQUE_ELEMENT"));
174 ilm 96
        rowValsDepotElt.putNulls("MONTANT", "TIERS", "PIECE");
156 ilm 97
        rowValsDepotElt.putRowValues("ID_CLIENT").putNulls("NOM", "ID_COMPTE_PCE");
98
        final SQLRowValues rowValuesChq = rowValsDepotElt.putRowValues("ID_CHEQUE_A_ENCAISSER");
99
        rowValuesChq.putNulls("SANS_VALEUR_ENCAISSEMENT").putRowValues("ID_MOUVEMENT").putNulls("ID_PIECE");
100
        rowValuesChq.putNulls("ID_COMPTE_PCE_TIERS").putNulls("NUMERO");
101
        List<SQLRowValues> cheques = SQLRowValuesListFetcher.create(rowValsDepotElt).fetch(new Where(rowValsDepotElt.getTable().getField("ID_DEPOT_CHEQUE"), "=", depot.getID()));
102
        for (SQLRowValues sqlRowAccessor : cheques) {
103
            final SQLRowAccessor clientRow = sqlRowAccessor.getForeign("ID_CLIENT");
104
            // this.nom = this.nom + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
174 ilm 105
            this.putValue("NOM", this.nom + " " + sqlRowAccessor.getString("PIECE") + " " + StringUtils.limitLength(clientRow.getString("NOM"), 20));
156 ilm 106
            SQLRowAccessor chequeRow = sqlRowAccessor.getForeign("ID_CHEQUE_A_ENCAISSER");
107
            pieceIDs.add(chequeRow.getForeign("ID_MOUVEMENT").getForeignID("ID_PIECE"));
108
            // compte Clients
109
            SQLRowAccessor rowCptTiers = chequeRow.getForeign("ID_COMPTE_PCE_TIERS");
110
            int idCompteClient = rowCptTiers != null && !rowCptTiers.isUndefined() ? rowCptTiers.getID() : -1;
111
 
112
            if (chequeRow.getBoolean("SANS_VALEUR_ENCAISSEMENT")) {
113
                if (idCompteClient == -1) {
114
                    if (clientRow != null) {
115
                        idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
116
                    }
117
                    if (idCompteClient <= 1) {
118
                        idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
119
                        if (idCompteClient <= 1) {
120
                            idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
121
                        }
122
                    }
123
                }
124
            } else {
125
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_VALEUR_ENCAISSEMENT");
126
                if (idCompteClient <= 1) {
127
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("ValeurEncaissement");
128
                }
129
            }
130
 
131
            this.putValue("ID_COMPTE_PCE", new Integer(idCompteClient));
132
            this.putValue("DEBIT", new Long(0));
133
            this.putValue("CREDIT", new Long(sqlRowAccessor.getLong("MONTANT")));
134
            SQLRow insertedRow = ajoutEcriture();
135
 
136
            sqlRowAccessor.createEmptyUpdateRow().put("ID_ECRITURE", insertedRow.getID()).getGraph().store(StoreMode.COMMIT, false);
137
            sqlRowAccessor.getForeign("ID_CHEQUE_A_ENCAISSER").createEmptyUpdateRow().put("ENCAISSE", Boolean.TRUE).getGraph().store(StoreMode.COMMIT, false);
138
        }
139
        // compte de reglement cheque, ...
140
        fillCompteBanqueFromRow(depot, "VenteCheque", false);
141
        this.putValue("NOM", this.nom);
142
        this.putValue("DEBIT", new Long(this.montant));
143
        this.putValue("CREDIT", new Long(0));
144
        SQLRow insertedRow = ajoutEcriture();
145
 
146
        depot.createEmptyUpdateRow().put("ID_MOUVEMENT", idMvt).put("ID_ECRITURE", insertedRow.getID()).getGraph().store(StoreMode.COMMIT, false);
147
 
148
        pieceIDs.add(mouvementTable.getRow(idMvt).getForeignID("ID_PIECE"));
149
        lettrageAuto(pieceIDs, this.date);
150
 
151
        System.err.println("Ecritures générées pour le mouvement " + this.idMvt);
152
 
153
    }
154
 
155
    // private void setDateReglement(int idCheque, Date d) throws SQLException {
156
    // if (idCheque > 1) {
157
    // SQLRow chequeRow =
158
    // Configuration.getInstance().getBase().getTable("CHEQUE_A_ENCAISSER").getRow(idCheque);
159
    // final int sourceId = MouvementSQLElement.getSourceId(chequeRow.getInt("ID_MOUVEMENT"));
160
    // SQLRow rowMvt = Configuration.getInstance().getBase().getTable("MOUVEMENT").getRow(sourceId);
161
    //
162
    // if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
163
    // SQLElement eltFacture =
164
    // Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
165
    // SQLRow saisieRow = eltFacture.getTable().getRow(rowMvt.getInt("IDSOURCE"));
166
    // // On fixe la date du paiement
167
    // SQLRowValues rowValsUpdateVF = saisieRow.createEmptyUpdateRow();
168
    // rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
169
    // rowValsUpdateVF.update();
170
    // }
171
    // }
172
    // }
173
 
174
}