OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Rev 180 | 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;
177 ilm 23
import org.openconcerto.erp.core.finance.payment.element.SEPAMandateSQLElement;
18 ilm 24
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
25
import org.openconcerto.erp.model.PrixTTC;
144 ilm 26
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 27
import org.openconcerto.sql.Configuration;
83 ilm 28
import org.openconcerto.sql.element.SQLElement;
18 ilm 29
import org.openconcerto.sql.model.SQLRow;
83 ilm 30
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 31
import org.openconcerto.sql.model.SQLRowValues;
83 ilm 32
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
33
import org.openconcerto.sql.model.SQLSelect;
18 ilm 34
import org.openconcerto.sql.model.SQLTable;
83 ilm 35
import org.openconcerto.sql.model.Where;
144 ilm 36
import org.openconcerto.sql.preferences.SQLPreferences;
83 ilm 37
import org.openconcerto.utils.cc.ITransformer;
18 ilm 38
 
39
import java.sql.SQLException;
40
import java.sql.Timestamp;
41
import java.util.ArrayList;
42
import java.util.Calendar;
43
import java.util.Date;
44
import java.util.List;
45
 
46
public class GenerationReglementVenteNG extends GenerationEcritures {
47
 
48
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
49
    private static final SQLTable tableEncaisse = base.getTable("ENCAISSER_MONTANT");
50
    private static final SQLTable tableEncaisseElt = base.getTable("ENCAISSER_MONTANT_ELEMENT");
51
    private static final SQLTable tableSaisieVenteFacture = base.getTable("SAISIE_VENTE_FACTURE");
52
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
53
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
54
 
83 ilm 55
    public SQLRow ecrClient = null;
56
 
177 ilm 57
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource) throws SQLException {
18 ilm 58
        this(label, rowClient, ttc, d, modeReglement, source, mvtSource, true);
59
    }
60
 
177 ilm 61
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource, boolean createEncaisse) throws SQLException {
142 ilm 62
        this(label, rowClient, ttc, d, modeReglement, source, mvtSource, createEncaisse, false);
63
    }
18 ilm 64
 
142 ilm 65
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource, boolean createEncaisse, boolean avance)
177 ilm 66
            throws SQLException {
156 ilm 67
        this(label, rowClient, ttc, d, modeReglement, source, mvtSource, createEncaisse, avance, rowClient.getString("NOM"), null);
68
    }
142 ilm 69
 
156 ilm 70
    public GenerationReglementVenteNG(String label, SQLRow rowClient, PrixTTC ttc, Date d, SQLRow modeReglement, SQLRow source, SQLRow mvtSource, boolean createEncaisse, boolean avance, String tiers,
177 ilm 71
            SQLRowAccessor cptTiers) throws SQLException {
156 ilm 72
 
18 ilm 73
        SQLRow typeRegRow = modeReglement.getForeignRow("ID_TYPE_REGLEMENT");
94 ilm 74
        setRowAnalytiqueSource(source);
18 ilm 75
        // iniatilisation des valeurs de la map
76
        this.date = d;
149 ilm 77
        if (typeRegRow.getID() == TypeReglementSQLElement.CB && modeReglement.getObject("DATE") != null) {
78
            this.date = (Date) modeReglement.getObject("DATE");
79
        }
18 ilm 80
 
81
        // TODO Nommage des ecritures
82
 
83
        this.nom = label;
84
 
149 ilm 85
        this.putValue("DATE", this.date);
86
        this.putValue("NOM", this.nom);
83 ilm 87
        fillJournalBanqueFromRow(modeReglement);
18 ilm 88
 
149 ilm 89
        this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 90
        if (source.getTable().getName().equalsIgnoreCase("ENCAISSER_MONTANT")) {
149 ilm 91
            if (source.getBoolean("ACOMPTE")) {
92
                final String numeroDevis = source.asRow().getForeign("ID_DEVIS").getString("NUMERO");
93
                this.idMvt = getNewMouvement(source.getTable().getName(), source.getID(), 1, numeroDevis);
94
                this.putValue("NOM_PIECE", numeroDevis);
95
            } else {
96
                List<SQLRow> l = source.getReferentRows(source.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
97
                for (SQLRow sqlRow : l) {
98
                    SQLRow mvtEch = sqlRow.getForeignRow("ID_MOUVEMENT_ECHEANCE");
99
                    if (mvtEch.getID() != mvtSource.getID()) {
100
                        getNewMouvement(source.getTable().getName(), source.getID(), mvtEch.getID(), mvtEch.getInt("ID_PIECE"));
101
                    }
18 ilm 102
                }
103
            }
104
        }
105
        // si paiement comptant
106
        if ((!typeRegRow.getBoolean("ECHEANCE"))
107
                && ((modeReglement.getBoolean("COMPTANT")) || (!modeReglement.getBoolean("DATE_FACTURE") && (modeReglement.getInt("AJOURS") == 0 && modeReglement.getInt("LENJOUR") == 0)))) {
108
 
109
            SQLRow rowEncaisse = source;
110
 
111
            SQLRow rowEncaisseElt = null;
83 ilm 112
            // On cre un encaissement
18 ilm 113
            if (createEncaisse) {
73 ilm 114
                SQLRowValues rowVals = new SQLRowValues(tableEncaisse);
115
                rowVals.put("MONTANT", ttc.getLongValue());
156 ilm 116
                rowVals.put("ID_CLIENT", rowClient != null ? rowClient.getID() : null);
117
                rowVals.put("TIERS", tiers);
73 ilm 118
                rowVals.put("DATE", this.date);
119
                if (typeRegRow.getID() >= TypeReglementSQLElement.TRAITE) {
120
                    Calendar c2 = modeReglement.getDate("DATE_VIREMENT");
121
                    if (c2 != null) {
122
                        rowVals.put("DATE", c2.getTime());
18 ilm 123
                    }
73 ilm 124
                }
125
                SQLRowValues rowValsRegl = new SQLRowValues(modeReglement.asRowValues());
126
                SQLRow copy = rowValsRegl.insert();
127
                rowVals.put("ID_MODE_REGLEMENT", copy.getID());
128
                rowVals.put("NOM", label);
129
                rowEncaisse = rowVals.insert();
130
                SQLRowValues rowValsElt = new SQLRowValues(tableEncaisseElt);
131
                rowValsElt.put("MONTANT_REGLE", ttc.getLongValue());
132
                rowValsElt.put("ID_ENCAISSER_MONTANT", rowEncaisse.getID());
133
                rowEncaisseElt = rowValsElt.insert();
18 ilm 134
 
135
            }
149 ilm 136
            if (!source.getTable().getName().equalsIgnoreCase("ENCAISSER_MONTANT") || !source.getBoolean("ACOMPTE")) {
137
                this.idMvt = getNewMouvement(rowEncaisse.getTable().getName(), rowEncaisse.getID(), mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
138
            }
139
            this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
18 ilm 140
 
141
            SQLRowValues rowVals = rowEncaisse.createEmptyUpdateRow();
142
            rowVals.put("ID_MOUVEMENT", this.idMvt);
73 ilm 143
 
144
            rowVals.update();
145
 
18 ilm 146
            if (rowEncaisseElt != null) {
147
                SQLRowValues rowVals2 = rowEncaisseElt.createEmptyUpdateRow();
148
                rowVals2.put("ID_MOUVEMENT_ECHEANCE", this.idMvt);
73 ilm 149
                rowVals2.update();
18 ilm 150
            }
151
 
152
            // Cheque
153
            if (typeRegRow.getID() == TypeReglementSQLElement.CHEQUE) {
154
 
155
                Date dateTmp = this.date;
156
                if (modeReglement.getObject("DATE") != null) {
157
                    dateTmp = modeReglement.getDate("DATE").getTime();
158
                }
177 ilm 159
                // On fixe la date du règlement de la facture à reception du cheque
160
                setDateReglement(source, this.date);
18 ilm 161
 
162
                Calendar c = modeReglement.getDate("DATE_DEPOT");
163
                if (c != null) {
156 ilm 164
                    paiementCheque(c.getTime(), source, ttc, rowClient, modeReglement, mvtSource.getTable().getRow(idMvt), avance, tiers, cptTiers);
18 ilm 165
                } else {
156 ilm 166
                    paiementCheque(this.date, source, ttc, rowClient, modeReglement, mvtSource.getTable().getRow(idMvt), avance, tiers, cptTiers);
18 ilm 167
                }
168
 
169
            } else {
170
                // On fixe la date du règlement de la facture
171
                if (typeRegRow.getID() >= TypeReglementSQLElement.TRAITE) {
172
                    Calendar c2 = modeReglement.getDate("DATE_VIREMENT");
173
                    if (c2 == null) {
174
                        setDateReglement(source, this.date);
175
                    } else {
176
                        setDateReglement(source, c2.getTime());
177
                    }
178
 
179
                } else {
180
                    setDateReglement(source, this.date);
181
                }
182
                if (typeRegRow.getID() == TypeReglementSQLElement.ESPECE) {
149 ilm 183
                    this.putValue("ID_JOURNAL", JournalSQLElement.CAISSES);
18 ilm 184
                }
185
 
177 ilm 186
                if (typeRegRow.getID() == TypeReglementSQLElement.CB && this.rowPrefsCompte.getTable().contains("ID_JOURNAL_CB_ATTENTE")
187
                        && !this.rowPrefsCompte.isForeignEmpty("ID_JOURNAL_CB_ATTENTE")) {
188
                    this.putValue("ID_JOURNAL", this.rowPrefsCompte.getForeignID("ID_JOURNAL_CB_ATTENTE"));
189
                }
190
 
156 ilm 191
                int idCompteClient = cptTiers != null && !cptTiers.isUndefined() ? cptTiers.getID() : rowClient.getInt("ID_COMPTE_PCE");
142 ilm 192
                if (avance) {
193
                    idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_AVANCE_CLIENT");
18 ilm 194
                    if (idCompteClient <= 1) {
142 ilm 195
                        idCompteClient = ComptePCESQLElement.getIdComptePceDefault("AvanceClients");
18 ilm 196
                    }
142 ilm 197
                } else {
198
 
199
                    // compte Clients
200
 
201
                    if (idCompteClient <= 1) {
202
                        idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
203
                        if (idCompteClient <= 1) {
204
                            idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
205
                        }
206
                    }
73 ilm 207
                }
149 ilm 208
                this.putValue("ID_COMPTE_PCE", idCompteClient);
209
                this.putValue("DEBIT", Long.valueOf(0));
210
                this.putValue("CREDIT", Long.valueOf(ttc.getLongValue()));
142 ilm 211
 
83 ilm 212
                this.ecrClient = ajoutEcriture();
18 ilm 213
 
73 ilm 214
                // compte de reglement, caisse, cheque, ...
83 ilm 215
                if (typeRegRow.getID() == TypeReglementSQLElement.ESPECE) {
216
                    int idCompteRegl = typeRegRow.getInt("ID_COMPTE_PCE_CLIENT");
217
                    if (idCompteRegl <= 1) {
218
                        idCompteRegl = ComptePCESQLElement.getIdComptePceDefault("VenteEspece");
219
                    }
220
 
149 ilm 221
                    this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteRegl));
83 ilm 222
                } else {
177 ilm 223
                    try {
224
                        fillCompteBanqueFromRow(modeReglement, "VenteCB", false);
225
                    } catch (Exception e) {
226
                        throw new SQLException(e);
227
                    }
83 ilm 228
                }
177 ilm 229
                if (typeRegRow.getID() == TypeReglementSQLElement.CB && this.rowPrefsCompte.getTable().contains("ID_COMPTE_PCE_CB_ATTENTE")
230
                        && !this.rowPrefsCompte.isForeignEmpty("ID_COMPTE_PCE_CB_ATTENTE")) {
231
 
232
                    this.putValue("ID_COMPTE_PCE", this.rowPrefsCompte.getForeignID("ID_COMPTE_PCE_CB_ATTENTE"));
233
                }
234
 
149 ilm 235
                this.putValue("DEBIT", Long.valueOf(ttc.getLongValue()));
236
                this.putValue("CREDIT", Long.valueOf(0));
73 ilm 237
                ajoutEcriture();
238
 
151 ilm 239
                List<Integer> pieceIDs = new ArrayList<Integer>();
240
                if (source.getTable().getName().equals("ENCAISSER_MONTANT")) {
241
                    List<SQLRow> l = source.getReferentRows(base.getTable("ENCAISSER_MONTANT_ELEMENT"));
242
                    for (SQLRow sqlRow : l) {
243
                        pieceIDs.add(sqlRow.getForeign("ID_MOUVEMENT_ECHEANCE").getForeignID("ID_PIECE"));
244
                    }
245
                } else {
246
                    pieceIDs.add(mvtSource.getForeignID("ID_PIECE"));
247
                }
248
                lettrageAuto(pieceIDs, d);
18 ilm 249
            }
250
        } else {
251
 
252
                Date dateEch = ModeDeReglementSQLElement.calculDate(modeReglement.getInt("AJOURS"), modeReglement.getInt("LENJOUR"), this.date);
253
 
83 ilm 254
                System.out.println("Echance client");
18 ilm 255
 
256
                // Ajout dans echeance
21 ilm 257
                final SQLTable tableEch = base.getTable("ECHEANCE_CLIENT");
258
                SQLRowValues valEcheance = new SQLRowValues(tableEch);
18 ilm 259
 
260
                this.idMvt = getNewMouvement("ECHEANCE_CLIENT", 1, mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
261
                valEcheance.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
262
                valEcheance.put("DATE", dateEch);
263
                valEcheance.put("MONTANT", Long.valueOf(ttc.getLongValue()));
156 ilm 264
                valEcheance.put("ID_CLIENT", rowClient == null ? null : rowClient.getID());
265
                valEcheance.put("TIERS", tiers);
266
                valEcheance.put("ID_COMPTE_PCE_TIERS", cptTiers == null || !cptTiers.isUndefined() ? null : cptTiers.getID());
73 ilm 267
                if (source.getTable().equals(tableSaisieVenteFacture)) {
268
                    valEcheance.put("ID_SAISIE_VENTE_FACTURE", source.getID());
269
                }
177 ilm 270
                if (source.getTable().getName().equals(valEcheance.getTable().getName())) {
271
                    valEcheance.put("ID_SAISIE_VENTE_FACTURE", source.getObject("ID_SAISIE_VENTE_FACTURE"));
272
                }
273
                if (modeReglement.getForeign("ID_TYPE_REGLEMENT").getBoolean("SEPA")) {
274
                    final String foreignBanqueFieldName = "ID_" + BanqueSQLElement.TABLENAME;
275
                    if (valEcheance.getTable().contains(foreignBanqueFieldName))
276
                        valEcheance.put(foreignBanqueFieldName, modeReglement.getInt(foreignBanqueFieldName));
18 ilm 277
 
177 ilm 278
                    valEcheance.put("ETS", modeReglement.getObject("ETS"));
279
 
280
                    if (!modeReglement.isForeignEmpty("ID_SEPA_MANDATE")) {
281
                        valEcheance.put("ID_SEPA_MANDATE", modeReglement.getForeignID("ID_SEPA_MANDATE"));
282
                    } else if (rowClient != null && !rowClient.isForeignEmpty("ID_SEPA_MANDATE_DEFAULT")) {
283
                        valEcheance.put("ID_SEPA_MANDATE", rowClient.getForeignID("ID_SEPA_MANDATE_DEFAULT"));
284
                    } else {
285
                        SEPAMandateSQLElement mandateElem = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(SEPAMandateSQLElement.class);
286
                        final String mandateID = mandateElem.generateMandateIdentification("Facturation", '0', true, true);
287
                        // TODO gestion si pas de client (ex : don ou cotisation association)
288
                        if (rowClient != null) {
289
                            final SQLRowValues newVals = mandateElem.createRecurrent(rowClient.getID(), mandateID, source.getDate("DATE").getTime());
290
                            SQLRow rowMandate = newVals.commit();
291
                            valEcheance.put("ID_SEPA_MANDATE", rowMandate.getID());
292
                            rowClient.createEmptyUpdateRow().put("ID_SEPA_MANDATE_DEFAULT", rowMandate.getID()).commit();
293
                        }
294
                    }
295
                }
296
 
73 ilm 297
                // ajout de l'ecriture
298
                SQLRow row = valEcheance.insert();
299
                SQLRowValues rowVals = new SQLRowValues(tableMouvement);
300
                rowVals.put("IDSOURCE", row.getID());
301
                rowVals.update(this.idMvt);
18 ilm 302
 
303
        }
304
    }
305
 
73 ilm 306
    private void setDateReglement(SQLRow source, Date d) throws SQLException {
18 ilm 307
        List<SQLRow> sources = new ArrayList<SQLRow>();
308
        if (source.getTable().getName().equalsIgnoreCase("ENCAISSER_MONTANT")) {
309
 
310
            List<SQLRow> rows = source.getReferentRows(source.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
311
            for (SQLRow sqlRow : rows) {
312
                SQLRow rowEch = sqlRow.getForeignRow("ID_ECHEANCE_CLIENT");
313
                if (rowEch != null && rowEch.getID() > 1) {
314
                    SQLRow rowMvt = tableMouvement.getRow(MouvementSQLElement.getSourceId(rowEch.getInt("ID_MOUVEMENT")));
315
                    if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
316
                        sources.add(tableSaisieVenteFacture.getRow(rowMvt.getInt("IDSOURCE")));
317
                    }
318
                }
319
            }
320
 
321
        } else {
322
            sources.add(source);
323
        }
324
        for (SQLRow sqlRow : sources) {
325
            if (sqlRow.getTable().getName().equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
326
                SQLRowValues rowValsUpdateVF = sqlRow.createEmptyUpdateRow();
327
                rowValsUpdateVF.put("DATE_REGLEMENT", new Timestamp(d.getTime()));
73 ilm 328
                rowValsUpdateVF.update();
18 ilm 329
            }
330
        }
331
 
332
    }
333
 
83 ilm 334
    public void doLettrageAuto(final SQLRowAccessor source, Date dateLettrage) {
335
        // A. On lettre les critures client (facture ET reglement)
336
        // A1. Recherche criture client de facturation
337
 
338
        final SQLRowValues g1 = new SQLRowValues(ecritureTable);
339
        g1.put("DEBIT", null);
340
        final SQLRowValuesListFetcher fetch1 = new SQLRowValuesListFetcher(g1);
341
        fetch1.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
342
            @Override
343
            public SQLSelect transformChecked(SQLSelect input) {
344
                input.setWhere(new Where(ecritureTable.getField("ID_MOUVEMENT"), "=", source.getForeignID("ID_MOUVEMENT")));
345
                input.andWhere(new Where(ecritureTable.getField("ID_COMPTE_PCE"), "=", ecrClient.getForeignID("ID_COMPTE_PCE")));
346
                input.andWhere(new Where(ecritureTable.getField("JOURNAL_CODE"), "=", "VE"));
347
                return input;
348
            }
349
 
350
        });
351
        final List<SQLRowValues> rowsEcriture1 = fetch1.fetch();
352
        if (rowsEcriture1.size() != 1) {
353
            System.out.println("critures VE trouves. Erreur");
354
            return;
355
        }
356
        final SQLRowValues rEcriture1 = rowsEcriture1.get(0);
357
        System.out.println("Ecriture vente: " + rEcriture1.getID());
358
        System.out.println("Ecriture paiement: " + this.ecrClient);
359
 
360
        // Récupère lettrage
361
        String codeLettre = NumerotationAutoSQLElement.getNextCodeLettrage();
362
 
363
        // TODO: vérifier somme = 0
364
 
365
        // Met à  jour les 2 écritures
366
        SQLRowValues rowVals = new SQLRowValues(ecritureTable);
367
        rowVals.put("LETTRAGE", codeLettre);
368
        rowVals.put("DATE_LETTRAGE", dateLettrage);
369
        try {
370
            rowVals.update(rEcriture1.getID());
371
            rowVals.update(this.ecrClient.getID());
372
        } catch (SQLException e1) {
373
            e1.printStackTrace();
374
        }
375
 
376
        // Mise à  jour du code de lettrage
377
        SQLElement numElt = Configuration.getInstance().getDirectory().getElement("NUMEROTATION_AUTO");
378
        SQLRowValues rowVals1 = numElt.getTable().getRow(2).createEmptyUpdateRow();
379
        rowVals1.put("CODE_LETTRAGE", codeLettre);
380
        try {
381
            rowVals1.update();
382
        } catch (SQLException e) {
383
            e.printStackTrace();
384
        }
385
    }
386
 
177 ilm 387
    private void paiementCheque(Date dateEch, SQLRow source, PrixTTC ttc, SQLRow rowClient, SQLRow modeRegl, SQLRow mvtSource, boolean avance, String tiers, SQLRowAccessor cptTiers)
388
            throws SQLException {
18 ilm 389
 
390
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_A_ENCAISSER"));
144 ilm 391
        SQLPreferences prefs = SQLPreferences.getMemCached(valCheque.getTable().getDBRoot());
392
        boolean createEcr = prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.CREATE_ECR_CHQ, true);
393
 
156 ilm 394
        valCheque.put("ID_CLIENT", rowClient == null ? null : rowClient.getID());
395
        valCheque.put("ID_COMPTE_PCE_TIERS", cptTiers == null ? null : cptTiers.getID());
396
        valCheque.put("TIERS", tiers);
144 ilm 397
        valCheque.put("SANS_VALEUR_ENCAISSEMENT", !createEcr);
83 ilm 398
        final String foreignBanqueFieldName = "ID_" + BanqueSQLElement.TABLENAME;
399
        if (valCheque.getTable().contains(foreignBanqueFieldName))
400
            valCheque.put(foreignBanqueFieldName, modeRegl.getInt(foreignBanqueFieldName));
401
 
18 ilm 402
        valCheque.put("NUMERO", modeRegl.getObject("NUMERO"));
403
        valCheque.put("DATE", modeRegl.getObject("DATE"));
404
        valCheque.put("ETS", modeRegl.getObject("ETS"));
405
        valCheque.put("DATE_VENTE", this.date);
406
        this.idMvt = getNewMouvement("CHEQUE_A_ENCAISSER", 1, mvtSource.getID(), mvtSource.getInt("ID_PIECE"));
407
        valCheque.put("DATE_MIN_DEPOT", dateEch);
408
        valCheque.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
409
        valCheque.put("MONTANT", Long.valueOf(ttc.getLongValue()));
410
 
73 ilm 411
        if (valCheque.getInvalid() == null) {
412
            // ajout de l'ecriture
413
            SQLRow row = valCheque.insert();
414
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
415
            rowVals.put("IDSOURCE", row.getID());
416
            rowVals.update(this.idMvt);
18 ilm 417
        }
73 ilm 418
 
144 ilm 419
        if (createEcr) {
156 ilm 420
            int idCompteClient = cptTiers == null || cptTiers.isUndefined() ? rowClient.getInt("ID_COMPTE_PCE") : cptTiers.getID();
144 ilm 421
            if (avance) {
422
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_AVANCE_CLIENT");
423
                if (idCompteClient <= 1) {
424
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("AvanceClients");
425
                }
426
            } else {
142 ilm 427
 
144 ilm 428
                // compte Clients
142 ilm 429
 
430
                if (idCompteClient <= 1) {
144 ilm 431
                    idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
432
                    if (idCompteClient <= 1) {
433
                        idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
434
                    }
142 ilm 435
                }
436
            }
437
 
174 ilm 438
            int idJournal = JournalSQLElement.BANQUES;
439
            if (rowPrefsCompte.getObject("ID_JOURNAL_VALEUR_ENCAISSEMENT") != null && !rowPrefsCompte.isForeignEmpty("ID_JOURNAL_VALEUR_ENCAISSEMENT")) {
440
                idJournal = rowPrefsCompte.getForeignID("ID_JOURNAL_VALEUR_ENCAISSEMENT");
441
            }
177 ilm 442
 
174 ilm 443
            this.putValue("ID_JOURNAL", idJournal);
149 ilm 444
            this.putValue("ID_COMPTE_PCE", idCompteClient);
445
            this.putValue("DEBIT", Long.valueOf(0));
446
            this.putValue("CREDIT", Long.valueOf(ttc.getLongValue()));
142 ilm 447
 
144 ilm 448
            this.ecrClient = ajoutEcriture();
449
 
450
            // compte de reglement, caisse, cheque, ...
451
            int idCompteRegl = rowPrefsCompte.getInt("ID_COMPTE_PCE_VALEUR_ENCAISSEMENT");
452
            if (idCompteRegl <= 1) {
453
                idCompteRegl = ComptePCESQLElement.getIdComptePceDefault("ValeurEncaissement");
454
            }
149 ilm 455
            this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteRegl));
456
            this.putValue("DEBIT", Long.valueOf(ttc.getLongValue()));
457
            this.putValue("CREDIT", Long.valueOf(0));
144 ilm 458
 
459
            ajoutEcriture();
142 ilm 460
        }
461
 
18 ilm 462
    }
463
}