OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | Rev 73 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 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;
15
 
16
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.utils.GestionDevise;
23
 
24
import java.text.DateFormat;
25
import java.text.SimpleDateFormat;
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.Date;
29
import java.util.List;
30
 
63 ilm 31
import org.jdom.Attribute;
18 ilm 32
import org.jdom.Element;
33
 
34
public class OOXMLElement {
35
    protected Element elt;
36
    protected SQLElement sqlElt;
37
    protected int id;
63 ilm 38
    protected SQLRowAccessor row;
19 ilm 39
    protected SQLRow rowLanguage;
63 ilm 40
    protected OOXMLCache cache;
18 ilm 41
 
63 ilm 42
    public OOXMLElement(Element elt, SQLElement sqlElt, int id, SQLRow rowLanguage, OOXMLCache cache) {
43
        this(elt, sqlElt, id, null, rowLanguage, cache);
44
 
18 ilm 45
    }
46
 
63 ilm 47
    public OOXMLElement(Element elt, SQLElement sqlElt, int id, SQLRowAccessor row, SQLRow rowLanguage, OOXMLCache cache) {
18 ilm 48
        this.elt = elt;
49
        this.sqlElt = sqlElt;
50
        this.id = id;
51
        this.row = row;
19 ilm 52
        this.rowLanguage = rowLanguage;
63 ilm 53
        this.cache = cache;
18 ilm 54
    }
55
 
56
    public Object getValue() {
57
        Object res = "";
58
 
63 ilm 59
        final String type = this.elt.getAttributeValue("type");
60
        SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(type);
61
        if (provider != null) {
62
            final SpreadSheetCellValueContext context = new SpreadSheetCellValueContext(this.row);
63
            List<Attribute> attrs = this.elt.getAttributes();
64
            for (Attribute attr : attrs) {
65
                context.put(attr.getName(), attr.getValue());
66
            }
67 ilm 67
            res = provider.getValue(context);
68
        } else
18 ilm 69
 
63 ilm 70
        if (type.equalsIgnoreCase("TotalHTTable")) {
67 ilm 71
            res = getTotalHTTable(row);
72
        } else if (type.equalsIgnoreCase("DateEcheance")) {
18 ilm 73
            int idModeReglement = row.getInt("ID_MODE_REGLEMENT");
74
            Date d = (Date) row.getObject("DATE");
67 ilm 75
            res = getDateEcheance(idModeReglement, d, this.elt.getAttributeValue("datePattern"));
76
        } else {
18 ilm 77
 
67 ilm 78
            final List<Element> eltFields = this.elt.getChildren("field");
18 ilm 79
 
67 ilm 80
            if (eltFields != null) {
81
                if (eltFields.size() > 1) {
82
                    String result = "";
83
                    for (Element eltField : eltFields) {
18 ilm 84
 
67 ilm 85
                        OOXMLField field = new OOXMLField(eltField, this.row, this.sqlElt, this.id, this.rowLanguage, cache);
18 ilm 86
 
67 ilm 87
                        Object value = field.getValue();
88
                        if (value != null) {
89
                            result += value.toString() + " ";
90
                        }
18 ilm 91
                    }
67 ilm 92
                    res = result;
93
                } else {
94
                    OOXMLField field = new OOXMLField(eltFields.get(0), this.row, this.sqlElt, this.id, this.rowLanguage, cache);
95
                    res = field.getValue();
18 ilm 96
                }
97
            }
98
        }
67 ilm 99
 
100
        // Liste des valeurs à ne pas afficher
101
        List<String> listOfExcludedValues = null;
102
 
103
        List<Element> excludeValue = this.elt.getChildren("exclude");
104
        if (excludeValue != null && excludeValue.size() > 0) {
105
 
106
            listOfExcludedValues = new ArrayList<String>();
107
 
108
            for (Element element : excludeValue) {
109
                String attributeValue = element.getAttributeValue("value");
110
                listOfExcludedValues.add(attributeValue);
111
            }
112
        }
113
 
114
        if (res != null && listOfExcludedValues != null && listOfExcludedValues.contains(res.toString())) {
115
            res = null;
116
        }
117
 
18 ilm 118
        return res;
119
    }
120
 
121
 
122
    public static DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
123
 
124
    protected String getStringProposition(SQLRowAccessor rowProp) {
125
 
126
        return "Notre proposition " + rowProp.getString("NUMERO") + " du " + format.format(rowProp.getObject("DATE"));
127
    }
128
 
129
 
130
    public Double getTotalHTTable(SQLRowAccessor rowFact) {
131
 
132
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
133
        Collection<? extends SQLRowAccessor> set = rowFact.getReferentRows(tableElt);
134
        long total = 0;
135
        for (SQLRowAccessor row : set) {
136
            total += row.getLong("T_PV_HT");
137
        }
138
 
139
        return new Double(GestionDevise.currencyToString(total, false));
140
    }
141
 
142
 
143
    /**
144
     * Calcul la date d'échéance d'un élément par rapport au mode de reglement et à la date
145
     * d'émission
146
     *
147
     * @param idModeRegl
148
     * @param currentDate
61 ilm 149
     * @return la date d'échéance au format dd/MM/yy si datePattern !=null sinon une Date
18 ilm 150
     */
61 ilm 151
    protected Object getDateEcheance(int idModeRegl, Date currentDate, String datePattern) {
18 ilm 152
        SQLElement eltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
153
        SQLRow row = eltModeRegl.getTable().getRow(idModeRegl);
154
        int aJ = row.getInt("AJOURS");
155
        int nJ = row.getInt("LENJOUR");
156
        if (aJ + nJ == 0) {
157
            if (row.getBoolean("DATE_FACTURE")) {
158
                return Configuration.getInstance().getTranslator().getLabelFor(row.getTable().getField("DATE_FACTURE"));
159
            } else {
160
                return " ";
161
            }
162
        }
61 ilm 163
        Date calculDate = ModeDeReglementSQLElement.calculDate(aJ, nJ, currentDate);
164
        if (datePattern != null && datePattern.trim().length() > 0) {
165
            final DateFormat format2 = new SimpleDateFormat(datePattern);
166
            return format2.format(calculDate);
167
        } else {
168
            return calculDate;
169
        }
18 ilm 170
    }
171
 
172
    public boolean isTypeReplace() {
173
        // remplacement d'un pattern contenu dans la cellule
174
        return this.elt.getAttributeValue("type").equalsIgnoreCase("Replace");
175
    }
176
 
177
    public String getReplacePattern() {
178
        return this.elt.getAttributeValue("replacePattern");
179
    }
180
 
181
    public boolean isMultilineAuto() {
182
        // gestion manuel du multiligne
183
        final String multiLineValue = this.elt.getAttributeValue("controleMultiline");
184
        return (multiLineValue == null) ? true : !multiLineValue.equalsIgnoreCase("false");
185
    }
186
 
187
}