OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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
 *
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
        }
18 ilm 136
        return res;
137
    }
138
 
80 ilm 139
    private Object getFromSQLFunction(SQLField field, String function) {
140
        SQLSelect sel = new SQLSelect();
141
        sel.addSelect(field, function);
142
        Where w = new Where(field.getTable().getField("ID_" + this.row.getTable().getName()), "=", this.row.getID());
143
        sel.setWhere(w);
144
        return Configuration.getInstance().getBase().getDataSource().executeScalar(sel.asString());
145
    }
18 ilm 146
 
80 ilm 147
 
132 ilm 148
    public DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
18 ilm 149
 
150
    protected String getStringProposition(SQLRowAccessor rowProp) {
151
 
182 ilm 152
        if (rowProp.getTable().getName().equalsIgnoreCase("AFFAIRE")) {
153
            final SQLRowAccessor nonEmptyForeignProp = rowProp.getNonEmptyForeign("ID_PROPOSITION");
154
            String result = "";
155
            if (rowProp.getString("NUMERO_PROPOSITION").trim().length() > 0) {
156
                return "Notre proposition " + rowProp.getString("NUMERO_PROPOSITION");
157
            }
158
            if (nonEmptyForeignProp != null && !nonEmptyForeignProp.isUndefined()) {
159
                if (result.length() == 0) {
160
                    result = "Notre proposition " + nonEmptyForeignProp.getString("NUMERO_PROPOSITION");
161
                }
162
                result += " du " + format.format(rowProp.getObject("DATE"));
163
            }
164
            return result;
165
        } else {
166
 
167
            return "Notre proposition " + rowProp.getString("NUMERO") + " du " + format.format(rowProp.getObject("DATE"));
168
        }
18 ilm 169
    }
170
 
171
 
172
    public Double getTotalHTTable(SQLRowAccessor rowFact) {
173
 
174
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
175
        Collection<? extends SQLRowAccessor> set = rowFact.getReferentRows(tableElt);
176
        long total = 0;
177
        for (SQLRowAccessor row : set) {
178
            total += row.getLong("T_PV_HT");
179
        }
180
 
181
        return new Double(GestionDevise.currencyToString(total, false));
182
    }
183
 
184
 
185
    /**
186
     * Calcul la date d'échéance d'un élément par rapport au mode de reglement et à la date
187
     * d'émission
188
     *
189
     * @param idModeRegl
190
     * @param currentDate
61 ilm 191
     * @return la date d'échéance au format dd/MM/yy si datePattern !=null sinon une Date
18 ilm 192
     */
61 ilm 193
    protected Object getDateEcheance(int idModeRegl, Date currentDate, String datePattern) {
18 ilm 194
        SQLElement eltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
195
        SQLRow row = eltModeRegl.getTable().getRow(idModeRegl);
196
        int aJ = row.getInt("AJOURS");
197
        int nJ = row.getInt("LENJOUR");
198
        if (aJ + nJ == 0) {
177 ilm 199
            if (!row.getBoolean("COMPTANT") && row.getBoolean("DATE_FACTURE")) {
18 ilm 200
                return Configuration.getInstance().getTranslator().getLabelFor(row.getTable().getField("DATE_FACTURE"));
201
            } else {
202
                return " ";
203
            }
204
        }
61 ilm 205
        Date calculDate = ModeDeReglementSQLElement.calculDate(aJ, nJ, currentDate);
206
        if (datePattern != null && datePattern.trim().length() > 0) {
207
            final DateFormat format2 = new SimpleDateFormat(datePattern);
208
            return format2.format(calculDate);
209
        } else {
210
            return calculDate;
211
        }
18 ilm 212
    }
213
 
214
    public boolean isTypeReplace() {
215
        // remplacement d'un pattern contenu dans la cellule
216
        return this.elt.getAttributeValue("type").equalsIgnoreCase("Replace");
217
    }
218
 
219
    public String getReplacePattern() {
220
        return this.elt.getAttributeValue("replacePattern");
221
    }
222
 
223
    public boolean isMultilineAuto() {
224
        // gestion manuel du multiligne
225
        final String multiLineValue = this.elt.getAttributeValue("controleMultiline");
226
        return (multiLineValue == null) ? true : !multiLineValue.equalsIgnoreCase("false");
227
    }
228
 
132 ilm 229
    public boolean isKeepingEmptyLines() {
230
        // gestion manuel du multiligne
231
        final String k = this.elt.getAttributeValue("keepEmptyLines");
232
        return (k == null) ? false : k.equalsIgnoreCase("true");
233
    }
234
 
180 ilm 235
    public boolean isImage() {
236
        return this.elt.getAttributeValue("type").equalsIgnoreCase("image");
237
    }
182 ilm 238
 
239
    public SQLElement getSQLElement() {
240
        return this.sqlElt;
241
    }
18 ilm 242
}