OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 94 | Go to most recent revision | 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.config.ComptaPropsConfiguration;
83 ilm 17
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18 ilm 19
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
20
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
21
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
22
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
23
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
24
import org.openconcerto.erp.model.PrixTTC;
25
import org.openconcerto.sql.Configuration;
83 ilm 26
import org.openconcerto.sql.element.SQLElement;
18 ilm 27
import org.openconcerto.sql.model.SQLRow;
83 ilm 28
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 29
import org.openconcerto.sql.model.SQLRowValues;
83 ilm 30
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
31
import org.openconcerto.sql.model.SQLSelect;
18 ilm 32
import org.openconcerto.sql.model.SQLTable;
83 ilm 33
import org.openconcerto.sql.model.Where;
34
import org.openconcerto.utils.cc.ITransformer;
18 ilm 35
 
36
import java.sql.SQLException;
37
import java.sql.Timestamp;
38
import java.util.ArrayList;
39
import java.util.Calendar;
40
import java.util.Date;
41
import java.util.List;
42
 
43
public class GenerationReglementVenteNG extends GenerationEcritures {
44
 
45
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
46
    private static final SQLTable tableEncaisse = base.getTable("ENCAISSER_MONTANT");
47
    private static final SQLTable tableEncaisseElt = base.getTable("ENCAISSER_MONTANT_ELEMENT");
48
    private static final SQLTable tableSaisieVenteFacture = base.getTable("SAISIE_VENTE_FACTURE");
49
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
50
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
51
 
83 ilm 52
    public SQLRow ecrClient = null;
53
 
73 ilm 54
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource) throws Exception {
18 ilm 55
        this(label, rowClient, ttc, d, modeReglement, source, mvtSource, true);
56
    }
57
 
73 ilm 58
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource, boolean createEncaisse) throws Exception {
18 ilm 59
 
60
        SQLRow typeRegRow = modeReglement.getForeignRow("ID_TYPE_REGLEMENT");
61
 
62
        // iniatilisation des valeurs de la map
63
        this.date = d;
64
 
65
        // TODO Nommage des ecritures
66
 
67
        this.nom = label;
68
 
69
        this.mEcritures.put("DATE", this.date);
70
        this.mEcritures.put("NOM", this.nom);
83 ilm 71
        fillJournalBanqueFromRow(modeReglement);
18 ilm 72
 
73
        this.mEcritures.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
74
        if (source.getTable().getName().equalsIgnoreCase("ENCAISSER_MONTANT")) {
75
            List<SQLRow> l = source.getReferentRows(source.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
76
            for (SQLRow sqlRow : l) {
77
                SQLRow mvtEch = sqlRow.getForeignRow("ID_MOUVEMENT_ECHEANCE");
78
                if (mvtEch.getID() != mvtSource.getID()) {
79
                    getNewMouvement(source.getTable().getName(), source.getID(), mvtEch.getID(), mvtEch.getInt("ID_PIECE"));
80
                }
81
            }
82
        }
83
        // si paiement comptant
84
        if ((!typeRegRow.getBoolean("ECHEANCE"))
85
                && ((modeReglement.getBoolean("COMPTANT")) || (!modeReglement.getBoolean("DATE_FACTURE") && (modeReglement.getInt("AJOURS") == 0 && modeReglement.getInt("LENJOUR") == 0)))) {
86
 
87
            SQLRow rowEncaisse = source;
88
 
89
            SQLRow rowEncaisseElt = null;
83 ilm 90
            // On cre un encaissement
18 ilm 91
            if (createEncaisse) {
73 ilm 92
                SQLRowValues rowVals = new SQLRowValues(tableEncaisse);
93
                rowVals.put("MONTANT", ttc.getLongValue());
94
                rowVals.put("ID_CLIENT", rowClient.getID());
95
                rowVals.put("DATE", this.date);
96
                if (typeRegRow.getID() >= TypeReglementSQLElement.TRAITE) {
97
                    Calendar c2 = modeReglement.getDate("DATE_VIREMENT");
98
                    if (c2 != null) {
99
                        rowVals.put("DATE", c2.getTime());
18 ilm 100
                    }
73 ilm 101
                }
102
                SQLRowValues rowValsRegl = new SQLRowValues(modeReglement.asRowValues());
103
                SQLRow copy = rowValsRegl.insert();
104
                rowVals.put("ID_MODE_REGLEMENT", copy.getID());
105
                rowVals.put("NOM", label);
106
                rowEncaisse = rowVals.insert();
107
                SQLRowValues rowValsElt = new SQLRowValues(tableEncaisseElt);
108
                rowValsElt.put("MONTANT_REGLE", ttc.getLongValue());
109
                rowValsElt.put("ID_ENCAISSER_MONTANT", rowEncaisse.getID());
110
                rowEncaisseElt = rowValsElt.insert();
18 ilm 111
 
112
            }
113
 
114
            this.idMvt = getNewMouvement(rowEncaisse.getTable().getName(), rowEncaisse.getID(), mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
115
 
116
            this.mEcritures.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
117
 
118
            SQLRowValues rowVals = rowEncaisse.createEmptyUpdateRow();
119
            rowVals.put("ID_MOUVEMENT", this.idMvt);
73 ilm 120
 
121
            rowVals.update();
122
 
18 ilm 123
            if (rowEncaisseElt != null) {
124
                SQLRowValues rowVals2 = rowEncaisseElt.createEmptyUpdateRow();
125
                rowVals2.put("ID_MOUVEMENT_ECHEANCE", this.idMvt);
73 ilm 126
                rowVals2.update();
18 ilm 127
            }
128
 
129
            // Cheque
130
            if (typeRegRow.getID() == TypeReglementSQLElement.CHEQUE) {
131
 
132
                Date dateTmp = this.date;
133
                if (modeReglement.getObject("DATE") != null) {
134
                    dateTmp = modeReglement.getDate("DATE").getTime();
135
                }
136
                // On fixe la date du règlement de la facture
73 ilm 137
                setDateReglement(source, dateTmp);
18 ilm 138
 
139
                Calendar c = modeReglement.getDate("DATE_DEPOT");
140
                if (c != null) {
141
                    paiementCheque(c.getTime(), source, ttc, rowClient.getID(), modeReglement, mvtSource.getTable().getRow(idMvt));
142
                } else {
143
                    paiementCheque(this.date, source, ttc, rowClient.getID(), modeReglement, mvtSource.getTable().getRow(idMvt));
144
                }
145
 
146
            } else {
147
                // On fixe la date du règlement de la facture
148
                if (typeRegRow.getID() >= TypeReglementSQLElement.TRAITE) {
149
                    Calendar c2 = modeReglement.getDate("DATE_VIREMENT");
150
                    if (c2 == null) {
151
                        setDateReglement(source, this.date);
152
                    } else {
153
                        setDateReglement(source, c2.getTime());
154
                    }
155
 
156
                } else {
157
                    setDateReglement(source, this.date);
158
                }
159
                if (typeRegRow.getID() == TypeReglementSQLElement.ESPECE) {
160
                    this.mEcritures.put("ID_JOURNAL", JournalSQLElement.CAISSES);
161
                }
162
 
163
                // compte Clients
164
 
73 ilm 165
                int idCompteClient = rowClient.getInt("ID_COMPTE_PCE");
166
                if (idCompteClient <= 1) {
167
                    idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
18 ilm 168
                    if (idCompteClient <= 1) {
73 ilm 169
                        idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
18 ilm 170
                    }
73 ilm 171
                }
18 ilm 172
 
73 ilm 173
                this.mEcritures.put("ID_COMPTE_PCE", idCompteClient);
174
                this.mEcritures.put("DEBIT", Long.valueOf(0));
175
                this.mEcritures.put("CREDIT", Long.valueOf(ttc.getLongValue()));
83 ilm 176
                this.ecrClient = ajoutEcriture();
18 ilm 177
 
73 ilm 178
                // compte de reglement, caisse, cheque, ...
83 ilm 179
                if (typeRegRow.getID() == TypeReglementSQLElement.ESPECE) {
180
                    int idCompteRegl = typeRegRow.getInt("ID_COMPTE_PCE_CLIENT");
181
                    if (idCompteRegl <= 1) {
182
                        idCompteRegl = ComptePCESQLElement.getIdComptePceDefault("VenteEspece");
183
                    }
184
 
185
                    this.mEcritures.put("ID_COMPTE_PCE", Integer.valueOf(idCompteRegl));
186
                } else {
187
                    fillCompteBanqueFromRow(modeReglement, "VenteCB", false);
188
                }
73 ilm 189
                this.mEcritures.put("DEBIT", Long.valueOf(ttc.getLongValue()));
190
                this.mEcritures.put("CREDIT", Long.valueOf(0));
191
                ajoutEcriture();
192
 
18 ilm 193
            }
194
        } else {
195
 
196
                Date dateEch = ModeDeReglementSQLElement.calculDate(modeReglement.getInt("AJOURS"), modeReglement.getInt("LENJOUR"), this.date);
197
 
83 ilm 198
                System.out.println("Echance client");
18 ilm 199
 
200
                // Ajout dans echeance
21 ilm 201
                final SQLTable tableEch = base.getTable("ECHEANCE_CLIENT");
202
                SQLRowValues valEcheance = new SQLRowValues(tableEch);
18 ilm 203
 
204
                this.idMvt = getNewMouvement("ECHEANCE_CLIENT", 1, mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
205
                valEcheance.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
206
                valEcheance.put("DATE", dateEch);
207
                valEcheance.put("MONTANT", Long.valueOf(ttc.getLongValue()));
208
                valEcheance.put("ID_CLIENT", rowClient.getID());
73 ilm 209
                if (source.getTable().equals(tableSaisieVenteFacture)) {
210
                    valEcheance.put("ID_SAISIE_VENTE_FACTURE", source.getID());
211
                }
18 ilm 212
 
73 ilm 213
                // ajout de l'ecriture
214
                SQLRow row = valEcheance.insert();
215
                SQLRowValues rowVals = new SQLRowValues(tableMouvement);
216
                rowVals.put("IDSOURCE", row.getID());
217
                rowVals.update(this.idMvt);
18 ilm 218
 
219
        }
220
    }
221
 
73 ilm 222
    private void setDateReglement(SQLRow source, Date d) throws SQLException {
18 ilm 223
        List<SQLRow> sources = new ArrayList<SQLRow>();
224
        if (source.getTable().getName().equalsIgnoreCase("ENCAISSER_MONTANT")) {
225
 
226
            List<SQLRow> rows = source.getReferentRows(source.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
227
            for (SQLRow sqlRow : rows) {
228
                SQLRow rowEch = sqlRow.getForeignRow("ID_ECHEANCE_CLIENT");
229
                if (rowEch != null && rowEch.getID() > 1) {
230
                    SQLRow rowMvt = tableMouvement.getRow(MouvementSQLElement.getSourceId(rowEch.getInt("ID_MOUVEMENT")));
231
                    if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
232
                        sources.add(tableSaisieVenteFacture.getRow(rowMvt.getInt("IDSOURCE")));
233
                    }
234
                }
235
            }
236
 
237
        } else {
238
            sources.add(source);
239
        }
240
        for (SQLRow sqlRow : sources) {
241
            if (sqlRow.getTable().getName().equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
242
                SQLRowValues rowValsUpdateVF = sqlRow.createEmptyUpdateRow();
243
                rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
73 ilm 244
                rowValsUpdateVF.update();
18 ilm 245
            }
246
        }
247
 
248
    }
249
 
83 ilm 250
    public void doLettrageAuto(final SQLRowAccessor source, Date dateLettrage) {
251
        // A. On lettre les critures client (facture ET reglement)
252
        // A1. Recherche criture client de facturation
253
 
254
        final SQLRowValues g1 = new SQLRowValues(ecritureTable);
255
        g1.put("DEBIT", null);
256
        final SQLRowValuesListFetcher fetch1 = new SQLRowValuesListFetcher(g1);
257
        fetch1.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
258
            @Override
259
            public SQLSelect transformChecked(SQLSelect input) {
260
                input.setWhere(new Where(ecritureTable.getField("ID_MOUVEMENT"), "=", source.getForeignID("ID_MOUVEMENT")));
261
                input.andWhere(new Where(ecritureTable.getField("ID_COMPTE_PCE"), "=", ecrClient.getForeignID("ID_COMPTE_PCE")));
262
                input.andWhere(new Where(ecritureTable.getField("JOURNAL_CODE"), "=", "VE"));
263
                return input;
264
            }
265
 
266
        });
267
        final List<SQLRowValues> rowsEcriture1 = fetch1.fetch();
268
        if (rowsEcriture1.size() != 1) {
269
            System.out.println("critures VE trouves. Erreur");
270
            return;
271
        }
272
        final SQLRowValues rEcriture1 = rowsEcriture1.get(0);
273
        System.out.println("Ecriture vente: " + rEcriture1.getID());
274
        System.out.println("Ecriture paiement: " + this.ecrClient);
275
 
276
        // Récupère lettrage
277
        String codeLettre = NumerotationAutoSQLElement.getNextCodeLettrage();
278
 
279
        // TODO: vérifier somme = 0
280
 
281
        // Met à  jour les 2 écritures
282
        SQLRowValues rowVals = new SQLRowValues(ecritureTable);
283
        rowVals.put("LETTRAGE", codeLettre);
284
        rowVals.put("DATE_LETTRAGE", dateLettrage);
285
        try {
286
            rowVals.update(rEcriture1.getID());
287
            rowVals.update(this.ecrClient.getID());
288
        } catch (SQLException e1) {
289
            e1.printStackTrace();
290
        }
291
 
292
        // Mise à  jour du code de lettrage
293
        SQLElement numElt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
294
        SQLRowValues rowVals1 = numElt.getTable().getRow(2).createEmptyUpdateRow();
295
        rowVals1.put("CODE_LETTRAGE", codeLettre);
296
        try {
297
            rowVals1.update();
298
        } catch (SQLException e) {
299
            e.printStackTrace();
300
        }
301
    }
302
 
73 ilm 303
    private void paiementCheque(Date dateEch, SQLRow source, PrixTTC ttc, int idClient, SQLRow modeRegl, SQLRow mvtSource) throws SQLException {
18 ilm 304
 
305
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_A_ENCAISSER"));
306
        valCheque.put("ID_CLIENT", idClient);
83 ilm 307
 
308
        final String foreignBanqueFieldName = "ID_" + BanqueSQLElement.TABLENAME;
309
        if (valCheque.getTable().contains(foreignBanqueFieldName))
310
            valCheque.put(foreignBanqueFieldName, modeRegl.getInt(foreignBanqueFieldName));
311
 
18 ilm 312
        valCheque.put("NUMERO", modeRegl.getObject("NUMERO"));
313
        valCheque.put("DATE", modeRegl.getObject("DATE"));
314
        valCheque.put("ETS", modeRegl.getObject("ETS"));
315
        valCheque.put("DATE_VENTE", this.date);
316
        this.idMvt = getNewMouvement("CHEQUE_A_ENCAISSER", 1, mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
317
        valCheque.put("DATE_MIN_DEPOT", dateEch);
318
        valCheque.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
319
        valCheque.put("MONTANT", Long.valueOf(ttc.getLongValue()));
320
 
73 ilm 321
        if (valCheque.getInvalid() == null) {
322
            // ajout de l'ecriture
323
            SQLRow row = valCheque.insert();
324
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
325
            rowVals.put("IDSOURCE", row.getID());
326
            rowVals.update(this.idMvt);
18 ilm 327
        }
73 ilm 328
 
18 ilm 329
    }
330
}