OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 90 | 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.order.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.sales.order.component.CommandeClientSQLComponent;
19 ilm 18
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
19
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
18 ilm 20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.element.SQLComponent;
22
import org.openconcerto.sql.element.SQLElement;
19 ilm 23
import org.openconcerto.sql.model.SQLField;
24
import org.openconcerto.sql.model.SQLInjector;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowValues;
83 ilm 27
import org.openconcerto.sql.model.SQLSelect;
19 ilm 28
import org.openconcerto.sql.model.SQLTable;
83 ilm 29
import org.openconcerto.sql.model.Where;
67 ilm 30
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
83 ilm 31
import org.openconcerto.utils.ListMap;
18 ilm 32
 
67 ilm 33
import java.math.BigDecimal;
34
import java.math.MathContext;
83 ilm 35
import java.sql.SQLException;
18 ilm 36
import java.util.ArrayList;
37
import java.util.HashSet;
38
import java.util.List;
39
import java.util.Set;
40
 
83 ilm 41
import org.apache.commons.dbutils.handlers.ArrayListHandler;
42
 
18 ilm 43
public class CommandeClientSQLElement extends ComptaSQLConfElement {
44
 
45
    public CommandeClientSQLElement() {
46
        super("COMMANDE_CLIENT", "une commande client", "commandes clients");
47
    }
48
 
49
    /*
50
     * (non-Javadoc)
51
     *
52
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
53
     */
54
    protected List<String> getComboFields() {
73 ilm 55
        final List<String> l = new ArrayList<String>();
18 ilm 56
        l.add("NUMERO");
57
        return l;
58
    }
59
 
60
    /*
61
     * (non-Javadoc)
62
     *
63
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
64
     */
65
    protected List<String> getListFields() {
73 ilm 66
        final List<String> l = new ArrayList<String>();
18 ilm 67
        l.add("NUMERO");
68
        l.add("DATE");
69
        l.add("ID_CLIENT");
41 ilm 70
        l.add("ID_COMMERCIAL");
18 ilm 71
        l.add("T_HT");
72
        l.add("T_TTC");
73
        l.add("NOM");
74
        l.add("INFOS");
75
        return l;
76
    }
77
 
78
    @Override
79
    protected Set<String> getChildren() {
73 ilm 80
        final Set<String> set = new HashSet<String>();
18 ilm 81
        set.add("COMMANDE_CLIENT_ELEMENT");
82
        return set;
83
    }
84
 
28 ilm 85
    @Override
86
    public Set<String> getReadOnlyFields() {
73 ilm 87
        final Set<String> s = new HashSet<String>();
28 ilm 88
        s.add("ID_DEVIS");
89
        return s;
90
    }
91
 
67 ilm 92
    @Override
83 ilm 93
    protected void archive(SQLRow row, boolean cutLinks) throws SQLException {
94
        super.archive(row, cutLinks);
95
        // Mise à jour des stocks
96
        SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
97
        SQLSelect sel = new SQLSelect();
98
        sel.addSelect(eltMvtStock.getTable().getField("ID"));
99
        Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
100
        Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
101
        sel.setWhere(w.and(w2));
102
 
103
        @SuppressWarnings("rawtypes")
104
        List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
105
        if (l != null) {
106
            for (int i = 0; i < l.size(); i++) {
107
                Object[] tmp = (Object[]) l.get(i);
108
                eltMvtStock.archive(((Number) tmp[0]).intValue());
109
            }
110
        }
111
    }
112
 
113
    @Override
67 ilm 114
    protected SQLTableModelSourceOnline createTableSource() {
73 ilm 115
        final SQLTableModelSourceOnline source = super.createTableSource();
116
        // TODO: refaire un renderer pour les commandes transférées en BL
117
        // final CommandeClientRenderer rend = CommandeClientRenderer.getInstance();
118
        // final SQLTableModelColumn col = source.getColumn(getTable().getField("T_HT"));
119
        // col.setColumnInstaller(new IClosure<TableColumn>() {
120
        // @Override
121
        // public void executeChecked(TableColumn input) {
122
        // input.setCellRenderer(rend);
123
        // }
124
        // });
67 ilm 125
        return source;
126
    }
127
 
18 ilm 128
    /*
129
     * (non-Javadoc)
130
     *
131
     * @see org.openconcerto.devis.SQLElement#getComponent()
132
     */
133
    public SQLComponent createComponent() {
134
        return new CommandeClientSQLComponent();
135
    }
136
 
137
    /**
19 ilm 138
     * Transfert d'une commande en commande fournisseur
139
     *
140
     * @param commandeID
141
     */
142
    public void transfertCommande(int commandeID) {
143
 
144
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("COMMANDE_CLIENT_ELEMENT");
145
        SQLTable tableCmdElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_ELEMENT").getTable();
146
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement("ARTICLE");
147
        SQLRow rowCmd = getTable().getRow(commandeID);
148
        List<SQLRow> rows = rowCmd.getReferentRows(elt.getTable());
83 ilm 149
        final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
19 ilm 150
        for (SQLRow sqlRow : rows) {
151
            // on récupére l'article qui lui correspond
152
            SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
153
            for (SQLField field : eltArticle.getTable().getFields()) {
154
                if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
155
                    rowArticle.put(field.getName(), sqlRow.getObject(field.getName()));
156
                }
157
            }
73 ilm 158
 
19 ilm 159
            int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
160
            SQLRow rowArticleFind = eltArticle.getTable().getRow(idArticle);
161
            SQLInjector inj = SQLInjector.getInjector(rowArticle.getTable(), tableCmdElt);
162
            SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind));
25 ilm 163
            rowValsElt.put("ID_STYLE", sqlRow.getObject("ID_STYLE"));
19 ilm 164
            rowValsElt.put("QTE", sqlRow.getObject("QTE"));
165
            rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
67 ilm 166
            rowValsElt.put("T_PA_HT", ((BigDecimal) rowValsElt.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), MathContext.DECIMAL128));
167
            rowValsElt.put("T_PA_TTC",
168
                    ((BigDecimal) rowValsElt.getObject("T_PA_HT")).multiply(new BigDecimal((rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)), MathContext.DECIMAL128));
169
 
83 ilm 170
            map.add(rowArticleFind.getForeignRow("ID_FOURNISSEUR"), rowValsElt);
19 ilm 171
 
172
        }
67 ilm 173
        MouvementStockSQLElement.createCommandeF(map, rowCmd.getForeignRow("ID_TARIF").getForeignRow("ID_DEVISE"), rowCmd.getString("NUMERO") + " - " + rowCmd.getString("NOM"));
19 ilm 174
    }
18 ilm 175
}