OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
156 ilm 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;
182 ilm 19
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
156 ilm 20
import org.openconcerto.sql.Configuration;
182 ilm 21
import org.openconcerto.sql.element.SQLElement;
22
import org.openconcerto.sql.model.DBRoot;
156 ilm 23
import org.openconcerto.sql.model.SQLRow;
182 ilm 24
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLRowListRSH;
156 ilm 26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
182 ilm 28
import org.openconcerto.sql.model.SQLSelect;
156 ilm 29
import org.openconcerto.sql.model.SQLTable;
182 ilm 30
import org.openconcerto.sql.preferences.SQLPreferences;
31
import org.openconcerto.utils.Tuple3;
156 ilm 32
 
33
import java.io.File;
34
import java.io.IOException;
35
import java.math.BigDecimal;
36
import java.sql.SQLException;
37
import java.util.ArrayList;
182 ilm 38
import java.util.Arrays;
156 ilm 39
import java.util.Calendar;
40
import java.util.Collection;
41
import java.util.Date;
42
import java.util.HashMap;
43
import java.util.List;
44
import java.util.Map;
182 ilm 45
import java.util.Map.Entry;
156 ilm 46
import java.util.Set;
47
 
182 ilm 48
import javax.swing.JOptionPane;
49
 
156 ilm 50
public class EtatStockFromInventoryFileCreator {
51
 
52
    // Map<String, SQLRowValues> kits = new HashMap<String, SQLRowValues>();
53
    // List<String> codeKits = new ArrayList<String>();
54
    // List<SQLRowValues> rowValsArtNonSync = new ArrayList<SQLRowValues>();
55
 
182 ilm 56
    private static String FAMILLE = "Famille";
57
    private static String CODE = "Code";
58
    private static String NOM = "Nom";
59
    private static String TAILLE = "Taille";
60
    private static String COULEUR = "Couleur";
61
    private static String QTE = "Qté réelle relevée";
62
    private final Map<String, Integer> mapCouleur = new HashMap<String, Integer>();
63
    private final Map<String, Integer> mapTaille = new HashMap<String, Integer>();
64
    private final DBRoot root;
65
    private final SQLElement artElt;
66
 
67
    public EtatStockFromInventoryFileCreator(SQLElement artElt) {
68
        this.artElt = artElt;
69
        this.root = artElt.getTable().getDBRoot();
70
    }
71
 
156 ilm 72
    public void importArticles(File file, Date d) throws IOException, SQLException {
73
 
182 ilm 74
        final SQLTable table = this.root.findTable("ARTICLE");
156 ilm 75
 
182 ilm 76
        {
77
            SQLSelect sel = new SQLSelect();
78
            sel.addSelect(table.getDBRoot().getTable("ARTICLE_DECLINAISON_COULEUR").getKey());
79
            sel.addSelect(table.getDBRoot().getTable("ARTICLE_DECLINAISON_COULEUR").getField("NOM"));
156 ilm 80
 
182 ilm 81
            List<SQLRow> l = SQLRowListRSH.execute(sel);
82
 
83
            for (SQLRow sqlRow : l) {
84
                mapCouleur.put(sqlRow.getString("NOM").trim(), sqlRow.getID());
156 ilm 85
            }
182 ilm 86
        }
156 ilm 87
 
182 ilm 88
        {
89
            SQLSelect sel = new SQLSelect();
90
            sel.addSelect(table.getDBRoot().getTable("ARTICLE_DECLINAISON_TAILLE").getKey());
91
            sel.addSelect(table.getDBRoot().getTable("ARTICLE_DECLINAISON_TAILLE").getField("NOM"));
156 ilm 92
 
182 ilm 93
            List<SQLRow> l = SQLRowListRSH.execute(sel);
94
 
95
            for (SQLRow sqlRow : l) {
96
                mapTaille.put(sqlRow.getString("NOM").trim(), sqlRow.getID());
156 ilm 97
            }
182 ilm 98
        }
156 ilm 99
 
182 ilm 100
        fillArticles();
156 ilm 101
 
182 ilm 102
        Map<String, Integer> columnMapping = new HashMap<String, Integer>();
103
        columnMapping.put(CODE, null);
104
        columnMapping.put(NOM, null);
105
        columnMapping.put(TAILLE, null);
106
        columnMapping.put(FAMILLE, null);
107
        columnMapping.put(COULEUR, null);
108
        columnMapping.put(QTE, null);
109
        {
110
            // Searching column index from column Header
111
            final DataImporter importer = new DataImporter(table);
112
            importer.setSkipFirstLine(false);
113
            ArrayTableModel m = importer.createModelFrom(file);
114
            List<Object> line = m.getLineValuesAt(0);
115
            for (int i = 0; i < line.size(); i++) {
116
                Object object = line.get(i);
117
                if (object != null) {
118
                    for (String key : columnMapping.keySet()) {
119
                        if (object.toString().equalsIgnoreCase(key)) {
120
                            columnMapping.put(key, i);
121
                        }
122
                    }
123
                }
124
            }
125
        }
156 ilm 126
 
182 ilm 127
        String msg = "Colonnes importées : \n";
128
        final SQLPreferences prefs = SQLPreferences.getMemCached(table.getDBRoot());
129
        final boolean hasDeclinaison = prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false);
130
        List<String> required;
131
        if (hasDeclinaison) {
132
            required = Arrays.asList(CODE, QTE, TAILLE, COULEUR);
133
        } else {
134
            required = Arrays.asList(CODE, QTE);
135
        }
156 ilm 136
 
182 ilm 137
        for (Entry<String, Integer> e : columnMapping.entrySet()) {
138
            if (e.getValue() != null) {
139
                msg += e.getKey() + " : " + getColumnName(e.getValue()) + "\n";
156 ilm 140
            } else {
182 ilm 141
                if (required.contains(e.getKey())) {
142
                    JOptionPane.showMessageDialog(null, "Aucune colonne " + e.getKey() + " trouvée, import annulé!\nCette colonne est obligatoire.");
143
                    return;
144
                }
145
                msg += e.getKey() + " : non importée\n";
156 ilm 146
            }
147
        }
182 ilm 148
        msg += "\nVoulez vous continuer ?";
149
        int a = JOptionPane.showConfirmDialog(null, msg);
150
        if (a == JOptionPane.YES_OPTION) {
151
 
152
            final DataImporter importer = new DataImporter(table) {
153
                @Override
154
                protected void customizeRowValuesToFetch(SQLRowValues vals) {
155
 
156
                    vals.putRowValues("ID_STOCK").putNulls("ID", "QTE_REEL", "QTE_TH");
157
                }
158
            };
159
            importer.setSkipFirstLine(true);
160
 
161
            ArrayTableModel m = importer.createModelFrom(file);
162
 
163
            SQLRowValues rowValsEtatStock = new SQLRowValues(table.getTable("ETAT_STOCK"));
164
            rowValsEtatStock.put("DATE", d);
165
            SQLRow etatStock = rowValsEtatStock.commit();
166
            BigDecimal total = BigDecimal.ZERO;
167
            for (int i = 0; i < m.getRowCount(); i++) {
168
                List<Object> o = m.getLineValuesAt(i);
169
                String code = o.get(columnMapping.get(CODE)).toString();
170
                if (code.trim().length() == 0) {
171
                    break;
172
                }
173
 
174
                final String stringQty = o.get(columnMapping.get(QTE)).toString();
175
                Integer qty = stringQty.trim().length() == 0 ? 0 : Integer.valueOf(stringQty);
176
 
177
                String couleur = "";
178
                if (columnMapping.get(COULEUR) != null) {
179
                    couleur = o.get(columnMapping.get(COULEUR)).toString();
180
                }
181
 
182
                String taille = "";
183
                if (columnMapping.get(TAILLE) != null) {
184
                    taille = o.get(columnMapping.get(TAILLE)).toString();
185
                }
186
 
187
                SQLRowAccessor match = findArticle(code, couleur, taille);
188
                if (match != null) {
189
 
190
                    SQLRowValues stockValues = new SQLRowValues(table.getTable("ETAT_STOCK_ELEMENT"));
191
 
192
                    final BigDecimal qtyB = new BigDecimal(qty);
193
                    stockValues.put("QTE", qtyB);
194
                    stockValues.put("NOM", match.getString("NOM"));
195
                    stockValues.put("CODE", match.getString("CODE"));
196
                    stockValues.put("ID_ARTICLE", match.getID());
197
                    final BigDecimal prc = getPRC(match, qty, d);
198
                    stockValues.put("PA", prc);
199
                    final BigDecimal totalElt = prc.multiply(qtyB);
200
                    stockValues.put("T_PA", totalElt);
201
                    stockValues.put("ID_ETAT_STOCK", etatStock.getID());
202
                    stockValues.commit();
203
 
204
                    total = total.add(totalElt);
205
 
206
                } else {
207
                    System.err.println("Aucun article correspondant au code " + code);
208
                }
209
            }
210
            etatStock.createEmptyUpdateRow().put("MONTANT_HA", total).commit();
211
        }
156 ilm 212
    }
213
 
182 ilm 214
    public BigDecimal getPRC(SQLRowAccessor rowVals, int qty, Date d) {
156 ilm 215
        if (rowVals.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
216
            SQLTable table = rowVals.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
217
            Collection<SQLRow> prcs = rowVals.asRow().getReferentRows(table);
218
 
219
            BigDecimal result = null;
220
            final List<PriceByQty> prices = new ArrayList<PriceByQty>();
221
 
222
            for (SQLRow row : prcs) {
223
                Calendar date = Calendar.getInstance();
224
                date.set(Calendar.DAY_OF_MONTH, 1);
225
                date.set(Calendar.MONTH, 1);
226
                date.set(Calendar.YEAR, 2001);
227
                if (row.getObject("DATE") != null) {
228
                    date = row.getDate("DATE");
229
                }
230
                prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
231
            }
232
 
233
            result = PriceByQty.getPriceForQty(qty, prices, d);
234
            if (result == null) {
235
                // Can occur during editing
236
                result = BigDecimal.ZERO;
237
            }
238
            return result;
239
        } else {
240
            return rowVals.getBigDecimal("PA_HT");
241
        }
242
    }
243
 
182 ilm 244
    private final Map<String, SQLRowAccessor> mapArticle = new HashMap<String, SQLRowAccessor>();
245
    private final Map<Tuple3<String, String, String>, SQLRowAccessor> mapDeclArticle = new HashMap<Tuple3<String, String, String>, SQLRowAccessor>();
246
 
247
    private void fillArticles() throws SQLException {
156 ilm 248
        final SQLTable table = Configuration.getInstance().getRoot().findTable("ARTICLE");
249
        SQLRowValues graph = new SQLRowValues(table);
250
        graph.put("ID", null);
251
        graph.put("CODE", null);
252
        graph.put("SYNC_ID", null);
253
        graph.put("NOM", null);
254
        graph.put("PA_HT", null);
255
        graph.putRowValues("ID_STOCK").putNulls("ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
182 ilm 256
        graph.putRowValues("ID_ARTICLE_DECLINAISON_COULEUR").putNulls("ID", "NOM");
257
        graph.putRowValues("ID_ARTICLE_DECLINAISON_TAILLE").putNulls("ID", "NOM");
156 ilm 258
 
259
        final SQLTable tableArtElt = table.getTable("ARTICLE_ELEMENT");
260
        SQLRowValues artElt = new SQLRowValues(tableArtElt);
261
        artElt.put("ID", null);
262
        artElt.put("QTE", null);
263
        artElt.put("QTE_UNITAIRE", null);
264
        artElt.put("ID_ARTICLE_PARENT", graph);
182 ilm 265
        final SQLRowValues putRowValues = artElt.putRowValues("ID_ARTICLE");
266
        putRowValues.putNulls("ID", "CODE", "NOM").putRowValues("ID_STOCK").putNulls("QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
267
        putRowValues.putRowValues("ID_ARTICLE_DECLINAISON_TAILLE").putNulls("ID", "NOM");
268
        putRowValues.putRowValues("ID_ARTICLE_DECLINAISON_COULEUR").putNulls("ID", "NOM");
156 ilm 269
 
270
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
271
        List<SQLRowValues> results = fetcher.fetch();
272
        for (SQLRowValues sqlRowValues : results) {
273
 
274
            final Set<SQLRowValues> referentRows = sqlRowValues.getReferentRows(tableArtElt.getField("ID_ARTICLE_PARENT"));
275
            // On ne prend que les articles simples
276
            if (referentRows.size() == 0) {
182 ilm 277
                final SQLRowAccessor couleur = sqlRowValues.getObject("ID_ARTICLE_DECLINAISON_COULEUR") == null ? null : sqlRowValues.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_COULEUR");
278
                final SQLRowAccessor taille = sqlRowValues.getObject("ID_ARTICLE_DECLINAISON_TAILLE") == null ? null : sqlRowValues.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_TAILLE");
279
                if (couleur == null && taille == null) {
280
                    mapArticle.put(sqlRowValues.getString("CODE"), sqlRowValues);
281
                } else if (couleur == null) {
282
                    mapDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), null, taille.getString("NOM")), sqlRowValues);
283
                } else if (taille == null) {
284
                    mapDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), couleur.getString("NOM"), null), sqlRowValues);
285
                } else {
286
                    mapDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), couleur.getString("NOM"), taille.getString("NOM")), sqlRowValues);
287
                }
156 ilm 288
 
289
            } else {
290
 
291
            }
292
        }
182 ilm 293
 
156 ilm 294
    }
182 ilm 295
 
296
    private SQLRowAccessor findArticle(String code, String couleur, String taille) throws SQLException {
297
        if (!mapCouleur.containsKey(couleur)) {
298
            SQLRowValues rowVals = new SQLRowValues(root.getTable("ARTICLE_DECLINAISON_COULEUR"));
299
            rowVals.put("NOM", couleur);
300
            mapCouleur.put(couleur, rowVals.commit().getID());
301
        }
302
        if (!mapTaille.containsKey(taille)) {
303
            SQLRowValues rowVals = new SQLRowValues(root.getTable("ARTICLE_DECLINAISON_TAILLE"));
304
            rowVals.put("NOM", taille);
305
            mapTaille.put(taille, rowVals.commit().getID());
306
        }
307
 
308
        SQLRowAccessor sqlRowAccessor;
309
        if ((couleur == null || couleur.trim().length() == 0) && (taille == null || taille.trim().length() == 0)) {
310
 
311
            sqlRowAccessor = mapArticle.get(code);
312
 
313
        } else if (couleur == null || couleur.trim().length() == 0) {
314
            sqlRowAccessor = mapDeclArticle.get(Tuple3.create(code, null, taille.trim()));
315
            if (sqlRowAccessor == null) {
316
                sqlRowAccessor = cloneFromArticle(mapArticle.get(code), null, mapTaille.get(taille.trim()));
317
                mapDeclArticle.put(Tuple3.create(code, null, taille.trim()), sqlRowAccessor);
318
            }
319
        } else if (taille == null || taille.trim().length() == 0) {
320
            sqlRowAccessor = mapDeclArticle.get(Tuple3.create(code, couleur.trim(), null));
321
            if (sqlRowAccessor == null) {
322
                sqlRowAccessor = cloneFromArticle(mapArticle.get(code), mapCouleur.get(couleur.trim()), null);
323
                mapDeclArticle.put(Tuple3.create(code, couleur.trim(), null), sqlRowAccessor);
324
            }
325
        } else {
326
            sqlRowAccessor = mapDeclArticle.get(Tuple3.create(code, couleur.trim(), taille.trim()));
327
            if (sqlRowAccessor == null) {
328
                if (mapArticle.get(code) != null) {
329
                    sqlRowAccessor = cloneFromArticle(mapArticle.get(code),
330
 
331
                            mapCouleur.get(couleur.trim()), mapTaille.get(taille.trim()));
332
                    mapDeclArticle.put(Tuple3.create(code, couleur.trim(), taille.trim()), sqlRowAccessor);
333
                }
334
            }
335
        }
336
        return sqlRowAccessor;
337
    }
338
 
339
    public SQLRow cloneFromArticle(SQLRowAccessor rArt, Integer idCouleur, Integer idTaille) throws SQLException {
340
        if (rArt == null) {
341
            return null;
342
        }
343
        SQLRowValues copy = artElt.createCopy(rArt.getID());
344
        if (idCouleur != null) {
345
            copy.put("ID_ARTICLE_DECLINAISON_COULEUR", idCouleur);
346
        }
347
        if (idTaille != null) {
348
            copy.put("ID_ARTICLE_DECLINAISON_TAILLE", idTaille);
349
        }
350
        return copy.commit();
351
    }
352
 
353
    private String getColumnName(int columnNumber) {
354
        int dividend = columnNumber;
355
        String columnName = "";
356
        int modulo;
357
 
358
        while (dividend >= 0) {
359
            modulo = dividend % 26;
360
            columnName = String.valueOf((char) (65 + modulo)) + columnName;
361
            dividend = (int) ((dividend - modulo) / 26);
362
            if (dividend <= 0) {
363
                break;
364
            } else {
365
                dividend--;
366
            }
367
        }
368
 
369
        return columnName;
370
    }
156 ilm 371
}