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
80 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.account;
15
 
16
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
177 ilm 17
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable.TypeCalcul;
80 ilm 18
import org.openconcerto.erp.core.common.ui.Acompte;
19
import org.openconcerto.erp.core.common.ui.AcompteField;
20
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
21
import org.openconcerto.erp.core.sales.invoice.ui.FactureSituationItemTable;
177 ilm 22
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
23
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
24
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
25
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
83 ilm 26
import org.openconcerto.sql.element.GlobalMapper;
80 ilm 27
import org.openconcerto.sql.element.SQLElement;
28
import org.openconcerto.sql.model.SQLRow;
29
import org.openconcerto.sql.model.SQLRowAccessor;
30
import org.openconcerto.sql.model.SQLRowValues;
177 ilm 31
import org.openconcerto.sql.preferences.SQLPreferences;
83 ilm 32
import org.openconcerto.ui.group.Group;
142 ilm 33
import org.openconcerto.utils.DecimalUtils;
177 ilm 34
import org.openconcerto.utils.ExceptionHandler;
80 ilm 35
import org.openconcerto.utils.Tuple2;
36
 
37
import java.math.BigDecimal;
142 ilm 38
import java.math.RoundingMode;
177 ilm 39
import java.sql.SQLException;
80 ilm 40
import java.util.Collection;
41
import java.util.Date;
42
import java.util.HashSet;
43
import java.util.List;
44
import java.util.Set;
45
 
46
import javax.swing.JCheckBox;
47
 
48
public class VenteFactureSoldeSQLComponent extends VenteFactureSituationSQLComponent {
49
    public static final String ID = "sales.invoice.partial.balance";
50
 
51
    public VenteFactureSoldeSQLComponent(SQLElement element) {
83 ilm 52
        super(element, (Group) GlobalMapper.getInstance().get(ID));
80 ilm 53
    }
54
 
55
    @Override
56
    protected void addViews() {
57
 
58
        super.addViews();
59
        getEditor("sales.invoice.partial.amount").setEnabled(false);
60
        JCheckBox box = new JCheckBox("Solde");
61
        box.setSelected(true);
62
        this.addView(box, "SOLDE");
63
 
64
    }
65
 
66
    @Override
67
    protected SQLRowValues createDefaults() {
68
        SQLRowValues rowVals = new SQLRowValues(getTable());
69
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, new Date()));
70
        rowVals.put("SOLDE", Boolean.TRUE);
71
        return rowVals;
72
    }
73
 
74
    @Override
75
    public int insert(SQLRow order) {
177 ilm 76
        int id = super.insert(order);
77
        try {
78
            updateStock(id);
79
        } catch (SQLException e) {
80
            ExceptionHandler.handle("Erreur lors de la mise à jour du stock.", e);
81
            e.printStackTrace();
82
        }
83
        return id;
80 ilm 84
    }
85
 
86
    @Override
177 ilm 87
    public void update() {
88
        super.update();
89
        try {
90
            updateStock(getSelectedID());
91
        } catch (SQLException e) {
92
            ExceptionHandler.handle("Erreur lors de la mise à jour du stock.", e);
93
            e.printStackTrace();
94
        }
95
    }
96
 
97
    @Override
80 ilm 98
    public void importFrom(List<SQLRowValues> rows) {
99
 
100
        super.importFrom(rows);
101
        AcompteField field = (AcompteField) getEditor("sales.invoice.partial.amount");
102
 
103
        Tuple2<Long, Long> t = getTotalFacture(rows);
142 ilm 104
        // final Acompte a = new Acompte(null, new BigDecimal(t.get0() -
105
        // t.get1()).movePointLeft(2));
106
        // field.setValue(a);
107
        final BigDecimal percent = BigDecimal.ONE.subtract(new BigDecimal(t.get1()).divide(new BigDecimal(t.get0()), DecimalUtils.HIGH_PRECISION).setScale(6, RoundingMode.HALF_UP));
108
        final Acompte a = new Acompte(percent.movePointRight(2), null);
80 ilm 109
        field.setValue(a);
110
        final FactureSituationItemTable table = ((FactureSituationItemTable) getEditor("sales.invoice.partial.items.list"));
83 ilm 111
        table.calculPourcentage(a, TypeCalcul.CALCUL_FACTURABLE);
80 ilm 112
    }
113
 
114
    public Tuple2<Long, Long> getTotalFacture(List<SQLRowValues> context) {
115
 
116
        long totalFacture = 0;
117
        long totalCommande = 0;
118
        Set<SQLRowAccessor> facture = new HashSet<SQLRowAccessor>();
119
        for (SQLRowAccessor sqlRowAccessor : context) {
120
            totalFacture += getFacture(sqlRowAccessor, facture);
121
            totalCommande += sqlRowAccessor.getLong("T_HT");
122
        }
123
 
124
        return Tuple2.create(totalCommande, totalFacture);
125
    }
126
 
127
    public long getFacture(SQLRowAccessor sqlRowAccessor, Set<SQLRowAccessor> alreadyAdded) {
128
        Collection<? extends SQLRowAccessor> rows = sqlRowAccessor.getReferentRows(sqlRowAccessor.getTable().getTable("TR_COMMANDE_CLIENT"));
129
        long l = 0;
130
        for (SQLRowAccessor sqlRowAccessor2 : rows) {
131
            if (!sqlRowAccessor2.isForeignEmpty("ID_SAISIE_VENTE_FACTURE")) {
132
                SQLRowAccessor rowFacture = sqlRowAccessor2.getForeign("ID_SAISIE_VENTE_FACTURE");
133
                if (!alreadyAdded.contains(rowFacture)) {
134
                    alreadyAdded.add(rowFacture);
135
                    l += rowFacture.getLong("T_HT");
136
                }
137
            }
138
        }
139
        return l;
140
    }
141
 
177 ilm 142
    protected String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
143
        return "Saisie vente facture N°" + row.getString("NUMERO");
144
    }
145
 
146
    /**
147
     * Mise à jour des stocks pour chaque article composant la facture
148
     *
149
     * @throws SQLException
150
     */
151
    private void updateStock(int id) throws SQLException {
152
 
153
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
154
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {
155
            SQLRow row = getTable().getRow(id);
156
            StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
157
 
158
                @Override
159
                public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
160
                    return getLibelleStock(rowOrigin, rowElt);
161
                }
162
            }, row, row.getReferentRows(getTable().getTable("SAISIE_VENTE_FACTURE_ELEMENT")),
163
                    getTable().contains("CREATE_VIRTUAL_STOCK") && row.getBoolean("CREATE_VIRTUAL_STOCK") ? TypeStockUpdate.REAL_VIRTUAL_DELIVER : TypeStockUpdate.REAL_DELIVER);
164
 
165
            stockUpdater.update();
166
 
167
        }
168
    }
169
 
83 ilm 170
    // @Override
171
    // public Component addView(MutableRowItemView rowItemView, String fields, Object specObj) {
172
    //
173
    // if (fields.contains("ID_POLE_PRODUIT") && countPole == 0) {
174
    // countPole++;
175
    // return null;
176
    // } else {
177
    // return super.addView(rowItemView, fields, specObj);
178
    // }
179
    // }
180
 
80 ilm 181
}