OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Rev 174 | 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) {
269
                                result += suffix;
270
                            }
19 ilm 271
                            if (cellSize != null && cellSize.trim().length() != 0) {
272
                                result = splitStringCell(cellSize, result);
273
                            }
18 ilm 274
                            return result;
275
                        } else {
276
                            if (display == null || !display.equalsIgnoreCase("false")) {
65 ilm 277
                                if (cellSize != null && cellSize.trim().length() != 0 && o != null) {
278
                                    return splitStringCell(cellSize, o.toString());
279
                                } else {
280
                                    return (o == null) ? "" : o;
281
                                }
18 ilm 282
                            } else {
283
                                return "";
284
                            }
285
                        }
286
                    }
287
                }
288
            }
289
        }
290
 
19 ilm 291
        return null;
18 ilm 292
 
293
    }
294
 
65 ilm 295
    protected String splitStringCell(String cellSize, String result) {
19 ilm 296
        try {
297
            int nbCar = Integer.parseInt(cellSize);
298
            result = StringUtils.splitString(result, nbCar);
299
        } catch (NumberFormatException e) {
300
            e.printStackTrace();
301
        }
302
        return result;
303
    }
304
 
18 ilm 305
    protected Object getSpecialValue(String typeComp) {
83 ilm 306
 
307
        SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(typeComp);
308
        if (provider != null) {
309
            final SpreadSheetCellValueContext context = new SpreadSheetCellValueContext(this.row);
310
            List<Attribute> attrs = this.elt.getAttributes();
311
            for (Attribute attr : attrs) {
312
                context.put(attr.getName(), attr.getValue());
313
            }
314
            return provider.getValue(context);
315
        }
316
 
18 ilm 317
        String field = this.elt.getAttributeValue("name");
19 ilm 318
        final Object result = this.row.getObject(field);
18 ilm 319
 
320
        // Liste des valeurs à ne pas afficher
321
        List<String> listOfExpectedValues = null;
322
        if (this.elt.getAttributeValue("valuesExpected") != null) {
323
            listOfExpectedValues = SQLRow.toList(this.elt.getAttributeValue("valuesExpected"));
324
        }
325
 
326
        if (typeComp != null && typeComp.trim().length() > 0) {
327
 
328
            // Type spécial
329
 
330
            // Devise en Long transformée en double
331
            if (typeComp.equalsIgnoreCase("NumeroEcheance")) {
332
                SQLSelect sel = new SQLSelect(this.row.getTable().getBase());
333
                sel.addSelect(this.row.getTable().getKey(), "COUNT");
334
                Where w = new Where(this.row.getTable().getField("DATE"), "<=", this.row.getDate("DATE").getTime());
335
                sel.setWhere(w);
336
                return this.row.getTable().getBase().getDataSource().executeScalar(sel.asString());
19 ilm 337
            } else if (typeComp.equalsIgnoreCase("Devise")) {
144 ilm 338
                if (result == null) {
339
                    return "";
340
                }
19 ilm 341
                Number prix = (Number) result;
342
                if (listOfExpectedValues != null) {
343
                    for (String string : listOfExpectedValues) {
344
                        Long l = Long.parseLong(string);
345
                        if (l.longValue() == prix.longValue()) {
346
                            return "";
18 ilm 347
                        }
348
                    }
19 ilm 349
                }
67 ilm 350
                if (result instanceof Long) {
351
                    return new Double(GestionDevise.currencyToString(prix.longValue(), false));
352
                } else {
90 ilm 353
                    String scale = this.elt.getAttributeValue("decimalScale");
354
 
355
                    if (result != null && scale != null && scale.trim().length() > 0) {
356
 
93 ilm 357
                        return ((BigDecimal) result).setScale(Integer.valueOf(scale), RoundingMode.HALF_UP);
90 ilm 358
                    }
359
 
67 ilm 360
                    return result;
361
                }
91 ilm 362
            } else if (typeComp.equalsIgnoreCase("cumulPaye")) {
144 ilm 363
                assertNonNull(typeComp, result, field, this.row);
91 ilm 364
                double val = ((Number) result).doubleValue();
142 ilm 365
                double valC;
366
                if (this.row.getTable().getFieldsName().contains("ID_CUMULS_PAYE")) {
367
                    valC = ((Number) this.row.getForeign("ID_CUMULS_PAYE").getObject(field + "_C")).doubleValue();
368
                } else {
369
                    SQLRowAccessor refRow = this.row.getReferentRows(this.row.getTable().getTable("FICHE_PAYE")).iterator().next();
370
                    valC = ((Number) refRow.getForeign("ID_CUMULS_PAYE").getObject(field + "_C")).doubleValue();
371
                }
91 ilm 372
                return new Double(val + valC);
142 ilm 373
            } else if (typeComp.equalsIgnoreCase("cumulConges")) {
144 ilm 374
                assertNonNull(typeComp, result, field, this.row);
142 ilm 375
                double val = ((Number) result).doubleValue();
376
                SQLRowAccessor refRow = this.row.getReferentRows(this.row.getTable().getTable("FICHE_PAYE")).iterator().next();
377
                double valC = ((Number) refRow.getObject("CONGES_ACQUIS")).doubleValue();
378
                return new Double(val + valC);
19 ilm 379
            } else if (typeComp.equalsIgnoreCase("globalAcompte")) {
144 ilm 380
                assertNonNull(typeComp, result, field, this.row);
19 ilm 381
                Long prix = (Long) result;
382
                int pourcent = this.row.getInt("POURCENT_ACOMPTE");
383
                long l = Math.round(prix.longValue() / (pourcent / 100.0));
384
                return new Double(GestionDevise.currencyToString(l, false));
91 ilm 385
            } else if (typeComp.equalsIgnoreCase("DatePaye")) {
386
 
387
                SQLRowAccessor rowRegl = row.getForeign("ID_REGLEMENT_PAYE");
388
                Calendar c = Calendar.getInstance();
389
 
390
                c.set(Calendar.MONTH, this.row.getInt("ID_MOIS") - 2);
391
                c.set(Calendar.YEAR, Integer.parseInt(this.row.getString("ANNEE")));
392
 
393
                if (rowRegl.getInt("LE") != 31) {
394
 
395
                    c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
396
                }
397
 
398
                int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
399
                int day = Math.min(rowRegl.getInt("LE"), max);
400
 
401
                c.set(Calendar.DAY_OF_MONTH, day);
402
                return c.getTime();
19 ilm 403
            } else if (typeComp.equalsIgnoreCase("CumulPrec")) {
404
 
405
                final long cumulPrecedent = getCumulPrecedent(this.row);
406
                return new Double(GestionDevise.currencyToString(cumulPrecedent, false));
407
            } else if (typeComp.equalsIgnoreCase("DeviseLettre")) {
144 ilm 408
                assertNonNull(typeComp, result, field, this.row);
19 ilm 409
                // Devise exprimée en lettre
410
                Long prix = (Long) result;
94 ilm 411
                String deviseLabel = this.elt.getAttributeValue("deviseLabel");
412
                if (deviseLabel == null || deviseLabel.trim().length() == 0) {
413
                    deviseLabel = " euros";
414
                }
415
                String deviseCentLabel = this.elt.getAttributeValue("deviseCentLabel");
416
                if (deviseCentLabel == null || deviseCentLabel.trim().length() == 0) {
417
                    deviseCentLabel = " cents";
418
                }
419
                return getLettreFromDevise(prix.longValue(), Nombre.FR, Tuple2.create(deviseLabel, deviseCentLabel));
19 ilm 420
            } else if (typeComp.equalsIgnoreCase("DeviseLettreEng")) {
144 ilm 421
                assertNonNull(typeComp, result, field, this.row);
19 ilm 422
                Long prix = (Long) result;
94 ilm 423
                return getDeviseLettre(prix, Nombre.EN);
424
            } else if (typeComp.equalsIgnoreCase("DeviseLettrePL")) {
144 ilm 425
                assertNonNull(typeComp, result, field, this.row);
94 ilm 426
                Long prix = (Long) result;
427
                return getDeviseLettre(prix, Nombre.PL);
428
            } else if (typeComp.equalsIgnoreCase("DeviseLettreES")) {
144 ilm 429
                assertNonNull(typeComp, result, field, this.row);
94 ilm 430
                Long prix = (Long) result;
431
                return getDeviseLettre(prix, Nombre.ES);
73 ilm 432
            } else if (typeComp.equalsIgnoreCase("VilleFull")) {
433
                return this.row.getString("CODE_POSTAL") + " " + this.row.getString("VILLE");
19 ilm 434
            } else if (typeComp.equalsIgnoreCase("Ville")) {
144 ilm 435
                String stringValue = (result == null) ? "" : result.toString();
73 ilm 436
                return stringValue;
83 ilm 437
            } else if (typeComp.equalsIgnoreCase("Traduction")) {
438
                return getTraduction();
19 ilm 439
            } else if (typeComp.equalsIgnoreCase("VilleCP")) {
90 ilm 440
                if (this.row.getTable().contains("CODE_POSTAL")) {
441
                    // Code postal de la ville
442
                    return this.row.getString("CODE_POSTAL");
443
                } else {
444
                    Ville v = Ville.getVilleFromVilleEtCode(this.row.getString(field));
445
                    if (v != null) {
446
                        return v.getCodepostal();
447
                    }
448
                    return null;
449
                }
19 ilm 450
            } else if (typeComp.equalsIgnoreCase("DateEcheance")) {
451
                // Retourne la date d'échéance
452
                int idModeReglement = this.row.getInt("ID_MODE_REGLEMENT");
453
                Date d = (Date) this.row.getObject("DATE");
65 ilm 454
                return getDateEcheance(idModeReglement, d, this.elt.getAttributeValue("datePattern"));
19 ilm 455
            } else if (typeComp.equalsIgnoreCase("Jour")) {
456
                int day = this.row.getInt(field);
144 ilm 457
                String stringValue = "le " + String.valueOf(day);
19 ilm 458
                if (day == 31) {
459
                    return "fin de mois";
460
                } else if (day == 0) {
461
                    return "Date de facture";
462
                } else {
463
                    return stringValue;
464
                }
465
            } else if (typeComp.equalsIgnoreCase("Date")) {
466
 
65 ilm 467
                String datePattern = this.elt.getAttributeValue("datePattern");
19 ilm 468
                if (datePattern == null || datePattern.trim().length() == 0) {
469
                    datePattern = "dd/MM/yyyy";
470
                }
471
                SimpleDateFormat format = new SimpleDateFormat(datePattern);
472
                if (result != null) {
473
                    Date d = (Date) result;
474
                    return format.format(d);
475
                } else {
476
                    return "";
477
                }
478
            } else if (typeComp.equalsIgnoreCase("initiale")) {
144 ilm 479
                String stringValue = (result == null) ? "" : result.toString();
19 ilm 480
                if (stringValue.trim().length() > 0) {
481
                    stringValue = String.valueOf(stringValue.charAt(0));
482
                }
483
                return stringValue;
73 ilm 484
            } else if (typeComp.equalsIgnoreCase("initiale2")) {
144 ilm 485
                String stringValue = (result == null) ? "" : result.toString();
73 ilm 486
                if (stringValue.trim().length() > 0) {
487
                    stringValue = String.valueOf(stringValue.substring(0, 2));
488
                }
489
                return stringValue;
18 ilm 490
            }
19 ilm 491
 
18 ilm 492
        }
493
 
19 ilm 494
        return (result == null) ? "" : result;
18 ilm 495
    }
496
 
144 ilm 497
    private void assertNonNull(String type, Object result, String field, SQLRowAccessor row) {
498
        if (result == null) {
499
            throw new IllegalArgumentException("null value for " + type + " for field " + field + " in row : " + row.asRowValues().toString());
500
        }
501
    }
502
 
94 ilm 503
    private Object getDeviseLettre(Long prix, int country) {
504
        String deviseLabel = this.elt.getAttributeValue("deviseLabel");
505
        if (deviseLabel == null || deviseLabel.trim().length() == 0) {
506
            deviseLabel = " euros";
507
        }
508
        String deviseCentLabel = this.elt.getAttributeValue("deviseCentLabel");
509
        if (deviseCentLabel == null || deviseCentLabel.trim().length() == 0) {
510
            deviseCentLabel = " centimes";
511
        }
512
        Tuple2<String, String> defaultTuple = Tuple2.create(deviseLabel, deviseCentLabel);
513
        // Devise exprimée en lettre
514
 
515
        SQLRowAccessor tarif = this.row.getForeign("ID_TARIF");
516
        if (tarif.isUndefined()) {
517
            return getLettreFromDevise(prix.longValue(), country, defaultTuple);
518
        } else {
519
            SQLRowAccessor rowDevise = tarif.getForeign("ID_DEVISE");
520
            if (rowDevise.isUndefined()) {
521
                return getLettreFromDevise(prix.longValue(), country, defaultTuple);
522
            } else {
523
                return getLettreFromDevise(prix.longValue(), country, Tuple2.create(" " + rowDevise.getString("LIBELLE") + " ", " " + rowDevise.getString("LIBELLE_CENT") + " "));
524
            }
525
        }
526
    }
527
 
19 ilm 528
    private Object getTraduction() {
529
        if (this.rowLanguage == null || this.rowLanguage.isUndefined()) {
530
            return null;
531
        }
532
        int id = ReferenceArticleSQLElement.getIdForCNM(row.asRowValues(), false);
533
        SQLTable table = Configuration.getInstance().getBase().getTable("ARTICLE_DESIGNATION");
534
        SQLSelect sel = new SQLSelect(table.getBase());
535
        sel.addSelectStar(table);
536
        Where w = new Where(table.getField("ID_ARTICLE"), "=", id);
537
        w = w.and(new Where(table.getField("ID_LANGUE"), "=", this.rowLanguage.getID()));
538
        sel.setWhere(w);
539
        List<SQLRow> rows = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
540
        if (rows != null && rows.size() > 0) {
541
            return rows.get(0).getString(this.elt.getAttributeValue("name"));
542
        } else {
543
            return this.row.getObject(this.elt.getAttributeValue("name"));
544
        }
545
    }
546
 
18 ilm 547
    public boolean isValid() {
548
        String condField = this.elt.getAttributeValue("conditionField");
549
        String condValue = this.elt.getAttributeValue("conditionExpValue");
550
        boolean bIsBooleanCondValid = condField == null || this.row.getTable().getField(condField).getType().getJavaType() == Boolean.class && this.row.getBoolean(condField);
551
        boolean bIsCondValid = condValue == null || this.row.getObject(condField).toString().equalsIgnoreCase(condValue);
552
        return bIsBooleanCondValid || !bIsCondValid;
553
    }
554
 
555
    private static long getCumulPrecedent(SQLRowAccessor rowFact) {
556
 
557
        long cumul = 0;
558
 
61 ilm 559
        SQLRowAccessor rowAff = rowFact.getForeign("ID_AFFAIRE");
560
        Calendar date = rowFact.getDate("DATE");
561
        if (rowAff != null && !rowAff.isUndefined()) {
562
            if (rowAff.getBoolean("CCI")) {
18 ilm 563
 
61 ilm 564
                List<SQLRow> rows = rowAff.asRow().getReferentRows(rowFact.getTable());
565
                for (SQLRow sqlRow : rows) {
566
                    if (sqlRow.getID() != rowFact.getID() && sqlRow.getDate("DATE").before(date)) {
567
                        cumul += sqlRow.getLong("T_HT");
568
                    }
569
                }
570
            }
571
        } else {
19 ilm 572
 
61 ilm 573
            // On recupere les missions associées
574
            SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
575
            Collection<? extends SQLRowAccessor> factElts = rowFact.getReferentRows(tableElt);
576
 
577
            for (SQLRowAccessor row : factElts) {
578
 
579
                final SQLRowAccessor foreign = row.getForeign("ID_MISSION");
580
                if (foreign.getID() > 1) {
581
                    Collection<? extends SQLRowAccessor> rowsElt = foreign.getReferentRows(tableElt);
582
                    for (SQLRowAccessor row2 : rowsElt) {
583
                        SQLRowAccessor rowFacture = row2.getForeign("ID_SAISIE_VENTE_FACTURE");
584
                        if (rowFacture.getDate("DATE").before(date)) {
585
                            cumul += row2.getLong("T_PV_HT");
586
                        }
19 ilm 587
                    }
18 ilm 588
                }
589
            }
590
        }
591
 
592
        return cumul;
593
    }
594
 
19 ilm 595
    private static long getMontantGlobal(SQLRowAccessor rowFact) {
18 ilm 596
 
19 ilm 597
        long cumul = 0;
73 ilm 598
        final BigDecimal cent = new BigDecimal(100);
18 ilm 599
 
600
        // On recupere les missions associées
19 ilm 601
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
602
        Collection<? extends SQLRowAccessor> factElts = rowFact.getReferentRows(tableElt);
18 ilm 603
 
19 ilm 604
        for (SQLRowAccessor row : factElts) {
73 ilm 605
            BigDecimal p0 = row.getBigDecimal("MONTANT_INITIAL");
19 ilm 606
            Long l0 = (Long) row.getObject("INDICE_0");
607
            Long lN = (Long) row.getObject("INDICE_N");
73 ilm 608
            final BigDecimal o = row.getBigDecimal("POURCENT_ACOMPTE");
609
            final BigDecimal o2 = row.getBigDecimal("POURCENT_REMISE");
19 ilm 610
            double lA = (o == null) ? 0 : ((BigDecimal) o).doubleValue();
73 ilm 611
            BigDecimal lremise = (o2 == null) ? BigDecimal.ZERO : o2;
612
            BigDecimal p;
19 ilm 613
            if (l0 != 0) {
73 ilm 614
                BigDecimal d;
19 ilm 615
                double coeff = ((double) lN) / ((double) l0);
93 ilm 616
                d = new BigDecimal("0.15").add(new BigDecimal("0.85").multiply(new BigDecimal(coeff), DecimalUtils.HIGH_PRECISION));
90 ilm 617
                p = d.multiply(p0, DecimalUtils.HIGH_PRECISION);
19 ilm 618
            } else {
619
                p = p0;
18 ilm 620
            }
19 ilm 621
            // if (lA >= 0 && lA != 100) {
622
            // p = Math.round(p * (lA / 100.0));
623
            // }
73 ilm 624
            if (lremise.signum() != 0 && lremise.compareTo(BigDecimal.ZERO) > 0 && lremise.compareTo(cent) < 100) {
90 ilm 625
                p = p.multiply(cent.subtract(lremise).movePointLeft(2), DecimalUtils.HIGH_PRECISION);
18 ilm 626
            }
73 ilm 627
            cumul += p.setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
18 ilm 628
        }
629
 
19 ilm 630
        // Echantillons
631
        SQLTable tableEchElt = Configuration.getInstance().getRoot().findTable("ECHANTILLON_ELEMENT");
632
        Collection<? extends SQLRowAccessor> echElts = rowFact.getReferentRows(tableEchElt);
633
        for (SQLRowAccessor sqlRowAccessor : echElts) {
73 ilm 634
            cumul += sqlRowAccessor.getBigDecimal("T_PV_HT").setScale(2, RoundingMode.HALF_UP).movePointRight(2).longValue();
18 ilm 635
        }
19 ilm 636
        return cumul;
18 ilm 637
    }
638
 
19 ilm 639
 
18 ilm 640
    private static List<Integer> getListId(Collection<SQLRow> rowFactElts) {
641
        return getListId(rowFactElts, null);
642
    }
643
 
644
    private static List<Integer> getListId(Collection<SQLRow> rowFactElts, String field) {
645
        List<Integer> l = new ArrayList<Integer>();
646
 
647
        for (SQLRow row : rowFactElts) {
648
            if (field == null) {
649
                l.add(row.getID());
650
            } else {
651
                l.add(row.getInt(field));
652
            }
653
        }
654
        return l;
655
    }
656
 
657
 
658
    /**
659
     * transforme une devise exprimée en chiffres en lettres
660
     *
661
     * @param value
662
     * @return la devise exprimée en lettres
663
     */
132 ilm 664
    private String getLettreFromDevise(long value, int langue, Tuple2<String, String> deviseName) {
18 ilm 665
 
666
        StringBuffer result = new StringBuffer();
667
 
132 ilm 668
        if (this.op != null && this.op.trim().length() > 0) {
669
            String field2 = this.elt.getAttributeValue("name2");
670
 
671
            Number o2;
672
            if (field2 != null && field2.trim().length() > 0) {
673
                o2 = (Number) this.row.getObject(field2);
674
            } else {
675
                o2 = Double.parseDouble(this.elt.getAttributeValue("number"));
676
            }
677
 
678
            value = calcul(value, o2, this.op).setScale(0, RoundingMode.HALF_UP).longValue();
679
        }
680
 
18 ilm 681
        Long decimal = Long.valueOf(value % 100);
682
        Long entier = Long.valueOf(value / 100);
683
 
19 ilm 684
        Nombre n1 = new Nombre(entier.intValue(), langue);
685
        Nombre n2 = new Nombre(decimal.intValue(), langue);
18 ilm 686
 
142 ilm 687
        // 2.51 -> deux euros et cinquante et un centimes
21 ilm 688
        result.append(n1.getText() + " " + deviseName.get0().trim());
142 ilm 689
        if (decimal.intValue() > 0) {
690
            result.append(" " + n1.getSeparateurLabel() + " " + n2.getText() + deviseName.get1());
18 ilm 691
        }
142 ilm 692
        if (result != null && result.length() > 0) {
18 ilm 693
            return result.toString().replaceFirst(String.valueOf(result.charAt(0)), String.valueOf(result.charAt(0)).toUpperCase());
694
        }
142 ilm 695
        return "";
18 ilm 696
    }
697
 
67 ilm 698
    private static BigDecimal calcul(Object o1, Object o2, String op) {
18 ilm 699
 
67 ilm 700
        BigDecimal d1;
701
        if (o1 != null && o1 instanceof BigDecimal) {
702
            d1 = (BigDecimal) o1;
703
        } else {
704
            d1 = (o1 == null) ? BigDecimal.ZERO : new BigDecimal(o1.toString());
705
        }
18 ilm 706
 
67 ilm 707
        BigDecimal d2;
708
        if (o2 != null && o2 instanceof BigDecimal) {
709
            d2 = (BigDecimal) o2;
710
        } else {
711
            d2 = (o2 == null) ? BigDecimal.ZERO : new BigDecimal(o2.toString());
712
        }
144 ilm 713
        // Exemple cmdcliet.total - cmdclient.acompte
714
        if (o1 != null && o1 instanceof Long && o2 != null && o2 instanceof BigDecimal) {
715
            d2 = d2.movePointRight(2);
716
        }
18 ilm 717
        if (op.equalsIgnoreCase("+")) {
90 ilm 718
            return d1.add(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 719
        } else {
720
            if (op.equalsIgnoreCase("-")) {
90 ilm 721
                return d1.subtract(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 722
            } else {
723
                if (op.equalsIgnoreCase("*")) {
90 ilm 724
                    return d1.multiply(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 725
                } else {
67 ilm 726
                    if (op.equalsIgnoreCase("/") && d2.compareTo(BigDecimal.ZERO) != 0) {
90 ilm 727
                        return d1.divide(d2, DecimalUtils.HIGH_PRECISION);
18 ilm 728
                    }
729
                }
730
            }
731
        }
67 ilm 732
        return BigDecimal.ZERO;
18 ilm 733
    }
734
 
735
}