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.config.ComptaPropsConfiguration;
19 ilm 17
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
18 ilm 18
import org.openconcerto.map.model.Ville;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLField;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowAccessor;
24
import org.openconcerto.sql.model.SQLRowListRSH;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
90 ilm 28
import org.openconcerto.utils.DecimalUtils;
18 ilm 29
import org.openconcerto.utils.GestionDevise;
30
import org.openconcerto.utils.Nombre;
19 ilm 31
import org.openconcerto.utils.StringUtils;
32
import org.openconcerto.utils.Tuple2;
18 ilm 33
 
19 ilm 34
import java.math.BigDecimal;
73 ilm 35
import java.math.RoundingMode;
18 ilm 36
import java.text.DateFormat;
37
import java.text.SimpleDateFormat;
38
import java.util.ArrayList;
41 ilm 39
import java.util.Arrays;
18 ilm 40
import java.util.Calendar;
41
import java.util.Collection;
42
import java.util.Date;
43
import java.util.HashMap;
44
import java.util.List;
45
import java.util.Map;
46
 
132 ilm 47
import org.jdom2.Attribute;
48
import org.jdom2.Element;
18 ilm 49
 
50
public class OOXMLField extends OOXMLElement {
51
 
52
    private String op = "";
53
 
63 ilm 54
    public OOXMLField(Element eltField, SQLRowAccessor row, SQLElement sqlElt, int id, SQLRow rowLanguage, OOXMLCache cache) {
55
        super(eltField, sqlElt, id, rowLanguage, cache);
18 ilm 56
 
57
        String base = eltField.getAttributeValue("base");
58
        this.op = eltField.getAttributeValue("op");
59
 
60
        this.row = row;
61
        if ((this.row == null || !this.row.getTable().getSchema().getName().equalsIgnoreCase("Common")) && base != null && base.equalsIgnoreCase("COMMON")) {
62
            this.row = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
182 ilm 63
            if (!this.row.getString("DATABASE_NAME").equalsIgnoreCase(row.getTable().getDBRoot().getName())) {
64
                this.row = this.row.getTable().getRow(Integer.valueOf(row.getTable().getDBRoot().getName().replaceAll("OpenConcerto", "")));
65
            }
18 ilm 66
        }
19 ilm 67
        if (this.row == null) {
18 ilm 68
            this.row = sqlElt.getTable().getRow(id);
69
        }
70
 
71
    }
72
 
73
    /**
74
     * permet d'obtenir la valeur d'un élément field
75
     *
76
     * @param eltField
77
     * @param row
78
     * @param elt
79
     * @param id
80
     * @return la valeur du composant
81
     */
82
    public Object getValue() {
28 ilm 83
        if (this.row != null && !this.row.isUndefined()) {
18 ilm 84
 
85
            // if
86
            // (this.row.getTable().getName().equalsIgnoreCase(this.elt.getAttributeValue("table")))
87
            // {
88
            String field = this.elt.getAttributeValue("name");
89
 
156 ilm 90
            if (field != null && field.trim().length() > 0 && !this.row.getTable().contains(field)) {
91
                throw new InvalidTemplateException("Le champ " + field + " n'existe pas dans la table " + this.row.getTable().getName());
92
            }
83 ilm 93
            final SQLField sqlField = (field == null || field.trim().length() == 0) ? null : this.row.getTable().getField(field);
94
            boolean isForeignField = (sqlField == null) ? false : this.row.getTable().getForeignKeys().contains(sqlField);
18 ilm 95
 
96
            // le champ est une clef etrangere, on recupere la valeur du sous composant
97
            if (isForeignField && this.elt.getChild("field") != null) {
98
 
99
                String condField = this.elt.getAttributeValue("conditionField");
100
 
101
                if (condField != null && this.row.getTable().getField(condField).getType().getJavaType() == Boolean.class) {
102
                    final Boolean boolean1 = this.row.getBoolean(condField);
103
                    if (!(boolean1 != null && boolean1)) {
104
                        return null;
105
                    }
106
                }
107
 
63 ilm 108
                SQLRowAccessor foreignRow = cache.getForeignRow(this.row, sqlField);
18 ilm 109
                if (foreignRow != null && foreignRow.getID() > 1) {
110
                    final List<Element> children = this.elt.getChildren("field");
111
                    if (children.size() > 1) {
112
                        if (isValid()) {
113
 
114
                            String result = "";
115
                            for (Element ssComposant : children) {
63 ilm 116
                                OOXMLField childElt = new OOXMLField(ssComposant, foreignRow, this.sqlElt, this.id, this.rowLanguage, cache);
18 ilm 117
                                final Object valueComposantO = childElt.getValue();
118
                                result += (valueComposantO == null) ? "" : valueComposantO.toString() + " ";
119
                            }
19 ilm 120
                            String cellSize = this.elt.getAttributeValue("cellSize");
121
                            if (cellSize != null && cellSize.trim().length() != 0) {
122
                                result = splitStringCell(cellSize, result);
123
                            }
18 ilm 124
                            return result.trim();
125
                        } else {
126
                            return "";
127
                        }
128
                    } else {
129
                        if (isValid()) {
63 ilm 130
                            OOXMLField childElt = new OOXMLField(this.elt.getChild("field"), foreignRow, this.sqlElt, this.id, this.rowLanguage, cache);
18 ilm 131
                            return childElt.getValue();
132
                        } else {
133
                            return "";
134
                        }
135
                    }
136
                } else {
137
                    return null;
138
                }
139
            } else {
140
 
141
                // sinon on recupere directement la valeur
142
 
132 ilm 143
                String typeComp = this.elt.getAttributeValue("type");
144
                if (this.op != null && this.op.trim().length() > 0 && !(typeComp != null && typeComp.trim().length() > 0 && typeComp.toLowerCase().startsWith("deviselettre"))) {
18 ilm 145
                    String field2 = this.elt.getAttributeValue("name2");
156 ilm 146
                    if (!this.row.getTable().contains(field)) {
147
                        throw new InvalidTemplateException("Le champ " + field + " n'existe pas dans la table " + this.row.getTable().getName());
148
                    }
18 ilm 149
                    Number o = (Number) this.row.getObject(field);
150
 
21 ilm 151
                    Number o2;
152
                    if (field2 != null && field2.trim().length() > 0) {
156 ilm 153
                        if (!this.row.getTable().contains(field2)) {
154
                            throw new InvalidTemplateException("Le champ " + field2 + " n'existe pas dans la table " + this.row.getTable().getName());
155
                        }
21 ilm 156
                        o2 = (Number) this.row.getObject(field2);
157
                    } else {
158
                        o2 = Double.parseDouble(this.elt.getAttributeValue("number"));
159
                    }
160
 
18 ilm 161
                    if (typeComp != null && typeComp.trim().length() > 0) {
162
 
163
                        // Devise en Long transformée en double
164
                        if (typeComp.equalsIgnoreCase("Devise")) {
67 ilm 165
 
166
                            BigDecimal result = calcul(o, o2, this.op);
93 ilm 167
                            if (o != null && o.getClass().isAssignableFrom(Long.class)) {
67 ilm 168
                                long resultLong = Math.round(result.doubleValue());
169
                                return Double.valueOf(GestionDevise.currencyToString(resultLong, false));
170
                            } else {
171
                                return result;
172
                            }
18 ilm 173
                        }
174
                    }
175
 
176
                    return calcul(o, o2, this.op);
177
 
178
                }
179
 
180
                // Liste des valeurs à ne pas afficher
19 ilm 181
                List<String> listOfExcludedValues = null;
18 ilm 182
                if (this.elt.getAttributeValue("valuesExpected") != null) {
19 ilm 183
                    listOfExcludedValues = SQLRow.toList(this.elt.getAttributeValue("valuesExpected"));
18 ilm 184
                }
19 ilm 185
                List<Element> excludeValue = this.elt.getChildren("exclude");
186
                if (excludeValue != null && excludeValue.size() > 0) {
187
                    if (listOfExcludedValues == null) {
188
                        listOfExcludedValues = new ArrayList<String>();
189
                    } else {
190
                        listOfExcludedValues = new ArrayList<String>(listOfExcludedValues);
191
                    }
18 ilm 192
 
19 ilm 193
                    for (Element element : excludeValue) {
194
                        String attributeValue = element.getAttributeValue("value");
195
                        listOfExcludedValues.add(attributeValue);
196
                    }
197
                }
198
 
18 ilm 199
                // Champ boolean
200
                String condField = this.elt.getAttributeValue("conditionField");
201
                String condValue = this.elt.getAttributeValue("conditionExpValue");
202
 
25 ilm 203
                boolean bIsCondValid = condValue == null || !this.row.getObject(condField).toString().equalsIgnoreCase(condValue);
21 ilm 204
                if (condValue == null) {
205
                    boolean bIsBooleanCondValid = false;
206
                    if (condField == null) {
207
                        bIsBooleanCondValid = true;
208
                    } else {
209
                        if (this.row.getTable().getField(condField).getType().getJavaType() == Boolean.class) {
210
                            final Boolean boolean1 = this.row.getBoolean(condField);
211
                            if (boolean1 != null && boolean1) {
212
                                bIsBooleanCondValid = true;
213
                            }
18 ilm 214
                        }
215
                    }
21 ilm 216
                    bIsCondValid = bIsCondValid && bIsBooleanCondValid;
25 ilm 217
                } else {
21 ilm 218
                    System.err.println();
18 ilm 219
                }
21 ilm 220
                if (bIsCondValid) {
18 ilm 221
 
222
                    // Type du champ
223
                    Object o = getSpecialValue(typeComp);
224
 
225
                    String stringValue;
90 ilm 226
                    String scale = this.elt.getAttributeValue("decimalScale");
18 ilm 227
                    if (o != null) {
90 ilm 228
 
229
                        if (o != null && scale != null && scale.trim().length() > 0) {
230
 
94 ilm 231
                            o = ((BigDecimal) o).setScale(Integer.valueOf(scale), RoundingMode.HALF_UP);
90 ilm 232
                        }
18 ilm 233
                        if (this.elt.getAttributeValue("upperCase") != null) {
234
                            o = o.toString().toUpperCase();
235
                        }
236
                        stringValue = o.toString();
237
                    } else {
156 ilm 238
                        if (!this.row.getTable().contains(field)) {
239
                            throw new InvalidTemplateException("Le champ " + field + " n'existe pas dans la table " + this.row.getTable().getName());
240
                        }
18 ilm 241
                        Object o2 = this.row.getObject(field);
242
 
90 ilm 243
                        if (o2 != null && scale != null && scale.trim().length() > 0) {
244
 
94 ilm 245
                            o2 = ((BigDecimal) o2).setScale(Integer.valueOf(scale), RoundingMode.HALF_UP);
90 ilm 246
                        }
247
 
18 ilm 248
                        stringValue = (o2 == null) ? "" : o2.toString();
249
                    }
250
 
251
                    // on ne fait rien si le champ n'est pas à afficher
19 ilm 252
                    if (listOfExcludedValues == null || ((!listOfExcludedValues.contains(stringValue)) && stringValue.trim().length() > 0)) {
18 ilm 253
                        String prefix = this.elt.getAttributeValue("prefix");
254
                        String suffix = this.elt.getAttributeValue("suffix");
255
                        String display = this.elt.getAttributeValue("display");
142 ilm 256
                        // FIXME attribut cellSize à mettre sur l'element
19 ilm 257
                        String cellSize = this.elt.getAttributeValue("cellSize");
18 ilm 258
                        if (prefix != null || suffix != null) {
259
 
260
                            String result = "";
261
                            if (prefix != null) {
262
                                if (prefix.contains("#n")) {
263
                                    prefix = prefix.replaceAll("#n", "\n");
264
                                }
265
                                result += prefix;
266
                            }
267
 
268
                            if (display == null || !display.equalsIgnoreCase("false")) {
269
                                result += stringValue;
270
                            }
271
                            if (suffix != null) {
174 ilm 272
                                if (suffix.contains("#n")) {
273
                                    suffix = suffix.replaceAll("#n", "\n");
274
                                }
18 ilm 275
                                result += suffix;
276
                            }
19 ilm 277
                            if (cellSize != null && cellSize.trim().length() != 0) {
278
                                result = splitStringCell(cellSize, result);
279
                            }
185 ilm 280
                            String attributeValueMaxChar = this.elt.getAttributeValue("maxChar");
281
                            if (attributeValueMaxChar != null) {
282
                                int maxChar = Integer.valueOf(attributeValueMaxChar);
283
                                if (result != null && result.length() > maxChar) {
284
                                    result = result.substring(0, maxChar);
285
                                }
286
                            }
287
 
18 ilm 288
                            return result;
289
                        } else {
290
                            if (display == null || !display.equalsIgnoreCase("false")) {
185 ilm 291
 
292
                                String attributeValueMaxChar = this.elt.getAttributeValue("maxChar");
293
                                if (attributeValueMaxChar != null) {
294
                                    int maxChar = Integer.valueOf(attributeValueMaxChar);
295
                                    if (o != null && o.toString().length() > maxChar) {
296
                                        o = o.toString().substring(0, maxChar);
297
                                    }
298
                                }
299
 
65 ilm 300
                                if (cellSize != null && cellSize.trim().length() != 0 && o != null) {
301
                                    return splitStringCell(cellSize, o.toString());
302
                                } else {
303
                                    return (o == null) ? "" : o;
304
                                }
18 ilm 305
                            } else {
306
                                return "";
307
                            }
308
                        }
309
                    }
310
                }
311
            }
312
        }
313
 
19 ilm 314
        return null;
18 ilm 315
 
316
    }
317
 
65 ilm 318
    protected String splitStringCell(String cellSize, String result) {
19 ilm 319
        try {
320
            int nbCar = Integer.parseInt(cellSize);
321
            result = StringUtils.splitString(result, nbCar);
322
        } catch (NumberFormatException e) {
323
            e.printStackTrace();
324
        }
325
        return result;
326
    }
327
 
18 ilm 328
    protected Object getSpecialValue(String typeComp) {
83 ilm 329
 
330
        SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(typeComp);
331
        if (provider != null) {
332
            final SpreadSheetCellValueContext context = new SpreadSheetCellValueContext(this.row);
333
            List<Attribute> attrs = this.elt.getAttributes();
334
            for (Attribute attr : attrs) {
335
                context.put(attr.getName(), attr.getValue());
336
            }
337
            return provider.getValue(context);
338
        }
339
 
18 ilm 340
        String field = this.elt.getAttributeValue("name");
19 ilm 341
        final Object result = this.row.getObject(field);
18 ilm 342
 
343
        // Liste des valeurs à ne pas afficher
344
        List<String> listOfExpectedValues = null;
345
        if (this.elt.getAttributeValue("valuesExpected") != null) {
346
            listOfExpectedValues = SQLRow.toList(this.elt.getAttributeValue("valuesExpected"));
347
        }
348
 
349
        if (typeComp != null && typeComp.trim().length() > 0) {
350
 
351
            // Type spécial
352
 
353
            // Devise en Long transformée en double
354
            if (typeComp.equalsIgnoreCase("NumeroEcheance")) {
355
                SQLSelect sel = new SQLSelect(this.row.getTable().getBase());
356
                sel.addSelect(this.row.getTable().getKey(), "COUNT");
357
                Where w = new Where(this.row.getTable().getField("DATE"), "<=", this.row.getDate("DATE").getTime());
358
                sel.setWhere(w);
359
                return this.row.getTable().getBase().getDataSource().executeScalar(sel.asString());
19 ilm 360
            } else if (typeComp.equalsIgnoreCase("Devise")) {
144 ilm 361
                if (result == null) {
362
                    return "";
363
                }
19 ilm 364
                Number prix = (Number) result;
365
                if (listOfExpectedValues != null) {
366
                    for (String string : listOfExpectedValues) {
367
                        Long l = Long.parseLong(string);
368
                        if (l.longValue() == prix.longValue()) {
369
                            return "";
18 ilm 370
                        }
371
                    }
19 ilm 372
                }
67 ilm 373
                if (result instanceof Long) {
374
                    return new Double(GestionDevise.currencyToString(prix.longValue(), false));
375
                } else {
90 ilm 376
                    String scale = this.elt.getAttributeValue("decimalScale");
377
 
378
                    if (result != null && scale != null && scale.trim().length() > 0) {
379
 
93 ilm 380
                        return ((BigDecimal) result).setScale(Integer.valueOf(scale), RoundingMode.HALF_UP);
90 ilm 381
                    }
382
 
67 ilm 383
                    return result;
384
                }
91 ilm 385
            } else if (typeComp.equalsIgnoreCase("cumulPaye")) {
144 ilm 386
                assertNonNull(typeComp, result, field, this.row);
91 ilm 387
                double val = ((Number) result).doubleValue();
142 ilm 388
                double valC;
389
                if (this.row.getTable().getFieldsName().contains("ID_CUMULS_PAYE")) {
390
                    valC = ((Number) this.row.getForeign("ID_CUMULS_PAYE").getObject(field + "_C")).doubleValue();
391
                } else {
392
                    SQLRowAccessor refRow = this.row.getReferentRows(this.row.getTable().getTable("FICHE_PAYE")).iterator().next();
393
                    valC = ((Number) refRow.getForeign("ID_CUMULS_PAYE").getObject(field + "_C")).doubleValue();
394
                }
91 ilm 395
                return new Double(val + valC);
142 ilm 396
            } else if (typeComp.equalsIgnoreCase("cumulConges")) {
144 ilm 397
                assertNonNull(typeComp, result, field, this.row);
142 ilm 398
                double val = ((Number) result).doubleValue();
399
                SQLRowAccessor refRow = this.row.getReferentRows(this.row.getTable().getTable("FICHE_PAYE")).iterator().next();
400
                double valC = ((Number) refRow.getObject("CONGES_ACQUIS")).doubleValue();
401
                return new Double(val + valC);
19 ilm 402
            } else if (typeComp.equalsIgnoreCase("globalAcompte")) {
144 ilm 403
                assertNonNull(typeComp, result, field, this.row);
19 ilm 404
                Long prix = (Long) result;
405
                int pourcent = this.row.getInt("POURCENT_ACOMPTE");
406
                long l = Math.round(prix.longValue() / (pourcent / 100.0));
407
                return new Double(GestionDevise.currencyToString(l, false));
91 ilm 408
            } else if (typeComp.equalsIgnoreCase("DatePaye")) {
409
 
410
                SQLRowAccessor rowRegl = row.getForeign("ID_REGLEMENT_PAYE");
411
                Calendar c = Calendar.getInstance();
412
 
413
                c.set(Calendar.MONTH, this.row.getInt("ID_MOIS") - 2);
414
                c.set(Calendar.YEAR, Integer.parseInt(this.row.getString("ANNEE")));
415
 
416
                if (rowRegl.getInt("LE") != 31) {
417
 
418
                    c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
419
                }
420
 
421
                int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
422
                int day = Math.min(rowRegl.getInt("LE"), max);
423
 
424
                c.set(Calendar.DAY_OF_MONTH, day);
425
                return c.getTime();
19 ilm 426
            } else if (typeComp.equalsIgnoreCase("CumulPrec")) {
427
 
428
                final long cumulPrecedent = getCumulPrecedent(this.row);
429
                return new Double(GestionDevise.currencyToString(cumulPrecedent, false));
430
            } else if (typeComp.equalsIgnoreCase("DeviseLettre")) {
144 ilm 431
                assertNonNull(typeComp, result, field, this.row);
19 ilm 432
                // Devise exprimée en lettre
433
                Long prix = (Long) result;
94 ilm 434
                String deviseLabel = this.elt.getAttributeValue("deviseLabel");
435
                if (deviseLabel == null || deviseLabel.trim().length() == 0) {
436
                    deviseLabel = " euros";
437
                }
438
                String deviseCentLabel = this.elt.getAttributeValue("deviseCentLabel");
439
                if (deviseCentLabel == null || deviseCentLabel.trim().length() == 0) {
440
                    deviseCentLabel = " cents";
441
                }
442
                return getLettreFromDevise(prix.longValue(), Nombre.FR, Tuple2.create(deviseLabel, deviseCentLabel));
19 ilm 443
            } else if (typeComp.equalsIgnoreCase("DeviseLettreEng")) {
144 ilm 444
                assertNonNull(typeComp, result, field, this.row);
19 ilm 445
                Long prix = (Long) result;
94 ilm 446
                return getDeviseLettre(prix, Nombre.EN);
447
            } else if (typeComp.equalsIgnoreCase("DeviseLettrePL")) {
144 ilm 448
                assertNonNull(typeComp, result, field, this.row);
94 ilm 449
                Long prix = (Long) result;
450
                return getDeviseLettre(prix, Nombre.PL);
451
            } else if (typeComp.equalsIgnoreCase("DeviseLettreES")) {
144 ilm 452
                assertNonNull(typeComp, result, field, this.row);
94 ilm 453
                Long prix = (Long) result;
454
                return getDeviseLettre(prix, Nombre.ES);
73 ilm 455
            } else if (typeComp.equalsIgnoreCase("VilleFull")) {
456
                return this.row.getString("CODE_POSTAL") + " " + this.row.getString("VILLE");
19 ilm 457
            } else if (typeComp.equalsIgnoreCase("Ville")) {
144 ilm 458
                String stringValue = (result == null) ? "" : result.toString();
73 ilm 459
                return stringValue;
83 ilm 460
            } else if (typeComp.equalsIgnoreCase("Traduction")) {
461
                return getTraduction();
19 ilm 462
            } else if (typeComp.equalsIgnoreCase("VilleCP")) {
90 ilm 463
                if (this.row.getTable().contains("CODE_POSTAL")) {
464
                    // Code postal de la ville
465
                    return this.row.getString("CODE_POSTAL");
466
                } else {
467
                    Ville v = Ville.getVilleFromVilleEtCode(this.row.getString(field));
468
                    if (v != null) {
469
                        return v.getCodepostal();
470
                    }
471
                    return null;
472
                }
19 ilm 473
            } else if (typeComp.equalsIgnoreCase("DateEcheance")) {
474
                // Retourne la date d'échéance
475
                int idModeReglement = this.row.getInt("ID_MODE_REGLEMENT");
476
                Date d = (Date) this.row.getObject("DATE");
65 ilm 477
                return getDateEcheance(idModeReglement, d, this.elt.getAttributeValue("datePattern"));
19 ilm 478
            } else if (typeComp.equalsIgnoreCase("Jour")) {
479
                int day = this.row.getInt(field);
144 ilm 480
                String stringValue = "le " + String.valueOf(day);
19 ilm 481
                if (day == 31) {
482
                    return "fin de mois";
483
                } else if (day == 0) {
484
                    return "Date de facture";
485
                } else {
486
                    return stringValue;
487
                }
488
            } else if (typeComp.equalsIgnoreCase("Date")) {
489
 
65 ilm 490
                String datePattern = this.elt.getAttributeValue("datePattern");
19 ilm 491
                if (datePattern == null || datePattern.trim().length() == 0) {
492
                    datePattern = "dd/MM/yyyy";
493
                }
494
                SimpleDateFormat format = new SimpleDateFormat(datePattern);
495
                if (result != null) {
496
                    Date d = (Date) result;
497
                    return format.format(d);
498
                } else {
499
                    return "";
500
                }
501
            } else if (typeComp.equalsIgnoreCase("initiale")) {
144 ilm 502
                String stringValue = (result == null) ? "" : result.toString();
19 ilm 503
                if (stringValue.trim().length() > 0) {
504
                    stringValue = String.valueOf(stringValue.charAt(0));
505
                }
506
                return stringValue;
73 ilm 507
            } else if (typeComp.equalsIgnoreCase("initiale2")) {
144 ilm 508
                String stringValue = (result == null) ? "" : result.toString();
73 ilm 509
                if (stringValue.trim().length() > 0) {
510
                    stringValue = String.valueOf(stringValue.substring(0, 2));
511
                }
512
                return stringValue;
18 ilm 513
            }
19 ilm 514
 
18 ilm 515
        }
516
 
19 ilm 517
        return (result == null) ? "" : result;
18 ilm 518
    }
519
 
144 ilm 520
    private void assertNonNull(String type, Object result, String field, SQLRowAccessor row) {
521
        if (result == null) {
522
            throw new IllegalArgumentException("null value for " + type + " for field " + field + " in row : " + row.asRowValues().toString());
523
        }
524
    }
525
 
94 ilm 526
    private Object getDeviseLettre(Long prix, int country) {
527
        String deviseLabel = this.elt.getAttributeValue("deviseLabel");
528
        if (deviseLabel == null || deviseLabel.trim().length() == 0) {
529
            deviseLabel = " euros";
530
        }
531
        String deviseCentLabel = this.elt.getAttributeValue("deviseCentLabel");
532
        if (deviseCentLabel == null || deviseCentLabel.trim().length() == 0) {
533
            deviseCentLabel = " centimes";
534
        }
535
        Tuple2<String, String> defaultTuple = Tuple2.create(deviseLabel, deviseCentLabel);
536
        // Devise exprimée en lettre
537
 
538
        SQLRowAccessor tarif = this.row.getForeign("ID_TARIF");
539
        if (tarif.isUndefined()) {
540
            return getLettreFromDevise(prix.longValue(), country, defaultTuple);
541
        } else {
542
            SQLRowAccessor rowDevise = tarif.getForeign("ID_DEVISE");
543
            if (rowDevise.isUndefined()) {
544
                return getLettreFromDevise(prix.longValue(), country, defaultTuple);
545
            } else {
546
                return getLettreFromDevise(prix.longValue(), country, Tuple2.create(" " + rowDevise.getString("LIBELLE") + " ", " " + rowDevise.getString("LIBELLE_CENT") + " "));
547
            }
548
        }
549
    }
550
 
19 ilm 551
    private Object getTraduction() {
552
        if (this.rowLanguage == null || this.rowLanguage.isUndefined()) {
182 ilm 553
            return this.row.getObject(this.elt.getAttributeValue("name"));
19 ilm 554
        }
555
        int id = ReferenceArticleSQLElement.getIdForCNM(row.asRowValues(), false);
556
        SQLTable table = Configuration.getInstance().getBase().getTable("ARTICLE_DESIGNATION");
557
        SQLSelect sel = new SQLSelect(table.getBase());
558
        sel.addSelectStar(table);
559
        Where w = new Where(table.getField("ID_ARTICLE"), "=", id);
560
        w = w.and(new Where(table.getField("ID_LANGUE"), "=", this.rowLanguage.getID()));
561
        sel.setWhere(w);
562
        List<SQLRow> rows = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
563
        if (rows != null && rows.size() > 0) {
564
            return rows.get(0).getString(this.elt.getAttributeValue("name"));
565
        } else {
566
            return this.row.getObject(this.elt.getAttributeValue("name"));
567
        }
568
    }
569
 
18 ilm 570
    public boolean isValid() {
571
        String condField = this.elt.getAttributeValue("conditionField");
572
        String condValue = this.elt.getAttributeValue("conditionExpValue");
573
        boolean bIsBooleanCondValid = condField == null || this.row.getTable().getField(condField).getType().getJavaType() == Boolean.class && this.row.getBoolean(condField);
574
        boolean bIsCondValid = condValue == null || this.row.getObject(condField).toString().equalsIgnoreCase(condValue);
575
        return bIsBooleanCondValid || !bIsCondValid;
576
    }
577
 
578
    private static long getCumulPrecedent(SQLRowAccessor rowFact) {
579
 
580
        long cumul = 0;
581
 
61 ilm 582
        SQLRowAccessor rowAff = rowFact.getForeign("ID_AFFAIRE");
583
        Calendar date = rowFact.getDate("DATE");
584
        if (rowAff != null && !rowAff.isUndefined()) {
585
            if (rowAff.getBoolean("CCI")) {
18 ilm 586
 
61 ilm 587
                List<SQLRow> rows = rowAff.asRow().getReferentRows(rowFact.getTable());
588
                for (SQLRow sqlRow : rows) {
589
                    if (sqlRow.getID() != rowFact.getID() && sqlRow.getDate("DATE").before(date)) {
590
                        cumul += sqlRow.getLong("T_HT");
591
                    }
592
                }
593
            }
594
        } else {
19 ilm 595
 
61 ilm 596
            // On recupere les missions associées
597
            SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
598
            Collection<? extends SQLRowAccessor> factElts = rowFact.getReferentRows(tableElt);
599
 
600
            for (SQLRowAccessor row : factElts) {
601
 
602
                final SQLRowAccessor foreign = row.getForeign("ID_MISSION");
603
                if (foreign.getID() > 1) {
604
                    Collection<? extends SQLRowAccessor> rowsElt = foreign.getReferentRows(tableElt);
605
                    for (SQLRowAccessor row2 : rowsElt) {
606
                        SQLRowAccessor rowFacture = row2.getForeign("ID_SAISIE_VENTE_FACTURE");
607
                        if (rowFacture.getDate("DATE").before(date)) {
608
                            cumul += row2.getLong("T_PV_HT");
609
                        }
19 ilm 610
                    }
18 ilm 611
                }
612
            }
613
        }
614
 
615
        return cumul;
616
    }
617
 
19 ilm 618
    private static long getMontantGlobal(SQLRowAccessor rowFact) {
18 ilm 619
 
19 ilm 620
        long cumul = 0;
73 ilm 621
        final BigDecimal cent = new BigDecimal(100);
18 ilm 622
 
623
        // On recupere les missions associées
19 ilm 624
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
625
        Collection<? extends SQLRowAccessor> factElts = rowFact.getReferentRows(tableElt);
18 ilm 626
 
19 ilm 627
        for (SQLRowAccessor row : factElts) {
73 ilm 628
            BigDecimal p0 = row.getBigDecimal("MONTANT_INITIAL");
19 ilm 629
            Long l0 = (Long) row.getObject("INDICE_0");
630
            Long lN = (Long) row.getObject("INDICE_N");
73 ilm 631
            final BigDecimal o = row.getBigDecimal("POURCENT_ACOMPTE");
632
            final BigDecimal o2 = row.getBigDecimal("POURCENT_REMISE");
19 ilm 633
            double lA = (o == null) ? 0 : ((BigDecimal) o).doubleValue();
73 ilm 634
            BigDecimal lremise = (o2 == null) ? BigDecimal.ZERO : o2;
635
            BigDecimal p;
19 ilm 636
            if (l0 != 0) {
73 ilm 637
                BigDecimal d;
19 ilm 638
                double coeff = ((double) lN) / ((double) l0);
93 ilm 639
                d = new BigDecimal("0.15").add(new BigDecimal("0.85").multiply(new BigDecimal(coeff), DecimalUtils.HIGH_PRECISION));
90 ilm 640
                p = d.multiply(p0, DecimalUtils.HIGH_PRECISION);
19 ilm 641
            } else {
642
                p = p0;
18 ilm 643
            }
19 ilm 644
            // if (lA >= 0 && lA != 100) {
645
            // p = Math.round(p * (lA / 100.0));
646
            // }
73 ilm 647
            if (lremise.signum() != 0 && lremise.compareTo(BigDecimal.ZERO) > 0 && lremise.compareTo(cent) < 100) {
90 ilm 648
                p = p.multiply(cent.subtract(lremise).movePointLeft(2), DecimalUtils.HIGH_PRECISION);
18 ilm 649
            }
73 ilm 650
            cumul += p.setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
18 ilm 651
        }
652
 
19 ilm 653
        // Echantillons
654
        SQLTable tableEchElt = Configuration.getInstance().getRoot().findTable("ECHANTILLON_ELEMENT");
655
        Collection<? extends SQLRowAccessor> echElts = rowFact.getReferentRows(tableEchElt);
656
        for (SQLRowAccessor sqlRowAccessor : echElts) {
73 ilm 657
            cumul += sqlRowAccessor.getBigDecimal("T_PV_HT").setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
18 ilm 658
        }
19 ilm 659
        return cumul;
18 ilm 660
    }
661
 
19 ilm 662
 
18 ilm 663
    private static List<Integer> getListId(Collection<SQLRow> rowFactElts) {
664
        return getListId(rowFactElts, null);
665
    }
666
 
667
    private static List<Integer> getListId(Collection<SQLRow> rowFactElts, String field) {
668
        List<Integer> l = new ArrayList<Integer>();
669
 
670
        for (SQLRow row : rowFactElts) {
671
            if (field == null) {
672
                l.add(row.getID());
673
            } else {
674
                l.add(row.getInt(field));
675
            }
676
        }
677
        return l;
678
    }
679
 
680
 
681
    /**
682
     * transforme une devise exprimée en chiffres en lettres
683
     *
684
     * @param value
685
     * @return la devise exprimée en lettres
686
     */
132 ilm 687
    private String getLettreFromDevise(long value, int langue, Tuple2<String, String> deviseName) {
18 ilm 688
 
689
        StringBuffer result = new StringBuffer();
690
 
132 ilm 691
        if (this.op != null && this.op.trim().length() > 0) {
692
            String field2 = this.elt.getAttributeValue("name2");
693
 
694
            Number o2;
695
            if (field2 != null && field2.trim().length() > 0) {
696
                o2 = (Number) this.row.getObject(field2);
697
            } else {
698
                o2 = Double.parseDouble(this.elt.getAttributeValue("number"));
699
            }
700
 
701
            value = calcul(value, o2, this.op).setScale(0, RoundingMode.HALF_UP).longValue();
702
        }
703
 
18 ilm 704
        Long decimal = Long.valueOf(value % 100);
705
        Long entier = Long.valueOf(value / 100);
706
 
19 ilm 707
        Nombre n1 = new Nombre(entier.intValue(), langue);
708
        Nombre n2 = new Nombre(decimal.intValue(), langue);
18 ilm 709
 
142 ilm 710
        // 2.51 -> deux euros et cinquante et un centimes
21 ilm 711
        result.append(n1.getText() + " " + deviseName.get0().trim());
142 ilm 712
        if (decimal.intValue() > 0) {
713
            result.append(" " + n1.getSeparateurLabel() + " " + n2.getText() + deviseName.get1());
18 ilm 714
        }
142 ilm 715
        if (result != null && result.length() > 0) {
18 ilm 716
            return result.toString().replaceFirst(String.valueOf(result.charAt(0)), String.valueOf(result.charAt(0)).toUpperCase());
717
        }
142 ilm 718
        return "";
18 ilm 719
    }
720
 
67 ilm 721
    private static BigDecimal calcul(Object o1, Object o2, String op) {
18 ilm 722
 
67 ilm 723
        BigDecimal d1;
724
        if (o1 != null && o1 instanceof BigDecimal) {
725
            d1 = (BigDecimal) o1;
726
        } else {
727
            d1 = (o1 == null) ? BigDecimal.ZERO : new BigDecimal(o1.toString());
728
        }
18 ilm 729
 
67 ilm 730
        BigDecimal d2;
731
        if (o2 != null && o2 instanceof BigDecimal) {
732
            d2 = (BigDecimal) o2;
733
        } else {
734
            d2 = (o2 == null) ? BigDecimal.ZERO : new BigDecimal(o2.toString());
735
        }
144 ilm 736
        // Exemple cmdcliet.total - cmdclient.acompte
737
        if (o1 != null && o1 instanceof Long && o2 != null && o2 instanceof BigDecimal) {
738
            d2 = d2.movePointRight(2);
739
        }
18 ilm 740
        if (op.equalsIgnoreCase("+")) {
90 ilm 741
            return d1.add(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 742
        } else {
743
            if (op.equalsIgnoreCase("-")) {
90 ilm 744
                return d1.subtract(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 745
            } else {
746
                if (op.equalsIgnoreCase("*")) {
90 ilm 747
                    return d1.multiply(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 748
                } else {
67 ilm 749
                    if (op.equalsIgnoreCase("/") && d2.compareTo(BigDecimal.ZERO) != 0) {
90 ilm 750
                        return d1.divide(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 751
                    }
752
                }
753
            }
754
        }
67 ilm 755
        return BigDecimal.ZERO;
18 ilm 756
    }
757
 
758
}