OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 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
 
16
import java.math.BigDecimal;
17
import java.util.ArrayList;
18
import java.util.Collections;
19
import java.util.Comparator;
20
import java.util.Date;
21
import java.util.List;
22
 
23
public class PriceByQty {
24
 
25
    private long qty;
26
    private BigDecimal price;
27
    private Date startDate;
28
 
29
    public PriceByQty(long qty, BigDecimal price, Date startDate) {
30
        this.qty = qty;
31
        this.price = price;
32
        this.startDate = startDate;
33
    }
34
 
35
    public static BigDecimal getPriceForQty(int qty, List<PriceByQty> list) {
93 ilm 36
        return getPriceForQty(qty, list, new Date(System.currentTimeMillis()));
37
    }
38
 
144 ilm 39
    public BigDecimal getPrice() {
40
        return price;
41
    }
42
 
93 ilm 43
    public static BigDecimal getPriceForQty(int qty, List<PriceByQty> list, Date d) {
83 ilm 44
        BigDecimal result = null;
45
        Collections.sort(list, new Comparator<PriceByQty>() {
46
 
47
            @Override
48
            public int compare(PriceByQty o1, PriceByQty o2) {
49
 
50
                final int i = (int) (o1.qty - o2.qty);
51
                if (i != 0) {
52
                    return i;
53
                }
54
                return o1.startDate.compareTo(o2.startDate);
55
            }
56
        });
57
        for (PriceByQty priceByQty : list) {
58
            if (priceByQty.qty > qty) {
59
                break;
60
            }
132 ilm 61
            if (result == null || priceByQty.startDate.before(d)) {
83 ilm 62
                result = priceByQty.price;
63
            }
64
        }
65
        return result;
66
    }
67
 
68
    public static void main(String[] args) {
69
        List<PriceByQty> l = new ArrayList<PriceByQty>();
70
        final long currentTimeMillis = System.currentTimeMillis();
71
        l.add(new PriceByQty(50, new BigDecimal(11), new Date(currentTimeMillis)));
72
        l.add(new PriceByQty(1, new BigDecimal(14), new Date(currentTimeMillis + 2000)));
73
        l.add(new PriceByQty(1, new BigDecimal(13), new Date(currentTimeMillis - 5000)));
74
        l.add(new PriceByQty(1, new BigDecimal(12), new Date(currentTimeMillis)));
75
 
76
        System.out.println(getPriceForQty(0, l));
77
        System.out.println(getPriceForQty(1, l));
78
        System.out.println(getPriceForQty(49, l));
79
        System.out.println(getPriceForQty(50, l));
80
    }
81
}