OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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.core.finance.payment.element.ModeDeReglementSQLElement;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.users.UserManager;
23
import org.openconcerto.utils.GestionDevise;
24
 
25
import java.text.DateFormat;
26
import java.text.SimpleDateFormat;
27
import java.util.ArrayList;
28
import java.util.Collection;
29
import java.util.Date;
30
import java.util.List;
31
 
32
import org.jdom.Element;
33
 
34
public class OOXMLElement {
35
    protected Element elt;
36
    protected SQLElement sqlElt;
37
    protected int id;
38
    protected SQLRowAccessor row = null;
39
 
40
    public OOXMLElement(Element elt, SQLElement sqlElt, int id) {
41
        this.elt = elt;
42
        this.sqlElt = sqlElt;
43
        this.id = id;
44
    }
45
 
46
    public OOXMLElement(Element elt, SQLElement sqlElt, int id, SQLRowAccessor row) {
47
        this.elt = elt;
48
        this.sqlElt = sqlElt;
49
        this.id = id;
50
        this.row = row;
51
    }
52
 
53
    public Object getValue() {
54
        Object res = "";
55
 
56
        final String attributeValue = this.elt.getAttributeValue("type");
57
 
58
        if (attributeValue.equalsIgnoreCase("InitialesUtilisateur")) {
59
            return getInitialesUser();
60
        }
61
 
62
        if (attributeValue.equalsIgnoreCase("InitialesUtilisateurModif")) {
63
            return getInitialesUserModify();
64
        }
65
 
66
        if (attributeValue.equalsIgnoreCase("InitialesUtilisateurCreate")) {
67
            return getInitialesUserCreate();
68
        }
69
 
70
        if (attributeValue.equalsIgnoreCase("propositionFacture")) {
71
            return getProposition(row);
72
        }
73
 
74
        if (attributeValue.equalsIgnoreCase("ListeVerificateur")) {
75
            return getListeVerificateur(row);
76
        }
77
 
78
        if (attributeValue.equalsIgnoreCase("TotalHTTable")) {
79
            return getTotalHTTable(row);
80
        }
81
 
82
        if (attributeValue.equalsIgnoreCase("codesMissions")) {
83
            return getCodesMissions(this.id);
84
        }
85
 
86
        if (attributeValue.equalsIgnoreCase("DateEcheance")) {
87
            int idModeReglement = row.getInt("ID_MODE_REGLEMENT");
88
            Date d = (Date) row.getObject("DATE");
89
            return getDateEcheance(idModeReglement, d);
90
        }
91
 
92
        final List<Element> eltFields = this.elt.getChildren("field");
93
 
94
        if (eltFields != null) {
95
            if (eltFields.size() > 1) {
96
                String result = "";
97
                for (Element eltField : eltFields) {
98
 
99
                    OOXMLField field = new OOXMLField(eltField, this.row, this.sqlElt, this.id);
100
 
101
                    Object value = field.getValue();
102
                    if (value != null) {
103
                        result += value.toString() + " ";
104
                    }
105
                }
106
                res = result;
107
            } else {
108
                OOXMLField field = new OOXMLField(eltFields.get(0), this.row, this.sqlElt, this.id);
109
                res = field.getValue();
110
            }
111
        }
112
        return res;
113
    }
114
 
115
    private String getInitialesUserModify() {
116
 
117
        SQLRowAccessor rowUser = this.row.getForeign("ID_USER_COMMON_MODIFY");
118
        String s = rowUser.getString("NOM");
119
        String s2 = rowUser.getString("PRENOM");
120
 
121
        StringBuffer result = new StringBuffer(4);
122
        if (s2 != null && s2.trim().length() > 0) {
123
            result.append(s2.charAt(0));
124
        }
125
        if (s != null && s.trim().length() > 0) {
126
            result.append(s.charAt(0));
127
        }
128
        return result.toString();
129
    }
130
 
131
    private String getInitialesUserCreate() {
132
        SQLRowAccessor rowUser = this.row.getForeign("ID_USER_COMMON_CREATE");
133
        String s = rowUser.getString("NOM");
134
        String s2 = rowUser.getString("PRENOM");
135
 
136
        StringBuffer result = new StringBuffer(4);
137
        if (s2 != null && s2.trim().length() > 0) {
138
            result.append(s2.charAt(0));
139
        }
140
        if (s != null && s.trim().length() > 0) {
141
            result.append(s.charAt(0));
142
        }
143
        return result.toString();
144
    }
145
 
146
    private String getInitialesUser() {
147
 
148
        String s = UserManager.getInstance().getCurrentUser().getLastName();
149
        String s2 = UserManager.getInstance().getCurrentUser().getName();
150
 
151
        StringBuffer result = new StringBuffer(4);
152
        if (s2 != null && s2.trim().length() > 0) {
153
            result.append(s2.charAt(0));
154
        }
155
        if (s != null && s.trim().length() > 0) {
156
            result.append(s.charAt(0));
157
        }
158
        return result.toString();
159
    }
160
 
161
    /**
162
     * String proposition préventec
163
     *
164
     * @param row
165
     * @return une string du format Proposition + numero + du + date
166
     */
167
    protected String getProposition(SQLRowAccessor row) {
168
        String source = row.getString("SOURCE");
169
        int idSource = row.getInt("IDSOURCE");
170
 
171
        if (source.equalsIgnoreCase("PROPOSITION") && idSource > 1) {
172
            SQLElement eltProp = Configuration.getInstance().getDirectory().getElement("PROPOSITION");
173
            SQLRow rowProp = eltProp.getTable().getRow(idSource);
174
            return getStringProposition(rowProp);
175
        }
176
        return "";
177
    }
178
 
179
    public static DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
180
 
181
    protected String getStringProposition(SQLRowAccessor rowProp) {
182
 
183
        return "Notre proposition " + rowProp.getString("NUMERO") + " du " + format.format(rowProp.getObject("DATE"));
184
    }
185
 
186
    /**
187
     * Liste des vérificateur séparée par des virgules, premiere lettre du prenom + nom
188
     *
189
     * @param rowFact
190
     * @return
191
     */
192
    protected String getListeVerificateur(SQLRowAccessor rowFact) {
193
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
194
        SQLTable tablePourcent = Configuration.getInstance().getRoot().findTable("POURCENT_SERVICE");
195
        Collection<? extends SQLRowAccessor> rows = rowFact.getReferentRows(tableElt);
196
        List<SQLRowAccessor> l = new ArrayList<SQLRowAccessor>();
197
 
198
        StringBuffer result = new StringBuffer();
199
        for (SQLRowAccessor row : rows) {
200
 
201
            Collection<? extends SQLRowAccessor> s = row.getReferentRows(tablePourcent);
202
 
203
            for (SQLRowAccessor row2 : s) {
204
                SQLRowAccessor rowVerif = row2.getForeign("ID_VERIFICATEUR");
205
 
206
                if (rowVerif != null && !l.contains(rowVerif)) {
207
                    String prenom = rowVerif.getString("PRENOM");
208
                    if (prenom.length() > 1) {
209
                        prenom = String.valueOf(prenom.charAt(0));
210
                    }
211
                    result.append(prenom + ".");
212
                    String nom = rowVerif.getString("NOM");
213
                    if (nom.length() > 1) {
214
                        nom = String.valueOf(nom.charAt(0));
215
                    }
216
 
217
                    result.append(nom + ", ");
218
                    l.add(rowVerif);
219
                }
220
            }
221
        }
222
        if (result.length() > 0) {
223
            result.deleteCharAt(result.length() - 1);
224
            result.deleteCharAt(result.length() - 1);
225
        }
226
 
227
        return result.toString();
228
    }
229
 
230
    public Double getTotalHTTable(SQLRowAccessor rowFact) {
231
 
232
        SQLTable tableElt = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE_ELEMENT");
233
        Collection<? extends SQLRowAccessor> set = rowFact.getReferentRows(tableElt);
234
        long total = 0;
235
        for (SQLRowAccessor row : set) {
236
            total += row.getLong("T_PV_HT");
237
        }
238
 
239
        return new Double(GestionDevise.currencyToString(total, false));
240
    }
241
 
242
    /**
243
     *
244
     * @param idAffaire
245
     * @return la liste des noms des missions séparés par des virgules
246
     */
247
    protected String getCodesMissions(int idAffaire) {
248
 
249
        SQLElement eltAffaire = Configuration.getInstance().getDirectory().getElement("AFFAIRE");
250
        SQLElement eltAffaireElt = Configuration.getInstance().getDirectory().getElement("AFFAIRE_ELEMENT");
251
        List<SQLRow> s = eltAffaire.getTable().getRow(idAffaire).getReferentRows(eltAffaireElt.getTable());
252
 
253
        String codes = "";
254
        List<String> l = new ArrayList<String>(s.size());
255
        for (SQLRow row : s) {
256
 
257
            final String string = row.getString("NOM");
258
            if (!l.contains(string)) {
259
                l.add(string);
260
                String code = string;
261
                codes += code + ", ";
262
            }
263
        }
264
        if (codes.trim().length() > 0) {
265
            codes = codes.trim().substring(0, codes.trim().length() - 1);
266
        }
267
 
268
        return codes;
269
 
270
    }
271
 
272
    /**
273
     * Calcul la date d'échéance d'un élément par rapport au mode de reglement et à la date
274
     * d'émission
275
     *
276
     * @param idModeRegl
277
     * @param currentDate
278
     * @return la date d'échéance au format dd/MM/yy
279
     */
280
    protected String getDateEcheance(int idModeRegl, Date currentDate) {
281
        final DateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
282
        SQLElement eltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
283
        SQLRow row = eltModeRegl.getTable().getRow(idModeRegl);
284
        int aJ = row.getInt("AJOURS");
285
        int nJ = row.getInt("LENJOUR");
286
        if (aJ + nJ == 0) {
287
            if (row.getBoolean("DATE_FACTURE")) {
288
                return Configuration.getInstance().getTranslator().getLabelFor(row.getTable().getField("DATE_FACTURE"));
289
            } else {
290
                return " ";
291
            }
292
        }
293
        String s = format2.format(ModeDeReglementSQLElement.calculDate(aJ, nJ, currentDate));
294
        System.err.println(s);
295
        return s;
296
        // return format2.format(ModeDeReglementSQLElement.calculDate(aJ, nJ, currentDate));
297
    }
298
 
299
    public boolean isTypeReplace() {
300
        // remplacement d'un pattern contenu dans la cellule
301
        return this.elt.getAttributeValue("type").equalsIgnoreCase("Replace");
302
    }
303
 
304
    public String getReplacePattern() {
305
        return this.elt.getAttributeValue("replacePattern");
306
    }
307
 
308
    public boolean isMultilineAuto() {
309
        // gestion manuel du multiligne
310
        final String multiLineValue = this.elt.getAttributeValue("controleMultiline");
311
        return (multiLineValue == null) ? true : !multiLineValue.equalsIgnoreCase("false");
312
    }
313
 
314
}