OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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