OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
185 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.provider;
15
 
16
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
17
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProvider;
18
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.utils.DecimalUtils;
24
 
25
import java.math.BigDecimal;
26
import java.math.RoundingMode;
27
import java.util.Calendar;
28
import java.util.List;
29
 
30
public class RecapLigneFactureProvider implements SpreadSheetCellValueProvider {
31
 
32
    private enum TypeLineRecapFactureProvider {
33
        HT, TTC, PERCENT;
34
    };
35
 
36
    private final TypeLineRecapFactureProvider type;
37
    private final boolean old, withAvoir;
38
 
39
    public RecapLigneFactureProvider(TypeLineRecapFactureProvider t, boolean old) {
40
        this(t, old, false);
41
    }
42
 
43
    public RecapLigneFactureProvider(TypeLineRecapFactureProvider t, boolean old, boolean withAvoir) {
44
        this.type = t;
45
        this.old = old;
46
        this.withAvoir = withAvoir;
47
    }
48
 
49
    public Object getValue(SpreadSheetCellValueContext context) {
50
        SQLRowAccessor row = context.getRow();
51
        final SQLRowAccessor factureOrigin = row.getNonEmptyForeign("ID_SAISIE_VENTE_FACTURE");
52
        final Calendar date = factureOrigin.getDate("DATE");
53
 
54
        final SQLRowAccessor foreignCmdItem = row.getNonEmptyForeign("ID_COMMANDE_CLIENT_ELEMENT");
55
        if (foreignCmdItem != null && foreignCmdItem.getBigDecimal("T_PV_HT").signum() != 0) {
56
 
57
            SQLRowValues rowValsFactItem2Fetch = new SQLRowValues(row.getTable());
58
            rowValsFactItem2Fetch.putNulls("T_PV_HT", "T_PV_TTC");
59
 
60
            rowValsFactItem2Fetch.putRowValues("ID_SAISIE_VENTE_FACTURE").putNulls("DATE", "ID_AVOIR_CLIENT");
61
 
62
            final List<SQLRowValues> fetch = SQLRowValuesListFetcher.create(rowValsFactItem2Fetch).fetch(new Where(row.getTable().getField("ID_COMMANDE_CLIENT_ELEMENT"), "=", foreignCmdItem.getID()));
63
 
64
            BigDecimal total = BigDecimal.ZERO;
65
            BigDecimal totalTTC = BigDecimal.ZERO;
66
            for (SQLRowAccessor sqlRowAccessor : fetch) {
67
                final SQLRowAccessor nonEmptyForeign = sqlRowAccessor.getNonEmptyForeign("ID_SAISIE_VENTE_FACTURE");
68
                if (nonEmptyForeign != null && (!withAvoir || (withAvoir && sqlRowAccessor.isForeignEmpty("ID_AVOIR_CLIENT")))) {
69
                    final Calendar date2 = nonEmptyForeign.getDate("DATE");
70
                    final boolean same = old && factureOrigin.getID() == nonEmptyForeign.getID();
71
                    if (same || date2.before(date) || (date2.equals(date) && nonEmptyForeign.getID() < factureOrigin.getID())) {
72
                        total = total.add(sqlRowAccessor.getBigDecimal("T_PV_HT"));
73
                        totalTTC = totalTTC.add(sqlRowAccessor.getBigDecimal("T_PV_TTC"));
74
                    }
75
                }
76
            }
77
            if (this.type == TypeLineRecapFactureProvider.HT) {
78
                return total;
79
            } else if (this.type == TypeLineRecapFactureProvider.TTC) {
80
                return totalTTC;
81
            } else {
82
                if (foreignCmdItem.getBigDecimal("T_PV_HT").signum() != 0) {
83
                    return total.divide(foreignCmdItem.getBigDecimal("T_PV_HT"), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP) + "%";
84
                } else {
85
                    return "";
86
                }
87
            }
88
        } else {
89
            return "";
90
        }
91
    }
92
 
93
    public static void register() {
94
        SpreadSheetCellValueProviderManager.put("sales.account.line.history", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, false));
95
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, false));
96
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, false));
97
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, true));
98
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, true));
99
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, true));
100
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, false, true));
101
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, false, true));
102
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, false, true));
103
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, true, true));
104
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, true, true));
105
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, true, true));
106
    }
107
 
108
}