OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Rev 144 | 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
 
39
    public static BigDecimal getPriceForQty(int qty, List<PriceByQty> list, Date d) {
83 ilm 40
        BigDecimal result = null;
41
        Collections.sort(list, new Comparator<PriceByQty>() {
42
 
43
            @Override
44
            public int compare(PriceByQty o1, PriceByQty o2) {
45
 
46
                final int i = (int) (o1.qty - o2.qty);
47
                if (i != 0) {
48
                    return i;
49
                }
50
                return o1.startDate.compareTo(o2.startDate);
51
            }
52
        });
53
        for (PriceByQty priceByQty : list) {
54
            if (priceByQty.qty > qty) {
55
                break;
56
            }
132 ilm 57
            if (result == null || priceByQty.startDate.before(d)) {
83 ilm 58
                result = priceByQty.price;
59
            }
60
        }
61
        return result;
62
    }
63
 
64
    public static void main(String[] args) {
65
        List<PriceByQty> l = new ArrayList<PriceByQty>();
66
        final long currentTimeMillis = System.currentTimeMillis();
67
        l.add(new PriceByQty(50, new BigDecimal(11), new Date(currentTimeMillis)));
68
        l.add(new PriceByQty(1, new BigDecimal(14), new Date(currentTimeMillis + 2000)));
69
        l.add(new PriceByQty(1, new BigDecimal(13), new Date(currentTimeMillis - 5000)));
70
        l.add(new PriceByQty(1, new BigDecimal(12), new Date(currentTimeMillis)));
71
 
72
        System.out.println(getPriceForQty(0, l));
73
        System.out.println(getPriceForQty(1, l));
74
        System.out.println(getPriceForQty(49, l));
75
        System.out.println(getPriceForQty(50, l));
76
    }
77
}