OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 67 | 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.core.finance.accounting.report;
15
 
16
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
17
import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
18
import org.openconcerto.erp.preferences.PrinterNXProps;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowListRSH;
23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.Where;
25
 
26
import java.util.ArrayList;
27
import java.util.Calendar;
28
import java.util.Date;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32
 
33
public class BalanceAgeeListeSheetXML extends AbstractListeSheetXml {
34
 
35
    private Date deb, fin;
25 ilm 36
    public static String TEMPLATE_ID = "Balance agée";
18 ilm 37
 
38
    public BalanceAgeeListeSheetXML(Date deb, Date fin) {
39
        this.printer = PrinterNXProps.getInstance().getStringProperty("BonPrinter");
40
        this.deb = deb;
41
        this.fin = fin;
42
    }
43
 
21 ilm 44
    @Override
25 ilm 45
    public String getDefaultTemplateId() {
21 ilm 46
        return "BalanceAgee";
47
    }
48
 
25 ilm 49
    @Override
50
    public String getName() {
51
        return "BalanceAgee" + new Date().getTime();
18 ilm 52
    }
53
 
54
    protected void createListeValues() {
55
        SQLElement ecr = Configuration.getInstance().getDirectory().getElement("ECRITURE");
56
        SQLElement cpt = Configuration.getInstance().getDirectory().getElement("COMPTE_PCE");
57
        SQLElement fact = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
58
 
59
        SQLSelect sel = new SQLSelect(ecr.getTable().getBase());
60
        sel.addSelectStar(ecr.getTable());
61
        Where w = new Where(cpt.getTable().getField("NUMERO"), "LIKE", "411%");
62
        if (this.deb != null) {
63
            w = w.and(new Where(ecr.getTable().getField("DATE"), ">=", this.deb));
64
        }
65
        if (this.fin != null) {
66
            w = w.and(new Where(ecr.getTable().getField("DATE"), "<=", this.fin));
67
        }
68
        // w = w.and(new Where(cpt.getTable().getField("NOM"), "LIKE", "%RIBEIRO%"));
69
        w = w.and(new Where(ecr.getTable().getField("ID_COMPTE_PCE"), "=", cpt.getTable().getKey()));
70
        w = w.and(new Where(ecr.getTable().getField("LETTRAGE"), "=", "").or(new Where(ecr.getTable().getField("LETTRAGE"), "=", (Object) null)));
71
 
72
        sel.setWhere(w);
73
        sel.addFieldOrder(ecr.getTable().getField("COMPTE_NUMERO"));
74
 
75
        System.err.println(sel.asString());
76
        List<Map<String, Object>> valuesTab = new ArrayList<Map<String, Object>>();
77
 
78
        List<SQLRow> l = (List<SQLRow>) ecr.getTable().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
79
 
80
        Calendar c = Calendar.getInstance();
81
        Map<String, Map<String, Object>> vals = new HashMap<String, Map<String, Object>>();
82
 
83
        long total30 = 0;
84
        long total60 = 0;
85
        long total90 = 0;
86
        long totalPlus = 0;
87
        long totalFull = 0;
88
 
89
        for (SQLRow sqlRow : l) {
90
            long date = sqlRow.getDate("DATE").getTimeInMillis();
91
            SQLRow rowMvt = sqlRow.getForeignRow("ID_MOUVEMENT");
92
            if (rowMvt.getString("SOURCE").equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
93
                SQLRow rowFact = fact.getTable().getRow(rowMvt.getInt("IDSOURCE"));
94
                date = ModeDeReglementSQLElement.calculDate(rowFact.getForeignRow("ID_MODE_REGLEMENT"), rowFact.getDate("DATE").getTime()).getTime();
95
            }
96
 
97
            long time = c.getTimeInMillis() - date;
98
            long day = time / 86400000;
99
            if (day < 0) {
100
                continue;
101
            }
102
            final SQLRow rowCpt = sqlRow.getForeignRow("ID_COMPTE_PCE");
103
            String num = rowCpt.getString("NUMERO");
104
            Map<String, Object> m;
105
            if (vals.get(num) == null) {
106
                m = new HashMap<String, Object>();
107
                vals.put(num, m);
108
            } else {
109
                m = vals.get(num);
110
            }
111
 
112
            final long value = sqlRow.getLong("DEBIT") - sqlRow.getLong("CREDIT");
113
            totalFull += value;
114
            String key = "+90";
115
            if (day <= 30) {
116
                key = "30";
117
                total30 += value;
118
            } else if (day <= 60) {
119
                key = "60";
120
                total60 += value;
121
            } else if (day <= 90) {
122
                key = "90";
123
                total90 += value;
124
            } else {
125
                totalPlus += value;
126
            }
127
 
128
            m.put("NUMERO", num);
129
            m.put("NOM", rowCpt.getString("NOM"));
130
 
131
            if (m.get(key) == null) {
132
                m.put(key, value);
133
            } else {
134
                long cred = (Long) m.get(key);
135
                m.put(key, cred + value);
136
            }
137
 
138
            long total = 0;
139
            if (m.get("TOTAL") != null) {
140
                total = (Long) m.get("TOTAL");
141
            }
142
            m.put("TOTAL", total + value);
143
 
144
        }
145
 
146
        for (String k : vals.keySet()) {
147
            final Map<String, Object> e = vals.get(k);
148
            Long l1 = (Long) e.get("30");
149
            Long l2 = (Long) e.get("60");
150
            Long l3 = (Long) e.get("90");
151
            Long l4 = (Long) e.get("+90");
152
            Long l5 = (Long) e.get("TOTAL");
153
 
154
            if ((l1 != null && l1 != 0) || (l2 != null && l2 != 0) || (l3 != null && l3 != 0) || (l4 != null && l4 != 0)) {
155
                if (l1 != null && l1 != 0) {
156
                    e.put("30", l1 / 100.0);
157
                }
158
 
159
                if (l2 != null && l2 != 0) {
160
                    e.put("60", l2 / 100.0);
161
                }
162
 
163
                if (l3 != null && l3 != 0) {
164
                    e.put("90", l3 / 100.0);
165
                }
166
 
167
                if (l4 != null && l4 != 0) {
168
                    e.put("+90", l4 / 100.0);
169
                }
170
 
171
                if (l5 != null && l5 != 0) {
172
                    e.put("TOTAL", l5 / 100.0);
173
                }
174
 
175
                valuesTab.add(e);
176
            }
177
        }
25 ilm 178
        final Map<String, Object> totalMap = new HashMap<String, Object>();
18 ilm 179
        totalMap.put("NOM", "TOTAL");
180
        totalMap.put("30", total30 / 100.0);
181
        totalMap.put("60", total60 / 100.0);
182
        totalMap.put("90", total90 / 100.0);
183
        totalMap.put("+90", totalPlus / 100.0);
184
        totalMap.put("TOTAL", totalFull / 100.0);
185
        valuesTab.add(totalMap);
186
 
187
        this.listAllSheetValues.put(0, valuesTab);
188
 
189
    }
190
}