OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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