OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 182 Rev 185
Line 14... Line 14...
14
 package org.openconcerto.erp.generationEcritures;
14
 package org.openconcerto.erp.generationEcritures;
15
 
15
 
16
import org.openconcerto.erp.core.common.ui.TotalCalculator;
16
import org.openconcerto.erp.core.common.ui.TotalCalculator;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
19
import org.openconcerto.erp.generationDoc.SheetXml;
19
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
20
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
20
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
-
 
21
import org.openconcerto.erp.generationDoc.SheetXml;
21
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
22
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
22
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
23
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
23
import org.openconcerto.erp.model.PrixTTC;
24
import org.openconcerto.erp.model.PrixTTC;
-
 
25
import org.openconcerto.erp.preferences.DefaultNXProps;
-
 
26
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
24
import org.openconcerto.sql.model.SQLRow;
27
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
28
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
30
import org.openconcerto.sql.model.SQLTable;
-
 
31
import org.openconcerto.sql.preferences.SQLPreferences;
-
 
32
import org.openconcerto.utils.DecimalUtils;
28
import org.openconcerto.utils.ExceptionHandler;
33
import org.openconcerto.utils.ExceptionHandler;
29
import org.openconcerto.utils.StringUtils;
34
import org.openconcerto.utils.StringUtils;
30
 
35
 
31
import java.math.BigDecimal;
36
import java.math.BigDecimal;
32
import java.math.RoundingMode;
37
import java.math.RoundingMode;
Line 40... Line 45...
40
 * reglement de la vente
45
 * reglement de la vente
41
 */
46
 */
42
public class GenerationMvtSaisieVenteFacture extends GenerationEcritures implements Runnable {
47
public class GenerationMvtSaisieVenteFacture extends GenerationEcritures implements Runnable {
43
 
48
 
44
    public static final String ID = "accounting.records.invoice.sales";
49
    public static final String ID = "accounting.records.invoice.sales";
-
 
50
    public static final String NOT_GEN_ECRITURE = "accounting.invoice.generation.disable";
45
    private static final String source = "SAISIE_VENTE_FACTURE";
51
    private static final String source = "SAISIE_VENTE_FACTURE";
46
    public static final Integer journal = Integer.valueOf(JournalSQLElement.VENTES);
52
    public static final Integer journal = Integer.valueOf(JournalSQLElement.VENTES);
47
    private int idSaisieVenteFacture;
53
    private int idSaisieVenteFacture;
48
    private boolean useComptePCEVente;
54
    private boolean useComptePCEVente;
49
    private static final SQLTable saisieVFTable = base.getTable("SAISIE_VENTE_FACTURE");
55
    private static final SQLTable saisieVFTable = base.getTable("SAISIE_VENTE_FACTURE");
Line 97... Line 103...
97
 
103
 
98
    public final void genereMouvement() throws Exception {
104
    public final void genereMouvement() throws Exception {
99
 
105
 
100
        SQLRow saisieRow = GenerationMvtSaisieVenteFacture.saisieVFTable.getRow(this.idSaisieVenteFacture);
106
        SQLRow saisieRow = GenerationMvtSaisieVenteFacture.saisieVFTable.getRow(this.idSaisieVenteFacture);
101
        setRowAnalytiqueSource(saisieRow);
107
        setRowAnalytiqueSource(saisieRow);
102
        SQLRow clientRow = saisieRow.getForeignRow("ID_CLIENT");
-
 
103
 
-
 
104
        // Calcul des montants
-
 
105
        PrixTTC prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue());
108
        boolean genEcrDisabled = DefaultNXProps.getInstance().getBooleanValue(GenerationMvtSaisieVenteFacture.NOT_GEN_ECRITURE, false);
106
        // Total des acomptes déjà versés sur la facture
-
 
107
        long montantAcompteTTC = 0;
-
 
108
 
109
 
-
 
110
        this.putValue("ID_JOURNAL", GenerationMvtSaisieVenteFacture.journal);
-
 
111
        SQLRow clientRow = saisieRow.getForeignRow("ID_CLIENT");
109
        int idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
112
        int idCompteClient = clientRow.getInt("ID_COMPTE_PCE");
110
 
113
 
111
        Boolean acompte = saisieRow.getBoolean("ACOMPTE");
114
        Boolean acompte = saisieRow.getBoolean("ACOMPTE");
112
        if (acompte != null && acompte) {
115
        if (acompte != null && acompte) {
113
            this.nom = "Fact. acompte client" + saisieRow.getObject("NUMERO").toString();
116
            this.nom = "Fact. acompte client" + saisieRow.getObject("NUMERO").toString();
114
        } else {
117
        } else {
115
            this.nom = "Fact. vente " + saisieRow.getObject("NUMERO").toString();
118
            this.nom = "Fact. vente " + saisieRow.getObject("NUMERO").toString();
116
        }
119
        }
-
 
120
 
-
 
121
        boolean typeCaisse = false;
-
 
122
        SQLRow modeRegl = saisieRow.getForeignRow("ID_MODE_REGLEMENT");
-
 
123
        final SQLRow typeRegRow = modeRegl.getForeignRow("ID_TYPE_REGLEMENT");
-
 
124
        final SQLRowAccessor nonEmptyForeign = modeRegl.getNonEmptyForeign("ID_BANQUE");
-
 
125
        if (nonEmptyForeign != null && nonEmptyForeign.getTable().contains("TYPE_CAISSE") && nonEmptyForeign.getBoolean("TYPE_CAISSE")) {
-
 
126
            typeCaisse = true;
-
 
127
            idCompteClient = nonEmptyForeign.getForeignID("ID_COMPTE_PCE");
-
 
128
            Number idJournal = nonEmptyForeign.getNonEmptyForeignIDNumber("ID_JOURNAL");
-
 
129
            if (idJournal != null) {
-
 
130
                this.putValue("ID_JOURNAL", idJournal);
-
 
131
            } else {
-
 
132
                this.putValue("ID_JOURNAL", JournalSQLElement.CAISSES);
-
 
133
            }
-
 
134
            this.nom += " (" + typeRegRow.getString("NOM") + ") " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
-
 
135
        } else {
117
        this.nom += " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
136
            this.nom += " " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
-
 
137
        }
-
 
138
 
-
 
139
        if (SQLPreferences.getMemCached(saisieRow.getTable().getDBRoot()).getBoolean(GestionCommercialeGlobalPreferencePanel.ECRITURE_FACTURE_REF_LIBELLE, false)) {
-
 
140
            if (saisieRow.getString("NOM") != null && saisieRow.getString("NOM").trim().length() > 0)
-
 
141
                this.nom += " " + StringUtils.limitLength(saisieRow.getString("NOM"), 30);
-
 
142
        }
-
 
143
 
-
 
144
 
-
 
145
        // Calcul des montants
-
 
146
        PrixTTC prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue());
-
 
147
        // Total des acomptes déjà versés sur la facture
-
 
148
        long montantAcompteTTC = 0;
-
 
149
 
118
        this.putValue("NOM", this.nom);
150
        this.putValue("NOM", this.nom);
119
 
151
 
120
        // iniatilisation des valeurs de la map
152
        // iniatilisation des valeurs de la map
121
        this.date = (Date) saisieRow.getObject("DATE");
153
        this.date = (Date) saisieRow.getObject("DATE");
122
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
154
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
123
        provider.putLabel(saisieRow, this.mEcritures);
155
        provider.putLabel(saisieRow, this.mEcritures);
124
 
156
 
125
        this.putValue("DATE", this.date);
157
        this.putValue("DATE", this.date);
126
        this.putValue("ID_JOURNAL", GenerationMvtSaisieVenteFacture.journal);
-
 
-
 
158
 
127
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));
159
        this.putValue("ID_MOUVEMENT", Integer.valueOf(1));
128
 
160
 
129
        // on calcule le nouveau numero de mouvement
161
        // on calcule le nouveau numero de mouvement
130
        if (this.idMvt == 1) {
162
        if (this.idMvt == 1) {
131
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
163
            SQLRowValues rowValsPiece = new SQLRowValues(pieceTable);
Line 174... Line 206...
174
                            if (compl.get(rowCompl) != null) {
206
                            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();
207
                                long taxeC = compl.get(rowCompl).multiply(rowCompl.getBigDecimal("POURCENT").movePointLeft(2)).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
176
                                this.putValue("ID_COMPTE_PCE", rowCompl.getForeignID("ID_COMPTE_PCE"));
208
                                this.putValue("ID_COMPTE_PCE", rowCompl.getForeignID("ID_COMPTE_PCE"));
177
                                this.putValue("DEBIT", Long.valueOf(0));
209
                                this.putValue("DEBIT", Long.valueOf(0));
178
                                this.putValue("CREDIT", Long.valueOf(taxeC));
210
                                this.putValue("CREDIT", Long.valueOf(taxeC));
-
 
211
                                if (!genEcrDisabled) {
179
                                ajoutEcriture();
212
                                    ajoutEcriture();
-
 
213
                                }
180
                                taxe += taxeC;
214
                                taxe += taxeC;
181
                                // this.putValue("ID_COMPTE_PCE",
215
                                // this.putValue("ID_COMPTE_PCE",
182
                                // rowCompl.getForeignID("ID_COMPTE_PCE_PRODUITS"));
216
                                // rowCompl.getForeignID("ID_COMPTE_PCE_PRODUITS"));
183
                                // this.putValue("DEBIT", Long.valueOf(taxeC));
217
                                // this.putValue("DEBIT", Long.valueOf(taxeC));
184
                                // this.putValue("CREDIT", Long.valueOf(0));
218
                                // this.putValue("CREDIT", Long.valueOf(0));
Line 195... Line 229...
195
                    }
229
                    }
196
 
230
 
197
                    this.putValue("ID_COMPTE_PCE", idComptePCE);
231
                    this.putValue("ID_COMPTE_PCE", idComptePCE);
198
                    this.putValue("DEBIT", Long.valueOf(0));
232
                    this.putValue("DEBIT", Long.valueOf(0));
199
                    this.putValue("CREDIT", Long.valueOf(b - taxe));
233
                    this.putValue("CREDIT", Long.valueOf(b - taxe));
-
 
234
                    if (!genEcrDisabled) {
200
                    ajoutEcriture();
235
                        ajoutEcriture();
201
                }
236
                    }
202
            }
237
                }
-
 
238
            }
203
 
239
 
204
            Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
240
            Map<SQLRowAccessor, BigDecimal> tvaMap = calc.getMapHtTVA();
205
            for (SQLRowAccessor rowAc : tvaMap.keySet()) {
241
            for (SQLRowAccessor rowAc : tvaMap.keySet()) {
206
                long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
242
                long longValue = tvaMap.get(rowAc).setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
207
                if (longValue != 0) {
243
                if (longValue != 0) {
208
                    this.putValue("ID_COMPTE_PCE", rowAc.getID());
244
                    this.putValue("ID_COMPTE_PCE", rowAc.getID());
209
                    this.putValue("DEBIT", Long.valueOf(0));
245
                    this.putValue("DEBIT", Long.valueOf(0));
210
                    this.putValue("CREDIT", longValue);
246
                    this.putValue("CREDIT", longValue);
-
 
247
                    if (!genEcrDisabled) {
211
                    ajoutEcriture();
248
                        ajoutEcriture();
212
                }
249
                    }
213
            }
250
                }
-
 
251
            }
214
 
252
 
215
            // compte Clients
253
            // compte Clients
216
 
254
 
217
            if (idCompteClient <= 1) {
255
            if (idCompteClient <= 1) {
218
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
256
                idCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
Line 224... Line 262...
224
            if (ecrTable.contains("CODE_CLIENT")) {
262
            if (ecrTable.contains("CODE_CLIENT")) {
225
                this.putValue("CODE_CLIENT", clientRow.getString("CODE"));
263
                this.putValue("CODE_CLIENT", clientRow.getString("CODE"));
226
            }
264
            }
227
            this.putValue("DEBIT", ttcLongValue);
265
            this.putValue("DEBIT", ttcLongValue);
228
            this.putValue("CREDIT", Long.valueOf(0));
266
            this.putValue("CREDIT", Long.valueOf(0));
-
 
267
            if (!genEcrDisabled) {
229
            ajoutEcriture();
268
                ajoutEcriture();
-
 
269
            }
230
 
270
 
231
            // TODO Gestion des factures d'acomptes
271
            // TODO Gestion des factures d'acomptes
232
            // Solde des acomptes
272
            // Solde des acomptes
233
            // List<SQLRow> rowsAcompte =
273
            // List<SQLRow> rowsAcompte =
234
            // saisieRow.getReferentRows(saisieVFTable.getField("ID_SAISIE_VENTE_FACTURE_ACOMPTE"));
274
            // saisieRow.getReferentRows(saisieVFTable.getField("ID_SAISIE_VENTE_FACTURE_ACOMPTE"));
Line 301... Line 341...
301
            valSasieVF.update(this.idSaisieVenteFacture);
341
            valSasieVF.update(this.idSaisieVenteFacture);
302
        }
342
        }
303
 
343
 
304
            if (genereReglement) {
344
            if (genereReglement) {
305
                // Génération du reglement
345
                // Génération du reglement
306
                SQLRow modeRegl = saisieRow.getForeignRow("ID_MODE_REGLEMENT");
-
 
307
                final SQLRow typeRegRow = modeRegl.getForeignRow("ID_TYPE_REGLEMENT");
-
 
-
 
346
 
308
                String label = this.nom + " (" + typeRegRow.getString("NOM") + ") " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
347
                String label = this.nom + " (" + typeRegRow.getString("NOM") + ") ";
309
                int idAvoir = saisieRow.getInt("ID_AVOIR_CLIENT");
348
                int idAvoir = saisieRow.getInt("ID_AVOIR_CLIENT");
310
                if (idAvoir > 1) {
349
                if (idAvoir > 1) {
311
                    // SQLRow avoirRow = base.getTable("AVOIR_CLIENT").getRow(idAvoir);
350
                    // SQLRow avoirRow = base.getTable("AVOIR_CLIENT").getRow(idAvoir);
312
                    long l = ((Number) saisieRow.getObject("T_AVOIR_TTC")).longValue();
351
                    long l = ((Number) saisieRow.getObject("T_AVOIR_TTC")).longValue();
313
                    prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue() - l);
352
                    prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue() - l);
314
                }
353
                }
315
                prixTTC = new PrixTTC(saisieRow.getLong("NET_A_PAYER"));
354
                final long totalNet = saisieRow.getLong("NET_A_PAYER");
-
 
355
                final SQLRow rowClient;
-
 
356
                final SQLRow rowCptTiers;
-
 
357
                if (typeCaisse) {
-
 
358
                    rowClient = null;
-
 
359
                    rowCptTiers = modeRegl.getForeign("ID_BANQUE").getForeign("ID_COMPTE_PCE");
-
 
360
                } else {
-
 
361
                    rowClient = clientRow;
-
 
362
                    rowCptTiers = null;
-
 
363
                }
-
 
364
                final String tiers = clientRow.getString("NOM");
-
 
365
 
-
 
366
                if (saisieRow.contains("POURCENT_RG") && saisieRow.getBigDecimal("POURCENT_RG") != null && saisieRow.getBigDecimal("POURCENT_RG").signum() != 0) {
-
 
367
                    long totalRG = new BigDecimal(totalNet).multiply(saisieRow.getBigDecimal("POURCENT_RG"), DecimalUtils.HIGH_PRECISION).movePointLeft(2).setScale(0, RoundingMode.HALF_UP)
-
 
368
                            .longValue();
-
 
369
                    PrixTTC prixRG = new PrixTTC(totalRG);
-
 
370
                    prixTTC = new PrixTTC(totalNet - totalRG);
316
                if (prixTTC.getLongValue() > 0) {
371
                    if (prixTTC.getLongValue() > 0) {
317
                    new GenerationReglementVenteNG(label.trim(), clientRow, prixTTC, this.date, modeRegl, saisieRow, mvtTable.getRow(idMvt));
372
                        new GenerationReglementVenteNG(label.trim(), rowClient, prixTTC, this.date, modeRegl, saisieRow, mvtTable.getRow(idMvt), true, false, tiers, rowCptTiers);
-
 
373
                    }
-
 
374
                    if (prixRG.getLongValue() > 0) {
-
 
375
                        SQLRowValues rowValsMdrRg = new SQLRowValues(modeRegl.getTable());
-
 
376
                        rowValsMdrRg.put("AJOURS", 365);
-
 
377
                        rowValsMdrRg.put("LENJOUR", 0);
-
 
378
                        rowValsMdrRg.put("COMPTANT", Boolean.FALSE);
-
 
379
                        rowValsMdrRg.put("FIN_MOIS", Boolean.FALSE);
-
 
380
                        rowValsMdrRg.put("DATE_FACTURE", Boolean.TRUE);
-
 
381
                        rowValsMdrRg.put("RG", Boolean.TRUE);
-
 
382
                        rowValsMdrRg.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.INDEFINI);
-
 
383
                        final SQLRow rowMdrRG = rowValsMdrRg.commit();
-
 
384
                        new GenerationReglementVenteNG(label.trim(), rowClient, prixRG, this.date, rowMdrRG, saisieRow, mvtTable.getRow(idMvt), true, false, tiers, rowCptTiers);
318
                }
385
                    }
-
 
386
                } else {
-
 
387
                    prixTTC = new PrixTTC(totalNet);
-
 
388
                    if (prixTTC.getLongValue() > 0) {
-
 
389
                        new GenerationReglementVenteNG(label.trim(), rowClient, prixTTC, this.date, modeRegl, saisieRow, mvtTable.getRow(idMvt), true, false, tiers, rowCptTiers);
-
 
390
                    }
-
 
391
                }
-
 
392
 
319
            }
393
            }
320
        // Mise à jour de mouvement associé à la facture
394
        // Mise à jour de mouvement associé à la facture
321
 
395
 
322
        SQLRowValues valSasieVF = new SQLRowValues(GenerationMvtSaisieVenteFacture.saisieVFTable);
396
        SQLRowValues valSasieVF = new SQLRowValues(GenerationMvtSaisieVenteFacture.saisieVFTable);
323
        valSasieVF.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
397
        valSasieVF.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));