OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
93 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.product.ui;
15
 
16
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
17
import org.openconcerto.sql.model.FieldPath;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.graph.Step;
20
import org.openconcerto.sql.view.list.RowValuesTable;
21
import org.openconcerto.sql.view.list.RowValuesTableModel;
22
import org.openconcerto.utils.GestionDevise;
23
 
24
import java.awt.Component;
25
import java.math.BigDecimal;
26
import java.util.List;
27
 
28
import javax.swing.JTable;
29
import javax.swing.SwingConstants;
30
import javax.swing.table.DefaultTableCellRenderer;
31
 
32
public class CurrencyWithSymbolRenderer extends DefaultTableCellRenderer {
33
 
34
    private final FieldPath fieldPath;
35
    private final CurrencyConverter c;
36
 
174 ilm 37
    private boolean hideZeroValue = false;
38
 
93 ilm 39
    private String getSymbol(String currencyCode) {
40
        // Because Java Currency return PLN as Symbol for Zl, we use our own talbe
41
        return org.openconcerto.erp.core.finance.accounting.model.Currency.getSymbol(currencyCode);
42
    }
43
 
44
    /**
45
     * Affiche une valeur monétaire en ajoutant le symbole de la devise de la société
46
     */
47
    public CurrencyWithSymbolRenderer() {
48
        this(null);
49
    }
50
 
174 ilm 51
    public void setHideZeroValue(boolean hideZeroValue) {
52
        this.hideZeroValue = hideZeroValue;
53
    }
54
 
93 ilm 55
    /**
56
     * Affiche une valeur monétaire en ajoutant le symbole de la devise du path
57
     *
58
     * @param path chemin jusqu'au champ DEVISE.CODE
59
     */
60
    public CurrencyWithSymbolRenderer(FieldPath path) {
61
        this.fieldPath = path;
62
        this.c = new CurrencyConverter();
63
    }
64
 
65
    @Override
66
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
67
 
68
        if (value != null) {
174 ilm 69
            BigDecimal amount = (BigDecimal) value;
93 ilm 70
 
174 ilm 71
            if (this.hideZeroValue && amount.signum() == 0) {
72
                value = "";
93 ilm 73
            } else {
174 ilm 74
                value = GestionDevise.currencyToString(amount);
93 ilm 75
 
174 ilm 76
                if (fieldPath == null) {
77
                    value = value + " " + getSymbol(c.getCompanyCurrencyCode());
78
                } else {
93 ilm 79
 
174 ilm 80
                    if (table instanceof RowValuesTable) {
93 ilm 81
 
174 ilm 82
                        RowValuesTableModel model = ((RowValuesTable) table).getRowValuesTableModel();
93 ilm 83
 
174 ilm 84
                        SQLRowAccessor rowVals = model.getRowValuesAt(row);
93 ilm 85
 
174 ilm 86
                        // final SQLRow asRow = rowVals.asRow();
87
                        // SQLRow deviseRow = asRow;
88
                        // Ne pas utiliser getDistant row --> fait une requete dans la base et ne
89
                        // reprend
90
                        // pas les valeurs de la SQLRow (ex : si on veut récupérer la devise du
91
                        // fournisseur
92
                        // sélectionné, getDistantRow ira chercher la valeur du fournisseur
93
                        // référencé en
94
                        // BDD
95
                        // et non dans la SQLRow)
96
 
97
                        final List<Step> steps = this.fieldPath.getPath().getSteps();
98
                        for (int i = 0; i < steps.size(); i++) {
99
                            final Step s = steps.get(i);
100
                            // On s'assure que la row existe
101
                            if (rowVals != null && !rowVals.isUndefined()) {
102
                                // On s'assure que la row contient le champ
103
                                if (!rowVals.getFields().contains(s.getSingleField().getName())) {
104
                                    rowVals = null;
105
                                    break;
106
                                }
107
                                final SQLRowAccessor foreign = rowVals.getForeign(s.getSingleField().getName());
108
                                if (i == 0 && foreign != null) {
109
                                    rowVals = foreign.asRow();
110
                                } else {
111
                                    rowVals = foreign;
112
                                }
93 ilm 113
                            }
114
                        }
115
 
174 ilm 116
                        if (rowVals != null && !rowVals.isUndefined()) {
117
                            String code = rowVals.getString(this.fieldPath.getFieldName());
118
                            value = value + " " + getSymbol(code);
119
                        }
93 ilm 120
                    }
121
                }
122
            }
123
        }
124
        setHorizontalAlignment(SwingConstants.RIGHT);
125
 
126
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
127
    }
128
}