📄 financialservices.java
字号:
*/ public static Map getIncomeStatementAccountSumsByDate(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); Timestamp fromDate = (Timestamp) context.get("fromDate"); Timestamp thruDate = (Timestamp) context.get("thruDate"); String organizationPartyId = (String) context.get("organizationPartyId"); String glFiscalTypeId = (String) context.get("glFiscalTypeId"); GenericValue userLogin = (GenericValue) context.get("userLogin"); try { // first find all accounting transaction entries for this organizationPartyId and following into this time period which are // income statement transactions (REVENUE, EXPENSE, INCOME). List transactionEntries = null; Map tmpResult = dispatcher.runSync("getAcctgTransAndEntriesByType", UtilMisc.toMap("organizationPartyId", organizationPartyId, "fromDate", fromDate, "thruDate", thruDate, "glFiscalTypeId", glFiscalTypeId, "glAccountClasses", UtilMisc.toList("REVENUE", "EXPENSE", "INCOME"), "userLogin", userLogin)); if ((tmpResult != null) && (tmpResult.get("transactionEntries") != null)) { transactionEntries = (List) tmpResult.get("transactionEntries"); } // very important - we do not want the PERIOD_CLOSING transactions as this will duplicate the net income transactionEntries = EntityUtil.filterOutByCondition(transactionEntries, new EntityExpr("acctgTransTypeId", EntityOperator.EQUALS, "PERIOD_CLOSING")); // now add them up by account. since we may have to deal with flipping the signs of transactions, we'd have analysis type of // transaction (debit/credit) vs class of account (debit/credit), so it wasn't possible to just use a view-entity to sum it all up Map glAccountSums = new HashMap(); tmpResult = dispatcher.runSync("addToAccountBalances", UtilMisc.toMap("glAccountSums", glAccountSums, "transactionEntries", transactionEntries, "userLogin", userLogin)); if (tmpResult.get("glAccountSums") == null) { return ServiceUtil.returnError("Cannot sum up account balances properly for income statement"); } else { Map result = ServiceUtil.returnSuccess(); result.put("glAccountSums", glAccountSums); return result; } } catch (GenericServiceException ex) { return(ServiceUtil.returnError(ex.getMessage())); } } /** * Takes an initial Map of GlAccount, sums and a List of AcctgTransAndEntries and adds them to the Map, * based on debit/credit flag of transaction transactionEntry and whether the account is a debit or credit account. * Useful for doing income statement and intra-time-period updating of balance sheets, etc. */ public static Map addToAccountBalances(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); Map glAccountSums = (Map) context.get("glAccountSums"); List transactionEntries = (List) context.get("transactionEntries"); /* for each transaction entry, determine if the amount to be added needs to have its sign reversed, which is the * case if the account and the debit/credit flag are opposite (ie, debit to a credit account or credit to a debit account.) * then, add that amount to the glAccountSums map. Note that the key of glAccountSums is actually the GlAccount entity, * not a glAccountId. */ try { for (Iterator tEi = transactionEntries.iterator(); tEi.hasNext(); ) { GenericValue transactionEntry = (GenericValue) tEi.next(); GenericValue account = transactionEntry.getRelatedOne("GlAccount"); double amountToAdd = transactionEntry.getDouble("amount").doubleValue(); if ((UtilAccounting.isDebitAccount(account) && (transactionEntry.getString("debitCreditFlag").equals("C"))) || (UtilAccounting.isCreditAccount(account) && (transactionEntry.getString("debitCreditFlag").equals("D")))) { amountToAdd = -amountToAdd; } UtilMisc.addToDoubleInMap(glAccountSums, transactionEntry.getRelatedOne("GlAccount"), new Double(amountToAdd)); } Map result = ServiceUtil.returnSuccess(); result.put("glAccountSums", glAccountSums); return result; } catch (GenericEntityException ex) { return(ServiceUtil.returnError(ex.getMessage())); } } /** * Little method to help out getBalanceSheetForDate. Gets all AcctgTransAndEntries of a glAccountClassId and add them to the * original accountBalances Map, which is returned. * @param accountBalances * @param organizationPartyId * @param fromDate * @param thruDate * @param glFiscalTypeId * @param glAccountClassId * @param userLogin * @return */ private static Map getAcctgTransAndEntriesForClass(Map accountBalances, String organizationPartyId, Timestamp fromDate, Timestamp thruDate, String glFiscalTypeId, String glAccountClassId, GenericValue userLogin, LocalDispatcher dispatcher) { try { // first get all the AcctgTransAndEntries of this glAccountClassId Map tmpResult = dispatcher.runSync("getAcctgTransAndEntriesByType", UtilMisc.toMap("organizationPartyId", organizationPartyId, "fromDate", fromDate, "thruDate", thruDate, "glFiscalTypeId", glFiscalTypeId, "glAccountClasses", UtilMisc.toList(glAccountClassId), "userLogin", userLogin)); List transactionEntries = (List) tmpResult.get("transactionEntries"); // now add it to accountBalances tmpResult = dispatcher.runSync("addToAccountBalances", UtilMisc.toMap("glAccountSums", accountBalances, "transactionEntries", transactionEntries, "userLogin", userLogin)); accountBalances = (Map) tmpResult.get("glAccountSums"); return accountBalances; } catch (GenericServiceException ex) { Debug.logError(ex.getMessage(), module); return null; } } public static Map getComparativeBalanceSheet(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); // input parameters String organizationPartyId = (String) context.get("organizationPartyId"); String glFiscalTypeId = (String) context.get("glFiscalTypeId"); Timestamp fromDate = (Timestamp) context.get("fromDate"); Timestamp thruDate = (Timestamp) context.get("thruDate"); GenericValue userLogin = (GenericValue) context.get("userLogin"); // validate the from/thru date if (fromDate.after(thruDate)) { return ServiceUtil.returnError("Cannot create a comparative balance for a from date that is after the thru date!"); } try { // create the balance sheet for the fromDate Map input = UtilMisc.toMap("organizationPartyId", organizationPartyId, "glFiscalTypeId", glFiscalTypeId, "userLogin", userLogin); input.put("asOfDate", fromDate); Map fromDateResults = dispatcher.runSync("getBalanceSheetForDate", input); if (ServiceUtil.isError(fromDateResults)) { return ServiceUtil.returnError("Failed to create comparative balance sheet.", null, null, fromDateResults); } // create the balance sheet for the thruDate input.put("asOfDate", thruDate); Map thruDateResults = dispatcher.runSync("getBalanceSheetForDate", input); if (ServiceUtil.isError(thruDateResults)) { return ServiceUtil.returnError("Failed to create comparative balance sheet.", null, null, thruDateResults); } Map results = ServiceUtil.returnSuccess(); // include the two balance sheets in the results results.put("fromDateAccountBalances", fromDateResults); results.put("thruDateAccountBalances", thruDateResults); // compute the balance difference for each type of account results.put("liabilityAccountBalances", calculateDifferenceBalance((Map) fromDateResults.get("liabilityAccountBalances"), (Map) thruDateResults.get("liabilityAccountBalances"))); results.put("assetAccountBalances", calculateDifferenceBalance((Map) fromDateResults.get("assetAccountBalances"), (Map) thruDateResults.get("assetAccountBalances"))); results.put("equityAccountBalances", calculateDifferenceBalance((Map) fromDateResults.get("equityAccountBalances"), (Map) thruDateResults.get("equityAccountBalances"))); return results; } catch (GenericServiceException e) { Debug.logError(e, "Failed to compute comparative balance: " + e.getMessage(), module); return ServiceUtil.returnError("Failed to compute comparative balance: " + e.getMessage()); } } /** * Calculates the difference between the two balances for the input maps of {account, fromDateBalance} and {account, thruDateBalance} */ private static Map calculateDifferenceBalance(Map fromDateMap, Map thruDateMap) { Map resultMap = FastMap.newInstance(); // first iteratee through the thru date accounts for (Iterator iter = thruDateMap.keySet().iterator(); iter.hasNext(); ) { GenericValue account = (GenericValue) iter.next(); Double thruDateBalance = (Double) thruDateMap.get(account); Double fromDateBalance = (Double) fromDateMap.get(account); if (thruDateBalance == null) thruDateBalance = new Double(0); if (fromDateBalance == null) fromDateBalance = new Double(0); double difference = thruDateBalance.doubleValue() - fromDateBalance.doubleValue(); resultMap.put(account, (new BigDecimal(difference)).setScale(decimals, rounding)); } // iterate through the from date accounts that were missed because no thru date account exists for (Iterator iter = fromDateMap.keySet().iterator(); iter.hasNext(); ) { GenericValue account = (GenericValue) iter.next(); if (resultMap.get(account) != null) continue; // already have a balance Double fromDateBalance = (Double) fromDateMap.get(account); if (fromDateBalance == null) fromDateBalance = new Double(0); double difference = 0 - fromDateBalance.doubleValue(); resultMap.put(account, (new BigDecimal(difference)).setScale(decimals, rounding)); } return resultMap; } /** * Helper method to transform input from and thru time periods to fromDate and thruDates. It will then call * the given service with the given input map and handle service/entity exceptions. */ private static Map reportServiceTimePeriodHelper(DispatchContext dctx, Map context, String serviceName, Map input) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); String fromTimePeriodId = (String) context.get("fromTimePeriodId"); String thruTimePeriodId = (String) context.get("thruTimePeriodId"); String organizationPartyId = (String) input.get("organizationPartyId"); try { // get the right start and ending custom time periods GenericValue fromTimePeriod = delegator.findByPrimaryKey("CustomTimePeriod", UtilMisc.toMap("customTimePeriodId", fromTimePeriodId)); GenericValue thruTimePeriod = delegator.findByPrimaryKey("CustomTimePeriod", UtilMisc.toMap("customTimePeriodId", thruTimePeriodId)); // make sure these time periods belong to the organization and the from and thru dates are there if (fromTimePeriod == null) { return ServiceUtil.returnError("Custom time period " + fromTimePeriodId + " does not exist"); } if (thruTimePeriod == null) { return ServiceUtil.returnError("Custom time period " + thruTimePeriodId + " does not exist"); } if (!(fromTimePeriod.getString("organizationPartyId").equals(organizationPartyId))) { return ServiceUtil.returnError("Custom time period " + fromTimePeriodId + " does not belong to " + organizationPartyId); } if (!(thruTimePeriod.getString("organizationPartyId").equals(organizationPartyId))) { return ServiceUtil.returnError("Custom time period " + thruTimePeriodId + " does not belong to " + organizationPartyId); } if (fromTimePeriod.get("fromDate") == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -