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
180 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.customerrelationship.customer.report;
15
 
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
20
import org.openconcerto.sql.model.SQLSelect;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.utils.cc.ITransformer;
24
 
25
import java.math.BigDecimal;
26
import java.util.Collection;
27
import java.util.Date;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31
 
32
public class ReportingCommercialFournisseurCreator {
33
 
34
    final private Date deb, fin;
35
    final private DBRoot root;
36
 
37
    public ReportingCommercialFournisseurCreator(Date deb, Date fin, DBRoot root) {
38
        this.deb = deb;
39
        this.fin = fin;
40
        this.root = root;
41
    }
42
 
43
    public Collection<ReportingCommercial> getValues() {
44
 
45
        // Facture
46
        final SQLTable tableFacture = this.root.getTable("SAISIE_VENTE_FACTURE_ELEMENT");
47
        SQLRowValues rowValues = new SQLRowValues(tableFacture);
48
        rowValues.putRowValues("ID_SAISIE_VENTE_FACTURE").putRowValues("ID_COMMERCIAL").putNulls("PRENOM", "NOM");
49
        rowValues.putRowValues("ID_ARTICLE").putRowValues("ID_FOURNISSEUR").putNulls("CODE", "NOM");
50
        rowValues.putNulls("T_PV_HT", "T_PV_TTC");
51
 
52
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValues);
53
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
54
 
55
            @Override
56
            public SQLSelect transformChecked(SQLSelect input) {
57
                Where w = new Where(input.getJoinFromField(tableFacture.getField("ID_SAISIE_VENTE_FACTURE")).getJoinedTable().getField("DATE"), deb, fin);
58
 
59
                input.setWhere(w);
60
                return input;
61
            }
62
        });
63
 
64
        Map<Integer, ReportingCommercial> mapCommercial = new HashMap<>();
65
        List<SQLRowValues> result = fetcher.fetch();
66
        for (SQLRowValues sqlRowValues : result) {
67
            if (!sqlRowValues.isForeignEmpty("ID_ARTICLE")) {
68
                SQLRowAccessor rowArticle = sqlRowValues.getForeign("ID_ARTICLE");
69
                if (!rowArticle.isForeignEmpty("ID_FOURNISSEUR")) {
70
                    SQLRowAccessor rowFournisseur = rowArticle.getForeign("ID_FOURNISSEUR");
71
                    int commercialID = sqlRowValues.getForeign("ID_SAISIE_VENTE_FACTURE").getForeignID("ID_COMMERCIAL");
72
                    if (!mapCommercial.containsKey(commercialID)) {
73
                        SQLRowAccessor commercialRow = sqlRowValues.getForeign("ID_SAISIE_VENTE_FACTURE").getForeign("ID_COMMERCIAL");
74
                        mapCommercial.put(commercialID, new ReportingCommercial(commercialRow.getString("PRENOM") + " " + commercialRow.getString("NOM"), deb, fin));
75
                    }
76
 
77
                    ReportingCommercial r = mapCommercial.get(commercialID);
78
                    BigDecimal bigDecimal = new BigDecimal(sqlRowValues.getLong("T_PV_HT")).movePointLeft(2);
79
                    r.add(rowFournisseur.getString("NOM"), bigDecimal);
80
                }
81
            }
82
        }
83
 
84
        // Avoir
85
        final SQLTable tableAvoir = this.root.getTable("AVOIR_CLIENT_ELEMENT");
86
        SQLRowValues rowValuesAvoir = new SQLRowValues(tableAvoir);
87
        rowValuesAvoir.putRowValues("ID_AVOIR_CLIENT").putRowValues("ID_COMMERCIAL").putNulls("PRENOM", "NOM");
88
        rowValuesAvoir.putRowValues("ID_ARTICLE").putRowValues("ID_FOURNISSEUR").putNulls("CODE", "NOM");
89
        rowValuesAvoir.putNulls("T_PV_HT", "T_PV_TTC");
90
 
91
        SQLRowValuesListFetcher fetcherAvoir = SQLRowValuesListFetcher.create(rowValuesAvoir);
92
        fetcherAvoir.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
93
 
94
            @Override
95
            public SQLSelect transformChecked(SQLSelect input) {
96
                Where w = new Where(input.getJoinFromField(tableAvoir.getField("ID_AVOIR_CLIENT")).getJoinedTable().getField("DATE"), deb, fin);
97
 
98
                input.setWhere(w);
99
                return input;
100
            }
101
        });
102
 
103
        List<SQLRowValues> resultA = fetcherAvoir.fetch();
104
        for (SQLRowValues sqlRowValues : resultA) {
105
 
106
            if (!sqlRowValues.isForeignEmpty("ID_ARTICLE")) {
107
                SQLRowAccessor rowArticle = sqlRowValues.getForeign("ID_ARTICLE");
108
                if (!rowArticle.isForeignEmpty("ID_FOURNISSEUR")) {
109
                    SQLRowAccessor rowFournisseur = rowArticle.getForeign("ID_FOURNISSEUR");
110
 
111
                    int commercialID = sqlRowValues.getForeign("ID_AVOIR_CLIENT").getForeignID("ID_COMMERCIAL");
112
                    if (!mapCommercial.containsKey(commercialID)) {
113
                        SQLRowAccessor commercialRow = sqlRowValues.getForeign("ID_AVOIR_CLIENT").getForeign("ID_COMMERCIAL");
114
                        mapCommercial.put(commercialID, new ReportingCommercial(commercialRow.getString("PRENOM") + " " + commercialRow.getString("NOM"), deb, fin));
115
                    }
116
 
117
                    ReportingCommercial r = mapCommercial.get(commercialID);
118
                    BigDecimal bigDecimal = new BigDecimal(sqlRowValues.getLong("T_PV_HT")).movePointLeft(2).negate();
119
                    r.add(rowFournisseur.getString("NOM"), bigDecimal);
120
                }
121
            }
122
        }
123
 
124
        return mapCommercial.values();
125
    }
126
 
127
}