OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 177
Line 13... Line 13...
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.SQLRowListRSH;
19
import org.openconcerto.sql.model.SQLRowListRSH;
19
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.SQLTable;
Line 25... Line 26...
25
import org.openconcerto.utils.ExceptionHandler;
26
import org.openconcerto.utils.ExceptionHandler;
26
import org.openconcerto.utils.cc.ITransformer;
27
import org.openconcerto.utils.cc.ITransformer;
27
 
28
 
28
import java.math.BigDecimal;
29
import java.math.BigDecimal;
29
import java.sql.SQLException;
30
import java.sql.SQLException;
30
import java.util.Calendar;
-
 
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
 
41
 
41
    public EtatStockSnapshotCreator(Date d, DBRoot root) {
42
    public EtatStockSnapshotCreator(SQLRowAccessor depot, Date d, DBRoot root) {
42
        this.d = d;
43
        this.d = d;
-
 
44
        this.depot = depot;
43
        this.root = root;
45
        this.root = root;
44
    }
46
    }
45
 
47
 
46
    public void create() {
48
    public void create() {
47
        // Récupération des inventaires
49
        // Récupération des inventaires
48
        SQLTable tableEtatStock = this.root.getTable("ETAT_STOCK");
50
        SQLTable tableEtatStock = this.root.getTable("ETAT_STOCK");
49
        SQLSelect sel = new SQLSelect();
51
        SQLSelect sel = new SQLSelect();
50
        sel.addSelectStar(tableEtatStock);
52
        sel.addSelectStar(tableEtatStock);
51
        Where wEtat = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
53
        Where wEtat = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
-
 
54
        wEtat = wEtat.and(new Where(tableEtatStock.getField("ID_DEPOT_STOCK"), "=", this.depot.getID()));        
52
        sel.setWhere(wEtat);
55
        sel.setWhere(wEtat);
53
        List<SQLRow> rowsEtatStock = SQLRowListRSH.execute(sel);
56
        List<SQLRow> rowsEtatStock = SQLRowListRSH.execute(sel);
54
        Map<Integer, Integer> mapEtatStock = new HashMap<Integer, Integer>();
57
        Map<Integer, Integer> mapEtatStock = new HashMap<Integer, Integer>();
55
        for (SQLRow sqlRow : rowsEtatStock) {
58
        for (SQLRow sqlRow : rowsEtatStock) {
56
            SQLTable tableMvtStock = this.root.getTable("MOUVEMENT_STOCK");
59
            SQLTable tableMvtStock = this.root.getTable("MOUVEMENT_STOCK");
-
 
60
            SQLTable tableStock = this.root.getTable("STOCK");
57
            SQLSelect selMvt = new SQLSelect();
61
            SQLSelect selMvt = new SQLSelect();
58
            selMvt.addSelect(tableMvtStock.getKey(), "MIN");
62
            selMvt.addSelect(tableMvtStock.getKey(), "MIN");
59
            Where wMvt = new Where(tableMvtStock.getField("OUVERTURE"), "=", Boolean.TRUE);
63
            Where wMvt = new Where(tableMvtStock.getField("OUVERTURE"), "=", Boolean.TRUE);
60
            wMvt = new Where(tableMvtStock.getField("ID_ETAT_STOCK"), "=", sqlRow.getID());
64
            wMvt = wMvt.and(new Where(tableMvtStock.getField("ID_ETAT_STOCK"), "=", sqlRow.getID()));
-
 
65
            wMvt = wMvt.and(new Where(tableMvtStock.getField("ID_STOCK"), "=", tableStock.getKey()));
-
 
66
            wMvt = wMvt.and(new Where(tableStock.getField("ID_DEPOT_STOCK"), "=", depot.getID()));           
61
            selMvt.setWhere(wMvt);
67
            selMvt.setWhere(wMvt);
62
            Integer idMvt = (Integer) tableMvtStock.getDBSystemRoot().getDataSource().executeScalar(selMvt.asString());
68
            Integer idMvt = (Integer) tableMvtStock.getDBSystemRoot().getDataSource().executeScalar(selMvt.asString());
63
            if (idMvt != null) {
69
            if (idMvt != null) {
64
                mapEtatStock.put(sqlRow.getID(), idMvt);
70
                mapEtatStock.put(sqlRow.getID(), idMvt);
65
            }
71
            }
Line 74... Line 80...
74
            vals.put("QTE", null);
80
            vals.put("QTE", null);
75
            if (tableStock.contains("PRICE")) {
81
            if (tableStock.contains("PRICE")) {
76
                vals.put("PRICE", null);
82
                vals.put("PRICE", null);
77
            }
83
            }
78
            vals.put("ID_ARTICLE", null);
84
            vals.put("ID_ARTICLE", null);
-
 
85
            vals.putRowValues("ID_STOCK").putNulls("QTE_REEL").putRowValues("ID_DEPOT_STOCK").putNulls("ID", "NOM", "CODE");
-
 
86
            
79
            // Calendar cal0116 = Calendar.getInstance();
87
            // Calendar cal0116 = Calendar.getInstance();
80
            // cal0116.set(2016, Calendar.JANUARY, 1, 0, 0, 0);
88
            // cal0116.set(2016, Calendar.JANUARY, 1, 0, 0, 0);
81
            // final Date dateDeb = cal0116.getTime();
89
            // final Date dateDeb = cal0116.getTime();
82
 
90
 
83
            // Récupération du dernier etat de stock
91
            // Récupération du dernier etat de stock
84
            SQLSelect selEtatD = new SQLSelect();
92
            SQLSelect selEtatD = new SQLSelect();
85
            selEtatD.addSelectStar(tableEtatStock);
93
            selEtatD.addSelectStar(tableEtatStock);
86
            Where wEtatD = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
94
            Where wEtatD = new Where(tableEtatStock.getField("INVENTAIRE"), "=", Boolean.TRUE);
-
 
95
            wEtatD = wEtatD.and(new Where(tableEtatStock.getField("ID_DEPOT_STOCK"), "=", this.depot.getID()));            
87
            selEtatD.setWhere(wEtatD);
96
            selEtatD.setWhere(wEtatD);
88
            List<SQLRow> rowsEtatStockD = SQLRowListRSH.execute(selEtatD);
97
            List<SQLRow> rowsEtatStockD = SQLRowListRSH.execute(selEtatD);
89
            SQLRow rowEtatStockDeb = null;
98
            SQLRow rowEtatStockDeb = null;
90
            for (SQLRow sqlRow : rowsEtatStockD) {
99
            for (SQLRow sqlRow : rowsEtatStockD) {
91
 
100
 
Line 118... Line 127...
118
                        w = w.and(new Where(tableStock.getField("DATE"), ">=", dateDeb));
127
                        w = w.and(new Where(tableStock.getField("DATE"), ">=", dateDeb));
119
                        w = w.and(new Where(tableStock.getKey(), ">=", idMvtStockDeb));
128
                        w = w.and(new Where(tableStock.getKey(), ">=", idMvtStockDeb));
120
                        w = w.and(new Where(tableStock.getField("CLOTURE"), "!=", Boolean.TRUE));
129
                        w = w.and(new Where(tableStock.getField("CLOTURE"), "!=", Boolean.TRUE));
121
                    }
130
                    }
122
                    w = w.and(new Where(tableStock.getField("REEL"), "=", Boolean.TRUE));
131
                    w = w.and(new Where(tableStock.getField("REEL"), "=", Boolean.TRUE));
-
 
132
                    w = w.and(new Where(sel.getJoin(tableStock.getField("ID_STOCK")).getJoinedTable().getField("ID_DEPOT_STOCK"), "=", depot.getID()));
123
 
133
 
124
                    sel.setWhere(w);
134
                    sel.setWhere(w);
125
                    return sel;
135
                    return sel;
126
                }
136
                }
127
            });
137
            });
Line 148... Line 158...
148
            }
158
            }
149
 
159
 
150
            SQLRowValues rowVals = new SQLRowValues(tableEtatStock);
160
            SQLRowValues rowVals = new SQLRowValues(tableEtatStock);
151
            rowVals.put("DATE", d);
161
            rowVals.put("DATE", d);
152
            rowVals.put("MONTANT_HA", totalHT);
162
            rowVals.put("MONTANT_HA", totalHT);
-
 
163
            rowVals.put("ID_DEPOT_STOCK", depot.getID());
153
 
164
 
154
            for (EtatStock etatItem : mapStockSnap.values()) {
165
            for (EtatStock etatItem : mapStockSnap.values()) {
155
                SQLRowValues rowValsItem = new SQLRowValues(tableEtatStock.getTable("ETAT_STOCK_ELEMENT"));
166
                SQLRowValues rowValsItem = new SQLRowValues(tableEtatStock.getTable("ETAT_STOCK_ELEMENT"));
156
                rowValsItem.put("ID_ETAT_STOCK", rowVals);
167
                rowValsItem.put("ID_ETAT_STOCK", rowVals);
157
                rowValsItem.put("PA", etatItem.getPa());
168
                rowValsItem.put("PA", etatItem.getPa());