OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 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.generationDoc.provider;
15
 
16
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
17
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLTable;
20
 
21
import java.math.BigDecimal;
22
import java.util.Collection;
23
 
24
public class QteTotalDocProvider extends UserInitialsValueProvider {
25
 
26
    private enum TypeQteTotalDocProvider {
177 ilm 27
        QTE_KG, QTE, COLIS, PDS_BRUT, QTE_MULT
174 ilm 28
    };
29
 
30
    private final TypeQteTotalDocProvider type;
177 ilm 31
    private boolean ha = false;
174 ilm 32
 
33
    public QteTotalDocProvider(TypeQteTotalDocProvider t) {
177 ilm 34
        this(t, false);
35
    }
36
 
37
    public QteTotalDocProvider(TypeQteTotalDocProvider t, boolean ha) {
174 ilm 38
        this.type = t;
177 ilm 39
        this.ha = ha;
174 ilm 40
    }
41
 
42
    @Override
43
    public Object getValue(SpreadSheetCellValueContext context) {
44
        SQLRowAccessor row = context.getRow();
45
 
46
        final SQLTable table = row.getTable();
47
        Collection<? extends SQLRowAccessor> cols = row.getReferentRows(table.getTable(table.getName() + "_ELEMENT"));
48
        BigDecimal total = BigDecimal.ZERO;
49
        for (SQLRowAccessor sqlRowAccessor : cols) {
50
 
51
            if (!sqlRowAccessor.getTable().contains("NIVEAU") || sqlRowAccessor.getInt("NIVEAU") == 1) {
177 ilm 52
                String field = "PV_HT";
53
                if (this.ha) {
54
                    field = "PA_HT";
55
                }
56
                BigDecimal prixUnitaire = sqlRowAccessor.getBigDecimal(field);
57
                if (prixUnitaire != null && prixUnitaire.signum() != 0) {
58
                    if (this.type == TypeQteTotalDocProvider.QTE || this.type == TypeQteTotalDocProvider.QTE_KG) {
59
                        if (this.type == TypeQteTotalDocProvider.QTE || sqlRowAccessor.getForeignID("ID_UNITE_VENTE") == 7) {
174 ilm 60
 
177 ilm 61
                            BigDecimal qte = sqlRowAccessor.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRowAccessor.getInt("QTE")));
62
                            total = total.add(qte);
63
                        }
64
                    } else if (this.type == TypeQteTotalDocProvider.PDS_BRUT) {
65
                        total = total.add(sqlRowAccessor.getBigDecimal("T_POIDS_BRUT"));
66
                    } else if (this.type == TypeQteTotalDocProvider.QTE_MULT) {
67
                        BigDecimal qte = new BigDecimal(sqlRowAccessor.getInt("QTE"));
68
                        total = total.add(qte);
69
                    } else {
70
                        if (sqlRowAccessor.getObject("NB_COLIS") != null) {
71
                            total = total.add(new BigDecimal(sqlRowAccessor.getInt("NB_COLIS")));
72
                        }
174 ilm 73
                    }
74
                }
75
            }
76
        }
77
 
78
        return total;
79
    }
80
 
81
    public static void register() {
177 ilm 82
        SpreadSheetCellValueProviderManager.put("sales.qty.multiple.total", new QteTotalDocProvider(TypeQteTotalDocProvider.QTE_MULT));
174 ilm 83
        SpreadSheetCellValueProviderManager.put("sales.qty.total", new QteTotalDocProvider(TypeQteTotalDocProvider.QTE));
177 ilm 84
        SpreadSheetCellValueProviderManager.put("purchase.qty.total", new QteTotalDocProvider(TypeQteTotalDocProvider.QTE, true));
85
        SpreadSheetCellValueProviderManager.put("sales.qty.total.kg", new QteTotalDocProvider(TypeQteTotalDocProvider.QTE_KG));
174 ilm 86
        SpreadSheetCellValueProviderManager.put("sales.package.total", new QteTotalDocProvider(TypeQteTotalDocProvider.COLIS));
177 ilm 87
        SpreadSheetCellValueProviderManager.put("sales.package.brut", new QteTotalDocProvider(TypeQteTotalDocProvider.PDS_BRUT));
174 ilm 88
    }
89
}