OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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;
80 ilm 19
import org.openconcerto.sql.model.SQLField;
18 ilm 20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowAccessor;
80 ilm 22
import org.openconcerto.sql.model.SQLSelect;
18 ilm 23
import org.openconcerto.sql.model.SQLTable;
80 ilm 24
import org.openconcerto.sql.model.Where;
18 ilm 25
import org.openconcerto.utils.GestionDevise;
26
 
27
import java.text.DateFormat;
28
import java.text.SimpleDateFormat;
29
import java.util.ArrayList;
30
import java.util.Collection;
31
import java.util.Date;
32
import java.util.List;
33
 
132 ilm 34
import org.jdom2.Attribute;
35
import org.jdom2.Element;
18 ilm 36
 
37
public class OOXMLElement {
38
    protected Element elt;
39
    protected SQLElement sqlElt;
40
    protected int id;
63 ilm 41
    protected SQLRowAccessor row;
19 ilm 42
    protected SQLRow rowLanguage;
63 ilm 43
    protected OOXMLCache cache;
18 ilm 44
 
63 ilm 45
    public OOXMLElement(Element elt, SQLElement sqlElt, int id, SQLRow rowLanguage, OOXMLCache cache) {
46
        this(elt, sqlElt, id, null, rowLanguage, cache);
47
 
18 ilm 48
    }
49
 
63 ilm 50
    public OOXMLElement(Element elt, SQLElement sqlElt, int id, SQLRowAccessor row, SQLRow rowLanguage, OOXMLCache cache) {
18 ilm 51
        this.elt = elt;
52
        this.sqlElt = sqlElt;
53
        this.id = id;
54
        this.row = row;
19 ilm 55
        this.rowLanguage = rowLanguage;
63 ilm 56
        this.cache = cache;
18 ilm 57
    }
58
 
59
    public Object getValue() {
60
        Object res = "";
61
 
63 ilm 62
        final String type = this.elt.getAttributeValue("type");
63
        SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(type);
64
        if (provider != null) {
65
            final SpreadSheetCellValueContext context = new SpreadSheetCellValueContext(this.row);
66
            List<Attribute> attrs = this.elt.getAttributes();
67
            for (Attribute attr : attrs) {
68
                context.put(attr.getName(), attr.getValue());
69
            }
67 ilm 70
            res = provider.getValue(context);
71
        } else
18 ilm 72
 
63 ilm 73
        if (type.equalsIgnoreCase("TotalHTTable")) {
67 ilm 74
            res = getTotalHTTable(row);
80 ilm 75
        } else if (type.equalsIgnoreCase("sql.function")) {
76
            String field = this.elt.getAttributeValue("field");
77
            String table = this.elt.getAttributeValue("table");
78
            String function = this.elt.getAttributeValue("function");
79
            SQLTable referentTable = this.row.getTable().getTable(table);
80
            return getFromSQLFunction(referentTable.getField(field), function);
67 ilm 81
        } else if (type.equalsIgnoreCase("DateEcheance")) {
18 ilm 82
            int idModeReglement = row.getInt("ID_MODE_REGLEMENT");
83
            Date d = (Date) row.getObject("DATE");
67 ilm 84
            res = getDateEcheance(idModeReglement, d, this.elt.getAttributeValue("datePattern"));
85
        } else {
18 ilm 86
 
67 ilm 87
            final List<Element> eltFields = this.elt.getChildren("field");
18 ilm 88
 
73 ilm 89
            if (eltFields != null && !eltFields.isEmpty()) {
67 ilm 90
                if (eltFields.size() > 1) {
91
                    String result = "";
92
                    for (Element eltField : eltFields) {
18 ilm 93
 
67 ilm 94
                        OOXMLField field = new OOXMLField(eltField, this.row, this.sqlElt, this.id, this.rowLanguage, cache);
18 ilm 95
 
67 ilm 96
                        Object value = field.getValue();
97
                        if (value != null) {
98
                            result += value.toString() + " ";
99
                        }
18 ilm 100
                    }
67 ilm 101
                    res = result;
102
                } else {
103
                    OOXMLField field = new OOXMLField(eltFields.get(0), this.row, this.sqlElt, this.id, this.rowLanguage, cache);
104
                    res = field.getValue();
18 ilm 105
                }
106
            }
107
        }
67 ilm 108
 
109
        // Liste des valeurs à ne pas afficher
110
        List<String> listOfExcludedValues = null;
111
 
112
        List<Element> excludeValue = this.elt.getChildren("exclude");
113
        if (excludeValue != null && excludeValue.size() > 0) {
114
 
115
            listOfExcludedValues = new ArrayList<String>();
116
 
117
            for (Element element : excludeValue) {
118
                String attributeValue = element.getAttributeValue("value");
119
                listOfExcludedValues.add(attributeValue);
120
            }
121
        }
122
 
123
        if (res != null && listOfExcludedValues != null && listOfExcludedValues.contains(res.toString())) {
124
            res = null;
125
        }
126
 
132 ilm 127
        final String k = this.elt.getAttributeValue("removeBreakLine");
128
        boolean rmBreakLines = (k == null) ? false : k.equalsIgnoreCase("true");
129
        if (rmBreakLines) {
130
            res = (res == null ? res : res.toString().replaceAll("\n", ","));
131
        }
182 ilm 132
        final String brk = this.elt.getAttributeValue("replaceWithBreakLine");
133
        if (brk != null && brk.trim().length() > 0) {
134
            res = (res == null ? res : res.toString().replaceAll(brk, "\n"));
135
        }
185 ilm 136
 
137
        String attributeValueMaxChar = this.elt.getAttributeValue("maxChar");
138
        if (attributeValueMaxChar != null) {
139
            int maxChar = Integer.valueOf(attributeValueMaxChar);
140
            if (res != null && res.toString().length() > maxChar) {
141
                res = res.toString().substring(0, maxChar);
142
            }
143
        }
144
 
18 ilm 145
        return res;
146
    }
147
 
80 ilm 148
    private Object getFromSQLFunction(SQLField field, String function) {
149
        SQLSelect sel = new SQLSelect();
150
        sel.addSelect(field, function);
151
        Where w = new Where(field.getTable().getField("ID_" + this.row.getTable().getName()), "=", this.row.getID());
152
        sel.setWhere(w);
185 ilm 153
 
80 ilm 154
        return Configuration.getInstance().getBase().getDataSource().executeScalar(sel.asString());
155
    }
18 ilm 156
 
80 ilm 157
 
132 ilm 158
    public DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
18 ilm 159
 
160
    protected String getStringProposition(SQLRowAccessor rowProp) {
161
 
182 ilm 162
        if (rowProp.getTable().getName().equalsIgnoreCase("AFFAIRE")) {
163
            final SQLRowAccessor nonEmptyForeignProp = rowProp.getNonEmptyForeign("ID_PROPOSITION");
164
            String result = "";
165
            if (rowProp.getString("NUMERO_PROPOSITION").trim().length() > 0) {
166
                return "Notre proposition " + rowProp.getString("NUMERO_PROPOSITION");
167
            }
168
            if (nonEmptyForeignProp != null && !nonEmptyForeignProp.isUndefined()) {
169
                if (result.length() == 0) {
170
                    result = "Notre proposition " + nonEmptyForeignProp.getString("NUMERO_PROPOSITION");
171
                }
172
                result += " du " + format.format(rowProp.getObject("DATE"));
173
            }
174
            return result;
175
        } else {
176
 
177
            return "Notre proposition " + rowProp.getString("NUMERO") + " du " + format.format(rowProp.getObject("DATE"));
178
        }
18 ilm 179
    }
180
 
181
 
182
    public Double getTotalHTTable(SQLRowAccessor rowFact) {
183
 
184
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
185
        Collection<? extends SQLRowAccessor> set = rowFact.getReferentRows(tableElt);
186
        long total = 0;
187
        for (SQLRowAccessor row : set) {
188
            total += row.getLong("T_PV_HT");
189
        }
190
 
191
        return new Double(GestionDevise.currencyToString(total, false));
192
    }
193
 
194
 
195
    /**
196
     * Calcul la date d'échéance d'un élément par rapport au mode de reglement et à la date
197
     * d'émission
198
     *
199
     * @param idModeRegl
200
     * @param currentDate
61 ilm 201
     * @return la date d'échéance au format dd/MM/yy si datePattern !=null sinon une Date
18 ilm 202
     */
61 ilm 203
    protected Object getDateEcheance(int idModeRegl, Date currentDate, String datePattern) {
18 ilm 204
        SQLElement eltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
205
        SQLRow row = eltModeRegl.getTable().getRow(idModeRegl);
206
        int aJ = row.getInt("AJOURS");
207
        int nJ = row.getInt("LENJOUR");
208
        if (aJ + nJ == 0) {
177 ilm 209
            if (!row.getBoolean("COMPTANT") && row.getBoolean("DATE_FACTURE")) {
18 ilm 210
                return Configuration.getInstance().getTranslator().getLabelFor(row.getTable().getField("DATE_FACTURE"));
211
            } else {
212
                return " ";
213
            }
214
        }
61 ilm 215
        Date calculDate = ModeDeReglementSQLElement.calculDate(aJ, nJ, currentDate);
216
        if (datePattern != null && datePattern.trim().length() > 0) {
217
            final DateFormat format2 = new SimpleDateFormat(datePattern);
218
            return format2.format(calculDate);
219
        } else {
220
            return calculDate;
221
        }
18 ilm 222
    }
223
 
224
    public boolean isTypeReplace() {
225
        // remplacement d'un pattern contenu dans la cellule
226
        return this.elt.getAttributeValue("type").equalsIgnoreCase("Replace");
227
    }
228
 
229
    public String getReplacePattern() {
230
        return this.elt.getAttributeValue("replacePattern");
231
    }
232
 
233
    public boolean isMultilineAuto() {
234
        // gestion manuel du multiligne
235
        final String multiLineValue = this.elt.getAttributeValue("controleMultiline");
236
        return (multiLineValue == null) ? true : !multiLineValue.equalsIgnoreCase("false");
237
    }
238
 
132 ilm 239
    public boolean isKeepingEmptyLines() {
240
        // gestion manuel du multiligne
241
        final String k = this.elt.getAttributeValue("keepEmptyLines");
242
        return (k == null) ? false : k.equalsIgnoreCase("true");
243
    }
244
 
180 ilm 245
    public boolean isImage() {
246
        return this.elt.getAttributeValue("type").equalsIgnoreCase("image");
247
    }
182 ilm 248
 
249
    public SQLElement getSQLElement() {
250
        return this.sqlElt;
251
    }
18 ilm 252
}