1 27 28 package org.ofbiz.accounting.util; 29 30 import java.math.BigDecimal ; 31 import java.util.List ; 32 import java.util.Map ; 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 module = UtilAccounting.class.getName(); 45 46 58 public static String getProductOrgGlAccountId(String productId, 59 String glAccountTypeId, String organizationPartyId, GenericDelegator delegator) 60 throws AccountingException { 61 62 GenericValue account = null; 63 try { 64 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 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 (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 return account.getString("glAccountId"); 87 } 88 89 97 public static String getDefaultAccountId(String glAccountTypeId, String organizationPartyId, GenericDelegator delegator) throws AccountingException { 98 return getProductOrgGlAccountId(null, glAccountTypeId, organizationPartyId, delegator); 99 } 100 101 107 public static Double getNetBalance(GenericValue account, String 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 (balance); 117 } catch (GenericEntityException ex) { 118 Debug.logError(ex.getMessage(), debugModule); 119 return null; 120 } 121 } 122 123 126 private static boolean isPaymentTypeRecurse(GenericValue paymentType, String inputTypeId) throws GenericEntityException { 127 128 String parentTypeId = paymentType.getString("parentTypeId"); 130 if (parentTypeId == null) { 131 return false; 132 } 133 if (parentTypeId.equals(inputTypeId)) { 134 return true; 135 } 136 137 return isPaymentTypeRecurse(paymentType.getRelatedOne("ParentPaymentType"), inputTypeId); 139 } 140 141 142 146 public static boolean isPaymentType(GenericValue payment, String 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 paymentTypeId = paymentType.getString("paymentTypeId"); 157 if (inputTypeId.equals(paymentTypeId)) { 158 return true; 159 } 160 161 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 182 public static boolean isAccountClassClass(GenericValue glAccountClass, String parentGlAccountClassId) throws GenericEntityException { 183 if (glAccountClass == null) return false; 184 185 if (parentGlAccountClassId.equals(glAccountClass.get("glAccountClassId"))) { 187 return true; 188 } 189 190 String parentClassId = glAccountClass.getString("parentClassId"); 192 if (parentClassId == null) { 193 return false; 194 } 195 if (parentClassId.equals(parentGlAccountClassId)) { 196 return true; 197 } 198 199 return isAccountClassClass(glAccountClass.getRelatedOneCache("ParentGlAccountClass"), parentGlAccountClassId); 201 } 202 203 207 public static boolean isAccountClass(GenericValue glAccount, String 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 |