diff --git a/modules/minigl/src/main/java/org/jpos/gl/AccountDetail.java b/modules/minigl/src/main/java/org/jpos/gl/AccountDetail.java index 4b334120b0..2a5c12eff0 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/AccountDetail.java +++ b/modules/minigl/src/main/java/org/jpos/gl/AccountDetail.java @@ -44,26 +44,50 @@ public class AccountDetail { * Constructs an AccountDetail. * @param journal the Journal. * @param account the account. - * @param initialBalance initial balance (reporting currency). + * @param balance balance (reporting currency), could be initial if naturalOrder or final if not. * @param start start date (inclusive). * @param end end date (inclusive). * @param entries list of GLEntries. * @param layers the layers involved in this detail + * @param ascendingOrder if we should compute balance normally or inverted */ public AccountDetail( Journal journal, Account account, - BigDecimal initialBalance, - Date start, Date end, List entries, short[] layers) + BigDecimal balance, + Date start, Date end, List entries, short[] layers, boolean ascendingOrder) { super(); this.journal = journal; this.account = account; - this.initialBalance = initialBalance; this.start = start; this.end = end; this.entries = entries; this.layers = layers; - computeBalances(); + if (ascendingOrder) { + this.initialBalance = balance; + computeBalances(); + } else { + this.finalBalance = balance; + computeReverseBalances(balance); + } + } + + /** + * Constructs an AccountDetail. + * @param journal the Journal. + * @param account the account. + * @param balance balance (reporting currency), could be initial if naturalOrder or final if not. + * @param start start date (inclusive). + * @param end end date (inclusive). + * @param entries list of GLEntries. + * @param layers the layers involved in this detail + */ + public AccountDetail( + Journal journal, Account account, + BigDecimal balance, + Date start, Date end, List entries, short[] layers) + { + this(journal, account, balance, start, end, entries, layers, true); } /** @@ -79,13 +103,7 @@ public AccountDetail( BigDecimal balance, List entries, short[] layers) { - super(); - this.journal = journal; - this.account = account; - this.finalBalance = balance; - this.entries = entries; - this.layers = layers; - computeReverseBalances(balance); + this(journal, account, balance, null, null, entries, layers, false); } public Journal getJournal() { diff --git a/modules/minigl/src/main/java/org/jpos/gl/GLSession.java b/modules/minigl/src/main/java/org/jpos/gl/GLSession.java index 68d260469d..5c96813e9e 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/GLSession.java +++ b/modules/minigl/src/main/java/org/jpos/gl/GLSession.java @@ -1092,17 +1092,17 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc * @param acct the account. * @param start date (inclusive). * @param end date (inclusive). + * @param layers array of the layers included. + * @param ascendingOrder boolean. + * @param maxResults int. * @return Account detail for given period. * @throws GLException if user doesn't have READ permission on this journal. */ public AccountDetail getAccountDetail - (Journal journal, Account acct, Date start, Date end, short[] layers) + (Journal journal, Account acct, Date start, Date end, short[] layers, boolean ascendingOrder, int maxResults) throws HibernateException, GLException { checkPermission (GLPermission.READ); - start = Util.floor (start); - end = Util.ceil (end); - Criteria crit = session.createCriteria (GLEntry.class); boolean hasChildren = false; @@ -1123,21 +1123,55 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc crit.add (Restrictions.in ("layer", (Object[])toShortArray (layers))); crit = crit.createCriteria ("transaction") - .add (Restrictions.eq ("journal", journal)) - .add (Restrictions.ge ("postDate", start)) - .add (Restrictions.le ("postDate", end)); + .add (Restrictions.eq ("journal", journal)); + if (start != null || (start == null && ascendingOrder)) { + start = Util.floor(start); + crit.add (Restrictions.ge ("postDate", start)); + } + if (end != null || (end == null && ascendingOrder)) { + end = Util.ceil(end); + crit.add (Restrictions.le ("postDate", end)); + } - BigDecimal initialBalance[] = getBalances (journal, acct, start, false, layers, 0L); - crit.addOrder (Order.asc ("postDate")); - crit.addOrder (Order.asc ("timestamp")); - crit.addOrder (Order.asc ("id")); - List entries = crit.list(); - // BigDecimal finalBalance = applyEntries (initialBalance[0], entries); + if (maxResults > 0) + crit.setMaxResults(maxResults); + + long maxEntry = 0L; + List entries; + BigDecimal initialBalance[]; + if (ascendingOrder) { + crit.addOrder (Order.asc ("postDate")); + crit.addOrder (Order.asc ("timestamp")); + crit.addOrder (Order.asc ("id")); + entries = crit.list(); + initialBalance = getBalances(journal, acct, start, false, layers, maxEntry); + } else { + crit.addOrder (Order.desc ("postDate")); + crit.addOrder (Order.desc ("timestamp")); + crit.addOrder (Order.desc ("id")); + entries = crit.list(); + if (entries.size() > 0) { + maxEntry = entries.get(0).getId(); + } + initialBalance = getBalances(journal, acct, end, true, layers, maxEntry); + } + return new AccountDetail(journal, acct, initialBalance[0], start, end, entries, layers, ascendingOrder); + } - return new AccountDetail ( - journal, acct, - initialBalance[0], - start, end, entries, layers ); + /** + * AccountDetail for date range + * @param journal the journal. + * @param acct the account. + * @param start date (inclusive). + * @param end date (inclusive). + * @return Account detail for given period. + * @throws GLException if user doesn't have READ permission on this journal. + */ + public AccountDetail getAccountDetail + (Journal journal, Account acct, Date start, Date end, short[] layers) + throws HibernateException, GLException + { + return getAccountDetail(journal, acct, start, end, layers, true, 0); } /** @@ -1153,37 +1187,7 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc (Journal journal, Account acct, short[] layers, int maxResults) throws HibernateException, GLException { - checkPermission (GLPermission.READ); - Criteria crit = session.createCriteria (GLEntry.class); - - boolean hasChildren = false; - if (acct.isCompositeAccount()) { - Disjunction dis = Restrictions.disjunction(); - for (Long l : getChildren (acct)) { - hasChildren = true; - dis.add (Restrictions.idEq(l)); - } - if (hasChildren) { - Criteria subCrit = crit.createCriteria(("account")); - subCrit.add (dis); - } - } - if (!hasChildren) { - crit.add (Restrictions.eq ("account", acct)); - } - - crit.add (Restrictions.in ("layer", (Object[])toShortArray (layers))); - crit = crit.createCriteria ("transaction") - .add (Restrictions.eq ("journal", journal)); - - crit.addOrder (Order.desc ("id")); - crit.setMaxResults(maxResults); - List entries = crit.list(); - BigDecimal balance = ZERO; - if (entries.size() > 0) - balance = getBalances(journal, acct, (Date) null, true, layers, entries.get(0).getId())[0]; - - return new AccountDetail(journal, acct, balance, entries, layers); + return getAccountDetail(journal, acct, null, null, layers, false, maxResults); }