OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
185 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
132 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.provider;
15
 
16
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
17
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProvider;
18
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.utils.GestionDevise;
21
 
185 ilm 22
import java.text.SimpleDateFormat;
132 ilm 23
import java.util.Calendar;
24
import java.util.Collection;
25
import java.util.HashSet;
26
import java.util.Set;
27
 
28
public class RecapFactureProvider implements SpreadSheetCellValueProvider {
29
 
142 ilm 30
    private enum TypeRecapFactureProvider {
31
        HT, TTC;
32
    };
33
 
34
    private final TypeRecapFactureProvider type;
185 ilm 35
    private final boolean withDate, withAvoir;
142 ilm 36
 
185 ilm 37
    public RecapFactureProvider(TypeRecapFactureProvider t, boolean withDate, boolean withAvoir) {
142 ilm 38
        this.type = t;
185 ilm 39
        this.withDate = withDate;
40
        this.withAvoir = withAvoir;
142 ilm 41
    }
42
 
132 ilm 43
    public Object getValue(SpreadSheetCellValueContext context) {
44
        SQLRowAccessor row = context.getRow();
45
        Calendar c = row.getDate("DATE");
46
 
47
        Collection<? extends SQLRowAccessor> rows = row.getReferentRows(row.getTable().getTable("TR_COMMANDE_CLIENT"));
48
        StringBuffer result = new StringBuffer();
49
        Set<SQLRowAccessor> facture = new HashSet<SQLRowAccessor>();
50
        facture.add(row);
51
        for (SQLRowAccessor sqlRowAccessor : rows) {
52
            result.append(getPreviousAcompte(sqlRowAccessor.getForeign("ID_COMMANDE_CLIENT"), facture, c));
53
        }
54
        if (result.length() > 0) {
55
            return "Facturation précédente :" + result.toString();
56
        } else {
57
            return null;
58
        }
59
    }
60
 
61
    public static void register() {
185 ilm 62
        SpreadSheetCellValueProviderManager.put("sales.account.history", new RecapFactureProvider(TypeRecapFactureProvider.HT, false, false));
63
        SpreadSheetCellValueProviderManager.put("sales.account.history.ttc", new RecapFactureProvider(TypeRecapFactureProvider.TTC, false, false));
64
        SpreadSheetCellValueProviderManager.put("sales.account.history.withdate", new RecapFactureProvider(TypeRecapFactureProvider.HT, true, false));
65
        SpreadSheetCellValueProviderManager.put("sales.account.history.ttc.withdate", new RecapFactureProvider(TypeRecapFactureProvider.TTC, true, false));
66
        SpreadSheetCellValueProviderManager.put("sales.account.history.with.credit", new RecapFactureProvider(TypeRecapFactureProvider.HT, false, true));
67
        SpreadSheetCellValueProviderManager.put("sales.account.history.with.credit.ttc", new RecapFactureProvider(TypeRecapFactureProvider.TTC, false, true));
68
        SpreadSheetCellValueProviderManager.put("sales.account.history.with.credit.withdate", new RecapFactureProvider(TypeRecapFactureProvider.HT, true, true));
69
        SpreadSheetCellValueProviderManager.put("sales.account.history.with.credit.ttc.withdate", new RecapFactureProvider(TypeRecapFactureProvider.TTC, true, true));
132 ilm 70
    }
71
 
72
    public String getPreviousAcompte(SQLRowAccessor sqlRowAccessor, Set<SQLRowAccessor> alreadyAdded, Calendar c) {
73
        if (sqlRowAccessor == null || sqlRowAccessor.isUndefined()) {
74
            return "";
75
        }
76
        Collection<? extends SQLRowAccessor> rows = sqlRowAccessor.getReferentRows(sqlRowAccessor.getTable().getTable("TR_COMMANDE_CLIENT"));
77
        StringBuffer result = new StringBuffer();
185 ilm 78
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
132 ilm 79
        for (SQLRowAccessor sqlRowAccessor2 : rows) {
80
            SQLRowAccessor rowFact = sqlRowAccessor2.getForeign("ID_SAISIE_VENTE_FACTURE");
81
 
185 ilm 82
            if (rowFact != null && !rowFact.isUndefined() && !alreadyAdded.contains(rowFact) && rowFact.getDate("DATE").before(c)
83
                    && (!this.withAvoir || (this.withAvoir && rowFact.isForeignEmpty("ID_AVOIR_CLIENT")))) {
132 ilm 84
                alreadyAdded.add(rowFact);
142 ilm 85
                final String fieldTotal = this.type == TypeRecapFactureProvider.HT ? "T_HT" : "T_TTC";
185 ilm 86
                result.append(rowFact.getString("NUMERO"));
87
                result.append(" (");
88
                if (this.withDate) {
89
                    result.append(format.format(rowFact.getDate("DATE").getTime()) + " ");
90
                }
91
                result.append(GestionDevise.currencyToString(rowFact.getLong(fieldTotal)) + "€), ");
92
                if (this.withDate) {
93
                    result.append("\n");
94
                }
132 ilm 95
            }
96
        }
97
 
98
        return result.toString();
99
    }
100
 
101
}