OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 90 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 90 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.utils;
14
 package org.openconcerto.erp.utils;
15
 
15
 
16
import org.openconcerto.utils.DecimalUtils;
16
import org.openconcerto.utils.DecimalUtils;
17
 
17
 
18
import java.math.BigDecimal;
18
import java.math.BigDecimal;
19
import java.math.RoundingMode;
19
import java.math.RoundingMode;
20
 
20
 
21
public class ConvertDevise {
21
public class ConvertDevise {
22
 
22
 
23
	/**
23
    /**
24
	 * Convertit un prix ht en ttc
24
     * Convertit un prix ht en ttc
25
	 * 
25
     * 
26
	 * @param ht
-
 
27
	 *            la valeur hors taxe à convertir
26
     * @param ht la valeur hors taxe à convertir
28
	 * @param taxe
-
 
29
	 *            taux de la tva à appliquer (ex : 7, 20)
27
     * @param taxe taux de la tva à appliquer (ex : 7, 20)
30
	 * @param scale
-
 
31
	 *            précision à appliquer sur le ttc retourné
28
     * @param scale précision à appliquer sur le ttc retourné
32
	 * @return le ttc avec la précision scale
29
     * @return le ttc avec la précision scale
33
	 */
30
     */
34
	public static final BigDecimal getTtcFromHt(BigDecimal ht, BigDecimal taxe,
31
    public static final BigDecimal getTtcFromHt(BigDecimal ht, BigDecimal taxe, int scale) {
35
			int scale) {
-
 
36
 
32
 
37
		BigDecimal tauxB = taxe.movePointLeft(2).add(BigDecimal.ONE);
33
        BigDecimal tauxB = taxe.movePointLeft(2).add(BigDecimal.ONE);
38
		BigDecimal result = ht.multiply(tauxB, DecimalUtils.HIGH_PRECISION)
34
        BigDecimal result = ht.multiply(tauxB, DecimalUtils.HIGH_PRECISION).setScale(scale, RoundingMode.HALF_UP);
39
				.setScale(scale, RoundingMode.HALF_UP);
-
 
40
		return result;
35
        return result;
41
 
36
 
42
	}
37
    }
43
 
38
 
44
	/**
39
    /**
45
	 * Convertit un prix ttc en ht
40
     * Convertit un prix ttc en ht
46
	 * 
41
     * 
47
	 * @param tts
-
 
48
	 *            la valeur tts à convertir
42
     * @param tts la valeur tts à convertir
49
	 * @param taxe
-
 
50
	 *            taux de la tva à appliquer (ex : 7, 20)
43
     * @param taxe taux de la tva à appliquer (ex : 7, 20)
51
	 * @param scale
-
 
52
	 *            précision à appliquer sur le ht retourné
44
     * @param scale précision à appliquer sur le ht retourné
53
	 * @return le ht avec la précision scale
45
     * @return le ht avec la précision scale
54
	 */
46
     */
55
	public static final BigDecimal getHtFromTtc(BigDecimal ttc,
47
    public static final BigDecimal getHtFromTtc(BigDecimal ttc, BigDecimal taxe, int scale) {
56
			BigDecimal taxe, int scale) {
-
 
57
		if (taxe.signum() == 0) {
48
        if (taxe.signum() == 0) {
58
			return ttc.setScale(scale, RoundingMode.HALF_UP);
49
            return ttc.setScale(scale, RoundingMode.HALF_UP);
59
		}
50
        }
60
 
51
 
61
		BigDecimal tauxB = taxe.movePointLeft(2).add(BigDecimal.ONE);
52
        BigDecimal tauxB = taxe.movePointLeft(2).add(BigDecimal.ONE);
62
        BigDecimal result = ttc.divide(tauxB, DecimalUtils.HIGH_PRECISION).setScale(
53
        BigDecimal result = ttc.divide(tauxB, DecimalUtils.HIGH_PRECISION).setScale(scale, RoundingMode.HALF_UP);
63
				scale, RoundingMode.HALF_UP);
-
 
64
		return result;
54
        return result;
65
 
55
 
66
	}
56
    }
67
 
57
 
68
	public static void main(String[] args) {
58
    public static void main(String[] args) {
69
 
59
 
70
		BigDecimal ttcFromHt = getTtcFromHt(BigDecimal.ONE, new BigDecimal(20),
60
        BigDecimal ttcFromHt = getTtcFromHt(BigDecimal.ONE, new BigDecimal(20), 6);
71
				6);
-
 
72
		System.err.println(ttcFromHt);
61
        System.err.println(ttcFromHt);
73
		System.err.println(getHtFromTtc(ttcFromHt, new BigDecimal(20), 6));
62
        System.err.println(getHtFromTtc(ttcFromHt, new BigDecimal(20), 6));
74
 
63
 
75
	}
64
    }
76
}
65
}