From f0258d5d147e808a3c3044c4b5b5e70ae4e8cc1b Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Tue, 12 Jun 2018 15:14:45 -0300 Subject: [PATCH 1/4] getAccountDetail allows set order and max entries --- .../main/java/org/jpos/gl/AccountDetail.java | 42 ++++++++++----- .../src/main/java/org/jpos/gl/GLSession.java | 53 ++++++++++++++----- 2 files changed, 70 insertions(+), 25 deletions(-) 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..639e094bbb 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,18 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc * @param acct the account. * @param start date (inclusive). * @param end date (inclusive). + * @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; @@ -1127,17 +1128,43 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc .add (Restrictions.ge ("postDate", start)) .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); - return new AccountDetail ( - journal, acct, - initialBalance[0], - start, end, entries, layers ); + long maxEntry = 0L; + List entries; + if (ascendingOrder) { + crit.addOrder (Order.asc ("postDate")); + crit.addOrder (Order.asc ("timestamp")); + crit.addOrder (Order.asc ("id")); + entries = crit.list(); + } 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(); + } + } + BigDecimal initialBalance[] = getBalances(journal, acct, start, false, layers, maxEntry); + return new AccountDetail(journal, acct, initialBalance[0], start, end, entries, layers, ascendingOrder); + } + + /** + * 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); } /** @@ -1183,7 +1210,7 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc 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 new AccountDetail(journal, acct, balance, null, null, entries, layers, false); } From 5cc7f641125595c0b89b5effbc94deace8c30523 Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Thu, 14 Jun 2018 13:49:42 -0300 Subject: [PATCH 2/4] Re-use getAccountDetail in getMiniStatement --- .../src/main/java/org/jpos/gl/GLSession.java | 46 ++++--------------- 1 file changed, 10 insertions(+), 36 deletions(-) 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 639e094bbb..39208be8fd 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/GLSession.java +++ b/modules/minigl/src/main/java/org/jpos/gl/GLSession.java @@ -1102,8 +1102,6 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc throws HibernateException, GLException { checkPermission (GLPermission.READ); - start = Util.floor (start); - end = Util.ceil (end); Criteria crit = session.createCriteria (GLEntry.class); boolean hasChildren = false; @@ -1124,9 +1122,15 @@ 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 = Util.floor(start); + crit.add (Restrictions.ge ("postDate", start)); + } + if (end != null) { + end = Util.ceil(end); + crit.add (Restrictions.le ("postDate", end)); + } if (maxResults > 0) crit.setMaxResults(maxResults); @@ -1180,37 +1184,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, null, null, entries, layers, false); + return getAccountDetail(journal, acct, null, null, layers, false, maxResults); } From 0a772fb01fc5f3cf202023739a29514439fc79e2 Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Thu, 14 Jun 2018 16:56:18 -0300 Subject: [PATCH 3/4] Fix initialBalance calculation when order desc --- modules/minigl/src/main/java/org/jpos/gl/GLSession.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 39208be8fd..c29f47a1be 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/GLSession.java +++ b/modules/minigl/src/main/java/org/jpos/gl/GLSession.java @@ -1123,11 +1123,11 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc crit.add (Restrictions.in ("layer", (Object[])toShortArray (layers))); crit = crit.createCriteria ("transaction") .add (Restrictions.eq ("journal", journal)); - if (start != null) { + if (start != null || (start == null && ascendingOrder)) { start = Util.floor(start); crit.add (Restrictions.ge ("postDate", start)); } - if (end != null) { + if (end != null || (end == null && ascendingOrder)) { end = Util.ceil(end); crit.add (Restrictions.le ("postDate", end)); } @@ -1137,11 +1137,13 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc 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")); @@ -1150,8 +1152,8 @@ public List getDeepFinalChildren(Account acct) throws HibernateExc if (entries.size() > 0) { maxEntry = entries.get(0).getId(); } + initialBalance = getBalances(journal, acct, end, true, layers, maxEntry); } - BigDecimal initialBalance[] = getBalances(journal, acct, start, false, layers, maxEntry); return new AccountDetail(journal, acct, initialBalance[0], start, end, entries, layers, ascendingOrder); } From 05f8ddf0c78dad2b2d368091866eb78e3d77946f Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Mon, 18 Jun 2018 16:02:31 -0300 Subject: [PATCH 4/4] Add javadocs of layer param --- modules/minigl/src/main/java/org/jpos/gl/GLSession.java | 1 + 1 file changed, 1 insertion(+) 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 c29f47a1be..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,6 +1092,7 @@ 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.