OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | 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.ProductComponent;
17
import org.openconcerto.erp.core.sales.product.model.ProductHelper;
18
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
19
import org.openconcerto.erp.importer.ArrayTableModel;
20
import org.openconcerto.erp.importer.DataImporter;
182 ilm 21
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
156 ilm 22
import org.openconcerto.sql.Configuration;
182 ilm 23
import org.openconcerto.sql.element.SQLElement;
156 ilm 24
import org.openconcerto.sql.model.DBRoot;
185 ilm 25
import org.openconcerto.sql.model.SQLBase;
26
import org.openconcerto.sql.model.SQLInsert;
156 ilm 27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowAccessor;
182 ilm 29
import org.openconcerto.sql.model.SQLRowListRSH;
156 ilm 30
import org.openconcerto.sql.model.SQLRowValues;
31
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
182 ilm 32
import org.openconcerto.sql.model.SQLSelect;
156 ilm 33
import org.openconcerto.sql.model.SQLTable;
185 ilm 34
import org.openconcerto.sql.model.SQLUpdate;
35
import org.openconcerto.sql.model.Where;
182 ilm 36
import org.openconcerto.sql.preferences.SQLPreferences;
185 ilm 37
import org.openconcerto.sql.request.UpdateBuilder;
156 ilm 38
import org.openconcerto.sql.utils.SQLUtils;
185 ilm 39
import org.openconcerto.utils.CollectionUtils;
182 ilm 40
import org.openconcerto.utils.Tuple3;
185 ilm 41
import org.openconcerto.utils.cc.ITransformer;
156 ilm 42
 
43
import java.io.File;
44
import java.io.IOException;
45
import java.math.BigDecimal;
46
import java.sql.SQLException;
47
import java.util.ArrayList;
182 ilm 48
import java.util.Arrays;
156 ilm 49
import java.util.Calendar;
50
import java.util.Collection;
51
import java.util.Date;
52
import java.util.HashMap;
185 ilm 53
import java.util.HashSet;
156 ilm 54
import java.util.List;
55
import java.util.Map;
182 ilm 56
import java.util.Map.Entry;
156 ilm 57
import java.util.Set;
58
 
182 ilm 59
import javax.swing.JOptionPane;
60
 
156 ilm 61
import org.apache.commons.dbutils.ResultSetHandler;
62
 
63
public class InventaireFromEtatStockImporter {
64
 
177 ilm 65
    private Map<String, SQLRowValues> kits = new HashMap<String, SQLRowValues>();
185 ilm 66
    private Map<Integer, InventaireProductLine> kitsChildren = new HashMap<Integer, InventaireProductLine>();
177 ilm 67
    private List<String> codeKits = new ArrayList<String>();
182 ilm 68
    private static String FAMILLE = "Famille";
69
    private static String CODE = "Code";
70
    private static String NOM = "Nom";
71
    private static String TAILLE = "Taille";
185 ilm 72
    private static String DEPOT = "Dépôt";
182 ilm 73
    private static String COULEUR = "Couleur";
74
    private static String QTE = "Qté réelle relevée";
75
    private static String QTE_OPENCONCERTO = "QTE OpenConcerto";
76
    private final Map<String, Integer> mapCouleur = new HashMap<String, Integer>();
77
    private final Map<String, Integer> mapTaille = new HashMap<String, Integer>();
78
    private final DBRoot root;
79
    private final SQLElement artElt;
156 ilm 80
 
185 ilm 81
    public InventaireFromEtatStockImporter(SQLElement articleElt) {
182 ilm 82
        this.root = articleElt.getTable().getDBRoot();
83
        this.artElt = articleElt;
84
        {
85
            SQLSelect sel = new SQLSelect();
86
            sel.addSelect(articleElt.getTable().getTable("ARTICLE_DECLINAISON_COULEUR").getKey());
87
            sel.addSelect(articleElt.getTable().getTable("ARTICLE_DECLINAISON_COULEUR").getField("NOM"));
88
 
89
            List<SQLRow> l = SQLRowListRSH.execute(sel);
90
 
91
            for (SQLRow sqlRow : l) {
185 ilm 92
                this.mapCouleur.put(sqlRow.getString("NOM").trim(), sqlRow.getID());
182 ilm 93
            }
94
        }
95
 
96
        {
97
            SQLSelect sel = new SQLSelect();
98
            sel.addSelect(articleElt.getTable().getTable("ARTICLE_DECLINAISON_TAILLE").getKey());
99
            sel.addSelect(articleElt.getTable().getTable("ARTICLE_DECLINAISON_TAILLE").getField("NOM"));
100
 
101
            List<SQLRow> l = SQLRowListRSH.execute(sel);
102
 
103
            for (SQLRow sqlRow : l) {
185 ilm 104
                this.mapTaille.put(sqlRow.getString("NOM").trim(), sqlRow.getID());
182 ilm 105
            }
106
        }
156 ilm 107
    }
108
 
109
    public void importArticles(File file, DBRoot root) throws IOException, SQLException {
110
 
111
        final SQLTable table = root.findTable("ARTICLE");
112
        final SQLTable tableArtElt = root.findTable("ARTICLE_ELEMENT");
113
 
182 ilm 114
        // Récupération des couples articles/stocks existant
115
        fillArticles();
156 ilm 116
 
182 ilm 117
        // Matching des colonnes
118
        Map<String, Integer> columnMapping = new HashMap<String, Integer>();
119
        columnMapping.put(CODE, null);
120
        columnMapping.put(NOM, null);
121
        columnMapping.put(TAILLE, null);
122
        columnMapping.put(FAMILLE, null);
123
        columnMapping.put(COULEUR, null);
124
        columnMapping.put(QTE, null);
125
        columnMapping.put(QTE_OPENCONCERTO, null);
185 ilm 126
        columnMapping.put(DEPOT, null);
182 ilm 127
        {
128
            // Searching column index from column Header
129
            final DataImporter importer = new DataImporter(table);
130
            importer.setSkipFirstLine(false);
131
            ArrayTableModel m = importer.createModelFrom(file);
132
            List<Object> line = m.getLineValuesAt(0);
133
            for (int i = 0; i < line.size(); i++) {
134
                Object object = line.get(i);
135
                if (object != null) {
136
                    for (String key : columnMapping.keySet()) {
137
                        if (object.toString().equalsIgnoreCase(key)) {
138
                            columnMapping.put(key, i);
139
                        }
140
                    }
141
                }
142
            }
143
        }
156 ilm 144
 
182 ilm 145
        String msg = "Colonnes importées : \n";
146
        final SQLPreferences prefs = SQLPreferences.getMemCached(table.getDBRoot());
147
        final boolean hasDeclinaison = prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false);
148
        List<String> required;
149
        if (hasDeclinaison) {
185 ilm 150
 
182 ilm 151
            required = Arrays.asList(CODE, QTE, QTE_OPENCONCERTO, TAILLE, COULEUR);
152
        } else {
153
            required = Arrays.asList(CODE, QTE, QTE_OPENCONCERTO);
154
        }
155
 
185 ilm 156
        SQLSelect selDepot = new SQLSelect();
157
        selDepot.addSelectStar(tableArtElt.getTable("DEPOT_STOCK"));
158
        final List<SQLRow> listDepot = SQLRowListRSH.execute(selDepot);
159
        final Map<String, SQLRow> mapDepot = new HashMap<>();
160
        for (SQLRow sqlRow : listDepot) {
161
            mapDepot.put(sqlRow.getString("NOM"), sqlRow);
162
        }
163
 
182 ilm 164
        for (Entry<String, Integer> e : columnMapping.entrySet()) {
165
            if (e.getValue() != null) {
166
                msg += e.getKey() + " : " + getColumnName(e.getValue()) + "\n";
167
            } else {
168
                if (required.contains(e.getKey())) {
169
                    JOptionPane.showMessageDialog(null, "Aucune colonne " + e.getKey() + " trouvée, import annulé!\nCette colonne est obligatoire.");
170
                    return;
171
                }
172
                msg += e.getKey() + " : non importée\n";
156 ilm 173
            }
182 ilm 174
        }
175
        msg += "\nVoulez vous continuer ?";
176
        int a = JOptionPane.showConfirmDialog(null, msg);
177
        if (a == JOptionPane.YES_OPTION) {
156 ilm 178
 
182 ilm 179
            final DataImporter importer = new DataImporter(table) {
180
                @Override
181
                protected void customizeRowValuesToFetch(SQLRowValues vals) {
156 ilm 182
 
182 ilm 183
                    vals.putRowValues("ID_STOCK").putNulls("ID", "QTE_REEL", "QTE_TH", "ID_DEPOT_STOCK");
184
                }
185
            };
186
            importer.setSkipFirstLine(true);
156 ilm 187
 
182 ilm 188
            ArrayTableModel m = importer.createModelFrom(file);
156 ilm 189
 
185 ilm 190
            // Check depot
191
            boolean multiDepotPrefs = prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false);
192
            SQLRowAccessor defaultDepotRow = null;
193
            boolean multiDepotFile = columnMapping.containsKey(DEPOT);
194
            if (!multiDepotFile) {
195
                if (multiDepotPrefs) {
196
                    // TODO popup choix depot
197
                } else {
198
                    defaultDepotRow = table.getTable("DEPOT_STOCK").getRow(DepotStockSQLElement.DEFAULT_ID);
199
                }
200
            } else {
201
                Set<String> errors = new HashSet<>();
202
                for (int i = 1; i < m.getRowCount(); i++) {
203
                    List<Object> o = m.getLineValuesAt(i);
204
                    if (o.size() >= 5) {
205
                        System.err.println(o);
206
                        String code = o.get(columnMapping.get(CODE)).toString();
207
                        if (code.trim().length() > 0) {
208
 
209
                            final String depotName = o.get(columnMapping.get(DEPOT)).toString().trim();
210
                            if (depotName.length() == 0) {
211
                                errors.add("Le Depôt n'est pas renseigné, ligne " + i + ". Import annulé!");
212
 
213
                            } else if (mapDepot.get(depotName) == null) {
214
                                errors.add("Impossible de trouver le dépôt " + depotName + ", ligne " + i + ". Import annulé!");
215
                            }
216
 
217
                        }
218
                    }
219
                }
220
                if (!errors.isEmpty()) {
221
                    JOptionPane.showMessageDialog(null, errors);
222
                    return;
223
                }
224
            }
225
 
182 ilm 226
            Calendar c = Calendar.getInstance();
227
            // c.set(Calendar.DAY_OF_MONTH, 1);
228
            // c.set(Calendar.MONTH, Calendar.JANUARY);
229
            // c.set(Calendar.HOUR_OF_DAY, 0);
230
            Date today = c.getTime();
156 ilm 231
 
182 ilm 232
            // TODO ne pas vider les stocks des kits, recalculer les stocks des kits
156 ilm 233
 
185 ilm 234
            Map<Integer, Integer> mapDepotEtat = new HashMap<>();
156 ilm 235
 
185 ilm 236
            List<SQLInsert> inserts = new ArrayList<>();
237
            List<SQLUpdate> updates = new ArrayList<>();
182 ilm 238
            for (int i = 1; i < m.getRowCount(); i++) {
239
                List<Object> o = m.getLineValuesAt(i);
240
                if (o.size() >= 5) {
241
                    System.err.println(o);
242
                    String code = o.get(columnMapping.get(CODE)).toString();
243
                    if (code.trim().length() > 0) {
156 ilm 244
 
182 ilm 245
                        final String stringQty = o.get(columnMapping.get(QTE)).toString();
246
                        Double qty = stringQty.trim().length() == 0 ? 0 : Double.valueOf(stringQty);
247
                        final String stringQtyOld = o.get(columnMapping.get(QTE_OPENCONCERTO)).toString();
248
                        float qtyOld = stringQtyOld.trim().length() == 0 ? 0 : Float.valueOf(stringQtyOld);
156 ilm 249
 
182 ilm 250
                        String couleur = "";
251
                        if (columnMapping.get(COULEUR) != null) {
252
                            couleur = o.get(columnMapping.get(COULEUR)).toString();
253
                        }
156 ilm 254
 
182 ilm 255
                        String taille = "";
256
                        if (columnMapping.get(TAILLE) != null) {
257
                            taille = o.get(columnMapping.get(TAILLE)).toString();
156 ilm 258
                        }
259
 
182 ilm 260
                        // SQLRowAccessor match = findArticle(code, couleur, taille);
156 ilm 261
 
185 ilm 262
                        InventaireProductLine match = findArticle(code, couleur, taille);
182 ilm 263
                        if (match != null) {
156 ilm 264
 
185 ilm 265
                            SQLRowAccessor depotRow = defaultDepotRow;
156 ilm 266
 
185 ilm 267
                            if (multiDepotFile) {
268
                                final String depotName = o.get(columnMapping.get(DEPOT)).toString().trim();
269
                                depotRow = mapDepot.get(depotName);
270
                            }
271
 
272
                            if (!mapDepotEtat.containsKey(depotRow.getID())) {
273
                                mapDepotEtat.put(depotRow.getID(), createEtat(table.getTable("ETAT_STOCK"), depotRow.getID()).commit().getID());
274
                            }
275
 
276
                            int etatID = mapDepotEtat.get(depotRow.getID());
277
 
278
                            SQLRowAccessor stockValues = match.getOrCreateStockRowValues(depotRow);
279
 
182 ilm 280
                            final SQLTable tableMvt = table.getTable("MOUVEMENT_STOCK");
185 ilm 281
                            SQLInsert insertMvtStockClotureFermeture = new SQLInsert();
282
                            insertMvtStockClotureFermeture.add(tableMvt.getField("QTE"), -qtyOld);
283
                            insertMvtStockClotureFermeture.add(tableMvt.getField("NOM"), "Clôture stock avant inventaire");
284
                            insertMvtStockClotureFermeture.add(tableMvt.getField("ID_ARTICLE"), match.getRowValsArt().getID());
285
                            insertMvtStockClotureFermeture.add(tableMvt.getField("DATE"), today);
286
                            insertMvtStockClotureFermeture.add(tableMvt.getField("REEL"), Boolean.TRUE);
287
                            insertMvtStockClotureFermeture.add(tableMvt.getField("ID_STOCK"), stockValues.getID());
156 ilm 288
 
185 ilm 289
                            BigDecimal prc = getPRC(match.getRowValsArt(), Math.round(qtyOld), today);
182 ilm 290
                            if (prc == null) {
291
                                prc = BigDecimal.ZERO;
292
                            }
293
                            if (tableMvt.contains("PRICE")) {
185 ilm 294
                                insertMvtStockClotureFermeture.add(tableMvt.getField("PRICE"), prc);
182 ilm 295
                            }
185 ilm 296
                            insertMvtStockClotureFermeture.add(tableMvt.getField("CLOTURE"), Boolean.TRUE);
297
                            insertMvtStockClotureFermeture.add(tableMvt.getField("ID_ETAT_STOCK"), etatID);
298
                            // insertMvtStockClotureFermeture.getGraph().store(StoreMode.COMMIT,
299
                            // false);
182 ilm 300
 
185 ilm 301
                            inserts.add(insertMvtStockClotureFermeture);
182 ilm 302
 
185 ilm 303
                            final SQLTable tableEtatElt = table.getTable("ETAT_STOCK_ELEMENT");
304
                            SQLInsert insertItem = new SQLInsert();
305
                            insertItem.add(tableEtatElt.getField("ID_ETAT_STOCK"), etatID);
306
                            insertItem.add(tableEtatElt.getField("PA"), prc);
307
                            insertItem.add(tableEtatElt.getField("PV"), BigDecimal.ZERO);
308
                            insertItem.add(tableEtatElt.getField("QTE"), qtyOld);
309
                            insertItem.add(tableEtatElt.getField("T_PA"), prc.multiply(new BigDecimal(qtyOld)));
310
                            insertItem.add(tableEtatElt.getField("T_PV"), BigDecimal.ZERO);
311
                            insertItem.add(tableEtatElt.getField("CODE"), match.getRowValsArt().getString("CODE"));
312
                            insertItem.add(tableEtatElt.getField("NOM"), match.getRowValsArt().getString("NOM"));
313
                            insertItem.add(tableEtatElt.getField("ID_ARTICLE"), match.getRowValsArt().getID());
314
                            // insertItem.getGraph().store(StoreMode.COMMIT, false);
315
                            inserts.add(insertItem);
316
 
317
                            SQLInsert insertMvtStockClotureOuverture = new SQLInsert();
318
                            insertMvtStockClotureOuverture.add(tableMvt.getField("QTE"), qty);
319
                            insertMvtStockClotureOuverture.add(tableMvt.getField("NOM"), "Mise en stock inventaire");
320
                            insertMvtStockClotureOuverture.add(tableMvt.getField("ID_ETAT_STOCK"), etatID);
321
                            insertMvtStockClotureOuverture.add(tableMvt.getField("ID_ARTICLE"), match.getRowValsArt().getID());
322
                            insertMvtStockClotureOuverture.add(tableMvt.getField("DATE"), today);
323
                            insertMvtStockClotureOuverture.add(tableMvt.getField("REEL"), Boolean.TRUE);
324
                            insertMvtStockClotureOuverture.add(tableMvt.getField("ID_STOCK"), stockValues.getID());
325
                            insertMvtStockClotureOuverture.add(tableMvt.getField("OUVERTURE"), Boolean.TRUE);
182 ilm 326
                            if (tableMvt.contains("PRICE")) {
185 ilm 327
                                insertMvtStockClotureOuverture.add(tableMvt.getField("PRICE"), getPRC(match.getRowValsArt(), qty.intValue(), today));
182 ilm 328
                            }
185 ilm 329
                            inserts.add(insertMvtStockClotureOuverture);
330
                            // insertMvtStockClotureOuverture.getGraph().store(StoreMode.COMMIT,
331
                            // false);
182 ilm 332
 
333
                            // if (!match.isForeignEmpty("ID_STOCK")) {
334
                            // match.getForeign("ID_STOCK").createEmptyUpdateRow().put("QTE_REEL",
335
                            // qty).commit();
336
                            // } else {
185 ilm 337
                            // final SQLRowValues createEmptyUpdateRow =
338
                            // stockValues.createEmptyUpdateRow();
339
                            // createEmptyUpdateRow.put("QTE_REEL", qty);
340
                            // createEmptyUpdateRow.getGraph().store(StoreMode.COMMIT, false);
341
                            SQLUpdate up = new SQLUpdate(new Where(stockValues.getTable().getKey(), "=", stockValues.getID()));
342
                            up.add(stockValues.getTable().getField("QTE_REEL"), qty);
343
                            updates.add(up);
182 ilm 344
                            // }
345
 
346
                        } else {
185 ilm 347
                            System.err.println("\t Aucun article correspondant au code " + code);
182 ilm 348
                        }
156 ilm 349
                    }
350
                }
351
            }
352
 
185 ilm 353
            if (!inserts.isEmpty()) {
354
                SQLInsert.executeMultipleWithBatch(table.getDBSystemRoot(), inserts);
355
            }
356
            if (!updates.isEmpty()) {
357
                SQLUpdate.executeMultipleWithBatch(table.getDBSystemRoot(), updates);
358
                System.err.println(CollectionUtils.join(updates, ";\n"));
359
            }
360
 
182 ilm 361
            /**
362
             * Mise à jour des kits
363
             */
156 ilm 364
 
185 ilm 365
            final List<? extends SQLRowAccessor> rowsDepot;
366
            if (multiDepotFile) {
367
                rowsDepot = listDepot;
368
            } else {
369
                rowsDepot = Arrays.asList(defaultDepotRow);
370
            }
371
 
182 ilm 372
            List<String> reqs = new ArrayList<String>();
185 ilm 373
            // Recalcul des stocks pour chaque kit impacté
374
            for (String code : this.codeKits) {
182 ilm 375
                System.err.println(code);
185 ilm 376
                SQLRowValues rowValsKit = this.kits.get(code);
377
 
378
                // récupération des stocks initialisés pour ce kit
379
                final Set<SQLRowValues> referentRowsStock = rowValsKit.getReferentRows(table.getTable("STOCK").getField("ID_ARTICLE"));
380
                for (SQLRowAccessor d : referentRowsStock) {
381
                    StockItem item = new StockItem(rowValsKit, d);
382
                    Collection<SQLRowValues> elts = rowValsKit.getReferentRows(tableArtElt.getField("ID_ARTICLE_PARENT"));
383
 
384
                    // Recalcul du stock pour le dépot
385
                    for (SQLRowValues sqlRowValues : elts) {
386
                        final InventaireProductLine inventaireProductLine = this.kitsChildren.get(sqlRowValues.getID());
387
 
388
                        if (inventaireProductLine != null && sqlRowValues.getForeign("ID_ARTICLE") != null) {
389
                            final SQLRowValues stockArticle = inventaireProductLine.getStockRowValues(d.getForeign("ID_DEPOT_STOCK"));
390
                            if (stockArticle != null)
391
                                item.addItemComponent(new StockItemComponent(new StockItem(sqlRowValues.getForeign("ID_ARTICLE"), stockArticle), sqlRowValues.getBigDecimal("QTE_UNITAIRE"),
392
                                        sqlRowValues.getInt("QTE")));
393
                        }
182 ilm 394
                    }
185 ilm 395
                    item.updateQtyFromChildren();
396
                    reqs.add(item.getUpdateRequest());
156 ilm 397
                }
398
            }
399
 
185 ilm 400
            if (!reqs.isEmpty()) {
401
                List<? extends ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(reqs.size());
402
                for (String s : reqs) {
403
                    handlers.add(null);
404
                }
405
                // FIXME FIRE TABLE CHANGED TO UPDATE ILISTE ??
406
                SQLUtils.executeMultiple(table.getDBSystemRoot(), reqs, handlers);
182 ilm 407
            }
185 ilm 408
            // Recalcul du stock théorique
409
            final SQLTable tableStock = root.getTable("STOCK");
410
            org.openconcerto.sql.request.UpdateBuilder req = new UpdateBuilder(tableStock);
411
            req.set("QTE_TH", SQLBase.quoteIdentifier("QTE_REEL") + "-" + SQLBase.quoteIdentifier("QTE_LIV_ATTENTE") + "+" + SQLBase.quoteIdentifier("QTE_RECEPT_ATTENTE"));
412
            table.getDBSystemRoot().getDataSource().execute(req.asString());
182 ilm 413
 
414
            /**
415
             * Mise à jour des prix mini
416
             */
417
            // for (SQLRowValues rowValsArt : rowValsArtNonSync) {
418
            // SQLRow rowArt = rowValsArt.asRow();
419
            // List<SQLRow> rowsPVMin =
420
            // rowArt.getReferentRows(tableArtElt.getTable("ARTICLE_PRIX_MIN_VENTE"));
421
            // List<SQLRow> rowsPA =
422
            // rowArt.getReferentRows(tableArtElt.getTable("ARTICLE_TARIF_FOURNISSEUR"));
423
            //
424
            // // On récupére les derniers prix min valides
425
            // Map<Integer, SQLRow> mapLastValidRows = new HashMap<Integer, SQLRow>();
426
            // for (SQLRow rowPVMin : rowsPVMin) {
427
            // final int qteMinPrice = rowPVMin.getInt("QTE");
428
            // SQLRow rowValsLastValid = mapLastValidRows.get(qteMinPrice);
429
            // if (rowValsLastValid == null || rowValsLastValid.getDate("DATE") == null ||
430
            // rowValsLastValid.getDate("DATE").before(rowPVMin.getDate("DATE"))) {
431
            // mapLastValidRows.put(qteMinPrice, rowPVMin);
432
            // }
433
            // }
434
            //
435
            // // On récupére les derniers Prix d'achat valide
436
            // Map<Integer, SQLRow> mapLastValidAchatRows = new HashMap<Integer, SQLRow>();
437
            // for (SQLRow rowPA : rowsPA) {
438
            // final int qtePRC = rowPA.getInt("QTE");
439
            // SQLRow rowValsLastValid = mapLastValidAchatRows.get(qtePRC);
440
            // if (rowValsLastValid == null || rowValsLastValid.getDate("DATE_PRIX") == null ||
441
            // rowValsLastValid.getDate("DATE_PRIX").before(rowPA.getDate("DATE_PRIX"))) {
442
            // mapLastValidAchatRows.put(qtePRC, rowPA);
443
            // }
444
            // }
445
            //
446
            // // Mise à jour, si Prix < au prix min, du PRC et des prix min
447
            // for (Integer qte : mapLastValidAchatRows.keySet()) {
448
            // SQLRow rowVals = mapLastValidAchatRows.get(qte);
449
            // checkMinPrice(rowVals, mapLastValidRows.get(qte));
450
            // }
451
            // }
156 ilm 452
        }
185 ilm 453
 
156 ilm 454
    }
455
 
185 ilm 456
    public static SQLInsert getCreateStock(SQLRowAccessor article, SQLRowAccessor depot) {
457
 
458
        SQLTable stockTable = article.getTable().getTable("STOCK");
459
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
460
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
461
 
462
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
463
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", depot.getID());
464
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", article.getID());
465
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
466
        if (rowValsResult.size() == 0) {
467
            SQLInsert insert = new SQLInsert();
468
            insert.add(stockTable.getField("ID_ARTICLE"), article.getID());
469
            insert.add(stockTable.getField("ID_DEPOT_STOCK"), depot.getID());
470
            insert.add(stockTable.getField("QTE_TH"), 0F);
471
            insert.add(stockTable.getField("QTE_REEL"), 0F);
472
            insert.add(stockTable.getField("QTE_RECEPT_ATTENTE"), 0F);
473
            insert.add(stockTable.getField("QTE_LIV_ATTENTE"), 0F);
474
            return insert;
475
        }
476
        return null;
477
 
478
    }
479
 
480
    private SQLRowValues createEtat(SQLTable tableEtat, int depotID) {
481
        SQLRowValues rowVals = new SQLRowValues(tableEtat);
482
        rowVals.put("DATE", new Date());
483
        rowVals.put("INVENTAIRE", Boolean.TRUE);
484
        rowVals.put("ID_DEPOT_STOCK", depotID);
485
        return rowVals;
486
    }
487
 
156 ilm 488
    private void checkMinPrice(SQLRow rowValsSuplierLastValid, SQLRow lastValidRow) {
489
        boolean update = false;
490
        final ProductHelper helper = new ProductHelper(rowValsSuplierLastValid.getTable().getDBRoot());
491
 
492
        BigDecimal result = helper.getEnumPrice(rowValsSuplierLastValid, SupplierPriceField.COEF_PRIX_MINI);
493
        if (result != null) {
494
            final int qteSuplier = rowValsSuplierLastValid.getInt("QTE");
495
 
496
            final Calendar date2 = rowValsSuplierLastValid.getDate("DATE_PRIX");
497
            if (date2 != null) {
498
                if (lastValidRow != null) {
499
                    final Calendar date1 = lastValidRow.getDate("DATE");
500
                    if ((date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR) && date1.get(Calendar.MONTH) == date2.get(Calendar.MONTH)
501
                            && date1.get(Calendar.DAY_OF_MONTH) == date2.get(Calendar.DAY_OF_MONTH)) || date1.after(date2)) {
502
                        if (lastValidRow.getBigDecimal("PRIX") != null && lastValidRow.getInt("QTE") <= qteSuplier) {
503
                            try {
504
                                lastValidRow.asRowValues().put("PRIX", result).commit();
505
                            } catch (SQLException e) {
506
                                // TODO Auto-generated catch block
507
                                e.printStackTrace();
508
                            }
509
                            // purchaseMinPriceListTable.setPriceMin(result);
510
                            update = true;
511
                        }
512
                    } else {
513
                        if (date1.before(date2)) {
514
                            SQLRowValues rowValsToInsert = new SQLRowValues(lastValidRow.getTable());
515
                            rowValsToInsert.put("PRIX", result);
516
                            rowValsToInsert.put("DATE", rowValsSuplierLastValid.getObject("DATE_PRIX"));
517
                            rowValsToInsert.put("QTE", rowValsSuplierLastValid.getObject("QTE"));
518
                            rowValsToInsert.put("ID_ARTICLE", rowValsSuplierLastValid.getInt("ID_ARTICLE"));
519
                            try {
520
                                rowValsToInsert.commit();
521
                            } catch (SQLException e) {
522
                                // TODO Auto-generated catch block
523
                                e.printStackTrace();
524
                            }
525
                        }
526
                    }
527
                }
528
            }
529
        }
530
 
531
    }
532
 
533
    public BigDecimal getPRC(SQLRowValues rowVals, int qty, Date d) {
534
        // SQLTable table = rowVals.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
535
        // Collection<SQLRow> prcs = rowVals.asRow().getReferentRows(table);
536
        //
537
        // BigDecimal result = null;
538
        // final List<PriceByQty> prices = new ArrayList<PriceByQty>();
539
        //
540
        // for (SQLRow row : prcs) {
541
        // Calendar date = Calendar.getInstance();
542
        // date.set(Calendar.DAY_OF_MONTH, 1);
543
        // date.set(Calendar.MONTH, 1);
544
        // date.set(Calendar.YEAR, 2001);
545
        // if (row.getObject("DATE") != null) {
546
        // date = row.getDate("DATE");
547
        // }
548
        // prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"),
549
        // date.getTime()));
550
        // }
551
        //
552
        // result = PriceByQty.getPriceForQty(qty, prices, d);
553
        // if (result == null) {
554
        // // Can occur during editing
555
        // result = BigDecimal.ZERO;
556
        // }
557
 
558
        ProductComponent comp = new ProductComponent(rowVals, new BigDecimal(qty), null, null);
559
        return comp.getPRC(d);
560
        // return result;
561
    }
562
 
185 ilm 563
    private final Map<String, InventaireProductLine> mapDepotArticle = new HashMap();
564
    private final Map<String, InventaireProductLine> mapDepotArticleVirtuel = new HashMap();
565
    private final Map<Tuple3<String, String, String>, InventaireProductLine> mapDepotDeclArticle = new HashMap();
182 ilm 566
 
567
    private void fillArticles() throws SQLException {
156 ilm 568
        final SQLTable table = Configuration.getInstance().getRoot().findTable("ARTICLE");
185 ilm 569
 
156 ilm 570
        SQLRowValues graph = new SQLRowValues(table);
571
        graph.put("ID", null);
572
        graph.put("CODE", null);
573
        graph.put("NOM", null);
182 ilm 574
        graph.put("VIRTUEL", null);
575
        graph.putRowValues("ID_ARTICLE_DECLINAISON_COULEUR").putNulls("ID", "NOM");
576
        graph.putRowValues("ID_ARTICLE_DECLINAISON_TAILLE").putNulls("ID", "NOM");
577
 
177 ilm 578
        final SQLTable foreignTableStock = table.getForeignTable("ID_STOCK");
579
        SQLRowValues graphStock = new SQLRowValues(foreignTableStock);
185 ilm 580
        graphStock.putNulls("ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE").putRowValues("ID_DEPOT_STOCK").putNulls("NOM");
177 ilm 581
        graphStock.put("ID_ARTICLE", graph);
156 ilm 582
 
182 ilm 583
        SQLRowValues graphStockArt = new SQLRowValues(foreignTableStock);
185 ilm 584
        graphStockArt.putNulls("ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE").putRowValues("ID_DEPOT_STOCK").putNulls("NOM");
182 ilm 585
        graph.put("ID_STOCK", graphStockArt);
586
 
156 ilm 587
        final SQLTable tableArtElt = table.getTable("ARTICLE_ELEMENT");
588
        SQLRowValues artElt = new SQLRowValues(tableArtElt);
589
        artElt.put("ID", null);
590
        artElt.put("QTE", null);
591
        artElt.put("QTE_UNITAIRE", null);
592
        artElt.put("ID_ARTICLE_PARENT", graph);
177 ilm 593
        final SQLRowValues articleParent = artElt.putRowValues("ID_ARTICLE");
182 ilm 594
        articleParent.putNulls("ID", "CODE", "NOM", "VIRTUEL");
595
        articleParent.putRowValues("ID_ARTICLE_DECLINAISON_COULEUR").putNulls("ID", "NOM");
596
        articleParent.putRowValues("ID_ARTICLE_DECLINAISON_TAILLE").putNulls("ID", "NOM");
597
 
177 ilm 598
        SQLRowValues graphStockItem = new SQLRowValues(foreignTableStock);
599
        graphStockItem.put("ID_ARTICLE", articleParent);
156 ilm 600
 
182 ilm 601
        SQLRowValues graphStockArtItem = new SQLRowValues(foreignTableStock);
602
        graphStockArtItem.putNulls("ID_DEPOT_STOCK", "ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
603
        articleParent.put("ID_STOCK", graphStockArtItem);
604
 
156 ilm 605
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
606
        List<SQLRowValues> results = fetcher.fetch();
607
 
608
        Calendar c = Calendar.getInstance();
609
        // c.set(Calendar.DAY_OF_MONTH, 1);
610
        c.add(Calendar.MONTH, -2);
611
        c.set(Calendar.DAY_OF_MONTH, 31);
612
 
613
        for (SQLRowValues sqlRowValues : results) {
614
            final String code = sqlRowValues.getString("CODE");
615
 
182 ilm 616
            if (sqlRowValues.getBoolean("VIRTUEL")) {
185 ilm 617
                this.mapDepotArticleVirtuel.put(sqlRowValues.getString("CODE"), new InventaireProductLine(sqlRowValues));
177 ilm 618
            }
619
 
182 ilm 620
            final SQLRowAccessor couleur = sqlRowValues.getObject("ID_ARTICLE_DECLINAISON_COULEUR") == null ? null : sqlRowValues.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_COULEUR");
621
            final SQLRowAccessor taille = sqlRowValues.getObject("ID_ARTICLE_DECLINAISON_TAILLE") == null ? null : sqlRowValues.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_TAILLE");
622
            if (couleur == null && taille == null) {
185 ilm 623
                this.mapDepotArticle.put(sqlRowValues.getString("CODE"), new InventaireProductLine(sqlRowValues));
182 ilm 624
            } else if (couleur == null) {
185 ilm 625
                this.mapDepotDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), null, taille.getString("NOM")), new InventaireProductLine(sqlRowValues));
182 ilm 626
            } else if (taille == null) {
185 ilm 627
                this.mapDepotDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), couleur.getString("NOM"), null), new InventaireProductLine(sqlRowValues));
182 ilm 628
            } else {
185 ilm 629
                this.mapDepotDeclArticle.put(Tuple3.create(sqlRowValues.getString("CODE"), couleur.getString("NOM"), taille.getString("NOM")), new InventaireProductLine(sqlRowValues));
182 ilm 630
            }
177 ilm 631
 
156 ilm 632
            final Set<SQLRowValues> referentRows = sqlRowValues.getReferentRows(tableArtElt.getField("ID_ARTICLE_PARENT"));
633
            if (referentRows.size() == 0) {
634
                // if (!sqlRowValues.isForeignEmpty("ID_STOCK")) {
635
                // SQLRowAccessor rowValsStock = sqlRowValues.getForeign("ID_STOCK");
636
                // int qteReel = rowValsStock.getInt("QTE_REEL");
637
                // {
638
                // SQLRowValues rowValsMvtStockCloture = new
639
                // SQLRowValues(table.getTable("MOUVEMENT_STOCK"));
640
                // rowValsMvtStockCloture.put("QTE", -qteReel);
641
                // rowValsMvtStockCloture.put("NOM", "Clôture du stock avant inventaire");
642
                // rowValsMvtStockCloture.put("ID_ARTICLE", sqlRowValues.getID());
643
                // rowValsMvtStockCloture.put("DATE", dEndYear);
644
                // rowValsMvtStockCloture.put("REEL", Boolean.TRUE);
645
                // rowValsMvtStockCloture.put("PRICE", getPRC(sqlRowValues, qteReel, dEndYear));
646
                // rowValsMvtStockCloture.commit();
647
                //
648
                // rowValsStock.createEmptyUpdateRow().put("QTE_REEL", 0).commit();
649
                // }
650
                //
651
                // } else {
652
                // sqlRowValues.putRowValues("ID_STOCK").commit();
653
                // }
654
            } else {
655
                boolean contains = false;
656
                for (SQLRowValues sqlRowValues2 : referentRows) {
180 ilm 657
                    if (sqlRowValues2.getForeign("ID_ARTICLE") != null && !sqlRowValues2.isForeignEmpty("ID_ARTICLE") && sqlRowValues2.getForeign("ID_ARTICLE").getString("CODE") != null) {
185 ilm 658
                        if (this.codeKits.contains(sqlRowValues2.getForeign("ID_ARTICLE").getString("CODE"))) {
156 ilm 659
                            contains = true;
660
                            break;
661
                        }
662
                    }
663
                }
664
                if (!contains) {
185 ilm 665
                    this.codeKits.add(0, code);
156 ilm 666
                } else {
185 ilm 667
                    this.codeKits.add(code);
156 ilm 668
                }
185 ilm 669
                this.kits.put(code, sqlRowValues);
156 ilm 670
                // if (sqlRowValues.isForeignEmpty("ID_STOCK")) {
671
                // sqlRowValues.putRowValues("ID_STOCK").commit();
672
                // }
673
            }
185 ilm 674
 
156 ilm 675
        }
185 ilm 676
        fillKitChildren();
156 ilm 677
    }
182 ilm 678
 
185 ilm 679
    private void fillKitChildren() {
680
        final SQLTable table = Configuration.getInstance().getRoot().findTable("ARTICLE");
681
 
682
        SQLRowValues graph = new SQLRowValues(table);
683
        graph.put("ID", null);
684
        graph.put("CODE", null);
685
        graph.put("NOM", null);
686
        graph.put("VIRTUEL", null);
687
        graph.putRowValues("ID_ARTICLE_DECLINAISON_COULEUR").putNulls("ID", "NOM");
688
        graph.putRowValues("ID_ARTICLE_DECLINAISON_TAILLE").putNulls("ID", "NOM");
689
 
690
        final SQLTable foreignTableStock = table.getForeignTable("ID_STOCK");
691
        SQLRowValues graphStock = new SQLRowValues(foreignTableStock);
692
        graphStock.putNulls("ID", "QTE_REEL", "QTE_TH", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE").putRowValues("ID_DEPOT_STOCK").putNulls("NOM");
693
        graphStock.put("ID_ARTICLE", graph);
694
 
695
        final SQLTable tableArtElt = table.getTable("ARTICLE_ELEMENT");
696
        SQLRowValues artElt = new SQLRowValues(tableArtElt);
697
        artElt.put("ID", null);
698
        artElt.put("QTE", null);
699
        artElt.put("QTE_UNITAIRE", null);
700
        artElt.put("ID_ARTICLE", graph);
701
 
702
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(artElt);
703
        fetcher.appendSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
704
 
705
            @Override
706
            public SQLSelect transformChecked(SQLSelect input) {
707
                input.setWhere(Where.inValues(tableArtElt.getField("ID_ARTICLE_PARENT"), SQLRow.getIDs(InventaireFromEtatStockImporter.this.kits.values())));
708
                return input;
709
            }
710
        });
711
        List<SQLRowValues> results = fetcher.fetch();
712
        for (SQLRowValues sqlRowValues : results) {
713
            final SQLRowAccessor foreign = sqlRowValues.getNonEmptyForeign("ID_ARTICLE");
714
            if (foreign != null) {
715
                this.kitsChildren.put(foreign.getID(), new InventaireProductLine(foreign.asRowValues()));
716
            }
717
        }
718
    }
719
 
720
    private InventaireProductLine findArticle(String code, String couleur, String taille) throws SQLException {
721
        if (!this.mapCouleur.containsKey(couleur)) {
722
            SQLRowValues rowVals = new SQLRowValues(this.root.getTable("ARTICLE_DECLINAISON_COULEUR"));
182 ilm 723
            rowVals.put("NOM", couleur);
185 ilm 724
            this.mapCouleur.put(couleur, rowVals.commit().getID());
182 ilm 725
        }
185 ilm 726
        if (!this.mapTaille.containsKey(taille)) {
727
            SQLRowValues rowVals = new SQLRowValues(this.root.getTable("ARTICLE_DECLINAISON_TAILLE"));
182 ilm 728
            rowVals.put("NOM", taille);
185 ilm 729
            this.mapTaille.put(taille, rowVals.commit().getID());
182 ilm 730
        }
731
 
185 ilm 732
        InventaireProductLine t;
182 ilm 733
        if ((couleur == null || couleur.trim().length() == 0) && (taille == null || taille.trim().length() == 0)) {
185 ilm 734
            t = this.mapDepotArticle.get(code);
182 ilm 735
        } else if (couleur == null || couleur.trim().length() == 0) {
185 ilm 736
            t = this.mapDepotDeclArticle.get(Tuple3.create(code, null, taille.trim()));
182 ilm 737
            if (t == null) {
185 ilm 738
                t = this.mapDepotDeclArticle.get(Tuple3.create(code, "", taille.trim()));
182 ilm 739
            }
185 ilm 740
            if (t == null) {
741
                System.err.println("\tCREATE ARTICLE " + code + " taille : " + taille);
742
                SQLRowValues rARtVirt = this.mapDepotArticleVirtuel.get(code).getRowValsArt();
743
                SQLRowValues artRow = cloneFromArticle(rARtVirt, null, this.mapTaille.get(taille.trim())).asRowValues();
744
                t = new InventaireProductLine(artRow);
745
                this.mapDepotDeclArticle.put(Tuple3.create(code, null, taille.trim()), t);
746
            }
182 ilm 747
        } else if (taille == null || taille.trim().length() == 0) {
185 ilm 748
            t = this.mapDepotDeclArticle.get(Tuple3.create(code, couleur.trim(), null));
182 ilm 749
            if (t == null) {
185 ilm 750
                t = this.mapDepotDeclArticle.get(Tuple3.create(code, couleur.trim(), ""));
182 ilm 751
            }
185 ilm 752
            if (t == null) {
753
                System.err.println("\tCREATE ARTICLE " + code + " couleur : " + couleur);
754
                SQLRowValues rARtVirt = this.mapDepotArticleVirtuel.get(code).getRowValsArt();
755
                SQLRowValues artRow = cloneFromArticle(rARtVirt, this.mapCouleur.get(couleur.trim()), null).asRowValues();
756
                t = new InventaireProductLine(artRow);
757
                this.mapDepotDeclArticle.put(Tuple3.create(code, couleur.trim(), null), t);
758
            }
182 ilm 759
        } else {
185 ilm 760
            t = this.mapDepotDeclArticle.get(Tuple3.create(code, couleur.trim(), taille.trim()));
182 ilm 761
            if (t == null) {
185 ilm 762
                if (this.mapDepotArticleVirtuel.get(code) != null) {
763
                    System.err.println("\tCREATE ARTICLE " + code + " taille : " + taille + " couleur : " + couleur);
764
                    SQLRowValues rARtVirt = this.mapDepotArticleVirtuel.get(code).getRowValsArt();
765
                    SQLRowValues artRow = cloneFromArticle(rARtVirt, this.mapCouleur.get(couleur.trim()), this.mapTaille.get(taille.trim())).asRowValues();
766
                    t = new InventaireProductLine(artRow);
767
                    this.mapDepotDeclArticle.put(Tuple3.create(code, couleur.trim(), taille.trim()), t);
182 ilm 768
                }
769
            }
770
        }
771
        return t;
772
    }
773
 
774
    public SQLRow cloneFromArticle(SQLRowAccessor rArt, Integer idCouleur, Integer idTaille) throws SQLException {
775
        if (rArt == null) {
776
            return null;
777
        }
185 ilm 778
        SQLRowValues copy = this.artElt.createCopy(rArt.getID());
182 ilm 779
        copy.put("VIRTUEL", Boolean.FALSE);
780
        copy.put("ID_ARTICLE_VIRTUEL_PERE", rArt.getID());
781
        if (idCouleur != null) {
782
            copy.put("ID_ARTICLE_DECLINAISON_COULEUR", idCouleur);
783
        }
784
        if (idTaille != null) {
785
            copy.put("ID_ARTICLE_DECLINAISON_TAILLE", idTaille);
786
        }
787
        return copy.commit();
788
    }
789
 
790
    private String getColumnName(int columnNumber) {
791
        int dividend = columnNumber;
792
        String columnName = "";
793
        int modulo;
794
 
795
        while (dividend >= 0) {
796
            modulo = dividend % 26;
797
            columnName = String.valueOf((char) (65 + modulo)) + columnName;
798
            dividend = (int) ((dividend - modulo) / 26);
799
            if (dividend <= 0) {
800
                break;
801
            } else {
802
                dividend--;
803
            }
804
        }
805
 
806
        return columnName;
807
    }
185 ilm 808
 
809
    class InventaireProductLine {
810
        private final SQLRowValues rowValsArt;
811
        private final Map<String, SQLRowValues> stockMap = new HashMap<>();
812
 
813
        public InventaireProductLine(SQLRowValues rowValsArt) {
814
            this.rowValsArt = rowValsArt;
815
 
816
            Collection<SQLRowValues> stocks = this.rowValsArt.getReferentRows(this.rowValsArt.getTable().getTable("STOCK"));
817
 
818
            for (SQLRowValues rowValsStock : stocks) {
819
                this.stockMap.put(rowValsStock.getForeign("ID_DEPOT_STOCK").getString("NOM"), rowValsStock);
820
            }
821
        }
822
 
823
        public SQLRowValues getOrCreateStockRowValues(SQLRowAccessor depot) {
824
            final String depotName = depot.getString("NOM");
825
            if (!this.stockMap.containsKey(depotName)) {
826
                this.stockMap.put(depotName, ProductComponent.findOrCreateStock(this.rowValsArt, depot).asRowValues());
827
            }
828
            return this.stockMap.get(depotName);
829
        }
830
 
831
        public SQLRowValues getStockRowValues(SQLRowAccessor depot) {
832
            final String depotName = depot.getString("NOM");
833
            if (!this.stockMap.containsKey(depotName)) {
834
                return null;
835
            }
836
            return this.stockMap.get(depotName);
837
        }
838
 
839
        public SQLRowValues getRowValsArt() {
840
            return this.rowValsArt;
841
        }
842
 
843
    }
156 ilm 844
}