OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
182 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
 
67 ilm 16
import org.openconcerto.erp.core.common.ui.TotalCalculator;
18 ilm 17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
94 ilm 19
import org.openconcerto.erp.generationDoc.SheetXml;
182 ilm 20
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
67 ilm 21
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
22
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
18 ilm 23
import org.openconcerto.erp.model.PrixTTC;
24
import org.openconcerto.sql.model.SQLRow;
67 ilm 25
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.utils.ExceptionHandler;
149 ilm 29
import org.openconcerto.utils.StringUtils;
18 ilm 30
 
67 ilm 31
import java.math.BigDecimal;
32
import java.math.RoundingMode;
18 ilm 33
import java.util.Date;
34
import java.util.Map;
35
 
36
// FIXME probleme lors de certaines generation tout reste figer
37
 
38
/**
39
 * Génération des ecritures associées à une saisie de vente avec facture. Entaine la génération du
40
 * reglement de la vente
41
 */
42
public class GenerationMvtSaisieVenteFacture extends GenerationEcritures implements Runnable {
43
 
67 ilm 44
    public static final String ID = "accounting.records.invoice.sales";
18 ilm 45
    private static final String source = "SAISIE_VENTE_FACTURE";
46
    public static final Integer journal = Integer.valueOf(JournalSQLElement.VENTES);
47
    private int idSaisieVenteFacture;
83 ilm 48
    private boolean useComptePCEVente;
18 ilm 49
    private static final SQLTable saisieVFTable = base.getTable("SAISIE_VENTE_FACTURE");
50
    private static final SQLTable mvtTable = base.getTable("MOUVEMENT");
67 ilm 51
    private static final SQLTable ecrTable = base.getTable("ECRITURE");
18 ilm 52
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
53
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
93 ilm 54
    private final boolean genereReglement;
18 ilm 55
 
56
    /**
57
     * Generation de la comptabilité associée à la modification d'une saisie de vente facture
58
     *
59
     * @param idSaisieVenteFacture
60
     * @param idMvt id du mouvement qui est dejà associé à la facture
61
     */
93 ilm 62
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture, int idMvt, boolean useComptePCEVente, boolean genereReglement) {
151 ilm 63
        this(idSaisieVenteFacture, idMvt, useComptePCEVente, genereReglement, false);
64
    }
65
 
66
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture, int idMvt, boolean useComptePCEVente, boolean genereReglement, boolean threadSafe) {
18 ilm 67
        System.err.println("********* init GeneRation");
68
        this.idMvt = idMvt;
69
        this.idSaisieVenteFacture = idSaisieVenteFacture;
83 ilm 70
        this.useComptePCEVente = useComptePCEVente;
93 ilm 71
        this.genereReglement = genereReglement;
94 ilm 72
        // Submit in sheetxml queue in order to get the good paiement in document
151 ilm 73
        if (!threadSafe)
74
            SheetXml.submitInQueue(GenerationMvtSaisieVenteFacture.this);
18 ilm 75
    }
76
 
93 ilm 77
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture, int idMvt, boolean useComptePCEVente) {
78
        this(idSaisieVenteFacture, idMvt, useComptePCEVente, true);
79
    }
80
 
83 ilm 81
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture, int idMvt) {
93 ilm 82
        this(idSaisieVenteFacture, idMvt, false, true);
83 ilm 83
    }
84
 
18 ilm 85
    /**
86
     * Generation de la comptabilité associée à la création d'une saisie de vente facture
87
     *
88
     * @param idSaisieVenteFacture
89
     */
90
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture) {
151 ilm 91
        this(idSaisieVenteFacture, false);
18 ilm 92
    }
93
 
151 ilm 94
    public GenerationMvtSaisieVenteFacture(int idSaisieVenteFacture, final boolean threadSafe) {
95
        this(idSaisieVenteFacture, 1, false, true, threadSafe);
96
    }
18 ilm 97
 
151 ilm 98
    public final void genereMouvement() throws Exception {
99
 
18 ilm 100
        SQLRow saisieRow = GenerationMvtSaisieVenteFacture.saisieVFTable.getRow(this.idSaisieVenteFacture);
93 ilm 101
        setRowAnalytiqueSource(saisieRow);
18 ilm 102
        SQLRow clientRow = saisieRow.getForeignRow("ID_CLIENT");
103
 
104
        // Calcul des montants
105
        PrixTTC prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue());
65 ilm 106
        // Total des acomptes déjà versés sur la facture
107
        long montantAcompteTTC = 0;
18 ilm 108
 
65 ilm 109
        int idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
110
 
111
        Boolean acompte = saisieRow.getBoolean("ACOMPTE");
112
        if (acompte != null && acompte) {
113
            this.nom = "Fact. acompte client" + saisieRow.getObject("NUMERO").toString();
114
        } else {
115
            this.nom = "Fact. vente " + saisieRow.getObject("NUMERO").toString();
116
        }
151 ilm 117
        this.nom += " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
149 ilm 118
        this.putValue("NOM", this.nom);
67 ilm 119
 
120
        // iniatilisation des valeurs de la map
121
        this.date = (Date) saisieRow.getObject("DATE");
122
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
123
        provider.putLabel(saisieRow, this.mEcritures);
124
 
149 ilm 125
        this.putValue("DATE", this.date);
126
        this.putValue("ID_JOURNAL", GenerationMvtSaisieVenteFacture.journal);
127
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));
18 ilm 128
 
129
        // on calcule le nouveau numero de mouvement
130
        if (this.idMvt == 1) {
67 ilm 131
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
132
            provider.putPieceLabel(saisieRow, rowValsPiece);
133
            getNewMouvement(GenerationMvtSaisieVenteFacture.source, this.idSaisieVenteFacture, 1, rowValsPiece);
18 ilm 134
        } else {
149 ilm 135
            this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
65 ilm 136
            SQLRowValues rowValsPiece = mvtTable.getRow(idMvt).getForeign("ID_PIECE").asRowValues();
67 ilm 137
            provider.putPieceLabel(saisieRow, rowValsPiece);
73 ilm 138
            rowValsPiece.update();
18 ilm 139
        }
140
 
67 ilm 141
        SQLTable tableEchantillon = null;
142
        BigDecimal portHT = BigDecimal.valueOf(saisieRow.getLong("PORT_HT")).movePointLeft(2);
156 ilm 143
        BigDecimal fraisDocHT = BigDecimal.valueOf(saisieRow.getLong("FRAIS_DOCUMENT_HT")).movePointLeft(2);
182 ilm 144
        SQLRow taxeDoc = saisieRow.getNonEmptyForeign("ID_TAXE_FRAIS_DOCUMENT") == null ? TaxeCache.getCache().getFirstTaxe()
145
                : TaxeCache.getCache().getRowFromId(saisieRow.getForeignID("ID_TAXE_FRAIS_DOCUMENT")).asRow();
146
 
93 ilm 147
        TotalCalculator calc;
182 ilm 148
        SQLRow taxePort = saisieRow.getNonEmptyForeign("ID_TAXE_PORT") == null ? TaxeCache.getCache().getFirstTaxe()
149
                : TaxeCache.getCache().getRowFromId(saisieRow.getForeignID("ID_TAXE_PORT")).asRow();
150
 
93 ilm 151
        if (clientRow.getTable().contains("ID_COMPTE_PCE_PRODUIT") && !clientRow.isForeignEmpty("ID_COMPTE_PCE_PRODUIT")) {
182 ilm 152
 
153
            calc = getValuesFromElement(false, false, "T_PV_HT", saisieRow, saisieVFTable.getTable("SAISIE_VENTE_FACTURE_ELEMENT"), portHT, taxePort, fraisDocHT, taxeDoc, tableEchantillon,
154
                    clientRow.getForeign("ID_COMPTE_PCE_PRODUIT"));
93 ilm 155
        } else {
182 ilm 156
 
157
            calc = getValuesFromElement(saisieRow, saisieVFTable.getTable("SAISIE_VENTE_FACTURE_ELEMENT"), portHT, taxePort, fraisDocHT, taxeDoc, tableEchantillon);
93 ilm 158
        }
67 ilm 159
 
65 ilm 160
        // On génére les ecritures si la facture n'est pas un acompte
67 ilm 161
        long ttcLongValue = calc.getTotalTTC().movePointRight(2).longValue();
65 ilm 162
        if (acompte == null || !acompte) {
18 ilm 163
 
144 ilm 164
            Map<SQLRowAccessor, Map<SQLRowAccessor, BigDecimal>> taxeCompl = calc.getMapHtTaxeCompl();
67 ilm 165
            for (SQLRowAccessor row : calc.getMapHt().keySet()) {
166
                long b = calc.getMapHt().get(row).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
167
                if (b != 0) {
83 ilm 168
                    final Integer idComptePCE;
144 ilm 169
                    long taxe = 0;
170
 
171
                    if (taxeCompl.containsKey(row)) {
172
                        Map<SQLRowAccessor, BigDecimal> compl = taxeCompl.get(row);
173
                        for (SQLRowAccessor rowCompl : compl.keySet()) {
174
                            if (compl.get(rowCompl) != null) {
175
                                long taxeC = compl.get(rowCompl).multiply(rowCompl.getBigDecimal("POURCENT").movePointLeft(2)).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
149 ilm 176
                                this.putValue("ID_COMPTE_PCE", rowCompl.getForeignID("ID_COMPTE_PCE"));
177
                                this.putValue("DEBIT", Long.valueOf(0));
178
                                this.putValue("CREDIT", Long.valueOf(taxeC));
144 ilm 179
                                ajoutEcriture();
180
                                taxe += taxeC;
149 ilm 181
                                // this.putValue("ID_COMPTE_PCE",
144 ilm 182
                                // rowCompl.getForeignID("ID_COMPTE_PCE_PRODUITS"));
149 ilm 183
                                // this.putValue("DEBIT", Long.valueOf(taxeC));
184
                                // this.putValue("CREDIT", Long.valueOf(0));
144 ilm 185
                                // ajoutEcriture();
186
                            }
187
                        }
188
                    }
189
                    // TODO check if taxe compl > b
83 ilm 190
                    if (this.useComptePCEVente) {
191
                        // Utilise le compte de la facture
192
                        idComptePCE = saisieRow.getInt("ID_COMPTE_PCE_VENTE");
193
                    } else {
194
                        idComptePCE = Integer.valueOf(row.getID());
195
                    }
144 ilm 196
 
149 ilm 197
                    this.putValue("ID_COMPTE_PCE", idComptePCE);
198
                    this.putValue("DEBIT", Long.valueOf(0));
199
                    this.putValue("CREDIT", Long.valueOf(b - taxe));
93 ilm 200
                    ajoutEcriture();
18 ilm 201
                }
202
            }
203
 
67 ilm 204
            Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
205
            for (SQLRowAccessor rowAc : tvaMap.keySet()) {
206
                long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
207
                if (longValue != 0) {
149 ilm 208
                    this.putValue("ID_COMPTE_PCE", rowAc.getID());
209
                    this.putValue("DEBIT", Long.valueOf(0));
210
                    this.putValue("CREDIT", longValue);
18 ilm 211
                    ajoutEcriture();
212
                }
67 ilm 213
            }
65 ilm 214
 
215
            // compte Clients
18 ilm 216
 
217
            if (idCompteClient <= 1) {
65 ilm 218
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
219
                if (idCompteClient <= 1) {
73 ilm 220
                    idCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
18 ilm 221
                }
222
            }
149 ilm 223
            this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
67 ilm 224
            if (ecrTable.contains("CODE_CLIENT")) {
149 ilm 225
                this.putValue("CODE_CLIENT", clientRow.getString("CODE"));
67 ilm 226
            }
149 ilm 227
            this.putValue("DEBIT", ttcLongValue);
228
            this.putValue("CREDIT", Long.valueOf(0));
65 ilm 229
            ajoutEcriture();
230
 
231
            // TODO Gestion des factures d'acomptes
232
            // Solde des acomptes
233
            // List<SQLRow> rowsAcompte =
234
            // saisieRow.getReferentRows(saisieVFTable.getField("ID_SAISIE_VENTE_FACTURE_ACOMPTE"));
235
            // if (rowsAcompte != null && rowsAcompte.size() > 0) {
236
            // // Compte acompte
237
            // int idCompteAcompteClient = ComptePCESQLElement.getId("4191",
238
            // "Clients - Avances et acomptes reçus sur commandes");
239
            // int idTVAAcompte = ComptePCESQLElement.getId("44587",
240
            // "Taxes sur le chiffre d'affaire à régulariser ou en attente");
241
            // for (SQLRow sqlRow : rowsAcompte) {
242
            // long acompteTTC = sqlRow.getLong("T_TTC");
243
            // long acompteHT = sqlRow.getLong("T_HT");
149 ilm 244
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteAcompteClient));
245
            // this.putValue("DEBIT", acompteTTC);
246
            // this.putValue("CREDIT", Long.valueOf(0));
65 ilm 247
            // ajoutEcriture();
149 ilm 248
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteClient));
249
            // this.putValue("DEBIT", Long.valueOf(0));
250
            // this.putValue("CREDIT", acompteTTC);
65 ilm 251
            // ajoutEcriture();
252
            //
253
            // montantAcompteTTC += acompteTTC;
254
            //
255
            // long tva = acompteTTC - acompteHT;
256
            // if (tva > 0) {
257
            //
258
            //
149 ilm 259
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(idTVAAcompte));
260
            // this.putValue("DEBIT", Long.valueOf(0));
261
            // this.putValue("CREDIT", Long.valueOf(tva));
65 ilm 262
            // ajoutEcriture();
263
            //
264
            // Map<Integer, Long> m = getMultiTVAFromRow(saisieRow,
265
            // saisieVFTable.getTable("SAISIE_VENTE_FACTURE_ELEMENT"), true);
266
            // long allTaxe = 0;
267
            // for (Integer i : m.keySet()) {
268
            // Long l = m.get(i);
269
            // if (l != null && l > 0) {
270
            // // FIXME
271
            // int idCpt = i;
272
            // if (idCpt <= 1) {
273
            // idCpt = idCompteTVA;
274
            // }
149 ilm 275
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCpt));
276
            // this.putValue("DEBIT", Long.valueOf(0));
277
            // this.putValue("CREDIT", Long.valueOf(l));
65 ilm 278
            // ajoutEcriture();
279
            // allTaxe += l;
280
            // }
281
            // }
282
            // if (allTaxe < prixTVA.getLongValue()) {
149 ilm 283
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteTVA));
284
            // this.putValue("DEBIT", Long.valueOf(0));
285
            // this.putValue("CREDIT", Long.valueOf(prixTVA.getLongValue() - allTaxe));
65 ilm 286
            // ajoutEcriture();
287
            // }
288
            //
149 ilm 289
            // this.putValue("ID_COMPTE_PCE", Integer.valueOf(compteDebitTvaAcompte));
290
            // this.putValue("DEBIT", Long.valueOf(tva));
291
            // this.putValue("CREDIT", Long.valueOf(0));
65 ilm 292
            // ajoutEcriture();
293
            // }
294
            // }
295
            // }
18 ilm 296
        }
73 ilm 297
 
298
        {
299
            SQLRowValues valSasieVF = new SQLRowValues(GenerationMvtSaisieVenteFacture.saisieVFTable);
300
            valSasieVF.put("DATE_REGLEMENT", null);
301
            valSasieVF.update(this.idSaisieVenteFacture);
302
        }
303
 
93 ilm 304
            if (genereReglement) {
305
                // Génération du reglement
306
                SQLRow modeRegl = saisieRow.getForeignRow("ID_MODE_REGLEMENT");
307
                final SQLRow typeRegRow = modeRegl.getForeignRow("ID_TYPE_REGLEMENT");
149 ilm 308
                String label = this.nom + " (" + typeRegRow.getString("NOM") + ") " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
93 ilm 309
                int idAvoir = saisieRow.getInt("ID_AVOIR_CLIENT");
310
                if (idAvoir > 1) {
311
                    // SQLRow avoirRow = base.getTable("AVOIR_CLIENT").getRow(idAvoir);
312
                    long l = ((Number) saisieRow.getObject("T_AVOIR_TTC")).longValue();
313
                    prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue() - l);
314
                }
94 ilm 315
                prixTTC = new PrixTTC(saisieRow.getLong("NET_A_PAYER"));
93 ilm 316
                if (prixTTC.getLongValue() > 0) {
149 ilm 317
                    new GenerationReglementVenteNG(label.trim(), clientRow, prixTTC, this.date, modeRegl, saisieRow, mvtTable.getRow(idMvt));
93 ilm 318
                }
18 ilm 319
            }
320
        // Mise à jour de mouvement associé à la facture
321
 
322
        SQLRowValues valSasieVF = new SQLRowValues(GenerationMvtSaisieVenteFacture.saisieVFTable);
323
        valSasieVF.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
324
 
73 ilm 325
        if (valSasieVF.getInvalid() == null) {
326
            valSasieVF.update(this.idSaisieVenteFacture);
80 ilm 327
            displayMvtNumber();
73 ilm 328
        }
18 ilm 329
 
330
    }
331
 
332
    public void run() {
333
        try {
334
            genereMouvement();
73 ilm 335
        } catch (Exception e) {
18 ilm 336
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
73 ilm 337
 
18 ilm 338
        }
339
    }
340
}