OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | 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.product.ui;
15
 
73 ilm 16
import org.openconcerto.erp.core.common.ui.DeviseNumericHTConvertorCellEditor;
19 ilm 17
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18
import org.openconcerto.erp.core.sales.product.component.ReferenceArticleSQLComponent;
19
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
18 ilm 20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.element.SQLElement;
73 ilm 22
import org.openconcerto.sql.model.SQLField;
80 ilm 23
import org.openconcerto.sql.model.SQLRow;
19 ilm 24
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 25
import org.openconcerto.sql.model.SQLRowValues;
19 ilm 26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.view.list.CellDynamicModifier;
18 ilm 28
import org.openconcerto.sql.view.list.RowValuesTable;
29
import org.openconcerto.sql.view.list.RowValuesTableModel;
19 ilm 30
import org.openconcerto.sql.view.list.RowValuesTablePanel;
18 ilm 31
import org.openconcerto.sql.view.list.SQLTableElement;
156 ilm 32
import org.openconcerto.ui.table.NumberCellRenderer;
90 ilm 33
import org.openconcerto.utils.DecimalUtils;
18 ilm 34
 
67 ilm 35
import java.math.BigDecimal;
18 ilm 36
import java.util.List;
37
import java.util.Vector;
38
 
19 ilm 39
public class ArticleTarifTable extends RowValuesTablePanel {
40
 
18 ilm 41
    private SQLTableElement tarif;
19 ilm 42
    private SQLTable article = Configuration.getInstance().getBase().getTable("ARTICLE");
18 ilm 43
 
19 ilm 44
    private SQLRowValues rowValuesArticleCompile = new SQLRowValues(article);
45
    SQLTableElement tableElement_PrixMetrique1_VenteHT;
46
    ReferenceArticleSQLComponent comp;
47
 
48
    public ArticleTarifTable(ReferenceArticleSQLComponent comp) {
49
 
18 ilm 50
        init();
51
        uiInit();
19 ilm 52
        this.comp = comp;
18 ilm 53
    }
54
 
19 ilm 55
    public void setArticleValues(SQLRowAccessor articleAccessor) {
56
        rowValuesArticleCompile.put("VALEUR_METRIQUE_1", articleAccessor.getObject("VALEUR_METRIQUE_1"));
57
        rowValuesArticleCompile.put("VALEUR_METRIQUE_2", articleAccessor.getObject("VALEUR_METRIQUE_2"));
58
        rowValuesArticleCompile.put("VALEUR_METRIQUE_3", articleAccessor.getObject("VALEUR_METRIQUE_2"));
59
        rowValuesArticleCompile.put("PRIX_METRIQUE_VT_1", articleAccessor.getObject("PRIX_METRIQUE_VT_1"));
60
        rowValuesArticleCompile.put("PRIX_METRIQUE_VT_2", articleAccessor.getObject("PRIX_METRIQUE_VT_2"));
61
        rowValuesArticleCompile.put("PRIX_METRIQUE_VT_3", articleAccessor.getObject("PRIX_METRIQUE_VT_3"));
62
        rowValuesArticleCompile.put("ID_MODE_VENTE_ARTICLE", articleAccessor.getObject("ID_MODE_VENTE_ARTICLE"));
63
        rowValuesArticleCompile.put("ID_TAXE", articleAccessor.getObject("ID_TAXE"));
64
    }
65
 
66
    public void fireModification() {
67
 
68
        rowValuesArticleCompile.putAll(comp.getDetailsRowValues().getAbsolutelyAll());
69
        rowValuesArticleCompile.put("ID_TAXE", comp.getSelectedTaxe());
70
 
71
        int rows = getRowValuesTable().getRowCount();
72
        for (int i = 0; i < rows; i++) {
73
            SQLRowValues rowVals = getRowValuesTable().getRowValuesTableModel().getRowValuesAt(i);
74
            rowValuesArticleCompile.put("PRIX_METRIQUE_VT_1", rowVals.getObject("PRIX_METRIQUE_VT_1"));
75
            this.tableElement_PrixMetrique1_VenteHT.fireModification(rowVals);
76
        }
77
    }
78
 
18 ilm 79
    /**
80
     *
81
     */
82
    protected void init() {
83
 
84
        final SQLElement e = getSQLElement();
85
 
86
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
87
 
88
        this.tarif = new SQLTableElement(e.getTable().getField("ID_TARIF"));
89
        this.tarif.setEditable(false);
90
        list.add(this.tarif);
91
 
94 ilm 92
        list.add(new SQLTableElement(e.getTable().getField("QTE")));
93
 
19 ilm 94
        // Prix de vente HT de la métrique 1
73 ilm 95
        final SQLField field = e.getTable().getField("PRIX_METRIQUE_VT_1");
96
        final DeviseNumericHTConvertorCellEditor editorPVHT = new DeviseNumericHTConvertorCellEditor(field);
97
        this.tableElement_PrixMetrique1_VenteHT = new SQLTableElement(field, BigDecimal.class);
19 ilm 98
        list.add(tableElement_PrixMetrique1_VenteHT);
18 ilm 99
 
19 ilm 100
        // Devise
101
        final SQLTableElement tableElement_Devise = new SQLTableElement(e.getTable().getField("ID_DEVISE"));
102
        tableElement_Devise.setEditable(false);
103
        list.add(tableElement_Devise);
18 ilm 104
 
19 ilm 105
        // TVA
106
        final SQLTableElement tableElement_TVA = new SQLTableElement(e.getTable().getField("ID_TAXE"));
107
        list.add(tableElement_TVA);
108
 
109
        // Prix de vente unitaire HT
67 ilm 110
        final SQLTableElement tableElement_PrixVente_HT = new SQLTableElement(e.getTable().getField("PV_HT"), BigDecimal.class);
111
 
19 ilm 112
        tableElement_PrixVente_HT.setEditable(false);
113
        list.add(tableElement_PrixVente_HT);
114
 
115
        // Prix de vente unitaire TTC
67 ilm 116
        final SQLTableElement tableElement_PrixVente_TTC = new SQLTableElement(e.getTable().getField("PV_TTC"), BigDecimal.class);
117
 
19 ilm 118
        tableElement_PrixVente_TTC.setEditable(false);
119
        list.add(tableElement_PrixVente_TTC);
120
 
121
        this.defaultRowVals = new SQLRowValues(getSQLElement().getTable());
67 ilm 122
        this.defaultRowVals.put("PRIX_METRIQUE_VT_1", BigDecimal.ZERO);
123
        this.defaultRowVals.put("PV_HT", BigDecimal.ZERO);
124
        this.defaultRowVals.put("PV_TTC", BigDecimal.ZERO);
156 ilm 125
        if (e.getTable().contains("POURCENT_REMISE")) {
126
            final SQLTableElement tableElementDiscount = new SQLTableElement(e.getTable().getField("POURCENT_REMISE"), BigDecimal.class) {
127
                @Override
128
                protected Object getDefaultNullValue() {
129
                    return BigDecimal.ZERO;
130
                }
131
            };
132
            tableElementDiscount.setRenderer(new NumberCellRenderer());
133
            list.add(tableElementDiscount);
134
        }
19 ilm 135
        this.model = new RowValuesTableModel(e, list, e.getTable().getField("ID_TARIF"), false, this.defaultRowVals);
136
 
18 ilm 137
        this.table = new RowValuesTable(this.model, null);
138
 
19 ilm 139
        // Calcul automatique du prix de vente unitaire HT
18 ilm 140
 
19 ilm 141
        tableElement_PrixMetrique1_VenteHT.addModificationListener(tableElement_PrixVente_HT);
142
        tableElement_PrixVente_HT.setModifier(new CellDynamicModifier() {
93 ilm 143
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
19 ilm 144
                rowValuesArticleCompile.putAll(comp.getDetailsRowValues().getAbsolutelyAll());
145
                rowValuesArticleCompile.put("PRIX_METRIQUE_VT_1", row.getObject("PRIX_METRIQUE_VT_1"));
146
                Number n = (Number) rowValuesArticleCompile.getObject("ID_MODE_VENTE_ARTICLE");
147
                if (n.intValue() == ReferenceArticleSQLElement.A_LA_PIECE || n.intValue() <= 1) {
67 ilm 148
                    return row.getObject("PRIX_METRIQUE_VT_1");
19 ilm 149
                } else {
67 ilm 150
                    final BigDecimal prixVTFromDetails = ReferenceArticleSQLElement.getPrixVTFromDetails(rowValuesArticleCompile);
151
                    return prixVTFromDetails;
19 ilm 152
                }
153
            }
154
        });
155
        // Calcul automatique du prix de vente unitaire TTC
18 ilm 156
 
19 ilm 157
        tableElement_PrixVente_HT.addModificationListener(tableElement_PrixVente_TTC);
158
        tableElement_PrixVente_TTC.setModifier(new CellDynamicModifier() {
159
            @Override
93 ilm 160
            public Object computeValueFrom(SQLRowValues row, SQLTableElement source) {
18 ilm 161
 
19 ilm 162
                rowValuesArticleCompile.putAll(comp.getDetailsRowValues().getAbsolutelyAll());
163
                rowValuesArticleCompile.put("PRIX_METRIQUE_VT_1", row.getObject("PRIX_METRIQUE_VT_1"));
18 ilm 164
 
67 ilm 165
                BigDecimal pHT = (BigDecimal) row.getObject("PV_HT");
19 ilm 166
                Object object = row.getObject("ID_TAXE");
18 ilm 167
 
19 ilm 168
                int idTaux = 1;
169
                if (object != null) {
170
                    idTaux = Integer.parseInt(object.toString());
171
                }
172
                Float resultTaux = TaxeCache.getCache().getTauxFromId(idTaux);
18 ilm 173
 
19 ilm 174
                if (resultTaux == null) {
80 ilm 175
                    final SQLRow i = TaxeCache.getCache().getFirstTaxe();
176
                    rowValuesArticleCompile.put("ID_TAXE", i.getID());
177
                    resultTaux = i.getFloat("TAUX");
19 ilm 178
                }
18 ilm 179
 
19 ilm 180
                float taux = (resultTaux == null) ? 0.0F : resultTaux.floatValue();
73 ilm 181
                editorPVHT.setTaxe(taux);
90 ilm 182
                BigDecimal r = pHT.multiply(BigDecimal.valueOf(taux).divide(BigDecimal.valueOf(100)).add(BigDecimal.ONE), DecimalUtils.HIGH_PRECISION);
73 ilm 183
                return r.setScale(tableElement_PrixVente_TTC.getDecimalDigits(), BigDecimal.ROUND_HALF_UP);
184
 
19 ilm 185
            }
186
 
187
        });
188
 
18 ilm 189
    }
190
 
19 ilm 191
    public SQLElement getSQLElement() {
192
        return Configuration.getInstance().getDirectory().getElement("ARTICLE_TARIF");
18 ilm 193
    }
194
 
195
}