1 16 17 package com.opensourcestrategies.financials.accounts; 18 19 import java.math.BigDecimal ; 20 import java.sql.Timestamp ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javolution.util.FastMap; 27 28 import org.ofbiz.accounting.invoice.InvoiceWorker; 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilDateTime; 31 import org.ofbiz.base.util.UtilMisc; 32 import org.ofbiz.base.util.UtilNumber; 33 import org.ofbiz.entity.GenericDelegator; 34 import org.ofbiz.entity.GenericEntityException; 35 import org.ofbiz.entity.GenericValue; 36 import org.ofbiz.entity.condition.EntityConditionList; 37 import org.ofbiz.entity.condition.EntityExpr; 38 import org.ofbiz.entity.condition.EntityOperator; 39 import com.opensourcestrategies.financials.invoice.InvoiceWithOutstandingBalance; 40 41 public class AccountsHelper { 42 43 public static String module = AccountsHelper.class.getName(); 44 45 public static int decimals = UtilNumber.getBigDecimalScale("fin_arithmetic.properties", "financial.statements.decimals"); 46 public static int rounding = UtilNumber.getBigDecimalRoundingMode("fin_arithmetic.properties", "financial.statements.rounding"); 47 protected static final BigDecimal ZERO = new BigDecimal ("0"); protected static final BigDecimal MILLISECONDS_PER_DAY = new BigDecimal ("86400000"); 49 50 65 public static List getAcctgTransPartySums(String organizationPartyId, String glAccountTypeId, String glFiscalTypeId, String debitCreditFlag, 66 String partyId, String roleTypeId, Timestamp asOfDate, GenericDelegator delegator) throws GenericEntityException { 67 68 List conditions = UtilMisc.toList( 69 new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId), 70 new EntityExpr("debitCreditFlag", EntityOperator.EQUALS, debitCreditFlag), 71 new EntityExpr("isPosted", EntityOperator.EQUALS, "Y"), 72 new EntityExpr("glAccountTypeId", EntityOperator.EQUALS, glAccountTypeId), 73 new EntityExpr("glFiscalTypeId", EntityOperator.EQUALS, glFiscalTypeId), 74 new EntityExpr("transactionDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDate)); 75 if (partyId != null) { 76 conditions.add(new EntityExpr("partyId", EntityOperator.EQUALS, partyId)); 77 } 78 if (roleTypeId != null) { 79 conditions.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, roleTypeId)); 80 } 81 82 EntityConditionList findConditions = new EntityConditionList(conditions, EntityOperator.AND); 83 84 List fieldsToGet = UtilMisc.toList("partyId", "amount"); 85 86 List transactionEntries = delegator.findByCondition("AcctgTransEntryPartySum", findConditions, 87 fieldsToGet, UtilMisc.toList("partyId")); 90 return transactionEntries; 91 } 92 93 104 public static Map getBalancesHelper(String glAccountTypeId, String organizationPartyId, String partyId, String glFiscalTypeId, 105 Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 106 107 boolean isReceivable = (glAccountTypeId.equals("ACCOUNTS_RECEIVABLE") ? true : false); 109 String roleTypeId = (isReceivable ? "BILL_TO_CUSTOMER" : "BILL_FROM_VENDOR"); 110 111 List debitBalances = getAcctgTransPartySums(organizationPartyId, glAccountTypeId, glFiscalTypeId, "D", partyId, roleTypeId, 113 asOfDateTime, delegator); 114 List creditBalances = getAcctgTransPartySums(organizationPartyId, glAccountTypeId, glFiscalTypeId, "C", partyId, roleTypeId, 115 asOfDateTime, delegator); 116 117 Map balances = FastMap.newInstance(); 119 120 for (Iterator iter = debitBalances.iterator(); iter.hasNext(); ) { 122 GenericValue balance = (GenericValue) iter.next(); 123 BigDecimal balanceAmount = balance.getBigDecimal("amount").setScale(decimals, rounding); 124 if (!isReceivable) balanceAmount = ZERO.subtract(balanceAmount); 125 balances.put(balance.get("partyId"), balanceAmount); 126 } 127 128 for (Iterator iter = creditBalances.iterator(); iter.hasNext(); ) { 130 GenericValue balance = (GenericValue) iter.next(); 131 BigDecimal balanceAmount = balance.getBigDecimal("amount").setScale(decimals, rounding); 132 if (isReceivable) balanceAmount = ZERO.subtract(balanceAmount); 133 134 BigDecimal debitBalance = (BigDecimal ) balances.get(balance.get("partyId")); 136 if (debitBalance == null) debitBalance = ZERO; 137 138 balanceAmount = balanceAmount.add(debitBalance); 140 balances.put(balance.get("partyId"), balanceAmount); 141 } 142 143 return balances; 144 } 145 146 147 public static Map getBalancesForAllCustomers(String organizationPartyId, String glFiscalTypeId, Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 148 return getBalancesHelper("ACCOUNTS_RECEIVABLE", organizationPartyId, null, glFiscalTypeId, asOfDateTime, delegator); 149 } 150 151 152 public static Map getBalancesForAllVendors(String organizationPartyId, String glFiscalTypeId, Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 153 return getBalancesHelper("ACCOUNTS_PAYABLE", organizationPartyId, null, glFiscalTypeId, asOfDateTime, delegator); 154 } 155 156 157 public static BigDecimal getBalanceForCustomerPartyId(String customerPartyId, String organizationPartyId, String glFiscalTypeId, Timestamp asOfDateTime, GenericDelegator delegator) 158 throws GenericEntityException { 159 return (BigDecimal ) getBalancesHelper("ACCOUNTS_RECEIVABLE", organizationPartyId, customerPartyId, glFiscalTypeId, asOfDateTime, delegator).get(customerPartyId); 160 } 161 162 163 public static BigDecimal getBalanceForVendorPartyId(String vendorPartyId, String organizationPartyId, String glFiscalTypeId, Timestamp asOfDateTime, GenericDelegator delegator) 164 throws GenericEntityException { 165 return (BigDecimal ) getBalancesHelper("ACCOUNTS_PAYABLE", organizationPartyId, vendorPartyId, glFiscalTypeId, asOfDateTime, delegator).get(vendorPartyId); 166 } 167 168 172 public static Map getUnpaidInvoicesForCustomers(String organizationPartyId, List daysOutstandingPoints, Timestamp asOfDateTime, GenericDelegator delegator) 173 throws GenericEntityException { 174 return getUnpaidInvoicesHelper(organizationPartyId, "SALES_INVOICE", daysOutstandingPoints, asOfDateTime, delegator); 175 } 176 177 181 public static Map getUnpaidInvoicesForVendors(String organizationPartyId, List daysOutstandingPoints, Timestamp asOfDateTime, GenericDelegator delegator) 182 throws GenericEntityException { 183 return getUnpaidInvoicesHelper(organizationPartyId, "PURCHASE_INVOICE", daysOutstandingPoints, asOfDateTime, delegator); 184 } 185 186 198 public static Map getUnpaidInvoicesHelper(String organizationPartyId, String invoiceTypeId, List daysOutstandingPoints, 199 Timestamp asOfDateTime, GenericDelegator delegator) throws GenericEntityException { 200 201 Timestamp now = UtilDateTime.getDayEnd( UtilDateTime.nowTimestamp() ); 203 if (asOfDateTime.after(now)) asOfDateTime = now; 204 205 String organizationInvoiceField = null; 207 if (invoiceTypeId.equals("PURCHASE_INVOICE")) { 208 organizationInvoiceField = "partyId"; 209 } else { 210 organizationInvoiceField = "partyIdFrom"; 211 } 212 213 EntityConditionList invoiceDateConditions = new EntityConditionList(UtilMisc.toList( 216 new EntityExpr("paidDate", EntityOperator.GREATER_THAN, asOfDateTime), 217 new EntityExpr("paidDate", EntityOperator.EQUALS, null)), 218 EntityOperator.OR); 219 220 EntityConditionList conditions = new EntityConditionList(UtilMisc.toList( 221 new EntityExpr("invoiceTypeId", EntityOperator.EQUALS, invoiceTypeId), 222 new EntityExpr("invoiceDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime), 223 new EntityExpr(organizationInvoiceField, EntityOperator.EQUALS, organizationPartyId), 224 invoiceDateConditions), 225 EntityOperator.AND); 226 227 List invoices = delegator.findByCondition("Invoice", conditions, null, UtilMisc.toList("invoiceDate")); 229 230 Map outstandingInvoicesByAge = FastMap.newInstance(); 232 int maxDaysOutstanding = 0; 233 for (Iterator it = daysOutstandingPoints.iterator(); it.hasNext(); ) { 234 Integer nextDaysOutstanding = (Integer ) it.next(); 235 outstandingInvoicesByAge.put(nextDaysOutstanding, new LinkedList ()); 236 if (nextDaysOutstanding.intValue() > maxDaysOutstanding) { 237 maxDaysOutstanding = nextDaysOutstanding.intValue(); 238 } 239 } 240 241 Iterator iter = invoices.iterator(); 243 while (iter.hasNext()) { 244 GenericValue invoice = (GenericValue) iter.next(); 245 BigDecimal balance = InvoiceWorker.getInvoiceNotApplied(invoice, asOfDateTime); 246 247 if (balance.compareTo(ZERO) == 0) continue; 249 250 BigDecimal numberOfDays = new BigDecimal (UtilDateTime.getInterval(invoice.getTimestamp("invoiceDate"), asOfDateTime)).divide(MILLISECONDS_PER_DAY, decimals, rounding); 252 253 boolean isPastDue = false; 255 if ((invoice.getTimestamp("dueDate") != null) && (invoice.getTimestamp("dueDate").before(asOfDateTime))) { 256 isPastDue = true; 257 } 258 InvoiceWithOutstandingBalance invoiceWithBalance = new InvoiceWithOutstandingBalance(invoice, balance, isPastDue); 259 260 boolean foundDaysOutstandingPoint = false; 263 Iterator it = daysOutstandingPoints.iterator(); 264 while ((it.hasNext()) && (!foundDaysOutstandingPoint) ) { 265 Integer daysOutstandingPoint = (Integer ) it.next(); 266 if (numberOfDays.compareTo(new BigDecimal (daysOutstandingPoint.intValue())) == -1) { List invoicesByDaysOutstanding = (LinkedList ) outstandingInvoicesByAge.get(daysOutstandingPoint); 268 invoicesByDaysOutstanding.add(invoiceWithBalance); 269 foundDaysOutstandingPoint = true; 270 } 271 } 272 if (!foundDaysOutstandingPoint) { 273 List invoicesByDaysOutstanding = (LinkedList ) outstandingInvoicesByAge.get(new Integer (maxDaysOutstanding)); 274 invoicesByDaysOutstanding.add(invoiceWithBalance); 275 } 276 } 277 return outstandingInvoicesByAge; 278 } 279 } 280 | Popular Tags |