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.sales.invoice.report;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
18
import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
19
import org.openconcerto.erp.generationDoc.SheetXml;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.element.SQLElement;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowListRSH;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.Where;
27
import org.openconcerto.utils.Tuple2;
28
 
25 ilm 29
import java.io.File;
18 ilm 30
import java.text.DateFormat;
31
import java.text.SimpleDateFormat;
32
import java.util.ArrayList;
33
import java.util.Calendar;
34
import java.util.Date;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38
 
19 ilm 39
import javax.swing.JProgressBar;
40
import javax.swing.SwingUtilities;
41
 
18 ilm 42
public class ListeVenteXmlSheet extends AbstractListeSheetXml {
43
 
44
    private static final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
45
 
25 ilm 46
    public static final String TEMPLATE_ID = "ListeVentes";
47
    public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;
48
 
18 ilm 49
    private Date du, au;
50
    private List<SQLRow> listeIds;
25 ilm 51
    private JProgressBar bar;
52
    private SQLElement eltAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_CLIENT");
53
    private SQLElement eltEnc = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT");
54
    private SQLElement eltEncElt = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT_ELEMENT");
18 ilm 55
 
19 ilm 56
    public ListeVenteXmlSheet(List<SQLRow> listeIds, Date du, Date au, JProgressBar bar) {
18 ilm 57
        this.printer = PrinterNXProps.getInstance().getStringProperty("BonPrinter");
58
        this.listeIds = listeIds;
59
        this.du = du;
60
        this.au = au;
19 ilm 61
        this.bar = bar;
18 ilm 62
    }
63
 
25 ilm 64
    @Override
65
    public String getDefaultTemplateId() {
66
        return TEMPLATE_ID;
21 ilm 67
    };
68
 
25 ilm 69
    @Override
70
    public String getName() {
71
        return "JournalVentes";
72
    }
18 ilm 73
 
74
    protected void createListeValues() {
75
 
76
        if (this.listeIds == null) {
77
            return;
78
        }
19 ilm 79
 
80
        SwingUtilities.invokeLater(new Runnable() {
81
            public void run() {
82
                ListeVenteXmlSheet.this.bar.setMaximum(ListeVenteXmlSheet.this.listeIds.size());
83
            }
84
        });
18 ilm 85
        List<Map<String, Object>> listValues = new ArrayList<Map<String, Object>>(this.listeIds.size());
19 ilm 86
        int i = 1;
18 ilm 87
        for (SQLRow rowFacture : this.listeIds) {
88
            Map<String, Object> mValues = new HashMap<String, Object>();
89
            final String dateFacture = dateFormat.format((Date) rowFacture.getObject("DATE"));
90
            mValues.put("DATE", dateFacture);
91
            mValues.put("NUMERO", rowFacture.getObject("NUMERO"));
92
            if (rowFacture.getTable().getName().equalsIgnoreCase(this.eltAvoir.getTable().getName())) {
93
                mValues.put("MONTANT_HT", new Double(-((Number) rowFacture.getObject("MONTANT_HT")).longValue() / 100.0));
94
                mValues.put("MONTANT_TVA", new Double(-((Number) rowFacture.getObject("MONTANT_TVA")).longValue() / 100.0));
95
                mValues.put("MONTANT_TTC", new Double(-((Number) rowFacture.getObject("MONTANT_TTC")).longValue() / 100.0));
96
            } else {
97
                mValues.put("MONTANT_HT", new Double(((Number) rowFacture.getObject("T_HT")).longValue() / 100.0));
98
                mValues.put("MONTANT_TVA", new Double(((Number) rowFacture.getObject("T_TVA")).longValue() / 100.0));
99
                mValues.put("MONTANT_TTC", new Double(((Number) rowFacture.getObject("T_TTC")).longValue() / 100.0));
100
            }
101
 
102
            // Client
103
            SQLRow rowCli;
104
                rowCli = rowFacture.getForeignRow("ID_CLIENT");
105
            String libClient = rowCli.getString("FORME_JURIDIQUE") + " " + rowCli.getString("NOM");
106
            mValues.put("CLIENT", libClient.trim());
107
 
108
            // Mode de reglement
109
            SQLRow rowMode = rowFacture.getForeignRow("ID_MODE_REGLEMENT");
110
            final int typeReglement = rowMode.getInt("ID_TYPE_REGLEMENT");
111
            if (rowMode.getBoolean("COMPTANT") && typeReglement <= TypeReglementSQLElement.TRAITE) {
112
 
113
                final SQLRow foreignRow = rowMode.getForeignRow("ID_TYPE_REGLEMENT");
114
                Date d = (Date) rowFacture.getObject("DATE");
115
                if (foreignRow.getID() == TypeReglementSQLElement.TRAITE) {
116
                    Calendar c = (rowMode.getDate("DATE_VIREMENT"));
117
                    if (c != null) {
118
                        d = c.getTime();
119
                    }
120
                } else if (foreignRow.getID() == TypeReglementSQLElement.CHEQUE) {
121
                    Calendar c = (rowMode.getDate("DATE"));
122
                    if (c != null) {
123
                        d = c.getTime();
124
                    }
125
                }
126
 
127
                mValues.put("DATE_REGLEMENT", dateFormat.format(d));
128
                mValues.put("TYPE_REGLEMENT", foreignRow.getString("NOM"));
129
            } else {
130
 
131
                SQLRow rowMvt = rowFacture.getForeignRow("ID_MOUVEMENT");
132
                SQLRow rowPiece = rowMvt.getForeignRow("ID_PIECE");
133
 
134
                SQLSelect sel = new SQLSelect(rowFacture.getTable().getBase());
135
 
136
                sel.addSelect(this.eltEnc.getTable().getKey());
137
                sel.addSelect(this.eltEnc.getTable().getField("ID_MODE_REGLEMENT"));
138
 
139
                Where w = new Where(rowMvt.getTable().getField("ID_PIECE"), "=", rowPiece.getID());
140
                w = w.and(new Where(rowMvt.getTable().getKey(), "=", this.eltEncElt.getTable().getField("ID_MOUVEMENT_ECHEANCE")));
141
                w = w.and(new Where(this.eltEncElt.getTable().getField("ID_ENCAISSER_MONTANT"), "=", this.eltEnc.getTable().getKey()));
142
 
143
                sel.setWhere(w);
144
 
145
                List<SQLRow> l = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, eltEnc.getTable()));
146
                for (SQLRow sqlRow : l) {
147
                    final SQLRow foreignRow = sqlRow.getForeignRow("ID_MODE_REGLEMENT");
148
                    SQLRow rowTypeRegl = foreignRow.getForeignRow("ID_TYPE_REGLEMENT");
149
                    Calendar cDate = foreignRow.getDate("DATE");
150
                    Calendar cDateVirement = foreignRow.getDate("DATE_VIREMENT");
151
                    if (cDate != null) {
152
                        mValues.put("DATE_REGLEMENT", dateFormat.format(cDate.getTime()));
153
                    } else if (cDateVirement != null) {
154
                        mValues.put("DATE_REGLEMENT", dateFormat.format(cDateVirement.getTime()));
155
                    } else {
156
                        mValues.put("DATE_REGLEMENT", dateFormat.format(sqlRow.getDate("DATE").getTime()));
157
                    }
158
 
159
                    mValues.put("TYPE_REGLEMENT", rowTypeRegl.getString("NOM"));
160
                }
161
            }
162
 
163
            listValues.add(mValues);
19 ilm 164
            final int value = i++;
165
            SwingUtilities.invokeLater(new Runnable() {
166
                public void run() {
167
                    ListeVenteXmlSheet.this.bar.setValue(value);
168
                }
169
            });
18 ilm 170
        }
171
        final Map<String, Object> values = new HashMap<String, Object>();
172
        values.put("DATE", "Du " + dateFormat.format(this.du) + " au " + dateFormat.format(this.au));
173
 
174
        this.listAllSheetValues.put(0, listValues);
175
        this.mapAllSheetValues.put(0, values);
176
    }
177
 
178
}