OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
182 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011-2019 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.generationDoc.gestcomm;
15
 
16
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.utils.cc.ITransformer;
19
import org.openconcerto.utils.DecimalUtils;
20
 
21
import java.math.BigDecimal;
22
import java.util.ArrayList;
23
import java.util.HashSet;
24
import java.util.List;
25
import java.util.Set;
26
 
27
public class OptionDocProcessor implements ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> {
28
 
29
    @Override
30
    public List<SQLRowAccessor> transformChecked(List<SQLRowAccessor> input) {
31
 
32
        List<SQLRowValues> resultRowValues = new ArrayList(input.size());
33
        SQLRowValues lastRowNonOptionnal = null;
34
        Set<SQLRowValues> recalculPrixUnitaire = new HashSet<SQLRowValues>();
35
        for (SQLRowAccessor sqlRowAccessor : input) {
36
            final SQLRowAccessor nonEmptyForeign = sqlRowAccessor.getNonEmptyForeign("ID_ARTICLE");
37
            final SQLRowValues asRowValues = sqlRowAccessor.asRowValues();
38
            if (lastRowNonOptionnal != null && nonEmptyForeign != null && nonEmptyForeign.getBoolean("OPTION")) {
39
                lastRowNonOptionnal.put("T_PV_HT", lastRowNonOptionnal.getBigDecimal("T_PV_HT").add(asRowValues.getBigDecimal("T_PV_HT")));
40
                lastRowNonOptionnal.put("T_PV_TTC", lastRowNonOptionnal.getBigDecimal("T_PV_TTC").add(asRowValues.getBigDecimal("T_PV_TTC")));
41
                recalculPrixUnitaire.add(lastRowNonOptionnal);
42
                asRowValues.put("PRIX_METRIQUE_VT_1", null);
43
                asRowValues.put("PV_HT", null);
44
                asRowValues.put("T_PV_HT", null);
45
                asRowValues.put("T_PV_TTC", null);
46
                asRowValues.putEmptyLink("ID_TAXE");
47
            } else {
48
                lastRowNonOptionnal = asRowValues;
49
            }
50
            resultRowValues.add(asRowValues);
51
        }
52
        List<SQLRowAccessor> result = new ArrayList(input.size());
53
 
54
        for (SQLRowValues sqlRowValues : resultRowValues) {
55
            if (recalculPrixUnitaire.contains(sqlRowValues)) {
56
 
57
                String fieldQte = sqlRowValues.getTable().getName().startsWith("BON_DE_LIVRAISON") ? "QTE_LIVREE" : "QTE";
58
 
59
                BigDecimal qte = sqlRowValues.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRowValues.getInt(fieldQte), DecimalUtils.HIGH_PRECISION));
60
                if (qte.signum() != 0) {
61
 
62
                    BigDecimal totalHT = sqlRowValues.getBigDecimal("T_PV_HT");
63
                    final BigDecimal remisePercent = sqlRowValues.getBigDecimal("POURCENT_REMISE");
64
                    if (remisePercent != null && remisePercent.signum() > 0) {
65
                        totalHT = totalHT.divide(BigDecimal.ONE.subtract(remisePercent.movePointLeft(2)), org.openconcerto.utils.DecimalUtils.HIGH_PRECISION);
66
                    }
67
                    BigDecimal unitPrice = totalHT.divide(qte, DecimalUtils.HIGH_PRECISION);
68
                    sqlRowValues.put("PV_HT", unitPrice);
69
                    sqlRowValues.put("PRIX_METRIQUE_VT_1", unitPrice);
70
                }
71
            }
72
            result.add(sqlRowValues);
73
        }
74
 
75
        return result;
76
    }
77
 
78
}