KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > util > UtilAccounting


1 /*
2  * $Id:$
3  *
4  * Copyright (c) 2003-2006 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * @author Si Chen (sichen@opensourcestrategies.com)
25  * @author Leon Torres (leon@opensourcestrategies.com)
26  */

27
28 package org.ofbiz.accounting.util;
29
30 import java.math.BigDecimal JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.accounting.AccountingException;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilMisc;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.ofbiz.entity.GenericValue;
40 import org.ofbiz.service.ServiceUtil;
41
42 public class UtilAccounting {
43
44     public static String JavaDoc module = UtilAccounting.class.getName();
45
46     /**
47      * Get the GL Account for a product or the default account type based on input. This replaces the simple-method service
48      * getProductOrgGlAccount. First it will look in ProductGlAccount using the primary keys productId and
49      * productGlAccountTypeId. If none is found, it will look up GlAccountTypeDefault to find the default account for
50      * organizationPartyId with type glAccountTypeId.
51      *
52      * @param productId When searching for ProductGlAccounts, specify the productId
53      * @param glAccountTypeId The default glAccountTypeId to look for if no ProductGlAccount is found
54      * @param organizationPartyId The organization party of the default account
55      * @return The account ID (glAccountId) found
56      * @throws AccountingException When the no accounts found or an entity exception occurs
57      */

58     public static String JavaDoc getProductOrgGlAccountId(String JavaDoc productId,
59             String JavaDoc glAccountTypeId, String JavaDoc organizationPartyId, GenericDelegator delegator)
60         throws AccountingException {
61
62         GenericValue account = null;
63         try {
64             // first try to find the account in ProductGlAccount
65
account = delegator.findByPrimaryKeyCache("ProductGlAccount",
66                     UtilMisc.toMap("productId", productId, "glAccountTypeId", glAccountTypeId, "organizationPartyId", organizationPartyId));
67         } catch (GenericEntityException e) {
68             throw new AccountingException("Failed to find a ProductGLAccount for productId [" + productId + "], organization [" + organizationPartyId + "], and productGlAccountTypeId [" + glAccountTypeId + "].", e);
69         }
70
71         // otherwise try the default accounts
72
if (account == null) {
73             try {
74                 account = delegator.findByPrimaryKeyCache("GlAccountTypeDefault", UtilMisc.toMap("glAccountTypeId", glAccountTypeId, "organizationPartyId", organizationPartyId));
75             } catch (GenericEntityException e) {
76                 throw new AccountingException("Failed to find a GlAccountTypeDefault for glAccountTypeId [" + glAccountTypeId + "] and organizationPartyId [" + organizationPartyId+ "].", e);
77             }
78         }
79
80         // if no results yet, serious problem
81
if (account == null) {
82             throw new AccountingException("Failed to find any accounts for productId [" + productId + "], organization [" + organizationPartyId + "], and productGlAccountTypeId [" + glAccountTypeId + "] or any accounts in GlAccountTypeDefault for glAccountTypeId [" + glAccountTypeId + "] and organizationPartyId [" + organizationPartyId+ "]. Please check your data to make sure that at least a GlAccountTypeDefault is defined for this account type and organization.");
83         }
84
85         // otherwise return the glAccountId
86
return account.getString("glAccountId");
87     }
88
89     /**
90      * As above, but explicitly looking for default account for given type and organization
91      *
92      * @param glAccountTypeId The type of account
93      * @param organizationPartyId The organization of the account
94      * @return The default account ID (glAccountId) for this type
95      * @throws AccountingException When the default is not configured
96      */

97     public static String JavaDoc getDefaultAccountId(String JavaDoc glAccountTypeId, String JavaDoc organizationPartyId, GenericDelegator delegator) throws AccountingException {
98         return getProductOrgGlAccountId(null, glAccountTypeId, organizationPartyId, delegator);
99     }
100
101     /**
102      * Little method to figure out the net or ending balance of a GlAccountHistory or GlAccountAndHistory value, based on what kind
103      * of account (DEBIT or CREDIT) it is
104      * @param account - GlAccountHistory or GlAccountAndHistory value
105      * @return balance - a Double
106      */

107     public static Double JavaDoc getNetBalance(GenericValue account, String JavaDoc debugModule) {
108         try {
109             GenericValue glAccount = account.getRelatedOne("GlAccount");
110             double balance = 0.0;
111             if (isDebitAccount(glAccount)) {
112                 balance = account.getDouble("postedDebits").doubleValue() - account.getDouble("postedCredits").doubleValue();
113             } else if (isCreditAccount(glAccount)) {
114                 balance = account.getDouble("postedCredits").doubleValue() - account.getDouble("postedDebits").doubleValue();
115             }
116             return new Double JavaDoc(balance);
117         } catch (GenericEntityException ex) {
118             Debug.logError(ex.getMessage(), debugModule);
119             return null;
120         }
121     }
122
123     /**
124      * Recurses up payment type tree via parentTypeId to see if input payment type ID is in tree.
125      */

126     private static boolean isPaymentTypeRecurse(GenericValue paymentType, String JavaDoc inputTypeId) throws GenericEntityException {
127
128         // first check the parentTypeId against inputTypeId
129
String JavaDoc parentTypeId = paymentType.getString("parentTypeId");
130         if (parentTypeId == null) {
131             return false;
132         }
133         if (parentTypeId.equals(inputTypeId)) {
134             return true;
135         }
136
137         // otherwise, we have to go to the grandparent (recurse)
138
return isPaymentTypeRecurse(paymentType.getRelatedOne("ParentPaymentType"), inputTypeId);
139     }
140
141
142     /**
143      * Checks if a payment is of a specified PaymentType.paymentTypeId. Return false if payment is null. It's better to use the
144      * more specific calls like isTaxPayment().
145      */

146     public static boolean isPaymentType(GenericValue payment, String JavaDoc inputTypeId) throws GenericEntityException {
147         if (payment == null) {
148             return false;
149         }
150
151         GenericValue paymentType = payment.getRelatedOneCache("PaymentType");
152         if (paymentType == null) {
153             throw new GenericEntityException("Cannot find PaymentType for paymentId " + payment.getString("paymentId"));
154         }
155
156         String JavaDoc paymentTypeId = paymentType.getString("paymentTypeId");
157         if (inputTypeId.equals(paymentTypeId)) {
158             return true;
159         }
160
161         // recurse up tree
162
return isPaymentTypeRecurse(paymentType, inputTypeId);
163     }
164
165
166     public static boolean isTaxPayment(GenericValue payment) throws GenericEntityException {
167         return isPaymentType(payment, "TAX_PAYMENT");
168     }
169
170     public static boolean isDisbursement(GenericValue payment) throws GenericEntityException {
171         return isPaymentType(payment, "DISBURSEMENT");
172     }
173
174     public static boolean isReceipt(GenericValue payment) throws GenericEntityException {
175         return isPaymentType(payment, "RECEIPT");
176     }
177
178
179     /**
180      * Determines if a glAccountClass is of a child of a certain parent glAccountClass.
181      */

182     public static boolean isAccountClassClass(GenericValue glAccountClass, String JavaDoc parentGlAccountClassId) throws GenericEntityException {
183         if (glAccountClass == null) return false;
184
185         // check current class against input classId
186
if (parentGlAccountClassId.equals(glAccountClass.get("glAccountClassId"))) {
187             return true;
188         }
189
190         // check parentClassId against inputClassId
191
String JavaDoc parentClassId = glAccountClass.getString("parentClassId");
192         if (parentClassId == null) {
193             return false;
194         }
195         if (parentClassId.equals(parentGlAccountClassId)) {
196             return true;
197         }
198
199         // otherwise, we have to go to the grandparent (recurse)
200
return isAccountClassClass(glAccountClass.getRelatedOneCache("ParentGlAccountClass"), parentGlAccountClassId);
201     }
202
203     /**
204      * Checks if a GL account is of a specified GlAccountClass.glAccountClassId. Returns false if account is null. It's better to use the
205      * more specific calls like isDebitAccount().
206      */

207     public static boolean isAccountClass(GenericValue glAccount, String JavaDoc glAccountClassId) throws GenericEntityException {
208         if (glAccount == null) {
209             return false;
210         }
211
212         GenericValue glAccountClass = glAccount.getRelatedOneCache("GlAccountClass");
213         if (glAccountClass == null) {
214             throw new GenericEntityException("Cannot find GlAccountClass for glAccountId " + glAccount.getString("glAccountId"));
215         }
216
217         return isAccountClassClass(glAccountClass, glAccountClassId);
218     }
219
220
221     public static boolean isDebitAccount(GenericValue account) throws GenericEntityException {
222         return isAccountClass(account, "DEBIT");
223     }
224
225     public static boolean isCreditAccount(GenericValue account) throws GenericEntityException {
226         return isAccountClass(account, "CREDIT");
227     }
228
229     public static boolean isAssetAccount(GenericValue account) throws GenericEntityException {
230         return isAccountClass(account, "ASSET");
231     }
232
233     public static boolean isLiabilityAccount(GenericValue account) throws GenericEntityException {
234         return isAccountClass(account, "LIABILITY");
235     }
236
237     public static boolean isEquityAccount(GenericValue account) throws GenericEntityException {
238         return isAccountClass(account, "EQUITY");
239     }
240
241     public static boolean isIncomeAccount(GenericValue account) throws GenericEntityException {
242         return isAccountClass(account, "INCOME");
243     }
244
245     public static boolean isRevenueAccount(GenericValue account) throws GenericEntityException {
246         return isAccountClass(account, "REVENUE");
247     }
248
249     public static boolean isExpenseAccount(GenericValue account) throws GenericEntityException {
250         return isAccountClass(account, "EXPENSE");
251     }
252 }
253
Popular Tags