OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | 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.config.ComptaPropsConfiguration;
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
import org.openconcerto.utils.Nombre;
24
 
73 ilm 25
import java.math.BigDecimal;
132 ilm 26
import java.text.DateFormat;
27
import java.text.SimpleDateFormat;
18 ilm 28
import java.util.ArrayList;
41 ilm 29
import java.util.Arrays;
18 ilm 30
import java.util.Calendar;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.List;
34
 
132 ilm 35
import org.jdom2.Attribute;
36
import org.jdom2.Element;
18 ilm 37
 
38
public class OOXMLTableField extends OOXMLField {
39
 
40
    private String type;
41
    private int filterId, line;
21 ilm 42
    private String lineOption;
43
    private int idRef;
44
    private String style = "";
180 ilm 45
 
46
 
47
 
18 ilm 48
 
63 ilm 49
    public OOXMLTableField(Element eltField, SQLRowAccessor row, SQLElement sqlElt, int id, int filterId, SQLRow rowLanguage, int idRef, OOXMLCache cache) {
50
        super(eltField, row, sqlElt, id, rowLanguage, cache);
18 ilm 51
        this.type = eltField.getAttributeValue("type");
21 ilm 52
        this.lineOption = eltField.getAttributeValue("lineOption");
18 ilm 53
        this.filterId = filterId;
54
        String s = eltField.getAttributeValue("line");
55
        this.line = 1;
21 ilm 56
        this.idRef = idRef;
57
 
18 ilm 58
        if (s != null && s.trim().length() > 0) {
59
            this.line = Integer.valueOf(s);
60
        }
21 ilm 61
 
62
        String style = eltField.getAttributeValue("style");
63
        if (style != null && style.trim().length() > 0) {
64
            this.style = style;
65
        }
18 ilm 66
    }
67
 
68
    @Override
69
    public Object getValue() {
70
        Object value = null;
71
 
63 ilm 72
        SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(type);
73
        if (provider != null) {
74
            final SpreadSheetCellValueContext context = new SpreadSheetCellValueContext(this.row);
75
            List<Attribute> attrs = this.elt.getAttributes();
76
            for (Attribute attr : attrs) {
77
                context.put(attr.getName(), attr.getValue());
78
            }
67 ilm 79
            value = provider.getValue(context);
80
        } else if (this.type.equalsIgnoreCase("LineReference")) {
21 ilm 81
            return idRef;
82
        } else if (this.type.equalsIgnoreCase("DescriptifArticle")) {
18 ilm 83
            value = getDescriptifArticle(this.row);
19 ilm 84
        } else if (this.type.equalsIgnoreCase("DateEcheance")) {
65 ilm 85
            value = getDateEcheance(this.row.getInt("ID_MODE_REGLEMENT"), (Date) this.row.getObject("DATE"), this.elt.getAttributeValue("datePattern"));
19 ilm 86
        } else if (this.type.equalsIgnoreCase("MontantRevise")) {
87
            value = getMontantRevise(this.row);
88
        } else if (this.type.equalsIgnoreCase("Localisation")) {
89
            value = getLocalisation(this.row);
18 ilm 90
        } else {
63 ilm 91
            OOXMLElement eltXml = new OOXMLElement(this.elt, this.sqlElt, this.id, this.row, this.rowLanguage, cache);
19 ilm 92
            value = eltXml.getValue();
65 ilm 93
            String cellSize = this.elt.getAttributeValue("cellSize");
94
            if (cellSize != null && cellSize.trim().length() != 0 && value != null) {
95
                value = splitStringCell(cellSize, value.toString());
96
            }
18 ilm 97
        }
98
 
67 ilm 99
        // Liste des valeurs à ne pas afficher
100
        List<String> listOfExcludedValues = null;
101
 
102
        List<Element> excludeValue = this.elt.getChildren("exclude");
103
        if (excludeValue != null && excludeValue.size() > 0) {
104
 
105
            listOfExcludedValues = new ArrayList<String>();
106
 
107
            for (Element element : excludeValue) {
108
                String attributeValue = element.getAttributeValue("value");
109
                listOfExcludedValues.add(attributeValue);
110
            }
111
        }
112
 
113
        if (value != null && listOfExcludedValues != null && listOfExcludedValues.contains(value.toString())) {
114
            value = null;
115
        }
116
 
18 ilm 117
        return value;
118
    }
119
 
120
    public boolean isNeeding2Lines() {
132 ilm 121
        return (this.type.equalsIgnoreCase("DescriptifArticle") || this.type.equalsIgnoreCase("propositionFacture") || this.type.equalsIgnoreCase("DateEcheance")
122
                || this.type.equalsIgnoreCase("MontantRevise"));
18 ilm 123
    }
124
 
125
    public int getLine() {
126
        return this.line;
127
    }
128
 
21 ilm 129
    public boolean isLineOption() {
130
        return Boolean.valueOf(this.lineOption);
131
    }
132
 
18 ilm 133
    /**
134
     * @param row
135
     * @return Descriptif de l'article (Longueur, largeur, poids, surface)
136
     */
137
    private static String getDescriptifArticle(SQLRowAccessor row) {
138
        float longueur = row.getFloat("VALEUR_METRIQUE_1");
139
        float largeur = row.getFloat("VALEUR_METRIQUE_2");
140
        // float f = row.getFloat("POIDS");
141
 
142
        StringBuffer result = new StringBuffer();
143
        if (longueur != 0) {
144
            result.append(" Longueur: " + longueur + " m");
145
        }
146
        if (largeur != 0) {
147
            result.append(" Largeur: " + largeur + " m");
148
        }
149
        if (largeur != 0 && longueur != 0) {
150
            result.append(" Surface : " + Math.round(largeur * longueur * 1000) / 1000.0 + " m²");
151
        }
152
        // if (f != 0) {
153
        // result.append(" Poids : " + f + " kg");
154
        // }
155
        return result.toString();
156
    }
157
 
158
    /**
159
     * @param row
160
     * @return la date + la localisation
161
     */
162
    private static String getLocalisation(SQLRowAccessor row) {
163
        StringBuffer string = new StringBuffer();
164
        String site = row.getString("LOCAL_OBJET_INSPECTE");
165
        if (site != null) {
166
            string.append(site);
167
        }
168
        Object date = row.getObject("DATE");
169
        Object dateFin = row.getObject("DATE_FIN");
170
        if (date != null) {
132 ilm 171
            DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
18 ilm 172
            String stringDate = format.format((Date) date);
173
            if (dateFin != null) {
174
                String stringDateFin = format.format((Date) dateFin);
175
                string.append(" du " + stringDate + " au " + stringDateFin);
176
            } else {
177
                string.append(" le " + stringDate);
178
            }
179
        }
180
 
181
        return string.toString();
182
    }
183
 
184
    /**
185
     * @param row
186
     * @return la formule de calcul du montant revise
187
     */
63 ilm 188
    private String getMontantRevise(SQLRowAccessor row) {
18 ilm 189
        long indice0 = (Long) row.getObject("INDICE_0");
190
        long indiceN = (Long) row.getObject("INDICE_N");
73 ilm 191
        BigDecimal montantInit = row.getBigDecimal("MONTANT_INITIAL");
18 ilm 192
 
193
        if (indice0 == indiceN || indice0 == 0) {
194
            return null;
195
        }
196
 
197
        Boolean clientPrive = false;
198
        try {
199
            SQLRowAccessor rowFact;
200
            if (row.getTable().getName().startsWith("SAISIE_VENTE_FACTURE")) {
63 ilm 201
                rowFact = cache.getForeignRow(row, row.getTable().getField("ID_SAISIE_VENTE_FACTURE"));
18 ilm 202
            } else {
63 ilm 203
                rowFact = cache.getForeignRow(row, row.getTable().getField("ID_AVOIR_CLIENT"));
18 ilm 204
            }
205
            if (rowFact != null) {
63 ilm 206
                SQLRowAccessor rowClient = cache.getForeignRow(rowFact, rowFact.getTable().getField("ID_CLIENT"));
18 ilm 207
                if (rowClient != null) {
132 ilm 208
                    clientPrive = rowClient.getForeign("ID_CLIENT").getBoolean("MARCHE_PRIVE");
18 ilm 209
                }
210
            }
211
        } catch (IllegalArgumentException e) {
212
            e.printStackTrace();
213
        }
214
 
215
        StringBuffer result = new StringBuffer();
216
        result.append("Détail : ");
217
 
218
        if (clientPrive) {
219
            result.append(GestionDevise.currencyToString(indiceN));
220
            result.append(" / ");
221
            result.append(GestionDevise.currencyToString(indice0));
222
            result.append(" x ");
223
            result.append(GestionDevise.currencyToString(montantInit));
224
            result.append(" € HT");
225
            return result.toString();
226
        } else {
93 ilm 227
            // result.append("(0,125 + 0,875 x (");
18 ilm 228
            result.append("(0,15 + 0,85 x (");
229
            result.append(GestionDevise.currencyToString(indiceN));
230
            result.append(" / ");
231
            result.append(GestionDevise.currencyToString(indice0));
232
            result.append(")) x ");
233
            result.append(GestionDevise.currencyToString(montantInit));
234
            result.append(" € HT");
235
            return result.toString();
236
        }
237
    }
238
 
239
    public List<String> getBlankStyle() {
240
        // Cellule pour un style défini
241
        String blankOnStyle = this.elt.getAttributeValue("blankOnStyle");
242
        List<String> listBlankStyle = new ArrayList<String>();
243
        if (blankOnStyle != null) {
244
            listBlankStyle = SQLRow.toList(blankOnStyle.trim());
245
        }
246
        return listBlankStyle;
247
    }
21 ilm 248
 
249
    public String getStyle() {
250
 
251
        return this.style;
252
    }
180 ilm 253
 
254
    public boolean isImage() {
255
        return this.type.equalsIgnoreCase("image");
256
    }
257
 
18 ilm 258
}