OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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