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.sql.model.DBRoot;
185 ilm 17
import org.openconcerto.sql.model.SQLInsert;
156 ilm 18
import org.openconcerto.sql.model.SQLRow;
177 ilm 19
import org.openconcerto.sql.model.SQLRowAccessor;
156 ilm 20
import org.openconcerto.sql.model.SQLRowListRSH;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.utils.DecimalUtils;
27
import org.openconcerto.utils.ExceptionHandler;
28
import org.openconcerto.utils.cc.ITransformer;
29
 
30
import java.math.BigDecimal;
31
import java.sql.SQLException;
185 ilm 32
import java.util.ArrayList;
33
import java.util.Arrays;
156 ilm 34
import java.util.Date;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38
 
39
public class EtatStockSnapshotCreator {
40
 
41
    private final Date d;
42
    private final DBRoot root;
177 ilm 43
    private final SQLRowAccessor depot;
182 ilm 44
    private final boolean withAllProducts;
156 ilm 45
 
182 ilm 46
    public EtatStockSnapshotCreator(SQLRowAccessor depot, Date d, DBRoot root, boolean withAllProducts) {
156 ilm 47
        this.d = d;
177 ilm 48
        this.depot = depot;
156 ilm 49
        this.root = root;
182 ilm 50
        this.withAllProducts = withAllProducts;
156 ilm 51
    }
52
 
185 ilm 53
    public int create() {
156 ilm 54
        // Récupération des inventaires
55
        SQLTable tableEtatStock = this.root.getTable("ETAT_STOCK");
56
        SQLSelect sel = new SQLSelect();
57
        sel.addSelectStar(tableEtatStock);
58
        Where wEtat = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
182 ilm 59
        wEtat = wEtat.and(new Where(tableEtatStock.getField("ID_DEPOT_STOCK"), "=", this.depot.getID()));
156 ilm 60
        sel.setWhere(wEtat);
61
        List<SQLRow> rowsEtatStock = SQLRowListRSH.execute(sel);
62
        Map<Integer, Integer> mapEtatStock = new HashMap<Integer, Integer>();
63
        for (SQLRow sqlRow : rowsEtatStock) {
64
            SQLTable tableMvtStock = this.root.getTable("MOUVEMENT_STOCK");
177 ilm 65
            SQLTable tableStock = this.root.getTable("STOCK");
156 ilm 66
            SQLSelect selMvt = new SQLSelect();
67
            selMvt.addSelect(tableMvtStock.getKey(), "MIN");
68
            Where wMvt = new Where(tableMvtStock.getField("OUVERTURE"), "=", Boolean.TRUE);
177 ilm 69
            wMvt = wMvt.and(new Where(tableMvtStock.getField("ID_ETAT_STOCK"), "=", sqlRow.getID()));
70
            wMvt = wMvt.and(new Where(tableMvtStock.getField("ID_STOCK"), "=", tableStock.getKey()));
182 ilm 71
            wMvt = wMvt.and(new Where(tableStock.getField("ID_DEPOT_STOCK"), "=", depot.getID()));
156 ilm 72
            selMvt.setWhere(wMvt);
73
            Integer idMvt = (Integer) tableMvtStock.getDBSystemRoot().getDataSource().executeScalar(selMvt.asString());
74
            if (idMvt != null) {
75
                mapEtatStock.put(sqlRow.getID(), idMvt);
76
            }
77
        }
78
 
79
        Map<Integer, EtatStock> mapStockSnap = new HashMap<Integer, EtatStock>();
80
        {
182 ilm 81
            final SQLTable tableMvtStock = this.root.getTable("MOUVEMENT_STOCK");
156 ilm 82
 
182 ilm 83
            final SQLRowValues vals = new SQLRowValues(tableMvtStock);
156 ilm 84
 
85
            vals.put("QTE", null);
182 ilm 86
            if (tableMvtStock.contains("PRICE")) {
156 ilm 87
                vals.put("PRICE", null);
88
            }
185 ilm 89
            vals.putRowValues("ID_ARTICLE").putNulls("CODE", "NOM");
177 ilm 90
            vals.putRowValues("ID_STOCK").putNulls("QTE_REEL").putRowValues("ID_DEPOT_STOCK").putNulls("ID", "NOM", "CODE");
182 ilm 91
 
156 ilm 92
            // Calendar cal0116 = Calendar.getInstance();
93
            // cal0116.set(2016, Calendar.JANUARY, 1, 0, 0, 0);
94
            // final Date dateDeb = cal0116.getTime();
95
 
96
            // Récupération du dernier etat de stock
97
            SQLSelect selEtatD = new SQLSelect();
98
            selEtatD.addSelectStar(tableEtatStock);
99
            Where wEtatD = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
182 ilm 100
            wEtatD = wEtatD.and(new Where(tableEtatStock.getField("ID_DEPOT_STOCK"), "=", this.depot.getID()));
156 ilm 101
            selEtatD.setWhere(wEtatD);
102
            List<SQLRow> rowsEtatStockD = SQLRowListRSH.execute(selEtatD);
103
            SQLRow rowEtatStockDeb = null;
104
            for (SQLRow sqlRow : rowsEtatStockD) {
105
 
106
                if (sqlRow.getDate("DATE").getTime().before(this.d)) {
107
                    if (rowEtatStockDeb == null || rowEtatStockDeb.getDate("DATE").before(sqlRow.getDate("DATE"))) {
108
                        rowEtatStockDeb = sqlRow;
109
                    }
110
                }
111
 
112
            }
113
            final Date dateDeb;
114
            final Integer idMvtStockDeb;
115
            if (rowEtatStockDeb != null) {
116
                dateDeb = rowEtatStockDeb.getDate("DATE").getTime();
117
                idMvtStockDeb = mapEtatStock.get(rowEtatStockDeb.getID());
118
            } else {
119
                dateDeb = null;
120
                idMvtStockDeb = null;
121
            }
122
 
123
            final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(vals);
124
            fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
125
 
126
                @Override
127
                public SQLSelect transformChecked(SQLSelect sel) {
128
 
182 ilm 129
                    Where w = (new Where(tableMvtStock.getField("DATE"), "<=", d));
156 ilm 130
 
131
                    if (dateDeb != null) {
182 ilm 132
                        w = w.and(new Where(tableMvtStock.getField("DATE"), ">=", dateDeb));
133
                        w = w.and(new Where(tableMvtStock.getKey(), ">=", idMvtStockDeb));
134
                        w = w.and(new Where(tableMvtStock.getField("CLOTURE"), "!=", Boolean.TRUE));
156 ilm 135
                    }
182 ilm 136
                    w = w.and(new Where(tableMvtStock.getField("REEL"), "=", Boolean.TRUE));
137
                    w = w.and(new Where(sel.getJoin(tableMvtStock.getField("ID_STOCK")).getJoinedTable().getField("ID_DEPOT_STOCK"), "=", depot.getID()));
156 ilm 138
 
139
                    sel.setWhere(w);
140
                    return sel;
141
                }
142
            });
143
 
144
            List<SQLRowValues> list = fetcher.fetch();
145
 
146
            BigDecimal totalHT = BigDecimal.ZERO;
147
            for (int i = 0; i < list.size(); i++) {
148
                SQLRowValues rowVF = list.get(i);
149
                if (!rowVF.isForeignEmpty("ID_ARTICLE") && rowVF.getForeignID("ID_ARTICLE") > rowVF.getForeign("ID_ARTICLE").getTable().getUndefinedID()) {
150
                    final int foreignIDArt = rowVF.getForeignID("ID_ARTICLE");
151
                    if (!mapStockSnap.containsKey(foreignIDArt)) {
152
                        mapStockSnap.put(foreignIDArt, new EtatStock(rowVF.getForeign("ID_ARTICLE")));
153
                    }
154
                    EtatStock et = mapStockSnap.get(foreignIDArt);
155
                    et.setQte(et.getQte().add(new BigDecimal(rowVF.getFloat("QTE"))));
156
                    BigDecimal bigDecimal = BigDecimal.ZERO;
182 ilm 157
                    if (tableMvtStock.contains("PRICE")) {
156 ilm 158
                        bigDecimal = rowVF.getBigDecimal("PRICE");
159
                    }
160
                    et.setPa(bigDecimal);
161
                    totalHT = totalHT.add(bigDecimal.multiply(new BigDecimal(rowVF.getFloat("QTE"), DecimalUtils.HIGH_PRECISION)));
162
                }
163
            }
164
 
182 ilm 165
            // Ajout des articles sans mouvement de stock sur la période
166
            final SQLTable tableStock = tableMvtStock.getTable("STOCK");
167
            final SQLRowValues valsStock = new SQLRowValues(tableStock);
168
            valsStock.putRowValues("ID_ARTICLE").putNulls("CODE", "NOM", "OBSOLETE");
169
            valsStock.putNulls("QTE_REEL").putRowValues("ID_DEPOT_STOCK").putNulls("ID", "NOM", "CODE");
170
            SQLRowValuesListFetcher fetcherStock = SQLRowValuesListFetcher.create(valsStock);
171
            SQLTable tableArt = tableStock.getForeignTable("ID_ARTICLE");
172
            fetcherStock.addSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
173
                @Override
174
                public SQLSelect transformChecked(SQLSelect input) {
175
                    Where w = new Where(tableStock.getField("ID_DEPOT_STOCK"), "=", depot.getID());
176
                    w = w.and(new Where(input.getAlias(tableArt).getField("OBSOLETE"), "=", Boolean.FALSE));
177
                    input.setWhere(w);
178
                    System.err.println(input.asString());
179
                    return input;
180
                }
181
            }, 0);
182
            List<SQLRowValues> resultAllStock = fetcherStock.fetch();
183
 
184
            for (SQLRowValues sqlRowValues : resultAllStock) {
185
                final int foreignIDArt = sqlRowValues.getForeignID("ID_ARTICLE");
186
                if (!mapStockSnap.containsKey(foreignIDArt)) {
187
                    mapStockSnap.put(foreignIDArt, new EtatStock(sqlRowValues.getForeign("ID_ARTICLE")));
188
                }
189
            }
190
 
191
            if (this.withAllProducts) {
192
                // Ajout de tous les articles non obsoletes
193
                final SQLRowValues valsArt = new SQLRowValues(tableArt);
194
                valsArt.putNulls("CODE", "NOM", "OBSOLETE");
195
                SQLRowValuesListFetcher fetcherArt = SQLRowValuesListFetcher.create(valsArt);
196
                fetcherArt.addSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
197
                    @Override
198
                    public SQLSelect transformChecked(SQLSelect input) {
199
                        Where w = new Where(tableArt.getField("VIRTUEL"), "=", Boolean.FALSE);
200
                        w = w.and(new Where(tableArt.getField("OBSOLETE"), "=", Boolean.FALSE));
201
                        input.setWhere(w);
202
                        System.err.println(input.asString());
203
                        return input;
204
                    }
205
                }, 0);
206
                List<SQLRowValues> resultArt = fetcherArt.fetch();
207
 
208
                for (SQLRowValues sqlRowValues : resultArt) {
209
                    final int idArt = sqlRowValues.getID();
210
                    if (!mapStockSnap.containsKey(idArt)) {
211
                        mapStockSnap.put(idArt, new EtatStock(sqlRowValues));
212
                    }
213
                }
214
 
215
            }
216
 
185 ilm 217
            SQLInsert insertEtat = new SQLInsert();
218
            insertEtat.add(tableEtatStock.getField("DATE"), d);
219
            insertEtat.add(tableEtatStock.getField("MONTANT_HA"), totalHT);
220
            insertEtat.add(tableEtatStock.getField("ID_DEPOT_STOCK"), depot.getID());
156 ilm 221
 
222
            try {
185 ilm 223
                int idEtat = SQLInsert.executeSimilarInserts(tableEtatStock.getDBSystemRoot(), Arrays.asList(insertEtat), true).get(0).intValue();
224
 
225
                List<SQLInsert> inserts = new ArrayList<SQLInsert>();
226
                final SQLTable tableElt = tableEtatStock.getTable("ETAT_STOCK_ELEMENT");
227
                for (EtatStock etatItem : mapStockSnap.values()) {
228
                    SQLInsert insertItem = new SQLInsert();
229
                    insertItem.add(tableElt.getField("ID_ETAT_STOCK"), idEtat);
230
                    insertItem.add(tableElt.getField("PA"), etatItem.getPa());
231
                    insertItem.add(tableElt.getField("PV"), etatItem.getPv());
232
                    insertItem.add(tableElt.getField("QTE"), etatItem.getQte());
233
                    insertItem.add(tableElt.getField("T_PA"), etatItem.getTotalPA());
234
                    insertItem.add(tableElt.getField("T_PV"), etatItem.getTotalPV());
235
                    insertItem.add(tableElt.getField("CODE"), etatItem.getArticle().getString("CODE"));
236
                    insertItem.add(tableElt.getField("NOM"), etatItem.getArticle().getString("NOM"));
237
                    insertItem.add(tableElt.getField("ID_ARTICLE"), etatItem.getArticle().getID());
238
                    inserts.add(insertItem);
239
                }
240
                if (!inserts.isEmpty()) {
241
                    // TODO voir avec Guillaume si il faut spliter par tranche de 1000
242
                    SQLInsert.executeSimilarInserts(tableElt.getDBSystemRoot(), inserts, false);
243
                }
244
                return idEtat;
156 ilm 245
            } catch (SQLException e) {
246
                ExceptionHandler.handle("Erreur lors de la création de l'état", e);
185 ilm 247
                return SQLRow.NONEXISTANT_ID;
156 ilm 248
            }
249
        }
250
    }
251
 
252
}