OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 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.supplychain.stock.element;
15
 
16
import org.openconcerto.erp.core.sales.product.model.PriceByQty;
17
import org.openconcerto.erp.importer.ArrayTableModel;
18
import org.openconcerto.erp.importer.DataImporter;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
23
import org.openconcerto.sql.model.SQLTable;
24
 
25
import java.io.File;
26
import java.io.IOException;
27
import java.math.BigDecimal;
28
import java.sql.SQLException;
29
import java.util.ArrayList;
30
import java.util.Calendar;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.Set;
37
 
38
public class EtatStockFromInventoryFileCreator {
39
 
40
    // Map<String, SQLRowValues> kits = new HashMap<String, SQLRowValues>();
41
    // List<String> codeKits = new ArrayList<String>();
42
    // List<SQLRowValues> rowValsArtNonSync = new ArrayList<SQLRowValues>();
43
 
44
    public void importArticles(File file, Date d) throws IOException, SQLException {
45
 
46
        final SQLTable table = Configuration.getInstance().getRoot().findTable("ARTICLE");
47
        Map<String, SQLRowValues> articles = getArticles();
48
 
49
        final DataImporter importer = new DataImporter(table) {
50
            @Override
51
            protected void customizeRowValuesToFetch(SQLRowValues vals) {
52
 
53
                vals.putRowValues("ID_STOCK").putNulls("ID", "QTE_REEL", "QTE_TH");
54
            }
55
        };
56
        importer.setSkipFirstLine(true);
57
 
58
        ArrayTableModel m = importer.createModelFrom(file);
59
 
60
        SQLRowValues rowValsEtatStock = new SQLRowValues(table.getTable("ETAT_STOCK"));
61
        rowValsEtatStock.put("DATE", d);
62
        SQLRow etatStock = rowValsEtatStock.commit();
63
        BigDecimal total = BigDecimal.ZERO;
64
        for (int i = 0; i < m.getRowCount(); i++) {
65
            List<Object> o = m.getLineValuesAt(i);
66
            String code = o.get(0).toString();
67
            if (code.trim().length() == 0) {
68
                break;
69
            }
70
            final String stringQty = o.get(3).toString();
71
            Integer qty = stringQty.trim().length() == 0 ? 0 : Integer.valueOf(stringQty);
72
 
73
            SQLRowValues match = articles.get(code);
74
            if (match != null) {
75
 
76
                SQLRowValues stockValues = new SQLRowValues(table.getTable("ETAT_STOCK_ELEMENT"));
77
 
78
                final BigDecimal qtyB = new BigDecimal(qty);
79
                stockValues.put("QTE", qtyB);
80
                stockValues.put("NOM", match.getString("NOM"));
81
                stockValues.put("CODE", match.getString("CODE"));
82
                stockValues.put("ID_ARTICLE", match.getID());
83
                final BigDecimal prc = getPRC(match, qty, d);
84
                stockValues.put("PA", prc);
85
                final BigDecimal totalElt = prc.multiply(qtyB);
86
                stockValues.put("T_PA", totalElt);
87
                stockValues.put("ID_ETAT_STOCK", etatStock.getID());
88
                stockValues.commit();
89
 
90
                total = total.add(totalElt);
91
 
92
            } else {
93
                System.err.println("Aucun article correspondant au code " + code);
94
            }
95
        }
96
        etatStock.createEmptyUpdateRow().put("MONTANT_HA", total).commit();
97
    }
98
 
99
    public BigDecimal getPRC(SQLRowValues rowVals, int qty, Date d) {
100
        if (rowVals.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
101
            SQLTable table = rowVals.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
102
            Collection<SQLRow> prcs = rowVals.asRow().getReferentRows(table);
103
 
104
            BigDecimal result = null;
105
            final List<PriceByQty> prices = new ArrayList<PriceByQty>();
106
 
107
            for (SQLRow row : prcs) {
108
                Calendar date = Calendar.getInstance();
109
                date.set(Calendar.DAY_OF_MONTH, 1);
110
                date.set(Calendar.MONTH, 1);
111
                date.set(Calendar.YEAR, 2001);
112
                if (row.getObject("DATE") != null) {
113
                    date = row.getDate("DATE");
114
                }
115
                prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
116
            }
117
 
118
            result = PriceByQty.getPriceForQty(qty, prices, d);
119
            if (result == null) {
120
                // Can occur during editing
121
                result = BigDecimal.ZERO;
122
            }
123
            return result;
124
        } else {
125
            return rowVals.getBigDecimal("PA_HT");
126
        }
127
    }
128
 
129
    private Map<String, SQLRowValues> getArticles() throws SQLException {
130
        final SQLTable table = Configuration.getInstance().getRoot().findTable("ARTICLE");
131
        SQLRowValues graph = new SQLRowValues(table);
132
        graph.put("ID", null);
133
        graph.put("CODE", null);
134
        graph.put("SYNC_ID", null);
135
        graph.put("NOM", null);
136
        graph.put("PA_HT", null);
137
        graph.putRowValues("ID_STOCK").putNulls("ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
138
 
139
        final SQLTable tableArtElt = table.getTable("ARTICLE_ELEMENT");
140
        SQLRowValues artElt = new SQLRowValues(tableArtElt);
141
        artElt.put("ID", null);
142
        artElt.put("QTE", null);
143
        artElt.put("QTE_UNITAIRE", null);
144
        artElt.put("ID_ARTICLE_PARENT", graph);
145
        artElt.putRowValues("ID_ARTICLE").putNulls("ID", "CODE", "NOM").putRowValues("ID_STOCK").putNulls("QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
146
 
147
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
148
        List<SQLRowValues> results = fetcher.fetch();
149
 
150
        Map<String, SQLRowValues> vals = new HashMap<String, SQLRowValues>();
151
        for (SQLRowValues sqlRowValues : results) {
152
 
153
            final Set<SQLRowValues> referentRows = sqlRowValues.getReferentRows(tableArtElt.getField("ID_ARTICLE_PARENT"));
154
            // On ne prend que les articles simples
155
            if (referentRows.size() == 0) {
156
                final String code = sqlRowValues.getString("CODE");
157
                vals.put(code, sqlRowValues);
158
 
159
            } else {
160
 
161
            }
162
        }
163
        return vals;
164
    }
165
}