OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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