KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MTax


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Tax Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MTax.java,v 1.2 2003/10/17 06:15:20 jjanke Exp $
27  */

28 public class MTax extends X_C_Tax
29 {
30     /**
31      * Standard Constructor
32      * @param ctx
33      * @param C_Tax_ID
34      */

35     public MTax (Properties ctx, int C_Tax_ID)
36     {
37         super (ctx, C_Tax_ID);
38         if (C_Tax_ID == 0)
39         {
40         // setC_Tax_ID (0); PK
41
setIsDefault (false);
42             setIsDocumentLevel (true);
43             setIsSummary (false);
44             setIsTaxExempt (false);
45         // setName (null);
46
setRate (Env.ZERO);
47             setRequiresTaxCertificate (false);
48         // setC_TaxCategory_ID (0); // FK
49
setSOPOType (SOPOTYPE_Both);
50             setValidFrom (TimeUtil.getDay(1,1,1990));
51         }
52     } // MTax
53

54     /**
55      * Load Constructor
56      * @param ctx context
57      * @param rs result set
58      */

59     public MTax (Properties ctx, ResultSet rs)
60     {
61         super (ctx, rs);
62     } // MTax
63

64     /**
65      * New Constructor
66      * @param ctx
67      * @param Name
68      * @param Rate
69      * @param C_TaxCategory_ID
70      */

71     public MTax (Properties ctx, String JavaDoc Name, BigDecimal Rate, int C_TaxCategory_ID)
72     {
73         this (ctx, 0);
74         setName (Name);
75         setRate (Rate == null ? Env.ZERO : Rate);
76         setC_TaxCategory_ID (C_TaxCategory_ID); // FK
77
} // MTax
78

79     private static BigDecimal ONEHUNDRED = new BigDecimal(100);
80
81     /**
82      * Calculate Tax - no rounding
83      * @param amount amount
84      * @param fromNet if true tax is calculated from net otherwise from gross
85      * @param scale scale
86      * @return tax amount
87      */

88     public BigDecimal calculateTax (BigDecimal amount, boolean fromNet, int scale)
89     {
90         // Null Tax
91
if (getRate().compareTo(Env.ZERO) == 0)
92             return Env.ZERO;
93         
94         double multiplier = getRate().doubleValue() / 100;
95
96         BigDecimal tax = null;
97         if (fromNet) // $100 * 6 / 100 == $6 == $100 * 0.06
98
{
99             double result = amount.doubleValue() * multiplier;
100             tax = new BigDecimal(result);
101         }
102         else // $106 - ($106 / (100+6)/100) == $6 == $106 - ($106/1.06)
103
{
104             multiplier += 1;
105             double base = amount.doubleValue() / multiplier;
106             tax = amount.subtract(new BigDecimal(base));
107         }
108         tax = tax.setScale(scale, BigDecimal.ROUND_HALF_UP);
109         log.debug ("calculateTax " + amount + " (net=" + fromNet + " mult=" + multiplier + ") = " + tax);
110         return tax;
111     } // getTaxFromNet
112

113
114 } // MTax
115
Popular Tags