OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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