OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 132 Rev 156
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 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.sales.product.model;
14
 package org.openconcerto.erp.core.sales.product.model;
15
 
15
 
16
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
16
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
-
 
17
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
20
import org.openconcerto.sql.model.SQLRowValues;
-
 
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
19
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.SQLTable;
-
 
23
import org.openconcerto.sql.model.Where;
20
import org.openconcerto.utils.DecimalUtils;
24
import org.openconcerto.utils.DecimalUtils;
-
 
25
import org.openconcerto.utils.ExceptionHandler;
21
 
26
 
22
import java.math.BigDecimal;
27
import java.math.BigDecimal;
23
import java.math.RoundingMode;
28
import java.math.RoundingMode;
-
 
29
import java.sql.SQLException;
24
import java.util.ArrayList;
30
import java.util.ArrayList;
25
import java.util.Calendar;
31
import java.util.Calendar;
26
import java.util.Collection;
32
import java.util.Collection;
27
import java.util.Date;
33
import java.util.Date;
28
import java.util.List;
34
import java.util.List;
29
 
35
 
30
public class ProductComponent {
36
public class ProductComponent {
31
    private final SQLRowAccessor product;
37
    private final SQLRowAccessor product;
-
 
38
    private final SQLRowAccessor source;
-
 
39
    private final SQLRowAccessor stock;
32
    private BigDecimal qty;
40
    private BigDecimal qty;
33
    private final ProductHelper helper;
41
    private final ProductHelper helper;
34
 
42
 
35
    public ProductComponent(SQLRowAccessor product, BigDecimal qty) {
43
    public ProductComponent(SQLRowAccessor product, BigDecimal qty, SQLRowAccessor source, SQLRowAccessor stock) {
36
        this.product = product;
44
        this.product = product;
37
        this.qty = qty;
45
        this.qty = qty;
38
        this.helper = new ProductHelper(product.getTable().getDBRoot());
46
        this.helper = new ProductHelper(product.getTable().getDBRoot());
-
 
47
        this.source = source;
-
 
48
        this.stock = stock;
39
    }
49
    }
40
 
50
 
41
    public SQLRowAccessor getProduct() {
51
    public SQLRowAccessor getProduct() {
42
        return product;
52
        return product;
43
    }
53
    }
44
 
54
 
-
 
55
    public SQLRowAccessor getSource() {
-
 
56
        return source;
-
 
57
    }
-
 
58
 
-
 
59
    public SQLRowAccessor getStock() {
-
 
60
        return stock;
-
 
61
    }
-
 
62
 
45
    public BigDecimal getQty() {
63
    public BigDecimal getQty() {
46
        return qty;
64
        return qty;
47
    }
65
    }
48
 
66
 
49
    public void setQty(BigDecimal qty) {
67
    public void setQty(BigDecimal qty) {
50
        this.qty = qty;
68
        this.qty = qty;
51
    }
69
    }
52
 
70
 
53
    public void addQty(BigDecimal b) {
71
    public void addQty(BigDecimal b) {
54
        this.qty = qty.add(b);
72
        this.qty = qty.add(b);
55
    }
73
    }
56
 
74
 
57
    /**
75
    /**
58
     * permet de valoriser les mouvements de stocks
76
     * permet de valoriser les mouvements de stocks
59
     * 
77
     * 
60
     * @return
78
     * @return
61
     */
79
     */
62
    public BigDecimal getPRC(Date d) {
80
    public BigDecimal getPRC(Date d) {
63
        if (product.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
81
        if (product.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
64
            BigDecimal result = null;
82
            BigDecimal result = null;
65
            {
83
            {
66
                SQLTable table = product.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
84
                SQLTable table = product.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
67
                Collection<SQLRow> prcs = product.asRow().getReferentRows(table);
85
                Collection<SQLRow> prcs = product.asRow().getReferentRows(table);
68
 
86
 
69
                Date lastDate = null;
87
                Date lastDate = null;
70
                final List<PriceByQty> prices = new ArrayList<PriceByQty>();
88
                final List<PriceByQty> prices = new ArrayList<PriceByQty>();
71
                for (SQLRow row : prcs) {
89
                for (SQLRow row : prcs) {
72
                    Calendar date = Calendar.getInstance();
90
                    Calendar date = Calendar.getInstance();
73
                    if (row.getObject("DATE") != null) {
91
                    if (row.getObject("DATE") != null) {
74
                        date = row.getDate("DATE");
92
                        date = row.getDate("DATE");
75
                        lastDate = date.getTime();
93
                        lastDate = date.getTime();
76
                    }
94
                    }
77
                    prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
95
                    prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
78
                }
96
                }
79
 
97
 
80
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
98
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
81
                if (result == null) {
99
                if (result == null) {
82
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
100
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
-
 
101
                } else if (prices.size() > 0) {
-
 
102
                    result = prices.get(0).getPrice();
83
                }
103
                }
84
            }
104
            }
85
            if (result == null) {
105
            if (result == null) {
86
                SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
106
                SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
87
                Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
107
                Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
88
 
108
 
89
                Date lastDateATF = null;
109
                Date lastDateATF = null;
90
                final List<PriceByQty> pricesATF = new ArrayList<PriceByQty>();
110
                final List<PriceByQty> pricesATF = new ArrayList<PriceByQty>();
91
                for (SQLRow row : atfs) {
111
                for (SQLRow row : atfs) {
92
                    Calendar date = Calendar.getInstance();
112
                    Calendar date = Calendar.getInstance();
93
                    if (row.getObject("DATE_PRIX") != null) {
113
                    if (row.getObject("DATE_PRIX") != null) {
94
                        date = row.getDate("DATE_PRIX");
114
                        date = row.getDate("DATE_PRIX");
95
                        lastDateATF = date.getTime();
115
                        lastDateATF = date.getTime();
96
                    }
116
                    }
97
                    pricesATF.add(new PriceByQty(row.getLong("QTE"), this.helper.getEnumPrice(row, SupplierPriceField.COEF_TRANSPORT_SIEGE), date.getTime()));
117
                    pricesATF.add(new PriceByQty(row.getLong("QTE"), this.helper.getEnumPrice(row, SupplierPriceField.COEF_TRANSPORT_SIEGE), date.getTime()));
98
                }
118
                }
99
 
119
 
100
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, d);
120
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, d);
101
                if (result == null) {
121
                if (result == null) {
102
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, lastDateATF);
122
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, lastDateATF);
103
                    if (result == null) {
123
                    if (result == null) {
104
                        // Can occur during editing
124
                        // Can occur during editing
105
                        result = BigDecimal.ZERO;
125
                        result = BigDecimal.ZERO;
106
                    }
126
                    }
107
                }
127
                }
108
            }
128
            }
109
 
129
 
110
            return result;
130
            return result;
111
        }
131
        }
112
        return null;
132
        return null;
113
    }
133
    }
114
 
134
 
-
 
135
    public static ProductComponent createFromRowArticle(SQLRowAccessor rowArticle, SQLRowAccessor rowValsSource) {
-
 
136
        SQLRowAccessor rowStock = getStock(rowArticle, rowArticle, rowValsSource);
-
 
137
 
-
 
138
        return new ProductComponent(rowArticle, BigDecimal.ONE, rowValsSource, rowStock);
-
 
139
    }
-
 
140
 
115
    public static ProductComponent createFrom(SQLRowAccessor rowVals) {
141
    public static ProductComponent createFrom(SQLRowAccessor rowVals) {
116
        return createFrom(rowVals, 1);
142
        return createFrom(rowVals, 1, rowVals);
-
 
143
    }
-
 
144
 
-
 
145
    public static ProductComponent createFrom(SQLRowAccessor rowVals, SQLRowAccessor rowValsSource) {
-
 
146
        return createFrom(rowVals, 1, rowValsSource);
117
    }
147
    }
118
 
148
 
119
    public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple) {
149
    public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple, SQLRowAccessor rowValsSource) {
120
 
150
 
-
 
151
        if (rowVals.getForeign("ID_ARTICLE") == null || rowVals.isForeignEmpty("ID_ARTICLE")) {
-
 
152
            throw new IllegalArgumentException("Aucun article associé à la row " + rowVals.getTable().getName() + " " + rowVals.getID());
-
 
153
        }
121
        final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
154
        final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
122
        final int qte = qteMult * qteMultiple;
155
        final int qte = qteMult * qteMultiple;
123
        final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
156
        final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
124
        BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
157
        BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
-
 
158
        SQLRowAccessor rowStock = getStock(rowVals.getForeign("ID_ARTICLE"), rowVals, rowValsSource);
-
 
159
 
-
 
160
        // }
-
 
161
        // else {
-
 
162
        // rowStock = rowVals.getForeign("ID_ARTICLE").getForeign("ID_STOCK");
-
 
163
        // }
-
 
164
        return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal, rowValsSource, rowStock);
125
        return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
165
        // return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
-
 
166
    }
-
 
167
 
-
 
168
    private static SQLRowAccessor getStock(SQLRowAccessor rowValsProduct, SQLRowAccessor rowValsElt, SQLRowAccessor rowValsSource) {
-
 
169
        SQLRowAccessor rowStock = null;
-
 
170
        final int idDepot;
-
 
171
        if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
-
 
172
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
-
 
173
        } else {
-
 
174
            if (rowValsElt.getForeign("ID_DEPOT_STOCK") != null && !rowValsElt.isForeignEmpty("ID_DEPOT_STOCK")) {
-
 
175
                idDepot = rowValsElt.getForeignID("ID_DEPOT_STOCK");
-
 
176
            } else {
-
 
177
                idDepot = DepotStockSQLElement.DEFAULT_ID;
-
 
178
                try {
-
 
179
                    rowValsElt.createEmptyUpdateRow().put("ID_DEPOT_STOCK", idDepot).commit();
-
 
180
                } catch (SQLException e) {
-
 
181
                    ExceptionHandler.handle("Erreur lors de l'initialisation du stock!", e);
-
 
182
                }
-
 
183
 
-
 
184
            }
-
 
185
        }
-
 
186
 
-
 
187
        SQLTable stockTable = rowValsElt.getTable().getTable("STOCK");
-
 
188
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
-
 
189
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
-
 
190
 
-
 
191
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
-
 
192
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot);
-
 
193
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsProduct.getID());
-
 
194
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
-
 
195
        if (rowValsResult.size() == 0) {
-
 
196
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
-
 
197
            rowValsStock.put("ID_ARTICLE", rowValsProduct.getID());
-
 
198
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
-
 
199
            rowValsStock.put("QTE_TH", 0F);
-
 
200
            rowValsStock.put("QTE_REEL", 0F);
-
 
201
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
-
 
202
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
-
 
203
            try {
-
 
204
                rowStock = rowValsStock.insert();
-
 
205
                if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
-
 
206
                    rowValsProduct.createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
-
 
207
                }
-
 
208
            } catch (SQLException e) {
-
 
209
                ExceptionHandler.handle("Erreur lors la création du stock!", e);
-
 
210
            }
-
 
211
        } else if (rowValsResult.size() == 1) {
-
 
212
            rowStock = rowValsResult.iterator().next();
-
 
213
        } else if (rowValsResult.size() > 1) {
-
 
214
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsProduct.getID() + " Depot " + rowValsElt.getForeignID("ID_DEPOT_STOCK"));
-
 
215
        }
-
 
216
        return rowStock;
126
    }
217
    }
127
}
218
}