OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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