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 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.sales.product.model;
15
 
132 ilm 16
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
156 ilm 17
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
93 ilm 18
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRowAccessor;
156 ilm 20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
93 ilm 22
import org.openconcerto.sql.model.SQLTable;
156 ilm 23
import org.openconcerto.sql.model.Where;
93 ilm 24
import org.openconcerto.utils.DecimalUtils;
156 ilm 25
import org.openconcerto.utils.ExceptionHandler;
93 ilm 26
 
27
import java.math.BigDecimal;
28
import java.math.RoundingMode;
156 ilm 29
import java.sql.SQLException;
93 ilm 30
import java.util.ArrayList;
31
import java.util.Calendar;
32
import java.util.Collection;
33
import java.util.Date;
34
import java.util.List;
35
 
36
public class ProductComponent {
37
    private final SQLRowAccessor product;
156 ilm 38
    private final SQLRowAccessor source;
39
    private final SQLRowAccessor stock;
93 ilm 40
    private BigDecimal qty;
132 ilm 41
    private final ProductHelper helper;
93 ilm 42
 
156 ilm 43
    public ProductComponent(SQLRowAccessor product, BigDecimal qty, SQLRowAccessor source, SQLRowAccessor stock) {
93 ilm 44
        this.product = product;
45
        this.qty = qty;
132 ilm 46
        this.helper = new ProductHelper(product.getTable().getDBRoot());
156 ilm 47
        this.source = source;
48
        this.stock = stock;
93 ilm 49
    }
50
 
51
    public SQLRowAccessor getProduct() {
52
        return product;
53
    }
54
 
156 ilm 55
    public SQLRowAccessor getSource() {
56
        return source;
57
    }
58
 
59
    public SQLRowAccessor getStock() {
60
        return stock;
61
    }
62
 
93 ilm 63
    public BigDecimal getQty() {
64
        return qty;
65
    }
66
 
67
    public void setQty(BigDecimal qty) {
68
        this.qty = qty;
69
    }
70
 
71
    public void addQty(BigDecimal b) {
72
        this.qty = qty.add(b);
73
    }
74
 
75
    /**
76
     * permet de valoriser les mouvements de stocks
77
     *
78
     * @return
79
     */
80
    public BigDecimal getPRC(Date d) {
81
        if (product.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
132 ilm 82
            BigDecimal result = null;
83
            {
84
                SQLTable table = product.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
85
                Collection<SQLRow> prcs = product.asRow().getReferentRows(table);
93 ilm 86
 
132 ilm 87
                Date lastDate = null;
88
                final List<PriceByQty> prices = new ArrayList<PriceByQty>();
89
                for (SQLRow row : prcs) {
90
                    Calendar date = Calendar.getInstance();
91
                    if (row.getObject("DATE") != null) {
92
                        date = row.getDate("DATE");
93
                        lastDate = date.getTime();
94
                    }
95
                    prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
93 ilm 96
                }
132 ilm 97
 
98
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
99
                if (result == null) {
100
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
156 ilm 101
                } else if (prices.size() > 0) {
102
                    result = prices.get(0).getPrice();
132 ilm 103
                }
93 ilm 104
            }
132 ilm 105
            if (result == null) {
106
                SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
107
                Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
93 ilm 108
 
132 ilm 109
                Date lastDateATF = null;
110
                final List<PriceByQty> pricesATF = new ArrayList<PriceByQty>();
111
                for (SQLRow row : atfs) {
112
                    Calendar date = Calendar.getInstance();
113
                    if (row.getObject("DATE_PRIX") != null) {
114
                        date = row.getDate("DATE_PRIX");
115
                        lastDateATF = date.getTime();
116
                    }
117
                    pricesATF.add(new PriceByQty(row.getLong("QTE"), this.helper.getEnumPrice(row, SupplierPriceField.COEF_TRANSPORT_SIEGE), date.getTime()));
118
                }
119
 
120
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, d);
121
                if (result == null) {
122
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, lastDateATF);
123
                    if (result == null) {
124
                        // Can occur during editing
125
                        result = BigDecimal.ZERO;
126
                    }
127
                }
93 ilm 128
            }
132 ilm 129
 
93 ilm 130
            return result;
131
        }
132
        return null;
133
    }
134
 
156 ilm 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
 
93 ilm 141
    public static ProductComponent createFrom(SQLRowAccessor rowVals) {
156 ilm 142
        return createFrom(rowVals, 1, rowVals);
93 ilm 143
    }
144
 
156 ilm 145
    public static ProductComponent createFrom(SQLRowAccessor rowVals, SQLRowAccessor rowValsSource) {
146
        return createFrom(rowVals, 1, rowValsSource);
147
    }
93 ilm 148
 
156 ilm 149
    public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple, SQLRowAccessor rowValsSource) {
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
        }
132 ilm 154
        final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
155
        final int qte = qteMult * qteMultiple;
93 ilm 156
        final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
157
        BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
156 ilm 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);
165
        // return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
93 ilm 166
    }
156 ilm 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;
217
    }
93 ilm 218
}