OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Rev 156 | 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;
93 ilm 17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.utils.DecimalUtils;
21
 
22
import java.math.BigDecimal;
23
import java.math.RoundingMode;
24
import java.util.ArrayList;
25
import java.util.Calendar;
26
import java.util.Collection;
27
import java.util.Date;
28
import java.util.List;
29
 
30
public class ProductComponent {
31
    private final SQLRowAccessor product;
32
    private BigDecimal qty;
132 ilm 33
    private final ProductHelper helper;
93 ilm 34
 
35
    public ProductComponent(SQLRowAccessor product, BigDecimal qty) {
36
        this.product = product;
37
        this.qty = qty;
132 ilm 38
        this.helper = new ProductHelper(product.getTable().getDBRoot());
93 ilm 39
    }
40
 
41
    public SQLRowAccessor getProduct() {
42
        return product;
43
    }
44
 
45
    public BigDecimal getQty() {
46
        return qty;
47
    }
48
 
49
    public void setQty(BigDecimal qty) {
50
        this.qty = qty;
51
    }
52
 
53
    public void addQty(BigDecimal b) {
54
        this.qty = qty.add(b);
55
    }
56
 
57
    /**
58
     * permet de valoriser les mouvements de stocks
59
     *
60
     * @return
61
     */
62
    public BigDecimal getPRC(Date d) {
63
        if (product.getTable().getDBRoot().contains("ARTICLE_PRIX_REVIENT")) {
132 ilm 64
            BigDecimal result = null;
65
            {
66
                SQLTable table = product.getTable().getDBRoot().getTable("ARTICLE_PRIX_REVIENT");
67
                Collection<SQLRow> prcs = product.asRow().getReferentRows(table);
93 ilm 68
 
132 ilm 69
                Date lastDate = null;
70
                final List<PriceByQty> prices = new ArrayList<PriceByQty>();
71
                for (SQLRow row : prcs) {
72
                    Calendar date = Calendar.getInstance();
73
                    if (row.getObject("DATE") != null) {
74
                        date = row.getDate("DATE");
75
                        lastDate = date.getTime();
76
                    }
77
                    prices.add(new PriceByQty(row.getLong("QTE"), row.getBigDecimal("PRIX"), date.getTime()));
93 ilm 78
                }
132 ilm 79
 
80
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
81
                if (result == null) {
82
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
83
                }
93 ilm 84
            }
132 ilm 85
            if (result == null) {
86
                SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
87
                Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
93 ilm 88
 
132 ilm 89
                Date lastDateATF = null;
90
                final List<PriceByQty> pricesATF = new ArrayList<PriceByQty>();
91
                for (SQLRow row : atfs) {
92
                    Calendar date = Calendar.getInstance();
93
                    if (row.getObject("DATE_PRIX") != null) {
94
                        date = row.getDate("DATE_PRIX");
95
                        lastDateATF = date.getTime();
96
                    }
97
                    pricesATF.add(new PriceByQty(row.getLong("QTE"), this.helper.getEnumPrice(row, SupplierPriceField.COEF_TRANSPORT_SIEGE), date.getTime()));
98
                }
99
 
100
                result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, d);
101
                if (result == null) {
102
                    result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), pricesATF, lastDateATF);
103
                    if (result == null) {
104
                        // Can occur during editing
105
                        result = BigDecimal.ZERO;
106
                    }
107
                }
93 ilm 108
            }
132 ilm 109
 
93 ilm 110
            return result;
111
        }
112
        return null;
113
    }
114
 
115
    public static ProductComponent createFrom(SQLRowAccessor rowVals) {
116
        return createFrom(rowVals, 1);
117
    }
118
 
119
    public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple) {
120
 
132 ilm 121
        final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
122
        final int qte = qteMult * qteMultiple;
93 ilm 123
        final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
124
        BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
125
        return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
126
    }
127
}