OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
67 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.core.common.ui;
15
 
16
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
17
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
149 ilm 18
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
67 ilm 19
import org.openconcerto.sql.Configuration;
156 ilm 20
import org.openconcerto.sql.model.SQLBackgroundTableCache;
21
import org.openconcerto.sql.model.SQLBackgroundTableCacheItem;
67 ilm 22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
26
import org.openconcerto.sql.model.SQLTable;
149 ilm 27
import org.openconcerto.sql.preferences.SQLPreferences;
90 ilm 28
import org.openconcerto.utils.DecimalUtils;
142 ilm 29
import org.openconcerto.utils.Tuple2;
67 ilm 30
 
31
import java.math.BigDecimal;
32
import java.math.RoundingMode;
156 ilm 33
import java.util.Collection;
67 ilm 34
import java.util.HashMap;
73 ilm 35
import java.util.HashSet;
67 ilm 36
import java.util.List;
37
import java.util.Map;
73 ilm 38
import java.util.Set;
67 ilm 39
 
40
public class TotalCalculator {
41
 
42
    private static String FIELD_SERVICE = "SERVICE";
73 ilm 43
    private static String FIELD_POIDS = "T_POIDS";
67 ilm 44
    private final String fieldHT, fieldHA, fieldDevise;
149 ilm 45
    private boolean imputEcart = true;
80 ilm 46
    private SQLRowAccessor rowDefaultCptProduit, rowDefaultCptService, rowDefaultCptTVACollecte, rowDefaultCptTVADeductible, rowDefaultCptAchat;
67 ilm 47
 
156 ilm 48
    private final SQLRowAccessor rowCatComptable;
49
 
93 ilm 50
    private SQLRowAccessor rowDefaultCptProduitStandard;
51
 
73 ilm 52
    private double totalPoids;
67 ilm 53
 
54
    private BigDecimal totalDevise, totalDeviseSel;
55
    private BigDecimal totalHA, totalHASel;
142 ilm 56
    private BigDecimal totalEco, totalEcoSel;
67 ilm 57
    private BigDecimal totalService, totalServiceSel;
142 ilm 58
    private BigDecimal totalHTSansFActurable, totalTTC, totalTTCSel;
67 ilm 59
    private long remiseHT, remiseRestante;
80 ilm 60
    private final boolean achat;
67 ilm 61
 
62
    // Total des HT par comptes
63
    private Map<SQLRowAccessor, BigDecimal> mapHt = new HashMap<SQLRowAccessor, BigDecimal>();
64
    private Map<SQLRowAccessor, BigDecimal> mapHtSel = new HashMap<SQLRowAccessor, BigDecimal>();
65
 
66
    // Total des TVA par comptes
67
    private Map<SQLRowAccessor, BigDecimal> mapHtTVA = new HashMap<SQLRowAccessor, BigDecimal>();
144 ilm 68
    private Map<SQLRowAccessor, Map<SQLRowAccessor, BigDecimal>> mapHtTaxeCompl = new HashMap<SQLRowAccessor, Map<SQLRowAccessor, BigDecimal>>();
142 ilm 69
    private Map<SQLRowAccessor, BigDecimal> mapHtTVAIntra = new HashMap<SQLRowAccessor, BigDecimal>();
67 ilm 70
    private Map<SQLRowAccessor, BigDecimal> mapHtTVASel = new HashMap<SQLRowAccessor, BigDecimal>();
142 ilm 71
 
72
    // Total HT par TVA
73
    private Map<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>> mapHtTVARowTaux = new HashMap<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>>();
73 ilm 74
    private int[] selectedRows;
67 ilm 75
 
76
    private Boolean bServiceActive;
77
    private BigDecimal totalHTAvantRemise;
142 ilm 78
    private boolean intraComm = false;
67 ilm 79
 
156 ilm 80
    public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, SQLRowAccessor rowCatComptable) {
81
        this(fieldHA, fieldHT, fieldDeviseTotal, false, null, rowCatComptable);
80 ilm 82
    }
67 ilm 83
 
90 ilm 84
    public void setRowDefaultCptService(SQLRowAccessor rowDefaultCptService) {
85
        this.rowDefaultCptService = rowDefaultCptService;
86
    }
87
 
142 ilm 88
    public void setIntraComm(boolean intraComm) {
89
        this.intraComm = intraComm;
90
    }
91
 
156 ilm 92
    public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, boolean achat, SQLRowAccessor defaultCompte, SQLRowAccessor rowCatComptable) {
80 ilm 93
 
94
        this.achat = achat;
67 ilm 95
        initValues();
96
 
97
        this.fieldDevise = fieldDeviseTotal;
98
        this.fieldHA = fieldHA;
99
        this.fieldHT = fieldHT;
91 ilm 100
        final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE");
156 ilm 101
        final SQLBackgroundTableCacheItem cacheForTablePref = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte);
102
        final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte.getTable("COMPTE_PCE"));
103
        final SQLRow rowPrefsCompte = cacheForTablePref.getRowFromId(2);
104
 
149 ilm 105
        SQLPreferences prefs = SQLPreferences.getMemCached(tablePrefCompte.getDBRoot());
106
        this.imputEcart = prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.IMPUT_ECART, true);
107
 
142 ilm 108
        // FIXME faire un fetcher pour ne pas faire 5 requetes (1 par getForeign)
67 ilm 109
        // Comptes par défaut
156 ilm 110
        this.rowDefaultCptService = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_VENTE_SERVICE"));
67 ilm 111
        if (this.rowDefaultCptService == null || this.rowDefaultCptService.isUndefined()) {
112
            try {
113
                this.rowDefaultCptService = ComptePCESQLElement.getRowComptePceDefault("VentesServices");
114
            } catch (Exception e) {
115
                e.printStackTrace();
116
            }
117
        }
118
 
156 ilm 119
        this.rowCatComptable = rowCatComptable;
120
 
93 ilm 121
        if (defaultCompte == null || defaultCompte.isUndefined()) {
156 ilm 122
 
123
            if (rowCatComptable != null && !rowCatComptable.isUndefined() && !rowCatComptable.isForeignEmpty("ID_COMPTE_PCE_VENTE")) {
124
                this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE_VENTE"));
125
            } else {
126
                this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_VENTE_PRODUIT"));
127
                if (this.rowDefaultCptProduit == null || this.rowDefaultCptProduit.isUndefined()) {
128
                    try {
129
                        this.rowDefaultCptProduit = ComptePCESQLElement.getRowComptePceDefault("VentesProduits");
130
                    } catch (Exception e) {
131
                        e.printStackTrace();
132
                    }
93 ilm 133
                }
67 ilm 134
            }
93 ilm 135
        } else {
136
            this.rowDefaultCptProduit = defaultCompte;
67 ilm 137
        }
93 ilm 138
        this.rowDefaultCptProduitStandard = this.rowDefaultCptProduit;
67 ilm 139
 
156 ilm 140
        this.rowDefaultCptTVACollecte = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_TVA_VENTE"));
80 ilm 141
        if (this.rowDefaultCptTVACollecte == null || this.rowDefaultCptTVACollecte.isUndefined()) {
67 ilm 142
            try {
80 ilm 143
                this.rowDefaultCptTVACollecte = ComptePCESQLElement.getRowComptePceDefault("TVACollectee");
67 ilm 144
            } catch (Exception e) {
145
                e.printStackTrace();
146
            }
147
        }
148
 
156 ilm 149
        this.rowDefaultCptTVADeductible = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_TVA_ACHAT"));
80 ilm 150
        if (this.rowDefaultCptTVADeductible == null || this.rowDefaultCptTVADeductible.isUndefined()) {
151
            try {
152
                this.rowDefaultCptTVADeductible = ComptePCESQLElement.getRowComptePceDefault("TVADeductible");
153
            } catch (Exception e) {
154
                e.printStackTrace();
155
            }
156
        }
157
 
158
        if (defaultCompte == null || defaultCompte.isUndefined()) {
156 ilm 159
            if (rowCatComptable != null && !rowCatComptable.isUndefined() && !rowCatComptable.isForeignEmpty("ID_COMPTE_PCE_ACHAT")) {
160
                this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE_ACHAT"));
161
            } else {
162
                this.rowDefaultCptAchat = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_ACHAT"));
163
                if (this.rowDefaultCptAchat == null || this.rowDefaultCptAchat.isUndefined()) {
164
                    try {
165
                        this.rowDefaultCptAchat = ComptePCESQLElement.getRowComptePceDefault("Achats");
166
                    } catch (Exception e) {
167
                        e.printStackTrace();
168
                    }
80 ilm 169
                }
170
            }
171
        } else {
172
            this.rowDefaultCptAchat = defaultCompte;
173
        }
174
 
67 ilm 175
    }
176
 
93 ilm 177
    public void setRowDefaultCptProduit(SQLRowAccessor rowDefaultCptProduit) {
178
        this.rowDefaultCptProduit = rowDefaultCptProduit;
179
    }
180
 
181
    public void retoreRowDefaultCptProduit() {
182
        this.rowDefaultCptProduit = rowDefaultCptProduitStandard;
183
    }
184
 
67 ilm 185
    /**
186
     * Définition d'une remise HT à appliquer
187
     *
188
     * @param remiseHT montant de la remise en cents
189
     * @param totalHTAvantRemise montant de la facture avant remise
190
     */
191
    public void setRemise(long remiseHT, BigDecimal totalHTAvantRemise) {
192
        this.remiseHT = remiseHT;
193
        this.remiseRestante = remiseHT;
194
        this.totalHTAvantRemise = totalHTAvantRemise;
195
    }
196
 
197
    /**
198
     * Gestion de la vente de service
199
     *
200
     * @param b
201
     */
202
    public void setServiceActive(boolean b) {
203
        this.bServiceActive = b;
204
    }
205
 
206
    /**
207
     * Remise à zéro des valeurs de calcul
208
     */
209
    public void initValues() {
210
        this.remiseHT = 0;
211
        this.remiseRestante = 0;
212
        this.totalHTAvantRemise = BigDecimal.ZERO;
213
 
214
        this.selectedRows = null;
215
 
142 ilm 216
        this.totalHTSansFActurable = BigDecimal.ZERO;
67 ilm 217
        this.totalTTC = BigDecimal.ZERO;
218
        this.totalTTCSel = BigDecimal.ZERO;
219
 
142 ilm 220
        this.totalEco = BigDecimal.ZERO;
221
        this.totalEcoSel = BigDecimal.ZERO;
222
 
67 ilm 223
        this.totalHA = BigDecimal.ZERO;
224
        this.totalHASel = BigDecimal.ZERO;
225
 
226
        this.totalService = BigDecimal.ZERO;
227
        this.totalServiceSel = BigDecimal.ZERO;
228
 
229
        this.totalDeviseSel = BigDecimal.ZERO;
230
        this.totalDevise = BigDecimal.ZERO;
231
 
232
        this.totalPoids = 0;
233
 
234
        // Total des HT par comptes
235
        this.mapHt.clear();
236
        this.mapHtSel.clear();
237
 
238
        // Total des TVA par comptes
239
        this.mapHtTVA.clear();
142 ilm 240
        this.mapHtTaxeCompl.clear();
241
        this.mapHtTVAIntra.clear();
242
        this.mapHtTVARowTaux.clear();
67 ilm 243
        this.mapHtTVASel.clear();
244
 
245
    }
246
 
247
    public void setSelectedRows(int[] selectedRows) {
248
        this.selectedRows = selectedRows;
249
    }
250
 
251
    public void addEchantillon(BigDecimal ht, SQLRowAccessor tva) {
142 ilm 252
        addHT(ht, ht, tva, this.rowDefaultCptProduit, false);
67 ilm 253
    }
254
 
149 ilm 255
    // Cache de TVA
256
    private static Map<Integer, SQLRowAccessor> mapTVA;
257
    private static long lastTVAfetchedTime = 0;
258
 
67 ilm 259
    final SQLTable tvaTable = Configuration.getInstance().getRoot().findTable("TAXE");
260
    final SQLTable compteTable = Configuration.getInstance().getRoot().findTable("COMPTE_PCE");
261
 
262
    /**
263
     * Mise en cache des comptes de TVA
264
     */
265
    private void fetchTVA() {
266
        mapTVA = new HashMap<Integer, SQLRowAccessor>();
267
        SQLRowValues rowVals = new SQLRowValues(tvaTable);
268
        SQLRowValues rowValsC1 = new SQLRowValues(compteTable);
269
        rowValsC1.put("NUMERO", null);
270
        rowValsC1.put("ID", null);
271
 
80 ilm 272
        SQLRowValues rowValsC2 = new SQLRowValues(compteTable);
273
        rowValsC2.put("NUMERO", null);
274
        rowValsC2.put("ID", null);
275
 
94 ilm 276
        SQLRowValues rowValsC3 = new SQLRowValues(compteTable);
277
        rowValsC3.put("NUMERO", null);
278
        rowValsC3.put("ID", null);
279
        SQLRowValues rowValsC4 = new SQLRowValues(compteTable);
280
        rowValsC4.put("NUMERO", null);
281
        rowValsC4.put("ID", null);
282
 
67 ilm 283
        rowVals.put(tvaTable.getKey().getName(), null);
83 ilm 284
        rowVals.put("ID_COMPTE_PCE_COLLECTE", rowValsC1);
80 ilm 285
        rowVals.put("ID_COMPTE_PCE_DED", rowValsC2);
94 ilm 286
        rowVals.put("ID_COMPTE_PCE_VENTE", rowValsC3);
287
        rowVals.put("ID_COMPTE_PCE_VENTE_SERVICE", rowValsC4);
67 ilm 288
 
142 ilm 289
        if (tvaTable.contains("ID_COMPTE_PCE_COLLECTE_INTRA")) {
290
            SQLRowValues rowValsC1Intra = new SQLRowValues(compteTable);
291
            rowValsC1Intra.put("NUMERO", null);
292
            rowValsC1Intra.put("ID", null);
293
            rowVals.put("ID_COMPTE_PCE_COLLECTE_INTRA", rowValsC1Intra);
294
            SQLRowValues rowValsC2Intra = new SQLRowValues(compteTable);
295
            rowValsC2Intra.put("NUMERO", null);
296
            rowValsC2Intra.put("ID", null);
297
            rowVals.put("ID_COMPTE_PCE_DED_INTRA", rowValsC2Intra);
298
        }
67 ilm 299
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
300
        List<SQLRowValues> rowValsList = fetch.fetch();
301
 
302
        for (SQLRowValues sqlRowValues : rowValsList) {
303
            mapTVA.put(sqlRowValues.getID(), sqlRowValues);
304
        }
149 ilm 305
        lastTVAfetchedTime = System.currentTimeMillis();
67 ilm 306
    }
307
 
142 ilm 308
    private void addHT(BigDecimal ht, BigDecimal htSansFacturable, SQLRowAccessor tva, SQLRowAccessor cptArticle, boolean selection) {
67 ilm 309
 
310
        BigDecimal ttc;
311
        BigDecimal totalTVA;
156 ilm 312
        final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(cptArticle.getTable());
67 ilm 313
 
314
        if (tva == null || tva.isUndefined()) {
315
            ttc = ht;
316
            totalTVA = BigDecimal.ZERO;
317
        } else {
318
            BigDecimal tauxTVA = BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(tva.getID())).movePointLeft(2);
90 ilm 319
            ttc = tauxTVA.add(BigDecimal.ONE).multiply(ht, DecimalUtils.HIGH_PRECISION);
67 ilm 320
            totalTVA = ttc.subtract(ht);
321
        }
322
 
323
        if (tva != null && !tva.isUndefined()) {
80 ilm 324
            SQLRowAccessor rowCptTva;
142 ilm 325
            if (this.intraComm) {
326
 
327
                // Intra comm TTC=HT et solde de TVA
156 ilm 328
                rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_DED_INTRA"));
142 ilm 329
                if (rowCptTva == null || rowCptTva.isUndefined()) {
330
                    rowCptTva = this.rowDefaultCptTVADeductible;
331
                }
332
 
156 ilm 333
                SQLRowAccessor rowCptTvaIntra = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_COLLECTE_INTRA"));
142 ilm 334
                if (rowCptTvaIntra == null || rowCptTvaIntra.isUndefined()) {
335
                    rowCptTvaIntra = this.rowDefaultCptTVADeductible;
336
                }
156 ilm 337
 
142 ilm 338
                if (mapHtTVAIntra.get(rowCptTvaIntra) == null) {
339
                    mapHtTVAIntra.put(rowCptTvaIntra, totalTVA);
156 ilm 340
                } else {
341
                    BigDecimal l = mapHtTVAIntra.get(rowCptTvaIntra);
342
                    mapHtTVAIntra.put(rowCptTvaIntra, l.add(totalTVA));
142 ilm 343
                }
344
 
156 ilm 345
                ttc = ht;
142 ilm 346
            } else if (this.achat) {
156 ilm 347
                rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_DED"));
80 ilm 348
                if (rowCptTva == null || rowCptTva.isUndefined()) {
349
                    rowCptTva = this.rowDefaultCptTVADeductible;
350
                }
351
            } else {
156 ilm 352
                rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_COLLECTE"));
80 ilm 353
                if (rowCptTva == null || rowCptTva.isUndefined()) {
354
                    rowCptTva = this.rowDefaultCptTVACollecte;
355
                }
67 ilm 356
            }
357
            if (mapHtTVA.get(rowCptTva) == null) {
358
                mapHtTVA.put(rowCptTva, totalTVA);
359
            } else {
360
                BigDecimal l = mapHtTVA.get(rowCptTva);
361
                mapHtTVA.put(rowCptTva, l.add(totalTVA));
362
            }
142 ilm 363
            if (ht.signum() != 0) {
364
                if (mapHtTVARowTaux.get(tva) == null) {
365
                    mapHtTVARowTaux.put(tva, Tuple2.create(ht, totalTVA));
366
                } else {
367
                    Tuple2<BigDecimal, BigDecimal> l = mapHtTVARowTaux.get(tva);
368
                    mapHtTVARowTaux.put(tva, Tuple2.create(ht.add(l.get0()), l.get1().add(totalTVA)));
369
                }
370
            }
67 ilm 371
            if (selection) {
372
                if (mapHtTVASel.get(rowCptTva) == null) {
373
                    mapHtTVASel.put(rowCptTva, totalTVA);
374
                } else {
375
                    BigDecimal l = mapHtTVASel.get(rowCptTva);
376
                    mapHtTVASel.put(rowCptTva, l.add(totalTVA));
377
                }
378
            }
379
        }
380
 
381
        if (mapHt.get(cptArticle) == null) {
382
            mapHt.put(cptArticle, ht);
383
        } else {
384
            BigDecimal l = mapHt.get(cptArticle);
385
            mapHt.put(cptArticle, l.add(ht));
386
        }
387
 
388
        this.totalTTC = this.totalTTC.add(ttc);
142 ilm 389
        this.totalHTSansFActurable = this.totalHTSansFActurable.add(htSansFacturable);
67 ilm 390
        if (selection) {
391
 
392
            if (mapHtSel.get(cptArticle) == null) {
393
                mapHtSel.put(cptArticle, ht);
394
            } else {
395
                BigDecimal l = mapHtSel.get(cptArticle);
396
                mapHtSel.put(cptArticle, l.add(ht));
397
            }
398
            this.totalTTCSel = this.totalTTCSel.add(ttc);
399
        }
400
    }
401
 
402
    private static boolean containsInt(int[] tab, int i) {
403
        if (tab == null) {
404
            return false;
405
        }
406
 
407
        for (int j = 0; j < tab.length; j++) {
408
            if (tab[j] == i) {
409
                return true;
410
            }
411
        }
412
        return false;
413
    }
414
 
415
    public void addLine(SQLRowAccessor rowAccessorLine, SQLRowAccessor article, int lineNumber, boolean last) {
416
 
142 ilm 417
        if (rowAccessorLine.getFields().contains("NIVEAU") && rowAccessorLine.getInt("NIVEAU") != 1) {
418
            return;
419
        }
420
 
67 ilm 421
        // Total HT de la ligne
422
        BigDecimal totalLineHT = rowAccessorLine.getObject(fieldHT) == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject(fieldHT);
142 ilm 423
        BigDecimal totalLineEco = rowAccessorLine.getObject("T_ECO_CONTRIBUTION") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("T_ECO_CONTRIBUTION");
67 ilm 424
 
142 ilm 425
        BigDecimal totalLineHTSansFacturable = totalLineHT;
426
        if (!achat) {
427
            totalLineHTSansFacturable = rowAccessorLine.getObject("PV_HT") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("PV_HT");
428
            BigDecimal qteUV = rowAccessorLine.getObject("QTE_UNITAIRE") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("QTE_UNITAIRE");
429
            int qte = rowAccessorLine.getInt("QTE");
430
            totalLineHTSansFacturable = totalLineHTSansFacturable.multiply(qteUV).multiply(new BigDecimal(qte));
431
        }
67 ilm 432
        // Prix Unitaire de la ligne
433
        // TODO voir pour passer le prix total et non le prix unitaire
434
        BigDecimal totalHALigne = rowAccessorLine.getObject(fieldHA) == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject(fieldHA);
435
 
436
        Boolean service = rowAccessorLine.getBoolean(FIELD_SERVICE);
437
 
438
        BigDecimal totalLineDevise = (fieldDevise == null || rowAccessorLine.getObject(fieldDevise) == null) ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject(fieldDevise);
439
 
440
        Number nPoids = (Number) rowAccessorLine.getObject(FIELD_POIDS);
441
 
442
        // Si il y a une remise à appliquer
83 ilm 443
        if (this.remiseHT != 0 && this.remiseRestante > 0 && this.totalHTAvantRemise != null && this.totalHTAvantRemise.signum() != 0) {
67 ilm 444
 
445
            // Si c'est la derniere ligne, on applique le restant de la remise
446
            if (last) {
447
                totalLineHT = totalLineHT.subtract(new BigDecimal(this.remiseRestante).movePointLeft(2));
142 ilm 448
                totalLineHTSansFacturable = totalLineHTSansFacturable.subtract(new BigDecimal(this.remiseRestante).movePointLeft(2));
67 ilm 449
                this.remiseRestante = 0;
450
            } else {
90 ilm 451
                BigDecimal percent = totalLineHT.divide(this.totalHTAvantRemise, DecimalUtils.HIGH_PRECISION);
67 ilm 452
 
90 ilm 453
                BigDecimal remiseApply = percent.multiply(new BigDecimal(this.remiseHT), DecimalUtils.HIGH_PRECISION).setScale(0, RoundingMode.HALF_UP);
67 ilm 454
                totalLineHT = totalLineHT.subtract(remiseApply.movePointLeft(2));
142 ilm 455
                totalLineHTSansFacturable = totalLineHTSansFacturable.subtract(remiseApply.movePointLeft(2));
67 ilm 456
                this.remiseRestante -= remiseApply.longValue();
457
            }
458
        }
459
 
149 ilm 460
        // TODO : generaliser le cache
461
        if (mapTVA == null || lastTVAfetchedTime + 30 * 1000 < System.currentTimeMillis()) {
94 ilm 462
            fetchTVA();
463
        }
464
        Integer idTVA = null;
156 ilm 465
        if (rowAccessorLine.getObject("ID_TAXE") != null && !rowAccessorLine.isForeignEmpty("ID_TAXE")) {
466
            idTVA = rowAccessorLine.getForeignID("ID_TAXE");
467
        } else {
468
            final SQLRowAccessor foreignTVA = rowAccessorLine.getForeign("ID_TAXE");
469
            if (foreignTVA != null) {
470
                idTVA = foreignTVA.getID();
471
            }
94 ilm 472
        }
473
        SQLRowAccessor tva = mapTVA.get(idTVA);
474
 
80 ilm 475
        SQLRowAccessor cpt = (achat ? this.rowDefaultCptAchat : this.rowDefaultCptProduit);
156 ilm 476
        final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(cpt.getTable());
477
 
94 ilm 478
        if (!achat) {
479
            // Total Service
480
            if (bServiceActive != null && bServiceActive && service != null && service.booleanValue()) {
481
                totalService = totalService.add(totalLineHT);
482
                cpt = this.rowDefaultCptService;
483
                if (tva != null && !tva.isForeignEmpty("ID_COMPTE_PCE_VENTE_SERVICE")) {
156 ilm 484
                    cpt = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_VENTE_SERVICE"));
94 ilm 485
                }
486
            } else {
487
                // Compte defini par défaut dans la TVA
488
                if (tva != null && !tva.isForeignEmpty("ID_COMPTE_PCE_VENTE")) {
156 ilm 489
                    cpt = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_VENTE"));
94 ilm 490
                }
491
 
492
            }
493
        }
67 ilm 494
        if (article != null && !article.isUndefined()) {
156 ilm 495
            SQLRowAccessor cptCatComptable = null;
496
            // TODO Optimiser les requetes
497
            if (rowCatComptable != null && !rowCatComptable.isUndefined()) {
498
                String suffix = (this.achat ? "_ACHAT" : "_VENTE");
499
                Collection<? extends SQLRowAccessor> rows = article.getReferentRows(compteTable.getTable("ARTICLE_CATEGORIE_COMPTABLE"));
500
                for (SQLRowAccessor sqlRowAccessor : rows) {
501
                    if (sqlRowAccessor.getForeignID("ID_CATEGORIE_COMPTABLE") == this.rowCatComptable.getID()) {
502
                        cptCatComptable = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE" + suffix));
503
                    }
504
                }
505
            }
73 ilm 506
 
156 ilm 507
            if (cptCatComptable == null) {
508
                String suffix = (this.achat ? "_ACHAT" : "");
509
                SQLRowAccessor compteArticle = cacheForTableCompte.getRowFromId(article.getForeignID("ID_COMPTE_PCE" + suffix));
510
                if (compteArticle != null && !compteArticle.isUndefined()) {
511
                    cpt = compteArticle;
512
                } else {
513
                    SQLRowAccessor familleArticle = article.getForeign("ID_FAMILLE_ARTICLE");
514
                    Set<SQLRowAccessor> unique = new HashSet<SQLRowAccessor>();
515
                    while (familleArticle != null && !familleArticle.isUndefined() && !unique.contains(familleArticle)) {
516
 
517
                        unique.add(familleArticle);
518
                        if (familleArticle.getObject("ID_COMPTE_PCE" + suffix) != null && !familleArticle.isForeignEmpty("ID_COMPTE_PCE" + suffix)) {
519
                            SQLRowAccessor compteFamilleArticle = cacheForTableCompte.getRowFromId(familleArticle.getForeignID("ID_COMPTE_PCE" + suffix));
520
                            if (compteFamilleArticle != null && !compteFamilleArticle.isUndefined()) {
521
                                cpt = compteFamilleArticle;
522
                                break;
523
                            }
524
                        }
525
                        familleArticle = familleArticle.getForeign("ID_FAMILLE_ARTICLE_PERE");
67 ilm 526
                    }
527
                }
156 ilm 528
            } else {
529
                cpt = cptCatComptable;
67 ilm 530
            }
142 ilm 531
            if (!achat) {
144 ilm 532
                SQLRowAccessor taxeCompl = (article.getFields().contains("ID_TAXE_COMPLEMENTAIRE") && article.getObject("ID_TAXE_COMPLEMENTAIRE") != null
533
                        && !article.isForeignEmpty("ID_TAXE_COMPLEMENTAIRE") ? article.getForeign("ID_TAXE_COMPLEMENTAIRE") : null);
142 ilm 534
                if (taxeCompl != null && !taxeCompl.isUndefined()) {
144 ilm 535
 
536
                    Map<SQLRowAccessor, BigDecimal> mapCptTaxeValue = this.mapHtTaxeCompl.get(cpt);
537
                    if (mapCptTaxeValue == null) {
538
                        mapCptTaxeValue = new HashMap<SQLRowAccessor, BigDecimal>();
539
                        this.mapHtTaxeCompl.put(cpt, mapCptTaxeValue);
540
                    }
541
                    BigDecimal b = mapCptTaxeValue.get(taxeCompl);
142 ilm 542
                    if (b == null) {
543
                        b = BigDecimal.ZERO;
544
                    }
545
                    b = b.add(totalLineHT);
144 ilm 546
                    mapCptTaxeValue.put(taxeCompl, b);
142 ilm 547
                }
548
            }
67 ilm 549
        }
550
 
94 ilm 551
        if (achat) {
552
            // Total Service
553
            if (bServiceActive != null && bServiceActive) {
554
                if (service != null && service.booleanValue()) {
555
                    totalService = totalService.add(totalLineHT);
556
                    cpt = this.rowDefaultCptService;
557
                }
67 ilm 558
            }
559
        }
560
 
561
        // Total HA
562
        this.totalHA = this.totalHA.add(totalHALigne);
563
 
564
        // Total Devise
565
        if (totalLineDevise != null) {
566
            totalDevise = totalDevise.add(totalLineDevise);
567
        }
568
 
569
        // Total Poids
570
 
571
        totalPoids += nPoids == null ? 0 : nPoids.doubleValue();
572
 
142 ilm 573
        // Eco-contribution
574
        this.totalEco = this.totalEco.add(totalLineEco);
575
 
67 ilm 576
        // Calcul total sélectionné
577
        boolean selection = containsInt(selectedRows, lineNumber);
578
        if (selection) {
579
 
580
            totalHASel = totalHASel.add(totalHALigne);
581
 
582
            if (bServiceActive != null && bServiceActive) {
73 ilm 583
                if (service != null && service.booleanValue()) {
67 ilm 584
                    totalServiceSel = totalServiceSel.add(totalLineHT);
585
                }
586
            }
142 ilm 587
            this.totalEcoSel = this.totalEcoSel.add(totalLineEco);
67 ilm 588
 
589
            if (totalLineDevise != null) {
590
                totalDeviseSel = totalDeviseSel.add(totalLineDevise);
591
            }
592
        }
593
 
142 ilm 594
        addHT(totalLineHT, totalLineHTSansFacturable, tva, cpt, selection);
67 ilm 595
    }
596
 
597
    /**
598
     * Vérifie si ht + tva = ttc
599
     */
600
    public void checkResult() {
601
        BigDecimal ht = getTotalHT();
602
        BigDecimal tva = getTotalTVA();
603
        BigDecimal totalTTC2 = getTotalTTC();
604
        BigDecimal reste = totalTTC2.subtract(ht.add(tva));
142 ilm 605
        if (!intraComm && reste.compareTo(BigDecimal.ZERO) != 0) {
73 ilm 606
            System.err.print("Ecarts: " + reste + "(HT:" + ht);
607
            System.err.print(" TVA:" + tva);
608
            System.err.println(" TTC:" + totalTTC2);
149 ilm 609
 
67 ilm 610
            // TODO Check if row already exist in MAP ??
149 ilm 611
            if (!this.imputEcart) {
612
                for (SQLRowAccessor rHT : this.mapHt.keySet()) {
613
                    BigDecimal input = this.mapHt.get(rHT);
614
                    if (input != null && input.signum() != 0) {
615
                        this.mapHt.put(rHT, input.add(reste));
616
                        break;
617
                    }
618
                }
619
            } else {
620
                SQLRow row = ComptePCESQLElement.getRow("758", "Ecarts arrondis");
621
                this.mapHt.put(row, reste);
622
            }
67 ilm 623
        }
624
    }
625
 
626
    public BigDecimal getTotalDevise() {
627
        return totalDevise;
628
    }
629
 
630
    public BigDecimal getTotalDeviseSel() {
631
        return totalDeviseSel;
632
    }
633
 
634
    public BigDecimal getTotalHA() {
635
        return totalHA;
636
    }
637
 
638
    public BigDecimal getTotalHASel() {
639
        return totalHASel;
640
    }
641
 
642
    public double getTotalPoids() {
643
        return totalPoids;
644
    }
645
 
646
    public BigDecimal getTotalService() {
647
        return totalService;
648
    }
649
 
650
    public BigDecimal getTotalServiceSel() {
651
        return totalServiceSel;
652
    }
653
 
144 ilm 654
    public Map<SQLRowAccessor, Map<SQLRowAccessor, BigDecimal>> getMapHtTaxeCompl() {
142 ilm 655
        return mapHtTaxeCompl;
656
    }
657
 
67 ilm 658
    public BigDecimal getTotalHT() {
659
        BigDecimal ht = BigDecimal.ZERO;
660
        for (SQLRowAccessor row : this.mapHt.keySet()) {
661
            ht = ht.add(this.mapHt.get(row).setScale(2, RoundingMode.HALF_UP));
662
        }
663
 
664
        return ht;
665
    }
666
 
667
    public BigDecimal getTotalTVA() {
668
        BigDecimal tva = BigDecimal.ZERO;
669
        for (SQLRowAccessor row : this.mapHtTVA.keySet()) {
670
            tva = tva.add(this.mapHtTVA.get(row).setScale(2, RoundingMode.HALF_UP));
671
        }
672
        return tva;
673
    }
674
 
142 ilm 675
    public BigDecimal getTotalEco() {
676
        return this.totalEco.setScale(2, RoundingMode.HALF_UP);
677
    }
678
 
679
    public BigDecimal getTotalEcoSel() {
680
        return this.totalEcoSel.setScale(2, RoundingMode.HALF_UP);
681
    }
682
 
683
    public BigDecimal getTotalHTSansFActurable() {
684
        return totalHTSansFActurable;
685
    }
686
 
67 ilm 687
    public BigDecimal getTotalTTC() {
688
        return this.totalTTC.setScale(2, RoundingMode.HALF_UP);
689
    }
690
 
691
    public BigDecimal getTotalHTSel() {
692
        BigDecimal ht = BigDecimal.ZERO;
693
        for (SQLRowAccessor row : this.mapHtSel.keySet()) {
694
            ht = ht.add(this.mapHtSel.get(row).setScale(2, RoundingMode.HALF_UP));
695
        }
696
 
697
        return ht;
698
    }
699
 
700
    public BigDecimal getTotalTVASel() {
701
        BigDecimal tva = BigDecimal.ZERO;
702
        for (SQLRowAccessor row : this.mapHtTVASel.keySet()) {
703
            tva = tva.add(this.mapHtTVASel.get(row).setScale(2, RoundingMode.HALF_UP));
704
        }
705
        return tva;
706
    }
707
 
708
    public BigDecimal getTotalTTCSel() {
709
 
710
        return this.totalTTCSel.setScale(2, RoundingMode.HALF_UP);
711
    }
712
 
713
    public Map<SQLRowAccessor, BigDecimal> getMapHt() {
714
        return mapHt;
715
    }
716
 
142 ilm 717
    public Map<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>> getMapHtTVARowTaux() {
718
        return mapHtTVARowTaux;
719
    }
720
 
67 ilm 721
    public Map<SQLRowAccessor, BigDecimal> getMapHtTVA() {
722
        return mapHtTVA;
723
    }
142 ilm 724
 
725
    public Map<SQLRowAccessor, BigDecimal> getMapHtTVAIntra() {
726
        return mapHtTVAIntra;
727
    }
67 ilm 728
}