OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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