OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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