1 18 19 package org.ofbiz.order.finaccount; 20 21 import java.math.BigDecimal ; 22 import java.sql.Timestamp ; 23 import java.util.List ; 24 import java.util.Random ; 25 import java.util.regex.Pattern ; 26 27 import org.ofbiz.base.util.Debug; 28 import org.ofbiz.base.util.UtilDateTime; 29 import org.ofbiz.base.util.UtilMisc; 30 import org.ofbiz.base.util.UtilNumber; 31 import org.ofbiz.entity.GenericDelegator; 32 import org.ofbiz.entity.GenericEntity; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericValue; 35 import org.ofbiz.entity.condition.EntityConditionList; 36 import org.ofbiz.entity.condition.EntityExpr; 37 import org.ofbiz.entity.condition.EntityOperator; 38 import org.ofbiz.entity.model.ModelEntity; 39 import org.ofbiz.entity.util.EntityUtil; 40 41 46 public class FinAccountHelper { 47 48 public static final String module = FinAccountHelper.class.getName(); 49 52 public static int decimals = UtilNumber.getBigDecimalScale("finaccount.decimals"); 53 public static int rounding = UtilNumber.getBigDecimalRoundingMode("finaccount.rounding"); 54 public static final BigDecimal ZERO = (new BigDecimal ("0.0")).setScale(decimals, rounding); 55 56 public static final String giftCertFinAccountTypeId = "GIFTCERT_ACCOUNT"; 57 public static final boolean defaultPinRequired = false; 58 59 static char[] char_pool = new char[10+26]; 61 static { 62 int j = 0; 63 for (int i = "0".charAt(0); i <= "9".charAt(0); i++) { 64 char_pool[j++] = (char) i; 65 } 66 for (int i = "A".charAt(0); i <= "Z".charAt(0); i++) { 67 char_pool[j++] = (char) i; 68 } 69 } 70 71 72 82 public static BigDecimal addFirstEntryAmount(BigDecimal initialValue, List transactions, String fieldName, int decimals, int rounding) throws GenericEntityException { 83 if ((transactions != null) && (transactions.size() == 1)) { 84 GenericValue firstEntry = (GenericValue) transactions.get(0); 85 if (firstEntry.get(fieldName) != null) { 86 BigDecimal valueToAdd = new BigDecimal (firstEntry.getDouble(fieldName).doubleValue()); 87 BigDecimal newValue = initialValue.add(valueToAdd).setScale(decimals, rounding); 88 return newValue; 89 } else { 90 return initialValue; 91 } 92 } else { 93 return initialValue; 94 } 95 } 96 97 104 public static String getNewFinAccountCode(int codeLength, GenericDelegator delegator) throws GenericEntityException { 105 106 Random r = new Random (); 108 boolean foundUniqueNewCode = false; 109 StringBuffer newAccountCode = null; 110 111 while (!foundUniqueNewCode) { 112 newAccountCode = new StringBuffer (codeLength); 113 for (int i = 0; i < codeLength; i++) { 114 newAccountCode.append(char_pool[(int) r.nextInt(char_pool.length)]); 115 } 116 117 List existingAccountsWithCode = delegator.findByAnd("FinAccount", UtilMisc.toMap("finAccountCode", newAccountCode.toString())); 118 if (existingAccountsWithCode.size() == 0) { 119 foundUniqueNewCode = true; 120 } 121 } 122 123 return newAccountCode.toString(); 124 } 125 126 133 public static GenericValue getFinAccountFromCode(String finAccountCode, GenericDelegator delegator) throws GenericEntityException { 134 if (finAccountCode == null) { 136 return null; 137 } 138 139 Pattern filterRegex = Pattern.compile("[^0-9A-Z]"); 140 finAccountCode = finAccountCode.toUpperCase().replaceAll(filterRegex.pattern(), ""); 141 142 ModelEntity finAccountEntity = delegator.getModelEntity("FinAccount"); 145 GenericEntity encryptedFinAccount = GenericEntity.createGenericEntity(finAccountEntity, UtilMisc.toMap("finAccountCode", finAccountCode)); 146 delegator.encryptFields(encryptedFinAccount); 147 String encryptedFinAccountCode = encryptedFinAccount.getString("finAccountCode"); 148 149 List accounts = delegator.findByAnd("FinAccount", UtilMisc.toMap("finAccountCode", encryptedFinAccountCode)); 151 accounts = EntityUtil.filterByDate(accounts); 152 153 if ((accounts == null) || (accounts.size() == 0)) { 154 Debug.logWarning("No fin account found for account code [" + finAccountCode + "]", module); 156 return null; 157 } else if (accounts.size() > 1) { 158 Debug.logError("Multiple fin accounts found", module); 160 return null; 161 } else { 162 return (GenericValue) accounts.get(0); 163 } 164 } 165 166 167 176 public static BigDecimal getBalance(String finAccountId, String currencyUomId, Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 177 BigDecimal incrementTotal = ZERO; BigDecimal decrementTotal = ZERO; 180 EntityConditionList incrementConditions = new EntityConditionList(UtilMisc.toList( 182 new EntityExpr("finAccountId", EntityOperator.EQUALS, finAccountId), 183 new EntityExpr("transactionDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime), 184 new EntityExpr("currencyUomId", EntityOperator.EQUALS, currencyUomId), 185 new EntityConditionList(UtilMisc.toList( 186 new EntityExpr("finAccountTransTypeId", EntityOperator.EQUALS, "DEPOSIT"), 187 new EntityExpr("finAccountTransTypeId", EntityOperator.EQUALS, "ADJUSTMENT")), 188 EntityOperator.OR)), 189 EntityOperator.AND); 190 List transSums = delegator.findByCondition("FinAccountTransSum", incrementConditions, UtilMisc.toList("amount"), null); 191 incrementTotal = addFirstEntryAmount(incrementTotal, transSums, "amount", (decimals+1), rounding); 192 193 EntityConditionList decrementConditions = new EntityConditionList(UtilMisc.toList( 195 new EntityExpr("finAccountId", EntityOperator.EQUALS, finAccountId), 196 new EntityExpr("transactionDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime), 197 new EntityExpr("currencyUomId", EntityOperator.EQUALS, currencyUomId), 198 new EntityExpr("finAccountTransTypeId", EntityOperator.EQUALS, "WITHDRAWAL")), 199 EntityOperator.AND); 200 transSums = delegator.findByCondition("FinAccountTransSum", decrementConditions, UtilMisc.toList("amount"), null); 201 decrementTotal = addFirstEntryAmount(decrementTotal, transSums, "amount", (decimals+1), rounding); 202 203 BigDecimal netBalance = incrementTotal.subtract(decrementTotal).setScale(decimals, rounding); 205 return netBalance; 206 } 207 208 216 public static BigDecimal getBalance(String finAccountId, String currencyUomId, GenericDelegator delegator) throws GenericEntityException { 217 return getBalance(finAccountId, currencyUomId, UtilDateTime.nowTimestamp(), delegator); 218 } 219 220 229 public static BigDecimal getAvailableBalance(String finAccountId, String currencyUomId, Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 230 BigDecimal netBalance = getBalance(finAccountId, currencyUomId, asOfDateTime, delegator); 231 232 EntityConditionList authorizationConditions = new EntityConditionList(UtilMisc.toList( 234 new EntityExpr("finAccountId", EntityOperator.EQUALS, finAccountId), 235 new EntityExpr("authorizationDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime), 236 new EntityExpr("currencyUomId", EntityOperator.EQUALS, currencyUomId), 237 EntityUtil.getFilterByDateExpr(asOfDateTime)), 238 EntityOperator.AND); 239 240 List authSums = delegator.findByCondition("FinAccountAuthSum", authorizationConditions, UtilMisc.toList("amount"), null); 241 242 BigDecimal authorizationsTotal = addFirstEntryAmount(ZERO, authSums, "amount", (decimals+1), rounding); 243 244 BigDecimal netAvailableBalance = netBalance.subtract(authorizationsTotal).setScale(decimals, rounding); 246 return netAvailableBalance; 247 } 248 249 257 public static BigDecimal getAvailableBalance(String finAccountId, String currencyUomId, GenericDelegator delegator) throws GenericEntityException { 258 return getAvailableBalance(finAccountId, currencyUomId, UtilDateTime.nowTimestamp(), delegator); 259 } 260 } 261 | Popular Tags |